Remove Installed Libraries before Unit Tests

Change-Id: I9dea8c8fc46a717acd0b86ac7b07e0276bb5e95c
Reviewed-on: https://asterix-gerrit.ics.uci.edu/797
Reviewed-by: Murtadha Hubail <hubailmor@gmail.com>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/external/FeedWorkCollection.java b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/external/FeedWorkCollection.java
index 53b9792..2f338d0 100644
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/external/FeedWorkCollection.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/external/FeedWorkCollection.java
@@ -99,7 +99,6 @@
                     if (LOGGER.isLoggable(Level.SEVERE)) {
                         LOGGER.severe("Exception in executing " + request);
                     }
-                    throw new RuntimeException(e);
                 }
             }
         }
diff --git a/asterixdb/asterix-app/src/test/java/org/apache/asterix/app/external/TestLibrarian.java b/asterixdb/asterix-app/src/test/java/org/apache/asterix/app/external/TestLibrarian.java
index 4f8fa6f..3416a77 100644
--- a/asterixdb/asterix-app/src/test/java/org/apache/asterix/app/external/TestLibrarian.java
+++ b/asterixdb/asterix-app/src/test/java/org/apache/asterix/app/external/TestLibrarian.java
@@ -22,12 +22,15 @@
 import java.io.IOException;
 import java.rmi.RemoteException;
 import java.util.HashMap;
+import java.util.List;
 
 import org.apache.asterix.common.exceptions.ACIDException;
 import org.apache.asterix.common.exceptions.AsterixException;
 import org.apache.asterix.event.service.AsterixEventServiceUtil;
+import org.apache.asterix.external.library.ExternalLibraryManager;
 import org.apache.asterix.test.aql.ITestLibrarian;
 import org.apache.commons.io.FileUtils;
+import org.apache.hyracks.algebricks.common.utils.Pair;
 import org.apache.hyracks.api.exceptions.HyracksDataException;
 
 public class TestLibrarian implements ITestLibrarian {
@@ -64,6 +67,7 @@
     @Override
     public void uninstall(String dvName, String libName) throws RemoteException, AsterixException, ACIDException {
         ExternalLibraryUtils.uninstallLibrary(dvName, libName);
+        ExternalLibraryManager.deregisterLibraryClassLoader(dvName, libName);
     }
 
     public static void removeLibraryDir() throws IOException {
@@ -73,4 +77,12 @@
         }
         FileUtils.deleteQuietly(installLibDir);
     }
+
+    public static void cleanup() throws AsterixException, RemoteException, ACIDException {
+        List<Pair<String, String>> libs = ExternalLibraryManager.getAllLibraries();
+        for (Pair<String, String> dvAndLib : libs) {
+            ExternalLibraryUtils.uninstallLibrary(dvAndLib.first, dvAndLib.second);
+            ExternalLibraryManager.deregisterLibraryClassLoader(dvAndLib.first, dvAndLib.second);
+        }
+    }
 }
diff --git a/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/runtime/ExecutionTest.java b/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/runtime/ExecutionTest.java
index e372d31..dfb61c2 100644
--- a/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/runtime/ExecutionTest.java
+++ b/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/runtime/ExecutionTest.java
@@ -102,6 +102,7 @@
 
     @Test
     public void test() throws Exception {
+        TestLibrarian.cleanup();
         testExecutor.executeTest(PATH_ACTUAL, tcCtx, null, false, ExecutionTestUtil.FailedGroup);
     }
 }
diff --git a/asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/library/ExternalLibraryManager.java b/asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/library/ExternalLibraryManager.java
index db85e2f..0283bf2 100755
--- a/asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/library/ExternalLibraryManager.java
+++ b/asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/library/ExternalLibraryManager.java
@@ -18,8 +18,13 @@
  */
 package org.apache.asterix.external.library;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.hyracks.algebricks.common.utils.Pair;
 
 public class ExternalLibraryManager {
 
@@ -34,17 +39,27 @@
     public static void registerLibraryClassLoader(String dataverseName, String libraryName, ClassLoader classLoader) {
         String key = getKey(dataverseName, libraryName);
         synchronized (libraryClassLoaders) {
-            if (libraryClassLoaders.get(dataverseName) != null) {
+            if (libraryClassLoaders.get(key) != null) {
                 throw new IllegalStateException("Library class loader already registered!");
             }
             libraryClassLoaders.put(key, classLoader);
         }
     }
 
+    public static List<Pair<String, String>> getAllLibraries() {
+        ArrayList<Pair<String, String>> libs = new ArrayList<>();
+        synchronized (libraryClassLoaders) {
+            for (Entry<String, ClassLoader> entry : libraryClassLoaders.entrySet()) {
+                libs.add(getDataverseAndLibararyName(entry.getKey()));;
+            }
+        }
+        return libs;
+    }
+
     public static void deregisterLibraryClassLoader(String dataverseName, String libraryName) {
         String key = getKey(dataverseName, libraryName);
         synchronized (libraryClassLoaders) {
-            if (libraryClassLoaders.get(dataverseName) != null) {
+            if (libraryClassLoaders.get(key) != null) {
                 libraryClassLoaders.remove(key);
             }
         }
@@ -52,13 +67,18 @@
 
     public static ClassLoader getLibraryClassLoader(String dataverseName, String libraryName) {
         String key = getKey(dataverseName, libraryName);
-        synchronized (libraryClassLoaders) {
-            return libraryClassLoaders.get(key);
-        }
+        return libraryClassLoaders.get(key);
     }
 
     private static String getKey(String dataverseName, String libraryName) {
         return dataverseName + "." + libraryName;
     }
 
+    private static Pair<String, String> getDataverseAndLibararyName(String key) {
+        int index = key.indexOf(".");
+        String dataverse = key.substring(0, index);
+        String library = key.substring(index + 1);
+        return new Pair<String, String>(dataverse, library);
+    }
+
 }