Skip to content

Commit

Permalink
WELD-1894 - test disposer method called once.
Browse files Browse the repository at this point in the history
  • Loading branch information
tremes authored and jharting committed Mar 16, 2015
1 parent 6c42629 commit 5338c2e
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 0 deletions.
@@ -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());
}

}
@@ -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<DummyQualifier> implements DummyQualifier {

private final String value;

public Literal(String value) {
this.value = value;
}

@Override
public String value() {
return value;
}
}
}
@@ -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() {

}
}
@@ -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();
}
}
@@ -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() {

}
}
@@ -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<FirstBean> 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();
}
}

0 comments on commit 5338c2e

Please sign in to comment.