Skip to content

Commit

Permalink
fixes to test class detection - GRADLE-713
Browse files Browse the repository at this point in the history
  • Loading branch information
teyckmans committed Nov 16, 2009
1 parent 0447eb9 commit 5b7ad85
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 42 deletions.
Expand Up @@ -33,10 +33,11 @@
* @author Tom Eyckmans
*/
public abstract class AbstractTestFrameworkDetector<T extends TestClassVisitor> implements TestFrameworkDetector {
protected static final String JAVA_LANG = "java/lang";
protected static final String GROOVY_LANG = "groovy/lang";
protected static final String CLASS_FILE_EXT = ".class";

protected static final String TEST_CASE = "junit/framework/TestCase";
protected static final String GROOVY_TEST_CASE = "groovy/util/GroovyTestCase";

private final File testClassesDirectory;
private final FileCollection testClasspath;
private List<File> testClassDirectories;
Expand All @@ -45,10 +46,14 @@ public abstract class AbstractTestFrameworkDetector<T extends TestClassVisitor>

protected TestClassProcessor testClassProcessor;

protected List<String> knownTestCaseClassNames;

protected AbstractTestFrameworkDetector(File testClassesDirectory, FileCollection testClasspath) {
this.testClassesDirectory = testClassesDirectory;
this.testClasspath = testClasspath;
this.superClasses = new HashMap<File, Boolean>();
this.knownTestCaseClassNames = new ArrayList<String>();
addKnownTestCaseClassNames(TEST_CASE, GROOVY_TEST_CASE);
}

public File getTestClassesDirectory() {
Expand All @@ -62,25 +67,22 @@ protected File getSuperTestClassFile(String superClassName) {
if (StringUtils.isEmpty(superClassName)) {
throw new IllegalArgumentException("superClassName is empty!");
}
if (isLangPackageClassName(superClassName)) {
return null; // Object or GroovyObject class reached - no super class that has to be scanned
} else {
final Iterator<File> testClassDirectoriesIt = testClassDirectories.iterator();

File superTestClassFile = null;
while (superTestClassFile == null && testClassDirectoriesIt.hasNext()) {
final File testClassDirectory = testClassDirectoriesIt.next();
final File superTestClassFileCandidate = new File(testClassDirectory, superClassName + ".class");
if (superTestClassFileCandidate.exists()) {
superTestClassFile = superTestClassFileCandidate;
}

final Iterator<File> testClassDirectoriesIt = testClassDirectories.iterator();

File superTestClassFile = null;
while (superTestClassFile == null && testClassDirectoriesIt.hasNext()) {
final File testClassDirectory = testClassDirectoriesIt.next();
final File superTestClassFileCandidate = new File(testClassDirectory, superClassName + ".class");
if (superTestClassFileCandidate.exists()) {
superTestClassFile = superTestClassFileCandidate;
}
}

if (superTestClassFile != null) {
return superTestClassFile;
} else { // super test class file not in test class directories
return classFileExtractionManager.getLibraryClassFile(superClassName);
}
if (superTestClassFile != null) {
return superTestClassFile;
} else { // super test class file not in test class directories
return classFileExtractionManager.getLibraryClassFile(superClassName);
}
}

Expand Down Expand Up @@ -124,10 +126,6 @@ protected TestClassVisitor classVisitor(final File testClassFile) {
return classVisitor;
}

protected boolean isLangPackageClassName(final String className) {
return className.startsWith(JAVA_LANG) || className.startsWith(GROOVY_LANG);
}

protected String classVisitorToClassFilename(final TestClassVisitor classVisitor) {
final StrBuilder classFilenameBuilder = new StrBuilder();

Expand Down Expand Up @@ -179,4 +177,28 @@ public void manualTestClass(String testClassName) {
public void setTestClassProcessor(TestClassProcessor testClassProcessor) {
this.testClassProcessor = testClassProcessor;
}

public void addKnownTestCaseClassNames(String ... knownTestCaseClassNames) {
if ( knownTestCaseClassNames != null && knownTestCaseClassNames.length != 0 ) {
for ( String knownTestCaseClassName : knownTestCaseClassNames ) {
if ( StringUtils.isNotEmpty(knownTestCaseClassName) )
this.knownTestCaseClassNames.add(knownTestCaseClassName.replaceAll("\\.", "/"));
}
}
}

protected boolean isKnownTestCaseClassName(String testCaseClassName) {
boolean isKnownTestCase = false;

if ( StringUtils.isNotEmpty(testCaseClassName) ) {
final Iterator<String> knownTestCaseClassNamesIterator = knownTestCaseClassNames.iterator();
while ( !isKnownTestCase && knownTestCaseClassNamesIterator.hasNext() ) {
final String currentKnownTestCaseClassName = knownTestCaseClassNamesIterator.next();
if ( currentKnownTestCaseClassName.equals(testCaseClassName) )
isKnownTestCase = true;
}
}

return isKnownTestCase;
}
}
Expand Up @@ -29,9 +29,6 @@
public class JUnitDetector extends AbstractTestFrameworkDetector<JUnitTestClassDetecter> {
private static final Logger logger = LoggerFactory.getLogger(JUnitDetector.class);

protected static final String TEST_CASE = "junit/framework/TestCase";
protected static final String GROOVY_TEST_CASE = "groovy/util/GroovyTestCase";

JUnitDetector(File testClassesDirectory, FileCollection testClasspath) {
super(testClassesDirectory, testClasspath);
}
Expand All @@ -48,9 +45,7 @@ protected boolean processTestClass(final File testClassFile, boolean superClass)
if (!isTest) { // scan parent class
final String superClassName = classVisitor.getSuperClassName();

if (isLangPackageClassName(superClassName)) {
isTest = false;
} else if (isTestCaseClassName(superClassName)) {
if (isKnownTestCaseClassName(superClassName)) {
isTest = true;
} else {
final File superClassFile = getSuperTestClassFile(superClassName);
Expand All @@ -60,14 +55,12 @@ protected boolean processTestClass(final File testClassFile, boolean superClass)
} else
logger.debug("test-class-scan : failed to scan parent class {}, could not find the class file", superClassName);
}


}

publishTestClass(isTest, classVisitor, superClass);

return isTest;
}

protected boolean isTestCaseClassName(final String className) {
return TEST_CASE.equals(className) || GROOVY_TEST_CASE.equals(className);
}
}
Expand Up @@ -58,16 +58,12 @@ protected boolean processTestClass(final File testClassFile, boolean superClass)
if (!isTest) {
final String superClassName = classVisitor.getSuperClassName();

if (isLangPackageClassName(superClassName)) {
isTest = false;
} else {
final File superClassFile = getSuperTestClassFile(superClassName);
final File superClassFile = getSuperTestClassFile(superClassName);

if (superClassFile != null) {
isTest = processSuperClass(superClassFile);
} else
logger.debug("test-class-scan : failed to scan parent class {}, could not find the class file", superClassName);
}
if (superClassFile != null) {
isTest = processSuperClass(superClassFile);
} else
logger.debug("test-class-scan : failed to scan parent class {}, could not find the class file", superClassName);
}

publishTestClass(isTest, classVisitor, superClass);
Expand Down
Expand Up @@ -84,6 +84,8 @@ public void visit(
* this visitor is not interested in visiting this annotation.
*/
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
if ("Lorg/testng/annotations/Test;".equals(desc))
test = true;
return new EmptyVisitor();
}

Expand Down

0 comments on commit 5b7ad85

Please sign in to comment.