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.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
54
55
56
57
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
76
77
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
102
103
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
128
129
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
156
157
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 }