Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7e98a47

Browse files
committedJul 17, 2023
Add tests for InvocationContext.getInterceptorBindings()
1 parent 899d79e commit 7e98a47

20 files changed

+432
-1
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;
2+
3+
import jakarta.enterprise.util.AnnotationLiteral;
4+
import jakarta.interceptor.InterceptorBinding;
5+
6+
import java.lang.annotation.Inherited;
7+
import java.lang.annotation.Retention;
8+
import java.lang.annotation.Target;
9+
10+
import static java.lang.annotation.ElementType.CONSTRUCTOR;
11+
import static java.lang.annotation.ElementType.METHOD;
12+
import static java.lang.annotation.ElementType.TYPE;
13+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
14+
15+
@InterceptorBinding
16+
@Inherited
17+
@Target({ TYPE, METHOD, CONSTRUCTOR })
18+
@Retention(RUNTIME)
19+
public @interface AroundConstructBinding1 {
20+
class Literal extends AnnotationLiteral<AroundConstructBinding1> implements AroundConstructBinding1 {
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;
2+
3+
import jakarta.enterprise.util.AnnotationLiteral;
4+
import jakarta.interceptor.InterceptorBinding;
5+
6+
import java.lang.annotation.Inherited;
7+
import java.lang.annotation.Retention;
8+
import java.lang.annotation.Target;
9+
10+
import static java.lang.annotation.ElementType.CONSTRUCTOR;
11+
import static java.lang.annotation.ElementType.METHOD;
12+
import static java.lang.annotation.ElementType.TYPE;
13+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
14+
15+
@InterceptorBinding
16+
@Inherited
17+
@Target({ TYPE, METHOD, CONSTRUCTOR })
18+
@Retention(RUNTIME)
19+
public @interface AroundConstructBinding2 {
20+
class Literal extends AnnotationLiteral<AroundConstructBinding2> implements AroundConstructBinding2 {
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;
2+
3+
import jakarta.annotation.Priority;
4+
import jakarta.interceptor.AroundConstruct;
5+
import jakarta.interceptor.Interceptor;
6+
import jakarta.interceptor.InvocationContext;
7+
8+
import java.lang.annotation.Annotation;
9+
import java.util.Set;
10+
11+
@Interceptor
12+
@AroundConstructBinding1
13+
@Priority(100)
14+
public class AroundConstructInterceptor1 {
15+
private static Set<Annotation> allBindings;
16+
17+
@AroundConstruct
18+
public Object intercept(InvocationContext ctx) throws Exception {
19+
allBindings = ctx.getInterceptorBindings();
20+
return ctx.proceed();
21+
}
22+
23+
public static Set<Annotation> getAllBindings() {
24+
return allBindings;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;
2+
3+
import jakarta.annotation.Priority;
4+
import jakarta.interceptor.AroundConstruct;
5+
import jakarta.interceptor.Interceptor;
6+
import jakarta.interceptor.InvocationContext;
7+
8+
import java.lang.annotation.Annotation;
9+
import java.util.Set;
10+
11+
@Interceptor
12+
@AroundConstructBinding2
13+
@Priority(200)
14+
public class AroundConstructInterceptor2 {
15+
private static Set<Annotation> allBindings;
16+
17+
@AroundConstruct
18+
public Object intercept(InvocationContext ctx) throws Exception {
19+
allBindings = ctx.getInterceptorBindings();
20+
return ctx.proceed();
21+
}
22+
23+
public static Set<Annotation> getAllBindings() {
24+
return allBindings;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;
2+
3+
import jakarta.enterprise.util.AnnotationLiteral;
4+
import jakarta.interceptor.InterceptorBinding;
5+
6+
import java.lang.annotation.Inherited;
7+
import java.lang.annotation.Retention;
8+
import java.lang.annotation.Target;
9+
10+
import static java.lang.annotation.ElementType.METHOD;
11+
import static java.lang.annotation.ElementType.TYPE;
12+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
13+
14+
@InterceptorBinding
15+
@Inherited
16+
@Target({ TYPE, METHOD })
17+
@Retention(RUNTIME)
18+
public @interface Binding11 {
19+
class Literal extends AnnotationLiteral<Binding11> implements Binding11 {
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;
2+
3+
import jakarta.enterprise.util.AnnotationLiteral;
4+
import jakarta.interceptor.InterceptorBinding;
5+
6+
import java.lang.annotation.Inherited;
7+
import java.lang.annotation.Retention;
8+
import java.lang.annotation.Target;
9+
10+
import static java.lang.annotation.ElementType.METHOD;
11+
import static java.lang.annotation.ElementType.TYPE;
12+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
13+
14+
@InterceptorBinding
15+
@Inherited
16+
@Target({ TYPE, METHOD })
17+
@Retention(RUNTIME)
18+
public @interface Binding12 {
19+
class Literal extends AnnotationLiteral<Binding12> implements Binding12 {
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;
2+
3+
import jakarta.enterprise.util.AnnotationLiteral;
4+
import jakarta.interceptor.InterceptorBinding;
5+
6+
import java.lang.annotation.Inherited;
7+
import java.lang.annotation.Retention;
8+
import java.lang.annotation.Target;
9+
10+
import static java.lang.annotation.ElementType.METHOD;
11+
import static java.lang.annotation.ElementType.TYPE;
12+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
13+
14+
@InterceptorBinding
15+
@Inherited
16+
@Target({ TYPE, METHOD })
17+
@Retention(RUNTIME)
18+
public @interface Binding13 {
19+
String value();
20+
21+
class Literal extends AnnotationLiteral<Binding13> implements Binding13 {
22+
private final String value;
23+
24+
public Literal(String value) {
25+
this.value = value;
26+
}
27+
28+
@Override
29+
public String value() {
30+
return value;
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;
2+
3+
import jakarta.enterprise.util.AnnotationLiteral;
4+
import jakarta.enterprise.util.Nonbinding;
5+
import jakarta.interceptor.InterceptorBinding;
6+
7+
import java.lang.annotation.Inherited;
8+
import java.lang.annotation.Retention;
9+
import java.lang.annotation.Target;
10+
11+
import static java.lang.annotation.ElementType.METHOD;
12+
import static java.lang.annotation.ElementType.TYPE;
13+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
14+
15+
@InterceptorBinding
16+
@Inherited
17+
@Target({ TYPE, METHOD })
18+
@Retention(RUNTIME)
19+
public @interface Binding14 {
20+
@Nonbinding
21+
String value();
22+
23+
class Literal extends AnnotationLiteral<Binding14> implements Binding14 {
24+
private final String value;
25+
26+
public Literal(String value) {
27+
this.value = value;
28+
}
29+
30+
@Override
31+
public String value() {
32+
return value;
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;
2+
3+
import jakarta.annotation.Priority;
4+
import jakarta.interceptor.AroundInvoke;
5+
import jakarta.interceptor.Interceptor;
6+
import jakarta.interceptor.InvocationContext;
7+
8+
@Interceptor
9+
@Binding11
10+
@Priority(1100)
11+
public class Interceptor11 {
12+
@AroundInvoke
13+
public Object intercept(InvocationContext ctx) throws Exception {
14+
return ctx.proceed();
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;
2+
3+
import jakarta.annotation.Priority;
4+
import jakarta.interceptor.AroundInvoke;
5+
import jakarta.interceptor.Interceptor;
6+
import jakarta.interceptor.InvocationContext;
7+
8+
import java.lang.annotation.Annotation;
9+
import java.util.Set;
10+
11+
@Interceptor
12+
@Binding12
13+
@Priority(1200)
14+
public class Interceptor12 {
15+
private static Set<Annotation> allBindings;
16+
17+
private static Set<Binding12> binding12s; // must be non-empty
18+
private static Binding12 binding12; // must be non-null
19+
20+
private static Set<Binding5> binding5s; // must be empty
21+
private static Binding6 binding6; // must be null
22+
23+
@AroundInvoke
24+
public Object intercept(InvocationContext ctx) throws Exception {
25+
allBindings = ctx.getInterceptorBindings();
26+
binding12s = ctx.getInterceptorBindings(Binding12.class);
27+
binding12 = ctx.getInterceptorBinding(Binding12.class);
28+
binding5s = ctx.getInterceptorBindings(Binding5.class);
29+
binding6 = ctx.getInterceptorBinding(Binding6.class);
30+
return ctx.proceed();
31+
}
32+
33+
public static Set<Annotation> getAllBindings() {
34+
return allBindings;
35+
}
36+
37+
public static Set<Binding12> getBinding12s() {
38+
return binding12s;
39+
}
40+
41+
public static Binding12 getBinding12() {
42+
return binding12;
43+
}
44+
45+
public static Set<Binding5> getBinding5s() {
46+
return binding5s;
47+
}
48+
49+
public static Binding6 getBinding6() {
50+
return binding6;
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;
2+
3+
import jakarta.annotation.Priority;
4+
import jakarta.interceptor.AroundInvoke;
5+
import jakarta.interceptor.Interceptor;
6+
import jakarta.interceptor.InvocationContext;
7+
8+
@Interceptor
9+
@Binding13("ok")
10+
@Priority(1300)
11+
public class Interceptor13 {
12+
@AroundInvoke
13+
public Object intercept(InvocationContext ctx) throws Exception {
14+
return ctx.proceed();
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;
2+
3+
import jakarta.annotation.Priority;
4+
import jakarta.interceptor.AroundInvoke;
5+
import jakarta.interceptor.Interceptor;
6+
import jakarta.interceptor.InvocationContext;
7+
8+
@Interceptor
9+
@Binding14("")
10+
@Priority(1400)
11+
public class Interceptor14 {
12+
@AroundInvoke
13+
public Object intercept(InvocationContext ctx) throws Exception {
14+
return ctx.proceed();
15+
}
16+
}

‎impl/src/main/java/org/jboss/cdi/tck/interceptors/tests/contract/invocationContext/InvocationContextTest.java

+23
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.jboss.cdi.tck.interceptors.InterceptorsSections.INVOCATIONCONTEXT;
2121
import static org.testng.Assert.assertEquals;
2222
import static org.testng.Assert.assertFalse;
23+
import static org.testng.Assert.assertNull;
2324
import static org.testng.Assert.assertSame;
2425
import static org.testng.Assert.assertTrue;
2526

@@ -31,6 +32,8 @@
3132
import org.jboss.test.audit.annotations.SpecVersion;
3233
import org.testng.annotations.Test;
3334

35+
import java.util.Set;
36+
3437
/**
3538
* Tests for the InvocationContext implementation
3639
*
@@ -122,4 +125,24 @@ public void testBusinessMethodNotCalledWithoutProceedInvocation() {
122125
assertEquals(getContextualReference(SimpleBean.class).echo("foo"), "foo");
123126
assertFalse(SimpleBean.isEchoCalled());
124127
}
128+
129+
@Test
130+
@SpecAssertion(section = INVOCATIONCONTEXT, id = "n")
131+
@SpecAssertion(section = INVOCATIONCONTEXT, id = "o")
132+
public void testGetInterceptorBindings() {
133+
assertTrue(getContextualReference(SimpleBean.class).bindings());
134+
assertEquals(AroundConstructInterceptor1.getAllBindings(), Set.of(new SimplePCBinding.Literal(),
135+
new PseudoBinding.Literal(), new AroundConstructBinding1.Literal(),
136+
new AroundConstructBinding2.Literal()));
137+
assertEquals(AroundConstructInterceptor1.getAllBindings(), AroundConstructInterceptor2.getAllBindings());
138+
assertEquals(PostConstructInterceptor.getAllBindings(), Set.of(new SimplePCBinding.Literal(),
139+
new PseudoBinding.Literal(), new AroundConstructBinding1.Literal()));
140+
assertEquals(Interceptor12.getAllBindings(), Set.of(new SimplePCBinding.Literal(), new PseudoBinding.Literal(),
141+
new AroundConstructBinding1.Literal(), new Binding11.Literal(), new Binding12.Literal(),
142+
new Binding13.Literal("ko"), new Binding14.Literal("foobar")));
143+
assertEquals(Interceptor12.getBinding12s(), Set.of(new Binding12.Literal()));
144+
assertEquals(Interceptor12.getBinding12(), new Binding12.Literal());
145+
assertEquals(Interceptor12.getBinding5s(), Set.of());
146+
assertNull(Interceptor12.getBinding6());
147+
}
125148
}

‎impl/src/main/java/org/jboss/cdi/tck/interceptors/tests/contract/invocationContext/PostConstructInterceptor.java

+11
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,27 @@
2121
import jakarta.interceptor.Interceptor;
2222
import jakarta.interceptor.InvocationContext;
2323

24+
import java.lang.annotation.Annotation;
25+
import java.util.Set;
26+
2427
@Interceptor
2528
@SimplePCBinding
2629
@Priority(100)
2730
public class PostConstructInterceptor {
2831
private static boolean getMethodReturnsNull = false;
2932
private static boolean ctxProceedReturnsNull = false;
3033

34+
private static Set<Annotation> allBindings = null;
35+
3136
@PostConstruct
3237
public void postConstruct(InvocationContext ctx) {
3338
getMethodReturnsNull = ctx.getMethod() == null;
3439
try {
3540
ctxProceedReturnsNull = ctx.proceed() == null;
3641
} catch (Exception e) {
3742
}
43+
44+
allBindings = ctx.getInterceptorBindings();
3845
}
3946

4047
public static boolean isGetMethodReturnsNull() {
@@ -44,4 +51,8 @@ public static boolean isGetMethodReturnsNull() {
4451
public static boolean isCtxProceedReturnsNull() {
4552
return ctxProceedReturnsNull;
4653
}
54+
55+
public static Set<Annotation> getAllBindings() {
56+
return allBindings;
57+
}
4758
}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Please sign in to comment.