diff options
author | James Moger <james.moger@gmail.com> | 2011-08-11 14:10:28 -0400 |
---|---|---|
committer | James Moger <james.moger@gmail.com> | 2011-08-11 14:10:28 -0400 |
commit | 64184c246a552024dda12f5ee0830ec5b3c6161f (patch) | |
tree | 24c2aa5228bad6d80557085611d46b2f88829fcc | |
parent | 0333ed4cf0b5db3f9ffcb0da31787f6e44139af5 (diff) | |
download | iciql-64184c246a552024dda12f5ee0830ec5b3c6161f.tar.gz iciql-64184c246a552024dda12f5ee0830ec5b3c6161f.zip |
Restructured test suite to run against multiple databases.
20 files changed, 229 insertions, 96 deletions
diff --git a/tests/com/iciql/test/AliasMapTest.java b/tests/com/iciql/test/AliasMapTest.java index 4d19bd0..f0d5c15 100644 --- a/tests/com/iciql/test/AliasMapTest.java +++ b/tests/com/iciql/test/AliasMapTest.java @@ -37,7 +37,7 @@ public class AliasMapTest { @Test public void testAliasMapping() throws Exception { - Db db = Db.open("jdbc:h2:mem:", "sa", "sa"); + Db db = IciqlSuite.openDb(); db.insertAll(Product.getList()); Product p = new Product(); diff --git a/tests/com/iciql/test/AnnotationsTest.java b/tests/com/iciql/test/AnnotationsTest.java index 92e0d5e..897acd6 100644 --- a/tests/com/iciql/test/AnnotationsTest.java +++ b/tests/com/iciql/test/AnnotationsTest.java @@ -27,7 +27,6 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; -import org.h2.constant.ErrorCode; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -39,6 +38,8 @@ import com.iciql.test.models.ProductAnnotationOnly; import com.iciql.test.models.ProductInheritedAnnotation; import com.iciql.test.models.ProductMixedAnnotation; import com.iciql.test.models.ProductNoCreateTable; +import com.iciql.util.JdbcUtils; +import com.iciql.util.Utils; /** * Test annotation processing. @@ -54,7 +55,7 @@ public class AnnotationsTest { @Before public void setUp() { - db = Db.open("jdbc:h2:mem:", "sa", "sa"); + db = IciqlSuite.openDb(); db.insertAll(Product.getList()); db.insertAll(ProductAnnotationOnly.getList()); db.insertAll(ProductMixedAnnotation.getList()); @@ -69,9 +70,14 @@ public class AnnotationsTest { public void testIndexCreation() throws SQLException { // test indexes are created, and columns are in the right order DatabaseMetaData meta = db.getConnection().getMetaData(); + boolean isH2 = meta.getDatabaseProductName().equals("H2"); ResultSet rs = meta.getIndexInfo(null, "PUBLIC", "ANNOTATEDPRODUCT", false, true); + // first index is primary key index + // H2 gives this a testable name. assertTrue(rs.next()); - assertStartsWith(rs.getString("INDEX_NAME"), "PRIMARY_KEY"); + if (isH2) { + assertStartsWith(rs.getString("INDEX_NAME"), "PRIMARY_KEY"); + } assertTrue(rs.next()); assertStartsWith(rs.getString("INDEX_NAME"), "ANNOTATEDPRODUCT_0"); assertStartsWith(rs.getString("COLUMN_NAME"), "NAME"); @@ -94,18 +100,21 @@ public class AnnotationsTest { // test IQTable.annotationsOnly=true // public String unmappedField is ignored by iciql - assertEquals(0, db.from(p).where(p.unmappedField).is("unmapped").selectCount()); - - // test IQColumn.autoIncrement=true + try { + db.from(p).where(p.unmappedField).is("unmapped").selectCount(); + assertTrue("this should never execute", false); + } catch (IciqlException e) { + assertEquals(IciqlException.CODE_UNMAPPED_FIELD, e.getIciqlCode()); + } + // 10 objects, 10 autoIncremented unique values - assertEquals(10, db.from(p).selectDistinct(p.autoIncrement).size()); + assertEquals(10, db.from(p).selectDistinct(p.productName).size()); // test IQTable.primaryKey=id try { db.insertAll(ProductAnnotationOnly.getList()); - } catch (IciqlException r) { - SQLException s = (SQLException) r.getCause(); - assertEquals(ErrorCode.DUPLICATE_KEY_1, s.getErrorCode()); + } catch (IciqlException e) { + assertEquals(IciqlException.CODE_DUPLICATE_KEY, e.getIciqlCode()); } } @@ -123,9 +132,8 @@ public class AnnotationsTest { // test IQColumn.primaryKey=true try { db.insertAll(ProductMixedAnnotation.getList()); - } catch (IciqlException r) { - SQLException s = (SQLException) r.getCause(); - assertEquals(ErrorCode.DUPLICATE_KEY_1, s.getErrorCode()); + } catch (IciqlException e) { + assertEquals(IciqlException.CODE_DUPLICATE_KEY, e.getIciqlCode()); } } @@ -171,9 +179,8 @@ public class AnnotationsTest { // tests IQTable.createTableIfRequired=false try { db.insertAll(ProductNoCreateTable.getList()); - } catch (IciqlException r) { - SQLException s = (SQLException) r.getCause(); - assertEquals(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, s.getErrorCode()); + } catch (IciqlException e) { + assertEquals(IciqlException.CODE_TABLE_NOT_FOUND, e.getIciqlCode()); } } diff --git a/tests/com/iciql/test/BooleanModelTest.java b/tests/com/iciql/test/BooleanModelTest.java index 0287e06..6111e72 100644 --- a/tests/com/iciql/test/BooleanModelTest.java +++ b/tests/com/iciql/test/BooleanModelTest.java @@ -39,7 +39,7 @@ public class BooleanModelTest { @Test
public void testBooleanColumn() {
- Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
+ Db db = IciqlSuite.openDb();
db.insertAll(BooleanModel.getList());
BooleanAsIntModel b = new BooleanAsIntModel();
List<BooleanAsIntModel> models = db.from(b).select();
@@ -82,7 +82,7 @@ public class BooleanModelTest { @Test
public void testIntColumn() {
- Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
+ Db db = IciqlSuite.openDb();
// insert INT column
db.insertAll(BooleanAsIntModel.getList());
diff --git a/tests/com/iciql/test/ClobTest.java b/tests/com/iciql/test/ClobTest.java index 1f2eff4..aada364 100644 --- a/tests/com/iciql/test/ClobTest.java +++ b/tests/com/iciql/test/ClobTest.java @@ -38,14 +38,14 @@ public class ClobTest { @Test public void testClob() throws Exception { String create = "CREATE TABLE CLOB_TEST(ID INT PRIMARY KEY, WORDS {0})"; - Db db = Db.open("jdbc:h2:mem:", "sa", "sa"); + Db db = IciqlSuite.openDb(); db.executeUpdate(MessageFormat.format(create, "VARCHAR(255)")); db.insertAll(StringRecord.getList()); testSimpleUpdate(db, "VARCHAR fail"); db.close(); - db = Db.open("jdbc:h2:mem:", "sa", "sa"); - db.executeUpdate(MessageFormat.format(create, "TEXT")); + db = IciqlSuite.openDb(); + db.executeUpdate(MessageFormat.format(create, "CLOB")); db.insertAll(StringRecord.getList()); testSimpleUpdate(db, "CLOB fail because of single quote artifacts"); db.close(); diff --git a/tests/com/iciql/test/ConcurrencyTest.java b/tests/com/iciql/test/ConcurrencyTest.java index 88b23f6..1641eb1 100644 --- a/tests/com/iciql/test/ConcurrencyTest.java +++ b/tests/com/iciql/test/ConcurrencyTest.java @@ -27,6 +27,7 @@ import org.junit.Before; import org.junit.Test; import com.iciql.Db; +import com.iciql.IciqlException; import com.iciql.Query; import com.iciql.test.models.Product; import com.iciql.util.Utils; @@ -41,7 +42,7 @@ public class ConcurrencyTest { @Before public void setUp() { - db = Db.open("jdbc:h2:mem:", "sa", "sa"); + db = IciqlSuite.openDb(); db.insertAll(Product.getList()); } @@ -58,7 +59,12 @@ public class ConcurrencyTest { Query<Product> query2 = db.from(p); // if you could share alias instances both counts should be equal - long count1 = query1.where(p.category).is("Beverages").selectCount(); + long count1 = 0; + try { + count1 = query1.where(p.category).is("Beverages").selectCount(); + } catch (IciqlException e) { + assertEquals(IciqlException.CODE_UNMAPPED_FIELD, e.getIciqlCode()); + } long count2 = query2.where(p.category).is("Beverages").selectCount(); // but they aren't @@ -84,10 +90,14 @@ public class ConcurrencyTest { try { int testCase = testNumber % 10; test(testCase, p); - } catch (Throwable rex) { + } catch (AssertionError e) { + failures.incrementAndGet(); + } catch (IciqlException e) { failures.incrementAndGet(); - System.err.println("EXPECTED ERROR"); - rex.printStackTrace(); + if (e.getIciqlCode() != IciqlException.CODE_UNMAPPED_FIELD) { + System.err.println("UNEXPECTED ERROR in testConcurrencyFinal()"); + e.printStackTrace(); + } } } }, "ICIQL-" + i); @@ -115,9 +125,14 @@ public class ConcurrencyTest { try { int testCase = testNumber % 10; test(testCase, tl.get()); - } catch (Throwable rex) { + } catch (AssertionError e) { + failures.incrementAndGet(); + } catch (IciqlException e) { failures.incrementAndGet(); - rex.printStackTrace(); + if (e.getIciqlCode() != IciqlException.CODE_UNMAPPED_FIELD) { + System.err.println("UNEXPECTED ERROR in testConcurrencyThreadLocal()"); + e.printStackTrace(); + } } } }, "ICIQL-" + i); diff --git a/tests/com/iciql/test/DefaultValuesTest.java b/tests/com/iciql/test/DefaultValuesTest.java index b39dd7c..7c7c84f 100644 --- a/tests/com/iciql/test/DefaultValuesTest.java +++ b/tests/com/iciql/test/DefaultValuesTest.java @@ -34,7 +34,7 @@ public class DefaultValuesTest { @Test
public void testDefaultObjectValues() {
- Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
+ Db db = IciqlSuite.openDb();
// insert random model
DefaultValuesModel model = new DefaultValuesModel();
diff --git a/tests/com/iciql/test/EnumsTest.java b/tests/com/iciql/test/EnumsTest.java index b66aa9e..fd6af2f 100644 --- a/tests/com/iciql/test/EnumsTest.java +++ b/tests/com/iciql/test/EnumsTest.java @@ -40,7 +40,7 @@ public class EnumsTest { @Before
public void setUp() {
- db = Db.open("jdbc:h2:mem:", "sa", "sa");
+ db = IciqlSuite.openDb();
db.insertAll(EnumIdModel.createList());
db.insertAll(EnumOrdinalModel.createList());
db.insertAll(EnumStringModel.createList());
diff --git a/tests/com/iciql/test/IciqlSuite.java b/tests/com/iciql/test/IciqlSuite.java index c9dab77..5842a2e 100644 --- a/tests/com/iciql/test/IciqlSuite.java +++ b/tests/com/iciql/test/IciqlSuite.java @@ -15,23 +15,107 @@ */
package com.iciql.test;
+import java.sql.SQLException;
+import java.text.MessageFormat;
+import java.util.concurrent.atomic.AtomicInteger;
+
import org.junit.Assert;
+import org.junit.runner.JUnitCore;
+import org.junit.runner.Result;
import org.junit.runner.RunWith;
+import org.junit.runner.notification.Failure;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
+import com.iciql.Db;
+
/**
* JUnit 4 iciql test suite.
*
+ * By default this test suite will run against the H2 database. You can change
+ * this by switching the DEFAULT_TEST_DB value.
+ * <p>
+ * Alternatively, you can run this class an application which will run all tests
+ * for all tested databases.
+ *
*/
@RunWith(Suite.class)
@SuiteClasses({ AliasMapTest.class, AnnotationsTest.class, BooleanModelTest.class, ClobTest.class,
ConcurrencyTest.class, EnumsTest.class, ModelsTest.class, PrimitivesTest.class,
- RuntimeQueryTest.class, SamplesTest.class, StatementLoggerTest.class, UpdateTest.class,
- UUIDTest.class })
+ RuntimeQueryTest.class, SamplesTest.class, UpdateTest.class, UUIDTest.class })
public class IciqlSuite {
- public static void assertStartsWith(String a, String b) {
- Assert.assertTrue(a.startsWith(b));
+ 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 DEFAULT_TEST_DB = TEST_DBS[0];
+
+ private static AtomicInteger openCount = new AtomicInteger(0);
+
+ 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() {
+ 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");
+ }
+
+ public static String getDatabaseName(Db db) {
+ String database = "";
+ try {
+ database = db.getConnection().getMetaData().getDatabaseProductName();
+ } catch (SQLException s) {
+ }
+ return database;
+ }
+
+ public static void main(String... args) {
+ SuiteClasses suiteClasses = IciqlSuite.class.getAnnotation(SuiteClasses.class);
+ for (TestDb testDb : TEST_DBS) {
+ System.out.println("*********************************************");
+ System.out.println("Testing " + testDb.name + " " + testDb.getVersion());
+ System.out.println("*********************************************");
+ 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",
+ result.getRunCount(), result.getFailureCount(), result.getIgnoreCount(),
+ result.getRunTime()));
+ for (Failure failure : result.getFailures()) {
+ System.out.println(MessageFormat.format("{0}: {1}", failure.getTestHeader(),
+ failure.getMessage()));
+ }
+ System.out.println();
+ }
+ }
+
+ /**
+ * Represents a test database url.
+ */
+ private static class TestDb {
+ final String name;
+ final String url;
+
+ TestDb(String name, String url) {
+ this.name = name;
+ this.url = url;
+ }
+
+ String getVersion() {
+ try {
+ Db db = Db.open(url, "sa", "sa");
+ String version = db.getConnection().getMetaData().getDatabaseProductVersion();
+ db.close();
+ return version;
+ } catch (SQLException s) {
+ }
+ return "";
+ }
}
}
diff --git a/tests/com/iciql/test/ModelsTest.java b/tests/com/iciql/test/ModelsTest.java index 652bc4f..3a6b4e7 100644 --- a/tests/com/iciql/test/ModelsTest.java +++ b/tests/com/iciql/test/ModelsTest.java @@ -62,7 +62,7 @@ public class ModelsTest { @Before public void setUp() { - db = Db.open("jdbc:h2:mem:", "sa", "sa"); + db = IciqlSuite.openDb(); db.insertAll(Product.getList()); db.insertAll(ProductAnnotationOnly.getList()); db.insertAll(ProductMixedAnnotation.getList()); @@ -75,22 +75,28 @@ public class ModelsTest { @Test public void testValidateModels() { + boolean isH2 = IciqlSuite.getDatabaseName(db).equals("H2"); DbInspector inspector = new DbInspector(db); - validateModel(inspector, new Product()); - validateModel(inspector, new ProductAnnotationOnly()); - validateModel(inspector, new ProductMixedAnnotation()); + validateModel(inspector, new Product(), 3); + validateModel(inspector, new ProductAnnotationOnly(), isH2 ? 2 : 3); + validateModel(inspector, new ProductMixedAnnotation(), isH2 ? 3 : 4); } - private void validateModel(DbInspector inspector, Object o) { + private void validateModel(DbInspector inspector, Object o, int expected) { List<ValidationRemark> remarks = inspector.validateModel(o, false); - assertTrue("Validation remarks are null for " + o.getClass().getName(), remarks != null); - log("Validation remarks for " + o.getClass().getName()); + assertTrue("validation remarks are null for " + o.getClass().getName(), remarks != null); + StringBuilder sb = new StringBuilder(); + sb.append("validation remarks for " + o.getClass().getName()); + sb.append('\n'); for (ValidationRemark remark : remarks) { - log(remark.toString()); + sb.append(remark.toString()); + sb.append('\n'); if (remark.isError()) { errorCollector.addError(new SQLException(remark.toString())); } } + assertTrue(remarks.get(0).message.equals("@IQSchema(name=PUBLIC)")); + assertEquals(sb.toString(), expected, remarks.size()); } @Test @@ -115,7 +121,13 @@ public class ModelsTest { true); assertEquals(1, models.size()); // a poor test, but a start - assertEquals(1456, models.get(0).length()); + String dbName = IciqlSuite.getDatabaseName(db); + if (dbName.equals("H2")) { + assertEquals(1478, models.get(0).length()); + } else if (dbName.startsWith("HSQL")) { + // HSQL uses Double instead of Float + assertEquals(1479, models.get(0).length()); + } } @Test @@ -135,7 +147,7 @@ public class ModelsTest { @Test public void testTableUpgrade() { - Db db = Db.open("jdbc:h2:mem:", "sa", "sa"); + Db db = IciqlSuite.openDb(); // insert first, this will create version record automatically List<SupportedTypes> original = SupportedTypes.createList(); diff --git a/tests/com/iciql/test/PrimitivesTest.java b/tests/com/iciql/test/PrimitivesTest.java index 2d591c3..1cdeb1c 100644 --- a/tests/com/iciql/test/PrimitivesTest.java +++ b/tests/com/iciql/test/PrimitivesTest.java @@ -30,7 +30,7 @@ public class PrimitivesTest { @Test
public void testPrimitives() {
- Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
+ Db db = IciqlSuite.openDb();
// insert random model
PrimitivesModel model = new PrimitivesModel();
diff --git a/tests/com/iciql/test/RuntimeQueryTest.java b/tests/com/iciql/test/RuntimeQueryTest.java index ff6fe2a..fa3ee1c 100644 --- a/tests/com/iciql/test/RuntimeQueryTest.java +++ b/tests/com/iciql/test/RuntimeQueryTest.java @@ -34,7 +34,7 @@ public class RuntimeQueryTest { @Test
public void testRuntimeQuery() {
- Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
+ Db db = IciqlSuite.openDb();
db.insertAll(Product.getList());
Product p = new Product();
@@ -50,7 +50,7 @@ public class RuntimeQueryTest { @Test
public void testExecuteQuery() throws SQLException {
- Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
+ Db db = IciqlSuite.openDb();
db.insertAll(Product.getList());
// test plain statement
@@ -68,7 +68,7 @@ public class RuntimeQueryTest { @Test
public void testBuildObjects() throws SQLException {
- Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
+ Db db = IciqlSuite.openDb();
db.insertAll(Product.getList());
// test plain statement
diff --git a/tests/com/iciql/test/SamplesTest.java b/tests/com/iciql/test/SamplesTest.java index 309bd5d..30723bb 100644 --- a/tests/com/iciql/test/SamplesTest.java +++ b/tests/com/iciql/test/SamplesTest.java @@ -28,6 +28,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue;
import java.math.BigDecimal;
+import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -59,7 +60,7 @@ public class SamplesTest { @Before
public void setUp() {
- db = Db.open("jdbc:h2:mem:", "sa", "sa");
+ db = IciqlSuite.openDb();
db.insertAll(Product.getList());
db.insertAll(Customer.getList());
db.insertAll(Order.getList());
@@ -82,7 +83,7 @@ public class SamplesTest { @Test
public void testReverseColumns() {
- db.executeUpdate("create table TestReverse(id int, name varchar, additional varchar)");
+ db.executeUpdate("create table TestReverse(id int, name varchar(10), additional varchar(10))");
TestReverse t = new TestReverse();
t.id = 10;
t.name = "Hello";
@@ -222,7 +223,7 @@ public class SamplesTest { final Customer c = new Customer();
final Order o = new Order();
List<CustOrder> orders = db.from(c).innerJoin(o).on(c.customerId).is(o.customerId).where(o.total)
- .lessThan(new BigDecimal("100.00")).orderBy(1).select(new CustOrder() {
+ .lessThan(new BigDecimal("100.00")).orderBy(c.customerId).select(new CustOrder() {
{
customerId = c.customerId;
orderId = o.orderId;
@@ -267,8 +268,10 @@ public class SamplesTest { @Test
public void testLength() {
Product p = new Product();
- List<Integer> lengths = db.from(p).where(length(p.productName)).lessThan(10).orderBy(1)
+ List<Integer> lengths = db.from(p).where(length(p.productName)).lessThan(10)
.selectDistinct(length(p.productName));
+ // Formerly used orderBy(1) here, but that is not portable across DBs
+ Collections.sort(lengths);
assertEquals("[4, 5]", lengths.toString());
}
@@ -310,15 +313,17 @@ public class SamplesTest { ComplexObject co = new ComplexObject();
String sql = db.from(co).where(co.id).is(1).and(co.amount).is(1L).and(co.birthday)
.lessThan(new java.util.Date()).and(co.created)
- .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);
+ .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);
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)
- .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")).selectCount();
+ .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")).selectCount();
assertEquals(1, count);
}
@@ -335,7 +340,7 @@ 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);
+ assertEquals("SELECT * FROM ComplexObject WHERE id=? AND ?=name AND 'hello'=name", sql);
long count = db.from(co).where(new Filter() {
public boolean where() {
@@ -393,14 +398,15 @@ public class SamplesTest { // };
final Product p = new Product();
- List<ProductGroup> list = db.from(p).groupBy(p.category).orderBy(1).select(new ProductGroup() {
- {
- category = p.category;
- productCount = count();
- }
- });
+ List<ProductGroup> list = db.from(p).groupBy(p.category).orderBy(p.category)
+ .select(new ProductGroup() {
+ {
+ category = p.category;
+ productCount = count();
+ }
+ });
- assertEquals("[Beverages:2, Condiments:5, " + "Meat/Poultry:1, Produce:1, Seafood:1]", list.toString());
+ assertEquals("[Beverages:2, Condiments:5, Meat/Poultry:1, Produce:1, Seafood:1]", list.toString());
}
}
diff --git a/tests/com/iciql/test/UUIDTest.java b/tests/com/iciql/test/UUIDTest.java index b35d623..b7f83e6 100644 --- a/tests/com/iciql/test/UUIDTest.java +++ b/tests/com/iciql/test/UUIDTest.java @@ -32,7 +32,9 @@ import com.iciql.Iciql.IQColumn; import com.iciql.Iciql.IQTable; /** - * Tests if UUID type. + * Tests of UUID type. + * <p> + * H2 only. */ public class UUIDTest { diff --git a/tests/com/iciql/test/UpdateTest.java b/tests/com/iciql/test/UpdateTest.java index 9ff5593..a4ee61c 100644 --- a/tests/com/iciql/test/UpdateTest.java +++ b/tests/com/iciql/test/UpdateTest.java @@ -41,7 +41,7 @@ public class UpdateTest { @Before public void setUp() throws Exception { - db = Db.open("jdbc:h2:mem:", "sa", "sa"); + db = IciqlSuite.openDb(); db.insertAll(Product.getList()); db.insertAll(Customer.getList()); db.insertAll(Order.getList()); diff --git a/tests/com/iciql/test/models/Customer.java b/tests/com/iciql/test/models/Customer.java index c2b8da9..a0bbb7e 100644 --- a/tests/com/iciql/test/models/Customer.java +++ b/tests/com/iciql/test/models/Customer.java @@ -20,12 +20,19 @@ package com.iciql.test.models; import java.util.Arrays;
import java.util.List;
+import com.iciql.Iciql.IQColumn;
+import com.iciql.Iciql.IQTable;
+
/**
* A table containing customer data.
*/
+@IQTable
public class Customer {
+ @IQColumn(length = 25)
public String customerId;
+
+ @IQColumn(length = 2)
public String region;
public Customer() {
@@ -42,7 +49,8 @@ public class Customer { }
public static List<Customer> getList() {
- Customer[] list = { new Customer("ALFKI", "WA"), new Customer("ANATR", "WA"), new Customer("ANTON", "CA") };
+ Customer[] list = { new Customer("ALFKI", "WA"), new Customer("ANATR", "WA"),
+ new Customer("ANTON", "CA") };
return Arrays.asList(list);
}
diff --git a/tests/com/iciql/test/models/Order.java b/tests/com/iciql/test/models/Order.java index f0cf7f3..e123d09 100644 --- a/tests/com/iciql/test/models/Order.java +++ b/tests/com/iciql/test/models/Order.java @@ -17,7 +17,9 @@ package com.iciql.test.models;
+import static com.iciql.Define.length;
import static com.iciql.Define.primaryKey;
+import static com.iciql.Define.scale;
import static com.iciql.Define.tableName;
import java.math.BigDecimal;
@@ -25,6 +27,7 @@ import java.util.Arrays; import java.util.Date;
import java.util.List;
+
import com.iciql.Iciql;
/**
@@ -34,7 +37,7 @@ import com.iciql.Iciql; public class Order implements Iciql {
public String customerId;
public Integer orderId;
- public Date orderDate;
+ public Date orderDate;
public BigDecimal total;
public Order(String customerId, Integer orderId, String total, String orderDate) {
@@ -50,6 +53,9 @@ public class Order implements Iciql { public void defineIQ() {
tableName("Orders");
+ length(customerId, 25);
+ length(total, 10);
+ scale(total, 2);
primaryKey(customerId, orderId);
}
diff --git a/tests/com/iciql/test/models/Product.java b/tests/com/iciql/test/models/Product.java index 065e624..35eaa67 100644 --- a/tests/com/iciql/test/models/Product.java +++ b/tests/com/iciql/test/models/Product.java @@ -54,6 +54,7 @@ public class Product implements Iciql { public void defineIQ() {
tableName("Product");
primaryKey(productId);
+ length(productName, 255);
length(category, 255);
index(productName, category);
}
diff --git a/tests/com/iciql/test/models/ProductAnnotationOnly.java b/tests/com/iciql/test/models/ProductAnnotationOnly.java index 673ca14..80bf0a1 100644 --- a/tests/com/iciql/test/models/ProductAnnotationOnly.java +++ b/tests/com/iciql/test/models/ProductAnnotationOnly.java @@ -45,8 +45,8 @@ public class ProductAnnotationOnly { @IQColumn(name = "cat", length = 15, trim = true) public String category; - @IQColumn(name = "name") - private String productName; + @IQColumn(name = "name", length = 50) + public String productName; @SuppressWarnings("unused") @IQColumn diff --git a/tests/com/iciql/test/models/ProductMixedAnnotation.java b/tests/com/iciql/test/models/ProductMixedAnnotation.java index ff7d46f..646f75c 100644 --- a/tests/com/iciql/test/models/ProductMixedAnnotation.java +++ b/tests/com/iciql/test/models/ProductMixedAnnotation.java @@ -42,7 +42,7 @@ public class ProductMixedAnnotation { @IQColumn(name = "id", primaryKey = true) private Integer productId; - @IQColumn(name = "name") + @IQColumn(name = "name", length = 255) private String productName; public ProductMixedAnnotation() { diff --git a/tests/com/iciql/test/models/SupportedTypes.java b/tests/com/iciql/test/models/SupportedTypes.java index 49dd951..77fdbff 100644 --- a/tests/com/iciql/test/models/SupportedTypes.java +++ b/tests/com/iciql/test/models/SupportedTypes.java @@ -18,6 +18,9 @@ package com.iciql.test.models; import java.math.BigDecimal; +import java.math.RoundingMode; +import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.List; import java.util.Random; @@ -75,10 +78,11 @@ public class SupportedTypes { @IQColumn private Double myDouble; - @IQColumn + // scale change must match the test value scale + @IQColumn(length = 10, scale = 5) private BigDecimal myBigDecimal; - @IQColumn + @IQColumn(length = 40) private String myString; @IQColumn @@ -137,6 +141,8 @@ public class SupportedTypes { s.myFloat = new Float(rand.nextFloat()); s.myDouble = new Double(rand.nextDouble()); s.myBigDecimal = new BigDecimal(rand.nextDouble()); + // scale must match annotation + s.myBigDecimal = s.myBigDecimal.setScale(5, RoundingMode.UP); s.myString = Long.toHexString(rand.nextLong()); s.myUtilDate = new java.util.Date(rand.nextLong()); s.mySqlDate = new java.sql.Date(rand.nextLong()); @@ -160,13 +166,14 @@ public class SupportedTypes { same &= myLong.equals(s.myLong); same &= myFloat.equals(s.myFloat); same &= myDouble.equals(s.myDouble); - same &= myBigDecimal.equals(s.myBigDecimal); - same &= myUtilDate.getTime() == s.myUtilDate.getTime(); - same &= mySqlTimestamp.getTime() == s.mySqlTimestamp.getTime(); + 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)); + same &= df.format(mySqlTimestamp).equals(df.format(s.mySqlTimestamp)); same &= mySqlDate.toString().equals(s.mySqlDate.toString()); same &= mySqlTime.toString().equals(s.mySqlTime.toString()); same &= myString.equals(s.myString); - same &= compare(myBlob, s.myBlob); + same &= Arrays.equals(myBlob, s.myBlob); same &= myDefaultFlower.equals(s.myDefaultFlower); same &= myFavoriteFlower.equals(s.myFavoriteFlower); same &= myOtherFavoriteFlower.equals(s.myOtherFavoriteFlower); @@ -174,22 +181,7 @@ public class SupportedTypes { same &= myOtherFavoriteTree.equals(s.myOtherFavoriteTree); return same; } - - private boolean compare(byte[] a, byte[] b) { - if (b == null) { - return false; - } - if (a.length != b.length) { - return false; - } - for (int i = 0; i < a.length; i++) { - if (a[i] != b[i]) { - return false; - } - } - return true; - } - + /** * This class demonstrates the table upgrade. */ |