Skip to content

Commit 98ffc2f

Browse files
committed
fix test that interceptors are dependent instances of intercepted beans
The test previously used a `@PreDestroy` callback declared on the interceptor to prove that the interceptor is destroyed when the intercepted bean is destroyed, which is incorrect. Interceptors cannot declare lifecycle callbacks for themselves, the `@PreDestroy` callback actually interposed on the destruction of the intercepted bean. This commit uses a new dependent-scoped bean that is injected into the interceptor, which makes it a dependent instance of the interceptor. The new bean has a `@PreDestroy` callback for itself, which means we can verify that the new bean is destroyed when the intercepted bean is destroyed. Such chain of dependent instance destruction proves that the interceptor is indeed a dependent instance of the intercepted bean.
1 parent a0ecc2e commit 98ffc2f

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

impl/src/main/java/org/jboss/cdi/tck/tests/context/dependent/DependentContextTest.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,13 @@ public void testDependentsDestroyedWhenObserverMethodEvaluationCompletes() {
414414
@Test
415415
@SpecAssertion(section = DEPENDENT_OBJECTS, id = "aa")
416416
public void testDependentScopedInterceptorsAreDependentObjectsOfBean() {
417-
TransactionalInterceptor.destroyed = false;
417+
// since interceptors can't declare `@PreDestroy` callbacks for themselves, we inject
418+
// another dependent-scoped bean into the interceptor and add a `@PreDestroy` callback
419+
// there -- this dependency will be a dependent instance of the interceptor, and so
420+
// if destroying the intercepted bean will destroy the other bean, we have a proof
421+
// that the interceptor was also destroyed
422+
423+
TransactionalInterceptorDependency.destroyed = false;
418424
TransactionalInterceptor.intercepted = false;
419425

420426
Bean<AccountTransaction> bean = getBeans(AccountTransaction.class).iterator().next();
@@ -427,6 +433,6 @@ public void testDependentScopedInterceptorsAreDependentObjectsOfBean() {
427433

428434
ctx.release();
429435

430-
assert TransactionalInterceptor.destroyed;
436+
assert TransactionalInterceptorDependency.destroyed;
431437
}
432438
}

impl/src/main/java/org/jboss/cdi/tck/tests/context/dependent/TransactionalInterceptor.java

+4-13
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
*/
1717
package org.jboss.cdi.tck.tests.context.dependent;
1818

19-
import jakarta.annotation.PreDestroy;
2019
import jakarta.annotation.Priority;
2120
import jakarta.enterprise.context.Dependent;
21+
import jakarta.inject.Inject;
2222
import jakarta.interceptor.AroundInvoke;
2323
import jakarta.interceptor.Interceptor;
2424
import jakarta.interceptor.InvocationContext;
@@ -28,23 +28,14 @@
2828
@Interceptor
2929
@Priority(1)
3030
public class TransactionalInterceptor {
31-
public static boolean destroyed = false;
3231
public static boolean intercepted = false;
3332

33+
@Inject
34+
TransactionalInterceptorDependency dependency;
35+
3436
@AroundInvoke
3537
public Object alwaysReturnThis(InvocationContext ctx) throws Exception {
3638
intercepted = true;
3739
return ctx.proceed();
3840
}
39-
40-
@PreDestroy
41-
public void destroy(InvocationContext ctx) {
42-
destroyed = true;
43-
try {
44-
ctx.proceed();
45-
} catch (Exception e) {
46-
// catch and wrap
47-
throw new RuntimeException(e);
48-
}
49-
}
5041
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.jboss.cdi.tck.tests.context.dependent;
2+
3+
import jakarta.annotation.PreDestroy;
4+
import jakarta.enterprise.context.Dependent;
5+
6+
@Dependent
7+
public class TransactionalInterceptorDependency {
8+
public static boolean destroyed = false;
9+
10+
@PreDestroy
11+
public void destroy() {
12+
destroyed = true;
13+
}
14+
}

0 commit comments

Comments
 (0)