View Javadoc
1   /*
2    * #%L
3    * IsisFish
4    * 
5    * $Id: SimulatorStorageTest.java 4291 2015-06-24 08:04:58Z echatellier $
6    * $HeadURL: https://svn.codelutin.com/isis-fish/tags/isis-fish-4.4.0.2/src/test/java/fr/ifremer/isisfish/datastore/SimulatorStorageTest.java $
7    * %%
8    * Copyright (C) 2009 - 2012 Ifremer, CodeLutin, 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.datastore;
27  
28  import java.io.File;
29  import java.io.StringWriter;
30  import java.io.Writer;
31  import java.util.Date;
32  import java.util.HashMap;
33  import java.util.Map;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import org.junit.Assert;
38  import org.junit.Before;
39  import org.junit.Test;
40  
41  import fr.ifremer.isisfish.AbstractIsisFishTest;
42  import fr.ifremer.isisfish.IsisFish;
43  import fr.ifremer.isisfish.IsisFishException;
44  import fr.ifremer.isisfish.datastore.CodeSourceStorage.Location;
45  import fr.ifremer.isisfish.simulator.Simulator;
46  import freemarker.template.Configuration;
47  import freemarker.template.Template;
48  
49  /**
50   * SimulatorStorageTest.
51   * 
52   * Created: 7 août 2006 11:07:57
53   *
54   * @author poussin
55   * @version $Revision: 4291 $
56   *
57   * Last update: $Date: 2015-06-24 10:04:58 +0200 (Wed, 24 Jun 2015) $
58   * by : $Author: echatellier $
59   */
60  public class SimulatorStorageTest extends AbstractIsisFishTest {
61  
62      private static final Log log = LogFactory.getLog(SimulatorStorageTest.class);
63      
64      protected Configuration freemarkerConfiguration;
65  
66      @Before
67      public void setUp() throws Exception {
68          freemarkerConfiguration = getFreemarkerConfiguration();
69      }
70  
71      @Test
72      public void testNewSimulatorWithCompilation() throws Exception {
73  
74          String fileName = "TestSimulator1";
75  
76          SimulatorStorage simulatorStorage = SimulatorStorage
77                  .createSimulator(fileName, Location.COMMUNITY);
78  
79          // get template
80          Template template = freemarkerConfiguration
81                  .getTemplate(SimulatorStorage.SIMULATOR_TEMPLATE);
82  
83          // context values
84          Map<String, Object> root = new HashMap<>();
85          root.put("name", fileName);
86          root.put("date", new Date());
87          root.put("author", IsisFish.config.getUserName());
88          root.put("email", IsisFish.config.getUserMail());
89  
90          // process template
91          Writer out = new StringWriter();
92          template.process(root, out);
93          out.flush();
94          simulatorStorage.setContent(out.toString());
95  
96          // 0 = compile success
97          int compileResult = simulatorStorage.compile(false, null);
98          Assert.assertEquals(0, compileResult);
99      }
100 
101     /**
102      * Test compilation on {@link JavaSourceStorage} and instanciation.
103      * 
104      * There was a bug who not compiled if file was never of a non existent file.
105      * @throws IsisFishException 
106      */
107     @Test
108     public void testSimulatorStorage() throws IsisFishException {
109         
110         // Make sur that class file does'nt exists !!!
111         File f = new File(IsisFish.config.getCompileDirectory(),
112                 "simulators" + File.separator + "DefaultSimulator.class");
113         if (f.exists()) {
114             if (log.isDebugEnabled()) {
115                 log.debug("Delete file : " + f);
116             }
117             f.delete();
118         }
119         else {
120             if (log.isDebugEnabled()) {
121                 log.debug("File : " + f + " doesn't exists !");
122             }
123         }
124 
125         SimulatorStorage simulatorStorage = SimulatorStorage.getSimulator("DefaultSimulator");
126         Simulator simulator = simulatorStorage.getNewInstance();
127         Assert.assertNotNull(simulator);
128         Assert.assertTrue(f.exists());
129     }
130 }