|
| 1 | +/* |
| 2 | + * Copyright 2013 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package grails.test.mixin |
| 18 | + |
| 19 | +import org.apache.tools.ant.BuildException |
| 20 | +import org.apache.tools.ant.Project |
| 21 | +import org.apache.tools.ant.types.Path |
| 22 | +import org.codehaus.groovy.grails.test.compiler.GrailsIntegrationTestCompiler |
| 23 | +import org.junit.rules.TemporaryFolder |
| 24 | +import spock.lang.Specification |
| 25 | +import spock.lang.Unroll |
| 26 | + |
| 27 | +import java.lang.reflect.Field |
| 28 | + |
| 29 | +/** |
| 30 | + * Tests for {@link org.codehaus.groovy.grails.compiler.injection.test.IntegrationTestMixinTransformation} |
| 31 | + * to verify that it only transforms the right classes and ignores the rest. |
| 32 | + * |
| 33 | + * @author patrick.jungermann |
| 34 | + * @since 12/01/16 |
| 35 | + * @see <a href="https://github.com/grails/grails-core/issues/9552">https://github.com/grails/grails-core/issues/9552</a> |
| 36 | + * @see <a href="https://github.com/grails/grails-core/issues/9553">https://github.com/grails/grails-core/issues/9553</a> |
| 37 | + */ |
| 38 | +@Unroll |
| 39 | +class IntegrationTestMixinTransformationSpec extends Specification { |
| 40 | + |
| 41 | + @SuppressWarnings("GroovyPointlessBoolean") |
| 42 | + void "IntegrationTestMixin should get applied to #type? #toBeApplied (class: #clazz)"(String type, Class clazz, boolean toBeApplied) { |
| 43 | + expect: |
| 44 | + clazz != null // class loading failed otherwise |
| 45 | + Field field = getDeclaredField(clazz, '$integrationTestMixin') |
| 46 | + toBeApplied == (field != null) |
| 47 | + |
| 48 | + where: |
| 49 | + type | clazz || toBeApplied |
| 50 | + "class" | getJunit4TestClass() || true |
| 51 | + "interface" | getInterfaceClass() || false |
| 52 | + "trait" | getTraitClass() || false |
| 53 | + "annotation" | getAnnotationClass() || false |
| 54 | + "inner class" | getInnerClassOfJunit4TestClass() || false |
| 55 | + "static inner class" | getStaticInnerClassOfJunit4TestClass() || false |
| 56 | + "anonymous class" | getAnonymousClassOfJunit4TestClass() || false |
| 57 | + } |
| 58 | + |
| 59 | + Field getDeclaredField(Class clazz, String name) { |
| 60 | + try { |
| 61 | + return clazz.getDeclaredField(name) |
| 62 | + |
| 63 | + } catch (NoSuchFieldException ignore) { |
| 64 | + return null |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + Class getJunit4TestClass() { |
| 69 | + return compileClass('MyJunit4Test', ''' |
| 70 | +class MyJunit4Test { |
| 71 | +
|
| 72 | + @org.junit.Test |
| 73 | + void testSomething() { |
| 74 | + grailsApplication != null |
| 75 | + applicationContext != null |
| 76 | + callMe() |
| 77 | + } |
| 78 | +} |
| 79 | +''') |
| 80 | + } |
| 81 | + |
| 82 | + Class getInterfaceClass() { |
| 83 | + return compileClass('MyInterface', ''' |
| 84 | +interface MyInterface { |
| 85 | +
|
| 86 | + void something() |
| 87 | +} |
| 88 | +''') |
| 89 | + } |
| 90 | + |
| 91 | + Class getTraitClass() { |
| 92 | + return compileClass('MyTrait', ''' |
| 93 | +trait MyTrait { |
| 94 | +
|
| 95 | + void something() { |
| 96 | + println "something" |
| 97 | + } |
| 98 | +} |
| 99 | +''') |
| 100 | + } |
| 101 | + |
| 102 | + Class getAnnotationClass() { |
| 103 | + return compileClass('MyAnnotation', ''' |
| 104 | +public @interface MyAnnotation { |
| 105 | +} |
| 106 | +''') |
| 107 | + } |
| 108 | + |
| 109 | + Class getInnerClassOfJunit4TestClass() { |
| 110 | + return compileClass('MyJunit4Test', ''' |
| 111 | +class MyJunit4Test { |
| 112 | +
|
| 113 | + @org.junit.Test |
| 114 | + void testSomething() { |
| 115 | + new InnerClass() |
| 116 | + } |
| 117 | +
|
| 118 | + class InnerClass {} |
| 119 | +} |
| 120 | +''', 'InnerClass') |
| 121 | + } |
| 122 | + |
| 123 | + Class getStaticInnerClassOfJunit4TestClass() { |
| 124 | + return compileClass('MyJunit4Test', ''' |
| 125 | +class MyJunit4Test { |
| 126 | +
|
| 127 | + @org.junit.Test |
| 128 | + void testSomething() { |
| 129 | + new StaticInnerClass() |
| 130 | + } |
| 131 | +
|
| 132 | + static class StaticInnerClass {} |
| 133 | +} |
| 134 | +''', 'StaticInnerClass') |
| 135 | + } |
| 136 | + |
| 137 | + Class getAnonymousClassOfJunit4TestClass() { |
| 138 | + return compileClass('MyJunit4Test', ''' |
| 139 | +class MyJunit4Test { |
| 140 | +
|
| 141 | + Runnable anonymousClassInstance = new Runnable() { |
| 142 | +
|
| 143 | + @Override |
| 144 | + void run() { |
| 145 | + } |
| 146 | + } |
| 147 | +
|
| 148 | + @org.junit.Test |
| 149 | + void testSomething() { |
| 150 | + grailsApplication != null |
| 151 | + applicationContext != null |
| 152 | + callMe() |
| 153 | + } |
| 154 | +} |
| 155 | +''', '1') |
| 156 | + } |
| 157 | + |
| 158 | + Class compileClass(String name, String content, String innerClassToLoad = null) { |
| 159 | + // @Rule creation happens after the data table creation |
| 160 | + TemporaryFolder temp = new TemporaryFolder() |
| 161 | + temp.create() |
| 162 | + try { |
| 163 | + File srcDir = temp.newFolder() |
| 164 | + File destDir = temp.newFolder() |
| 165 | + File source = new File(srcDir, name + ".groovy") |
| 166 | + source.createNewFile() |
| 167 | + source.write(content, "UTF-8") |
| 168 | + |
| 169 | + GrailsIntegrationTestCompiler compiler = new GrailsIntegrationTestCompiler() |
| 170 | + compiler.setProject(new Project()) |
| 171 | + Path srcDirPath = new Path(compiler.getProject()) |
| 172 | + srcDirPath.setLocation(srcDir) |
| 173 | + compiler.setSrcdir(srcDirPath) |
| 174 | + compiler.setDestdir(destDir) |
| 175 | + |
| 176 | + compiler.execute() |
| 177 | + |
| 178 | + GroovyClassLoader cl = new GroovyClassLoader() |
| 179 | + cl.addClasspath(destDir.canonicalPath) |
| 180 | + |
| 181 | + Class clazz = cl.loadClass(name) |
| 182 | + if (innerClassToLoad) { |
| 183 | + clazz = cl.loadClass(name + '$' + innerClassToLoad) |
| 184 | + } |
| 185 | + return clazz |
| 186 | + |
| 187 | + } catch (BuildException ignore) { |
| 188 | + // usually caused by org.codehaus.groovy.control.MultipleCompilationErrorsException |
| 189 | + return null |
| 190 | + |
| 191 | + } finally { |
| 192 | + temp.delete() |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments