Skip to content

Commit d3aa0f0

Browse files
committed
Document new feature scoped interceptors
1 parent 9268a9d commit d3aa0f0

File tree

2 files changed

+91
-36
lines changed

2 files changed

+91
-36
lines changed

docs/extensions.adoc

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -898,45 +898,16 @@ interceptor`. If you attach your interceptor to both of them and need a differen
898898
can of course build two different interceptors or add a parameter to your interceptor and create two instances, telling
899899
each at addition time whether it is attached to the method interceptor or the other one.
900900

901+
[source,groovy,indent=0]
902+
----
903+
include::{sourcedir}/extension/InterceptorSpec.groovy[tag=interceptor-class]
904+
----
901905
.Add All Interceptors
902-
[source,groovy]
906+
[source,groovy,indent=0]
903907
----
904-
class I extends AbstractMethodInterceptor { I(def s) {} }
905-
906-
// DISCLAIMER: The following shows all possible injection points that you could use
907-
// depending on need and situation. You should normally not need to
908-
// register a listener to all these places.
909-
//
910-
// Also, when building an annotation driven local extension, you should
911-
// consider where you want the effects to be present, for example only
912-
// for the features in the same class (specInfo.features), or for features
913-
// in the same and superclasses (specInfo.allFeatures), or also for
914-
// features in subclasses (specInfo.bottomSpec.allFeatures), and so on.
915-
916-
// on SpecInfo
917-
specInfo.specsBottomToTop*.addSharedInitializerInterceptor new I('shared initializer')
918-
specInfo.allSharedInitializerMethods*.addInterceptor new I('shared initializer method')
919-
specInfo.addInterceptor new I('specification')
920-
specInfo.specsBottomToTop*.addSetupSpecInterceptor new I('setup spec')
921-
specInfo.allSetupSpecMethods*.addInterceptor new I('setup spec method')
922-
specInfo.allFeatures*.addInterceptor new I('feature')
923-
specInfo.specsBottomToTop*.addInitializerInterceptor new I('initializer')
924-
specInfo.allInitializerMethods*.addInterceptor new I('initializer method')
925-
specInfo.allFeatures*.addIterationInterceptor new I('iteration')
926-
specInfo.specsBottomToTop*.addSetupInterceptor new I('setup')
927-
specInfo.allSetupMethods*.addInterceptor new I('setup method')
928-
specInfo.allFeatures*.featureMethod*.addInterceptor new I('feature method')
929-
specInfo.specsBottomToTop*.addCleanupInterceptor new I('cleanup')
930-
specInfo.allCleanupMethods*.addInterceptor new I('cleanup method')
931-
specInfo.specsBottomToTop*.addCleanupSpecInterceptor new I('cleanup spec')
932-
specInfo.allCleanupSpecMethods*.addInterceptor new I('cleanup spec method')
933-
specInfo.allFixtureMethods*.addInterceptor new I('fixture method')
934-
935-
// on FeatureInfo (already included above, handling all features)
936-
featureInfo.addInterceptor new I('feature')
937-
featureInfo.addIterationInterceptor new I('iteration')
938-
featureInfo.featureMethod.addInterceptor new I('feature method')
908+
include::{sourcedir}/extension/InterceptorSpec.groovy[tag=interceptor-register-spec]
939909
910+
include::{sourcedir}/extension/InterceptorSpec.groovy[tag=interceptor-register-feature]
940911
----
941912

942913
==== Injecting Method Parameters
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.spockframework.docs.extension
2+
3+
import org.spockframework.runtime.extension.AbstractMethodInterceptor
4+
import org.spockframework.runtime.extension.ExtensionAnnotation
5+
import org.spockframework.runtime.extension.IAnnotationDrivenExtension
6+
import org.spockframework.runtime.model.FeatureInfo
7+
import org.spockframework.runtime.model.SpecInfo
8+
import spock.lang.Specification
9+
10+
import java.lang.annotation.ElementType
11+
import java.lang.annotation.Retention
12+
import java.lang.annotation.RetentionPolicy
13+
import java.lang.annotation.Target
14+
15+
@InterceptorDemo
16+
class InterceptorSpec extends Specification {
17+
@InterceptorDemo
18+
def "a method"() {
19+
expect: true
20+
}
21+
}
22+
23+
// tag::interceptor-class[]
24+
class I extends AbstractMethodInterceptor {
25+
I(def s) {}
26+
}
27+
// end::interceptor-class[]
28+
29+
@Retention(RetentionPolicy.RUNTIME)
30+
@Target([ElementType.TYPE, ElementType.METHOD])
31+
@ExtensionAnnotation(InterceptorDemoExtension)
32+
@interface InterceptorDemo {}
33+
34+
class InterceptorDemoExtension implements IAnnotationDrivenExtension<InterceptorDemo> {
35+
@Override
36+
void visitSpecAnnotation(InterceptorDemo annotation, SpecInfo specInfo) {
37+
// tag::interceptor-register-spec[]
38+
// DISCLAIMER: The following shows all possible injection points that you could use
39+
// depending on need and situation. You should normally not need to
40+
// register a listener to all these places.
41+
//
42+
// Also, when building an annotation driven local extension, you should
43+
// consider where you want the effects to be present, for example only
44+
// for the features in the same class (specInfo.features), or for features
45+
// in the same and superclasses (specInfo.allFeatures), or also for
46+
// features in subclasses (specInfo.bottomSpec.allFeatures), and so on.
47+
48+
// on SpecInfo
49+
specInfo.specsBottomToTop*.addSharedInitializerInterceptor new I('shared initializer')
50+
specInfo.allSharedInitializerMethods*.addInterceptor new I('shared initializer method')
51+
specInfo.addInterceptor new I('specification')
52+
specInfo.specsBottomToTop*.addSetupSpecInterceptor new I('setup spec')
53+
specInfo.allSetupSpecMethods*.addInterceptor new I('setup spec method')
54+
specInfo.allFeatures*.addInterceptor new I('feature')
55+
specInfo.specsBottomToTop*.addInitializerInterceptor new I('initializer')
56+
specInfo.allInitializerMethods*.addInterceptor new I('initializer method')
57+
specInfo.allFeatures*.addIterationInterceptor new I('iteration')
58+
specInfo.specsBottomToTop*.addSetupInterceptor new I('setup')
59+
specInfo.allSetupMethods*.addInterceptor new I('setup method')
60+
specInfo.allFeatures*.featureMethod*.addInterceptor new I('feature method')
61+
specInfo.specsBottomToTop*.addCleanupInterceptor new I('cleanup')
62+
specInfo.allCleanupMethods*.addInterceptor new I('cleanup method')
63+
specInfo.specsBottomToTop*.addCleanupSpecInterceptor new I('cleanup spec')
64+
specInfo.allCleanupSpecMethods*.addInterceptor new I('cleanup spec method')
65+
specInfo.allFixtureMethods*.addInterceptor new I('fixture method')
66+
// end::interceptor-register-spec[]
67+
}
68+
69+
@Override
70+
void visitFeatureAnnotation(InterceptorDemo annotation, FeatureInfo featureInfo) {
71+
// tag::interceptor-register-feature[]
72+
// on FeatureInfo (already included above, handling all features)
73+
featureInfo.addInterceptor new I('feature')
74+
featureInfo.addIterationInterceptor new I('iteration')
75+
featureInfo.featureMethod.addInterceptor new I('feature method')
76+
77+
// since Spock 2.4 there are also feature scoped interceptors that only apply for a single feature
78+
featureInfo.addInitializerInterceptor new I('feature scoped initializer')
79+
featureInfo.addSetupInterceptor new I('feature scoped setup')
80+
featureInfo.addCleanupInterceptor new I('feature scoped cleanup')
81+
82+
// end::interceptor-register-feature[]
83+
}
84+
}

0 commit comments

Comments
 (0)