1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package p3j.database.hibernate;
17
18 import james.core.data.DBConnectionData;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import junit.framework.TestCase;
24 import p3j.database.DatabaseFactory;
25 import p3j.database.DatabaseType;
26 import p3j.database.IP3MDatabase;
27 import p3j.misc.MatrixDimension;
28 import p3j.misc.Misc;
29 import p3j.misc.math.Matrix;
30 import p3j.misc.math.Matrix2D;
31 import p3j.pppm.PPPModelFactory;
32 import p3j.pppm.ProjectionModel;
33 import p3j.pppm.parameters.Parameter;
34 import p3j.pppm.parameters.ParameterAssignment;
35 import p3j.pppm.parameters.ParameterInstance;
36 import p3j.pppm.parameters.Population;
37 import p3j.pppm.sets.Set;
38 import p3j.pppm.sets.SetType;
39
40
41
42
43
44
45
46
47
48
49 public class TestHibernateConnection extends TestCase {
50
51
52
53
54 public static final String EXPL_INSTANTIATION = "Instance should have been generated";
55
56
57
58
59 public static final String EXPL_ID_SAVE = "ID should be set by hibernate on save";
60
61
62
63
64 public static final String EXPL_UNIQUE = "ID should be the same as before.";
65
66
67
68
69 public static final String EXPL_ONE_ENTITY = "Only one entity should be in the DB.";
70
71
72
73
74 public static final String EXPL_TWO_ENTITIES = "Two entities should be in the database.";
75
76
77
78
79 public static final String EXPL_DELETION = "Deletion of entityfrom data base should work";
80
81
82
83
84 public static final String EXPL_EQUAL_FIRST = "The remaining instance should equal the first one.";
85
86
87
88
89 public static final int TEST_SQUARE_MATRIX_WIDTH = 128;
90
91
92
93
94 public static final int TEST_GENERATIONS = 7;
95
96
97
98
99 public static final int TEST_MAX_AGE = 100;
100
101
102 public static final int TEST_JUMP_OFF_YEAR = PPPModelFactory.DEFAULT_JUMP_OFF_YEAR;
103
104
105
106
107 public static final int TEST_PRED_YEARS = 100;
108
109
110
111
112 IP3MDatabase db;
113
114
115
116
117 Parameter param;
118
119
120
121
122 ParameterInstance instance;
123
124
125
126
127 Matrix matrix1;
128
129
130
131
132 Matrix matrix2;
133
134
135
136
137 ParameterAssignment assignment1;
138
139
140
141
142 ParameterAssignment assignment2;
143
144
145
146
147 Set set;
148
149
150
151
152 SetType setType;
153
154 @Override
155 public void setUp() throws Exception {
156 super.setUp();
157 DatabaseFactory.setDbConnData(new DBConnectionData(
158 "jdbc:hsqldb:mem:ppm_db", "testuser", "", Misc.HIBERNATE_DIALECTS
159 .get(DatabaseType.HSQLDB)));
160 db = DatabaseFactory.createDatabase(Misc.TEST_HIBERNATE_CONFIG_FILE);
161 }
162
163 @Override
164 public void tearDown() throws Exception {
165 db.clear();
166 DatabaseFactory.reset();
167 }
168
169
170
171
172
173
174
175 public void testParameterOperations() throws Exception {
176 param = db.newParameter("test1", true, MatrixDimension.SINGLE,
177 MatrixDimension.AGES, Population.NATIVES);
178 assertNotNull(EXPL_INSTANTIATION, param);
179 assertEquals(EXPL_ID_SAVE, 1, param.getID());
180 Parameter param2 = db.newParameter("test1", true, MatrixDimension.SINGLE,
181 MatrixDimension.AGES, Population.NATIVES);
182 assertEquals(EXPL_UNIQUE, 1, param2.getID());
183 Parameter param3 = db.getParameter("test1");
184 assertEquals(EXPL_ONE_ENTITY, param2, param3);
185 param2 = db.newParameter("test2", true, MatrixDimension.SINGLE,
186 MatrixDimension.AGES, Population.NATIVES);
187 List<Parameter> parameters = db.getAllParameters();
188 assertEquals(EXPL_TWO_ENTITIES, 2, parameters.size());
189 assertTrue(EXPL_DELETION, db.deleteParameter(param2));
190 parameters = db.getAllParameters();
191 assertEquals(EXPL_ONE_ENTITY, 1, parameters.size());
192 assertEquals(EXPL_EQUAL_FIRST, param, parameters.get(0));
193 }
194
195
196
197
198
199
200
201 public void testParameterInstanceOperations() throws Exception {
202 testParameterOperations();
203 parameterInstanceOperations();
204 }
205
206 public void parameterInstanceOperations() throws Exception {
207 instance = db.newParameterInstance(1, param, 1);
208 assertNotNull(EXPL_INSTANTIATION, instance);
209 assertEquals(EXPL_ID_SAVE, 1, instance.getID());
210 ParameterInstance instance2 = db.newParameterInstance(1, param, 1);
211 assertEquals(EXPL_UNIQUE, 1, instance2.getID());
212 ParameterInstance instance3 = db.getParameterInstance(param, 1);
213 assertEquals(EXPL_ONE_ENTITY, instance2, instance3);
214 instance2 = db.newParameterInstance(2, param, 2);
215 List<ParameterInstance> instances = db.getAllParameterInstances();
216 assertEquals(EXPL_TWO_ENTITIES, 2, instances.size());
217 assertTrue(EXPL_DELETION, db.deleteParameterInstance(instance2));
218 instances = db.getAllParameterInstances();
219 assertEquals(EXPL_ONE_ENTITY, 1, instances.size());
220 assertEquals(EXPL_EQUAL_FIRST, instance, instances.get(0));
221 }
222
223
224
225
226
227
228
229 public void testMatrixOperations() throws Exception {
230 testParameterOperations();
231 parameterInstanceOperations();
232 matrixOperations();
233 }
234
235 public void matrixOperations() throws Exception {
236
237 matrix1 = db.newMatrix(new Matrix2D(TEST_SQUARE_MATRIX_WIDTH,
238 TEST_SQUARE_MATRIX_WIDTH));
239 assertNotNull(EXPL_INSTANTIATION, matrix1);
240 assertEquals(EXPL_ID_SAVE, 1, matrix1.getID());
241 matrix2 = db.newMatrix(new Matrix2D(TEST_SQUARE_MATRIX_WIDTH,
242 TEST_SQUARE_MATRIX_WIDTH));
243 assertEquals(EXPL_UNIQUE, 1, matrix2.getID());
244 Matrix matrix3 = db.getMatrix(new Matrix2D(TEST_SQUARE_MATRIX_WIDTH,
245 TEST_SQUARE_MATRIX_WIDTH));
246 assertEquals(EXPL_ONE_ENTITY, matrix2, matrix3);
247 Matrix2D matValue = new Matrix2D(TEST_SQUARE_MATRIX_WIDTH,
248 TEST_SQUARE_MATRIX_WIDTH);
249 matValue.setQuick(0, 0, 1);
250 matrix2 = db.newMatrix(matValue);
251 List<Matrix> matrices = db.getAllMatrices();
252 assertEquals(EXPL_TWO_ENTITIES, 2, matrices.size());
253 assertTrue(EXPL_DELETION, db.deleteMatrix(matrix2));
254 matrices = db.getAllMatrices();
255 assertEquals(EXPL_ONE_ENTITY, 1, matrices.size());
256 assertEquals(EXPL_EQUAL_FIRST, matrix1, matrices.get(0));
257 matrix2 = db.newMatrix(matValue);
258
259 }
260
261 public void testParameterAssignmentOperations() throws Exception {
262 testParameterOperations();
263 parameterInstanceOperations();
264 matrixOperations();
265 parameterAssignmentOperations();
266 }
267
268 public void parameterAssignmentOperations() throws Exception {
269 assignment1 = db.newParameterAssignment(instance, "assignment1", "no desc",
270 1, 0.2, matrix1.getValue().copy());
271 assignment2 = db.newParameterAssignment(instance, "assignment2",
272 "no desc, again", 1, 0.2, matrix2.getValue().copy());
273 assertEquals("Two assignments have been saved.", 2, db
274 .getAllParameterAssignments(instance).size());
275 ParameterAssignment pa = db.newParameterAssignment(instance,
276 "special assignment", "-", 1, 0.2, new Matrix2D(1, 1));
277 assertEquals("Three assignments have been saved.", 3, db
278 .getAllParameterAssignments(instance).size());
279 db.deleteParameterAssignment(pa);
280 assertEquals("Two assignments due to deletion.", 2, db
281 .getAllParameterAssignments(instance).size());
282 }
283
284 public void testSetOperations() throws Exception {
285 testParameterOperations();
286 parameterInstanceOperations();
287 matrixOperations();
288 parameterAssignmentOperations();
289 setOperations();
290 }
291
292 public void setOperations() throws Exception {
293 List<ParameterInstance> instances = new ArrayList<ParameterInstance>();
294 instances.add(instance);
295 set = db.newSet(instances, "set1", "set1desc", 0.5);
296 set.addParameterAssignment(assignment1);
297 set.addParameterAssignment(assignment2);
298 db.saveSet(set);
299 assertEquals(EXPL_ONE_ENTITY, 1, db.getAllSets().size());
300 Set set2 = db.newSet(instances, "set2", "set2desc", 0.5);
301 assertEquals(EXPL_TWO_ENTITIES, 2, db.getAllSets().size());
302 db.deleteSet(set2);
303 assertEquals(EXPL_ONE_ENTITY, 1, db.getAllSets().size());
304 }
305
306 public void testSetTypeOperations() throws Exception {
307 testParameterOperations();
308 parameterInstanceOperations();
309 matrixOperations();
310 parameterAssignmentOperations();
311 setOperations();
312 setTypeOperations();
313 }
314
315 public void setTypeOperations() throws Exception {
316 setType = db.newSetType("SetType1", "setType description");
317 setType.addInstance(instance);
318 setType.createSet("myTestSet", "Set of SetType1", 1);
319 db.saveSetType(setType);
320 assertNotNull(EXPL_INSTANTIATION, setType);
321 assertEquals(EXPL_ONE_ENTITY, 1, db.getAllSetTypes().size());
322 assertEquals(EXPL_TWO_ENTITIES, 2, db.getAllSets().size());
323 SetType st2 = db.newSetType("SetType2", "Another setType description");
324 assertEquals(EXPL_TWO_ENTITIES, 2, db.getAllSetTypes().size());
325 db.deleteSeType(st2);
326 assertEquals(EXPL_ONE_ENTITY, 1, db.getAllSetTypes().size());
327 }
328
329 public void testProjectionOperations() throws Exception {
330 projectionOperations();
331 }
332
333 public void projectionOperations() throws Exception {
334 ProjectionModel projection = new ProjectionModel("test scenario",
335 "Scenario descrption", TEST_GENERATIONS, TEST_PRED_YEARS, TEST_MAX_AGE,
336 TEST_JUMP_OFF_YEAR);
337 db.newProjection(projection);
338 assertNotNull(EXPL_INSTANTIATION, projection);
339 SetType sType = projection.createSetType("SetType1",
340 "setType description for a concrete Projection");
341 projection.assignParameterInstance(projection.getAllParameterInstances()
342 .get(0), sType, false);
343 db.saveProjection(projection);
344 List<ProjectionModel> projections = db.getAllProjections();
345 assertEquals(EXPL_ONE_ENTITY, 1, projections.size());
346 assertEquals(EXPL_ONE_ENTITY, 1, projections.get(0).getUserDefinedTypes()
347 .get(0).getDefinedParameters().size());
348 ProjectionModel projection2 = new ProjectionModel("test scenario2",
349 "Scenario descrption 2", TEST_GENERATIONS + 1, TEST_PRED_YEARS,
350 TEST_MAX_AGE, TEST_JUMP_OFF_YEAR);
351 db.newProjection(projection2);
352 projections = db.getAllProjections();
353 assertEquals(EXPL_TWO_ENTITIES, 2, projections.size());
354 db.deleteProjection(projection2, null);
355 projections = db.getAllProjections();
356 assertEquals(EXPL_ONE_ENTITY, 1, projections.size());
357 }
358
359 public void testResultStorage() {
360
361 }
362 }