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.simulator;
27
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.Collections;
31 import java.util.Properties;
32
33 import fr.ifremer.isisfish.types.Month;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.nuiton.util.SortedProperties;
37 import org.junit.Assert;
38 import org.junit.Test;
39
40 import fr.ifremer.isisfish.AbstractIsisFishTest;
41 import fr.ifremer.isisfish.IsisConfig;
42
43
44
45
46
47
48
49
50
51
52 public class SimulationParameterTest extends AbstractIsisFishTest {
53
54
55 private static Log log = LogFactory.getLog(SimulationParameterTest.class);
56
57
58
59
60 @Test
61 public void testDefaultProperties() {
62 SimulationParameter params = new SimulationParameterImpl();
63
64 Assert.assertEquals(IsisConfig.getVersion(), params.getIsisFishVersion());
65 Assert.assertEquals("", params.getDescription());
66 Assert.assertEquals("", params.getRegionName());
67 Assert.assertEquals(Month.NUMBER_OF_MONTH, params.getNumberOfMonths());
68 Assert.assertEquals(1, params.getNumberOfYear());
69 Assert.assertEquals("DefaultSimulator.java", params.getSimulatorName());
70 Assert.assertTrue(params.getUseCache());
71 Assert.assertFalse(params.getUseStatistic());
72 Assert.assertFalse(params.getUseOptimization());
73 Assert.assertTrue(params.getStrategies().isEmpty());
74 Assert.assertTrue(params.getPopulations().isEmpty());
75
76
77 Assert.assertTrue(params.getRules().isEmpty());
78 Assert.assertTrue(params.getExtraRules().isEmpty());
79 Assert.assertTrue(params.getSimulationPlans().isEmpty());
80 Assert.assertTrue(params.getExportNames().isEmpty());
81 Assert.assertFalse(params.getUsePreScript());
82 Assert.assertEquals("", params.getPreScript());
83 Assert.assertFalse(params.getUseSimulationPlan());
84 Assert.assertEquals(-1, params.getSimulationPlanNumber());
85 Assert.assertTrue(params.getSensitivityExport().isEmpty());
86 Assert.assertEquals(-1, params.getNumberOfSensitivitySimulation());
87 Assert.assertNull(params.getSensitivityAnalysis());
88 Assert.assertTrue(params.getResultEnabled().isEmpty());
89 Assert.assertTrue(params.getTagValue().isEmpty());
90 Assert.assertEquals("info", params.getSimulLogLevel());
91 Assert.assertEquals("info", params.getScriptLogLevel());
92 Assert.assertEquals("error", params.getLibLogLevel());
93 }
94
95
96
97
98
99
100
101 @Test
102 public void testToPropertiesFromProperties() {
103
104 SimulationParameter params = new SimulationParameterImpl();
105 params.setIsisFishVersion("3.2.0.4");
106 params.setDescription("desc");
107 params.setResultEnabled(Collections.singleton("bakh"));
108
109
110 Properties props = params.toProperties();
111 SimulationParameter params2 = new SimulationParameterImpl();
112 params2.fromProperties(props);
113
114 Assert.assertEquals("3.2.0.4", params2.getIsisFishVersion());
115 Assert.assertEquals("desc", params2.getDescription());
116 Assert.assertEquals(1, params2.getResultEnabled().size());
117 }
118
119
120
121
122
123
124
125
126
127
128 @Test
129 public void testBasicFilePropertiesLoading() throws IOException {
130
131 InputStream basicFileStream = SimulationParameterTest.class.getResourceAsStream("parameters_basic.properties");
132
133 Properties props = new SortedProperties();
134 props.load(basicFileStream);
135 SimulationParameterImpl param = new SimulationParameterImpl();
136 param.fromProperties(props);
137
138 Assert.assertEquals("3.2.0.6", param.getIsisFishVersion());
139
140
141 Assert.assertNull(param.rules);
142 Assert.assertEquals(2, param.getRules().size());
143 Assert.assertNotNull(param.rules);
144
145
146 Assert.assertNull(param.exportNames);
147
148
149 Assert.assertNull(param.sensitivityExports);
150 Assert.assertEquals(0, param.getSensitivityExport().size());
151 Assert.assertNotNull(param.sensitivityExports);
152 }
153
154
155
156
157
158
159
160
161 @Test
162 public void testBasicFilePropertiesCopyWithoutInstancation() throws IOException {
163 InputStream basicFileStream = SimulationParameterTest.class.getResourceAsStream("parameters_basic.properties");
164
165 Properties props = new SortedProperties();
166 props.load(basicFileStream);
167 SimulationParameterImpl param = new SimulationParameterImpl();
168 param.fromProperties(props);
169
170 Properties props2 = param.toProperties();
171 SimulationParameter param2 = new SimulationParameterImpl();
172 param2.fromProperties(props2);
173
174
175 Assert.assertNull(param.rules);
176 Assert.assertEquals(2, param2.getRules().size());
177
178
179 Assert.assertNull(param.simulationPlans);
180 Assert.assertEquals(0, param2.getSimulationPlans().size());
181
182
183 Assert.assertNull(param.populations);
184 Assert.assertEquals(1, param2.getPopulations().size());
185 }
186
187
188
189
190
191
192
193
194 @Test
195 public void testPlanFileWithMissingClasses() throws IOException {
196 InputStream basicFileStream = SimulationParameterTest.class.getResourceAsStream("parameters_plan.properties");
197
198 Properties props = new SortedProperties();
199 props.load(basicFileStream);
200 SimulationParameterImpl param = new SimulationParameterImpl();
201 param.fromProperties(props);
202
203 Properties props2 = param.toProperties();
204 SimulationParameterImpl param2 = new SimulationParameterImpl();
205 param2.fromProperties(props2);
206
207
208 Assert.assertNull(param.simulationPlans);
209 Assert.assertTrue(param2.propertiesParameters.containsKey("plans"));
210
211 Assert.assertEquals(0, param2.getSimulationPlans().size());
212 }
213
214
215
216
217
218
219 @Test
220 public void testSimulationParametersCopy() throws IOException {
221 InputStream basicFileStream = SimulationParameterTest.class.getResourceAsStream("parameters_basic.properties");
222
223 Properties props = new SortedProperties();
224 props.load(basicFileStream);
225 SimulationParameter param = new SimulationParameterImpl();
226 param.fromProperties(props);
227
228
229 param.getSimulationPlans();
230 param.getStrategies();
231
232
233 SimulationParameter param2 = param.copy();
234
235 Assert.assertEquals("3.2.0.6", param2.getIsisFishVersion());
236 Assert.assertEquals("Done for unit testing", param2.getDescription());
237
238 Assert.assertEquals(0, param2.getSimulationPlans().size());
239 Assert.assertEquals(1, param2.getPopulations().size());
240 Assert.assertEquals(3, param2.getStrategies().size());
241 }
242
243
244
245
246
247
248 @Test
249 public void testSimulationParametersToString() throws IOException {
250 InputStream basicFileStream = SimulationParameterTest.class.getResourceAsStream("parameters_basic.properties");
251
252 Properties props = new SortedProperties();
253 props.load(basicFileStream);
254
255 SimulationParameter param = new SimulationParameterImpl();
256 param.fromProperties(props);
257
258 String toString = param.toString();
259 if (log.isInfoEnabled()) {
260 log.info("toString() result is : " + toString);
261 }
262 }
263
264
265
266
267 @Test
268 public void testCopyWithoutPropertiesLoad() {
269 SimulationParameter params = new SimulationParameterImpl();
270 params.setIsisFishVersion("3.2.0.4");
271 params.setDescription("desc");
272 params.setResultEnabled(Collections.singleton("bakh"));
273
274 SimulationParameter params2 = params.copy();
275
276 Assert.assertEquals("3.2.0.4", params2.getIsisFishVersion());
277 Assert.assertEquals("desc", params2.getDescription());
278 Assert.assertEquals(1, params2.getResultEnabled().size());
279 }
280
281
282
283
284 @Test
285 public void testLogLevelCopy() {
286 SimulationParameter params = new SimulationParameterImpl();
287 params.setSimulLogLevel("error");
288 SimulationParameter newInstance = params.deepCopy();
289 Assert.assertEquals("error", newInstance.getSimulLogLevel());
290 }
291 }