1 package org.andromda.translation.ocl.testsuite;
2
3 import java.io.File;
4 import java.net.URL;
5 import java.util.Arrays;
6 import java.util.LinkedHashMap;
7 import java.util.List;
8 import java.util.Map;
9 import org.andromda.core.common.XmlObjectFactory;
10 import org.apache.commons.lang.StringUtils;
11 import org.apache.log4j.Logger;
12
13
14
15
16
17
18
19 public class TranslationTestDiscoverer
20 {
21 private static final Logger logger = Logger.getLogger(TranslationTestDiscoverer.class);
22
23
24
25
26 private static final String TEST_PREFIX = "TranslationTest-";
27
28
29
30
31 private static final List<String> IGNORED_DIRECTORIES = Arrays.asList("CVS", ".svn");
32
33
34
35
36 private Map<String, TranslationTest> translationTests = new LinkedHashMap<String, TranslationTest>();
37
38
39
40
41 private static TranslationTestDiscoverer instance;
42
43
44
45
46
47
48 public static TranslationTestDiscoverer instance()
49 {
50 if (instance == null)
51 {
52 instance = new TranslationTestDiscoverer();
53 }
54 return instance;
55 }
56
57
58
59
60
61 public void discoverTests(final String directory)
62 {
63 if (StringUtils.isBlank(directory))
64 {
65 throw new TranslationTestDiscovererException("The 'directory' " +
66 " was not specified, please specify this value with the location from which to" +
67 " begin the discovery of translation test files");
68 }
69 if (this.translationTests.isEmpty())
70 {
71 this.discoverTests(new File(directory));
72 }
73 }
74
75
76
77
78
79 private void discoverTests(final File currentDirectory)
80 {
81 try
82 {
83 final String[] files = currentDirectory.list();
84 if (files == null || files.length == 0)
85 {
86 if (logger.isDebugEnabled())
87 {
88 logger.debug("no files or directories found in directory '" + currentDirectory + '\'');
89 }
90 } else
91 {
92 for (String filename : files)
93 {
94 File file = new File(currentDirectory, filename);
95 if (StringUtils.trimToEmpty(file.getName()).startsWith(TEST_PREFIX))
96 {
97 final URL testUrl = file.toURI().toURL();
98 if (logger.isInfoEnabled())
99 {
100 logger.info("found translation test --> '" + testUrl + '\'');
101 }
102
103 TranslationTest test =
104 (TranslationTest) XmlObjectFactory.getInstance(TranslationTest.class).getObject(testUrl);
105 test.setUri(testUrl);
106 this.translationTests.put(
107 test.getTranslation(),
108 test);
109 } else if (file.isDirectory() && !IGNORED_DIRECTORIES.contains(file.getName()))
110 {
111 this.discoverTests(file);
112 }
113 }
114 }
115 }
116 catch (final Throwable throwable)
117 {
118 logger.error(throwable);
119 throw new TranslationTestDiscovererException(throwable);
120 }
121 }
122
123
124
125
126
127
128
129
130 public TranslationTest getTest(String translation)
131 {
132 return this.translationTests.get(StringUtils.trimToEmpty(translation));
133 }
134
135
136
137
138
139 public Map<String, TranslationTest> getTests()
140 {
141 return this.translationTests;
142 }
143
144
145
146
147 public void shutdown()
148 {
149 this.translationTests.clear();
150 TranslationTestDiscoverer.instance = null;
151 }
152 }