001: package org.andromda.translation.ocl.testsuite;
002:
003: import java.io.File;
004:
005: import java.net.URL;
006:
007: import java.util.LinkedHashMap;
008: import java.util.Map;
009: import java.util.Arrays;
010: import java.util.List;
011:
012: import org.andromda.core.common.XmlObjectFactory;
013: import org.apache.commons.lang.StringUtils;
014: import org.apache.log4j.Logger;
015:
016: /**
017: * Finds all translation tests from the current base directory up.
018: *
019: * @author Chad Brandon
020: */
021: public class TranslationTestDiscoverer {
022: private static Logger logger = Logger
023: .getLogger(TranslationTestDiscoverer.class);
024:
025: /**
026: * This is the prefix of translation tests, each translation test must start with this in order to be found.
027: */
028: private static final String TEST_PREFIX = "TranslationTest-";
029:
030: /**
031: * Names of ignored files when file.isDirectory()
032: */
033: private static final List IGNORED_DIRECTORIES = Arrays
034: .asList(new String[] { "CVS", ".svn" });
035:
036: /**
037: * Stores the discovered translation tests.
038: */
039: private Map translationTests = new LinkedHashMap();
040:
041: /**
042: * The shared instance
043: */
044: private static TranslationTestDiscoverer instance;
045:
046: /**
047: * Gets the shared instance of this TranslationTestDiscoverer.
048: *
049: * @return the shared TranslationTestDiscoverer.
050: */
051: public static TranslationTestDiscoverer instance() {
052: if (instance == null) {
053: instance = new TranslationTestDiscoverer();
054: }
055: return instance;
056: }
057:
058: /**
059: * This method discovers all translation tests within the given <code>directory</code>.
060: */
061: public void discoverTests(final String directory) {
062: if (directory == null || directory.trim().length() == 0) {
063: throw new TranslationTestDiscovererException(
064: "The 'directory' "
065: + " was not specified, please specify this value with the location from which to"
066: + " begin the discovery of translation test files");
067: }
068: if (this .translationTests.isEmpty()) {
069: this .discoverTests(new File(directory));
070: }
071: }
072:
073: /**
074: * This method discovers all translation tests within the <code>currentDirectory</code>, it travels down the
075: * directory structure looking for files that have a prefix of 'TranslationTest-'.
076: */
077: private void discoverTests(final File currentDirectory) {
078: try {
079: final String[] files = currentDirectory.list();
080: if (files == null || files.length == 0) {
081: if (logger.isDebugEnabled()) {
082: logger
083: .debug("no files or directories found in directory '"
084: + currentDirectory + "'");
085: }
086: } else {
087: for (int ctr = 0; ctr < files.length; ctr++) {
088: File file = new File(currentDirectory, files[ctr]);
089: if (StringUtils.trimToEmpty(file.getName())
090: .startsWith(TEST_PREFIX)) {
091: final URL testUrl = file.toURL();
092: if (logger.isInfoEnabled()) {
093: logger.info("found translation test --> '"
094: + testUrl + "'");
095: }
096:
097: TranslationTest test = (TranslationTest) XmlObjectFactory
098: .getInstance(TranslationTest.class)
099: .getObject(testUrl);
100: test.setUri(testUrl);
101: this .translationTests.put(
102: test.getTranslation(), test);
103: } else if (file.isDirectory()
104: && !IGNORED_DIRECTORIES.contains(file
105: .getName())) {
106: this .discoverTests(file);
107: }
108: }
109: }
110: } catch (final Throwable throwable) {
111: logger.error(throwable);
112: throw new TranslationTestDiscovererException(throwable);
113: }
114: }
115:
116: /**
117: * Returns the TranslationTest for the given <code>translation</code> (if one can be found), otherwise returns
118: * null.
119: *
120: * @param translation the name of the translation
121: * @return TranslationTest
122: */
123: public TranslationTest getTest(String translation) {
124: return (TranslationTest) this .translationTests.get(StringUtils
125: .trimToEmpty(translation));
126: }
127:
128: /**
129: * Returns the discovered translation tests keyed by <code>translation<code>.
130: */
131: public Map getTests() {
132: return this .translationTests;
133: }
134:
135: /**
136: * Shuts down this instance and releases any resources.
137: */
138: public void shutdown() {
139: this.translationTests.clear();
140: instance = null;
141: }
142: }
|