View Javadoc
1   /*
2    * #%L
3    * Coser :: Business
4    * %%
5    * Copyright (C) 2010 - 2012 Ifremer, Codelutin, Chatellier Eric
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  
23  package fr.ifremer.coser.services;
24  
25  import java.io.File;
26  import java.io.FileFilter;
27  import java.io.IOException;
28  import java.util.ArrayList;
29  import java.util.HashMap;
30  import java.util.Locale;
31  import java.util.Map;
32  
33  import fr.ifremer.coser.CoserApplicationContext;
34  import fr.ifremer.coser.DefaultCoserApplicationContext;
35  import org.apache.commons.io.FileUtils;
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import org.junit.AfterClass;
39  import org.junit.Before;
40  import org.junit.BeforeClass;
41  import org.nuiton.i18n.I18n;
42  
43  import fr.ifremer.coser.CoserBusinessConfig;
44  import fr.ifremer.coser.CoserBusinessException;
45  import fr.ifremer.coser.CoserClassLoader;
46  import fr.ifremer.coser.CoserConstants.Category;
47  import fr.ifremer.coser.bean.Project;
48  import org.nuiton.util.FileUtil;
49  
50  /**
51   * Common coser test code.
52   * 
53   * @author chatellier
54  
55   * 
56  
57  
58   */
59  public abstract class CoserTestAbstract {
60  
61      private static final Log log = LogFactory.getLog(CoserTestAbstract.class);
62      
63      protected static CoserBusinessConfig config;
64      protected static CoserApplicationContext serviceContext;
65  
66      protected static File testDirectory;
67  
68      @BeforeClass
69      public static void setUpClassLoader() {
70          // declare new classloader because validation files are not
71          // in classpath
72          ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
73          CoserClassLoader coserClassLoader = new CoserClassLoader(currentClassLoader);
74          Thread.currentThread().setContextClassLoader(coserClassLoader);
75      }
76  
77      @BeforeClass
78      public static void initConfig() throws IOException {
79          String tmpDir = System.getProperty("java.io.tmpdir");
80          testDirectory = new File(tmpDir, "coser");
81          FileUtil.createDirectoryIfNecessary(testDirectory);
82          config = new CoserBusinessConfig();
83          config.setDatabaseDirectory(testDirectory.getAbsolutePath());
84          
85          I18n.init(null, Locale.UK);
86  
87          config.setWebIndicatorsFile(CoserTestAbstract.class.getResource("/webindicators.csv").getFile());
88          config.setWebZonesFile(CoserTestAbstract.class.getResource("/webzones.csv").getFile());
89          serviceContext = new DefaultCoserApplicationContext(config);
90      }
91  
92      @AfterClass
93      public static void cleanDirectory() throws IOException {
94          FileUtils.deleteDirectory(testDirectory);
95      }
96  
97      /**
98       * Recopie la base pour chaque test pour a avoir une vrai isolation
99       * au niveau des tests.
100      * 
101      * @throws IOException
102      */
103     @Before
104     public void initProjectDatabase() throws IOException {
105         FileUtils.cleanDirectory(config.getDatabaseDirectory());
106         copyDirectoryContent("src.test.resources.projects", config.getRSufiProjectsDirectory());
107     }
108 
109     /**
110      * Create an initialized project with csv file in test resources.
111      * 
112      * Project is filled with control data (not selections).
113      * 
114      * @param projectService project service
115      * @param controlValidated if {@code true} mark loaded control as {@code validated}
116      * @return project
117      * @throws CoserBusinessException
118      */
119     protected Project createTestProject(ProjectService projectService, boolean controlValidated) throws CoserBusinessException {
120         
121         Project project = new Project();
122         project.setName("Project-" + System.nanoTime());
123         
124         File testCatch = new File(CoserTestAbstract.class.getResource("/csv/correct/testcatch.csv").getFile());
125         File testHaul = new File(CoserTestAbstract.class.getResource("/csv/correct/testhaul.csv").getFile());
126         File testLength = new File(CoserTestAbstract.class.getResource("/csv/correct/testlength.csv").getFile());
127         File testStrata = new File(CoserTestAbstract.class.getResource("/csv/correct/teststrata.csv").getFile());
128         File testReftax = new File(CoserTestAbstract.class.getResource("/csv/correct/testreftax.csv").getFile());
129         File testTypeEspeces = new File(CoserTestAbstract.class.getResource("/csv/correct/testtypeespeces.csv").getFile());
130 
131         Map<Category, File> categoriesAndFile = new HashMap<Category, File>();
132         categoriesAndFile.put(Category.CATCH, testCatch);
133         categoriesAndFile.put(Category.HAUL, testHaul);
134         categoriesAndFile.put(Category.LENGTH, testLength);
135         categoriesAndFile.put(Category.STRATA, testStrata);
136         categoriesAndFile.put(Category.REFTAX_SPECIES, testReftax);
137         categoriesAndFile.put(Category.TYPE_ESPECES, testTypeEspeces);
138 
139         project.setCatchFileName(testCatch.getName());
140         project.setLengthFileName(testLength.getName());
141         project.setHaulFileName(testHaul.getName());
142         project.setStrataFileName(testStrata.getName());
143 
144         project = projectService.createProject(project, categoriesAndFile, new ArrayList<File>(), null);
145         project = projectService.loadControlData(project);
146         
147         // sans la sauvegarde des données de control, on ne pas creer de
148         // selection
149         projectService.saveProjectControl(project);
150         
151         project.getControl().setValidated(controlValidated);
152 
153         if (log.isDebugEnabled()) {
154             log.debug("Created project : " + project.getName());
155         }
156         return project;
157     }
158 
159     /**
160      * Ouvre un projet present dans le repertoire de test (copié depuis
161      * les resources ou créé durant les tests).
162      * 
163      * @param projectService project service
164      * @param projectName project name
165      * @return opened project (control data loaded)
166      * @throws CoserBusinessException
167      */
168     protected Project openTestProject(ProjectService projectService, String projectName) throws CoserBusinessException {
169 
170         Project project = projectService.openProject(projectName);
171         project = projectService.loadControlData(project);
172         return project;
173     }
174 
175     /**
176      * Util method used to build path for test.
177      * 
178      * @param root root dir
179      * @param paths path sub component
180      * @return file
181      */
182     protected File getFile(File root, String... paths) {
183         File localFile = root;
184         for (String path : paths) {
185             localFile = new File(localFile, path);
186         }
187         return localFile;
188     }
189 
190     protected void copyDirectoryContent(String path, File target) throws IOException {
191         File basedir = new File(new File("").getAbsolutePath());
192         File source = FileUtil.getFileFromFQN(basedir, path);
193         FileFilter fileterWithNoSvn = new FileFilter() {
194             @Override
195             public boolean accept(File pathname) {
196                 boolean result = true;
197                 if (pathname.getAbsolutePath().contains(File.separator + ".svn")) {
198                     result = false;
199                 }
200                 return result;
201             }
202         };
203         FileUtils.copyDirectory(source, target, fileterWithNoSvn);
204     }
205 }