--- /dev/null
+/*\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
*/\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
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.
}
}
- @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();