Added Asterix CLI to run tests from the command line

git-svn-id: https://asterixdb.googlecode.com/svn/trunk/asterix@33 eaa15691-b419-025a-1212-ee371bd00084
diff --git a/asterix-app/pom.xml b/asterix-app/pom.xml
index 6b5b39d..03ee55b 100644
--- a/asterix-app/pom.xml
+++ b/asterix-app/pom.xml
@@ -36,6 +36,10 @@
 									<mainClass>edu.uci.ics.asterix.drivers.AsterixClientDriver</mainClass>
 									<name>asterix-cmd</name>
 								</program>
+								<program>
+									<mainClass>edu.uci.ics.asterix.drivers.AsterixCLI</mainClass>
+									<name>asterix-cli</name>
+								</program>
 							</programs>
 							<repositoryLayout>flat</repositoryLayout>
 							<repositoryName>lib</repositoryName>
diff --git a/asterix-app/src/main/java/edu/uci/ics/asterix/drivers/AsterixCLI.java b/asterix-app/src/main/java/edu/uci/ics/asterix/drivers/AsterixCLI.java
new file mode 100644
index 0000000..c06c29c
--- /dev/null
+++ b/asterix-app/src/main/java/edu/uci/ics/asterix/drivers/AsterixCLI.java
@@ -0,0 +1,72 @@
+package edu.uci.ics.asterix.drivers;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.Reader;
+import java.util.List;
+
+import org.apache.commons.io.FileUtils;
+import org.kohsuke.args4j.Argument;
+import org.kohsuke.args4j.CmdLineParser;
+import org.kohsuke.args4j.Option;
+
+import edu.uci.ics.asterix.api.common.AsterixHyracksIntegrationUtil;
+import edu.uci.ics.asterix.api.java.AsterixJavaClient;
+import edu.uci.ics.asterix.common.config.GlobalConfig;
+
+public class AsterixCLI {
+    private static class Options {
+        @Option(name = "-properties", usage = "Name of properties file", required = true)
+        public String properties;
+
+        @Option(name = "-output", usage = "Output folder to place results", required = true)
+        public String outputFolder;
+
+        @Argument(usage = "AQL Files to run", multiValued = true, required = true)
+        public List<String> args;
+    }
+
+    public static void main(String args[]) throws Exception {
+        Options options = new Options();
+        CmdLineParser parser = new CmdLineParser(options);
+        parser.parseArgument(args);
+
+        setUp(options);
+        try {
+            for (String queryFile : options.args) {
+                Reader in = new FileReader(queryFile);
+                AsterixJavaClient ajc = new AsterixJavaClient(
+                        AsterixHyracksIntegrationUtil.getHyracksClientConnection(), in);
+                try {
+                    ajc.compile(true, false, false, false, false, true, false);
+                } finally {
+                    in.close();
+                }
+                ajc.execute();
+            }
+        } finally {
+            tearDown();
+        }
+        System.exit(0);
+    }
+
+    public static void setUp(Options options) throws Exception {
+        System.setProperty(GlobalConfig.CONFIG_FILE_PROPERTY, options.properties);
+        System.setProperty(GlobalConfig.WEB_SERVER_PORT_PROPERTY, "19002");
+        File outdir = new File(options.outputFolder);
+        outdir.mkdirs();
+
+        File log = new File("asterix_logs");
+        if (log.exists())
+            FileUtils.deleteDirectory(log);
+        File lsn = new File("last_checkpoint_lsn");
+        lsn.deleteOnExit();
+
+        AsterixHyracksIntegrationUtil.init();
+    }
+
+    public static void tearDown() throws Exception {
+        AsterixHyracksIntegrationUtil.deinit();
+    }
+
+}
\ No newline at end of file
diff --git a/asterix-app/src/test/java/edu/uci/ics/asterix/test/aql/TestsUtils.java b/asterix-app/src/test/java/edu/uci/ics/asterix/test/aql/TestsUtils.java
index ae29617..7adafc8 100644
--- a/asterix-app/src/test/java/edu/uci/ics/asterix/test/aql/TestsUtils.java
+++ b/asterix-app/src/test/java/edu/uci/ics/asterix/test/aql/TestsUtils.java
@@ -54,9 +54,10 @@
             asterix.compile(true, false, false, false, false, true, false);
         } catch (AsterixException e) {
             throw new Exception("Compile ERROR for " + scriptFile + ": " + e.getMessage(), e);
+        } finally {
+            query.close();
         }
         asterix.execute();
-        query.close();
         BufferedReader readerExpected = new BufferedReader(new FileReader(expectedFile));
         BufferedReader readerActual = new BufferedReader(new FileReader(actualFile));
         String lineExpected, lineActual;
diff --git a/asterix-app/src/test/java/edu/uci/ics/asterix/test/dml/DmlTest.java b/asterix-app/src/test/java/edu/uci/ics/asterix/test/dml/DmlTest.java
index 6864ec3..3e82ff6 100644
--- a/asterix-app/src/test/java/edu/uci/ics/asterix/test/dml/DmlTest.java
+++ b/asterix-app/src/test/java/edu/uci/ics/asterix/test/dml/DmlTest.java
@@ -41,6 +41,8 @@
             asterixLoad.compile(true, false, false, false, false, true, false);
         } catch (AsterixException e) {
             throw new Exception("Compile ERROR for " + LOAD_FOR_ENLIST_FILE + ": " + e.getMessage(), e);
+        } finally {
+            loadReader.close();
         }
         asterixLoad.execute();
         AsterixHyracksIntegrationUtil.destroyApp();
diff --git a/asterix-app/src/test/java/edu/uci/ics/asterix/test/metadata/MetadataTest.java b/asterix-app/src/test/java/edu/uci/ics/asterix/test/metadata/MetadataTest.java
index 416734f..baa91dc 100644
--- a/asterix-app/src/test/java/edu/uci/ics/asterix/test/metadata/MetadataTest.java
+++ b/asterix-app/src/test/java/edu/uci/ics/asterix/test/metadata/MetadataTest.java
@@ -153,6 +153,8 @@
             asterix.compile();
         } catch (AsterixException e) {
             throw new Exception("Compile ERROR for " + queryFile + ": " + e.getMessage(), e);
+        } finally {
+            query.close();
         }
         asterix.execute();
         query.close();
diff --git a/asterix-app/src/test/java/edu/uci/ics/asterix/test/metadata/MetadataTransactionsTest.java b/asterix-app/src/test/java/edu/uci/ics/asterix/test/metadata/MetadataTransactionsTest.java
index 5fc3599..6081994 100644
--- a/asterix-app/src/test/java/edu/uci/ics/asterix/test/metadata/MetadataTransactionsTest.java
+++ b/asterix-app/src/test/java/edu/uci/ics/asterix/test/metadata/MetadataTransactionsTest.java
@@ -84,7 +84,11 @@
             AsterixJavaClient asterix = new AsterixJavaClient(
                     AsterixHyracksIntegrationUtil.getHyracksClientConnection(), query, ERR);
             LOGGER.info("Query is: " + queryFileName);
-            asterix.compile(true, false, false, false, false, executeQuery, false);
+            try {
+                asterix.compile(true, false, false, false, false, executeQuery, false);
+            } finally {
+                query.close();
+            }
             // We don't want to execute a query if we expect only DDL
             // modifications.
             if (executeQuery) {