diff options
Diffstat (limited to 'tests/com/iciql')
-rw-r--r-- | tests/com/iciql/test/AnnotationsTest.java | 26 | ||||
-rw-r--r-- | tests/com/iciql/test/ClobTest.java | 4 | ||||
-rw-r--r-- | tests/com/iciql/test/IciqlSuite.java | 149 | ||||
-rw-r--r-- | tests/com/iciql/test/ModelsTest.java | 83 | ||||
-rw-r--r-- | tests/com/iciql/test/SamplesTest.java | 41 | ||||
-rw-r--r-- | tests/com/iciql/test/UUIDTest.java | 8 | ||||
-rw-r--r-- | tests/com/iciql/test/UpgradesTest.java | 134 | ||||
-rw-r--r-- | tests/com/iciql/test/models/PrimitivesModel.java | 5 | ||||
-rw-r--r-- | tests/com/iciql/test/models/ProductAnnotationOnly.java | 5 | ||||
-rw-r--r-- | tests/com/iciql/test/models/SupportedTypes.java | 5 |
10 files changed, 316 insertions, 144 deletions
diff --git a/tests/com/iciql/test/AnnotationsTest.java b/tests/com/iciql/test/AnnotationsTest.java index 7e4e918..4f50d61 100644 --- a/tests/com/iciql/test/AnnotationsTest.java +++ b/tests/com/iciql/test/AnnotationsTest.java @@ -69,30 +69,24 @@ public class AnnotationsTest { // test indexes are created, and columns are in the right order DatabaseMetaData meta = db.getConnection().getMetaData(); boolean isH2 = meta.getDatabaseProductName().equals("H2"); - boolean isDerby = meta.getDatabaseProductName().equals("Apache Derby"); - ResultSet rs; - if (isDerby) { - // Derby defaults to USERNAME schema - rs = meta.getIndexInfo(null, "SA", "ANNOTATEDPRODUCT", false, true); - } else { - // H2, HSQL default to PUBLIC schema - rs = meta.getIndexInfo(null, "PUBLIC", "ANNOTATEDPRODUCT", false, true); - } + String schema = IciqlSuite.getDefaultSchema(db); + ResultSet rs = meta.getIndexInfo(null, schema, "ANNOTATEDPRODUCT", false, true); + // first index is primary key index // H2 gives this a testable name. assertTrue(rs.next()); if (isH2) { - assertStartsWith(rs.getString("INDEX_NAME"), "PRIMARY_KEY"); + assertStartsWith(rs.getString("INDEX_NAME").toUpperCase(), "PRIMARY_KEY"); } assertTrue(rs.next()); - assertStartsWith(rs.getString("INDEX_NAME"), "ANNOTATEDPRODUCT_0"); - assertStartsWith(rs.getString("COLUMN_NAME"), "NAME"); + assertStartsWith(rs.getString("INDEX_NAME").toUpperCase(), "ANNOTATEDPRODUCT_0"); + assertStartsWith(rs.getString("COLUMN_NAME").toUpperCase(), "NAME"); assertTrue(rs.next()); - assertStartsWith(rs.getString("INDEX_NAME"), "ANNOTATEDPRODUCT_0"); - assertStartsWith(rs.getString("COLUMN_NAME"), "CAT"); + assertStartsWith(rs.getString("INDEX_NAME").toUpperCase(), "ANNOTATEDPRODUCT_0"); + assertStartsWith(rs.getString("COLUMN_NAME").toUpperCase(), "CAT"); assertTrue(rs.next()); - assertStartsWith(rs.getString("INDEX_NAME"), "NAMEIDX"); - assertStartsWith(rs.getString("COLUMN_NAME"), "NAME"); + assertStartsWith(rs.getString("INDEX_NAME").toUpperCase(), "NAMEIDX"); + assertStartsWith(rs.getString("COLUMN_NAME").toUpperCase(), "NAME"); assertFalse(rs.next()); } diff --git a/tests/com/iciql/test/ClobTest.java b/tests/com/iciql/test/ClobTest.java index 3b32dcb..49cee72 100644 --- a/tests/com/iciql/test/ClobTest.java +++ b/tests/com/iciql/test/ClobTest.java @@ -42,12 +42,14 @@ public class ClobTest { db.executeUpdate(MessageFormat.format(create, "VARCHAR(255)")); db.insertAll(StringRecord.getList()); testSimpleUpdate(db, "VARCHAR fail"); + db.executeUpdate("DROP TABLE CLOB_TEST"); db.close(); db = IciqlSuite.openNewDb(); - db.executeUpdate(MessageFormat.format(create, "CLOB")); + db.executeUpdate(MessageFormat.format(create, db.getDialect().convertSqlType("CLOB"))); db.insertAll(StringRecord.getList()); testSimpleUpdate(db, "CLOB fail because of single quote artifacts"); + db.executeUpdate("DROP TABLE CLOB_TEST"); db.close(); } diff --git a/tests/com/iciql/test/IciqlSuite.java b/tests/com/iciql/test/IciqlSuite.java index b91f3a4..0389d80 100644 --- a/tests/com/iciql/test/IciqlSuite.java +++ b/tests/com/iciql/test/IciqlSuite.java @@ -40,6 +40,20 @@ import com.beust.jcommander.ParameterException; import com.beust.jcommander.Parameters;
import com.iciql.Constants;
import com.iciql.Db;
+import com.iciql.test.models.BooleanModel;
+import com.iciql.test.models.ComplexObject;
+import com.iciql.test.models.Customer;
+import com.iciql.test.models.DefaultValuesModel;
+import com.iciql.test.models.EnumModels.EnumIdModel;
+import com.iciql.test.models.EnumModels.EnumOrdinalModel;
+import com.iciql.test.models.EnumModels.EnumStringModel;
+import com.iciql.test.models.Order;
+import com.iciql.test.models.PrimitivesModel;
+import com.iciql.test.models.Product;
+import com.iciql.test.models.ProductAnnotationOnly;
+import com.iciql.test.models.ProductInheritedAnnotation;
+import com.iciql.test.models.ProductMixedAnnotation;
+import com.iciql.test.models.SupportedTypes;
import com.iciql.util.StatementLogger;
import com.iciql.util.StatementLogger.StatementListener;
import com.iciql.util.StatementLogger.StatementType;
@@ -58,12 +72,13 @@ import com.iciql.util.StringUtils; @RunWith(Suite.class)
@SuiteClasses({ AliasMapTest.class, AnnotationsTest.class, BooleanModelTest.class, ClobTest.class,
ConcurrencyTest.class, EnumsTest.class, ModelsTest.class, PrimitivesTest.class,
- RuntimeQueryTest.class, SamplesTest.class, UpdateTest.class, UUIDTest.class })
+ RuntimeQueryTest.class, SamplesTest.class, UpdateTest.class, UpgradesTest.class, UUIDTest.class })
public class IciqlSuite {
- 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[] TEST_DBS = { new TestDb("H2 (embedded)", "jdbc:h2:mem:db{0,number,000}"),
+ new TestDb("HSQL (embedded)", "jdbc:hsqldb:mem:db{0,number,000}"),
+ new TestDb("Derby (embedded)", "jdbc:derby:memory:db{0,number,000};create=true"),
+ new TestDb("MySQL (tcp/myisam)", "jdbc:mysql://localhost:3306/iciql") };
private static final TestDb DEFAULT_TEST_DB = TEST_DBS[0];
@@ -82,6 +97,13 @@ public class IciqlSuite { value.startsWith(startsWith));
}
+ public static boolean equivalentTo(double expected, double actual) {
+ if (Double.compare(expected, actual) == 0) {
+ return true;
+ }
+ return Math.abs(expected - actual) <= 0.000001d;
+ }
+
/**
* Increment the database counter, open and create a new database.
*
@@ -93,7 +115,25 @@ public class IciqlSuite { testUrl = DEFAULT_TEST_DB.url;
}
testUrl = MessageFormat.format(testUrl, openCount.incrementAndGet());
- return Db.open(testUrl, username, password);
+ Db db = Db.open(testUrl, username, password);
+
+ // drop tables
+ db.dropTable(BooleanModel.class);
+ db.dropTable(ComplexObject.class);
+ db.dropTable(Customer.class);
+ db.dropTable(DefaultValuesModel.class);
+ db.dropTable(EnumIdModel.class);
+ db.dropTable(EnumOrdinalModel.class);
+ db.dropTable(EnumStringModel.class);
+ db.dropTable(Order.class);
+ db.dropTable(PrimitivesModel.class);
+ db.dropTable(Product.class);
+ db.dropTable(ProductAnnotationOnly.class);
+ db.dropTable(ProductInheritedAnnotation.class);
+ db.dropTable(ProductMixedAnnotation.class);
+ db.dropTable(SupportedTypes.class);
+
+ return db;
}
/**
@@ -111,6 +151,15 @@ public class IciqlSuite { }
/**
+ * Drops all tables from the current database.
+ *
+ * @return the current database
+ */
+ public static void dropAllTables(Db db) {
+
+ }
+
+ /**
* Returns the name of the underlying database engine for the Db object.
*
* @param db
@@ -146,6 +195,16 @@ public class IciqlSuite { }
/**
+ * Returns true if the underlying database engine is MySQL.
+ *
+ * @param db
+ * @return true if underlying database engine is MySQL
+ */
+ public static boolean isMySQL(Db db) {
+ return IciqlSuite.getDatabaseEngineName(db).equals("MySQL");
+ }
+
+ /**
* Gets the default schema of the underlying database engine.
*
* @param db
@@ -155,7 +214,11 @@ public class IciqlSuite { if (isDerby(db)) {
// Derby sets default schema name to username
return username.toUpperCase();
+ } else if (isMySQL(db)) {
+ // MySQL does not have schemas
+ return null;
}
+
return "PUBLIC";
}
@@ -206,8 +269,8 @@ public class IciqlSuite { SuiteClasses suiteClasses = IciqlSuite.class.getAnnotation(SuiteClasses.class);
long quickestDatabase = Long.MAX_VALUE;
- String dividerMajor = buildDivider('*', 70);
- String dividerMinor = buildDivider('-', 70);
+ String dividerMajor = buildDivider('*', 79);
+ String dividerMinor = buildDivider('-', 79);
// Header
out.println(dividerMajor);
@@ -225,7 +288,7 @@ public class IciqlSuite { showProperty("available processors", "" + Runtime.getRuntime().availableProcessors());
showProperty(
"available memory",
- MessageFormat.format("{0,number,#.0} GB", ((double) Runtime.getRuntime().maxMemory())
+ MessageFormat.format("{0,number,0.0} GB", ((double) Runtime.getRuntime().maxMemory())
/ (1024 * 1024)));
out.println();
@@ -246,32 +309,42 @@ public class IciqlSuite { statementWriter.append("\n\n");
}
- System.setProperty("iciql.url", testDb.url);
- Result result = JUnitCore.runClasses(suiteClasses.value());
- testDb.runtime = result.getRunTime();
- if (testDb.runtime < quickestDatabase) {
- quickestDatabase = testDb.runtime;
- }
- testDb.statements = StatementLogger.getTotalCount() - lastCount;
- // reset total count for next database
- lastCount = StatementLogger.getTotalCount();
-
- out.println(MessageFormat.format(
- "{0} tests ({1} failures, {2} ignores) {3} statements in {4,number,0.000} secs",
- result.getRunCount(), result.getFailureCount(), result.getIgnoreCount(),
- testDb.statements, result.getRunTime() / 1000f));
-
- if (result.getFailureCount() == 0) {
- out.println();
+ if (testDb.getVersion().equals("OFFLINE")) {
+ // Database not available
+ out.println("Skipping. Could not find " + testDb.url);
} else {
- for (Failure failure : result.getFailures()) {
- out.println(MessageFormat.format("\n + {0}\n {1}\n", failure.getTestHeader(),
- failure.getMessage()));
+ // Test database
+ System.setProperty("iciql.url", testDb.url);
+ Result result = JUnitCore.runClasses(suiteClasses.value());
+ testDb.runtime = result.getRunTime();
+ if (testDb.runtime < quickestDatabase) {
+ quickestDatabase = testDb.runtime;
+ }
+ testDb.statements = StatementLogger.getTotalCount() - lastCount;
+ // reset total count for next database
+ lastCount = StatementLogger.getTotalCount();
+
+ out.println(MessageFormat.format(
+ "{0} tests ({1} failures, {2} ignores) {3} statements in {4,number,0.000} secs",
+ result.getRunCount(), result.getFailureCount(), result.getIgnoreCount(),
+ testDb.statements, result.getRunTime() / 1000f));
+
+ if (result.getFailureCount() == 0) {
+ out.println();
+ out.println(" 100% successful test suite run.");
+ out.println();
+ } else {
+ for (Failure failure : result.getFailures()) {
+ out.println(MessageFormat.format("\n + {0}\n {1}", failure.getTestHeader(),
+ failure.getMessage()));
+ }
+ out.println();
}
}
}
// Display runtime results sorted by performance leader
+ out.println();
out.println(dividerMajor);
out.println(MessageFormat.format("{0} {1} ({2}) test suite performance results", Constants.NAME,
Constants.VERSION, Constants.VERSION_DATE));
@@ -281,6 +354,12 @@ public class IciqlSuite { @Override
public int compare(TestDb o1, TestDb o2) {
+ if (o1.runtime == 0) {
+ return 1;
+ }
+ if (o2.runtime == 0) {
+ return -1;
+ }
if (o1.runtime == o2.runtime) {
return 0;
}
@@ -292,11 +371,11 @@ public class IciqlSuite { });
for (TestDb testDb : dbs) {
out.println(MessageFormat.format(
- "{0} {1} {2,number,0.0} stats/sec {3,number,0.000} secs ({4,number,#.0}x)",
- StringUtils.pad(testDb.name, 6, " ", true),
- StringUtils.pad(testDb.getVersion(), 22, " ", true),
- ((double) testDb.statements)/ (testDb.runtime/1000d), testDb.runtime / 1000f,
- ((double) testDb.runtime) / quickestDatabase));
+ "{0} {1} {2,number,0} stats/sec {3,number,0.000} secs ({4,number,0.0}x)",
+ StringUtils.pad(testDb.name, 20, " ", true),
+ StringUtils.pad(testDb.getVersion(), 22, " ", true), ((double) testDb.statements)
+ / (testDb.runtime / 1000d), testDb.runtime / 1000f, ((double) testDb.runtime)
+ / quickestDatabase));
}
// close PrintStream and restore System.err
@@ -362,8 +441,8 @@ public class IciqlSuite { version = db.getConnection().getMetaData().getDatabaseProductVersion();
db.close();
return version;
- } catch (SQLException s) {
- version = "";
+ } catch (Throwable t) {
+ version = "OFFLINE";
}
}
return version;
diff --git a/tests/com/iciql/test/ModelsTest.java b/tests/com/iciql/test/ModelsTest.java index 9bbf450..d2e02fa 100644 --- a/tests/com/iciql/test/ModelsTest.java +++ b/tests/com/iciql/test/ModelsTest.java @@ -23,7 +23,6 @@ import static org.junit.Assert.assertTrue; import java.sql.SQLException; import java.text.MessageFormat; import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; import org.junit.After; import org.junit.Before; @@ -33,15 +32,12 @@ import org.junit.rules.ErrorCollector; import com.iciql.Db; import com.iciql.DbInspector; -import com.iciql.DbUpgrader; -import com.iciql.DbVersion; -import com.iciql.Iciql.IQVersion; import com.iciql.ValidationRemark; import com.iciql.test.models.Product; import com.iciql.test.models.ProductAnnotationOnly; import com.iciql.test.models.ProductMixedAnnotation; import com.iciql.test.models.SupportedTypes; -import com.iciql.test.models.SupportedTypes.SupportedTypes2; +import com.iciql.util.StringUtils; /** * Test that the mapping between classes and tables is done correctly. @@ -72,13 +68,9 @@ public class ModelsTest { @Test public void testValidateModels() { - boolean isH2 = IciqlSuite.isH2(db); - boolean isDerby = IciqlSuite.isDerby(db); String schemaName = IciqlSuite.getDefaultSchema(db); - DbInspector inspector = new DbInspector(db); - validateModel(inspector, schemaName, new Product(), 3); - validateModel(inspector, schemaName, new ProductAnnotationOnly(), (isH2 || isDerby) ? 2 : 3); + validateModel(inspector, schemaName, new ProductAnnotationOnly(), 2); validateModel(inspector, schemaName, new ProductMixedAnnotation(), 4); } @@ -95,8 +87,14 @@ public class ModelsTest { errorCollector.addError(new SQLException(remark.toString())); } } - assertTrue(remarks.get(0).message.equals(MessageFormat.format("@IQSchema(name={0})", schemaName))); - assertEquals(sb.toString(), expected, remarks.size()); + + if (StringUtils.isNullOrEmpty(schemaName)) { + // no schema expected + assertEquals(sb.toString(), expected - 1, remarks.size()); + } else { + assertEquals(sb.toString(), expected, remarks.size()); + assertEquals(MessageFormat.format("@IQSchema(\"{0}\")", schemaName), remarks.get(0).message); + } } @Test @@ -133,65 +131,4 @@ public class ModelsTest { assertEquals(1489, models.get(0).length()); } } - - @Test - public void testDatabaseUpgrade() { - // insert a database version record - db.insert(new DbVersion(1)); - - TestDbUpgrader dbUpgrader = new TestDbUpgrader(); - db.setDbUpgrader(dbUpgrader); - - List<SupportedTypes> original = SupportedTypes.createList(); - db.insertAll(original); - - assertEquals(1, dbUpgrader.oldVersion.get()); - assertEquals(2, dbUpgrader.newVersion.get()); - } - - @Test - public void testTableUpgrade() { - Db db = IciqlSuite.openNewDb(); - - // insert first, this will create version record automatically - List<SupportedTypes> original = SupportedTypes.createList(); - db.insertAll(original); - - // reset the dbUpgrader (clears the update check cache) - TestDbUpgrader dbUpgrader = new TestDbUpgrader(); - db.setDbUpgrader(dbUpgrader); - - SupportedTypes2 s2 = new SupportedTypes2(); - - List<SupportedTypes2> types = db.from(s2).select(); - assertEquals(10, types.size()); - assertEquals(1, dbUpgrader.oldVersion.get()); - assertEquals(2, dbUpgrader.newVersion.get()); - db.close(); - } - - /** - * A sample database upgrader class. - */ - @IQVersion(2) - class TestDbUpgrader implements DbUpgrader { - final AtomicInteger oldVersion = new AtomicInteger(0); - final AtomicInteger newVersion = new AtomicInteger(0); - - public boolean upgradeTable(Db db, String schema, String table, int fromVersion, int toVersion) { - // just claims success on upgrade request - oldVersion.set(fromVersion); - newVersion.set(toVersion); - return true; - } - - public boolean upgradeDatabase(Db db, int fromVersion, int toVersion) { - // just claims success on upgrade request - oldVersion.set(fromVersion); - newVersion.set(toVersion); - return true; - } - - } - } diff --git a/tests/com/iciql/test/SamplesTest.java b/tests/com/iciql/test/SamplesTest.java index f16672a..17c2151 100644 --- a/tests/com/iciql/test/SamplesTest.java +++ b/tests/com/iciql/test/SamplesTest.java @@ -91,6 +91,7 @@ public class SamplesTest { TestReverse check = db.from(new TestReverse()).selectFirst();
assertEquals(t.name, check.name);
assertEquals(t.id, check.id);
+ db.executeUpdate("DROP TABLE testreverse");
}
@Test
@@ -238,7 +239,8 @@ public class SamplesTest { public void testIsNull() {
Product p = new Product();
String sql = db.from(p).whereTrue(isNull(p.productName)).getSQL();
- assertEquals("SELECT * FROM Product WHERE (productName IS NULL)", sql);
+ assertEquals("SELECT * FROM Product WHERE (" + db.getDialect().prepareColumnName("productName")
+ + " IS NULL)", sql);
}
@Test
@@ -258,11 +260,13 @@ public class SamplesTest { public void testOrAndNot() {
Product p = new Product();
String sql = db.from(p).whereTrue(not(isNull(p.productName))).getSQL();
- assertEquals("SELECT * FROM Product WHERE (NOT productName IS NULL)", sql);
+ String productName = db.getDialect().prepareColumnName("productName");
+ assertEquals("SELECT * FROM Product WHERE (NOT " + productName + " IS NULL)", sql);
sql = db.from(p).whereTrue(not(isNull(p.productName))).getSQL();
- assertEquals("SELECT * FROM Product WHERE (NOT productName IS NULL)", sql);
+ assertEquals("SELECT * FROM Product WHERE (NOT " + productName + " IS NULL)", sql);
sql = db.from(p).whereTrue(db.test(p.productId).is(1)).getSQL();
- assertEquals("SELECT * FROM Product WHERE ((productId = ?))", sql);
+ String productId = db.getDialect().prepareColumnName("productId");
+ assertEquals("SELECT * FROM Product WHERE ((" + productId + " = ?))", sql);
}
@Test
@@ -316,8 +320,24 @@ public class SamplesTest { .lessThan(java.sql.Timestamp.valueOf("2005-05-05 05:05:05")).and(co.name).is("hello")
.and(co.time).lessThan(java.sql.Time.valueOf("23:23:23")).and(co.value)
.is(new BigDecimal("1")).getSQL();
- assertEquals("SELECT * FROM ComplexObject WHERE id = ? AND amount = ? "
- + "AND birthday < ? AND created < ? AND name = ? AND time < ? AND value = ?", sql);
+
+ StringBuilder sb = new StringBuilder();
+ sb.append("SELECT * FROM ComplexObject WHERE ");
+ sb.append(db.getDialect().prepareColumnName("id"));
+ sb.append(" = ? AND ");
+ sb.append(db.getDialect().prepareColumnName("amount"));
+ sb.append(" = ? AND ");
+ sb.append(db.getDialect().prepareColumnName("birthday"));
+ sb.append(" < ? AND ");
+ sb.append(db.getDialect().prepareColumnName("created"));
+ sb.append(" < ? AND ");
+ sb.append(db.getDialect().prepareColumnName("name"));
+ sb.append(" = ? AND ");
+ sb.append(db.getDialect().prepareColumnName("time"));
+ sb.append(" < ? AND ");
+ sb.append(db.getDialect().prepareColumnName("value"));
+ sb.append(" = ?");
+ assertEquals(sb.toString(), sql);
long count = db.from(co).where(co.id).is(1).and(co.amount).is(1L).and(co.birthday)
.lessThan(new java.util.Date()).and(co.created)
@@ -340,7 +360,14 @@ public class SamplesTest { return co.id == x && co.name.equals(name) && co.name.equals("hello");
}
}).getSQL();
- assertEquals("SELECT * FROM ComplexObject WHERE id=? AND ?=name AND 'hello'=name", sql);
+ StringBuilder sb = new StringBuilder();
+ sb.append("SELECT * FROM ComplexObject WHERE ");
+ sb.append(db.getDialect().prepareColumnName("id"));
+ sb.append("=? AND ?=");
+ sb.append(db.getDialect().prepareColumnName("name"));
+ sb.append(" AND 'hello'=");
+ sb.append(db.getDialect().prepareColumnName("name"));
+ assertEquals(sb.toString(), sql);
long count = db.from(co).where(new Filter() {
public boolean where() {
diff --git a/tests/com/iciql/test/UUIDTest.java b/tests/com/iciql/test/UUIDTest.java index bc3bd12..9b88cb8 100644 --- a/tests/com/iciql/test/UUIDTest.java +++ b/tests/com/iciql/test/UUIDTest.java @@ -24,6 +24,7 @@ import java.util.List; import java.util.UUID; import org.junit.After; +import org.junit.Assume; import org.junit.Before; import org.junit.Test; @@ -52,10 +53,9 @@ public class UUIDTest { @Test public void testUUIDs() throws Exception { - if (!IciqlSuite.isH2(db)) { - // do not test non-H2 databases - return; - } + // do not test non-H2 databases + Assume.assumeTrue(IciqlSuite.isH2(db)); + List<UUIDRecord> originals = UUIDRecord.getList(); db.insertAll(originals); UUIDRecord u = new UUIDRecord(); diff --git a/tests/com/iciql/test/UpgradesTest.java b/tests/com/iciql/test/UpgradesTest.java new file mode 100644 index 0000000..d4c15e1 --- /dev/null +++ b/tests/com/iciql/test/UpgradesTest.java @@ -0,0 +1,134 @@ +/*
+ * Copyright 2011 James Moger.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.iciql.test;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.junit.Test;
+
+import com.iciql.Db;
+import com.iciql.DbUpgrader;
+import com.iciql.Iciql.IQVersion;
+import com.iciql.test.models.Product;
+import com.iciql.test.models.SupportedTypes;
+import com.iciql.test.models.SupportedTypes.SupportedTypes2;
+
+/**
+ * Tests the database and table upgrade functions.
+ *
+ */
+public class UpgradesTest {
+
+ @Test
+ public void testDatabaseUpgrade() {
+ Db db = IciqlSuite.openNewDb();
+
+ List<Product> products = Product.getList();
+
+ // set the v1 upgrader and insert a record.
+ // this will trigger the upgrade.
+ V1DbUpgrader v1 = new V1DbUpgrader();
+ db.setDbUpgrader(v1);
+ db.insert(products.get(0));
+
+ // confirm that upgrade occurred
+ assertEquals(0, v1.oldVersion.get());
+ assertEquals(1, v1.newVersion.get());
+
+ // open a second connection to the database
+ // and then apply the v2 upgrade.
+ // For H2 its important to keep the first connection
+ // alive so that the database is not destroyed.
+ Db db2 = IciqlSuite.openCurrentDb();
+
+ // set the v2 upgrader and insert a record.
+ // this will trigger the upgrade.
+ V2DbUpgrader v2 = new V2DbUpgrader();
+ db2.setDbUpgrader(v2);
+ db2.insert(products.get(1));
+
+ // confirm that upgrade occurred
+ assertEquals(1, v2.oldVersion.get());
+ assertEquals(2, v2.newVersion.get());
+
+ db.executeUpdate("DROP TABLE iq_versions");
+ db.close();
+ db2.close();
+ }
+
+ @Test
+ public void testTableUpgrade() {
+ Db db = IciqlSuite.openNewDb();
+
+ // insert first, this will create version record automatically
+ List<SupportedTypes> original = SupportedTypes.createList();
+ db.insertAll(original);
+
+ // reset the dbUpgrader (clears the update check cache)
+ V2DbUpgrader dbUpgrader = new V2DbUpgrader();
+ db.setDbUpgrader(dbUpgrader);
+
+ SupportedTypes2 s2 = new SupportedTypes2();
+
+ List<SupportedTypes2> types = db.from(s2).select();
+ assertEquals(10, types.size());
+ assertEquals(1, dbUpgrader.oldVersion.get());
+ assertEquals(2, dbUpgrader.newVersion.get());
+ db.executeUpdate("DROP TABLE iq_versions");
+ db.close();
+ }
+
+ /**
+ * A sample database upgrader class.
+ */
+ class BaseDbUpgrader implements DbUpgrader {
+ final AtomicInteger oldVersion = new AtomicInteger(0);
+ final AtomicInteger newVersion = new AtomicInteger(0);
+
+ public boolean upgradeTable(Db db, String schema, String table, int fromVersion, int toVersion) {
+ // just claims success on upgrade request
+ oldVersion.set(fromVersion);
+ newVersion.set(toVersion);
+ return true;
+ }
+
+ public boolean upgradeDatabase(Db db, int fromVersion, int toVersion) {
+ // just claims success on upgrade request
+ oldVersion.set(fromVersion);
+ newVersion.set(toVersion);
+ return true;
+ }
+ }
+
+ /**
+ * A sample V1 database upgrader class.
+ */
+ @IQVersion(1)
+ class V1DbUpgrader extends BaseDbUpgrader {
+ }
+
+ /**
+ * A sample V2 database upgrader class.
+ */
+ @IQVersion(2)
+ class V2DbUpgrader extends BaseDbUpgrader {
+ }
+
+}
diff --git a/tests/com/iciql/test/models/PrimitivesModel.java b/tests/com/iciql/test/models/PrimitivesModel.java index 0affd28..44e8b9b 100644 --- a/tests/com/iciql/test/models/PrimitivesModel.java +++ b/tests/com/iciql/test/models/PrimitivesModel.java @@ -21,6 +21,7 @@ import java.util.Random; import com.iciql.Iciql.IQColumn;
import com.iciql.Iciql.IQTable;
+import com.iciql.test.IciqlSuite;
/**
* Primitive types model.
@@ -67,8 +68,8 @@ public class PrimitivesModel { same &= myShort == p.myShort;
same &= myByte == p.myByte;
same &= myBoolean == p.myBoolean;
- same &= myDouble == p.myDouble;
- same &= myFloat == p.myFloat;
+ same &= IciqlSuite.equivalentTo(myDouble, p.myDouble);
+ same &= IciqlSuite.equivalentTo(myFloat, p.myFloat);
return same;
}
diff --git a/tests/com/iciql/test/models/ProductAnnotationOnly.java b/tests/com/iciql/test/models/ProductAnnotationOnly.java index 80bf0a1..e4de22d 100644 --- a/tests/com/iciql/test/models/ProductAnnotationOnly.java +++ b/tests/com/iciql/test/models/ProductAnnotationOnly.java @@ -34,12 +34,9 @@ import com.iciql.Iciql.IndexType; @IQIndexes({ @IQIndex({ "name", "cat" }), @IQIndex(name = "nameidx", type = IndexType.HASH, value = "name") }) public class ProductAnnotationOnly { - @IQColumn(autoIncrement = true) - public Integer autoIncrement; - public String unmappedField; - @IQColumn(name = "id") + @IQColumn(name = "id", autoIncrement = true) public Integer productId; @IQColumn(name = "cat", length = 15, trim = true) diff --git a/tests/com/iciql/test/models/SupportedTypes.java b/tests/com/iciql/test/models/SupportedTypes.java index 596af74..1aaa833 100644 --- a/tests/com/iciql/test/models/SupportedTypes.java +++ b/tests/com/iciql/test/models/SupportedTypes.java @@ -32,6 +32,7 @@ import com.iciql.Iciql.IQIndexes; import com.iciql.Iciql.IQTable; import com.iciql.Iciql.IQVersion; import com.iciql.Iciql.IndexType; +import com.iciql.test.IciqlSuite; import com.iciql.test.models.EnumModels.Tree; import com.iciql.util.Utils; @@ -166,8 +167,8 @@ public class SupportedTypes { same &= myShort.equals(s.myShort); same &= myInteger.equals(s.myInteger); same &= myLong.equals(s.myLong); - same &= myFloat.equals(s.myFloat); - same &= myDouble.equals(s.myDouble); + same &= IciqlSuite.equivalentTo(myFloat, s.myFloat); + same &= IciqlSuite.equivalentTo(myDouble, s.myDouble); same &= myBigDecimal.compareTo(s.myBigDecimal) == 0; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); same &= df.format(myUtilDate).equals(df.format(s.myUtilDate)); |