aboutsummaryrefslogtreecommitdiffstats
path: root/tests/com/iciql/test
diff options
context:
space:
mode:
authorJames Moger <james.moger@gmail.com>2011-08-07 15:53:18 -0400
committerJames Moger <james.moger@gmail.com>2011-08-07 15:53:18 -0400
commit684838def9e1646c266ffb46bbc65b5dfdc8f14d (patch)
treeab0625631b4c845ee4d9b2e48b12035df13a038b /tests/com/iciql/test
parent0c5463e7372fe4c47341dcf0c48bdd94682d0c7c (diff)
downloadiciql-684838def9e1646c266ffb46bbc65b5dfdc8f14d.tar.gz
iciql-684838def9e1646c266ffb46bbc65b5dfdc8f14d.zip
Finished enum query support (issue 4)
Diffstat (limited to 'tests/com/iciql/test')
-rw-r--r--tests/com/iciql/test/ModelsTest.java41
-rw-r--r--tests/com/iciql/test/models/EnumModels.java153
-rw-r--r--tests/com/iciql/test/models/SupportedTypes.java37
3 files changed, 199 insertions, 32 deletions
diff --git a/tests/com/iciql/test/ModelsTest.java b/tests/com/iciql/test/ModelsTest.java
index 8fbf219..acaf634 100644
--- a/tests/com/iciql/test/ModelsTest.java
+++ b/tests/com/iciql/test/ModelsTest.java
@@ -36,11 +36,17 @@ import com.iciql.DbUpgrader;
import com.iciql.DbVersion;
import com.iciql.Iciql.IQVersion;
import com.iciql.ValidationRemark;
+import com.iciql.test.models.EnumModels;
+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.EnumModels.Tree;
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.StatementLogger;
/**
* Test that the mapping between classes and tables is done correctly.
@@ -59,7 +65,7 @@ public class ModelsTest {
private void log(String text) {
System.out.println(text);
}
-
+
@Before
public void setUp() {
db = Db.open("jdbc:h2:mem:", "sa", "sa");
@@ -107,6 +113,39 @@ public class ModelsTest {
}
@Test
+ public void testEnumQueries() {
+ StatementLogger.activateConsoleLogger();
+ testIntEnums(new EnumIdModel(), EnumIdModel.createList());
+ testIntEnums(new EnumOrdinalModel(), EnumOrdinalModel.createList());
+ testStringEnums(new EnumStringModel(), EnumStringModel.createList());
+ StatementLogger.deactivateConsoleLogger();
+ }
+
+ private void testIntEnums(EnumModels e, List<?> models) {
+ db.insertAll(models);
+
+ EnumModels model = db.from(e).where(e.tree()).is(Tree.WALNUT).selectFirst();
+
+ assertEquals(400, model.id.intValue());
+ assertEquals(Tree.WALNUT, model.tree());
+
+ List<EnumModels> list = db.from(e).where(e.tree()).atLeast(Tree.BIRCH).select();
+ assertEquals(3, list.size());
+ }
+
+ private void testStringEnums(EnumModels e, List<?> models) {
+ db.insertAll(models);
+
+ EnumModels model = db.from(e).where(e.tree()).is(Tree.WALNUT).selectFirst();
+
+ assertEquals(400, model.id.intValue());
+ assertEquals(Tree.WALNUT, model.tree());
+
+ List<EnumModels> list = db.from(e).where(e.tree()).isNot(Tree.BIRCH).select();
+ assertEquals(models.size() - 1, list.size());
+ }
+
+ @Test
public void testModelGeneration() {
List<SupportedTypes> original = SupportedTypes.createList();
db.insertAll(original);
diff --git a/tests/com/iciql/test/models/EnumModels.java b/tests/com/iciql/test/models/EnumModels.java
new file mode 100644
index 0000000..bd9d88a
--- /dev/null
+++ b/tests/com/iciql/test/models/EnumModels.java
@@ -0,0 +1,153 @@
+/*
+ * 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.EnumId;
+import com.iciql.Iciql.EnumType;
+import com.iciql.Iciql.IQColumn;
+import com.iciql.Iciql.IQEnum;
+import com.iciql.Iciql.IQTable;
+
+public abstract class EnumModels {
+
+ /**
+ * Test of @IQEnum annotated enumeration. This strategy is the default
+ * strategy for all fields of the Tree enum.
+ *
+ * Individual Tree field declarations can override this strategy by
+ * specifying a different @IQEnum annotation.
+ *
+ * Here ORDINAL specifies that this enum will be mapped to an INT column.
+ */
+ @IQEnum(EnumType.ENUMID)
+ public enum Tree implements EnumId {
+ PINE(10), OAK(20), BIRCH(30), WALNUT(40), MAPLE(50);
+
+ private int enumid;
+
+ Tree(int id) {
+ this.enumid = id;
+ }
+
+ @Override
+ public int enumId() {
+ return enumid;
+ }
+ }
+
+ @IQColumn(primaryKey = true)
+ public Integer id;
+
+ public abstract Tree tree();
+
+ /**
+ * Test model for enum-as-enumid.
+ */
+ @IQTable(inheritColumns = true)
+ public static class EnumIdModel extends EnumModels {
+
+ // no need to specify ENUMID type as the enumeration definition
+ // specifies it.
+ @IQColumn
+ private Tree tree;
+
+ public EnumIdModel() {
+ }
+
+ public EnumIdModel(int id, Tree tree) {
+ this.id = id;
+ this.tree = tree;
+ }
+
+ @Override
+ public Tree tree() {
+ return tree;
+ }
+
+ public static List<EnumIdModel> createList() {
+ return Arrays.asList(new EnumIdModel(400, Tree.WALNUT), new EnumIdModel(200, Tree.OAK),
+ new EnumIdModel(500, Tree.MAPLE), new EnumIdModel(300, Tree.BIRCH), new EnumIdModel(100,
+ Tree.PINE));
+ }
+ }
+
+ /**
+ * Test model for enum-as-ordinal.
+ */
+ @IQTable(inheritColumns = true)
+ public static class EnumOrdinalModel extends EnumModels {
+
+ // override the enumtype to ordinal
+ @IQEnum(EnumType.ORDINAL)
+ @IQColumn
+ private Tree tree;
+
+ public EnumOrdinalModel() {
+ }
+
+ public EnumOrdinalModel(int id, Tree tree) {
+ this.id = id;
+ this.tree = tree;
+ }
+
+ @Override
+ public Tree tree() {
+ return tree;
+ }
+
+ public static List<EnumOrdinalModel> createList() {
+ return Arrays.asList(new EnumOrdinalModel(400, Tree.WALNUT), new EnumOrdinalModel(200, Tree.OAK),
+ new EnumOrdinalModel(500, Tree.MAPLE), new EnumOrdinalModel(300, Tree.BIRCH),
+ new EnumOrdinalModel(100, Tree.PINE));
+ }
+ }
+
+ /**
+ * Test model for enum-as-string.
+ */
+ @IQTable(inheritColumns = true)
+ public static class EnumStringModel extends EnumModels {
+
+ // override the enumtype to string
+ // ensure that we specify a length so that the column is VARCHAR
+ @IQEnum(EnumType.STRING)
+ @IQColumn(length = 25)
+ private Tree tree;
+
+ public EnumStringModel() {
+ }
+
+ public EnumStringModel(int id, Tree tree) {
+ this.id = id;
+ this.tree = tree;
+ }
+
+ @Override
+ public Tree tree() {
+ return tree;
+ }
+
+ public static List<EnumStringModel> createList() {
+ return Arrays.asList(new EnumStringModel(400, Tree.WALNUT), new EnumStringModel(200, Tree.OAK),
+ new EnumStringModel(500, Tree.MAPLE), new EnumStringModel(300, Tree.BIRCH),
+ new EnumStringModel(100, Tree.PINE));
+ }
+ }
+}
diff --git a/tests/com/iciql/test/models/SupportedTypes.java b/tests/com/iciql/test/models/SupportedTypes.java
index c8aebcb..b8abf2d 100644
--- a/tests/com/iciql/test/models/SupportedTypes.java
+++ b/tests/com/iciql/test/models/SupportedTypes.java
@@ -21,7 +21,6 @@ import java.math.BigDecimal;
import java.util.List;
import java.util.Random;
-import com.iciql.Iciql.EnumId;
import com.iciql.Iciql.EnumType;
import com.iciql.Iciql.IQColumn;
import com.iciql.Iciql.IQEnum;
@@ -30,6 +29,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.models.EnumModels.Tree;
import com.iciql.util.Utils;
/**
@@ -51,31 +51,6 @@ public class SupportedTypes {
ROSE, TULIP, MUM, PETUNIA, MARIGOLD, DAFFODIL;
}
- /**
- * Test of @IQEnum annotated enumeration. This strategy is the default
- * strategy for all fields of the Tree enum.
- *
- * Individual Tree field declarations can override this strategy by
- * specifying a different @IQEnum annotation.
- *
- * Here ORDINAL specifies that this enum will be mapped to an INT column.
- */
- @IQEnum(EnumType.ORDINAL)
- public enum Tree implements EnumId {
- PINE(10), OAK(20), BIRCH(30), WALNUT(40), MAPLE(50);
-
- private int enumid;
-
- Tree(int id) {
- this.enumid = id;
- }
-
- @Override
- public int enumId() {
- return enumid;
- }
- }
-
@IQColumn(primaryKey = true, autoIncrement = true)
public Integer id;
@@ -129,14 +104,14 @@ public class SupportedTypes {
@IQColumn
private Flower myOtherFavoriteFlower;
- @IQColumn(length = 25)
+ @IQEnum(EnumType.ORDINAL)
+ @IQColumn
+ // override the default enum strategy and use the ordinal value
+ private Tree myFavoriteTree;
+
// @IQEnum is set on the enumeration definition and is shared
// by all uses of Tree as an @IQColumn
- private Tree myFavoriteTree;
-
- @IQEnum(EnumType.ENUMID)
@IQColumn
- // override the default enum strategy and use the custom enumid
private Tree myOtherFavoriteTree;
public static List<SupportedTypes> createList() {