View Javadoc
1   /*
2    * #%L
3    * IsisFish
4    * 
5    * $Id: MexicoHelperTest.java 4301 2015-09-29 16:46:43Z echatellier $
6    * $HeadURL: https://svn.codelutin.com/isis-fish/tags/isis-fish-4.4.0.2/src/test/java/fr/ifremer/isisfish/mexico/MexicoHelperTest.java $
7    * %%
8    * Copyright (C) 2006 - 2011 Ifremer, Code Lutin, Chatellier Eric
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.mexico;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.io.StringReader;
31  import java.net.URL;
32  import java.util.Arrays;
33  import java.util.Date;
34  import java.util.List;
35  import java.util.Properties;
36  import java.util.SortedMap;
37  import java.util.TreeMap;
38  
39  import javax.xml.XMLConstants;
40  import javax.xml.transform.stream.StreamSource;
41  import javax.xml.validation.Schema;
42  import javax.xml.validation.SchemaFactory;
43  import javax.xml.validation.Validator;
44  
45  import org.apache.commons.io.FileUtils;
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  import org.junit.Assert;
49  import org.junit.Before;
50  import org.junit.Test;
51  import org.nuiton.math.matrix.MatrixFactory;
52  import org.nuiton.math.matrix.MatrixHelper;
53  import org.nuiton.math.matrix.MatrixND;
54  import org.nuiton.topia.TopiaContext;
55  import org.nuiton.topia.TopiaException;
56  
57  import fr.ifremer.isisfish.AbstractIsisFishTest;
58  import fr.ifremer.isisfish.IsisFishDAOHelper;
59  import fr.ifremer.isisfish.IsisFishException;
60  import fr.ifremer.isisfish.datastore.RegionStorage;
61  import fr.ifremer.isisfish.datastore.RuleStorage;
62  import fr.ifremer.isisfish.entities.Population;
63  import fr.ifremer.isisfish.entities.PopulationImpl;
64  import fr.ifremer.isisfish.entities.PopulationSeasonInfo;
65  import fr.ifremer.isisfish.entities.PopulationSeasonInfoImpl;
66  import fr.ifremer.isisfish.entities.Strategy;
67  import fr.ifremer.isisfish.entities.Zone;
68  import fr.ifremer.isisfish.rule.Rule;
69  import fr.ifremer.isisfish.rule.RuleHelper;
70  import fr.ifremer.isisfish.simulator.sensitivity.DesignPlan;
71  import fr.ifremer.isisfish.simulator.sensitivity.Distribution;
72  import fr.ifremer.isisfish.simulator.sensitivity.Factor;
73  import fr.ifremer.isisfish.simulator.sensitivity.FactorGroup;
74  import fr.ifremer.isisfish.simulator.sensitivity.domain.ContinuousDomain;
75  import fr.ifremer.isisfish.simulator.sensitivity.domain.DiscreteDomain;
76  import fr.ifremer.isisfish.simulator.sensitivity.domain.EquationDiscreteDomain;
77  import fr.ifremer.isisfish.simulator.sensitivity.domain.RuleDiscreteDomain;
78  import fr.ifremer.isisfish.types.TimeStep;
79  
80  /**
81   * Test for MexicoHelper class.
82   * 
83   * @author chatellier
84   * @version $Revision: 4301 $
85   * 
86   * Last update: $Date: 2015-09-29 18:46:43 +0200 (Tue, 29 Sep 2015) $ by :
87   * $Author: echatellier $
88   */
89  public class MexicoHelperTest extends AbstractIsisFishTest {
90  
91      /** Commons logging log. */
92      private static Log log = LogFactory.getLog(MexicoHelperTest.class);
93  
94      protected TopiaContext regionContext;
95  
96      @Before
97      public void setUpRegion() {
98          RegionStorage regionStorage = RegionStorage.getRegion("BaseMotosICA");
99          regionContext = regionStorage.getStorage();
100     }
101 
102     /**
103      * Get a test design plan composed with all possibles value types.
104      * 
105      * @param use40Features build design plan with sub factor groups
106      * @param use401Features build plan with distribution definition
107      * @return a test design plan
108      * @throws IsisFishException 
109      * @throws TopiaException 
110      */
111     protected DesignPlan getTestDesignPlan(boolean use40Features, boolean use401Features) throws IsisFishException, TopiaException {
112         DesignPlan designPlan = new DesignPlan();
113 
114         // matrix 1
115         MatrixND matrix1 = MatrixFactory.getInstance().create("test1",
116                 new int[] { 3, 2 }, new String[] { "col1", "col2" });
117         matrix1.setValue(new int[] { 0, 0 }, 13);
118         matrix1.setValue(new int[] { 0, 1 }, -14);
119         matrix1.setValue(new int[] { 1, 0 }, 21);
120         matrix1.setValue(new int[] { 1, 1 }, 2);
121         matrix1.setValue(new int[] { 2, 0 }, 12);
122         matrix1.setValue(new int[] { 2, 1 }, -1);
123 
124         // rule 1, species = c'est une des especes de la base BaseMotosICA
125         Rule ruleTacPoids = RuleStorage.getRule("TACpoids").getNewInstance();
126         Properties props = new Properties();
127         props.setProperty("rule.0.parameter.species", "fr.ifremer.isisfish.entities.Species#1169028645830#0.022262364425031378");
128         RuleHelper.populateRule(0, regionContext, ruleTacPoids, props);
129 
130         // rule 2, species = c'est une des especes de la base BaseMotosICA
131         Rule ruleTailleMin = RuleStorage.getRule("TailleMin").getNewInstance();
132         props = new Properties();
133         props.setProperty("rule.0.parameter.species", "fr.ifremer.isisfish.entities.Species#1169028645830#0.022262364425031378");
134         props.setProperty("rule.0.parameter.propSurvie", "42");
135         RuleHelper.populateRule(0, regionContext, ruleTailleMin, props);
136 
137         // factor 1
138         Factor factorContinuous = new Factor("factor 1 (double continuous)");
139         ContinuousDomain domain1 = new ContinuousDomain(Distribution.QUNIFMM);
140         domain1.addDistributionParam("min", 0.0);
141         domain1.addDistributionParam("max", 50.0);
142         factorContinuous.setDomain(domain1);
143         factorContinuous.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521013#0.1715620681984218#maxLength");
144 
145         // factor 2
146         Factor factorDiscrete = new Factor("factor 2 (double discrete)");
147         DiscreteDomain domain2 = new DiscreteDomain();
148         domain2.getValues().put(0.0, 12.3);
149         domain2.getValues().put(1.0, 70.9);
150         domain2.getValues().put(2.0, 21.0);
151         domain2.getValues().put(3.0, -12.1);
152         domain2.getValues().put(4.0, -8.45);
153         factorDiscrete.setDomain(domain2);
154         factorDiscrete.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521064#0.022976136053553198#minLength");
155         factorDiscrete.setValue(3.0);
156 
157         // factor 3
158         Factor factorContinuous2 = new Factor("factor 3 (integer discrete)");
159         DiscreteDomain domain3 = new DiscreteDomain();
160         domain3.getValues().put(0.0, 13);
161         domain3.getValues().put(1.0, 14);
162         domain3.getValues().put(2.0, 45);
163         factorContinuous2.setDomain(domain3);
164         factorContinuous2.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521076#0.6526656643346673#minLength");
165 
166         // factor 4
167         Factor factorMatrixContinuous = new Factor("factor 4 (MatrixContinuous)");
168         ContinuousDomain domain4 = new ContinuousDomain(Distribution.QUNIFPC);
169         domain4.addDistributionParam("reference", matrix1);
170         domain4.addDistributionParam("coefficient", 0.799);
171         factorMatrixContinuous.setDomain(domain4);
172         factorMatrixContinuous.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521076#0.6526656643346673#minLength");
173 
174         // factor 5
175         Factor factorEquationContinuous = new Factor("factor 5 (EquationContinuous)");
176         factorEquationContinuous.setEquationVariableName("Ktemp");
177         ContinuousDomain domain5 = new ContinuousDomain(Distribution.QUNIFPC);
178         domain5.addDistributionParam("reference", 45.0);
179         domain5.addDistributionParam("coefficient", 0.1);
180         factorEquationContinuous.setDomain(domain5);
181         factorEquationContinuous.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521076#0.6526656643346673#maxLength");
182 
183         // factor 1
184         Factor factorContinuousPercentage = new Factor("factor 6 (double continuous percentage)");
185         ContinuousDomain domain6 = new ContinuousDomain(Distribution.QUNIFPC);
186         domain6.addDistributionParam("reference", 14.0);
187         domain6.addDistributionParam("coefficient", 5.0);
188         factorContinuousPercentage.setCardinality(5);
189         factorContinuousPercentage.setDomain(domain6);
190         factorContinuousPercentage.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1142003453434#0.223499349929004#size");
191 
192         // 4.0.0.0
193         if (use40Features) {
194 
195             // factor 7 : rule factor
196             Factor factorRuleDiscrete = new Factor("factor 7 (RuleDiscrete)");
197             RuleDiscreteDomain domain7 = new RuleDiscreteDomain();
198             SortedMap<Object, Object> rules = new TreeMap<Object, Object>();
199             rules.put("ruleset1", Arrays.asList(ruleTacPoids, ruleTailleMin));
200             rules.put("ruleset2", Arrays.asList(ruleTailleMin));
201             domain7.setValues(rules);
202             factorRuleDiscrete.setDomain(domain7);
203             factorRuleDiscrete.setPath("parameters.rules");
204             
205             FactorGroup group1 = new FactorGroup("discretegrp");
206             group1.addFactor(factorDiscrete);
207             group1.addFactor(factorRuleDiscrete);
208             FactorGroup group2 = new FactorGroup("continuousgrp", true);
209             group2.addFactor(factorContinuous);
210             group2.addFactor(factorMatrixContinuous);
211             group2.addFactor(factorEquationContinuous);
212             
213             designPlan.addFactor(group1);
214             designPlan.addFactor(group2);
215             designPlan.addFactor(factorContinuous2);
216             
217             // factor 8 : discrete rule params factor
218             Factor factorRuleParamsDiscrete = new Factor("factor 8 (RuleParamsDiscrete)");
219             DiscreteDomain domain8 = new DiscreteDomain();
220             SortedMap<Object, Object> values8 = new TreeMap<Object, Object>();
221             TopiaContext context = regionContext.beginTransaction();
222             values8.put("1", IsisFishDAOHelper.getStrategyDAO(context).findByName("PelProfil1"));
223             values8.put("2", IsisFishDAOHelper.getStrategyDAO(context).findByName("PelProfil2"));
224             context.closeContext();
225             domain8.setValues(values8);
226             factorRuleParamsDiscrete.setDomain(domain8);
227             designPlan.addFactor(factorRuleParamsDiscrete);
228         }
229         else {
230             // 3.3.0.0
231             designPlan.addFactor(factorContinuous);
232             designPlan.addFactor(factorDiscrete);
233             designPlan.addFactor(factorContinuous2);
234             designPlan.addFactor(factorMatrixContinuous);
235             designPlan.addFactor(factorEquationContinuous);
236             designPlan.addFactor(factorContinuousPercentage);
237         }
238 
239         return designPlan;
240     }
241 
242     /**
243      * Test method {@link MexicoHelper#getDesignPlanAsXML(DesignPlan, String...)}.
244      * 
245      * Sans groupe de facteur, tel qu'il était en 3.3.0.0.
246      * 
247      * @throws IOException
248      * @throws IsisFishException 
249      * @throws TopiaException 
250      */
251     @Test
252     public void testGetDesignPlanAsXML() throws IOException, IsisFishException, TopiaException {
253 
254         DesignPlan testDesignPlan = getTestDesignPlan(false, false);
255 
256         // test xml export
257         String xml = MexicoHelper.getDesignPlanAsXML(testDesignPlan);
258 
259         if (log.isDebugEnabled()) {
260             log.debug("testGetDesignPlanAsXML xml = " + xml);
261         }
262 
263         // factor 1
264         Assert.assertTrue(xml.contains("distributionParameter name=\"min\" type=\"decimal\">0.0"));
265         Assert.assertTrue(xml.contains("distributionParameter name=\"max\" type=\"decimal\">50.0"));
266         // factor 2
267         Assert.assertTrue(xml.contains("name=\"factor 2 (double discrete)\""));
268         Assert.assertTrue(xml.contains("<level>70.9</level>"));
269         // factor 3
270         Assert.assertTrue(xml.contains("<level>14</level>"));
271         // factor 4
272         Assert.assertTrue(xml.contains("<d>-14.0</d>"));
273         Assert.assertTrue(xml.contains("0.799"));
274         Assert.assertTrue(xml.contains("<mx name=\"test1\">"));
275         // factor 5
276         Assert.assertTrue(xml.contains("name=\"equationVariableName\">Ktemp"));
277         Assert.assertTrue(xml.contains("distributionParameter name=\"coefficient\" type=\"decimal\">0.1"));
278         Assert.assertTrue(xml.contains("distributionParameter name=\"reference\" type=\"decimal\">45.0"));
279     }
280 
281     /**
282      * Test method {@link MexicoHelper#getDesignPlanFromXML(File, TopiaContext)}.
283      * 
284      * Sans groupe de facteur.
285      * 
286      * @throws IOException
287      */
288     @Test
289     public void testGetDesignPlanFromXML() throws IOException {
290 
291         File testFile = new File("src/test/resources/mexico/mexicohelper_designplan.xml");
292         testFile = testFile.getAbsoluteFile(); // for maven
293 
294         // topia context can be null in tests
295         DesignPlan plan = MexicoHelper.getDesignPlanFromXML(testFile, null);
296         List<Factor> factors = plan.getFactors();
297 
298         Assert.assertEquals(6, factors.size());
299 
300         String xml = MexicoHelper.getDesignPlanAsXML(plan);
301         if (log.isDebugEnabled()) {
302             log.debug("testGetDesignPlanFromXML xml = " + xml);
303         }
304 
305         // assert on continuous factor (percentage, non percentage) (3.4.0.0)
306         ContinuousDomain domain1 = (ContinuousDomain)factors.get(0).getDomain();
307         Assert.assertNull(domain1.getDistributionParameters().get("coefficient"));
308         ContinuousDomain domain6 = (ContinuousDomain)factors.get(5).getDomain();
309         Assert.assertNotNull(domain6.getDistributionParameters().get("coefficient"));
310     }
311 
312     /**
313      * Test que l'export XML de l'import XML produise le meme xml.
314      * 
315      * @throws IOException
316      * @throws IsisFishException 
317      * @throws TopiaException 
318      */
319     @Test
320     public void testExportImport() throws IOException, IsisFishException, TopiaException {
321 
322         Date date = new Date();
323 
324         // first export
325         DesignPlan testDesignPlan = getTestDesignPlan(false, false);
326         String xml1 = MexicoHelper.getDesignPlanAsXML(testDesignPlan, "date", date.toString());
327         if (log.isDebugEnabled()) {
328             log.debug("xml 1 = " + xml1);
329         }
330 
331         // export
332         File testFile = new File("src/test/resources/mexico/mexicohelper_designplan.xml");
333         testFile = testFile.getAbsoluteFile(); // for maven
334 
335         // topia context can be null in tests
336         DesignPlan plan = MexicoHelper.getDesignPlanFromXML(testFile, regionContext);
337         String xml2 = MexicoHelper.getDesignPlanAsXML(plan, "date", date.toString());
338         if (log.isDebugEnabled()) {
339             log.debug("xml 2 = " + xml2);
340         }
341         Assert.assertEquals(xml1, xml2);
342     }
343 
344     /**
345      * Test le chargement des groupes de facteurs et des facteurs de type
346      * regles depuis isis 4.0.0.0.
347      * 
348      * @throws IOException
349      * @throws IsisFishException 
350      * @throws TopiaException 
351      */
352     @Test
353     public void testFactorGroupAndRule() throws IOException, IsisFishException, TopiaException {
354         
355         DesignPlan testDesignPlan = getTestDesignPlan(true, false);
356 
357         // test xml export
358         String xml = MexicoHelper.getDesignPlanAsXML(testDesignPlan);
359 
360         if (log.isDebugEnabled()) {
361             log.debug("testFactorGroupAndRule xml = " + xml);
362         }
363 
364         // factor groups
365         Assert.assertTrue(xml.indexOf("<feature name=\"group\">continuousgrp") != -1);
366         Assert.assertTrue(xml.indexOf("<feature name=\"grouptype\">continuous") != -1);
367         Assert.assertTrue(xml.indexOf("<feature name=\"group\">discretegrp") != -1);
368         Assert.assertTrue(xml.indexOf("<feature name=\"group\">discrete") != -1);
369 
370         // factor 6
371         Assert.assertTrue(xml.indexOf("<rule name=\"TACpoids\">") != -1);
372         Assert.assertTrue(xml.indexOf("<rule name=\"TailleMin\">") != -1);
373         Assert.assertTrue(xml.indexOf("<param key=\"rule.0.parameter.propSurvie\">42.0</param>") != -1);
374         Assert.assertTrue(xml.indexOf("<param key=\"rule.0.parameter.species\">fr.ifremer.isisfish.entities.Species#1169028645830#0.022262364425031378:EngEnc</param>") != -1);
375     }
376     
377     /**
378      * Test que l'export XML de l'import XML produise le meme xml.
379      * 
380      * @throws IOException
381      * @throws IsisFishException 
382      * @throws TopiaException 
383      */
384     @Test
385     public void testExportImportWithGroup() throws IOException, IsisFishException, TopiaException {
386 
387         Date date = new Date();
388 
389         // first export
390         DesignPlan testDesignPlan = getTestDesignPlan(true, false);
391         String xml1 = MexicoHelper.getDesignPlanAsXML(testDesignPlan, "date", date.toString());
392         if (log.isDebugEnabled()) {
393             log.debug("xml 1 = " + xml1);
394         }
395         
396         // export
397         File testFile = new File("src/test/resources/mexico/mexicohelper_factorgroupdp.xml");
398         testFile = testFile.getAbsoluteFile(); // for maven
399 
400         // topia context can be null in tests
401         TopiaContext context = regionContext.beginTransaction();
402         DesignPlan plan = MexicoHelper.getDesignPlanFromXML(testFile, context);
403         context.closeContext();
404         String xml2 = MexicoHelper.getDesignPlanAsXML(plan, "date", date.toString());
405         if (log.isDebugEnabled()) {
406             log.debug("xml 2 = " + xml2);
407         }
408         Assert.assertEquals(xml1, xml2);
409     }
410 
411     /**
412      * Test le chargement des groupes de facteurs et des facteurs de type
413      * entite (ou autre) sur les parametres de regles.
414      * 
415      * @throws IOException
416      * @throws IsisFishException 
417      * @throws TopiaException 
418      */
419     @Test
420     public void testFactorRuleParams() throws IOException, IsisFishException, TopiaException {
421         
422         DesignPlan testDesignPlan = getTestDesignPlan(true, false);
423 
424         // test xml export
425         String xml = MexicoHelper.getDesignPlanAsXML(testDesignPlan);
426 
427         if (log.isDebugEnabled()) {
428             log.debug("testFactorRuleParams xml = " + xml);
429         }
430 
431         // factor groups
432         Assert.assertTrue(xml.indexOf("type=\"string\"") != -1);
433         
434         // factor 8
435         Assert.assertTrue(xml.indexOf("<level>Strategy:PelProfil1</level>") != -1);
436         Assert.assertTrue(xml.indexOf("<level>Strategy:PelProfil2</level>") != -1);
437     }
438 
439     /**
440      * Test l'ajout des nouveaux type de facteurs, min/max et pourcentage.
441      * 
442      * @throws IOException
443      * @throws IsisFishException
444      * @throws TopiaException
445      * 
446      * This test may fail because of date delay by one seconds due to slow testing
447      */
448     //@Test
449     public void testPercentageMinMaxFactor() throws IOException, IsisFishException, TopiaException {
450 
451         // get test plan and add some STRANGES factors
452         DesignPlan testDesignPlan = getTestDesignPlan(false, false);
453         
454         // matrix 1
455         MatrixND matrix1 = MatrixFactory.getInstance().create("test1",
456                 new int[] { 3, 2 }, new String[] { "col1", "col2" });
457         matrix1 = MatrixHelper.convertToId(matrix1);
458         
459         // factor x1
460         Factor factorContinuous = new Factor("factor x1");
461         ContinuousDomain domain1 = new ContinuousDomain(Distribution.QUNIFPC);
462         domain1.addDistributionParam("reference", 42.0);
463         domain1.addDistributionParam("coefficient", 0.05);
464         factorContinuous.setDomain(domain1);
465         factorContinuous.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521013#0.1715620681984218#maxLength");
466 
467         // factor x4
468         Factor factorMatrixContinuous = new Factor("factor x4");
469         ContinuousDomain domain4 = new ContinuousDomain(Distribution.QUNIFMM);
470         domain4.addDistributionParam("min", matrix1);
471         domain4.addDistributionParam("max", matrix1);
472         factorMatrixContinuous.setDomain(domain4);
473         factorMatrixContinuous.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521076#0.6526656643346673#minLength");
474 
475         // factor x5
476         Factor factorEquationContinuous = new Factor("factor x5");
477         ContinuousDomain domain5 = new ContinuousDomain(Distribution.QUNIFMM);
478         domain5.addDistributionParam("min", 40.0);
479         domain5.addDistributionParam("max", 50.0);
480         factorEquationContinuous.setEquationVariableName("Lx1");
481         factorEquationContinuous.setDomain(domain5);
482         factorEquationContinuous.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521076#0.6526656643346673#maxLength");
483         
484         testDesignPlan.addFactor(factorContinuous);
485         testDesignPlan.addFactor(factorMatrixContinuous);
486         testDesignPlan.addFactor(factorEquationContinuous);
487         
488         // test write
489         String content = MexicoHelper.getDesignPlanAsXML(testDesignPlan);
490         File tempFile = File.createTempFile("testdesignplan", ".xml");
491         tempFile.deleteOnExit();
492         FileUtils.writeStringToFile(tempFile, content);
493 
494         // test to read it and get content
495         DesignPlan plan = MexicoHelper.getDesignPlanFromXML(tempFile, null);
496         String reReadContent = MexicoHelper.getDesignPlanAsXML(plan);
497 
498         Assert.assertEquals(content, reReadContent);
499     }
500     
501     /**
502      * Test l'ajout des nouveaux type de facteurs equation discrete.
503      * 
504      * @throws IOException
505      * @throws IsisFishException
506      * @throws TopiaException 
507      */
508     @Test
509     public void testDiscreteEquationFactor() throws IOException, IsisFishException, TopiaException {
510 
511         // get test plan and add some STRANGES factors
512         DesignPlan testDesignPlan = getTestDesignPlan(false, false);
513 
514         // factor x6
515         Factor factorEquationDiscrete = new Factor("factor x6");
516         EquationDiscreteDomain domain6 = new EquationDiscreteDomain();
517         domain6.getValues().put("1", "if ( context.getValue() < 1.0) {\n\treturn 1.0;\n} else {\treturn context.getValue();\n}");
518         domain6.getValues().put("2", "if ( context.getValue() < 1.666) {\n\treturn 1.666;\n} else {\treturn context.getValue();\n}");
519         factorEquationDiscrete.setDomain(domain6);
520         factorEquationDiscrete.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521076#0.6526656643346673#maxLength");
521 
522         testDesignPlan.addFactor(factorEquationDiscrete);
523 
524         Date date = new Date();
525         // test write
526         String content = MexicoHelper.getDesignPlanAsXML(testDesignPlan, "date", date.toString());
527         Assert.assertTrue(content.contains("context.getValue() &lt; 1.0"));
528 
529         File tempFile = File.createTempFile("testdesignplan", ".xml");
530         tempFile.deleteOnExit();
531         FileUtils.writeStringToFile(tempFile, content);
532 
533         // test to read it and get content
534         DesignPlan plan = MexicoHelper.getDesignPlanFromXML(tempFile, null);
535         String reReadContent = MexicoHelper.getDesignPlanAsXML(plan, "date", date.toString());
536         
537         Assert.assertEquals(content, reReadContent);
538     }
539     
540     /**
541      * Test que l'export XML de l'import XML produise le meme xml.
542      * Pareil, mais pour une version v2 du schema xml.
543      * 
544      * @throws IOException
545      * @throws IsisFishException 
546      * @throws TopiaException 
547      */
548     @Test
549     public void testExportImportV2() throws IOException, IsisFishException, TopiaException {
550 
551         Date date = new Date();
552 
553         // first export
554         DesignPlan testDesignPlan = getTestDesignPlan(false, false);
555         String xml1 = MexicoHelper.getDesignPlanAsXML(testDesignPlan, "date", date.toString());
556         if (log.isDebugEnabled()) {
557             log.debug("xml 1 = " + xml1);
558         }
559 
560         // export
561         File testFile = new File("src/test/resources/mexico/mexicohelper_designplanV2.xml");
562         testFile = testFile.getAbsoluteFile(); // for maven
563 
564         // topia context can be null in tests
565         DesignPlan plan = MexicoHelper.getDesignPlanFromXML(testFile, regionContext);
566         String xml2 = MexicoHelper.getDesignPlanAsXML(plan, "date", date.toString());
567         if (log.isDebugEnabled()) {
568             log.debug("xml 2 = " + xml2);
569         }
570         Assert.assertEquals(xml1, xml2);
571     }
572 
573     /**
574      * Test la representation string d'un object dans le format mexico.
575      */
576     @Test
577     public void testGetStringFromObject() {
578         Population pop = new PopulationImpl();
579         pop.setName("tutu");
580         Assert.assertEquals("Population:tutu", MexicoHelper.getStringFromObject(pop));
581 
582         PopulationSeasonInfo psi = new PopulationSeasonInfoImpl();
583         Assert.assertNull(MexicoHelper.getStringFromObject(psi));
584         
585         TimeStep step = new TimeStep(42);
586         Assert.assertEquals("TimeStep:42", MexicoHelper.getStringFromObject(step));
587     }
588 
589     /**
590      * Test que lors de la lecture du design plan, les types representés en string
591      * seront correctement convertit en objets.
592      * @throws TopiaException 
593      */
594     @Test
595     public void testGetObjectFormString() throws TopiaException {
596         TopiaContext context = regionContext.beginTransaction();
597         
598         Zone zone = (Zone)MexicoHelper.getObjectFromString("Zone:port", context);
599         Assert.assertEquals("port", zone.getName());
600         
601         Strategy strat = (Strategy)MexicoHelper.getObjectFromString("Strategy:PelProfil1", context);
602         Assert.assertEquals("PelProfil1", strat.getName());
603         
604         TimeStep step = (TimeStep)MexicoHelper.getObjectFromString("TimeStep:42", context);
605         Assert.assertEquals(42, step.getStep());
606         
607         context.closeContext();
608     }
609 
610     /**
611      * Test que le xml généré est valide avec la xsd mexico (exp design).
612      * 
613      * Test is currently disabled, isis fish doesn't respect all mexico
614      * file format since it's not possible.
615      * 
616      * @throws Exception 
617      */
618     public void validOutputXml() throws Exception {
619 
620         DesignPlan testDesignPlan = getTestDesignPlan(true, true);
621         String xml1 = MexicoHelper.getDesignPlanAsXML(testDesignPlan);
622         URL schemaUrl = MexicoHelperTest.class.getResource("/mexico/expDesign.xsd");
623 
624         SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
625         Schema schema =  factory.newSchema(schemaUrl);
626         Validator validator = schema.newValidator();
627         // at last perform validation:
628         validator.validate(new StreamSource(new StringReader(xml1)));
629     }
630 
631     /**
632      * Test que l'export XML de l'import XML produise le meme xml.
633      * Pareil, mais pour une version v3 du schema xml.
634      * 
635      * @throws IOException
636      * @throws IsisFishException 
637      * @throws TopiaException 
638      */
639     @Test
640     public void testExportImportV3() throws IOException, IsisFishException, TopiaException {
641 
642         TopiaContext context = regionContext.beginTransaction();
643         Date date = new Date();
644 
645         // first export
646         DesignPlan testDesignPlan = getTestDesignPlan(true, true);
647         String xml1 = MexicoHelper.getDesignPlanAsXML(testDesignPlan, "date", date.toString());
648         if (log.isDebugEnabled()) {
649             log.debug("xml 1 = " + xml1);
650         }
651 
652         // export
653         File testFile = new File("src/test/resources/mexico/mexicohelper_designplanV3.xml");
654         testFile = testFile.getAbsoluteFile(); // for maven
655 
656         // topia context can be null in tests
657         DesignPlan plan = MexicoHelper.getDesignPlanFromXML(testFile, context);
658         String xml2 = MexicoHelper.getDesignPlanAsXML(plan, "date", date.toString());
659         if (log.isDebugEnabled()) {
660             log.debug("xml 2 = " + xml2);
661         }
662         Assert.assertEquals(xml1, xml2);
663         
664         context.closeContext();
665     }
666 }