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