1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
/*
* 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.After;
import org.junit.Before;
import org.junit.Test;
import com.iciql.Db;
import com.iciql.IciqlException;
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;
/**
* Tests enum support.
*/
public class EnumsTest {
private Db db;
@Before
public void setUp() {
db = IciqlSuite.openNewDb();
db.insertAll(EnumIdModel.createList());
db.insertAll(EnumOrdinalModel.createList());
db.insertAll(EnumStringModel.createList());
}
@After
public void tearDown() {
db.close();
}
@Test
public void testEnumQueries() {
testIntEnums(new EnumIdModel());
testIntEnums(new EnumOrdinalModel());
testStringEnums(new EnumStringModel());
}
private void testIntEnums(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.tree()).is(Tree.PINE).selectFirst();
assertEquals(Tree.PINE, firstEnumValue.tree());
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) {
// 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.tree()).is(Tree.PINE).selectFirst();
assertEquals(Tree.PINE, firstEnumValue.tree());
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(count - 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 testMultipleEnumInstances() {
BadEnums b = new BadEnums();
try {
db.from(b).where(b.tree1).is(Tree.BIRCH).and (b.tree2).is(Tree.MAPLE).getSQL();
assertTrue("Failed to detect multiple Tree fields?!", false);
} catch (IciqlException e) {
assertTrue(e.getMessage(), e.getMessage().startsWith("Can not explicitly reference Tree"));
}
}
public static class BadEnums {
Tree tree1 = Tree.BIRCH;
Tree tree2 = Tree.MAPLE;
}
}
|