diff options
Diffstat (limited to 'tests/com/iciql/test')
-rw-r--r-- | tests/com/iciql/test/AliasMapTest.java | 109 | ||||
-rw-r--r-- | tests/com/iciql/test/PrimitivesTest.java | 46 | ||||
-rw-r--r-- | tests/com/iciql/test/models/PrimitivesModel.java | 17 |
3 files changed, 147 insertions, 25 deletions
diff --git a/tests/com/iciql/test/AliasMapTest.java b/tests/com/iciql/test/AliasMapTest.java index f0d5c15..0f91365 100644 --- a/tests/com/iciql/test/AliasMapTest.java +++ b/tests/com/iciql/test/AliasMapTest.java @@ -18,33 +18,122 @@ package com.iciql.test; import static org.junit.Assert.assertEquals; - -import java.util.List; +import static org.junit.Assert.assertTrue; import org.junit.Test; import com.iciql.Db; +import com.iciql.IciqlException; +import com.iciql.test.models.PrimitivesModel; import com.iciql.test.models.Product; +import com.iciql.util.Utils; /** - * Tests that columns (p.unitsInStock) are not compared by value with the value - * (9), but by reference (using an identity hash map). See - * http://code.google.com/p/h2database/issues/detail?id=119 - * - * @author d moebius at scoop dash gmbh dot de + * Tests object and primitive alias referencing. */ public class AliasMapTest { + /** + * Tests that columns (p.unitsInStock) are not compared by value with the + * value (9), but by reference (using an identity hash map). See + * http://code.google.com/p/h2database/issues/detail?id=119 + * + * @author d moebius at scoop dash gmbh dot de + */ @Test - public void testAliasMapping() throws Exception { + public void testObjectAliasMapping() throws Exception { Db db = IciqlSuite.openDb(); db.insertAll(Product.getList()); + // baseline count is the next id value + long bc = Utils.COUNTER.get(); + // number of fields in primitives model class + // each from() call will increment Utils.COUNTER by this amount + int fc = Product.class.getFields().length; + Product p = new Product(); - List<Product> products = db.from(p).where(p.unitsInStock).is(9).orderBy(p.productId).select(); + // This test confirms standard object referencing querying. + long count = db.from(p).where(p.productId).is(9).selectCount(); + assertEquals(1, count); + // Confirms that productId counter value is baseline counter value + assertEquals(bc, p.productId.intValue()); + try { + // This test compares "bc + fc" which is the counter value of + // unitsInStock assigned by Utils.newObject() after the 2nd pass + // through from(). + // + // Object fields map by REFERENCE, not value. + db.from(p).where(Long.valueOf(bc + fc).intValue()).is(9).orderBy(p.productId).select(); + assertTrue("Fail: object field is mapping by value.", false); + } catch (IciqlException e) { + assertEquals(IciqlException.CODE_UNMAPPED_FIELD, e.getIciqlCode()); + assertEquals(bc + 5, p.productId.intValue()); + } + + try { + // This test compares Integer(bc) which is the counter value of + // unitsInStock assigned by Utils.newObject() after the 3rd pass + // through from(). + // + // Object fields map by REFERENCE, not value. + db.from(p).where(Long.valueOf(bc).intValue()).is(9).orderBy(p.productId).select(); + assertTrue("Fail: object field is mapping by value.", false); + } catch (IciqlException e) { + assertEquals(IciqlException.CODE_UNMAPPED_FIELD, e.getIciqlCode()); + assertEquals(bc + (2 * fc), p.productId.intValue()); + } + + db.close(); + } + + /** + * Confirms that primitive aliases ARE mapped by value. + */ + @Test + public void testPrimitiveAliasMapping() throws Exception { + Db db = IciqlSuite.openDb(); + PrimitivesModel model = new PrimitivesModel(); + model.myLong = 100L; + db.insert(model); + model.myLong = 200L; + db.insert(model); - assertEquals("[]", products.toString()); + // baseline count is the next id value + long bc = Utils.COUNTER.get(); + // number of fields in primitives model class + // each from() call will increment Utils.COUNTER by this amount + int fc = PrimitivesModel.class.getFields().length; + PrimitivesModel p = new PrimitivesModel(); + // This test confirms standard primitive referencing querying. + long count = db.from(p).where(p.myLong).is(100L).selectCount(); + assertEquals(1, count); + // Confirms that myLong counter value is bc + assertEquals(bc, p.myLong); + try { + // This test compares "bc + fc" which is the counter value + // of myLong assigned by Utils.newObject() after the 2nd pass + // through from(). + // + // Primitive fields map by VALUE. + count = db.from(p).where(bc + fc).is(100L).selectCount(); + assertEquals(1, count); + assertEquals(bc + fc, p.myLong); + } catch (IciqlException e) { + assertTrue(e.getMessage(), false); + } + try { + // This test compares "bc" which was the counter value of + // myLong assigned by Utils.newObject() after the 1st pass + // through from(). "bc" is unmapped now and will throw an + // exception. + // + // Primitive fields map by VALUE. + db.from(p).where(bc).is(100L).select(); + } catch (IciqlException e) { + assertEquals(IciqlException.CODE_UNMAPPED_FIELD, e.getIciqlCode()); + assertEquals(bc + (2 * fc), p.myLong); + } db.close(); } }
\ No newline at end of file diff --git a/tests/com/iciql/test/PrimitivesTest.java b/tests/com/iciql/test/PrimitivesTest.java index 1cdeb1c..a200b57 100644 --- a/tests/com/iciql/test/PrimitivesTest.java +++ b/tests/com/iciql/test/PrimitivesTest.java @@ -16,8 +16,12 @@ package com.iciql.test;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
+import java.util.Collections;
+import java.util.List;
+
import org.junit.Test;
import com.iciql.Db;
@@ -32,14 +36,17 @@ public class PrimitivesTest { public void testPrimitives() {
Db db = IciqlSuite.openDb();
- // insert random model
- PrimitivesModel model = new PrimitivesModel();
- db.insert(model);
+ // insert random models in reverse order
+ List<PrimitivesModel> models = PrimitivesModel.getList();
+ PrimitivesModel model = models.get(0);
+ Collections.reverse(models);
+ // insert them in reverse order
+ db.insertAll(models);
PrimitivesModel p = new PrimitivesModel();
// retrieve model and compare
- PrimitivesModel retrievedModel = db.from(p).selectFirst();
+ PrimitivesModel retrievedModel = db.from(p).orderBy(p.myLong).selectFirst();
assertTrue(model.equivalentTo(retrievedModel));
retrievedModel = db.from(p).where("mylong = ? and myinteger = ?", model.myLong, model.myInteger)
@@ -47,17 +54,26 @@ public class PrimitivesTest { assertTrue(model.equivalentTo(retrievedModel));
// retrieve with conditions and compare
- // StatementLogger.activateConsoleLogger();
- // retrievedModel =
- // db.from(p).where(p.myLong).is(model.myLong).and(p.myInteger).is(model.myInteger)
- // .selectFirst();
- // assertTrue(model.equivalentTo(retrievedModel));
- //
- // // update myInteger and compare
- // db.from(p).set(p.myInteger).to(10).where(p.myLong).is(model.myLong).update();
- // retrievedModel = db.from(p).selectFirst();
-
- // assertEquals(10, retrievedModel.myInteger);
+ retrievedModel = db.from(p).where(p.myLong).is(model.myLong).and(p.myInteger).is(model.myInteger)
+ .selectFirst();
+ assertTrue(model.equivalentTo(retrievedModel));
+
+ // set myInteger & myDouble
+ db.from(p).set(p.myInteger).to(10).set(p.myDouble).to(3.0d).where(p.myLong).is(model.myLong).update();
+ retrievedModel = db.from(p).orderBy(p.myLong).selectFirst();
+
+ assertEquals(10, retrievedModel.myInteger);
+ assertEquals(3d, retrievedModel.myDouble, 0.001d);
+
+ // increment my double by pi
+ db.from(p).increment(p.myDouble).by(3.14d).update();
+ retrievedModel = db.from(p).orderBy(p.myLong).selectFirst();
+ assertEquals(6.14d, retrievedModel.myDouble, 0.001d);
+
+ // test order by
+ List<PrimitivesModel> list = db.from(p).orderBy(p.myLong).select();
+ assertEquals(models.size(), list.size());
+ assertEquals("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]", list.toString());
db.close();
}
diff --git a/tests/com/iciql/test/models/PrimitivesModel.java b/tests/com/iciql/test/models/PrimitivesModel.java index 2d1a7da..07c1611 100644 --- a/tests/com/iciql/test/models/PrimitivesModel.java +++ b/tests/com/iciql/test/models/PrimitivesModel.java @@ -15,6 +15,8 @@ */
package com.iciql.test.models;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Random;
import com.iciql.Iciql.IQColumn;
@@ -69,4 +71,19 @@ public class PrimitivesModel { same &= myFloat == p.myFloat;
return same;
}
+
+ public static List<PrimitivesModel> getList() {
+ List<PrimitivesModel> list = new ArrayList<PrimitivesModel>();
+ for (int i = 1; i <= 10; i++) {
+ PrimitivesModel p = new PrimitivesModel();
+ p.myLong = i;
+ list.add(p);
+ }
+ return list;
+ }
+
+ @Override
+ public String toString() {
+ return String.valueOf(myLong);
+ }
}
|