All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.cdi.tck.tests.implementation.producer.method.lifecycle.ProducerMethodLifecycleTest Maven / Gradle / Ivy

There is a newer version: 2.0.5.SP1
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2010, 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.cdi.tck.tests.implementation.producer.method.lifecycle;

import static org.jboss.cdi.tck.cdi.Sections.CONTEXTUAL;
import static org.jboss.cdi.tck.cdi.Sections.PRODUCER_METHOD;
import static org.jboss.cdi.tck.cdi.Sections.PRODUCER_METHOD_LIFECYCLE;
import static org.jboss.cdi.tck.cdi.Sections.PRODUCER_OR_DISPOSER_METHODS_INVOCATION;
import static org.jboss.cdi.tck.cdi.Sections.SPECIALIZATION;

import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.CreationException;
import javax.enterprise.inject.IllegalProductException;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.util.AnnotationLiteral;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.cdi.tck.AbstractTest;
import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.test.audit.annotations.SpecAssertion;
import org.jboss.test.audit.annotations.SpecAssertions;
import org.jboss.test.audit.annotations.SpecVersion;
import org.testng.annotations.Test;

/**
 * NOTE May be able to get rid of some of the binding types if the producer method precedence question is resolved
 */
@SpecVersion(spec = "cdi", version = "2.0-EDR1")
public class ProducerMethodLifecycleTest extends AbstractTest {
    private AnnotationLiteral PET_LITERAL = new AnnotationLiteral() {
    };
    private AnnotationLiteral FIRST_BORN_LITERAL = new AnnotationLiteral() {
    };
    private AnnotationLiteral FAIL_LITERAL = new AnnotationLiteral() {
    };
    private AnnotationLiteral NULL_LITERAL = new AnnotationLiteral() {
    };

    @Deployment
    public static WebArchive createTestArchive() {
        return new WebArchiveBuilder().withTestClassPackage(ProducerMethodLifecycleTest.class).withBeansXml("beans.xml")
                .build();
    }

    @Test
    @SpecAssertion(section = PRODUCER_METHOD_LIFECYCLE, id = "ea")
    public void testProducerMethodBeanCreate() {
        PreferredSpiderProducer.reset();
        Bean tarantulaBean = getBeans(Tarantula.class, PET_LITERAL).iterator().next();
        CreationalContext tarantulaCc = getCurrentManager().createCreationalContext(tarantulaBean);
        Tarantula tarantula = tarantulaBean.create(tarantulaCc);
        assert PreferredSpiderProducer.getTarantulaCreated() == tarantula;
        assert PreferredSpiderProducer.getInjectedWeb() != null;
        assert PreferredSpiderProducer.getInjectedWeb().isDestroyed();
    }

    @Test
    @SpecAssertions({ @SpecAssertion(section = PRODUCER_METHOD_LIFECYCLE, id = "ea") })
    public void testProducerMethodInvokedOnCreate() {
        Bean eggBean = getBeans(SpiderEgg.class, FIRST_BORN_LITERAL).iterator().next();
        CreationalContext eggCc = getCurrentManager().createCreationalContext(eggBean);
        assert eggBean.create(eggCc) != null;
    }

    @Test
    @SpecAssertion(section = PRODUCER_METHOD, id = "j")
    public void testWhenApplicationInvokesProducerMethodParametersAreNotInjected() {
        try {
            getContextualReference(BrownRecluse.class).layAnEgg(null);
        } catch (AssertionError e) {
            return;
        }

        assert false : "The BeanManager should not have been injected into the producer method";
    }

    @Test
    @SpecAssertions({ @SpecAssertion(section = PRODUCER_OR_DISPOSER_METHODS_INVOCATION, id = "c"), @SpecAssertion(section = SPECIALIZATION, id = "cb") })
    public void testProducerMethodFromSpecializedBeanUsed() {
        SpiderProducer.reset();
        PreferredSpiderProducer.reset();
        Bean spiderBean = getBeans(Tarantula.class, PET_LITERAL).iterator().next();
        CreationalContext spiderBeanCc = getCurrentManager().createCreationalContext(spiderBean);
        Tarantula tarantula = spiderBean.create(spiderBeanCc);
        assert PreferredSpiderProducer.getTarantulaCreated() == tarantula;
        assert !SpiderProducer.isTarantulaCreated();
    }

    @Test
    @SpecAssertions({ @SpecAssertion(section = PRODUCER_METHOD_LIFECYCLE, id = "k") })
    public void testCreateReturnsNullIfProducerDoesAndDependent() {
        Bean nullSpiderBean = getBeans(Spider.class, NULL_LITERAL).iterator().next();
        CreationalContext nullSpiderBeanCc = getCurrentManager().createCreationalContext(nullSpiderBean);
        assert nullSpiderBean.create(nullSpiderBeanCc) == null;
    }

    @Test(expectedExceptions = IllegalProductException.class)
    @SpecAssertions({ @SpecAssertion(section = PRODUCER_METHOD_LIFECYCLE, id = "l") })
    public void testCreateFailsIfProducerReturnsNullAndNotDependent() {
        Bean potatoChipBean = getBeans(PotatoChip.class, NULL_LITERAL).iterator().next();
        assert potatoChipBean != null;

        CreationalContext chipCc = getCurrentManager().createCreationalContext(potatoChipBean);
        potatoChipBean.create(chipCc);
        assert false;
    }

    @Test
    @SpecAssertions({ @SpecAssertion(section = PRODUCER_METHOD_LIFECYCLE, id = "ma"), @SpecAssertion(section = PRODUCER_METHOD_LIFECYCLE, id = "r") })
    public void testProducerMethodBeanDestroy() {
        PreferredSpiderProducer.reset();
        Set> beans = getCurrentManager().getBeans(Tarantula.class, PET_LITERAL);
        Bean bean = getCurrentManager().resolve(beans);
        assert bean.getBeanClass().equals(PreferredSpiderProducer.class);
        assert bean.getTypes().contains(Tarantula.class);
        Bean tarantulaBean = (Bean) bean;
        CreationalContext tarantulaCc = getCurrentManager().createCreationalContext(tarantulaBean);
        Tarantula tarantula = tarantulaBean.create(tarantulaCc);
        PreferredSpiderProducer.resetInjections();
        tarantulaBean.destroy(tarantula, tarantulaCc);
        assert PreferredSpiderProducer.getTarantulaDestroyed() == tarantula;
        assert PreferredSpiderProducer.isDestroyArgumentsSet();
        assert PreferredSpiderProducer.getInjectedWeb() != null;
        assert PreferredSpiderProducer.getInjectedWeb().isDestroyed();
    }

    @Test(expectedExceptions = FooException.class)
    @SpecAssertions({ @SpecAssertion(section = CONTEXTUAL, id = "aa") })
    public void testCreateRethrowsUncheckedException() {
        Bean shipBean = getBeans(Ship.class, FAIL_LITERAL).iterator().next();
        CreationalContext shipCc = getCurrentManager().createCreationalContext(shipBean);
        shipBean.create(shipCc);
        assert false;
    }

    @Test(expectedExceptions = CreationException.class)
    @SpecAssertions({ @SpecAssertion(section = CONTEXTUAL, id = "aa") })
    public void testCreateWrapsCheckedExceptionAndRethrows() {
        Bean lorryBean = getBeans(Lorry.class, FAIL_LITERAL).iterator().next();
        CreationalContext lorryCc = getCurrentManager().createCreationalContext(lorryBean);
        lorryBean.create(lorryCc);
        assert false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy