View Javadoc
1   /*
2    * #%L
3    * IsisFish
4    * 
5    * $Id: ScriptStorageTest.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/ScriptStorageTest.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.StringWriter;
29  import java.io.Writer;
30  import java.util.Date;
31  import java.util.HashMap;
32  import java.util.Map;
33  
34  import org.apache.commons.io.FileUtils;
35  import org.junit.Assert;
36  import org.junit.Before;
37  import org.junit.Test;
38  
39  import fr.ifremer.isisfish.AbstractIsisFishTest;
40  import fr.ifremer.isisfish.IsisFish;
41  import fr.ifremer.isisfish.datastore.CodeSourceStorage.Location;
42  import freemarker.template.Configuration;
43  import freemarker.template.Template;
44  
45  /**
46   * ScriptStorageTest.
47   *
48   * Created: 7 août 2006 11:07:57
49   *
50   * @author poussin
51   * @version $Revision: 4291 $
52   *
53   * Last update: $Date: 2015-06-24 10:04:58 +0200 (Wed, 24 Jun 2015) $
54   * by : $Author: echatellier $
55   */
56  public class ScriptStorageTest extends AbstractIsisFishTest {
57  
58      protected Configuration freemarkerConfiguration;
59  
60      @Before
61      public void setUp() throws Exception {
62          freemarkerConfiguration = getFreemarkerConfiguration();
63      }
64  
65      /*
66       * Test method for 'fr.ifremer.isisfish.datastore.ScriptStorage.getScript(String)'
67       */
68      @Test
69      public void testGetScript() throws Exception {
70          String content1 = "public class test { public static void main(String[]args) {System.out.println(\"The test 1 :)\")} ";
71          String content2 = "public class test { public static void main(String[]args) {System.out.println(\"The test 2 :) with different length :(\")} ";
72  
73          ScriptStorage script = ScriptStorage.createScript("test", Location.COMMUNITY);
74          try {
75              //System.out.println("File: " + script.getFile());
76              //System.out.println("Content: '" + script.getContent() + "'");
77  
78              Assert.assertFalse(script.exists());
79              Assert.assertEquals("", script.getContent());
80  
81              script.setContent(content1);
82              Assert.assertTrue(script.exists());
83  
84              //System.out.println("Content: '" + script.getContent() + "'");
85              Assert.assertEquals(content1, script.getContent());
86  
87              FileUtils.writeStringToFile(script.getFile(), content2);
88              //System.out.println("Content: '" + script.getContent() + "'");
89              Assert.assertEquals(content2, script.getContent());
90          } finally {
91              script.delete(false);
92              Assert.assertFalse(script.exists());
93          }
94      }
95  
96      @Test
97      public void testNewScriptWithCompilation() throws Exception {
98  
99          String fileName = "TestScript1";
100 
101         ScriptStorage scriptStorage = ScriptStorage.createScript(fileName, Location.COMMUNITY);
102 
103         // get template
104         Template template = freemarkerConfiguration
105                 .getTemplate(ScriptStorage.SCRIPT_TEMPLATE);
106 
107         // context values
108         Map<String, Object> root = new HashMap<>();
109         root.put("name", fileName);
110         root.put("date", new Date());
111         root.put("author", IsisFish.config.getUserName());
112         root.put("email", IsisFish.config.getUserMail());
113 
114         // process template
115         Writer out = new StringWriter();
116         template.process(root, out);
117         out.flush();
118         String content = out.toString();
119 
120         scriptStorage.setContent(content);
121 
122         // 0 = compile success
123         int compileResult = scriptStorage.compile(false, null);
124         Assert.assertEquals(0, compileResult);
125     }
126 }