1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package fr.ifremer.isisfish.datastore;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.net.URL;
31 import java.text.DateFormat;
32 import java.text.ParseException;
33 import java.text.SimpleDateFormat;
34 import java.util.Date;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.junit.Assert;
39 import org.junit.Test;
40
41
42
43
44
45
46
47
48
49
50
51
52 public class SimulationInformationTest {
53
54
55 protected static Log log = LogFactory.getLog(SimulationInformationTest.class);
56
57
58
59
60
61
62 @Test
63 public void testSimulationEmpty() throws IOException {
64
65 File file = File.createTempFile("information-empty", ".txt");
66 file.deleteOnExit();
67
68 SimulationInformation info = new SimulationInformation(file);
69 Assert.assertEquals(new Date(0), info.getSimulationStart());
70 Assert.assertEquals(new Date(0), info.getSimulationEnd());
71 Assert.assertFalse(info.hasError());
72 Assert.assertNull(info.getException());
73 Assert.assertEquals(0, info.getExportTime("test"));
74 Assert.assertNotNull(info.toString());
75 }
76
77
78
79
80
81
82 @Test
83 public void testExistingSimulationFile() throws ParseException {
84 URL informationURL = SimulationInformationTest.class.getResource("information");
85 File file = new File(informationURL.getPath());
86
87 Assert.assertTrue(file.exists());
88
89
90 DateFormat df = new SimpleDateFormat("yyyy/dd/MM hh:mm:ss");
91
92 SimulationInformation info = new SimulationInformation(file);
93 Assert.assertEquals(df.parse("2009/31/08 14:44:57"), info.getSimulationStart());
94 Assert.assertEquals(df.parse("2009/31/08 14:48:00"), info.getSimulationEnd());
95 Assert.assertEquals(1482, info.getExportTime("VesselMargin.java"));
96 Assert.assertFalse(info.hasError());
97 Assert.assertTrue(info.getOptimizationUsage().indexOf("Cache used") > 0);
98 Assert.assertNull(info.getStatistic());
99 Assert.assertTrue(info.toString().indexOf("VesselMargin.java") > 0);
100 }
101
102
103
104
105
106
107 @Test
108 public void testExistingSimulationFileException() {
109 URL informationURL = SimulationInformationTest.class.getResource("exception.information");
110 File file = new File(informationURL.getPath());
111
112 Assert.assertTrue(file.exists());
113
114 SimulationInformation info = new SimulationInformation(file);
115 Assert.assertTrue(info.hasError());
116 Assert.assertTrue(info.getException().indexOf("TopiaException") > 0);
117 Assert.assertTrue(info.toString().indexOf("TopiaException") > 0);
118 }
119
120
121
122
123
124
125
126 @Test
127 public void testSimulationDate() throws IOException, ParseException {
128
129
130 DateFormat df = new SimpleDateFormat("hh:mm:ss a");
131
132 File file = File.createTempFile("information-date", ".txt");
133 file.deleteOnExit();
134
135 SimulationInformation info = new SimulationInformation(file);
136
137 Date d1 = df.parse("3:30:32 pm");
138 Date d2 = df.parse("3:50:47 pm");
139 info.setSimulationStart(d1);
140 info.setSimulationEnd(d2);
141
142 info.store();
143
144 SimulationInformation testInfo = new SimulationInformation(file);
145 Assert.assertEquals(d1, testInfo.getSimulationStart());
146 Assert.assertEquals(d2, testInfo.getSimulationEnd());
147 Assert.assertTrue(testInfo.toString().indexOf("15:50:47") > 0);
148 }
149
150
151
152
153
154
155 @Test
156 public void testSimulationException() throws IOException {
157 File file = File.createTempFile("information-exception", ".txt");
158 file.deleteOnExit();
159
160 SimulationInformation info = new SimulationInformation(file);
161 Assert.assertFalse(info.hasError());
162
163 Exception e = new Exception("Oula la, ya eu une exception super grave");
164 info.setException(e);
165 info.store();
166
167 SimulationInformation testInfo = new SimulationInformation(file);
168 Assert.assertTrue(testInfo.hasError());
169 Assert.assertTrue(testInfo.getException().indexOf("grave") > 0);
170 Assert.assertTrue(testInfo.toString().indexOf("grave") > 0);
171
172 }
173
174
175
176
177
178 @Test
179 public void testSimulationExportTime() throws IOException {
180 File file = File.createTempFile("information-export", ".txt");
181 file.deleteOnExit();
182
183 SimulationInformation info = new SimulationInformation(file);
184 info.addExportTime("export1", 30);
185 info.addExportTime("export2", 40);
186 info.addExportTime("export3", 50);
187 info.addExportTime("export4", 60);
188
189 info.store();
190
191 SimulationInformation testInfo = new SimulationInformation(file);
192 Assert.assertFalse(testInfo.hasError());
193 Assert.assertEquals(30, testInfo.getExportTime("export1"));
194 Assert.assertEquals(40, testInfo.getExportTime("export2"));
195 Assert.assertEquals(50, testInfo.getExportTime("export3"));
196 Assert.assertEquals(60, testInfo.getExportTime("export4"));
197 Assert.assertTrue(testInfo.toString().indexOf("export1") > 0);
198 }
199
200
201
202
203
204
205 @Test
206 public void testSimulationRuleTime() throws IOException {
207 File file = File.createTempFile("information-rule", ".txt");
208 file.deleteOnExit();
209
210 SimulationInformation info = new SimulationInformation(file);
211 info.addRuleInitTime("rule1", 30);
212 info.addRuleInitTime("rule2", 8);
213 info.addRuleInitTime("rule3", 321);
214 info.addRuleInitTime("rule4", 123);
215
216
217 info.addRulePreTime("rule1", 20);
218 info.addRulePostTime("rule1", 25);
219 info.addRulePreTime("rule1", 20);
220 info.addRulePostTime("rule1", 25);
221
222 info.addRulePreTime("rule2", 18);
223 info.addRulePostTime("rule2", 500);
224
225 info.addRulePreTime("rule3", 20);
226
227
228 info.store();
229
230 SimulationInformation testInfo = new SimulationInformation(file);
231 Assert.assertFalse(testInfo.hasError());
232 Assert.assertEquals(30, testInfo.getRuleInitTime("rule1"));
233 Assert.assertEquals(8, testInfo.getRuleInitTime("rule2"));
234 Assert.assertEquals(321, testInfo.getRuleInitTime("rule3"));
235 Assert.assertEquals(123, testInfo.getRuleInitTime("rule4"));
236
237 Assert.assertEquals(20 + 20, testInfo.getRulePreTime("rule1"));
238 Assert.assertEquals(25 + 25, testInfo.getRulePostTime("rule1"));
239
240 Assert.assertEquals(18, testInfo.getRulePreTime("rule2"));
241 Assert.assertEquals(500, testInfo.getRulePostTime("rule2"));
242
243 Assert.assertEquals(20, testInfo.getRulePreTime("rule3"));
244 Assert.assertEquals(0, testInfo.getRulePostTime("rule3"));
245
246 Assert.assertEquals(0, testInfo.getRulePreTime("rule4"));
247 Assert.assertEquals(0, testInfo.getRulePostTime("rule4"));
248
249
250 Assert.assertTrue(testInfo.toString().indexOf("rule2 : 0.526") > 0);
251 }
252 }