From b865898879f0d0eb1e752b41562b463b0c31c517 Mon Sep 17 00:00:00 2001 From: James Moger Date: Fri, 5 Aug 2011 11:04:29 -0400 Subject: Simplified annotations. Interchangeable int-boolean runtime mapping. --- tests/com/iciql/test/BooleanModelTest.java | 128 +++++++++++++++++++++ tests/com/iciql/test/IciqlSuite.java | 4 +- tests/com/iciql/test/ModelsTest.java | 2 +- tests/com/iciql/test/models/BooleanModel.java | 73 ++++++++++++ tests/com/iciql/test/models/Product.java | 4 +- .../iciql/test/models/ProductAnnotationOnly.java | 2 +- .../iciql/test/models/ProductMixedAnnotation.java | 2 +- tests/com/iciql/test/models/SupportedTypes.java | 10 +- 8 files changed, 213 insertions(+), 12 deletions(-) create mode 100644 tests/com/iciql/test/BooleanModelTest.java create mode 100644 tests/com/iciql/test/models/BooleanModel.java (limited to 'tests/com/iciql') diff --git a/tests/com/iciql/test/BooleanModelTest.java b/tests/com/iciql/test/BooleanModelTest.java new file mode 100644 index 0000000..0287e06 --- /dev/null +++ b/tests/com/iciql/test/BooleanModelTest.java @@ -0,0 +1,128 @@ +/* + * 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 static org.junit.Assert.assertTrue; + +import java.util.List; + +import org.junit.Test; + +import com.iciql.Db; +import com.iciql.test.models.BooleanModel; +import com.iciql.test.models.BooleanModel.BooleanAsIntModel; + +/** + * Tests interchangeable mapping of INT columns with Booleans and BOOL columns + * with Integers. + * + */ +public class BooleanModelTest { + + @Test + public void testBooleanColumn() { + Db db = Db.open("jdbc:h2:mem:", "sa", "sa"); + db.insertAll(BooleanModel.getList()); + BooleanAsIntModel b = new BooleanAsIntModel(); + List models = db.from(b).select(); + int count = 0; + for (BooleanAsIntModel model : models) { + if ((model.id % 2) == 1) { + // assert that odd ids are true + assertTrue(model.mybool > 0); + } else { + // assert that even ids are false + assertTrue(model.mybool == 0); + } + + // count true values + if (model.mybool > 0) { + count++; + } + } + assertEquals(2, count); + + // invert boolean values and update + for (BooleanAsIntModel model : models) { + model.mybool = model.mybool > 0 ? 0 : 1; + } + db.updateAll(models); + + // check even ids are true + models = db.from(b).select(); + for (BooleanAsIntModel model : models) { + if ((model.id % 2) == 1) { + // assert that odd ids are false + assertTrue(model.mybool == 0); + } else { + // assert that even ids are true + assertTrue(model.mybool > 0); + } + } + db.close(); + } + + @Test + public void testIntColumn() { + Db db = Db.open("jdbc:h2:mem:", "sa", "sa"); + // insert INT column + db.insertAll(BooleanAsIntModel.getList()); + + // select all rows with INT column and map to Boolean + BooleanModel b = new BooleanModel(); + List models = db.from(b).select(); + int count = 0; + for (BooleanModel model : models) { + if ((model.id % 2) == 1) { + // assert that odd ids are true + assertTrue(model.mybool); + } else { + // assert that even ids are false + assertTrue(!model.mybool); + } + + // count true values + if (model.mybool) { + count++; + } + } + assertEquals(2, count); + + // invert boolean values and update + for (BooleanModel model : models) { + model.mybool = !model.mybool; + } + db.updateAll(models); + + // check even ids are true + models = db.from(b).select(); + for (BooleanModel model : models) { + if ((model.id % 2) == 1) { + // assert that odd ids are false + assertTrue(!model.mybool); + } else { + // assert that even ids are true + assertTrue(model.mybool); + } + } + db.close(); + } +} diff --git a/tests/com/iciql/test/IciqlSuite.java b/tests/com/iciql/test/IciqlSuite.java index b235f6d..20dbea2 100644 --- a/tests/com/iciql/test/IciqlSuite.java +++ b/tests/com/iciql/test/IciqlSuite.java @@ -25,8 +25,8 @@ import org.junit.runners.Suite.SuiteClasses; * */ @RunWith(Suite.class) -@SuiteClasses({ AliasMapTest.class, AnnotationsTest.class, ClobTest.class, ConcurrencyTest.class, - ModelsTest.class, SamplesTest.class, UpdateTest.class, RuntimeQueryTest.class, +@SuiteClasses({ AliasMapTest.class, AnnotationsTest.class, BooleanModelTest.class, ClobTest.class, + ConcurrencyTest.class, ModelsTest.class, SamplesTest.class, UpdateTest.class, RuntimeQueryTest.class, StatementLoggerTest.class }) public class IciqlSuite { diff --git a/tests/com/iciql/test/ModelsTest.java b/tests/com/iciql/test/ModelsTest.java index 716b1bc..9012296 100644 --- a/tests/com/iciql/test/ModelsTest.java +++ b/tests/com/iciql/test/ModelsTest.java @@ -115,7 +115,7 @@ public class ModelsTest { true); assertEquals(1, models.size()); // a poor test, but a start - assertEquals(1904, models.get(0).length()); + assertEquals(1895, models.get(0).length()); } @Test diff --git a/tests/com/iciql/test/models/BooleanModel.java b/tests/com/iciql/test/models/BooleanModel.java new file mode 100644 index 0000000..d22e3c1 --- /dev/null +++ b/tests/com/iciql/test/models/BooleanModel.java @@ -0,0 +1,73 @@ +/* + * 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.models; + +import java.util.Arrays; +import java.util.List; + +import com.iciql.Iciql.IQColumn; +import com.iciql.Iciql.IQTable; + +/** + * Boolean types model. + */ +@IQTable(name = "BooleanTest") +public class BooleanModel { + + @IQColumn(primaryKey = true) + public Integer id; + + @IQColumn + public Boolean mybool; + + public BooleanModel() { + } + + BooleanModel(int id, boolean val) { + this.id = id; + this.mybool = val; + } + + public static List getList() { + return Arrays.asList(new BooleanModel(1, true), new BooleanModel(2, false), + new BooleanModel(3, true), new BooleanModel(4, false)); + } + + /** + * Test boolean as int + */ + @IQTable(name = "BooleanTest") + public static class BooleanAsIntModel { + @IQColumn(primaryKey = true) + public Integer id; + + @IQColumn + public Integer mybool; + + public BooleanAsIntModel() { + } + + BooleanAsIntModel(int id, boolean val) { + this.id = id; + this.mybool = val ? 1 : 0; + } + + public static List getList() { + return Arrays.asList(new BooleanAsIntModel(1, true), new BooleanAsIntModel(2, false), + new BooleanAsIntModel(3, true), new BooleanAsIntModel(4, false)); + } + } +} diff --git a/tests/com/iciql/test/models/Product.java b/tests/com/iciql/test/models/Product.java index 735ebab..065e624 100644 --- a/tests/com/iciql/test/models/Product.java +++ b/tests/com/iciql/test/models/Product.java @@ -18,7 +18,7 @@ package com.iciql.test.models; import static com.iciql.Define.index; -import static com.iciql.Define.maxLength; +import static com.iciql.Define.length; import static com.iciql.Define.primaryKey; import static com.iciql.Define.tableName; @@ -54,7 +54,7 @@ public class Product implements Iciql { public void defineIQ() { tableName("Product"); primaryKey(productId); - maxLength(category, 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 caf07c7..673ca14 100644 --- a/tests/com/iciql/test/models/ProductAnnotationOnly.java +++ b/tests/com/iciql/test/models/ProductAnnotationOnly.java @@ -42,7 +42,7 @@ public class ProductAnnotationOnly { @IQColumn(name = "id") public Integer productId; - @IQColumn(name = "cat", maxLength = 15, trimString = true) + @IQColumn(name = "cat", length = 15, trim = true) public String category; @IQColumn(name = "name") diff --git a/tests/com/iciql/test/models/ProductMixedAnnotation.java b/tests/com/iciql/test/models/ProductMixedAnnotation.java index 1d3cb8f..ff7d46f 100644 --- a/tests/com/iciql/test/models/ProductMixedAnnotation.java +++ b/tests/com/iciql/test/models/ProductMixedAnnotation.java @@ -36,7 +36,7 @@ public class ProductMixedAnnotation { public Integer unitsInStock; public String mappedField; - @IQColumn(name = "cat", maxLength = 255) + @IQColumn(name = "cat", length = 255) public String category; @IQColumn(name = "id", primaryKey = true) diff --git a/tests/com/iciql/test/models/SupportedTypes.java b/tests/com/iciql/test/models/SupportedTypes.java index 97a8675..c8aebcb 100644 --- a/tests/com/iciql/test/models/SupportedTypes.java +++ b/tests/com/iciql/test/models/SupportedTypes.java @@ -80,10 +80,10 @@ public class SupportedTypes { public Integer id; @IQColumn - private Boolean myBool = false; + private Boolean myBool; @IQColumn - private Byte myByte = 2; + private Byte myByte; @IQColumn private Short myShort; @@ -95,7 +95,7 @@ public class SupportedTypes { private Long myLong; @IQColumn - private Float myFloat = 1.0f; + private Float myFloat; @IQColumn private Double myDouble; @@ -122,14 +122,14 @@ public class SupportedTypes { private byte[] myBlob; @IQEnum(EnumType.STRING) - @IQColumn(trimString = true, maxLength = 25) + @IQColumn(trim = true, length = 25) private Flower myFavoriteFlower; @IQEnum(EnumType.ORDINAL) @IQColumn private Flower myOtherFavoriteFlower; - @IQColumn(maxLength = 25) + @IQColumn(length = 25) // @IQEnum is set on the enumeration definition and is shared // by all uses of Tree as an @IQColumn private Tree myFavoriteTree; -- cgit v1.2.3