package qat.parser;
// JDK imports
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.StringTokenizer;
// qat imports
import qat.parser.TestFinderInterface;
import qat.parser.ParserInterface;
import qat.common.Common;
/**
* This class is responsible for mapping registered test finders to their corresponding
* parsers.
*/
public class GenericTestFinder extends Object implements TestFinderInterface {
ArrayList<TestFinderInterface> testFinders;
private Properties properties;
/**
* Creates a tesfinder and registers a list of testfinder classes.
* The test finders are discovered from the property Common.DEFAULT_TESTFINDER_CLASSNAMES.
*/
public GenericTestFinder(Properties properties) {
this.properties = properties;
testFinders = new ArrayList<TestFinderInterface>();
String registeredTestFinders = properties.getProperty(Common.TESTFINDER_CLASSNAMES,
Common.DEFAULT_TESTFINDER_CLASSNAMES);
StringTokenizer tokens = new StringTokenizer(registeredTestFinders," ");
while (tokens.hasMoreTokens()) {
try {
Class<?> c = (Class.forName(tokens.nextToken()));
TestFinderInterface tfi = (TestFinderInterface)c.getConstructor((Class[])null).newInstance();
tfi.setProperties(properties);
testFinders.add(tfi);
}
catch (Throwable t) {
t.printStackTrace();
}
}
setProperties(properties);
}
public void setProjectRoot(String rootDirectory) {
for (int i = 0; i < testFinders.size(); i++) {
((TestFinderInterface)testFinders.get(i)).setProjectRoot(rootDirectory);
((TestFinderInterface)testFinders.get(i)).setProperties(properties);
}
}
/**
* Return true if this file is a QASH file.
*/
public boolean isTestFile(File file) {
for (int i = 0; i < testFinders.size(); i++) {
if (((TestFinderInterface)testFinders.get(i)).isTestFile(file))
return true;
}
return false;
}
public ParserInterface getParser(File file) {
ParserInterface parser;
for (int i = 0; i < testFinders.size(); i++) {
if ((parser=((TestFinderInterface)testFinders.get(i)).getParser(file))!=null) {
try {
parser.setTestPath(file.getCanonicalPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
parser.setProperties(getProperties());
return parser;
}
}
return null;
}
/**
* This method sets any default properties which will be required
* for parsing this file.
*/
public void setProperties(java.util.Properties p) {
properties = p;
for (int i = 0; i < testFinders.size(); i++) {
((TestFinderInterface)testFinders.get(i)).setProperties(p);
}
}
/**
* This method returns all the properties obtained by parsing this test file.
*/
public java.util.Properties getProperties() {
return properties;
}
public String getProperty(String name) {
return properties.getProperty(name);
}
public String getProperty(String name, String defaultValue) {
return properties.getProperty(name,defaultValue);
}
}