Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@

package org.spockframework.runtime.extension.builtin;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.LinkedList;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -115,6 +119,8 @@ static public boolean isPotentialMethod(Method method) {
}

static public void addInterceptors(SpecInfo spec) {
Map<FixtureType, Set<String>> appliedMethods=new HashMap<JUnitFixtureMethodsExtension.FixtureType, Set<String>>();

for (SpecInfo currentSpec : spec.getSpecsBottomToTop()) {
List<Method> potentialMethods = new LinkedList<Method>();
for (Method method : currentSpec.getReflection().getDeclaredMethods()) {
Expand All @@ -125,7 +131,21 @@ static public void addInterceptors(SpecInfo spec) {

if (!potentialMethods.isEmpty()) {
for (FixtureType fixtureType : FixtureType.values()) {
fixtureType.addInterceptor(currentSpec, potentialMethods);
Set<String> appliedMethodsForFixtureType = appliedMethods.get(fixtureType);
if(appliedMethodsForFixtureType==null) {
appliedMethodsForFixtureType=new HashSet<String>();
appliedMethods.put(fixtureType, appliedMethodsForFixtureType);
}
List<Method> filteredMethods = new LinkedList<Method>();
for (Method method : potentialMethods) {
if(!appliedMethodsForFixtureType.contains(method.getName())) {
filteredMethods.add(method);
appliedMethodsForFixtureType.add(method.getName());
}
}
if (!filteredMethods.isEmpty()) {
fixtureType.addInterceptor(currentSpec, filteredMethods);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,30 @@ class JUnitFixtureMethods extends EmbeddedSpecification {
"afterClass" | ["child", "parent"]
}

@Unroll("inheritance with shadowed methods for #fixtureType")
def "inheritance with shadowed methods"() {
when:
run """
abstract class Parent extends Specification {
$RECORD_INVOCATION_METHOD

${this."$fixtureType"("$fixtureType","${fixtureType}_parent")}
}

class Child extends Parent {
${this."$fixtureType"("$fixtureType","${fixtureType}_child")}

def feature1() { expect: true }
}
"""

then:
invocations == [fixtureType + '_child']

where:
fixtureType << ["beforeClass", "before", "after", "afterClass"]
}

@Unroll("invalid signature ignored because - #invalidBecause")
def "invalid signatures are ignored"() {
when:
Expand Down Expand Up @@ -155,10 +179,10 @@ class JUnitFixtureMethods extends EmbeddedSpecification {
"afterClass" | "@AfterClass static" | false | 0
}

protected beforeClass(name = "beforeClass") { "@BeforeClass static void $name() { record('$name') }" }
protected before(name = "before") { "@Before void $name() { record('$name') }" }
protected after(name = "after") { "@After void $name() { record('$name') } "}
protected afterClass(name = "afterClass") { "@AfterClass static void $name() { record('$name') } "}
protected beforeClass(name = "beforeClass", recordName = null) { "@BeforeClass static void $name() { record('${recordName?:name}') }" }
protected before(name = "before", recordName = null) { "@Before void $name() { record('${recordName?:name}') }" }
protected after(name = "after", recordName = null) { "@After void $name() { record('${recordName?:name}') } "}
protected afterClass(name = "afterClass", recordName = null) { "@AfterClass static void $name() { record('${recordName?:name}') } "}

protected addImports() {
runner.addPackageImport(getClass().package)
Expand Down