View Javadoc
1   /*
2    * #%L
3    * IsisFish
4    * 
5    * $Id: IsisMatrixSemanticMapperTest.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/util/IsisMatrixSemanticMapperTest.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.util;
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.Collections;
34  import java.util.List;
35  
36  import org.junit.After;
37  import org.junit.Assert;
38  import org.junit.Before;
39  import org.junit.Test;
40  import org.nuiton.math.matrix.MatrixFactory;
41  import org.nuiton.math.matrix.MatrixND;
42  import org.nuiton.topia.TopiaContext;
43  import org.nuiton.topia.TopiaException;
44  
45  import fr.ifremer.isisfish.AbstractIsisFishTest;
46  import fr.ifremer.isisfish.IsisFishDAOHelper;
47  import fr.ifremer.isisfish.datastore.SimulationStorage;
48  import fr.ifremer.isisfish.entities.Population;
49  import fr.ifremer.isisfish.simulator.SimulationContext;
50  import fr.ifremer.isisfish.types.TimeStep;
51  
52  /**
53   * Test que le semantic mapper est bien pris en compte lors de l'import
54   * de matrix nd et qu'il converti bien tous les types requis.
55   * 
56   * @author echatellier
57   * @since 4.1.0.3
58   */
59  public class IsisMatrixSemanticMapperTest extends AbstractIsisFishTest {
60  
61      @Before
62      public void setUp() throws IOException, TopiaException {
63          MatrixFactory.setSemanticMapper(new IsisMatrixSemanticMapper());
64          URL zipURL = this.getClass().getResource("/simulations/test-nonregression-20090203.zip");
65          SimulationStorage simRef = SimulationStorage.importAndRenameZip(new File(zipURL.getFile()), "test-" + System.currentTimeMillis());
66          SimulationContext.get().setSimulationStorage(simRef);
67      }
68  
69      @After
70      public void clearThreadLocal() {
71          SimulationContext.remove();
72      }
73  
74      /**
75       * Test la conversion des entités.
76       * @throws IOException 
77       * @throws TopiaException 
78       */
79      @Test
80      public void testImportEntityImportString() throws IOException, TopiaException {
81          String data = "[1, 1, 1]\n" +
82                  "Population:test population\n" +
83                  "Gear:Testengin\n" +
84                  "Zone:Zone test1\n" +
85                  "0;0;0;42.0";
86  
87          TopiaContext tx = SimulationContext.get().getDB();
88          
89          List[] sems = new List[] {
90                  Collections.singletonList(IsisFishDAOHelper.getPopulationDAO(tx).findByName("test population")),
91                  Collections.singletonList(IsisFishDAOHelper.getGearDAO(tx).findByName("Testengin")),
92                  Collections.singletonList(IsisFishDAOHelper.getZoneDAO(tx).findByName("Zone test1")),
93          };
94          MatrixND m = MatrixFactory.getInstance().create(sems);
95          m.importCSV(new StringReader(data), null);
96  
97          Assert.assertEquals(42.0, m.getValue(0, 0, 0), 0.001);
98      }
99  
100     /**
101      * Test la conversion des entités.
102      * @throws IOException 
103      * @throws TopiaException 
104      */
105     @Test
106     public void testImportEntity() throws IOException, TopiaException {
107         String data = "[1, 1, 1]\n" +
108                 "Population:test population\n" +
109                 "Gear:Testengin\n" +
110                 "Zone:Zone test1\n" +
111                 "0;0;0;42.0";
112 
113         TopiaContext tx = SimulationContext.get().getDB();
114         
115         List[] sems = new List[] {
116                 Collections.singletonList(IsisFishDAOHelper.getPopulationDAO(tx).findByName("test population")),
117                 Collections.singletonList(IsisFishDAOHelper.getGearDAO(tx).findByName("Testengin")),
118                 Collections.singletonList(IsisFishDAOHelper.getZoneDAO(tx).findByName("Zone test1")),
119         };
120         MatrixND m = MatrixFactory.getInstance().create(sems);
121         m.importCSV(new StringReader(data), null);
122 
123         Assert.assertEquals(42.0, m.getValue(0, 0, 0), 0.001);
124     }
125     
126     /**
127      * Test la conversion des entités et TimeStep
128      * @throws IOException 
129      * @throws TopiaException 
130      */
131     @Test
132     public void testImportEntityTimeStep() throws IOException, TopiaException {
133         String data = "[1, 1, 2]\n" +
134                 "Population:test population\n" +
135                 "Gear:Testengin\n" +
136                 "TimeStep:1,0\n" +
137                 "0;0;0;1.0\n" +
138                 "0;0;1;2.0";
139 
140         TopiaContext tx = SimulationContext.get().getDB();
141         
142         List[] sems = new List[] {
143                 Collections.singletonList(IsisFishDAOHelper.getPopulationDAO(tx).findByName("test population")),
144                 Collections.singletonList(IsisFishDAOHelper.getGearDAO(tx).findByName("Testengin")),
145                 Arrays.asList(new TimeStep(0), new TimeStep(1)),
146         };
147         MatrixND m = MatrixFactory.getInstance().create(sems);
148         m.importCSV(new StringReader(data), null);
149 
150         Assert.assertEquals(2.0, m.getValue(0, 0, 0), 0.001);
151         Assert.assertEquals(1.0, m.getValue(0, 0, 1), 0.001);
152     }
153     
154     /**
155      * Test la conversion des entités.
156      * @throws IOException 
157      * @throws TopiaException 
158      */
159     @Test
160     public void testImportPopulationGroup() throws IOException, TopiaException {
161         String data = "[1, 3, 1]\n" +
162                 "Population:test population\n" +
163                 "PopulationGroup:test population+2,test population+1,test population+0\n" +
164                 "Zone:Zone test1\n" +
165                 "0;2;0;42.0";
166 
167         TopiaContext tx = SimulationContext.get().getDB();
168 
169         Population pop = IsisFishDAOHelper.getPopulationDAO(tx).findByName("test population");
170         List[] sems = new List[] {
171                 Collections.singletonList(pop),
172                 Arrays.asList(pop.getPopulationGroup().get(0), pop.getPopulationGroup().get(1), pop.getPopulationGroup().get(2)),
173                 Collections.singletonList(IsisFishDAOHelper.getZoneDAO(tx).findByName("Zone test1")),
174         };
175         MatrixND m = MatrixFactory.getInstance().create(sems);
176         m.importCSV(new StringReader(data), null);
177 
178         Assert.assertEquals(42.0, m.getValue(0, 0, 0), 0.001);
179     }
180 }