aboutsummaryrefslogtreecommitdiffstats
path: root/tests/com/iciql/test/IciqlSuite.java
diff options
context:
space:
mode:
Diffstat (limited to 'tests/com/iciql/test/IciqlSuite.java')
-rw-r--r--tests/com/iciql/test/IciqlSuite.java243
1 files changed, 220 insertions, 23 deletions
diff --git a/tests/com/iciql/test/IciqlSuite.java b/tests/com/iciql/test/IciqlSuite.java
index 5842a2e..c43bdaf 100644
--- a/tests/com/iciql/test/IciqlSuite.java
+++ b/tests/com/iciql/test/IciqlSuite.java
@@ -15,8 +15,13 @@
*/
package com.iciql.test;
+import java.io.PrintStream;
import java.sql.SQLException;
import java.text.MessageFormat;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Assert;
@@ -27,7 +32,13 @@ import org.junit.runner.notification.Failure;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
+import com.beust.jcommander.JCommander;
+import com.beust.jcommander.Parameter;
+import com.beust.jcommander.ParameterException;
+import com.beust.jcommander.Parameters;
+import com.iciql.Constants;
import com.iciql.Db;
+import com.iciql.util.StringUtils;
/**
* JUnit 4 iciql test suite.
@@ -45,29 +56,62 @@ import com.iciql.Db;
RuntimeQueryTest.class, SamplesTest.class, UpdateTest.class, UUIDTest.class })
public class IciqlSuite {
- private static final TestDb[] TEST_DBS = {
- new TestDb("H2", "jdbc:h2:mem:"),
- new TestDb("HSQL", "jdbc:hsqldb:mem:db{0,number,000}") };
+ private static final TestDb[] TEST_DBS = { new TestDb("H2", "jdbc:h2:mem:db{0,number,000}"),
+ new TestDb("HSQL", "jdbc:hsqldb:mem:db{0,number,000}"),
+ new TestDb("Derby", "jdbc:derby:memory:db{0,number,000};create=true") };
private static final TestDb DEFAULT_TEST_DB = TEST_DBS[0];
+ private static final PrintStream ERR = System.err;
+
private static AtomicInteger openCount = new AtomicInteger(0);
+ private static String username = "sa";
+
+ private static String password = "sa";
+
+ private static PrintStream out = System.out;
+
public static void assertStartsWith(String value, String startsWith) {
Assert.assertTrue(MessageFormat.format("Expected \"{0}\", got: \"{1}\"", startsWith, value),
value.startsWith(startsWith));
}
- public static Db openDb() {
+ /**
+ * Increment the database counter, open and create a new database.
+ *
+ * @return a fresh database
+ */
+ public static Db openNewDb() {
String testUrl = System.getProperty("iciql.url");
if (testUrl == null) {
testUrl = DEFAULT_TEST_DB.url;
}
testUrl = MessageFormat.format(testUrl, openCount.incrementAndGet());
- return Db.open(testUrl, "sa", "sa");
+ return Db.open(testUrl, username, password);
}
- public static String getDatabaseName(Db db) {
+ /**
+ * Open the current database.
+ *
+ * @return the current database
+ */
+ public static Db openCurrentDb() {
+ String testUrl = System.getProperty("iciql.url");
+ if (testUrl == null) {
+ testUrl = DEFAULT_TEST_DB.url;
+ }
+ testUrl = MessageFormat.format(testUrl, openCount.get());
+ return Db.open(testUrl, username, password);
+ }
+
+ /**
+ * Returns the name of the underlying database engine for the Db object.
+ *
+ * @param db
+ * @return the database engine name
+ */
+ public static String getDatabaseEngineName(Db db) {
String database = "";
try {
database = db.getConnection().getMetaData().getDatabaseProductName();
@@ -76,23 +120,161 @@ public class IciqlSuite {
return database;
}
- public static void main(String... args) {
+ /**
+ * Returns true if the underlying database engine is Derby.
+ *
+ * @param db
+ * @return true if underlying database engine is Derby
+ */
+ public static boolean isDerby(Db db) {
+ return IciqlSuite.getDatabaseEngineName(db).equals("Apache Derby");
+ }
+
+ /**
+ * Returns true if the underlying database engine is H2.
+ *
+ * @param db
+ * @return true if underlying database engine is H2
+ */
+ public static boolean isH2(Db db) {
+ return IciqlSuite.getDatabaseEngineName(db).equals("H2");
+ }
+
+ /**
+ * Gets the default schema of the underlying database engine.
+ *
+ * @param db
+ * @return the default schema
+ */
+ public static String getDefaultSchema(Db db) {
+ if (isDerby(db)) {
+ // Derby sets default schema name to username
+ return username.toUpperCase();
+ }
+ return "PUBLIC";
+ }
+
+ /**
+ * Main entry point for the test suite. Executing this method will run the
+ * test suite on all registered databases.
+ *
+ * @param args
+ * @throws Exception
+ */
+ public static void main(String... args) throws Exception {
+ Params params = new Params();
+ JCommander jc = new JCommander(params);
+ try {
+ jc.parse(args);
+ } catch (ParameterException t) {
+ usage(jc, t);
+ }
+
+ // Replace System.out with a file
+ if (!StringUtils.isNullOrEmpty(params.outputFile)) {
+ out = new PrintStream(params.outputFile);
+ System.setErr(out);
+ }
+
SuiteClasses suiteClasses = IciqlSuite.class.getAnnotation(SuiteClasses.class);
+ long quickestDatabase = Long.MAX_VALUE;
+ String dividerMajor = buildDivider('*', 70);
+ String dividerMinor = buildDivider('-', 70);
+
+ // Header
+ out.println(dividerMajor);
+ out.println(MessageFormat.format("{0} {1} ({2}) testing {3} databases", Constants.NAME,
+ Constants.VERSION, Constants.VERSION_DATE, TEST_DBS.length));
+ out.println(dividerMajor);
+ out.println();
+
+ showProperty("java.vendor");
+ showProperty("java.runtime.version");
+ showProperty("java.vm.name");
+ showProperty("os.name");
+ showProperty("os.version");
+ showProperty("os.arch");
+ out.println();
+
+ // Test a database
for (TestDb testDb : TEST_DBS) {
- System.out.println("*********************************************");
- System.out.println("Testing " + testDb.name + " " + testDb.getVersion());
- System.out.println("*********************************************");
+ out.println(dividerMinor);
+ out.println("Testing " + testDb.name + " " + testDb.getVersion());
+ out.println(dividerMinor);
System.setProperty("iciql.url", testDb.url);
Result result = JUnitCore.runClasses(suiteClasses.value());
- System.out.println(MessageFormat.format("{0} runs, {1} failures, {2} ignores in {3} msecs",
+ testDb.runtime = result.getRunTime();
+ if (testDb.runtime < quickestDatabase) {
+ quickestDatabase = testDb.runtime;
+ }
+ out.println(MessageFormat.format("{0} tests: {1} failures, {2} ignores in {3,number,0.000} secs",
result.getRunCount(), result.getFailureCount(), result.getIgnoreCount(),
- result.getRunTime()));
- for (Failure failure : result.getFailures()) {
- System.out.println(MessageFormat.format("{0}: {1}", failure.getTestHeader(),
- failure.getMessage()));
+ result.getRunTime() / 1000f));
+ if (result.getFailureCount() == 0) {
+ out.println();
+ } else {
+ for (Failure failure : result.getFailures()) {
+ out.println(MessageFormat.format("\n + {0}\n {1}\n", failure.getTestHeader(),
+ failure.getMessage()));
+ }
}
+ }
+
+ // Display runtime results sorted by performance leader
+ out.println(dividerMajor);
+ out.println(MessageFormat.format("{0} {1} ({2}) test suite performance results", Constants.NAME,
+ Constants.VERSION, Constants.VERSION_DATE));
+ out.println(dividerMajor);
+ List<TestDb> dbs = Arrays.asList(TEST_DBS);
+ Collections.sort(dbs, new Comparator<TestDb>() {
+
+ @Override
+ public int compare(TestDb o1, TestDb o2) {
+ if (o1.runtime == o2.runtime) {
+ return 0;
+ }
+ if (o1.runtime > o2.runtime) {
+ return 1;
+ }
+ return -1;
+ }
+ });
+ for (TestDb testDb : dbs) {
+ out.println(MessageFormat.format("{0} {1} {2,number,0.000} secs ({3,number,#.0}x)",
+ StringUtils.pad(testDb.name, 6, " ", true),
+ StringUtils.pad(testDb.getVersion(), 22, " ", true), testDb.runtime / 1000f,
+ ((double) testDb.runtime) / quickestDatabase));
+ }
+
+ // close PrintStream and restore System.err
+ out.close();
+ System.setErr(ERR);
+ }
+
+ private static void showProperty(String name) {
+ out.print(StringUtils.pad(name, 25, " ", true));
+ out.println(System.getProperty(name));
+ }
+
+ private static void usage(JCommander jc, ParameterException t) {
+ System.out.println(Constants.NAME + " test suite v" + Constants.VERSION);
+ System.out.println();
+ if (t != null) {
+ System.out.println(t.getMessage());
System.out.println();
}
+ if (jc != null) {
+ jc.usage();
+ }
+ System.exit(0);
+ }
+
+ private static String buildDivider(char c, int length) {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < length; i++) {
+ sb.append(c);
+ }
+ return sb.toString();
}
/**
@@ -101,6 +283,8 @@ public class IciqlSuite {
private static class TestDb {
final String name;
final String url;
+ String version;
+ long runtime;
TestDb(String name, String url) {
this.name = name;
@@ -108,14 +292,27 @@ public class IciqlSuite {
}
String getVersion() {
- try {
- Db db = Db.open(url, "sa", "sa");
- String version = db.getConnection().getMetaData().getDatabaseProductVersion();
- db.close();
- return version;
- } catch (SQLException s) {
+ if (version == null) {
+ try {
+ Db db = Db.open(url, username, password);
+ version = db.getConnection().getMetaData().getDatabaseProductVersion();
+ db.close();
+ return version;
+ } catch (SQLException s) {
+ version = "";
+ }
}
- return "";
+ return version;
}
}
-}
+
+ /**
+ * Command-line parameters for TestSuite.
+ */
+ @Parameters(separators = " ")
+ private static class Params {
+
+ @Parameter(names = { "--outputFile" }, description = "Results text file", required = false)
+ public String outputFile;
+ }
+} \ No newline at end of file