1 | /**
|
---|
2 | *
|
---|
3 | */
|
---|
4 | package wjhk.jupload.translation;
|
---|
5 |
|
---|
6 | import java.io.BufferedReader;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.FileInputStream;
|
---|
9 | import java.io.FileNotFoundException;
|
---|
10 | import java.io.FileReader;
|
---|
11 | import java.io.FilenameFilter;
|
---|
12 | import java.io.IOException;
|
---|
13 | import java.io.UnsupportedEncodingException;
|
---|
14 | import java.util.Properties;
|
---|
15 |
|
---|
16 | import org.apache.maven.plugin.MojoExecutionException;
|
---|
17 | import org.junit.After;
|
---|
18 | import org.junit.Assert;
|
---|
19 | import org.junit.Before;
|
---|
20 | import org.junit.Test;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * @author etienne_sf
|
---|
24 | */
|
---|
25 | public class TranslationManagerMojoTest {
|
---|
26 |
|
---|
27 | final static String TEST_COPPERMINE_FOLDER = "src/test/resources/lang.coppermine.utf-8";
|
---|
28 |
|
---|
29 | final static String TEST_LANG_UTF8_FOLDER = "src/test/resources/lang.utf-8";
|
---|
30 |
|
---|
31 | final static String TEST_LANG_UTF16_FOLDER = "src/test/resources/lang.utf-16";
|
---|
32 |
|
---|
33 | final static String TEST_LANG_KO_FOLDER = "src/test/resources/lang.ko";
|
---|
34 |
|
---|
35 | final static String DEFAULT_DOC_FOLDER = "target/translationTest-docFolder";
|
---|
36 |
|
---|
37 | final static String DEFAULT_INPUT_FOLDER = "target/translationTest-inputFolder";
|
---|
38 |
|
---|
39 | final static String DEFAULT_RESOURCE_LANG_FOLDER = "target/translationTest-resourceLangFolder";
|
---|
40 |
|
---|
41 | final static String VERIF_UTF8_FOLDER = "src/test/resources/lang.verif.utf-8";
|
---|
42 |
|
---|
43 | final static String VERIF_UTF16_FOLDER = "src/test/resources/lang.verif.utf-16";
|
---|
44 |
|
---|
45 | final static String VERIF_UTF16_APT_FOLDER = "src/test/resources/lang.verif.utf-16_to_apt";
|
---|
46 |
|
---|
47 | final static String VERIF_UTF16_TO_UNICODE_FOLDER = "src/test/resources/lang.verif.utf-16_to_unicode";
|
---|
48 |
|
---|
49 | final static String RESOURCE_ENCODING = "UTF-8";
|
---|
50 |
|
---|
51 | TranslationManagerMojo translationManagerMojo = null;
|
---|
52 |
|
---|
53 | File projectRoot = null;
|
---|
54 |
|
---|
55 | File docFolder = null;
|
---|
56 |
|
---|
57 | File inputFolder = null;
|
---|
58 |
|
---|
59 | File resourceLangFolder = null;
|
---|
60 |
|
---|
61 | File verifUTF8Folder = null;
|
---|
62 |
|
---|
63 | File verifUTF16Folder = null;
|
---|
64 |
|
---|
65 | File verifUTF16AptFolder = null;
|
---|
66 |
|
---|
67 | FilenameFilter noSvnFilenameFilter = new NoSvnFilenameFilter();
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * @throws java.lang.Exception
|
---|
71 | */
|
---|
72 | @Before
|
---|
73 | public void setUp() throws Exception {
|
---|
74 | // Let's get the current folder.
|
---|
75 | projectRoot = new File(".");
|
---|
76 | String currentDir = projectRoot.getAbsolutePath();
|
---|
77 | if (projectRoot.getAbsolutePath().endsWith(".")) {
|
---|
78 | // This should always be the case.
|
---|
79 | currentDir = projectRoot.getAbsolutePath();
|
---|
80 | currentDir = currentDir.substring(0, currentDir.length() - 2);
|
---|
81 | projectRoot = new File(currentDir);
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (currentDir.endsWith("jupload")) {
|
---|
85 | currentDir = projectRoot.getAbsolutePath() + File.separator
|
---|
86 | + "jupload-translation";
|
---|
87 | projectRoot = new File(currentDir);
|
---|
88 | } else if (!currentDir.endsWith("maven-translation-plugin")) {
|
---|
89 | Assert.fail("Non-managed current folder: " + currentDir);
|
---|
90 | }
|
---|
91 |
|
---|
92 | verifUTF8Folder = new File(projectRoot, VERIF_UTF8_FOLDER);
|
---|
93 | verifUTF16Folder = new File(projectRoot, VERIF_UTF16_FOLDER);
|
---|
94 | verifUTF16AptFolder = new File(projectRoot, VERIF_UTF16_APT_FOLDER);
|
---|
95 |
|
---|
96 | docFolder = new File(projectRoot, DEFAULT_DOC_FOLDER);
|
---|
97 | if (!docFolder.exists()) {
|
---|
98 | docFolder.mkdirs();
|
---|
99 | } else if (!docFolder.isDirectory()) {
|
---|
100 | Assert.fail(docFolder.getAbsolutePath()
|
---|
101 | + " must be a valid directory");
|
---|
102 | } else {
|
---|
103 | TranslationManagerMojo.emptyFolder(docFolder);
|
---|
104 | }
|
---|
105 |
|
---|
106 | inputFolder = new File(projectRoot, DEFAULT_INPUT_FOLDER);
|
---|
107 | if (!inputFolder.exists()) {
|
---|
108 | inputFolder.mkdirs();
|
---|
109 | } else if (!inputFolder.isDirectory()) {
|
---|
110 | Assert.fail(inputFolder.getAbsolutePath()
|
---|
111 | + " must be a valid directory");
|
---|
112 | } else {
|
---|
113 | TranslationManagerMojo.emptyFolder(inputFolder);
|
---|
114 | }
|
---|
115 |
|
---|
116 | resourceLangFolder = new File(projectRoot, DEFAULT_RESOURCE_LANG_FOLDER);
|
---|
117 | if (!resourceLangFolder.exists()) {
|
---|
118 | resourceLangFolder.mkdirs();
|
---|
119 | } else if (!resourceLangFolder.isDirectory()) {
|
---|
120 | Assert.fail(resourceLangFolder.getAbsolutePath()
|
---|
121 | + " must be a valid directory");
|
---|
122 | } else {
|
---|
123 | TranslationManagerMojo.emptyFolder(resourceLangFolder);
|
---|
124 | }
|
---|
125 |
|
---|
126 | File originalTestLangFolder = new File(projectRoot,
|
---|
127 | TEST_LANG_UTF8_FOLDER);
|
---|
128 | if (!inputFolder.isDirectory()) {
|
---|
129 | Assert.fail(inputFolder.getAbsolutePath()
|
---|
130 | + " must be a valid directory");
|
---|
131 | }
|
---|
132 |
|
---|
133 | // Test initialization.
|
---|
134 | translationManagerMojo = new TranslationManagerMojo();
|
---|
135 | translationManagerMojo.setDocFolder(docFolder);
|
---|
136 | translationManagerMojo.setInputFolder(inputFolder);
|
---|
137 | translationManagerMojo.setResourceLangFolder(resourceLangFolder);
|
---|
138 | translationManagerMojo
|
---|
139 | .setTemplateAvailableTranslation("src/test/resources/available_translations.template");
|
---|
140 | translationManagerMojo
|
---|
141 | .setTemplateOneTranslation("src/test/resources/one_translation.template");
|
---|
142 |
|
---|
143 | // The next setter will create the folder, if it doesn't exist.
|
---|
144 | translationManagerMojo.setWorkFolder(new File(projectRoot,
|
---|
145 | "target/translation-workFolder"));
|
---|
146 |
|
---|
147 | // Let's copy the lang files from src/test/resources to
|
---|
148 | // target/inputFolder
|
---|
149 | // The original files will then remain unchanged by the runned tests.
|
---|
150 | copyInputFilesForTest(originalTestLangFolder, RESOURCE_ENCODING);
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Copy files for the current test. This inputFolder is emptied before
|
---|
155 | * copying the files to it, to prevent 'interaction' between tests.
|
---|
156 | *
|
---|
157 | * @param testCaseInputFolder
|
---|
158 | * The folder which contains the files for the current test.
|
---|
159 | * @throws IOException
|
---|
160 | */
|
---|
161 | private void copyInputFilesForTest(File testCaseInputFolder,
|
---|
162 | String fileEncoding) throws IOException {
|
---|
163 | // First step: emptying input folder.
|
---|
164 | File[] files = inputFolder.listFiles(noSvnFilenameFilter);
|
---|
165 | for (int i = 0; i < files.length; i += 1) {
|
---|
166 | files[i].delete();
|
---|
167 | }
|
---|
168 |
|
---|
169 | // Second step, copy the source files to the inputFolder.
|
---|
170 | files = testCaseInputFolder.listFiles(noSvnFilenameFilter);
|
---|
171 | for (int i = 0; i < files.length; i += 1) {
|
---|
172 | if (files[i].getName().endsWith(".properties")) {
|
---|
173 | translationManagerMojo
|
---|
174 | .copyFile(files[i], fileEncoding, new File(inputFolder,
|
---|
175 | files[i].getName()), fileEncoding);
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * @param source
|
---|
182 | * @param target
|
---|
183 | * @throws IOException
|
---|
184 | * @throws MojoExecutionException
|
---|
185 | */
|
---|
186 | private void compareFiles(File source, File target) throws IOException,
|
---|
187 | MojoExecutionException {
|
---|
188 | if (!source.isFile()) {
|
---|
189 | throw new MojoExecutionException(
|
---|
190 | "The source file to compare to doesn't exist ("
|
---|
191 | + source.getAbsolutePath() + ")");
|
---|
192 | } else if (!target.isFile()) {
|
---|
193 | throw new MojoExecutionException(
|
---|
194 | "The target file to compare to doesn't exist ("
|
---|
195 | + target.getAbsolutePath() + ")");
|
---|
196 | }
|
---|
197 | long lenSource = source.length();
|
---|
198 | Assert.assertEquals(
|
---|
199 | "Length of the files must be equal ("
|
---|
200 | + source.getAbsolutePath() + " and "
|
---|
201 | + target.getAbsolutePath(), lenSource, target.length());
|
---|
202 |
|
---|
203 | BufferedReader brSource = new BufferedReader(new FileReader(source));
|
---|
204 | BufferedReader brTarget = new BufferedReader(new FileReader(target));
|
---|
205 | String s, t;
|
---|
206 | while ((s = brSource.readLine()) != null
|
---|
207 | && (t = brTarget.readLine()) != null) {
|
---|
208 | // As the file length are equals, the above and below test are
|
---|
209 | // enough (no need to check that we found the end of a file before
|
---|
210 | // the other one.
|
---|
211 | Assert.assertEquals("Content of the files must be equal ("
|
---|
212 | + source.getAbsolutePath() + " and "
|
---|
213 | + target.getAbsolutePath(), s, t);
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * @throws java.lang.Exception
|
---|
219 | */
|
---|
220 | @After
|
---|
221 | public void tearDown() throws Exception {
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * @throws MojoExecutionException
|
---|
226 | * @throws IOException
|
---|
227 | */
|
---|
228 | @Test
|
---|
229 | public void testExecute_UTF8() throws MojoExecutionException, IOException {
|
---|
230 | String filename = "lang_fr.properties";
|
---|
231 | File fileLangFR_utf8 = new File(verifUTF8Folder, filename + ".utf-8");
|
---|
232 | File fileLangFRUnicode = new File(verifUTF8Folder, filename
|
---|
233 | + ".unicode");
|
---|
234 | translationManagerMojo.execute();
|
---|
235 |
|
---|
236 | // Let's check the inputFolder. The lang file should remain unchanged.
|
---|
237 | // The lang_fr should be formatted.
|
---|
238 | Assert.assertEquals("Number of generated files", 2, inputFolder
|
---|
239 | .listFiles(noSvnFilenameFilter).length);
|
---|
240 | compareFiles(new File(verifUTF8Folder, "lang.properties"), new File(
|
---|
241 | inputFolder, "lang.properties"));
|
---|
242 | compareFiles(new File(inputFolder + File.separator + filename),
|
---|
243 | fileLangFR_utf8);
|
---|
244 |
|
---|
245 | // Let's check the resourceLangFolder. The lang file should remain
|
---|
246 | // unchanged. The lang_fr should be formatted, and 'unicoded'.
|
---|
247 | Assert.assertEquals("Number of generated files", 2, resourceLangFolder
|
---|
248 | .listFiles(noSvnFilenameFilter).length);
|
---|
249 | compareFiles(new File(verifUTF8Folder, "lang.properties"), new File(
|
---|
250 | resourceLangFolder, "lang.properties"));
|
---|
251 | compareFiles(new File(resourceLangFolder + File.separator + filename),
|
---|
252 | fileLangFRUnicode);
|
---|
253 |
|
---|
254 | // Let's check the docFolder.
|
---|
255 | Assert.assertEquals("Number of generated files", 3, docFolder
|
---|
256 | .listFiles(noSvnFilenameFilter).length);
|
---|
257 | // We just check that the file exists. The file generation is tested in
|
---|
258 | // testExecute_UTF16()
|
---|
259 | // No file extension has been given ...
|
---|
260 | filename = "available_translations";
|
---|
261 | Assert
|
---|
262 | .assertTrue("The file '" + filename + "' must exist in the "
|
---|
263 | + docFolder + " folder", new File(docFolder, filename)
|
---|
264 | .isFile());
|
---|
265 | filename = "lang.properties";
|
---|
266 | Assert
|
---|
267 | .assertTrue("The file '" + filename + "' must exist in the "
|
---|
268 | + docFolder + " folder", new File(docFolder, filename)
|
---|
269 | .isFile());
|
---|
270 | filename = "lang_fr.properties";
|
---|
271 | Assert
|
---|
272 | .assertTrue("The file '" + filename + "' must exist in the "
|
---|
273 | + docFolder + " folder", new File(docFolder, filename)
|
---|
274 | .isFile());
|
---|
275 | }
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * @throws MojoExecutionException
|
---|
279 | * @throws IOException
|
---|
280 | */
|
---|
281 | @Test
|
---|
282 | public void testExecute_UTF16() throws MojoExecutionException, IOException {
|
---|
283 | // First step: initialize this test case with the UTF-16 files
|
---|
284 | File originalTestLangFolder = new File(projectRoot,
|
---|
285 | TEST_LANG_UTF16_FOLDER);
|
---|
286 | if (!inputFolder.isDirectory()) {
|
---|
287 | Assert.fail(inputFolder.getAbsolutePath()
|
---|
288 | + " must be a valid directory");
|
---|
289 | }
|
---|
290 | // Let's copy the lang files from src/test/resources to
|
---|
291 | // target/inputFolder
|
---|
292 | copyInputFilesForTest(originalTestLangFolder, "UTF-16");
|
---|
293 | File verifUTF16ToUnicodeFolder = new File(projectRoot,
|
---|
294 | VERIF_UTF16_TO_UNICODE_FOLDER);
|
---|
295 |
|
---|
296 | // Let's execute the whole process
|
---|
297 | translationManagerMojo.setCoppermineFolder(new File(projectRoot,
|
---|
298 | TEST_COPPERMINE_FOLDER));
|
---|
299 | translationManagerMojo.setInputEncoding("UTF-16");
|
---|
300 | translationManagerMojo.setDocFileExtension(".apt");
|
---|
301 | translationManagerMojo.execute();
|
---|
302 |
|
---|
303 | // Let's test, now.
|
---|
304 | Assert
|
---|
305 | .assertEquals(
|
---|
306 | "Number of generated files (check of the verification folders)",
|
---|
307 | verifUTF16Folder.listFiles(noSvnFilenameFilter).length,
|
---|
308 | verifUTF16ToUnicodeFolder
|
---|
309 | .listFiles(noSvnFilenameFilter).length);
|
---|
310 | Assert.assertEquals("Number of generated files (UTF-16)",
|
---|
311 | verifUTF16Folder.listFiles(noSvnFilenameFilter).length,
|
---|
312 | inputFolder.listFiles(noSvnFilenameFilter).length);
|
---|
313 | Assert.assertEquals("Number of generated files (unicode)",
|
---|
314 | verifUTF16Folder.listFiles(noSvnFilenameFilter).length,
|
---|
315 | resourceLangFolder.listFiles(noSvnFilenameFilter).length);
|
---|
316 |
|
---|
317 | // Check of the inputFolder result.
|
---|
318 | File[] inputFiles = inputFolder.listFiles(noSvnFilenameFilter);
|
---|
319 | for (int i = 0; i < inputFiles.length; i += 1) {
|
---|
320 | compareFiles(new File(verifUTF16Folder, inputFiles[i].getName()),
|
---|
321 | inputFiles[i]);
|
---|
322 | compareFiles(new File(verifUTF16ToUnicodeFolder, inputFiles[i]
|
---|
323 | .getName()), new File(translationManagerMojo
|
---|
324 | .getResourceLangFolder(), inputFiles[i].getName()));
|
---|
325 | }// for
|
---|
326 |
|
---|
327 | // Check of the docFolder content, based on the inputFolder files.
|
---|
328 | File[] htmlFiles = docFolder.listFiles(noSvnFilenameFilter);
|
---|
329 | Assert
|
---|
330 | .assertEquals(
|
---|
331 | "Number of html generated files, including the available_translations.html file.",
|
---|
332 | inputFiles.length + 1, htmlFiles.length);
|
---|
333 | for (int i = 0; i < htmlFiles.length; i += 1) {
|
---|
334 | File fileToCompare = new File(verifUTF16AptFolder, htmlFiles[i]
|
---|
335 | .getName());
|
---|
336 | compareFiles(fileToCompare, htmlFiles[i]);
|
---|
337 | }// for
|
---|
338 | }
|
---|
339 |
|
---|
340 | /**
|
---|
341 | * @throws MojoExecutionException
|
---|
342 | * @throws IOException
|
---|
343 | */
|
---|
344 | @Test
|
---|
345 | public void testExecute_KO() throws MojoExecutionException, IOException {
|
---|
346 | // Test with a null inputFolder.
|
---|
347 | try {
|
---|
348 | translationManagerMojo.inputFolder = null;
|
---|
349 | translationManagerMojo.execute();
|
---|
350 | Assert.fail("Null inputFolder is prohibited");
|
---|
351 | } catch (NullPointerException e) {
|
---|
352 | // Success.
|
---|
353 | }
|
---|
354 | // Test with a non directory folder.
|
---|
355 | try {
|
---|
356 | translationManagerMojo.inputFolder = new File(projectRoot,
|
---|
357 | "not a valid file");
|
---|
358 | translationManagerMojo.execute();
|
---|
359 | Assert.fail("inputFolder must be directory");
|
---|
360 | } catch (MojoExecutionException e) {
|
---|
361 | // Success.
|
---|
362 | }
|
---|
363 | }
|
---|
364 |
|
---|
365 | /**
|
---|
366 | * @throws MojoExecutionException
|
---|
367 | * @throws UnsupportedEncodingException
|
---|
368 | */
|
---|
369 | @Test
|
---|
370 | public void testLoadOneLangFile_OK() throws MojoExecutionException,
|
---|
371 | UnsupportedEncodingException {
|
---|
372 | File propFile = new File(projectRoot, DEFAULT_INPUT_FOLDER
|
---|
373 | + File.separator + "lang.properties");
|
---|
374 | Properties props = translationManagerMojo.loadOneLangFile(propFile,
|
---|
375 | "ASCII");
|
---|
376 | Assert.assertEquals("The lang.properties file contains 5 entries", 5,
|
---|
377 | props.size());
|
---|
378 | Assert
|
---|
379 | .assertEquals(
|
---|
380 | "The lang.properties file contains this entry",
|
---|
381 | "This is a key.With a new line in it, and with a \n character!",
|
---|
382 | props.get("aKey"));
|
---|
383 | Assert
|
---|
384 | .assertEquals(
|
---|
385 | "The lang.properties file contains this entry",
|
---|
386 | "This is another key.Also with a new line in it, and also with a \n character!",
|
---|
387 | props.get("anotherKey"));
|
---|
388 | Assert.assertEquals("The lang.properties file contains this entry",
|
---|
389 | "this it the last key", props.get("aLastKey"));
|
---|
390 |
|
---|
391 | propFile = new File(projectRoot, DEFAULT_INPUT_FOLDER + File.separator
|
---|
392 | + "lang_fr.properties");
|
---|
393 | props = translationManagerMojo.loadOneLangFile(propFile, "UTF-8");
|
---|
394 | Assert.assertEquals("The lang_fr.properties file contains 4 entries",
|
---|
395 | 4, props.size());
|
---|
396 | Assert.assertEquals("The lang_fr.properties file contains this entry",
|
---|
397 | "C'est une clef", props.get("aKey"));
|
---|
398 | // The properties files may not be in UTF-8, but our file is !
|
---|
399 | // Let's read the property, based on its UTF-8 inputEncoding.
|
---|
400 | String lastValue = ((String) props.get("aLastKey"));
|
---|
401 | Assert.assertEquals("The lang_fr.properties file contains this entry",
|
---|
402 | "C'est une derni\u00e8re clef\nAvec un passage à la ligne!",
|
---|
403 | lastValue);
|
---|
404 |
|
---|
405 | propFile = new File(projectRoot, TEST_LANG_UTF16_FOLDER
|
---|
406 | + File.separator + "lang_fr.properties");
|
---|
407 | props = translationManagerMojo.loadOneLangFile(propFile, "UTF-16");
|
---|
408 | Assert.assertEquals("The lang_fr.properties file contains 82 entries",
|
---|
409 | 82, props.size());
|
---|
410 | // The properties files may not be in UTF-8, but our file is !
|
---|
411 | // Let's read the property, based on its UTF-8 inputEncoding.
|
---|
412 | String propName = "preparingFile";
|
---|
413 | String propValueNotEncoded = (String) props.get(propName);
|
---|
414 | Assert.assertNotNull("The '" + propName + "' should exist",
|
---|
415 | propValueNotEncoded);
|
---|
416 | Assert.assertEquals(
|
---|
417 | "The lang_fr.properties file contains this entry: '" + propName
|
---|
418 | + "'", "Pr\u00E9paration du fichier %1$d/%2$d",
|
---|
419 | propValueNotEncoded);
|
---|
420 | }
|
---|
421 |
|
---|
422 | /**
|
---|
423 | */
|
---|
424 | @Test
|
---|
425 | public void testLoadOneLangFile_KO() {
|
---|
426 | File propFile = new File(projectRoot, DEFAULT_INPUT_FOLDER
|
---|
427 | + File.separator + "a_non_existing_file");
|
---|
428 | try {
|
---|
429 | translationManagerMojo.loadOneLangFile(propFile, "UTF-8");
|
---|
430 | Assert.fail(propFile.getAbsolutePath()
|
---|
431 | + " doesn't exist: should throw an error");
|
---|
432 | } catch (MojoExecutionException e) {
|
---|
433 | // Success
|
---|
434 | }
|
---|
435 | }
|
---|
436 |
|
---|
437 | /**
|
---|
438 | * @throws MojoExecutionException
|
---|
439 | * @throws IOException
|
---|
440 | */
|
---|
441 | @Test
|
---|
442 | public void testFormatOneLangFile_utf8_OK() throws MojoExecutionException,
|
---|
443 | IOException {
|
---|
444 | File propFile = new File(projectRoot, DEFAULT_INPUT_FOLDER
|
---|
445 | + File.separator + "lang_fr.properties");
|
---|
446 | translationManagerMojo.formatOneLangFile(propFile, RESOURCE_ENCODING);
|
---|
447 |
|
---|
448 | String generatedFile = translationManagerMojo.getWorkFolder()
|
---|
449 | .getAbsolutePath()
|
---|
450 | + File.separator + propFile.getName();
|
---|
451 | File verifFile = new File(verifUTF8Folder, propFile.getName()
|
---|
452 | + ".utf-8");
|
---|
453 | compareFiles(new File(generatedFile), verifFile);
|
---|
454 | }
|
---|
455 |
|
---|
456 | /**
|
---|
457 | * @throws IOException
|
---|
458 | */
|
---|
459 | @Test
|
---|
460 | public void testFormatOneLangFile_utf8_KO() throws IOException {
|
---|
461 | copyInputFilesForTest(new File(projectRoot, TEST_LANG_KO_FOLDER),
|
---|
462 | RESOURCE_ENCODING);
|
---|
463 | File propFile = new File(projectRoot, DEFAULT_INPUT_FOLDER
|
---|
464 | + File.separator + "lang_fr.properties");
|
---|
465 | try {
|
---|
466 | translationManagerMojo.formatOneLangFile(propFile,
|
---|
467 | RESOURCE_ENCODING);
|
---|
468 | Assert.fail("processing lang.KO should throw an exception");
|
---|
469 | } catch (MojoExecutionException e) {
|
---|
470 | // Success!
|
---|
471 | }
|
---|
472 | }
|
---|
473 |
|
---|
474 | /**
|
---|
475 | * @throws FileNotFoundException
|
---|
476 | */
|
---|
477 | @Test
|
---|
478 | public void testCloseStream() throws FileNotFoundException {
|
---|
479 | // Closing a null Closeable is allowed.
|
---|
480 | translationManagerMojo.closeStream(null,
|
---|
481 | "A null FileInputStream should not raise an exception");
|
---|
482 |
|
---|
483 | FileInputStream fis = new FileInputStream(new File(inputFolder,
|
---|
484 | "lang.properties"));
|
---|
485 | translationManagerMojo.closeStream(fis, "A test FileInputStream");
|
---|
486 | // The stream must be closed. The only way to check that, is to try to
|
---|
487 | // read from it.
|
---|
488 | try {
|
---|
489 | fis.read();
|
---|
490 | Assert.fail("The stream should be closed!");
|
---|
491 | } catch (IOException e) {
|
---|
492 | // Ok !
|
---|
493 | }
|
---|
494 | }
|
---|
495 |
|
---|
496 | /**
|
---|
497 | * @throws Exception
|
---|
498 | */
|
---|
499 | @Test
|
---|
500 | public void testCopyAllLangFilesToLangFolder() throws Exception {
|
---|
501 | File source = new File(translationManagerMojo.getWorkFolder(),
|
---|
502 | "lang_fr.properties");
|
---|
503 | File target = new File(translationManagerMojo.getInputFolder(),
|
---|
504 | "lang_fr.properties");
|
---|
505 | try {
|
---|
506 | compareFiles(source, target);
|
---|
507 | throw new Exception(
|
---|
508 | source.getAbsolutePath()
|
---|
509 | + " and "
|
---|
510 | + target.getAbsolutePath()
|
---|
511 | + " should be different, before copying it back to the lang folder");
|
---|
512 | } catch (MojoExecutionException e1) {
|
---|
513 | // OK
|
---|
514 | }
|
---|
515 |
|
---|
516 | translationManagerMojo.copyFile(new File(projectRoot,
|
---|
517 | DEFAULT_INPUT_FOLDER + File.separator + "lang_fr.properties"),
|
---|
518 | "UTF-8", source, "UTF-8");
|
---|
519 |
|
---|
520 | translationManagerMojo.copyAllLangFilesToInputFolder();
|
---|
521 | // Then the files must be equals
|
---|
522 | compareFiles(source, target);
|
---|
523 | }
|
---|
524 |
|
---|
525 | /**
|
---|
526 | * @throws MojoExecutionException
|
---|
527 | * @throws IOException
|
---|
528 | */
|
---|
529 | @Test
|
---|
530 | public void testCopyAllLangFilesToResourcesLangFolder()
|
---|
531 | throws MojoExecutionException, IOException {
|
---|
532 | File propFile = new File(projectRoot, DEFAULT_INPUT_FOLDER
|
---|
533 | + File.separator + "lang_fr.properties");
|
---|
534 | translationManagerMojo.formatOneLangFile(propFile, RESOURCE_ENCODING);
|
---|
535 | translationManagerMojo.copyAllLangFilesToResourcesLangFolder();
|
---|
536 |
|
---|
537 | String generatedFile = translationManagerMojo.getResourceLangFolder()
|
---|
538 | .getAbsolutePath()
|
---|
539 | + File.separator + propFile.getName();
|
---|
540 | File verifFile = new File(verifUTF8Folder, propFile.getName()
|
---|
541 | + ".unicode");
|
---|
542 | compareFiles(new File(generatedFile), verifFile);
|
---|
543 | }
|
---|
544 |
|
---|
545 | /**
|
---|
546 | * @throws MojoExecutionException
|
---|
547 | * @throws IOException
|
---|
548 | */
|
---|
549 | @Test
|
---|
550 | public void testGenerateDocForOneTranslation()
|
---|
551 | throws MojoExecutionException, IOException {
|
---|
552 | File file = new File(verifUTF16Folder, "lang_fr.properties");
|
---|
553 | Properties props = new Properties();
|
---|
554 | props.setProperty("language", "French_for_test");
|
---|
555 | translationManagerMojo.setDocFileExtension(".apt");
|
---|
556 | translationManagerMojo.generateDocForOneTranslation(file, props);
|
---|
557 |
|
---|
558 | File generatedFile = new File(translationManagerMojo.getDocFolder(),
|
---|
559 | "lang_fr.properties.apt");
|
---|
560 | File verifFolder = new File(projectRoot,
|
---|
561 | "src/test/resources/testGenerateDocForOneTranslation");
|
---|
562 | File verifFile = new File(verifFolder, "lang_fr.properties.apt");
|
---|
563 | compareFiles(generatedFile, verifFile);
|
---|
564 | }
|
---|
565 |
|
---|
566 | /**
|
---|
567 | * Test of the setCoppermineFolder parameter. Tests its constaints
|
---|
568 | *
|
---|
569 | * @throws MojoExecutionException
|
---|
570 | */
|
---|
571 | @Test
|
---|
572 | public void testSetCoppermineFolder() throws MojoExecutionException {
|
---|
573 | // null folder are allowed.
|
---|
574 | translationManagerMojo.setCoppermineFolder(null);
|
---|
575 | Assert.assertNull("coppermineFolder may be null",
|
---|
576 | translationManagerMojo.coppermineFolder);
|
---|
577 | Assert.assertNull("coppermineFolder may be null",
|
---|
578 | translationManagerMojo.getCoppermineFolder());
|
---|
579 |
|
---|
580 | // If it exists, it must be a folder.
|
---|
581 | try {
|
---|
582 | translationManagerMojo.setCoppermineFolder(new File(projectRoot,
|
---|
583 | DEFAULT_INPUT_FOLDER + File.separator + "lang.properties"));
|
---|
584 | Assert.fail("If it exists, it must be a folder");
|
---|
585 | } catch (MojoExecutionException e) {
|
---|
586 | // Ok !
|
---|
587 | }
|
---|
588 |
|
---|
589 | // If it is not null, it must exist
|
---|
590 | try {
|
---|
591 | translationManagerMojo.setCoppermineFolder(new File(projectRoot,
|
---|
592 | File.separator + "not_target" + File.separator
|
---|
593 | + "this_folder_does_not_exist"));
|
---|
594 | Assert
|
---|
595 | .fail("coppermineFolder which doesn't exist is allowed, only if the path contains /target/");
|
---|
596 | } catch (MojoExecutionException e) {
|
---|
597 | // Ok !
|
---|
598 | }
|
---|
599 |
|
---|
600 | // coppermineFolder must exist. We take a valid folder.
|
---|
601 | File coppermineFolder = docFolder;
|
---|
602 | translationManagerMojo.coppermineFolder = null;
|
---|
603 | translationManagerMojo.setCoppermineFolder(coppermineFolder);
|
---|
604 | Assert.assertEquals("coppermineFolder must be set", coppermineFolder,
|
---|
605 | translationManagerMojo.coppermineFolder);
|
---|
606 | Assert.assertEquals("coppermineFolder must be set", coppermineFolder,
|
---|
607 | translationManagerMojo.getCoppermineFolder());
|
---|
608 | }
|
---|
609 |
|
---|
610 | /**
|
---|
611 | * Test of the setHtmlFolder parameter. Tests its constaints
|
---|
612 | *
|
---|
613 | * @throws MojoExecutionException
|
---|
614 | */
|
---|
615 | @Test
|
---|
616 | public void testSetDocFolder() throws MojoExecutionException {
|
---|
617 | // null folder are allowed.
|
---|
618 | translationManagerMojo.setDocFolder(null);
|
---|
619 | Assert.assertNull("docFolder may be null",
|
---|
620 | translationManagerMojo.docFolder);
|
---|
621 | Assert.assertNull("docFolder may be null", translationManagerMojo
|
---|
622 | .getDocFolder());
|
---|
623 |
|
---|
624 | // If it exists, it must be a folder.
|
---|
625 | try {
|
---|
626 | translationManagerMojo.setDocFolder(new File(projectRoot,
|
---|
627 | DEFAULT_INPUT_FOLDER + File.separator + "lang.properties"));
|
---|
628 | Assert.fail("If it exists, it must be a folder");
|
---|
629 | } catch (MojoExecutionException e) {
|
---|
630 | // Ok !
|
---|
631 | }
|
---|
632 |
|
---|
633 | // If it doesn't exist, it must be within the /target/ folder.
|
---|
634 | translationManagerMojo.setDocFolder(new File(projectRoot,
|
---|
635 | DEFAULT_INPUT_FOLDER + File.separator + "target"
|
---|
636 | + File.separator + "lang.properties"));
|
---|
637 | try {
|
---|
638 | translationManagerMojo.setDocFolder(new File(projectRoot,
|
---|
639 | File.separator + "not_target" + File.separator
|
---|
640 | + "this_folder_does_not_exist"));
|
---|
641 | Assert
|
---|
642 | .fail("docFolder which doesn't exist is allowed, if the path contains /target/");
|
---|
643 | } catch (MojoExecutionException e) {
|
---|
644 | // Ok !
|
---|
645 | }
|
---|
646 |
|
---|
647 | translationManagerMojo.docFolder = null;
|
---|
648 | translationManagerMojo.setDocFolder(docFolder);
|
---|
649 | Assert.assertEquals("docFolder should be null", docFolder,
|
---|
650 | translationManagerMojo.docFolder);
|
---|
651 | Assert.assertEquals("docFolder should be null", docFolder,
|
---|
652 | translationManagerMojo.getDocFolder());
|
---|
653 | }
|
---|
654 |
|
---|
655 | /**
|
---|
656 | * Test of the setHtmlFolder parameter. Tests its constaints
|
---|
657 | *
|
---|
658 | * @throws MojoExecutionException
|
---|
659 | */
|
---|
660 | @Test
|
---|
661 | public void testSetDocExtension() throws MojoExecutionException {
|
---|
662 | // null folder are allowed.
|
---|
663 | translationManagerMojo.setDocFileExtension(null);
|
---|
664 | Assert.assertNull("docFileExtension may be null",
|
---|
665 | translationManagerMojo.docFileExtension);
|
---|
666 | Assert
|
---|
667 | .assertEquals(
|
---|
668 | "docFileExtension may be null. getDocFileExtension() should return an empty string",
|
---|
669 | "", translationManagerMojo.getDocFileExtension());
|
---|
670 |
|
---|
671 | String docFileExtension = ".an_extension";
|
---|
672 | translationManagerMojo.setDocFileExtension(docFileExtension);
|
---|
673 | Assert.assertEquals("DocFileExtension must be set", docFileExtension,
|
---|
674 | translationManagerMojo.docFileExtension);
|
---|
675 | Assert
|
---|
676 | .assertEquals(
|
---|
677 | "When DocFileExtension is null, getDocFileExtension() should return an empty string",
|
---|
678 | docFileExtension, translationManagerMojo
|
---|
679 | .getDocFileExtension());
|
---|
680 | }
|
---|
681 |
|
---|
682 | /**
|
---|
683 | * @throws MojoExecutionException
|
---|
684 | */
|
---|
685 | @Test
|
---|
686 | public void testSetInputEncoding() throws MojoExecutionException {
|
---|
687 | Assert.assertEquals("Default inputEncoding is UTF-8",
|
---|
688 | translationManagerMojo.inputEncoding, "UTF-8");
|
---|
689 |
|
---|
690 | translationManagerMojo.setInputEncoding("UTF-16");
|
---|
691 | Assert.assertEquals(translationManagerMojo.inputEncoding, "UTF-16");
|
---|
692 |
|
---|
693 | translationManagerMojo.setInputEncoding("UTF-8");
|
---|
694 | Assert.assertEquals("Setting to UTF-8",
|
---|
695 | translationManagerMojo.inputEncoding, "UTF-8");
|
---|
696 |
|
---|
697 | try {
|
---|
698 | translationManagerMojo.setInputEncoding(null);
|
---|
699 | Assert.fail("inputEncoding null value should be prohibited");
|
---|
700 | } catch (NullPointerException e) {
|
---|
701 | // Success!
|
---|
702 | }
|
---|
703 | try {
|
---|
704 | translationManagerMojo
|
---|
705 | .setInputEncoding("Not a valid inputEncoding");
|
---|
706 | Assert
|
---|
707 | .fail("inputEncoding value has a limited set of valid values");
|
---|
708 | } catch (MojoExecutionException e) {
|
---|
709 | // Success!
|
---|
710 | }
|
---|
711 |
|
---|
712 | }
|
---|
713 |
|
---|
714 | /** */
|
---|
715 | @Test
|
---|
716 | public void testGetLangFolder() {
|
---|
717 | Assert.assertEquals(translationManagerMojo.inputFolder,
|
---|
718 | translationManagerMojo.getInputFolder());
|
---|
719 | translationManagerMojo.inputFolder = null;
|
---|
720 | Assert.assertEquals(translationManagerMojo.inputFolder,
|
---|
721 | translationManagerMojo.getInputFolder());
|
---|
722 | }
|
---|
723 |
|
---|
724 | /**
|
---|
725 | * Test of the setLangFolder parameter. Tests its constaints
|
---|
726 | *
|
---|
727 | * @throws MojoExecutionException
|
---|
728 | */
|
---|
729 | @Test
|
---|
730 | public void testSetInputFolder() throws MojoExecutionException {
|
---|
731 | // null folder are forbidden.
|
---|
732 | try {
|
---|
733 | translationManagerMojo.setInputFolder(null);
|
---|
734 | Assert.fail("null folder are forbidden");
|
---|
735 | } catch (NullPointerException e) {
|
---|
736 | // Ok !
|
---|
737 | }
|
---|
738 |
|
---|
739 | // If it exists, it must be a folder.
|
---|
740 | try {
|
---|
741 | translationManagerMojo.setInputFolder(new File(projectRoot,
|
---|
742 | DEFAULT_INPUT_FOLDER + File.separator + "lang.properties"));
|
---|
743 | Assert.fail("If it exists, it must be a folder");
|
---|
744 | } catch (MojoExecutionException e) {
|
---|
745 | // Ok !
|
---|
746 | }
|
---|
747 |
|
---|
748 | translationManagerMojo.inputFolder = null;
|
---|
749 | translationManagerMojo.setInputFolder(new File(projectRoot,
|
---|
750 | DEFAULT_INPUT_FOLDER));
|
---|
751 | Assert.assertEquals("inputFolder must be set", inputFolder,
|
---|
752 | translationManagerMojo.inputFolder);
|
---|
753 |
|
---|
754 | }
|
---|
755 |
|
---|
756 | /**
|
---|
757 | */
|
---|
758 | @Test
|
---|
759 | public void testGetWorkDirectory() {
|
---|
760 | Assert.assertEquals(translationManagerMojo.workFolder,
|
---|
761 | translationManagerMojo.getWorkFolder());
|
---|
762 | }
|
---|
763 |
|
---|
764 | /**
|
---|
765 | * @throws MojoExecutionException
|
---|
766 | */
|
---|
767 | @Test
|
---|
768 | public void testSetWorkDirectory() throws MojoExecutionException {
|
---|
769 | File dummyWorkDirectory = new File("target/a_dummy_folder");
|
---|
770 | Assert.assertNotSame(translationManagerMojo.getWorkFolder(),
|
---|
771 | dummyWorkDirectory);
|
---|
772 |
|
---|
773 | // null folder are forbidden.
|
---|
774 | try {
|
---|
775 | translationManagerMojo.setWorkFolder(null);
|
---|
776 | Assert.fail("null folder are forbidden");
|
---|
777 | } catch (MojoExecutionException e) {
|
---|
778 | // Ok !
|
---|
779 | }
|
---|
780 |
|
---|
781 | // If it exists, it must be a folder.
|
---|
782 | try {
|
---|
783 | translationManagerMojo.setWorkFolder(new File(projectRoot,
|
---|
784 | DEFAULT_INPUT_FOLDER + File.separator + "lang.properties"));
|
---|
785 | Assert.fail("If it exists, it must be a folder");
|
---|
786 | } catch (MojoExecutionException e) {
|
---|
787 | // Ok !
|
---|
788 | }
|
---|
789 |
|
---|
790 | // We want to test the folder creation.
|
---|
791 | if (dummyWorkDirectory.exists()) {
|
---|
792 | dummyWorkDirectory.delete();
|
---|
793 | }
|
---|
794 | Assert.assertFalse("The " + dummyWorkDirectory.getAbsolutePath()
|
---|
795 | + " should not exist", dummyWorkDirectory.exists());
|
---|
796 |
|
---|
797 | // Let's go...
|
---|
798 | translationManagerMojo.setWorkFolder(dummyWorkDirectory);
|
---|
799 | Assert.assertEquals("After setter: the property should be set!",
|
---|
800 | translationManagerMojo.getWorkFolder(), dummyWorkDirectory);
|
---|
801 | Assert.assertTrue("The " + dummyWorkDirectory.getAbsolutePath()
|
---|
802 | + " should exist", dummyWorkDirectory.exists());
|
---|
803 | }
|
---|
804 |
|
---|
805 | /**
|
---|
806 | * @throws MojoExecutionException
|
---|
807 | */
|
---|
808 | @Test
|
---|
809 | public void testSetResourceLangFolder() throws MojoExecutionException {
|
---|
810 | translationManagerMojo.resourceLangFolder = null;
|
---|
811 | translationManagerMojo.setResourceLangFolder(resourceLangFolder);
|
---|
812 | Assert.assertEquals("resourceLangFolder must be set",
|
---|
813 | resourceLangFolder, translationManagerMojo.resourceLangFolder);
|
---|
814 |
|
---|
815 | // null folder are forbidden.
|
---|
816 | try {
|
---|
817 | translationManagerMojo.setResourceLangFolder(null);
|
---|
818 | Assert.fail("null folder are forbidden");
|
---|
819 | } catch (NullPointerException e) {
|
---|
820 | // Ok !
|
---|
821 | }
|
---|
822 |
|
---|
823 | // If it exists, it must be a folder.
|
---|
824 | try {
|
---|
825 | translationManagerMojo.setResourceLangFolder(new File(projectRoot,
|
---|
826 | DEFAULT_INPUT_FOLDER + File.separator + "lang.properties"));
|
---|
827 | Assert.fail("If it exists, it must be a folder");
|
---|
828 | } catch (MojoExecutionException e) {
|
---|
829 | // Ok !
|
---|
830 | }
|
---|
831 | }
|
---|
832 |
|
---|
833 | /**
|
---|
834 | * @throws MojoExecutionException
|
---|
835 | */
|
---|
836 | @Test
|
---|
837 | public void testSetTemplateAvailableTranslation()
|
---|
838 | throws MojoExecutionException {
|
---|
839 | translationManagerMojo.templateAvailableTranslation = null;
|
---|
840 | Assert.assertNull("templateAvailableTranslation should be null",
|
---|
841 | translationManagerMojo.getTemplateAvailableTranslation());
|
---|
842 |
|
---|
843 | String s = "a_non_existing_template";
|
---|
844 | try {
|
---|
845 | translationManagerMojo.setTemplateAvailableTranslation(s);
|
---|
846 | Assert
|
---|
847 | .fail("Call to setTemplateAvailableTranslation must be done with a valid file as an argument");
|
---|
848 | } catch (MojoExecutionException e) {
|
---|
849 | // Success!
|
---|
850 | }
|
---|
851 |
|
---|
852 | s = "src/test/resources/one_translation.template";
|
---|
853 | translationManagerMojo.setTemplateAvailableTranslation(s);
|
---|
854 | Assert.assertEquals("Test 1", s,
|
---|
855 | translationManagerMojo.templateAvailableTranslation);
|
---|
856 | Assert.assertEquals("Test 1", s, translationManagerMojo
|
---|
857 | .getTemplateAvailableTranslation());
|
---|
858 |
|
---|
859 | translationManagerMojo.setTemplateAvailableTranslation(null);
|
---|
860 | Assert.assertNull("templateAvailableTranslation should be null",
|
---|
861 | translationManagerMojo.templateAvailableTranslation);
|
---|
862 | }
|
---|
863 |
|
---|
864 | /**
|
---|
865 | * @throws MojoExecutionException
|
---|
866 | */
|
---|
867 | @Test
|
---|
868 | public void testSetTemplateOneTranslation() throws MojoExecutionException {
|
---|
869 | translationManagerMojo.templateOneTranslation = null;
|
---|
870 | Assert.assertNull("templateOneTranslation should be null",
|
---|
871 | translationManagerMojo.getTemplateOneTranslation());
|
---|
872 |
|
---|
873 | String s = "a_non_existing_template";
|
---|
874 | try {
|
---|
875 | translationManagerMojo.setTemplateOneTranslation(s);
|
---|
876 | Assert
|
---|
877 | .fail("Call to setTemplateOneTranslation must be done with a valid file as an argument");
|
---|
878 | } catch (MojoExecutionException e) {
|
---|
879 | // Success!
|
---|
880 | }
|
---|
881 |
|
---|
882 | s = "src/test/resources/one_translation.template";
|
---|
883 | translationManagerMojo.setTemplateOneTranslation(s);
|
---|
884 | Assert.assertEquals("Test 1", s,
|
---|
885 | translationManagerMojo.templateOneTranslation);
|
---|
886 | Assert.assertEquals("Test 1", s, translationManagerMojo
|
---|
887 | .getTemplateOneTranslation());
|
---|
888 |
|
---|
889 | translationManagerMojo.setTemplateOneTranslation(null);
|
---|
890 | Assert.assertNull("templateOneTranslation should be null",
|
---|
891 | translationManagerMojo.templateOneTranslation);
|
---|
892 | }
|
---|
893 | }
|
---|