aboutsummaryrefslogtreecommitdiffstats
path: root/tests/com/iciql/test/AliasMapTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'tests/com/iciql/test/AliasMapTest.java')
-rw-r--r--tests/com/iciql/test/AliasMapTest.java109
1 files changed, 99 insertions, 10 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