1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
52
53
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
71
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
99
100
101
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
111
112
113
114
115
116
117
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
148
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
161
162
163
164
165
166
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
177
178
179
180
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 }