]> source.dussan.org Git - iciql.git/commitdiff
Fixed first enum constant special case. (issue 4)
authorJames Moger <james.moger@gmail.com>
Mon, 8 Aug 2011 16:03:10 +0000 (12:03 -0400)
committerJames Moger <james.moger@gmail.com>
Mon, 8 Aug 2011 16:03:10 +0000 (12:03 -0400)
src/com/iciql/Query.java
tests/com/iciql/test/EnumsTest.java [new file with mode: 0644]
tests/com/iciql/test/IciqlSuite.java
tests/com/iciql/test/ModelsTest.java
tests/com/iciql/test/models/EnumModels.java

index 2d31e8df0751137825033c5852279f5557be04c8..2e58fe512cc847192caf38697e14d39cb0e65490 100644 (file)
@@ -359,6 +359,15 @@ public class Query<T> {
                        token.appendSQL(stat, this);\r
                        return;\r
                }\r
+               if (alias != null && value.getClass().isEnum()) {\r
+                       // special case:\r
+                       // value is first enum constant which is also the alias object.\r
+                       // the first enum constant is used as the alias because we can not\r
+                       // instantiate an enum reflectively.\r
+                       stat.appendSQL("?");\r
+                       addParameter(stat, alias, value);\r
+                       return;\r
+               }\r
                SelectColumn<T> col = aliasMap.get(value);\r
                if (col != null) {\r
                        col.appendSQL(stat);\r
diff --git a/tests/com/iciql/test/EnumsTest.java b/tests/com/iciql/test/EnumsTest.java
new file mode 100644 (file)
index 0000000..b66aa9e
--- /dev/null
@@ -0,0 +1,111 @@
+/*\r
+ * Copyright 2011 James Moger.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package com.iciql.test;\r
+\r
+import static org.junit.Assert.assertEquals;\r
+\r
+import java.util.List;\r
+\r
+import org.junit.After;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+\r
+import com.iciql.Db;\r
+import com.iciql.test.models.EnumModels;\r
+import com.iciql.test.models.EnumModels.EnumIdModel;\r
+import com.iciql.test.models.EnumModels.EnumOrdinalModel;\r
+import com.iciql.test.models.EnumModels.EnumStringModel;\r
+import com.iciql.test.models.EnumModels.Tree;\r
+\r
+/**\r
+ * Tests enum support.\r
+ */\r
+public class EnumsTest {\r
+\r
+       private Db db;\r
+\r
+       @Before\r
+       public void setUp() {\r
+               db = Db.open("jdbc:h2:mem:", "sa", "sa");\r
+               db.insertAll(EnumIdModel.createList());\r
+               db.insertAll(EnumOrdinalModel.createList());\r
+               db.insertAll(EnumStringModel.createList());\r
+       }\r
+\r
+       @After\r
+       public void tearDown() {\r
+               db.close();\r
+       }\r
+\r
+       @Test\r
+       public void testEnumQueries() {\r
+               testIntEnums(new EnumIdModel());\r
+               testIntEnums(new EnumOrdinalModel());\r
+               testStringEnums(new EnumStringModel());\r
+       }\r
+\r
+       private void testIntEnums(EnumModels e) {\r
+               // ensure all records inserted\r
+               long count = db.from(e).selectCount();\r
+               assertEquals(5, count);\r
+\r
+               // special case:\r
+               // value is first enum constant which is also the alias object.\r
+               // the first enum constant is used as the alias because we can not\r
+               // instantiate an enum reflectively.\r
+               EnumModels firstEnumValue = db.from(e).where(e.tree()).is(Tree.PINE).selectFirst();\r
+               assertEquals(Tree.PINE, firstEnumValue.tree());\r
+\r
+               EnumModels model = db.from(e).where(e.tree()).is(Tree.WALNUT).selectFirst();\r
+\r
+               assertEquals(400, model.id.intValue());\r
+               assertEquals(Tree.WALNUT, model.tree());\r
+\r
+               List<EnumModels> list = db.from(e).where(e.tree()).atLeast(Tree.BIRCH).select();\r
+               assertEquals(3, list.size());\r
+\r
+               // between is an int compare\r
+               list = db.from(e).where(e.tree()).between(Tree.BIRCH).and(Tree.WALNUT).select();\r
+               assertEquals(2, list.size());\r
+\r
+       }\r
+\r
+       private void testStringEnums(EnumModels e) {\r
+               // ensure all records inserted\r
+               long count = db.from(e).selectCount();\r
+               assertEquals(5, count);\r
+\r
+               // special case:\r
+               // value is first enum constant which is also the alias object.\r
+               // the first enum constant is used as the alias because we can not\r
+               // instantiate an enum reflectively.\r
+               EnumModels firstEnumValue = db.from(e).where(e.tree()).is(Tree.PINE).selectFirst();\r
+               assertEquals(Tree.PINE, firstEnumValue.tree());\r
+\r
+               EnumModels model = db.from(e).where(e.tree()).is(Tree.WALNUT).selectFirst();\r
+\r
+               assertEquals(400, model.id.intValue());\r
+               assertEquals(Tree.WALNUT, model.tree());\r
+\r
+               List<EnumModels> list = db.from(e).where(e.tree()).isNot(Tree.BIRCH).select();\r
+               assertEquals(count - 1, list.size());\r
+\r
+               // between is a string compare\r
+               list = db.from(e).where(e.tree()).between(Tree.MAPLE).and(Tree.PINE).select();\r
+               assertEquals(3, list.size());\r
+       }\r
+}\r
index 056c996885ab3e23730d056fc93df3f1d6bfd277..c9dab7793fbba81cdc0418aaadde86b135b04162 100644 (file)
@@ -26,8 +26,9 @@ import org.junit.runners.Suite.SuiteClasses;
  */\r
 @RunWith(Suite.class)\r
 @SuiteClasses({ AliasMapTest.class, AnnotationsTest.class, BooleanModelTest.class, ClobTest.class,\r
-               ConcurrencyTest.class, ModelsTest.class, PrimitivesTest.class, RuntimeQueryTest.class,\r
-               SamplesTest.class, StatementLoggerTest.class, UpdateTest.class, UUIDTest.class })\r
+               ConcurrencyTest.class, EnumsTest.class, ModelsTest.class, PrimitivesTest.class,\r
+               RuntimeQueryTest.class, SamplesTest.class, StatementLoggerTest.class, UpdateTest.class,\r
+               UUIDTest.class })\r
 public class IciqlSuite {\r
 \r
        public static void assertStartsWith(String a, String b) {\r
index 8a933aeef02279613b2f7e6c606a186b83996bec..d5edcfd8ad0c3d1c8ef651afefe941dc717479ff 100644 (file)
@@ -36,17 +36,11 @@ 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.
@@ -112,48 +106,6 @@ 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());
-
-               // between is an int compare
-               list = db.from(e).where(e.tree()).between(Tree.BIRCH).and(Tree.WALNUT).select();
-               assertEquals(2, 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());
-
-               // between is a string compare
-               list = db.from(e).where(e.tree()).between(Tree.MAPLE).and(Tree.PINE).select();
-               assertEquals(3, list.size());
-       }
-
        @Test
        public void testModelGeneration() {
                List<SupportedTypes> original = SupportedTypes.createList();
index e5f6f39ee5a9741ddf9dc4ef6030f5c5ac69a8c8..40b7806cef4aced56f07259fa68f04eaa46922cd 100644 (file)
@@ -54,7 +54,7 @@ public abstract class EnumModels {
 \r
        @IQColumn(primaryKey = true)\r
        public Integer id;\r
-       \r
+\r
        public abstract Tree tree();\r
 \r
        /**\r
@@ -106,7 +106,7 @@ public abstract class EnumModels {
                        this.id = id;\r
                        this.tree = tree;\r
                }\r
-               \r
+\r
                @Override\r
                public Tree tree() {\r
                        return tree;\r
@@ -143,7 +143,7 @@ public abstract class EnumModels {
                public Tree tree() {\r
                        return tree;\r
                }\r
-               \r
+\r
                public static List<EnumStringModel> createList() {\r
                        return Arrays.asList(new EnumStringModel(400, Tree.WALNUT), new EnumStringModel(200, Tree.OAK),\r
                                        new EnumStringModel(500, Tree.MAPLE), new EnumStringModel(300, Tree.BIRCH),\r