From 657fa7212c1a57e4e2eeb042018ec196f7362a90 Mon Sep 17 00:00:00 2001 From: Krzysztof Wolny Date: Thu, 27 Oct 2016 17:22:02 +0200 Subject: [PATCH] Search potential collaborators fields in parent classes --- .../SubjectsCollaboratorsUtils.groovy | 20 +++++--- .../SubjectsCollaboratorsUtilsSpec.groovy | 46 +++++++++++++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 src/test/groovy/com/blogspot/toomuchcoding/spock/subjcollabs/SubjectsCollaboratorsUtilsSpec.groovy diff --git a/src/main/groovy/com/blogspot/toomuchcoding/spock/subjcollabs/SubjectsCollaboratorsUtils.groovy b/src/main/groovy/com/blogspot/toomuchcoding/spock/subjcollabs/SubjectsCollaboratorsUtils.groovy index 46f577f..5744755 100755 --- a/src/main/groovy/com/blogspot/toomuchcoding/spock/subjcollabs/SubjectsCollaboratorsUtils.groovy +++ b/src/main/groovy/com/blogspot/toomuchcoding/spock/subjcollabs/SubjectsCollaboratorsUtils.groovy @@ -1,28 +1,36 @@ package com.blogspot.toomuchcoding.spock.subjcollabs +import java.lang.reflect.Field + import groovy.transform.PackageScope import org.spockframework.runtime.extension.IMethodInvocation import spock.lang.Specification -import java.lang.reflect.Field - @PackageScope class SubjectsCollaboratorsUtils { - public static Specification getSpec( IMethodInvocation invocation ) { - ( Specification ) invocation.target.with { + public static Specification getSpec(IMethodInvocation invocation) { + (Specification) invocation.target.with { delegate instanceof Specification ? delegate : invocation.instance } } public static Collection findAllDeclaredFieldsWithAnnotation(Object object, Class... annotatedClasses) { - return object.class.declaredFields.findAll { Field field -> - annotatedClasses.any { Class annotatedClass -> + return getInheritedFields(object.class).findAll {Field field -> + annotatedClasses.any {Class annotatedClass -> return annotatedClass in field.declaredAnnotations*.annotationType() } } } + static Collection getInheritedFields(Class type) { + List fields = new ArrayList() + for (Class c = type; c != null; c = c.superclass) { + fields.addAll(c.declaredFields) + } + return fields + } + public static boolean isFieldSet(Field field, Object target) { try { boolean accessible = field.accessible diff --git a/src/test/groovy/com/blogspot/toomuchcoding/spock/subjcollabs/SubjectsCollaboratorsUtilsSpec.groovy b/src/test/groovy/com/blogspot/toomuchcoding/spock/subjcollabs/SubjectsCollaboratorsUtilsSpec.groovy new file mode 100644 index 0000000..96fe46d --- /dev/null +++ b/src/test/groovy/com/blogspot/toomuchcoding/spock/subjcollabs/SubjectsCollaboratorsUtilsSpec.groovy @@ -0,0 +1,46 @@ +package com.blogspot.toomuchcoding.spock.subjcollabs + +import spock.lang.Specification + + +class SubjectsCollaboratorsUtilsSpec extends Specification { + + def "should find collaborator fields in class with no parent"() { + given: + BaseSpec specClass = new BaseSpec() + + when: + def fields = SubjectsCollaboratorsUtils.findAllDeclaredFieldsWithAnnotation(specClass, Collaborator) + + then: + fields.size() == 1 + fields[0].name == "field1" + } + + def "should find collaborator fields in class with parent class"() { + given: + SpecClass specClass = new SpecClass() + + when: + def fields = SubjectsCollaboratorsUtils.findAllDeclaredFieldsWithAnnotation(specClass, Collaborator) + + then: + fields.size() == 2 + fields.collect({it.getName()}).containsAll(["field1", "field3"]) + } + + class BaseSpec { + @Collaborator + String field1 + + String field2 + } + + class SpecClass extends BaseSpec { + @Collaborator + String field3 + + String field4 + } + +}