1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
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.IP3MDatabase;
26  import p3j.misc.MatrixDimension;
27  import p3j.misc.Misc;
28  import p3j.misc.math.Matrix;
29  import p3j.misc.math.Matrix2D;
30  import p3j.pppm.PPPModelFactory;
31  import p3j.pppm.ProjectionModel;
32  import p3j.pppm.parameters.Parameter;
33  import p3j.pppm.parameters.ParameterAssignment;
34  import p3j.pppm.parameters.ParameterInstance;
35  import p3j.pppm.parameters.Population;
36  import p3j.pppm.sets.Set;
37  import p3j.pppm.sets.SetType;
38  
39  /**
40   * Tests Hibernate connection.
41   * 
42   * Created: August 17, 2008
43   * 
44   * @author Christina Bohk
45   * @author Roland Ewald
46   * 
47   */
48  public class TestHibernateConnection extends TestCase {
49  
50  	/**
51  	 * Explanation for assertion regarding instance generation.
52  	 */
53  	public static final String EXPL_INSTANTIATION = "Instance should have been generated";
54  
55  	/**
56  	 * Explanation for assertion regarding automated ID generation.
57  	 */
58  	public static final String EXPL_ID_SAVE = "ID should be set by hibernate on save";
59  
60  	/**
61  	 * Explanation for asserting the retrieval of identical entities.
62  	 */
63  	public static final String EXPL_UNIQUE = "ID should be the same as before.";
64  
65  	/**
66  	 * Explanation for asserting the size of the table (1 row).
67  	 */
68  	public static final String EXPL_ONE_ENTITY = "Only one entity should be in the DB.";
69  
70  	/**
71  	 * Explanation for asserting the size of the table (2 rows).
72  	 */
73  	public static final String EXPL_TWO_ENTITIES = "Two entities should be in the database.";
74  
75  	/**
76  	 * Explanation for asserting a proper deletion.
77  	 */
78  	public static final String EXPL_DELETION = "Deletion of entityfrom data base should work";
79  
80  	/**
81  	 * Explanation for asserting object equality.
82  	 */
83  	public static final String EXPL_EQUAL_FIRST = "The remaining instance should equal the first one.";
84  
85  	/**
86  	 * Width of the square used for testing.
87  	 */
88  	public static final int TEST_SQUARE_MATRIX_WIDTH = 128;
89  
90  	/**
91  	 * Number of generations for which to test.
92  	 */
93  	public static final int TEST_GENERATIONS = 7;
94  
95  	/**
96  	 * Maximum age for testing.
97  	 */
98  	public static final int TEST_MAX_AGE = 100;
99  
100 	/** The jump-off year for testing. */
101 	public static final int TEST_JUMP_OFF_YEAR = PPPModelFactory.DEFAULT_JUMP_OFF_YEAR;
102 
103 	/**
104 	 * Number of years to be predicted when testing.
105 	 */
106 	public static final int TEST_PRED_YEARS = 100;
107 
108 	/**
109 	 * The hibernate configuration.
110 	 */
111 	IP3MDatabase db;
112 
113 	/**
114 	 * Test parameter.
115 	 */
116 	Parameter param;
117 
118 	/**
119 	 * Test parameter instance.
120 	 */
121 	ParameterInstance instance;
122 
123 	/**
124 	 * Test matrix number one.
125 	 */
126 	Matrix matrix1;
127 
128 	/**
129 	 * Test matrix number two.
130 	 */
131 	Matrix matrix2;
132 
133 	/**
134 	 * Test parameter assignment one.
135 	 */
136 	ParameterAssignment assignment1;
137 
138 	/**
139 	 * Test parameter assignment two.
140 	 */
141 	ParameterAssignment assignment2;
142 
143 	/**
144 	 * Test set, containing both assignments.
145 	 */
146 	Set set;
147 
148 	/**
149 	 * Test Settype, containing the set.
150 	 */
151 	SetType setType;
152 
153 	@Override
154 	public void setUp() throws Exception {
155 		super.setUp();
156 		DatabaseFactory.setDbConnData(new DBConnectionData(
157 		    "jdbc:hsqldb:mem:ppm_db", "testuser", "", null));
158 		db = DatabaseFactory.createDatabase(Misc.TEST_HIBERNATE_CONFIG_FILE);
159 	}
160 
161 	@Override
162 	public void tearDown() throws Exception {
163 		db.clear();
164 		DatabaseFactory.reset();
165 	}
166 
167 	/**
168 	 * Tests all database operations regarding {@link Parameter} objects.
169 	 * 
170 	 * @throws Exception
171 	 *           when something fails
172 	 */
173 	public void testParameterOperations() throws Exception {
174 		param = db.newParameter("test1", true, MatrixDimension.SINGLE,
175 		    MatrixDimension.AGES, Population.NATIVES);
176 		assertNotNull(EXPL_INSTANTIATION, param);
177 		assertEquals(EXPL_ID_SAVE, 1, param.getID());
178 		Parameter param2 = db.newParameter("test1", true, MatrixDimension.SINGLE,
179 		    MatrixDimension.AGES, Population.NATIVES);
180 		assertEquals(EXPL_UNIQUE, 1, param2.getID());
181 		Parameter param3 = db.getParameter("test1");
182 		assertEquals(EXPL_ONE_ENTITY, param2, param3);
183 		param2 = db.newParameter("test2", true, MatrixDimension.SINGLE,
184 		    MatrixDimension.AGES, Population.NATIVES);
185 		List<Parameter> parameters = db.getAllParameters();
186 		assertEquals(EXPL_TWO_ENTITIES, 2, parameters.size());
187 		assertTrue(EXPL_DELETION, db.deleteParameter(param2));
188 		parameters = db.getAllParameters();
189 		assertEquals(EXPL_ONE_ENTITY, 1, parameters.size());
190 		assertEquals(EXPL_EQUAL_FIRST, param, parameters.get(0));
191 	}
192 
193 	/**
194 	 * Tests database operations for {@link ParameterInstance} objects.
195 	 * 
196 	 * @throws Exception
197 	 *           when something fails
198 	 */
199 	public void testParameterInstanceOperations() throws Exception {
200 		testParameterOperations();
201 		parameterInstanceOperations();
202 	}
203 
204 	public void parameterInstanceOperations() throws Exception {
205 		instance = db.newParameterInstance(1, param, 1);
206 		assertNotNull(EXPL_INSTANTIATION, instance);
207 		assertEquals(EXPL_ID_SAVE, 1, instance.getID());
208 		ParameterInstance instance2 = db.newParameterInstance(1, param, 1);
209 		assertEquals(EXPL_UNIQUE, 1, instance2.getID());
210 		ParameterInstance instance3 = db.getParameterInstance(param, 1);
211 		assertEquals(EXPL_ONE_ENTITY, instance2, instance3);
212 		instance2 = db.newParameterInstance(2, param, 2);
213 		List<ParameterInstance> instances = db.getAllParameterInstances();
214 		assertEquals(EXPL_TWO_ENTITIES, 2, instances.size());
215 		assertTrue(EXPL_DELETION, db.deleteParameterInstance(instance2));
216 		instances = db.getAllParameterInstances();
217 		assertEquals(EXPL_ONE_ENTITY, 1, instances.size());
218 		assertEquals(EXPL_EQUAL_FIRST, instance, instances.get(0));
219 	}
220 
221 	/**
222 	 * Tests all matrix-related operations.
223 	 * 
224 	 * @throws Exception
225 	 *           if database operations fail
226 	 */
227 	public void testMatrixOperations() throws Exception {
228 		testParameterOperations();
229 		parameterInstanceOperations();
230 		matrixOperations();
231 	}
232 
233 	public void matrixOperations() throws Exception {
234 		// Set up everything required
235 		matrix1 = db.newMatrix(new Matrix2D(TEST_SQUARE_MATRIX_WIDTH,
236 		    TEST_SQUARE_MATRIX_WIDTH));
237 		assertNotNull(EXPL_INSTANTIATION, matrix1);
238 		assertEquals(EXPL_ID_SAVE, 1, matrix1.getID());
239 		matrix2 = db.newMatrix(new Matrix2D(TEST_SQUARE_MATRIX_WIDTH,
240 		    TEST_SQUARE_MATRIX_WIDTH));
241 		assertEquals(EXPL_UNIQUE, 1, matrix2.getID());
242 		Matrix matrix3 = db.getMatrix(new Matrix2D(TEST_SQUARE_MATRIX_WIDTH,
243 		    TEST_SQUARE_MATRIX_WIDTH));
244 		assertEquals(EXPL_ONE_ENTITY, matrix2, matrix3);
245 		Matrix2D matValue = new Matrix2D(TEST_SQUARE_MATRIX_WIDTH,
246 		    TEST_SQUARE_MATRIX_WIDTH);
247 		matValue.setQuick(0, 0, 1);
248 		matrix2 = db.newMatrix(matValue);
249 		List<Matrix> matrices = db.getAllMatrices();
250 		assertEquals(EXPL_TWO_ENTITIES, 2, matrices.size());
251 		assertTrue(EXPL_DELETION, db.deleteMatrix(matrix2));
252 		matrices = db.getAllMatrices();
253 		assertEquals(EXPL_ONE_ENTITY, 1, matrices.size());
254 		assertEquals(EXPL_EQUAL_FIRST, matrix1, matrices.get(0));
255 		matrix2 = db.newMatrix(matValue);
256 
257 	}
258 
259 	public void testParameterAssignmentOperations() throws Exception {
260 		testParameterOperations();
261 		parameterInstanceOperations();
262 		matrixOperations();
263 		parameterAssignmentOperations();
264 	}
265 
266 	public void parameterAssignmentOperations() throws Exception {
267 		assignment1 = db.newParameterAssignment(instance, "assignment1", "no desc",
268 		    1, 0.2, matrix1.getValue().copy());
269 		assignment2 = db.newParameterAssignment(instance, "assignment2",
270 		    "no desc, again", 1, 0.2, matrix2.getValue().copy());
271 		assertEquals("Two assignments have been saved.", 2, db
272 		    .getAllParameterAssignments(instance).size());
273 		ParameterAssignment pa = db.newParameterAssignment(instance,
274 		    "special assignment", "-", 1, 0.2, new Matrix2D(1, 1));
275 		assertEquals("Three assignments have been saved.", 3, db
276 		    .getAllParameterAssignments(instance).size());
277 		db.deleteParameterAssignment(pa);
278 		assertEquals("Two assignments due to deletion.", 2, db
279 		    .getAllParameterAssignments(instance).size());
280 	}
281 
282 	public void testSetOperations() throws Exception {
283 		testParameterOperations();
284 		parameterInstanceOperations();
285 		matrixOperations();
286 		parameterAssignmentOperations();
287 		setOperations();
288 	}
289 
290 	public void setOperations() throws Exception {
291 		List<ParameterInstance> instances = new ArrayList<ParameterInstance>();
292 		instances.add(instance);
293 		set = db.newSet(instances, "set1", "set1desc", 0.5);
294 		set.addParameterAssignment(assignment1);
295 		set.addParameterAssignment(assignment2);
296 		db.saveSet(set);
297 		assertEquals(EXPL_ONE_ENTITY, 1, db.getAllSets().size());
298 		Set set2 = db.newSet(instances, "set2", "set2desc", 0.5);
299 		assertEquals(EXPL_TWO_ENTITIES, 2, db.getAllSets().size());
300 		db.deleteSet(set2);
301 		assertEquals(EXPL_ONE_ENTITY, 1, db.getAllSets().size());
302 	}
303 
304 	public void testSetTypeOperations() throws Exception {
305 		testParameterOperations();
306 		parameterInstanceOperations();
307 		matrixOperations();
308 		parameterAssignmentOperations();
309 		setOperations();
310 		setTypeOperations();
311 	}
312 
313 	public void setTypeOperations() throws Exception {
314 		setType = db.newSetType("SetType1", "setType description");
315 		setType.addInstance(instance);
316 		setType.createSet("myTestSet", "Set of SetType1", 1);
317 		db.saveSetType(setType);
318 		assertNotNull(EXPL_INSTANTIATION, setType);
319 		assertEquals(EXPL_ONE_ENTITY, 1, db.getAllSetTypes().size());
320 		assertEquals(EXPL_TWO_ENTITIES, 2, db.getAllSets().size());
321 		SetType st2 = db.newSetType("SetType2", "Another setType description");
322 		assertEquals(EXPL_TWO_ENTITIES, 2, db.getAllSetTypes().size());
323 		db.deleteSeType(st2);
324 		assertEquals(EXPL_ONE_ENTITY, 1, db.getAllSetTypes().size());
325 	}
326 
327 	public void testProjectionOperations() throws Exception {
328 		projectionOperations();
329 	}
330 
331 	public void projectionOperations() throws Exception {
332 		ProjectionModel projection = new ProjectionModel("test scenario",
333 		    "Scenario descrption", TEST_GENERATIONS, TEST_PRED_YEARS, TEST_MAX_AGE,
334 		    TEST_JUMP_OFF_YEAR);
335 		db.newProjection(projection);
336 		assertNotNull(EXPL_INSTANTIATION, projection);
337 		SetType sType = projection.createSetType("SetType1",
338 		    "setType description for a concrete Projection");
339 		projection.assignParameterInstance(projection.getAllParameterInstances()
340 		    .get(0), sType, false);
341 		db.saveProjection(projection);
342 		List<ProjectionModel> projections = db.getAllProjections();
343 		assertEquals(EXPL_ONE_ENTITY, 1, projections.size());
344 		assertEquals(EXPL_ONE_ENTITY, 1, projections.get(0).getUserDefinedTypes()
345 		    .get(0).getDefinedParameters().size());
346 		ProjectionModel projection2 = new ProjectionModel("test scenario2",
347 		    "Scenario descrption 2", TEST_GENERATIONS + 1, TEST_PRED_YEARS,
348 		    TEST_MAX_AGE, TEST_JUMP_OFF_YEAR);
349 		db.newProjection(projection2);
350 		projections = db.getAllProjections();
351 		assertEquals(EXPL_TWO_ENTITIES, 2, projections.size());
352 		db.deleteProjection(projection2);
353 		projections = db.getAllProjections();
354 		assertEquals(EXPL_ONE_ENTITY, 1, projections.size());
355 	}
356 
357 	public void testResultStorage() {
358 
359 	}
360 }