From 5338c2e139ad6c57e92043275660e97ea92190c6 Mon Sep 17 00:00:00 2001 From: Tomas Remes Date: Wed, 11 Mar 2015 09:12:00 +0100 Subject: [PATCH] WELD-1894 - test disposer method called once. --- .../DisposesMethodCalledOnceTest.java | 87 +++++++++++++++ .../disposer/weld1894/DummyQualifier.java | 45 ++++++++ .../tests/disposer/weld1894/FirstBean.java | 27 +++++ .../tests/disposer/weld1894/ProducerBean.java | 104 ++++++++++++++++++ .../tests/disposer/weld1894/SecondBean.java | 27 +++++ .../tests/disposer/weld1894/TestObserver.java | 42 +++++++ 6 files changed, 332 insertions(+) create mode 100644 tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/DisposesMethodCalledOnceTest.java create mode 100644 tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/DummyQualifier.java create mode 100644 tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/FirstBean.java create mode 100644 tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/ProducerBean.java create mode 100644 tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/SecondBean.java create mode 100644 tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/TestObserver.java diff --git a/tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/DisposesMethodCalledOnceTest.java b/tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/DisposesMethodCalledOnceTest.java new file mode 100644 index 00000000000..e70e669d800 --- /dev/null +++ b/tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/DisposesMethodCalledOnceTest.java @@ -0,0 +1,87 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2015, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.weld.tests.disposer.weld1894; + +import javax.enterprise.inject.spi.BeanManager; +import javax.inject.Inject; + +import junit.framework.Assert; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.BeanArchive; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * @author Tomas Remes + */ +@RunWith(Arquillian.class) +public class DisposesMethodCalledOnceTest { + + @Inject + BeanManager beanManager; + + @Deployment + public static Archive getDeployment() { + return ShrinkWrap.create(BeanArchive.class).addPackage(DisposesMethodCalledOnceTest.class.getPackage()); + } + + @Test + public void testDisposerCalledOnce1() { + ProducerBean.reset(); + beanManager.fireEvent("Hello"); + Assert.assertEquals("Disposer method called multiple times!", 1, ProducerBean.firstDisposerCalled.get()); + } + + @Test + public void testDisposerCalledOnce2() { + ProducerBean.reset(); + beanManager.fireEvent("Hello"); + Assert.assertEquals("Disposer method called multiple times!", 1, ProducerBean.secondDisposerCalled.get()); + } + + @Test + public void testDisposerCalledOnce3() { + ProducerBean.reset(); + beanManager.fireEvent("Hello"); + Assert.assertEquals("Disposer method called multiple times!", 1, ProducerBean.thirdDisposerCalled.get()); + } + + @Test + public void testDisposerCalledOnce4() { + ProducerBean.reset(); + beanManager.fireEvent("Hello"); + Assert.assertEquals("Disposer method called multiple times!", 1, ProducerBean.forthDisposerCalled.get()); + } + + @Test + public void testDisposerCalledOnce5() { + ProducerBean.reset(); + beanManager.fireEvent("Hello"); + Assert.assertEquals("Disposer method called multiple times!", 1, ProducerBean.fifthDisposerCalled.get()); + } + + @Test + public void testDisposerCalledOnce6() { + ProducerBean.reset(); + beanManager.fireEvent("Hello"); + Assert.assertEquals("Disposer method called multiple times!", 1, ProducerBean.sixthDisposerCalled.get()); + } + +} diff --git a/tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/DummyQualifier.java b/tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/DummyQualifier.java new file mode 100644 index 00000000000..6d62fc5fbc8 --- /dev/null +++ b/tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/DummyQualifier.java @@ -0,0 +1,45 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2015, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.weld.tests.disposer.weld1894; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import javax.enterprise.util.AnnotationLiteral; +import javax.inject.Qualifier; + +@Retention(RetentionPolicy.RUNTIME) +@Qualifier +public @interface DummyQualifier { + + String value(); + + @SuppressWarnings("all") + public static class Literal extends AnnotationLiteral implements DummyQualifier { + + private final String value; + + public Literal(String value) { + this.value = value; + } + + @Override + public String value() { + return value; + } + } +} diff --git a/tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/FirstBean.java b/tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/FirstBean.java new file mode 100644 index 00000000000..b46a775d767 --- /dev/null +++ b/tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/FirstBean.java @@ -0,0 +1,27 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2015, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.weld.tests.disposer.weld1894; + +import javax.enterprise.context.Dependent; + +@Dependent +public class FirstBean { + + public void ping() { + + } +} diff --git a/tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/ProducerBean.java b/tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/ProducerBean.java new file mode 100644 index 00000000000..71709fa64b2 --- /dev/null +++ b/tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/ProducerBean.java @@ -0,0 +1,104 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2015, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.weld.tests.disposer.weld1894; + +import java.util.concurrent.atomic.AtomicInteger; + +import javax.enterprise.inject.Disposes; +import javax.enterprise.inject.Produces; +import javax.enterprise.inject.TransientReference; + +public class ProducerBean { + + public static AtomicInteger firstDisposerCalled = new AtomicInteger(); + public static AtomicInteger secondDisposerCalled = new AtomicInteger(); + public static AtomicInteger thirdDisposerCalled = new AtomicInteger(); + public static AtomicInteger forthDisposerCalled = new AtomicInteger(); + public static AtomicInteger fifthDisposerCalled = new AtomicInteger(); + public static AtomicInteger sixthDisposerCalled = new AtomicInteger(); + + @Produces + @DummyQualifier("A") + public FirstBean produceA() { + return new FirstBean(); + } + + @Produces + @DummyQualifier("B") + public FirstBean produceB() { + return new FirstBean(); + } + + @Produces + @DummyQualifier("C") + public FirstBean produceC(@DummyQualifier("D") SecondBean secondBean) { + secondBean.ping(); + return new FirstBean(); + } + + @Produces + @DummyQualifier("E") + public FirstBean produceE(@TransientReference @DummyQualifier("F") SecondBean secondBean) { + secondBean.ping(); + return new FirstBean(); + } + + @Produces + @DummyQualifier("D") + public SecondBean produceD() { + return new SecondBean(); + } + + @Produces + @DummyQualifier("F") + public SecondBean produceF() { + return new SecondBean(); + } + + public void disposeA(@Disposes @DummyQualifier("A") FirstBean bean) { + firstDisposerCalled.incrementAndGet(); + } + + public void disposeB(@Disposes @DummyQualifier("B") FirstBean bean) { + secondDisposerCalled.incrementAndGet(); + } + + public void disposeC(@Disposes @DummyQualifier("C") FirstBean bean) { + thirdDisposerCalled.incrementAndGet(); + } + + public void disposeE(@Disposes @DummyQualifier("E") FirstBean bean) { + sixthDisposerCalled.incrementAndGet(); + } + + public void disposeD(@Disposes @DummyQualifier("D") SecondBean bean) { + forthDisposerCalled.incrementAndGet(); + } + + public void disposeF(@Disposes @DummyQualifier("F") SecondBean bean) { + fifthDisposerCalled.incrementAndGet(); + } + + public static void reset() { + firstDisposerCalled = new AtomicInteger(); + secondDisposerCalled = new AtomicInteger(); + thirdDisposerCalled = new AtomicInteger(); + forthDisposerCalled = new AtomicInteger(); + fifthDisposerCalled = new AtomicInteger(); + sixthDisposerCalled = new AtomicInteger(); + } +} diff --git a/tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/SecondBean.java b/tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/SecondBean.java new file mode 100644 index 00000000000..6235013c2c5 --- /dev/null +++ b/tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/SecondBean.java @@ -0,0 +1,27 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2015, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.weld.tests.disposer.weld1894; + +import javax.enterprise.context.Dependent; + +@Dependent +public class SecondBean { + + public void ping() { + + } +} diff --git a/tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/TestObserver.java b/tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/TestObserver.java new file mode 100644 index 00000000000..4971a53828e --- /dev/null +++ b/tests-arquillian/src/test/java/org/jboss/weld/tests/disposer/weld1894/TestObserver.java @@ -0,0 +1,42 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2015, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.weld.tests.disposer.weld1894; + +import javax.enterprise.event.Observes; +import javax.enterprise.inject.Instance; +import javax.inject.Inject; + +public class TestObserver { + + @Inject + @DummyQualifier("B") + Instance beanB; + + @Inject + @DummyQualifier("C") + FirstBean beanC; + + @Inject + @DummyQualifier("E") + FirstBean beanE; + + public void observes(@Observes String message, @DummyQualifier("A") FirstBean dependentBean) { + beanB.destroy(beanB.get()); + beanE.ping(); + beanC.ping(); + } +}