View Javadoc
1   /*
2    * #%L
3    * IsisFish
4    * 
5    * $Id: SimulationInformationTest.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/datastore/SimulationInformationTest.java $
7    * %%
8    * Copyright (C) 2009 - 2010 Ifremer, CodeLutin
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.datastore;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.net.URL;
31  import java.text.DateFormat;
32  import java.text.ParseException;
33  import java.text.SimpleDateFormat;
34  import java.util.Date;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import org.junit.Assert;
39  import org.junit.Test;
40  
41  /**
42   * Test for {@link SimulationInformation} class.
43   * 
44   * No need for isolated directory (system directory not used).
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 SimulationInformationTest {
53  
54      /** Log. */
55      protected static Log log = LogFactory.getLog(SimulationInformationTest.class);
56      
57      /**
58       * Test simulation information with no data.
59       * 
60       * @throws IOException 
61       */
62      @Test
63      public void testSimulationEmpty() throws IOException {
64  
65          File file = File.createTempFile("information-empty", ".txt");
66          file.deleteOnExit();
67  
68          SimulationInformation info = new SimulationInformation(file);
69          Assert.assertEquals(new Date(0), info.getSimulationStart());
70          Assert.assertEquals(new Date(0), info.getSimulationEnd());
71          Assert.assertFalse(info.hasError());
72          Assert.assertNull(info.getException());
73          Assert.assertEquals(0, info.getExportTime("test"));
74          Assert.assertNotNull(info.toString());
75      }
76  
77      /**
78       * Test que le format du fichier de simulation ne change pas.
79       * 
80       * @throws ParseException 
81       */
82      @Test
83      public void testExistingSimulationFile() throws ParseException {
84          URL informationURL = SimulationInformationTest.class.getResource("information");
85          File file = new File(informationURL.getPath());
86          
87          Assert.assertTrue(file.exists());
88          
89          // Do not use same format as {@link SimulationInformation#dateFormat}
90          DateFormat df = new SimpleDateFormat("yyyy/dd/MM hh:mm:ss");
91          
92          SimulationInformation info = new SimulationInformation(file);
93          Assert.assertEquals(df.parse("2009/31/08 14:44:57"), info.getSimulationStart());
94          Assert.assertEquals(df.parse("2009/31/08 14:48:00"), info.getSimulationEnd());
95          Assert.assertEquals(1482, info.getExportTime("VesselMargin.java"));
96          Assert.assertFalse(info.hasError());
97          Assert.assertTrue(info.getOptimizationUsage().indexOf("Cache used") > 0);
98          Assert.assertNull(info.getStatistic());
99          Assert.assertTrue(info.toString().indexOf("VesselMargin.java") > 0);
100     }
101     
102     /**
103      * Test que le format du fichier de simulation ne change pas.
104      * 
105      * Cas d'un fichier avec une erreur.
106      */
107     @Test
108     public void testExistingSimulationFileException() {
109         URL informationURL = SimulationInformationTest.class.getResource("exception.information");
110         File file = new File(informationURL.getPath());
111         
112         Assert.assertTrue(file.exists());
113         
114         SimulationInformation info = new SimulationInformation(file);
115         Assert.assertTrue(info.hasError());
116         Assert.assertTrue(info.getException().indexOf("TopiaException") > 0);
117         Assert.assertTrue(info.toString().indexOf("TopiaException") > 0);
118     }
119 
120     /**
121      * Test simulation date storage.
122      * 
123      * @throws IOException 
124      * @throws ParseException 
125      */
126     @Test
127     public void testSimulationDate() throws IOException, ParseException {
128 
129         // Do not use same format as {@link SimulationInformation#dateFormat}
130         DateFormat df = new SimpleDateFormat("hh:mm:ss a");
131 
132         File file = File.createTempFile("information-date", ".txt");
133         file.deleteOnExit();
134 
135         SimulationInformation info = new SimulationInformation(file);
136 
137         Date d1 = df.parse("3:30:32 pm");
138         Date d2 = df.parse("3:50:47 pm");
139         info.setSimulationStart(d1);
140         info.setSimulationEnd(d2);
141 
142         info.store();
143 
144         SimulationInformation testInfo = new SimulationInformation(file);
145         Assert.assertEquals(d1, testInfo.getSimulationStart());
146         Assert.assertEquals(d2, testInfo.getSimulationEnd());
147         Assert.assertTrue(testInfo.toString().indexOf("15:50:47") > 0);
148     }
149     
150     /**
151      * Test simulation exception storage.
152      * 
153      * @throws IOException 
154      */
155     @Test
156     public void testSimulationException() throws IOException {
157         File file = File.createTempFile("information-exception", ".txt");
158         file.deleteOnExit();
159 
160         SimulationInformation info = new SimulationInformation(file);
161         Assert.assertFalse(info.hasError());
162         
163         Exception e = new Exception("Oula la, ya eu une exception super grave");
164         info.setException(e);
165         info.store();
166 
167         SimulationInformation testInfo = new SimulationInformation(file);
168         Assert.assertTrue(testInfo.hasError());
169         Assert.assertTrue(testInfo.getException().indexOf("grave") > 0);
170         Assert.assertTrue(testInfo.toString().indexOf("grave") > 0);
171 
172     }
173     
174     /**
175      * Test simulation export time.
176      * @throws IOException 
177      */
178     @Test
179     public void testSimulationExportTime() throws IOException {
180         File file = File.createTempFile("information-export", ".txt");
181         file.deleteOnExit();
182 
183         SimulationInformation info = new SimulationInformation(file);
184         info.addExportTime("export1", 30);
185         info.addExportTime("export2", 40);
186         info.addExportTime("export3", 50);
187         info.addExportTime("export4", 60);
188         
189         info.store();
190 
191         SimulationInformation testInfo = new SimulationInformation(file);
192         Assert.assertFalse(testInfo.hasError());
193         Assert.assertEquals(30, testInfo.getExportTime("export1"));
194         Assert.assertEquals(40, testInfo.getExportTime("export2"));
195         Assert.assertEquals(50, testInfo.getExportTime("export3"));
196         Assert.assertEquals(60, testInfo.getExportTime("export4"));
197         Assert.assertTrue(testInfo.toString().indexOf("export1") > 0);
198     }
199     
200     /**
201      * Test simulation rule time.
202      * 
203      * @throws IOException 
204      */
205     @Test
206     public void testSimulationRuleTime() throws IOException {
207         File file = File.createTempFile("information-rule", ".txt");
208         file.deleteOnExit();
209 
210         SimulationInformation info = new SimulationInformation(file);
211         info.addRuleInitTime("rule1", 30);
212         info.addRuleInitTime("rule2", 8);
213         info.addRuleInitTime("rule3", 321);
214         info.addRuleInitTime("rule4", 123);
215         
216         // time must be added
217         info.addRulePreTime("rule1", 20);
218         info.addRulePostTime("rule1", 25);
219         info.addRulePreTime("rule1", 20);
220         info.addRulePostTime("rule1", 25);
221         
222         info.addRulePreTime("rule2", 18);
223         info.addRulePostTime("rule2", 500);
224         
225         info.addRulePreTime("rule3", 20);
226         // no additional time for rule 4
227         
228         info.store();
229 
230         SimulationInformation testInfo = new SimulationInformation(file);
231         Assert.assertFalse(testInfo.hasError());
232         Assert.assertEquals(30, testInfo.getRuleInitTime("rule1"));
233         Assert.assertEquals(8, testInfo.getRuleInitTime("rule2"));
234         Assert.assertEquals(321, testInfo.getRuleInitTime("rule3"));
235         Assert.assertEquals(123, testInfo.getRuleInitTime("rule4"));
236         
237         Assert.assertEquals(20 + 20, testInfo.getRulePreTime("rule1"));
238         Assert.assertEquals(25 + 25, testInfo.getRulePostTime("rule1"));
239         
240         Assert.assertEquals(18, testInfo.getRulePreTime("rule2"));
241         Assert.assertEquals(500, testInfo.getRulePostTime("rule2"));
242         
243         Assert.assertEquals(20, testInfo.getRulePreTime("rule3"));
244         Assert.assertEquals(0, testInfo.getRulePostTime("rule3"));
245         
246         Assert.assertEquals(0, testInfo.getRulePreTime("rule4"));
247         Assert.assertEquals(0, testInfo.getRulePostTime("rule4"));
248 
249         // total time present rule2
250         Assert.assertTrue(testInfo.toString().indexOf("rule2 : 0.526") > 0);
251     }
252 }