Skip to content

Commit

Permalink
Add tests for InvocationContext.getInterceptorBindings()
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladicek committed Jul 17, 2023
1 parent 899d79e commit 7e98a47
Show file tree
Hide file tree
Showing 20 changed files with 432 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.interceptor.InterceptorBinding;

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@InterceptorBinding
@Inherited
@Target({ TYPE, METHOD, CONSTRUCTOR })
@Retention(RUNTIME)
public @interface AroundConstructBinding1 {
class Literal extends AnnotationLiteral<AroundConstructBinding1> implements AroundConstructBinding1 {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.interceptor.InterceptorBinding;

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@InterceptorBinding
@Inherited
@Target({ TYPE, METHOD, CONSTRUCTOR })
@Retention(RUNTIME)
public @interface AroundConstructBinding2 {
class Literal extends AnnotationLiteral<AroundConstructBinding2> implements AroundConstructBinding2 {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;

import jakarta.annotation.Priority;
import jakarta.interceptor.AroundConstruct;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

import java.lang.annotation.Annotation;
import java.util.Set;

@Interceptor
@AroundConstructBinding1
@Priority(100)
public class AroundConstructInterceptor1 {
private static Set<Annotation> allBindings;

@AroundConstruct
public Object intercept(InvocationContext ctx) throws Exception {
allBindings = ctx.getInterceptorBindings();
return ctx.proceed();
}

public static Set<Annotation> getAllBindings() {
return allBindings;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;

import jakarta.annotation.Priority;
import jakarta.interceptor.AroundConstruct;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

import java.lang.annotation.Annotation;
import java.util.Set;

@Interceptor
@AroundConstructBinding2
@Priority(200)
public class AroundConstructInterceptor2 {
private static Set<Annotation> allBindings;

@AroundConstruct
public Object intercept(InvocationContext ctx) throws Exception {
allBindings = ctx.getInterceptorBindings();
return ctx.proceed();
}

public static Set<Annotation> getAllBindings() {
return allBindings;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.interceptor.InterceptorBinding;

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@InterceptorBinding
@Inherited
@Target({ TYPE, METHOD })
@Retention(RUNTIME)
public @interface Binding11 {
class Literal extends AnnotationLiteral<Binding11> implements Binding11 {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.interceptor.InterceptorBinding;

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@InterceptorBinding
@Inherited
@Target({ TYPE, METHOD })
@Retention(RUNTIME)
public @interface Binding12 {
class Literal extends AnnotationLiteral<Binding12> implements Binding12 {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.interceptor.InterceptorBinding;

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@InterceptorBinding
@Inherited
@Target({ TYPE, METHOD })
@Retention(RUNTIME)
public @interface Binding13 {
String value();

class Literal extends AnnotationLiteral<Binding13> implements Binding13 {
private final String value;

public Literal(String value) {
this.value = value;
}

@Override
public String value() {
return value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.enterprise.util.Nonbinding;
import jakarta.interceptor.InterceptorBinding;

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@InterceptorBinding
@Inherited
@Target({ TYPE, METHOD })
@Retention(RUNTIME)
public @interface Binding14 {
@Nonbinding
String value();

class Literal extends AnnotationLiteral<Binding14> implements Binding14 {
private final String value;

public Literal(String value) {
this.value = value;
}

@Override
public String value() {
return value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;

import jakarta.annotation.Priority;
import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

@Interceptor
@Binding11
@Priority(1100)
public class Interceptor11 {
@AroundInvoke
public Object intercept(InvocationContext ctx) throws Exception {
return ctx.proceed();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;

import jakarta.annotation.Priority;
import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

import java.lang.annotation.Annotation;
import java.util.Set;

@Interceptor
@Binding12
@Priority(1200)
public class Interceptor12 {
private static Set<Annotation> allBindings;

private static Set<Binding12> binding12s; // must be non-empty
private static Binding12 binding12; // must be non-null

private static Set<Binding5> binding5s; // must be empty
private static Binding6 binding6; // must be null

@AroundInvoke
public Object intercept(InvocationContext ctx) throws Exception {
allBindings = ctx.getInterceptorBindings();
binding12s = ctx.getInterceptorBindings(Binding12.class);
binding12 = ctx.getInterceptorBinding(Binding12.class);
binding5s = ctx.getInterceptorBindings(Binding5.class);
binding6 = ctx.getInterceptorBinding(Binding6.class);
return ctx.proceed();
}

public static Set<Annotation> getAllBindings() {
return allBindings;
}

public static Set<Binding12> getBinding12s() {
return binding12s;
}

public static Binding12 getBinding12() {
return binding12;
}

public static Set<Binding5> getBinding5s() {
return binding5s;
}

public static Binding6 getBinding6() {
return binding6;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;

import jakarta.annotation.Priority;
import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

@Interceptor
@Binding13("ok")
@Priority(1300)
public class Interceptor13 {
@AroundInvoke
public Object intercept(InvocationContext ctx) throws Exception {
return ctx.proceed();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.jboss.cdi.tck.interceptors.tests.contract.invocationContext;

import jakarta.annotation.Priority;
import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

@Interceptor
@Binding14("")
@Priority(1400)
public class Interceptor14 {
@AroundInvoke
public Object intercept(InvocationContext ctx) throws Exception {
return ctx.proceed();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.jboss.cdi.tck.interceptors.InterceptorsSections.INVOCATIONCONTEXT;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;

Expand All @@ -31,6 +32,8 @@
import org.jboss.test.audit.annotations.SpecVersion;
import org.testng.annotations.Test;

import java.util.Set;

/**
* Tests for the InvocationContext implementation
*
Expand Down Expand Up @@ -122,4 +125,24 @@ public void testBusinessMethodNotCalledWithoutProceedInvocation() {
assertEquals(getContextualReference(SimpleBean.class).echo("foo"), "foo");
assertFalse(SimpleBean.isEchoCalled());
}

@Test
@SpecAssertion(section = INVOCATIONCONTEXT, id = "n")
@SpecAssertion(section = INVOCATIONCONTEXT, id = "o")
public void testGetInterceptorBindings() {
assertTrue(getContextualReference(SimpleBean.class).bindings());
assertEquals(AroundConstructInterceptor1.getAllBindings(), Set.of(new SimplePCBinding.Literal(),
new PseudoBinding.Literal(), new AroundConstructBinding1.Literal(),
new AroundConstructBinding2.Literal()));
assertEquals(AroundConstructInterceptor1.getAllBindings(), AroundConstructInterceptor2.getAllBindings());
assertEquals(PostConstructInterceptor.getAllBindings(), Set.of(new SimplePCBinding.Literal(),
new PseudoBinding.Literal(), new AroundConstructBinding1.Literal()));
assertEquals(Interceptor12.getAllBindings(), Set.of(new SimplePCBinding.Literal(), new PseudoBinding.Literal(),
new AroundConstructBinding1.Literal(), new Binding11.Literal(), new Binding12.Literal(),
new Binding13.Literal("ko"), new Binding14.Literal("foobar")));
assertEquals(Interceptor12.getBinding12s(), Set.of(new Binding12.Literal()));
assertEquals(Interceptor12.getBinding12(), new Binding12.Literal());
assertEquals(Interceptor12.getBinding5s(), Set.of());
assertNull(Interceptor12.getBinding6());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,27 @@
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

import java.lang.annotation.Annotation;
import java.util.Set;

@Interceptor
@SimplePCBinding
@Priority(100)
public class PostConstructInterceptor {
private static boolean getMethodReturnsNull = false;
private static boolean ctxProceedReturnsNull = false;

private static Set<Annotation> allBindings = null;

@PostConstruct
public void postConstruct(InvocationContext ctx) {
getMethodReturnsNull = ctx.getMethod() == null;
try {
ctxProceedReturnsNull = ctx.proceed() == null;
} catch (Exception e) {
}

allBindings = ctx.getInterceptorBindings();
}

public static boolean isGetMethodReturnsNull() {
Expand All @@ -44,4 +51,8 @@ public static boolean isGetMethodReturnsNull() {
public static boolean isCtxProceedReturnsNull() {
return ctxProceedReturnsNull;
}

public static Set<Annotation> getAllBindings() {
return allBindings;
}
}

0 comments on commit 7e98a47

Please sign in to comment.