Skip to content

Commit ef87ee7

Browse files
committed
Merge branch '3.0.x' into 3.1.x
2 parents 0af7d4c + 011e5d5 commit ef87ee7

File tree

3 files changed

+73
-15
lines changed

3 files changed

+73
-15
lines changed

grails-core/src/main/groovy/org/grails/transaction/transform/TransactionalTransform.groovy

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class TransactionalTransform implements ASTTransformation{
7575
private static final String METHOD_EXECUTE = "execute"
7676
private static final Set<String> METHOD_NAME_EXCLUDES = new HashSet<String>(Arrays.asList("afterPropertiesSet", "destroy"));
7777
private static final Set<String> ANNOTATION_NAME_EXCLUDES = new HashSet<String>(Arrays.asList(PostConstruct.class.getName(), PreDestroy.class.getName(), Transactional.class.getName(), Rollback.class.getName(), "grails.web.controllers.ControllerMethod", NotTransactional.class.getName()));
78+
private static final Set<String> JUNIT_ANNOTATION_NAMES = new HashSet<String>(Arrays.asList("org.junit.Before", "org.junit.After"));
7879
private static final String SPEC_CLASS = "spock.lang.Specification";
7980
public static final String PROPERTY_DATA_SOURCE = "datasource"
8081

@@ -116,7 +117,8 @@ class TransactionalTransform implements ASTTransformation{
116117
for (MethodNode md in methods) {
117118
String methodName = md.getName()
118119
int modifiers = md.modifiers
119-
if (!md.isSynthetic() && Modifier.isPublic(modifiers) && !Modifier.isAbstract(modifiers) && !Modifier.isStatic(modifiers)) {
120+
if (!md.isSynthetic() && Modifier.isPublic(modifiers) && !Modifier.isAbstract(modifiers) &&
121+
!Modifier.isStatic(modifiers) && !hasJunitAnnotation(md)) {
120122
if(hasExcludedAnnotation(md)) continue
121123

122124
def startsWithSpock = methodName.startsWith('$spock')
@@ -138,7 +140,8 @@ class TransactionalTransform implements ASTTransformation{
138140
if(hasAnnotation(md, DelegatingMethod.class)) continue
139141
weaveTransactionalMethod(source, classNode, annotationNode, md);
140142
}
141-
else if(("setup".equals(methodName) || "cleanup".equals(methodName)) && isSpockTest(classNode)) {
143+
else if ((("setup".equals(methodName) || "cleanup".equals(methodName)) && isSpockTest(classNode)) ||
144+
hasJunitAnnotation(md)) {
142145
def requiresNewTransaction = new AnnotationNode(annotationNode.classNode)
143146
requiresNewTransaction.addMember("propagation", new PropertyExpression(new ClassExpression(ClassHelper.make(Propagation.class)), "REQUIRES_NEW"))
144147
weaveTransactionalMethod(source, classNode, requiresNewTransaction, md, "execute")
@@ -161,6 +164,17 @@ class TransactionalTransform implements ASTTransformation{
161164
excludedAnnotation
162165
}
163166

167+
private boolean hasJunitAnnotation(MethodNode md) {
168+
boolean excludedAnnotation = false;
169+
for (AnnotationNode annotation : md.getAnnotations()) {
170+
if(JUNIT_ANNOTATION_NAMES.contains(annotation.getClassNode().getName())) {
171+
excludedAnnotation = true;
172+
break;
173+
}
174+
}
175+
excludedAnnotation
176+
}
177+
164178
ClassNode getAnnotationClassNode(String annotationName) {
165179
try {
166180
final classLoader = Thread.currentThread().contextClassLoader

grails-core/src/test/groovy/grails/transaction/TransactionalTransformSpec.groovy

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,46 @@ class TransactionalTransformSpec extends Specification {
130130

131131
}
132132

133+
void "Test @Rollback when applied to JUnit specifications"() {
134+
when:
135+
Class mySpec = new GroovyShell().evaluate('''
136+
import grails.transaction.*
137+
import org.junit.Test
138+
import org.junit.Before
139+
import org.junit.After
140+
141+
@Rollback
142+
class MyJunitTest {
143+
@Before
144+
def junitSetup() {
145+
146+
}
147+
148+
@After
149+
def junitCleanup() {
150+
151+
}
152+
153+
@Test
154+
void junitTest() {
155+
expect:
156+
1 == 1
157+
}
158+
}
159+
MyJunitTest
160+
''')
161+
162+
then: "It implements TransactionManagerAware"
163+
TransactionManagerAware.isAssignableFrom(mySpec)
164+
mySpec.getDeclaredMethod('junitSetup')
165+
mySpec.getDeclaredMethod('$tt__junitSetup', TransactionStatus)
166+
mySpec.getDeclaredMethod('junitCleanup')
167+
mySpec.getDeclaredMethod('$tt__junitCleanup', TransactionStatus)
168+
169+
mySpec.getDeclaredMethod('junitTest')
170+
mySpec.getDeclaredMethod('$tt__junitTest', TransactionStatus)
171+
}
172+
133173
void "Test @Rollback when applied to Spock specifications"() {
134174
when: "A new instance of a class with a @Transactional method is created that subclasses another transactional class"
135175
Class mySpec = new GroovyShell().evaluate('''

grails-gradle-plugin/src/main/groovy/org/grails/gradle/plugin/core/GrailsGradlePlugin.groovy

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import org.gradle.api.artifacts.ConfigurationContainer
3434
import org.gradle.api.artifacts.Dependency
3535
import org.gradle.api.artifacts.DependencyResolveDetails
3636
import org.gradle.api.file.CopySpec
37+
import org.gradle.api.file.FileCollection
3738
import org.gradle.api.java.archives.Manifest
3839
import org.gradle.api.plugins.GroovyPlugin
3940
import org.gradle.api.plugins.WarPlugin
@@ -489,7 +490,7 @@ class GrailsGradlePlugin extends GroovyPlugin {
489490

490491
protected void configurePathingJar(Project project) {
491492
project.afterEvaluate {
492-
ConfigurationContainer configurations = project.configurations
493+
ConfigurationContainer configurations = project.configurations
493494
Configuration runtime = configurations.getByName('runtime')
494495
Configuration console = configurations.getByName('console')
495496

@@ -507,25 +508,28 @@ class GrailsGradlePlugin extends GroovyPlugin {
507508
}
508509

509510
Jar pathingJar = createPathingJarTask(project, "pathingJar", runtime)
511+
FileCollection pathingClasspath = project.files("${project.buildDir}/classes/main",
512+
"${project.buildDir}/resources/main", "${project.projectDir}/gsp-classes", pathingJar.archivePath)
510513
Jar pathingJarCommand = createPathingJarTask(project, "pathingJarCommand", runtime, console)
514+
FileCollection pathingClasspathCommand = project.files("${project.buildDir}/classes/main",
515+
"${project.buildDir}/resources/main", "${project.projectDir}/gsp-classes", pathingJarCommand.archivePath)
511516

512517
GrailsExtension grailsExt = project.extensions.getByType(GrailsExtension)
513518

514519
if (grailsExt.pathingJar && Os.isFamily(Os.FAMILY_WINDOWS)) {
515-
516-
TaskContainer tasks = project.tasks
517-
518-
tasks.withType(JavaExec) { JavaExec task ->
519-
task.dependsOn(pathingJar)
520-
task.doFirst {
521-
classpath = project.files("${project.buildDir}/classes/main", "${project.buildDir}/resources/main", "${project.projectDir}/gsp-classes", pathingJar.archivePath)
520+
project.tasks.withType(JavaExec) { JavaExec task ->
521+
if (task.name in ['console', 'shell'] || task instanceof ApplicationContextCommandTask) {
522+
task.dependsOn(pathingJarCommand)
523+
task.doFirst {
524+
classpath = pathingClasspathCommand
525+
}
526+
} else {
527+
task.dependsOn(pathingJar)
528+
task.doFirst {
529+
classpath = pathingClasspath
530+
}
522531
}
523532
}
524-
525-
tasks.withType(ApplicationContextCommandTask) { ApplicationContextCommandTask task ->
526-
task.dependsOn(pathingJarCommand)
527-
task.systemProperties = ['grails.env': 'development']
528-
}
529533
}
530534
}
531535
}

0 commit comments

Comments
 (0)