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.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   * Tests Hibernate connection.
42   * 
43   * Created: August 17, 2008
44   * 
45   * @author Christina Bohk
46   * @author Roland Ewald
47   * 
48   */
49  public class TestHibernateConnection extends TestCase {
50  
51    /**
52     * Explanation for assertion regarding instance generation.
53     */
54    public static final String EXPL_INSTANTIATION = "Instance should have been generated";
55  
56    /**
57     * Explanation for assertion regarding automated ID generation.
58     */
59    public static final String EXPL_ID_SAVE = "ID should be set by hibernate on save";
60  
61    /**
62     * Explanation for asserting the retrieval of identical entities.
63     */
64    public static final String EXPL_UNIQUE = "ID should be the same as before.";
65  
66    /**
67     * Explanation for asserting the size of the table (1 row).
68     */
69    public static final String EXPL_ONE_ENTITY = "Only one entity should be in the DB.";
70  
71    /**
72     * Explanation for asserting the size of the table (2 rows).
73     */
74    public static final String EXPL_TWO_ENTITIES = "Two entities should be in the database.";
75  
76    /**
77     * Explanation for asserting a proper deletion.
78     */
79    public static final String EXPL_DELETION = "Deletion of entityfrom data base should work";
80  
81    /**
82     * Explanation for asserting object equality.
83     */
84    public static final String EXPL_EQUAL_FIRST = "The remaining instance should equal the first one.";
85  
86    /**
87     * Width of the square used for testing.
88     */
89    public static final int TEST_SQUARE_MATRIX_WIDTH = 128;
90  
91    /**
92     * Number of generations for which to test.
93     */
94    public static final int TEST_GENERATIONS = 7;
95  
96    /**
97     * Maximum age for testing.
98     */
99    public static final int TEST_MAX_AGE = 100;
100 
101   /** The jump-off year for testing. */
102   public static final int TEST_JUMP_OFF_YEAR = PPPModelFactory.DEFAULT_JUMP_OFF_YEAR;
103 
104   /**
105    * Number of years to be predicted when testing.
106    */
107   public static final int TEST_PRED_YEARS = 100;
108 
109   /**
110    * The hibernate configuration.
111    */
112   IP3MDatabase db;
113 
114   /**
115    * Test parameter.
116    */
117   Parameter param;
118 
119   /**
120    * Test parameter instance.
121    */
122   ParameterInstance instance;
123 
124   /**
125    * Test matrix number one.
126    */
127   Matrix matrix1;
128 
129   /**
130    * Test matrix number two.
131    */
132   Matrix matrix2;
133 
134   /**
135    * Test parameter assignment one.
136    */
137   ParameterAssignment assignment1;
138 
139   /**
140    * Test parameter assignment two.
141    */
142   ParameterAssignment assignment2;
143 
144   /**
145    * Test set, containing both assignments.
146    */
147   Set set;
148 
149   /**
150    * Test Settype, containing the set.
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    * Tests all database operations regarding {@link Parameter} objects.
171    * 
172    * @throws Exception
173    *           when something fails
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    * Tests database operations for {@link ParameterInstance} objects.
197    * 
198    * @throws Exception
199    *           when something fails
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    * Tests all matrix-related operations.
225    * 
226    * @throws Exception
227    *           if database operations fail
228    */
229   public void testMatrixOperations() throws Exception {
230     testParameterOperations();
231     parameterInstanceOperations();
232     matrixOperations();
233   }
234 
235   public void matrixOperations() throws Exception {
236     // Set up everything required
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 }