1 | package de.uka.ipd.sdq.sensorframework.tests; |
2 | |
3 | import java.io.IOException; |
4 | |
5 | import junit.framework.Assert; |
6 | import junit.framework.TestCase; |
7 | import de.uka.ipd.sdq.sensorframework.entities.Experiment; |
8 | import de.uka.ipd.sdq.sensorframework.entities.ExperimentRun; |
9 | import de.uka.ipd.sdq.sensorframework.entities.Measurement; |
10 | import de.uka.ipd.sdq.sensorframework.entities.Sensor; |
11 | import de.uka.ipd.sdq.sensorframework.entities.SensorAndMeasurements; |
12 | import de.uka.ipd.sdq.sensorframework.entities.State; |
13 | import de.uka.ipd.sdq.sensorframework.entities.StateMeasurement; |
14 | import de.uka.ipd.sdq.sensorframework.entities.StateSensor; |
15 | import de.uka.ipd.sdq.sensorframework.entities.TimeSpanMeasurement; |
16 | import de.uka.ipd.sdq.sensorframework.entities.TimeSpanSensor; |
17 | import de.uka.ipd.sdq.sensorframework.entities.dao.IDAOFactory; |
18 | import de.uka.ipd.sdq.sensorframework.entities.dao.IExperimentDAO; |
19 | |
20 | public abstract class AbstractSerialiserTests extends TestCase { |
21 | |
22 | private static final int CREATE_COUNT = 10; |
23 | |
24 | public void testExperimentStore() throws IOException { |
25 | // store some data |
26 | IDAOFactory factory = createCleanDAOFactory(); |
27 | IExperimentDAO experimentDAO = factory.createExperimentDAO(); |
28 | for (int i=0; i < CREATE_COUNT; i++) { |
29 | @SuppressWarnings("unused") |
30 | Experiment e = experimentDAO.addExperiment("Test"+i); |
31 | } |
32 | factory.finalizeAndClose(); |
33 | |
34 | // load it again |
35 | factory = createDAOFactory(); |
36 | experimentDAO = factory.createExperimentDAO(); |
37 | int count = 0; |
38 | for (Experiment e : experimentDAO.getExperiments()) { |
39 | count++; |
40 | Assert.assertEquals(0,e.getSensors().size()); |
41 | Assert.assertEquals(0,e.getExperimentRuns().size()); |
42 | } |
43 | Assert.assertEquals(10, experimentDAO.getExperiments().size()); |
44 | Assert.assertEquals(10, count); |
45 | factory.finalizeAndClose(); |
46 | } |
47 | |
48 | public void testExperimentStoreAfterReload() throws IOException { |
49 | // store some data |
50 | IDAOFactory factory = createCleanDAOFactory(); |
51 | factory.finalizeAndClose(); |
52 | |
53 | for (int i=0; i < CREATE_COUNT; i++) { |
54 | factory = createDAOFactory(); |
55 | IExperimentDAO experimentDAO = factory.createExperimentDAO(); |
56 | @SuppressWarnings("unused") |
57 | Experiment e = experimentDAO.addExperiment("Test"+i); |
58 | factory.finalizeAndClose(); |
59 | } |
60 | |
61 | // load it again |
62 | factory = createDAOFactory(); |
63 | IExperimentDAO experimentDAO = factory.createExperimentDAO(); |
64 | int count = 0; |
65 | for (Experiment e : experimentDAO.getExperiments()) { |
66 | count++; |
67 | Assert.assertEquals(0,e.getSensors().size()); |
68 | Assert.assertEquals(0,e.getExperimentRuns().size()); |
69 | } |
70 | factory.finalizeAndClose(); |
71 | Assert.assertEquals(CREATE_COUNT, count); |
72 | } |
73 | |
74 | public void testSensorStore() throws IOException { |
75 | // store some data |
76 | IDAOFactory factory = createCleanDAOFactory(); |
77 | IExperimentDAO experimentDAO = factory.createExperimentDAO(); |
78 | Experiment e = experimentDAO.addExperiment("Test"); |
79 | addTimeSpanSensors(factory, e); |
80 | addStateSensors(factory, e); |
81 | factory.finalizeAndClose(); |
82 | |
83 | // load it again |
84 | factory = createDAOFactory(); |
85 | experimentDAO = factory.createExperimentDAO(); |
86 | for (Experiment ex : experimentDAO.getExperiments()) { |
87 | int count = 0; int tssCount = 0; int stateCount = 0; |
88 | for (Sensor s : ex.getSensors()) { |
89 | count++; |
90 | if (s instanceof TimeSpanSensor) { |
91 | tssCount++; |
92 | } else { |
93 | stateCount++; |
94 | Assert.assertEquals(1,((StateSensor)s).getSensorStates().size()); |
95 | } |
96 | Assert.assertTrue(s instanceof TimeSpanSensor ? s.getSensorName().startsWith("TSS") : s.getSensorName().startsWith("StateSensor")); |
97 | } |
98 | Assert.assertEquals(2*CREATE_COUNT, count); |
99 | Assert.assertEquals(CREATE_COUNT, tssCount); |
100 | Assert.assertEquals(CREATE_COUNT, stateCount); |
101 | } |
102 | factory.finalizeAndClose(); |
103 | } |
104 | |
105 | public void testSensorStore2() throws IOException { |
106 | // store some data |
107 | IDAOFactory factory = createCleanDAOFactory(); |
108 | IExperimentDAO experimentDAO = factory.createExperimentDAO(); |
109 | Experiment e = experimentDAO.addExperiment("Test"); |
110 | addStateSensors(factory, e); |
111 | factory.finalizeAndClose(); |
112 | |
113 | // load it again |
114 | factory = createDAOFactory(); |
115 | int statesCount = factory.createStateDAO().getStates().size(); |
116 | factory.finalizeAndClose(); |
117 | Assert.assertEquals(1, statesCount); |
118 | } |
119 | |
120 | public void testExperimentRunStore() throws IOException { |
121 | // store some data |
122 | IDAOFactory factory = createCleanDAOFactory(); |
123 | IExperimentDAO experimentDAO = factory.createExperimentDAO(); |
124 | Experiment e = experimentDAO.addExperiment("Test"); |
125 | addTimeSpanSensors(factory, e); |
126 | addStateSensors(factory, e); |
127 | addExperimentRuns(factory, e); |
128 | factory.finalizeAndClose(); |
129 | |
130 | // load it again |
131 | factory = createDAOFactory(); |
132 | experimentDAO = factory.createExperimentDAO(); |
133 | for (Experiment ex : experimentDAO.getExperiments()) { |
134 | int count = 0; |
135 | for (ExperimentRun er : ex.getExperimentRuns()) { |
136 | count++; |
137 | Assert.assertTrue(er.getExperimentDateTime().startsWith("Date")); |
138 | } |
139 | Assert.assertEquals(CREATE_COUNT, count); |
140 | } |
141 | factory.finalizeAndClose(); |
142 | } |
143 | |
144 | public void testExperimentRunStore2() throws IOException { |
145 | // store some data |
146 | IDAOFactory factory = createCleanDAOFactory(); |
147 | IExperimentDAO experimentDAO = factory.createExperimentDAO(); |
148 | Experiment e = experimentDAO.addExperiment("Test"); |
149 | addExperimentRuns(factory, e); |
150 | factory.finalizeAndClose(); |
151 | |
152 | // load it again |
153 | factory = createDAOFactory(); |
154 | int count = factory.createExperimentRunDAO().getExperimentRuns().size(); |
155 | Assert.assertEquals(CREATE_COUNT, count); |
156 | factory.finalizeAndClose(); |
157 | } |
158 | |
159 | public void testExperimentRunStoreAfterReload() throws IOException { |
160 | testExperimentRunStore(); |
161 | |
162 | // store some additional data |
163 | IDAOFactory factory = createDAOFactory(); |
164 | IExperimentDAO experimentDAO = factory.createExperimentDAO(); |
165 | Experiment e = experimentDAO.addExperiment("Test2"); |
166 | addTimeSpanSensors(factory, e); |
167 | addStateSensors(factory, e); |
168 | addExperimentRuns(factory, e); |
169 | factory.finalizeAndClose(); |
170 | |
171 | // load it again |
172 | factory = createDAOFactory(); |
173 | experimentDAO = factory.createExperimentDAO(); |
174 | int count = 0; |
175 | for (@SuppressWarnings("unused") |
176 | Experiment ex : experimentDAO.getExperiments()) { |
177 | count++; |
178 | } |
179 | Assert.assertEquals(2, count); |
180 | factory.finalizeAndClose(); |
181 | } |
182 | |
183 | public void testTimeSpanMeasurements() throws IOException { |
184 | testTimeSpanMeasurementsInternal(CREATE_COUNT); |
185 | } |
186 | |
187 | public void testTimeSpanMeasurementsLarge() throws IOException { |
188 | testTimeSpanMeasurementsInternal(CREATE_COUNT*12345 + 123); |
189 | } |
190 | |
191 | |
192 | private void testTimeSpanMeasurementsInternal(final int createCount) throws IOException { |
193 | // store some additional data |
194 | IDAOFactory factory = createCleanDAOFactory(); |
195 | IExperimentDAO experimentDAO = factory.createExperimentDAO(); |
196 | Experiment e = experimentDAO.addExperiment("Test2"); |
197 | addTimeSpanSensors(factory, e); |
198 | addExperimentRuns(factory, e); |
199 | addTimeSpanMeasurements(factory,e,createCount); |
200 | factory.finalizeAndClose(); |
201 | |
202 | // load it again |
203 | factory = createDAOFactory(); |
204 | experimentDAO = factory.createExperimentDAO(); |
205 | e = experimentDAO.getExperiments().iterator().next(); |
206 | TimeSpanSensor tss = (TimeSpanSensor) e.getSensors().iterator().next(); |
207 | ExperimentRun er = e.getExperimentRuns().iterator().next(); |
208 | SensorAndMeasurements sam = er.getMeasurementsOfSensor(tss); |
209 | Assert.assertEquals(tss, sam.getSensor()); |
210 | int count = 0; |
211 | for (Measurement m : sam.getMeasurements()) { |
212 | Assert.assertTrue(m instanceof TimeSpanMeasurement); |
213 | TimeSpanMeasurement tsm = (TimeSpanMeasurement) m; |
214 | Assert.assertTrue(tsm.getTimeSpan() > 0); |
215 | Assert.assertTrue(tsm.getEventTime() > 0); |
216 | count++; |
217 | } |
218 | Assert.assertEquals(createCount, count); |
219 | Assert.assertEquals(createCount, sam.getMeasurements().size()); |
220 | factory.finalizeAndClose(); |
221 | } |
222 | |
223 | public void testStateMeasurements() throws IOException { |
224 | testStateMeasurementsInternal(CREATE_COUNT); |
225 | } |
226 | |
227 | public void testStateMeasurementsLarge() throws IOException { |
228 | testStateMeasurementsInternal(CREATE_COUNT*12345 + 123); |
229 | } |
230 | |
231 | private void testStateMeasurementsInternal(int createCount) throws IOException { |
232 | // store some additional data |
233 | IDAOFactory factory = createCleanDAOFactory(); |
234 | IExperimentDAO experimentDAO = factory.createExperimentDAO(); |
235 | Experiment e = experimentDAO.addExperiment("Test2"); |
236 | addStateSensors(factory, e); |
237 | addExperimentRuns(factory, e); |
238 | addStateMeasurements(factory,e,createCount); |
239 | factory.finalizeAndClose(); |
240 | |
241 | // load it again |
242 | factory = createDAOFactory(); |
243 | experimentDAO = factory.createExperimentDAO(); |
244 | e = experimentDAO.getExperiments().iterator().next(); |
245 | StateSensor ssensor = (StateSensor) e.getSensors().iterator().next(); |
246 | ExperimentRun er = e.getExperimentRuns().iterator().next(); |
247 | SensorAndMeasurements sam = er.getMeasurementsOfSensor(ssensor); |
248 | Assert.assertEquals(ssensor, sam.getSensor()); |
249 | int count = 0; |
250 | for (Measurement m : sam.getMeasurements()) { |
251 | Assert.assertTrue(m instanceof StateMeasurement); |
252 | StateMeasurement sm = (StateMeasurement) m; |
253 | Assert.assertNotNull(sm.getSensorState()); |
254 | Assert.assertEquals(sm.getSensorState(), ssensor.getInitialState()); |
255 | Assert.assertTrue(sm.getEventTime() > 0); |
256 | count++; |
257 | } |
258 | Assert.assertEquals(createCount, count); |
259 | Assert.assertEquals(createCount, sam.getMeasurements().size()); |
260 | factory.finalizeAndClose(); |
261 | } |
262 | |
263 | |
264 | private void addTimeSpanMeasurements(IDAOFactory factory, Experiment e, int create_count) { |
265 | TimeSpanSensor tss = (TimeSpanSensor) e.getSensors().iterator().next(); |
266 | ExperimentRun er = e.getExperimentRuns().iterator().next(); |
267 | for (int i = 0; i < create_count; i++) { |
268 | er.addTimeSpanMeasurement(tss, i + 1, i + 1); |
269 | } |
270 | } |
271 | |
272 | private void addStateMeasurements(IDAOFactory factory, Experiment e, int createCount) { |
273 | StateSensor ssensor = (StateSensor) e.getSensors().iterator().next(); |
274 | ExperimentRun er = e.getExperimentRuns().iterator().next(); |
275 | for (int i = 0; i < createCount; i++) { |
276 | er.addStateMeasurement(ssensor, ssensor.getInitialState(), i + 1); |
277 | } |
278 | } |
279 | |
280 | private void addExperimentRuns(IDAOFactory factory, Experiment e) { |
281 | for (int i=0; i < CREATE_COUNT; i++) { |
282 | ExperimentRun run = factory.createExperimentRunDAO().addExperimentRun("Date"+i); |
283 | e.addExperimentRun(run); |
284 | } |
285 | } |
286 | |
287 | private void addTimeSpanSensors(IDAOFactory factory, Experiment e) { |
288 | for (int i=0; i < CREATE_COUNT; i++) { |
289 | Sensor s = factory.createSensorDAO().addTimeSpanSensor("TSS"+i); |
290 | e.addSensor(s); |
291 | } |
292 | } |
293 | |
294 | private void addStateSensors(IDAOFactory factory, Experiment e) { |
295 | State state = factory.createStateDAO().addState("AState"); |
296 | for (int i=0; i < CREATE_COUNT; i++) { |
297 | StateSensor s = factory.createSensorDAO().addStateSensor(state, "StateSensor"+i); |
298 | s.addSensorState(state); |
299 | e.addSensor(s); |
300 | } |
301 | } |
302 | |
303 | protected abstract IDAOFactory createDAOFactory(); |
304 | protected abstract IDAOFactory createCleanDAOFactory() throws IOException; |
305 | } |