diff options
author | James Moger <james.moger@gitblit.com> | 2014-10-06 12:46:50 -0400 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2014-10-22 22:29:05 -0400 |
commit | 6889aa2e367d4658105c1bc2514afccb425770f3 (patch) | |
tree | 987102f49e6c6e41facc4dcb7d15db884d49c75b /src/test | |
parent | ecb0c4d4a0f118792c73b1244b389e6a02d00d31 (diff) | |
download | iciql-6889aa2e367d4658105c1bc2514afccb425770f3.tar.gz iciql-6889aa2e367d4658105c1bc2514afccb425770f3.zip |
Support generic types for EnumId mapping
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/java/com/iciql/test/EnumsTest.java | 30 | ||||
-rw-r--r-- | src/test/java/com/iciql/test/models/EnumModels.java | 98 |
2 files changed, 110 insertions, 18 deletions
diff --git a/src/test/java/com/iciql/test/EnumsTest.java b/src/test/java/com/iciql/test/EnumsTest.java index d8a0589..8e1f90e 100644 --- a/src/test/java/com/iciql/test/EnumsTest.java +++ b/src/test/java/com/iciql/test/EnumsTest.java @@ -31,6 +31,7 @@ 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.Genus;
import com.iciql.test.models.EnumModels.Tree;
/**
@@ -58,6 +59,7 @@ public class EnumsTest { testIntEnums(new EnumIdModel());
testIntEnums(new EnumOrdinalModel());
testStringEnums(new EnumStringModel());
+ testStringEnumIds(new EnumStringModel());
}
private void testIntEnums(EnumModels e) {
@@ -110,7 +112,31 @@ public class EnumsTest { list = db.from(e).where(e.tree()).between(Tree.MAPLE).and(Tree.PINE).select();
assertEquals(3, list.size());
}
-
+
+ private void testStringEnumIds(EnumModels e) {
+ // ensure all records inserted
+ long count = db.from(e).selectCount();
+ assertEquals(5, count);
+
+ // special case:
+ // value is first enum constant which is also the alias object.
+ // the first enum constant is used as the alias because we can not
+ // instantiate an enum reflectively.
+ EnumModels firstEnumValue = db.from(e).where(e.genus()).is(Genus.PINUS).selectFirst();
+ assertEquals(Tree.PINE, firstEnumValue.tree());
+ assertEquals(Genus.PINUS, firstEnumValue.genus());
+
+ EnumModels model = db.from(e).where(e.genus()).is(Genus.JUGLANS).selectFirst();
+
+ assertEquals(400, model.id.intValue());
+ assertEquals(Tree.WALNUT, model.tree());
+ assertEquals(Genus.JUGLANS, model.genus());
+
+ List<EnumModels> list = db.from(e).where(e.genus()).isNot(Genus.BETULA).select();
+ assertEquals(count - 1, list.size());
+
+ }
+
@Test
public void testMultipleEnumInstances() {
BadEnums b = new BadEnums();
@@ -121,7 +147,7 @@ public class EnumsTest { assertTrue(e.getMessage(), e.getMessage().startsWith("Can not explicitly reference Tree"));
}
}
-
+
public static class BadEnums {
Tree tree1 = Tree.BIRCH;
Tree tree2 = Tree.MAPLE;
diff --git a/src/test/java/com/iciql/test/models/EnumModels.java b/src/test/java/com/iciql/test/models/EnumModels.java index a865f6c..2320734 100644 --- a/src/test/java/com/iciql/test/models/EnumModels.java +++ b/src/test/java/com/iciql/test/models/EnumModels.java @@ -34,14 +34,14 @@ 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 {
+ public enum Tree implements EnumId<Integer> {
PINE(10), OAK(20), BIRCH(30), WALNUT(40), MAPLE(50);
private int enumid;
@@ -51,9 +51,39 @@ public abstract class EnumModels { }
@Override
- public int enumId() {
+ public Integer enumId() {
return enumid;
}
+
+ @Override
+ public Class<Integer> enumIdClass() {
+ return Integer.class;
+ }
+
+ }
+
+ /**
+ * Enum for testing custom ENUMID mapping.
+ */
+ @IQEnum(EnumType.ENUMID)
+ public enum Genus implements EnumId<String> {
+ PINUS("pinaceae"), QUERCUS("fagaceae"), BETULA("betulaceae"), JUGLANS("juglandaceae"), ACER("aceraceae");
+
+ private String family;
+
+ Genus(String id) {
+ this.family = id;
+ }
+
+ @Override
+ public String enumId() {
+ return family;
+ }
+
+ @Override
+ public Class<String> enumIdClass() {
+ return String.class;
+ }
}
@IQColumn(primaryKey = true)
@@ -61,6 +91,8 @@ public abstract class EnumModels { public abstract Tree tree();
+ public abstract Genus genus();
+
/**
* Test model for enum-as-enumid.
*/
@@ -72,12 +104,18 @@ public abstract class EnumModels { @IQColumn
private Tree tree;
+ // no need to specify ENUMID type as the enumeration definition
+ // specifies it.
+ @IQColumn
+ private Genus genus;
+
public EnumIdModel() {
}
- public EnumIdModel(int id, Tree tree) {
+ public EnumIdModel(int id, Tree tree, Genus genus) {
this.id = id;
this.tree = tree;
+ this.genus = genus;
}
@Override
@@ -85,10 +123,17 @@ public abstract class EnumModels { return tree;
}
+ @Override
+ public Genus genus() {
+ return genus;
+ }
+
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));
+ return Arrays.asList(new EnumIdModel(400, Tree.WALNUT, Genus.JUGLANS),
+ new EnumIdModel(200, Tree.OAK, Genus.QUERCUS),
+ new EnumIdModel(500, Tree.MAPLE, Genus.ACER),
+ new EnumIdModel(300, Tree.BIRCH, Genus.BETULA),
+ new EnumIdModel(100, Tree.PINE, Genus.PINUS));
}
}
@@ -103,10 +148,13 @@ public abstract class EnumModels { @IQColumn
private Tree tree;
+ @IQColumn
+ private Genus genus;
+
public EnumOrdinalModel() {
}
- public EnumOrdinalModel(int id, Tree tree) {
+ public EnumOrdinalModel(int id, Tree tree, Genus genus) {
this.id = id;
this.tree = tree;
}
@@ -116,10 +164,17 @@ public abstract class EnumModels { return tree;
}
+ @Override
+ public Genus genus() {
+ return genus;
+ }
+
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));
+ return Arrays.asList(new EnumOrdinalModel(400, Tree.WALNUT, Genus.JUGLANS),
+ new EnumOrdinalModel(200, Tree.OAK, Genus.QUERCUS),
+ new EnumOrdinalModel(500, Tree.MAPLE, Genus.ACER),
+ new EnumOrdinalModel(300, Tree.BIRCH, Genus.BETULA),
+ new EnumOrdinalModel(100, Tree.PINE, Genus.PINUS));
}
}
@@ -135,12 +190,16 @@ public abstract class EnumModels { @IQColumn(length = 25)
private Tree tree;
+ @IQColumn(trim = true, length = 25)
+ private Genus genus;
+
public EnumStringModel() {
}
- public EnumStringModel(int id, Tree tree) {
+ public EnumStringModel(int id, Tree tree, Genus genus) {
this.id = id;
this.tree = tree;
+ this.genus = genus;
}
@Override
@@ -148,10 +207,17 @@ public abstract class EnumModels { return tree;
}
+ @Override
+ public Genus genus() {
+ return genus;
+ }
+
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));
+ return Arrays.asList(new EnumStringModel(400, Tree.WALNUT, Genus.JUGLANS),
+ new EnumStringModel(200, Tree.OAK, Genus.QUERCUS),
+ new EnumStringModel(500, Tree.MAPLE, Genus.ACER),
+ new EnumStringModel(300, Tree.BIRCH, Genus.BETULA),
+ new EnumStringModel(100, Tree.PINE, Genus.PINUS));
}
}
}
|