Skip to content

Commit

Permalink
Don't apply TestFor if the class is not a junit 4, junit 3 or spock test
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher committed Feb 10, 2012
1 parent f731651 commit bffd2c4
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 34 deletions.
Expand Up @@ -122,52 +122,58 @@ public void visit(ASTNode[] astNodes, SourceUnit source) {
}

ClassNode classNode = (ClassNode) parent;
String cName = classNode.getName();
if (classNode.isInterface()) {
error(source, "Error processing interface '" + cName + "'. " + MY_TYPE_NAME + " not allowed for interfaces.");
if (classNode.isInterface() || Modifier.isAbstract(classNode.getModifiers())) {
return;
}

boolean junit3Test = isJunit3Test(classNode);
boolean spockTest = isSpockTest(classNode);
boolean isJunit = classNode.getName().endsWith("Tests");

if(!junit3Test && !spockTest && !isJunit) return;

Expression value = node.getMember("value");
ClassExpression ce;
if (value instanceof ClassExpression) {
ce = (ClassExpression) value;
testFor(classNode, ce);
}
else if (!isJunit3Test(classNode)){
List<AnnotationNode> annotations = classNode.getAnnotations(MY_TYPE);
if (annotations.size()>0) return; // bail out, in this case it was already applied as a local transform
// no explicit class specified try by convention
String fileName = source.getName();
String className = GrailsResourceUtils.getClassName(new FileSystemResource(fileName));
if (className != null) {
boolean isJunit = className.endsWith("Tests");
boolean isSpock = className.endsWith("Spec");
String targetClassName = null;

if (isJunit) {
targetClassName = className.substring(0, className.indexOf("Tests"));
}
else if (isSpock) {
targetClassName = className.substring(0, className.indexOf("Spec"));
}
else {
if (!junit3Test){
List<AnnotationNode> annotations = classNode.getAnnotations(MY_TYPE);
if (annotations.size()>0) return; // bail out, in this case it was already applied as a local transform
// no explicit class specified try by convention
String fileName = source.getName();
String className = GrailsResourceUtils.getClassName(new FileSystemResource(fileName));
if (className != null) {
boolean isSpock = className.endsWith("Spec");
String targetClassName = null;

if (isJunit) {
targetClassName = className.substring(0, className.indexOf("Tests"));
}
else if (isSpock) {
targetClassName = className.substring(0, className.indexOf("Spec"));
}

if (targetClassName != null) {
Resource targetResource = getResourceLocator().findResourceForClassName(targetClassName);
if (targetResource != null) {
try {
if (GrailsResourceUtils.isDomainClass(targetResource.getURL())) {
testFor(classNode, new ClassExpression(new ClassNode(targetClassName, 0, ClassHelper.OBJECT_TYPE)));
}
else {
for (String artefactType : artefactTypeToTestMap.keySet()) {
if (classNode.getName().endsWith(artefactType)) {
testFor(classNode, new ClassExpression(new ClassNode(targetClassName, 0, ClassHelper.OBJECT_TYPE)));
break;
if (targetClassName != null) {
Resource targetResource = getResourceLocator().findResourceForClassName(targetClassName);
if (targetResource != null) {
try {
if (GrailsResourceUtils.isDomainClass(targetResource.getURL())) {
testFor(classNode, new ClassExpression(new ClassNode(targetClassName, 0, ClassHelper.OBJECT_TYPE)));
}
else {
for (String artefactType : artefactTypeToTestMap.keySet()) {
if (classNode.getName().endsWith(artefactType)) {
testFor(classNode, new ClassExpression(new ClassNode(targetClassName, 0, ClassHelper.OBJECT_TYPE)));
break;
}
}
}
} catch (IOException e) {
// ignore
}
} catch (IOException e) {
// ignore
}
}
}
Expand Down
Expand Up @@ -35,7 +35,46 @@ class TestForSpec extends Specification{
test.getClass().getDeclaredMethod("Test index", null).getAnnotation(Test.class) == null
test.retrieveLog() instanceof Log
}

void "Test that TestFor doesn't apply if the test is not JUnit 4, Junit 4 or Spock"() {
when:
def test = invalidTest

then:
test != null
test.getClass().getDeclaredMethod("testBlah", null).getAnnotation(Test.class) == null

when:
test.retrieveLog() instanceof Log

then:
thrown MissingPropertyException

}

def getInvalidTest() {
final gcl = new GroovyClassLoader()
gcl.parseClass('''
class SimpleController {
def index = {
render "Hello"
}
}
''')
gcl.parseClass('''
import grails.test.mixin.*
@TestFor(SimpleController)
class ControllerHelper {
void testBlah() {}
def retrieveLog() { log }
}
''').newInstance()
}

def getSpockTest() {
final gcl = new GroovyClassLoader()
gcl.parseClass('''
Expand Down

0 comments on commit bffd2c4

Please sign in to comment.