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.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
51
52
53
54
55
56
57
58
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
80 Template template = freemarkerConfiguration
81 .getTemplate(SimulatorStorage.SIMULATOR_TEMPLATE);
82
83
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
91 Writer out = new StringWriter();
92 template.process(root, out);
93 out.flush();
94 simulatorStorage.setContent(out.toString());
95
96
97 int compileResult = simulatorStorage.compile(false, null);
98 Assert.assertEquals(0, compileResult);
99 }
100
101
102
103
104
105
106
107 @Test
108 public void testSimulatorStorage() throws IsisFishException {
109
110
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 }