|
| 1 | +package org.javaee7.cdi.instance; |
| 2 | + |
| 3 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 4 | +import org.jboss.arquillian.junit.Arquillian; |
| 5 | +import org.jboss.shrinkwrap.api.Archive; |
| 6 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 7 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | + |
| 11 | +import javax.enterprise.inject.Any; |
| 12 | +import javax.enterprise.inject.Default; |
| 13 | +import javax.enterprise.inject.Instance; |
| 14 | +import javax.enterprise.util.AnnotationLiteral; |
| 15 | +import javax.inject.Inject; |
| 16 | + |
| 17 | +import static org.hamcrest.CoreMatchers.instanceOf; |
| 18 | +import static org.junit.Assert.*; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Radim Hanus |
| 22 | + */ |
| 23 | +@RunWith(Arquillian.class) |
| 24 | +public class GreetingTest { |
| 25 | + @Deployment |
| 26 | + public static Archive<?> deploy() { |
| 27 | + return ShrinkWrap.create(JavaArchive.class) |
| 28 | + .addClasses(Greeting.class, SimpleGreeting.class, FormalGreeting.class, Business.class, Personal.class) |
| 29 | + .addAsManifestResource("beans.xml"); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Container will assume built-in @Default qualifier here as well as for beans that don't declare a qualifier. |
| 34 | + */ |
| 35 | + @Inject |
| 36 | + private Instance<Greeting> defaultInstance; |
| 37 | + |
| 38 | + /** |
| 39 | + * Qualifier @Personal is not qualifying any bean. |
| 40 | + */ |
| 41 | + @Inject @Personal |
| 42 | + private Instance<Greeting> personalInstance; |
| 43 | + |
| 44 | + /** |
| 45 | + * Built-in qualifier @Any is assumed on each bean regardless other qualifiers specified. |
| 46 | + */ |
| 47 | + @Inject @Any |
| 48 | + private Instance<Greeting> anyInstance; |
| 49 | + |
| 50 | + @Test |
| 51 | + public void test() throws Exception { |
| 52 | + // only SimpleGreeting instance should be available |
| 53 | + assertFalse(defaultInstance.isUnsatisfied()); |
| 54 | + assertFalse(defaultInstance.isAmbiguous()); |
| 55 | + assertThat(defaultInstance.get(), instanceOf(SimpleGreeting.class)); |
| 56 | + assertThat(defaultInstance.select(new AnnotationLiteral<Default>() {}).get(), instanceOf(SimpleGreeting.class)); |
| 57 | + |
| 58 | + // no instance should be available |
| 59 | + assertTrue(personalInstance.isUnsatisfied()); |
| 60 | + |
| 61 | + // both Greeting instances should be available |
| 62 | + assertFalse(anyInstance.isUnsatisfied()); |
| 63 | + assertTrue(anyInstance.isAmbiguous()); |
| 64 | + assertThat(anyInstance.select(new AnnotationLiteral<Business>() {}).get(), instanceOf(FormalGreeting.class)); |
| 65 | + assertThat(anyInstance.select(new AnnotationLiteral<Default>() {}).get(), instanceOf(SimpleGreeting.class)); |
| 66 | + } |
| 67 | +} |
| 68 | + |
0 commit comments