|
| 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