View Javadoc
1   /*
2    * #%L
3    * IsisFish
4    * 
5    * $Id: SimulationParameterTest.java 4156 2014-12-09 11:27:18Z echatellier $
6    * $HeadURL: https://svn.codelutin.com/isis-fish/tags/isis-fish-4.4.0.2/src/test/java/fr/ifremer/isisfish/simulator/SimulationParameterTest.java $
7    * %%
8    * Copyright (C) 2009 - 2010 Ifremer, Code Lutin
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU General Public License as
12   * published by the Free Software Foundation, either version 3 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/gpl-3.0.html>.
23   * #L%
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   * Simulation parameter test class.
45   *
46   * @author chatellier
47   * @version $Revision: 4156 $
48   * 
49   * Last update : $Date: 2014-12-09 12:27:18 +0100 (Tue, 09 Dec 2014) $
50   * By : $Author: echatellier $
51   */
52  public class SimulationParameterTest extends AbstractIsisFishTest {
53  
54      /** Logger for this class. */
55      private static Log log = LogFactory.getLog(SimulationParameterTest.class);
56      
57      /**
58       * Test les valeurs par defaut des parametres de simulations.
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          // Not easy to test it
76          //Assert.assertNotNull(params.getNumberOf(new PopulationImpl()));
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       * Test to build one simulation parameters.
97       * 
98       * Write it on disk, read it from disk.
99       * And test it.s
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         // make transform
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      * Test de lecture d'un fichier existant.
121      * 
122      * Test que les valeurs string sont correcte.
123      * Et que les instances regles/export... sont correctes.
124      * 
125      * Cas 1 : file basic
126      * @throws IOException 
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         // test des regles
141         Assert.assertNull(param.rules);
142         Assert.assertEquals(2, param.getRules().size());
143         Assert.assertNotNull(param.rules);
144         
145         // test des exports
146         Assert.assertNull(param.exportNames);
147         
148         // test des export de sensibilité
149         Assert.assertNull(param.sensitivityExports);
150         Assert.assertEquals(0, param.getSensitivityExport().size());
151         Assert.assertNotNull(param.sensitivityExports);
152     }
153     
154     /**
155      * Test une copies de simulation parameters sans instancier
156      * les regles, export, plans... pour verifier qu'il ne perde pas
157      * les informations des paramètres sur lesquels getxxx() n'a pas été
158      * appelé.
159      * @throws IOException
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         // test rules
175         Assert.assertNull(param.rules);
176         Assert.assertEquals(2, param2.getRules().size());
177         
178         // test analyse plans
179         Assert.assertNull(param.simulationPlans);
180         Assert.assertEquals(0, param2.getSimulationPlans().size());
181         
182         // test sur les populations
183         Assert.assertNull(param.populations);
184         Assert.assertEquals(1, param2.getPopulations().size());
185     }
186     
187     /**
188      * Test de copie, avec une classe manquante dans le config.
189      * L'intanciation de la classe ne doit pas etre appelée.
190      * Et la configuration de la classe doit être copiée.
191      * 
192      * @throws IOException
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         // test simulation plans
208         Assert.assertNull(param.simulationPlans);
209         Assert.assertTrue(param2.propertiesParameters.containsKey("plans"));
210         // ca rend zero parce que l'instanciation doit echouer
211         Assert.assertEquals(0, param2.getSimulationPlans().size());
212     }
213 
214     /**
215      * Test toString() method.
216      * 
217      * @throws IOException 
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         // force some properties instantiation
229         param.getSimulationPlans();
230         param.getStrategies();
231         // not populations
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         // test some collections
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      * Test toString() method.
245      * 
246      * @throws IOException 
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      * Test copy method without setting internals properties.
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      * Test que le log level est bien correctement recopié.
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 }