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 c34f1fb
Show file tree
Hide file tree
Showing 14 changed files with 287 additions and 1 deletion.
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,17 @@ 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(Interceptor12.getAllBindings(), Set.of(new SimplePCBinding.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 @@ -72,6 +72,14 @@ public String echo(String s) {
return s;
}

@Binding11
@Binding12
@Binding13("ko") // does not associate `Interceptor13` with this bean due to different annotation member
@Binding14("foobar")
public boolean bindings() {
return true;
}

public static boolean isEchoCalled() {
return echoCalled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

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

import static java.lang.annotation.ElementType.METHOD;
Expand All @@ -15,4 +16,6 @@
@Target({ TYPE })
@Retention(RUNTIME)
public @interface SimplePCBinding {
class Literal extends AnnotationLiteral<SimplePCBinding> implements SimplePCBinding {
}
}
13 changes: 13 additions & 0 deletions impl/src/main/resources/tck-audit-int.xml
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,19 @@
<note>Unclear meaning of validation.</note>
</assertion>

<assertion id="n">
<text>
The |getInterceptorBindings| method returns the set of interceptor binding annotations
that were used to associate interceptors with the target instance that is being intercepted.
</text>
</assertion>

<assertion id="o">
<text>
The |getInterceptorBinding| method returns the single interceptor binding annotation of given type
that was used to associate interceptors with the target instance that is being intercepted.
</text>
</assertion>
</section>

<section id="exceptions" title="Exceptions" level="2">
Expand Down
25 changes: 24 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<maven.compiler.release>11</maven.compiler.release>
<!-- Jakarta EE APIs Core -->
<annotations.api.version>2.1.0</annotations.api.version>
<interceptors.api.version>2.1.0</interceptors.api.version>
<interceptors.api.version>2.2.0-RC1</interceptors.api.version>
<atinject.api.version>2.0.1</atinject.api.version>
<el.api.version>5.0.0</el.api.version>

Expand Down Expand Up @@ -361,6 +361,29 @@
</build>
</profile>

<!-- TODO remove once Jakarta Interceptors 2.2.0-RC1 reaches Central -->
<profile>
<id>jakarta-ossrh-staging</id>
<activation>
<property>
<name>jakarta-ossrh-staging</name>
<value>!false</value>
</property>
</activation>
<repositories>
<repository>
<id>jakarta-ossrh-staging</id>
<url>https://jakarta.oss.sonatype.org/content/groups/staging</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</profile>

<profile>
<id>jboss-public-repository</id>
<activation>
Expand Down
13 changes: 13 additions & 0 deletions web/src/main/resources/tck-audit-int.xml
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,19 @@
<note>Unclear meaning of validation.</note>
</assertion>

<assertion id="n">
<text>
The |getInterceptorBindings| method returns the set of interceptor binding annotations
that were used to associate interceptors with the target instance that is being intercepted.
</text>
</assertion>

<assertion id="o">
<text>
The |getInterceptorBinding| method returns the single interceptor binding annotation of given type
that was used to associate interceptors with the target instance that is being intercepted.
</text>
</assertion>
</section>

<section id="exceptions" title="Exceptions" level="2">
Expand Down

0 comments on commit c34f1fb

Please sign in to comment.