View Javadoc
1   /*
2    * #%L
3    * IsisFish
4    * 
5    * $Id: RegionExplorerTest.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/mexico/export/RegionExplorerTest.java $
7    * %%
8    * Copyright (C) 2006 - 2012 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.export;
27  
28  import java.io.File;
29  import java.io.IOException;
30  
31  import org.apache.commons.io.FileUtils;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.junit.Assert;
35  import org.junit.Test;
36  import org.nuiton.topia.TopiaContext;
37  import org.nuiton.topia.TopiaException;
38  
39  import fr.ifremer.isisfish.AbstractIsisFishTest;
40  import fr.ifremer.isisfish.datastore.RegionStorage;
41  import fr.ifremer.isisfish.datastore.StorageException;
42  import fr.ifremer.isisfish.entities.FisheryRegion;
43  import fr.ifremer.isisfish.simulator.sensitivity.SensitivityUtils;
44  
45  /**
46   * Test class for {@link RegionExplorer}.
47   * 
48   * @author chatellier
49   * @version $Revision: 4156 $
50   * 
51   * Last update : $Date: 2014-12-09 12:27:18 +0100 (Tue, 09 Dec 2014) $
52   * By : $Author: echatellier $
53   */
54  public class RegionExplorerTest extends AbstractIsisFishTest {
55  
56      /** Class logger. */
57      private static Log log = LogFactory.getLog(RegionExplorerTest.class);
58  
59      /**
60       * Explore all region values and perform export implementation as xml.
61       * 
62       * @throws TopiaException 
63       * @throws StorageException 
64       * @throws IOException 
65       */
66      @Test
67      public void testExportRegionFactorsAsXml() throws TopiaException, StorageException, IOException {
68  
69          // get region to export
70          RegionStorage regionStorage = RegionStorage.getRegion("BaseMotosICA");
71          TopiaContext context = regionStorage.getStorage().beginTransaction();
72          FisheryRegion fisheryRegion = RegionStorage.getFisheryRegion(context);
73  
74          // export implementation
75          File file = File.createTempFile("xmlexport-", ".xml");
76          file.deleteOnExit();
77          RegionExportFactorXML xmlFactorExport = new RegionExportFactorXML(file);
78  
79          // explore region (export as xml)
80          RegionExplorer explorer = new RegionExplorer();
81          explorer.explore(fisheryRegion, xmlFactorExport);
82          String xmlExport = FileUtils.readFileToString(file);
83  
84          if (log.isDebugEnabled()) {
85              log.debug("Export xml is : \n" + xmlExport);
86          }
87          
88          // ========== here some tests ==========
89          // all factors must have a non null name
90          Assert.assertTrue("A factor has a null name", xmlExport.indexOf("name=\"null\"") == -1);
91          // TODO this assert is correct, for <factor name="">
92          // but fail on <mx name="">
93          //Assert.assertTrue("A factor has no name", xmlExport.indexOf("name=\"\"") == -1);
94          
95          // must be present (for BaseMotosICA base)
96          // equation is a special case (entity too)
97          Assert.assertTrue("An equation factor is missing", xmlExport.indexOf("Population.Anchois_long.growth") != -1);
98  
99          // close all
100         context.closeContext();
101     }
102     
103     /**
104      * Test que toutes les entités et propriétés définies dans le fichier de
105      * sensibilité sont présentes dans le contenu xml.
106      * 
107      * @throws TopiaException 
108      * @throws StorageException 
109      * @throws IOException 
110      */
111     @Test
112     public void testAllEntityPresence() throws TopiaException, StorageException, IOException {
113         // get region to export
114         RegionStorage regionStorage = RegionStorage.getRegion("Golfe de Gascogne");
115         TopiaContext context = regionStorage.getStorage().beginTransaction();
116         FisheryRegion fisheryRegion = RegionStorage.getFisheryRegion(context);
117 
118         // export implementation
119         File file = File.createTempFile("xmlexport-", ".xml");
120         file.deleteOnExit();
121         RegionExportFactorXML xmlFactorExport = new RegionExportFactorXML(file);
122 
123         // explore region (export as xml)
124         RegionExplorer explorer = new RegionExplorer();
125         explorer.explore(fisheryRegion, xmlFactorExport);
126         String xmlExport = FileUtils.readFileToString(file);
127 
128         // test that all sensitivity properties appear in xml content
129         // entity part and property part must appear in factors names
130         for (String property : SensitivityUtils.getProperties().stringPropertyNames()) {
131             String entityPart = property.substring(0, property.indexOf('.'));
132             
133             if ("Variable".equals(entityPart)) {
134                 // variable added since 4.1 and not yet present in test database
135                 continue;
136             }
137 
138             Assert.assertTrue("Entity " + entityPart + " is not present in xml export", xmlExport.indexOf(entityPart) != -1);
139             
140             String propertyPart = property.substring(property.indexOf('.'));
141             Assert.assertTrue("Property " + property + " is not present in xml export", xmlExport.indexOf(propertyPart) != -1);
142         }
143 
144         // close all
145         context.closeContext();
146     }
147 }