Skip to content

Commit

Permalink
Test binding added by extension visible
Browse files Browse the repository at this point in the history
Test that an interceptor binding added to a method via an annotation is
used to bind the interceptor and visible via
InterceptionContext.getInterceptorBinding

Signed-off-by: Andrew Rouse <anrouse@uk.ibm.com>
  • Loading branch information
Azquelt committed Jan 9, 2024
1 parent a20916e commit c2a5d5f
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeInterceptorBinding;

import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension;
import jakarta.enterprise.inject.build.compatible.spi.ClassConfig;
import jakarta.enterprise.inject.build.compatible.spi.Enhancement;

public class ChangeInterceptorBindingExtension implements BuildCompatibleExtension {

@Enhancement(types = MyService.class)
public void enhancement(ClassConfig config) {
System.out.println("Running extension");
config.methods()
.stream()
.filter(it -> "hello".equals(it.info().name()))
.forEach(m -> m.addAnnotation(new MyBinding.Literal("foo")));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeInterceptorBinding;

import static org.testng.Assert.assertEquals;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.cdi.tck.AbstractTest;
import org.jboss.cdi.tck.cdi.Sections;
import org.jboss.cdi.tck.interceptors.InterceptorsSections;
import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.test.audit.annotations.SpecAssertion;
import org.jboss.test.audit.annotations.SpecVersion;
import org.testng.annotations.Test;

/**
* Test interceptor bindings applied to methods via extension are used to bind the correct interceptor and returned from InvocationContext.getInterceptorBindings()
*/
@SpecVersion(spec = "interceptors", version = "2.2")
public class ChangeInterceptorBindingTest extends AbstractTest {

@Deployment
public static WebArchive createTestArchive() {
return new WebArchiveBuilder()
.withTestClassPackage(ChangeInterceptorBindingTest.class)
.withBuildCompatibleExtension(ChangeInterceptorBindingExtension.class)
.build();
}

@Test
@SpecAssertion(section = Sections.ENHANCEMENT_PHASE, id = "b")
@SpecAssertion(section = InterceptorsSections.INVOCATIONCONTEXT, id="o")
public void test() {
assertEquals(getContextualReference(MyService.class).hello(), "Intercepted(foo): Hello!");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeInterceptorBinding;

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

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

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

@InterceptorBinding
@Inherited
@Retention(RUNTIME)
@Target({ TYPE, METHOD })
public @interface MyBinding {

@Nonbinding
public String value();

public class Literal extends AnnotationLiteral<MyBinding> implements MyBinding {
private 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,18 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeInterceptorBinding;

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

@Interceptor
@MyBinding("")
@Priority(1000)
public class MyInterceptor {

@AroundInvoke
public Object aroundInvoke(InvocationContext ctx) throws Exception {
MyBinding binding = ctx.getInterceptorBinding(MyBinding.class);
return "Intercepted(" + binding.value() + "): " + ctx.proceed();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jboss.cdi.tck.tests.build.compatible.extensions.changeInterceptorBinding;

import jakarta.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class MyService {

public String hello() {
return "Hello!";
}
}

0 comments on commit c2a5d5f

Please sign in to comment.