Skip to content

Commit

Permalink
Merge pull request quarkusio#7933 from manovotn/multipleBindings
Browse files Browse the repository at this point in the history
Fix interceptor resolution process for multiple bindings, add automated test
  • Loading branch information
gsmet authored Mar 18, 2020
2 parents 5f35935 + 38fc280 commit 391d390
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public List<InterceptorInfo> resolve(InterceptionType interceptionType, Set<Anno
if (!interceptor.intercepts(interceptionType)) {
continue;
}
boolean matches = false;
boolean matches = true;
for (AnnotationInstance interceptorBinding : interceptor.getBindings()) {
if (hasInterceptorBinding(bindings, interceptorBinding)) {
matches = true;
if (!hasInterceptorBinding(bindings, interceptorBinding)) {
matches = false;
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkus.arc.test.interceptors.bindings.multiple;

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.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.interceptor.InterceptorBinding;

@InterceptorBinding
@Documented
@Retention(RUNTIME)
@Target({ METHOD, TYPE })
public @interface BarBinding {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkus.arc.test.interceptors.bindings.multiple;

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.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.interceptor.InterceptorBinding;

@InterceptorBinding
@Documented
@Retention(RUNTIME)
@Target({ METHOD, TYPE })
public @interface FooBinding {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.quarkus.arc.test.interceptors.bindings.multiple;

import static org.junit.jupiter.api.Assertions.assertEquals;

import io.quarkus.arc.Arc;
import io.quarkus.arc.test.ArcTestContainer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

/**
* Tests behavior the case where there is more then one binding specified on an interceptor.
*/
public class MultipleBindingsTest {
@RegisterExtension
public ArcTestContainer container = new ArcTestContainer(BarBinding.class, FooBinding.class,
MyBean.class, MyInterceptor.class, MyOtherBean.class);

@Test
public void testInterception() {
assertEquals(0, MyInterceptor.TIMES_INVOKED);
// bean only has one binding, the interceptor should not get triggered
Arc.container().instance(MyBean.class).get().foo();
assertEquals(0, MyInterceptor.TIMES_INVOKED);
// bean has both bindings that the interceptor has
Arc.container().instance(MyOtherBean.class).get().foo();
assertEquals(1, MyInterceptor.TIMES_INVOKED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.quarkus.arc.test.interceptors.bindings.multiple;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
@BarBinding
public class MyBean {

public String foo() {
return "foo";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.arc.test.interceptors.bindings.multiple;

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

@Interceptor
@Priority(1)
@BarBinding
@FooBinding
public class MyInterceptor {

public static int TIMES_INVOKED = 0;

@AroundInvoke
public Object doAround(InvocationContext context) throws Exception {
TIMES_INVOKED++;
return context.proceed();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.quarkus.arc.test.interceptors.bindings.multiple;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
@FooBinding
@BarBinding
public class MyOtherBean {

public String foo() {
return "anotherFoo";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

@Interceptor
@Priority(2)
@CounterBinding
@AnotherAnnotation // this is a transitive binding which brings in CounterBinding + SomeBinding as well

@SomeAnnotation // this is transitive binding, it also brings in @CounterBinding
public class TransitiveCounterInterceptor {

public static Integer timesInvoked = 0;
Expand Down

0 comments on commit 391d390

Please sign in to comment.