Repository: hive
Updated Branches:
  refs/heads/master bcbd2d529 -> f20311b00


http://git-wip-us.apache.org/repos/asf/hive/blob/f20311b0/standalone-metastore/src/test/java/org/apache/hadoop/hive/metastore/InjectableBehaviourObjectStore.java
----------------------------------------------------------------------
diff --git 
a/standalone-metastore/src/test/java/org/apache/hadoop/hive/metastore/InjectableBehaviourObjectStore.java
 
b/standalone-metastore/src/test/java/org/apache/hadoop/hive/metastore/InjectableBehaviourObjectStore.java
index fdb0dc4..abbcda3 100644
--- 
a/standalone-metastore/src/test/java/org/apache/hadoop/hive/metastore/InjectableBehaviourObjectStore.java
+++ 
b/standalone-metastore/src/test/java/org/apache/hadoop/hive/metastore/InjectableBehaviourObjectStore.java
@@ -19,9 +19,15 @@
 package org.apache.hadoop.hive.metastore;
 
 import java.util.List;
+import org.apache.hadoop.hive.metastore.api.Function;
+import org.apache.hadoop.hive.metastore.api.InvalidObjectException;
 import org.apache.hadoop.hive.metastore.api.MetaException;
 import org.apache.hadoop.hive.metastore.api.NotificationEventRequest;
 import org.apache.hadoop.hive.metastore.api.NotificationEventResponse;
+import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
+import org.apache.hadoop.hive.metastore.api.Partition;
+import org.apache.hadoop.hive.metastore.api.SQLForeignKey;
+import org.apache.hadoop.hive.metastore.api.SQLPrimaryKey;
 import org.apache.hadoop.hive.metastore.api.Table;
 
 import static org.junit.Assert.assertEquals;
@@ -39,8 +45,8 @@ public class InjectableBehaviourObjectStore extends 
ObjectStore {
   /**
    * A utility class that allows people injecting behaviour to determine if 
their injections occurred.
    */
-  public static abstract class BehaviourInjection<T,F>
-      implements com.google.common.base.Function<T,F>{
+  public static abstract class BehaviourInjection<T, F>
+      implements com.google.common.base.Function<T, F>{
     protected boolean injectionPathCalled = false;
     protected boolean nonInjectedPathCalled = false;
 
@@ -51,22 +57,49 @@ public class InjectableBehaviourObjectStore extends 
ObjectStore {
     }
   }
 
-  private static com.google.common.base.Function<Table,Table> getTableModifier 
=
+  /**
+   * A utility class to pass the arguments of the caller to the stub method.
+   */
+  public class CallerArguments {
+    public String dbName;
+    public String tblName;
+    public String funcName;
+    public String constraintTblName;
+
+    public CallerArguments(String dbName) {
+      this.dbName = dbName;
+    }
+  }
+
+  private static com.google.common.base.Function<Table, Table> 
getTableModifier =
       com.google.common.base.Functions.identity();
+  private static com.google.common.base.Function<Partition, Partition> 
getPartitionModifier =
+          com.google.common.base.Functions.identity();
   private static com.google.common.base.Function<List<String>, List<String>> 
listPartitionNamesModifier =
           com.google.common.base.Functions.identity();
   private static com.google.common.base.Function<NotificationEventResponse, 
NotificationEventResponse>
           getNextNotificationModifier = 
com.google.common.base.Functions.identity();
 
+  private static com.google.common.base.Function<CallerArguments, Boolean> 
callerVerifier = null;
+
   // Methods to set/reset getTable modifier
-  public static void 
setGetTableBehaviour(com.google.common.base.Function<Table,Table> modifier){
-    getTableModifier = (modifier == null)? 
com.google.common.base.Functions.identity() : modifier;
+  public static void 
setGetTableBehaviour(com.google.common.base.Function<Table, Table> modifier){
+    getTableModifier = (modifier == null) ? 
com.google.common.base.Functions.identity() : modifier;
   }
 
   public static void resetGetTableBehaviour(){
     setGetTableBehaviour(null);
   }
 
+  // Methods to set/reset getPartition modifier
+  public static void 
setGetPartitionBehaviour(com.google.common.base.Function<Partition, Partition> 
modifier){
+    getPartitionModifier = (modifier == null) ? 
com.google.common.base.Functions.identity() : modifier;
+  }
+
+  public static void resetGetPartitionBehaviour(){
+    setGetPartitionBehaviour(null);
+  }
+
   // Methods to set/reset listPartitionNames modifier
   public static void 
setListPartitionNamesBehaviour(com.google.common.base.Function<List<String>, 
List<String>> modifier){
     listPartitionNamesModifier = (modifier == null)? 
com.google.common.base.Functions.identity() : modifier;
@@ -86,6 +119,15 @@ public class InjectableBehaviourObjectStore extends 
ObjectStore {
     setGetNextNotificationBehaviour(null);
   }
 
+  // Methods to set/reset caller checker
+  public static void 
setCallerVerifier(com.google.common.base.Function<CallerArguments, Boolean> 
verifier){
+    callerVerifier = verifier;
+  }
+
+  public static void resetCallerVerifier(){
+    setCallerVerifier(null);
+  }
+
   // ObjectStore methods to be overridden with injected behavior
   @Override
   public Table getTable(String catName, String dbName, String tableName) 
throws MetaException {
@@ -93,7 +135,14 @@ public class InjectableBehaviourObjectStore extends 
ObjectStore {
   }
 
   @Override
-  public List<String> listPartitionNames(String catName, String dbName, String 
tableName, short max) throws MetaException {
+  public Partition getPartition(String catName, String dbName, String 
tableName,
+                                List<String> partVals) throws 
NoSuchObjectException, MetaException {
+    return getPartitionModifier.apply(super.getPartition(catName, dbName, 
tableName, partVals));
+  }
+
+  @Override
+  public List<String> listPartitionNames(String catName, String dbName, String 
tableName, short max)
+          throws MetaException {
     return listPartitionNamesModifier.apply(super.listPartitionNames(catName, 
dbName, tableName, max));
   }
 
@@ -101,4 +150,62 @@ public class InjectableBehaviourObjectStore extends 
ObjectStore {
   public NotificationEventResponse 
getNextNotification(NotificationEventRequest rqst) {
     return getNextNotificationModifier.apply(super.getNextNotification(rqst));
   }
+
+  @Override
+  public void createTable(Table tbl) throws InvalidObjectException, 
MetaException {
+    if (callerVerifier != null) {
+      CallerArguments args = new CallerArguments(tbl.getDbName());
+      args.tblName = tbl.getTableName();
+      Boolean success = callerVerifier.apply(args);
+      if ((success != null) && !success) {
+        throw new MetaException("InjectableBehaviourObjectStore: Invalid 
Create Table operation on DB: "
+                + args.dbName + " table: " + args.tblName);
+      }
+    }
+    super.createTable(tbl);
+  }
+
+  @Override
+  public void createFunction(Function func) throws InvalidObjectException, 
MetaException {
+    if (callerVerifier != null) {
+      CallerArguments args = new CallerArguments(func.getDbName());
+      args.funcName = func.getFunctionName();
+      Boolean success = callerVerifier.apply(args);
+      if ((success != null) && !success) {
+        throw new MetaException("InjectableBehaviourObjectStore: Invalid 
Create Function operation on DB: "
+                + args.dbName + " function: " + args.funcName);
+      }
+    }
+    super.createFunction(func);
+  }
+
+  @Override
+  public List<String> addPrimaryKeys(List<SQLPrimaryKey> pks) throws 
InvalidObjectException,
+          MetaException {
+    if (callerVerifier != null) {
+      CallerArguments args = new CallerArguments(pks.get(0).getTable_db());
+      args.constraintTblName = pks.get(0).getTable_name();
+      Boolean success = callerVerifier.apply(args);
+      if ((success != null) && !success) {
+        throw new MetaException("InjectableBehaviourObjectStore: Invalid Add 
Primary Key operation on DB: "
+                + args.dbName + " table: " + args.constraintTblName);
+      }
+    }
+    return super.addPrimaryKeys(pks);
+  }
+
+  @Override
+  public List<String> addForeignKeys(List<SQLForeignKey> fks) throws 
InvalidObjectException,
+          MetaException {
+    if (callerVerifier != null) {
+      CallerArguments args = new CallerArguments(fks.get(0).getFktable_db());
+      args.constraintTblName = fks.get(0).getFktable_name();
+      Boolean success = callerVerifier.apply(args);
+      if ((success != null) && !success) {
+        throw new MetaException("InjectableBehaviourObjectStore: Invalid Add 
Foreign Key operation on DB: "
+                + args.dbName + " table: " + args.constraintTblName);
+      }
+    }
+    return super.addForeignKeys(fks);
+  }
 }

Reply via email to