-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ArC - fix CreationalContext handling when used directly with BeanManager
Co-authored-by: Ladicek <[email protected]>
- Loading branch information
Showing
5 changed files
with
141 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...src/test/java/io/quarkus/arc/test/bean/destroy/DependentPreDestroyOnlyCalledOnceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package io.quarkus.arc.test.bean.destroy; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import jakarta.annotation.PreDestroy; | ||
import jakarta.annotation.Priority; | ||
import jakarta.enterprise.context.Dependent; | ||
import jakarta.enterprise.context.spi.CreationalContext; | ||
import jakarta.enterprise.inject.spi.Bean; | ||
import jakarta.enterprise.inject.spi.BeanManager; | ||
import jakarta.inject.Inject; | ||
import jakarta.interceptor.Interceptor; | ||
import jakarta.interceptor.InterceptorBinding; | ||
import jakarta.interceptor.InvocationContext; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.test.ArcTestContainer; | ||
|
||
public class DependentPreDestroyOnlyCalledOnceTest { | ||
@RegisterExtension | ||
ArcTestContainer container = new ArcTestContainer(MyDependency.class, MyBean.class, MyInterceptedBean.class, | ||
MyInterceptorBinding.class, MyInterceptor.class); | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
MyDependency.preDestroy = 0; | ||
MyBean.preDestroy = 0; | ||
MyInterceptedBean.preDestroy = 0; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Test | ||
public void preDestroyOnBeanOnly() { | ||
BeanManager beanManager = Arc.container().beanManager(); | ||
Bean<MyBean> bean = (Bean<MyBean>) beanManager.resolve(beanManager.getBeans(MyBean.class)); | ||
CreationalContext<MyBean> ctx = beanManager.createCreationalContext(bean); | ||
MyBean instance = (MyBean) beanManager.getReference(bean, MyBean.class, ctx); | ||
bean.destroy(instance, ctx); | ||
|
||
assertEquals(1, MyDependency.preDestroy); | ||
assertEquals(1, MyBean.preDestroy); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Test | ||
public void preDestroyOnBeanAndInterceptor() { | ||
BeanManager beanManager = Arc.container().beanManager(); | ||
Bean<MyInterceptedBean> bean = (Bean<MyInterceptedBean>) beanManager.resolve( | ||
beanManager.getBeans(MyInterceptedBean.class)); | ||
CreationalContext<MyInterceptedBean> ctx = beanManager.createCreationalContext(bean); | ||
MyInterceptedBean instance = (MyInterceptedBean) beanManager.getReference(bean, MyInterceptedBean.class, ctx); | ||
bean.destroy(instance, ctx); | ||
|
||
assertEquals(1, MyDependency.preDestroy); | ||
assertEquals(1, MyInterceptedBean.preDestroy); | ||
assertEquals(1, MyInterceptor.preDestroy); | ||
} | ||
|
||
@Dependent | ||
static class MyDependency { | ||
static int preDestroy = 0; | ||
|
||
@PreDestroy | ||
void destroy() { | ||
preDestroy++; | ||
} | ||
} | ||
|
||
@Dependent | ||
static class MyBean { | ||
static int preDestroy = 0; | ||
|
||
@Inject | ||
MyDependency dependency; | ||
|
||
@PreDestroy | ||
void destroy() { | ||
preDestroy++; | ||
} | ||
} | ||
|
||
@Dependent | ||
@MyInterceptorBinding | ||
static class MyInterceptedBean { | ||
static int preDestroy = 0; | ||
|
||
@Inject | ||
MyDependency dependency; | ||
|
||
@PreDestroy | ||
void destroy() { | ||
preDestroy++; | ||
} | ||
} | ||
|
||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@InterceptorBinding | ||
@interface MyInterceptorBinding { | ||
} | ||
|
||
@MyInterceptorBinding | ||
@Interceptor | ||
@Priority(1) | ||
static class MyInterceptor { | ||
static int preDestroy = 0; | ||
|
||
@PreDestroy | ||
void preDestroy(InvocationContext ctx) throws Exception { | ||
preDestroy++; | ||
ctx.proceed(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters