View Javadoc
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   * Finds all translation tests from the current base directory up.
16   *
17   * @author Chad Brandon
18   */
19  public class TranslationTestDiscoverer
20  {
21      private static final Logger logger = Logger.getLogger(TranslationTestDiscoverer.class);
22  
23      /**
24       * This is the prefix of translation tests, each translation test must start with this in order to be found.
25       */
26      private static final String TEST_PREFIX = "TranslationTest-";
27  
28      /**
29       * Names of ignored files when file.isDirectory()
30       */
31      private static final List<String> IGNORED_DIRECTORIES = Arrays.asList("CVS", ".svn");
32  
33      /**
34       * Stores the discovered translation tests.
35       */
36      private Map<String, TranslationTest> translationTests = new LinkedHashMap<String, TranslationTest>();
37  
38      /**
39       * The shared instance
40       */
41      private static TranslationTestDiscoverer instance;
42  
43      /**
44       * Gets the shared instance of this TranslationTestDiscoverer.
45       *
46       * @return the shared TranslationTestDiscoverer.
47       */
48      public static TranslationTestDiscoverer instance()
49      {
50          if (instance == null)
51          {
52              instance = new TranslationTestDiscoverer();
53          }
54          return instance;
55      }
56  
57      /**
58       * This method discovers all translation tests within the given <code>directory</code>.
59       * @param directory
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       * This method discovers all translation tests within the <code>currentDirectory</code>, it travels down the
77       * directory structure looking for files that have a prefix of 'TranslationTest-'.
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      * Returns the TranslationTest for the given <code>translation</code> (if one can be found), otherwise returns
125      * null.
126      *
127      * @param translation the name of the translation
128      * @return TranslationTest
129      */
130     public TranslationTest getTest(String translation)
131     {
132         return this.translationTests.get(StringUtils.trimToEmpty(translation));
133     }
134 
135     /**
136      * Returns the discovered translation tests keyed by <code>translation<code>.
137      * @return translationTests
138      */
139     public Map<String, TranslationTest> getTests()
140     {
141         return this.translationTests;
142     }
143 
144     /**
145      * Shuts down this instance and releases any resources.
146      */
147     public void shutdown()
148     {
149         this.translationTests.clear();
150         TranslationTestDiscoverer.instance = null;
151     }
152 }