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
130
131
132
133
134
135
136
137
138
139
|
/*
* Copyright 2004-2011 H2 Group.
* 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 org.junit.Test;
import com.iciql.Db;
import com.iciql.IciqlException;
import com.iciql.test.models.PrimitivesModel;
import com.iciql.test.models.Product;
import com.iciql.util.Utils;
/**
* Tests object and primitive alias referencing.
*/
public class AliasMapTest {
/**
* Tests that columns (p.unitsInStock) are not compared by value with the
* value (9), but by reference (using an identity hash map). See
* http://code.google.com/p/h2database/issues/detail?id=119
*
* @author d moebius at scoop dash gmbh dot de
*/
@Test
public void testObjectAliasMapping() throws Exception {
Db db = IciqlSuite.openNewDb();
db.insertAll(Product.getList());
// baseline count is the next id value
long bc = Utils.COUNTER.get();
// number of fields in primitives model class
// each from() call will increment Utils.COUNTER by this amount
int fc = Product.class.getFields().length;
Product p = new Product();
// This test confirms standard object referencing querying.
long count = db.from(p).where(p.productId).is(9).selectCount();
assertEquals(1, count);
// Confirms that productId counter value is baseline counter value
assertEquals(bc, p.productId.intValue());
try {
// This test compares "bc + fc" which is the counter value of
// unitsInStock assigned by Utils.newObject() after the 2nd pass
// through from().
//
// Object fields map by REFERENCE, not value.
db.from(p).where(Long.valueOf(bc + fc).intValue()).is(9).orderBy(p.productId).select();
assertTrue("Fail: object field is mapping by value.", false);
} catch (IciqlException e) {
assertEquals(IciqlException.CODE_UNMAPPED_FIELD, e.getIciqlCode());
assertEquals(bc + 5, p.productId.intValue());
}
try {
// This test compares Integer(bc) which is the counter value of
// unitsInStock assigned by Utils.newObject() after the 3rd pass
// through from().
//
// Object fields map by REFERENCE, not value.
db.from(p).where(Long.valueOf(bc).intValue()).is(9).orderBy(p.productId).select();
assertTrue("Fail: object field is mapping by value.", false);
} catch (IciqlException e) {
assertEquals(IciqlException.CODE_UNMAPPED_FIELD, e.getIciqlCode());
assertEquals(bc + (2 * fc), p.productId.intValue());
}
db.close();
}
/**
* Confirms that primitive aliases ARE mapped by value.
*/
@Test
public void testPrimitiveAliasMapping() throws Exception {
Db db = IciqlSuite.openNewDb();
PrimitivesModel model = new PrimitivesModel();
model.myLong = 100L;
db.insert(model);
model.myLong = 200L;
db.insert(model);
// baseline count is the next id value
long bc = Utils.COUNTER.get();
// number of fields in primitives model class
// each from() call will increment Utils.COUNTER by this amount
int fc = PrimitivesModel.class.getFields().length;
PrimitivesModel p = new PrimitivesModel();
// This test confirms standard primitive referencing querying.
long count = db.from(p).where(p.myLong).is(100L).selectCount();
assertEquals(1, count);
// Confirms that myLong counter value is bc
assertEquals(bc, p.myLong);
try {
// This test compares "bc + fc" which is the counter value
// of myLong assigned by Utils.newObject() after the 2nd pass
// through from().
//
// Primitive fields map by VALUE.
count = db.from(p).where(bc + fc).is(100L).selectCount();
assertEquals(1, count);
assertEquals(bc + fc, p.myLong);
} catch (IciqlException e) {
assertTrue(e.getMessage(), false);
}
try {
// This test compares "bc" which was the counter value of
// myLong assigned by Utils.newObject() after the 1st pass
// through from(). "bc" is unmapped now and will throw an
// exception.
//
// Primitive fields map by VALUE.
db.from(p).where(bc).is(100L).select();
} catch (IciqlException e) {
assertEquals(IciqlException.CODE_UNMAPPED_FIELD, e.getIciqlCode());
assertEquals(bc + (2 * fc), p.myLong);
}
db.close();
}
}
|