View Javadoc
1   /*
2    * #%L
3    * IsisFish
4    * 
5    * $Id: AbstractIsisFishTest.java 4213 2015-05-04 15:18:17Z echatellier $
6    * $HeadURL: https://svn.codelutin.com/isis-fish/tags/isis-fish-4.4.0.2/src/test/java/fr/ifremer/isisfish/AbstractIsisFishTest.java $
7    * %%
8    * Copyright (C) 2009 - 2012 Ifremer, Code Lutin, 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;
27  
28  import java.io.File;
29  
30  import org.apache.commons.io.FileUtils;
31  import org.apache.commons.io.filefilter.HiddenFileFilter;
32  import org.junit.AfterClass;
33  import org.junit.Before;
34  import org.junit.BeforeClass;
35  import org.nuiton.util.FileUtil;
36  import org.nuiton.config.ArgumentsParserException;
37  
38  import fr.ifremer.isisfish.datastore.AutoMigrationIsisH2Config;
39  import fr.ifremer.isisfish.datastore.CodeSourceStorage.Location;
40  import fr.ifremer.isisfish.datastore.DataStorageTestHelper;
41  import freemarker.cache.ClassTemplateLoader;
42  import freemarker.ext.beans.BeansWrapper;
43  import freemarker.template.Configuration;
44  
45  /**
46   * Abstract test case for isis fish.
47   * 
48   * Contains BeforeClass and AfterClass method to proper init cases.
49   * 
50   * Each test is done in a isolated isis-database directory, and user.home
51   * is set to this directory.
52   *
53   * @author chatellier
54   * @version $Revision: 4213 $
55   * 
56   * Last update : $Date: 2015-05-04 17:18:17 +0200 (Mon, 04 May 2015) $
57   * By : $Author: echatellier $
58   */
59  public abstract class AbstractIsisFishTest {
60  
61      protected static File dirIsisBase;
62  
63      /**
64       * Return specific temp dir.
65       * 
66       * @return system temp dir
67       */
68      public static File getTestDirectory() {
69          String tempdir = System.getProperty("java.io.tmpdir");
70  
71          File tempdirfile = new File(tempdir);
72          if (!tempdirfile.exists()) {
73              tempdirfile.mkdirs();
74          }
75  
76          return tempdirfile;
77      }
78  
79      /**
80       * Toujours appeler cette method pour les test d'isis.
81       * (sinon, il ira ecrire dans le isis-database officiel).
82       * 
83       * Create a temp dir and init isis with that temp dir as database.
84       * 
85       * @throws Exception
86       */
87      @BeforeClass
88      public static void init() throws Exception {
89  
90          File mavenTestDir = getTestDirectory();
91          dirIsisBase = FileUtil.createTempDirectory("isisdbtest", "", mavenTestDir);
92  
93          System.setProperty("user.home", dirIsisBase.getAbsolutePath());
94          System.setProperty(IsisConfig.Option.LAUNCH_UI.key, "false");
95          System.setProperty(IsisConfig.Option.ISIS_HOME_DIRECTORY.key, dirIsisBase.getAbsolutePath());
96          System.setProperty(IsisConfig.Option.SSH_KEY_FILE.key, dirIsisBase.getAbsolutePath() + File.separator + "ssh" + File.separator + "isis_test_dsa");
97  
98          IsisFish.init();
99          IsisFish.initVCS();
100         IsisFish.initCommunityVCS(); // for ui testing
101 
102         // install a new topia migration service callback
103         // to not ask for user for migration during test
104         AutoMigrationIsisH2Config.setTestMigrationCallBack();
105 
106         // reset static caches
107         DataStorageTestHelper.clearAllCache();
108 
109         FileUtils.copyDirectory(new File("src/test/resources/test-database").getAbsoluteFile(),
110             IsisFish.config.getDatabaseDirectory(), HiddenFileFilter.VISIBLE);
111     }
112 
113     /**
114      * Some tests modify configuration, reset it before all tests.
115      * @throws ArgumentsParserException 
116      */
117     @Before
118     public void resetConfig() throws ArgumentsParserException {
119         IsisFish.config = new IsisConfig();
120         IsisFish.config.parse();
121 
122         // fix static cache in enum
123         Location.OFFICIAL.setDirectory(IsisFish.config.getDatabaseDirectory());
124         Location.COMMUNITY.setDirectory(IsisFish.config.getCommunityDatabaseDirectory());
125         Location.ALL.setDirectory(IsisFish.config.getDatabaseDirectory(), IsisFish.config.getCommunityDatabaseDirectory());
126     }
127     
128     /**
129      * Return current database directory.
130      * 
131      * @return current database directory
132      */
133     protected static File getCurrentDatabaseDirectory() {
134         return dirIsisBase;
135     }
136 
137     /**
138      * Delete created temp directory.
139      */
140     @AfterClass
141     public static void clean() {
142         System.clearProperty("user.home");
143         if (dirIsisBase != null) {
144             FileUtils.deleteQuietly(dirIsisBase);
145             dirIsisBase = null;
146         }
147     }
148 
149     /**
150      * Return common script freemarker configuration.
151      * 
152      * @return freemarker {@link Configuration}
153      */
154     protected static Configuration getFreemarkerConfiguration() {
155         Configuration freemarkerConfiguration = new Configuration();
156 
157         // needed to overwrite "Defaults to default system encoding."
158         // fix encoding issue on some systems
159         freemarkerConfiguration.setDefaultEncoding("utf-8");
160 
161         // specific template loader to get template from jars (classpath)
162         ClassTemplateLoader templateLoader = new ClassTemplateLoader(AbstractIsisFishTest.class, "/");
163         freemarkerConfiguration.setTemplateLoader(templateLoader);
164 
165         freemarkerConfiguration.setObjectWrapper(new BeansWrapper());
166 
167         return freemarkerConfiguration;
168     }
169 }