From bffd2c49f2950160c24e462fb5b80a4f4140b710 Mon Sep 17 00:00:00 2001 From: graemerocher Date: Fri, 10 Feb 2012 10:14:59 +0100 Subject: [PATCH] Don't apply TestFor if the class is not a junit 4, junit 3 or spock test --- .../injection/test/TestForTransformation.java | 74 ++++++++++--------- .../grails/test/mixin/TestForSpec.groovy | 39 ++++++++++ 2 files changed, 79 insertions(+), 34 deletions(-) diff --git a/grails-plugin-testing/src/main/groovy/org/codehaus/groovy/grails/compiler/injection/test/TestForTransformation.java b/grails-plugin-testing/src/main/groovy/org/codehaus/groovy/grails/compiler/injection/test/TestForTransformation.java index ca3c576ea69..eca622db7e8 100644 --- a/grails-plugin-testing/src/main/groovy/org/codehaus/groovy/grails/compiler/injection/test/TestForTransformation.java +++ b/grails-plugin-testing/src/main/groovy/org/codehaus/groovy/grails/compiler/injection/test/TestForTransformation.java @@ -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 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 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 } } } diff --git a/grails-plugin-testing/src/test/groovy/grails/test/mixin/TestForSpec.groovy b/grails-plugin-testing/src/test/groovy/grails/test/mixin/TestForSpec.groovy index e2a78de4820..560d07761b4 100644 --- a/grails-plugin-testing/src/test/groovy/grails/test/mixin/TestForSpec.groovy +++ b/grails-plugin-testing/src/test/groovy/grails/test/mixin/TestForSpec.groovy @@ -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('''