Skip to content

Commit

Permalink
grails#157 - feedback - cleanup file from java import
Browse files Browse the repository at this point in the history
  • Loading branch information
jdaugherty committed Sep 26, 2024
1 parent 71fcea4 commit 3b41a55
Showing 1 changed file with 28 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package org.grails.testing.spock

import grails.testing.spock.OnceBefore
import grails.testing.spring.AutowiredTest
import groovy.transform.CompileStatic
import org.grails.testing.GrailsUnitTest
import org.junit.After
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
import org.spockframework.runtime.extension.AbstractGlobalExtension
import org.spockframework.runtime.extension.IGlobalExtension
import org.spockframework.runtime.model.MethodInfo
import org.spockframework.runtime.model.MethodKind
import org.spockframework.runtime.model.SpecInfo
Expand All @@ -19,7 +17,7 @@ import java.lang.reflect.Method
import java.lang.reflect.Modifier

@CompileStatic
class TestingSupportExtension extends AbstractGlobalExtension {
class TestingSupportExtension implements IGlobalExtension {

AutowiredInterceptor autowiredInterceptor = new AutowiredInterceptor()
CleanupContextInterceptor cleanupContextInterceptor = new CleanupContextInterceptor()
Expand All @@ -32,48 +30,49 @@ class TestingSupportExtension extends AbstractGlobalExtension {
if (GrailsUnitTest.isAssignableFrom(spec.reflection)) {
spec.addCleanupSpecInterceptor(cleanupContextInterceptor)
}
for (Method method : spec.getReflection().getDeclaredMethods()) {
for (Method method : (spec.getReflection().declaredMethods)) {
if (method.isAnnotationPresent(BeforeEach.class)) {
spec.getSetupMethods().add(0, createJUnitFixtureMethod(spec, method, MethodKind.SETUP, BeforeEach.class));
spec.setupMethods.add(0, createJUnitFixtureMethod(spec, method, MethodKind.SETUP, BeforeEach.class))
}
if (method.isAnnotationPresent(AfterEach.class)) {
spec.addCleanupMethod(createJUnitFixtureMethod(spec, method, MethodKind.CLEANUP, AfterEach.class));
spec.addCleanupMethod(createJUnitFixtureMethod(spec, method, MethodKind.CLEANUP, AfterEach.class))
}
if (method.isAnnotationPresent(BeforeAll.class)) {
spec.getSetupSpecMethods().add(0, createJUnitFixtureMethod(spec, method, MethodKind.SETUP_SPEC, BeforeAll.class));
spec.setupSpecMethods.add(0, createJUnitFixtureMethod(spec, method, MethodKind.SETUP_SPEC, BeforeAll.class))
}
if (method.isAnnotationPresent(AfterAll.class)) {
spec.addCleanupSpecMethod(createJUnitFixtureMethod(spec, method, MethodKind.CLEANUP_SPEC, AfterAll.class));
spec.addCleanupSpecMethod(createJUnitFixtureMethod(spec, method, MethodKind.CLEANUP_SPEC, AfterAll.class))
}
}
}

private MethodInfo createMethod(SpecInfo specInfo, Method method, MethodKind kind, String name) {
MethodInfo methodInfo = new MethodInfo();
methodInfo.setParent(specInfo);
methodInfo.setName(name);
methodInfo.setReflection(method);
methodInfo.setKind(kind);
return methodInfo;
private static MethodInfo createMethod(SpecInfo specInfo, Method method, MethodKind kind, String name) {
MethodInfo methodInfo = new MethodInfo()
methodInfo.parent = specInfo
methodInfo.name = name
methodInfo.reflection = method
methodInfo.kind = kind
return methodInfo
}

private MethodInfo createJUnitFixtureMethod(SpecInfo specInfo, Method method, MethodKind kind, Class<? extends Annotation> annotation) {
MethodInfo methodInfo = createMethod(specInfo, method, kind, method.getName());
methodInfo.setExcluded(isOverriddenJUnitFixtureMethod(specInfo, method, annotation));
return methodInfo;
private static MethodInfo createJUnitFixtureMethod(SpecInfo specInfo, Method method, MethodKind kind, Class<? extends Annotation> annotation) {
MethodInfo methodInfo = createMethod(specInfo, method, kind, method.name)
methodInfo.excluded = isOverriddenJUnitFixtureMethod(specInfo, method, annotation)
return methodInfo
}
private boolean isOverriddenJUnitFixtureMethod(SpecInfo specInfo, Method method, Class<? extends Annotation> annotation) {
if (Modifier.isPrivate(method.getModifiers())) return false;

for (Class<?> currClass = specInfo.class; currClass != specInfo.class.superclass; currClass = currClass.getSuperclass()) {
for (Method currMethod : currClass.getDeclaredMethods()) {
if (!currMethod.isAnnotationPresent(annotation)) continue;
if (!currMethod.getName().equals(method.getName())) continue;
if (!Arrays.deepEquals(currMethod.getParameterTypes(), method.getParameterTypes())) continue;
return true;
private static boolean isOverriddenJUnitFixtureMethod(SpecInfo specInfo, Method method, Class<? extends Annotation> annotation) {
if (Modifier.isPrivate(method.modifiers)) return false

for (Class<?> currClass = specInfo.class; currClass != specInfo.class.superclass; currClass = currClass.superclass) {
for (Method currMethod : currClass.declaredMethods) {
if (!currMethod.isAnnotationPresent(annotation)) continue
if (currMethod.name != method.name) continue
if (!Arrays.deepEquals(currMethod.parameterTypes, method.parameterTypes)) continue
return true
}
}

return false;
return false
}
}

0 comments on commit 3b41a55

Please sign in to comment.