(no commit message)
[utils] / support / cdi / src / test / java / org / wamblee / cdi / WeldTest.java
1 package org.wamblee.cdi;
2
3 import static junit.framework.Assert.*;
4
5 import javax.enterprise.context.spi.CreationalContext;
6 import javax.enterprise.inject.spi.AnnotatedType;
7 import javax.enterprise.inject.spi.InjectionTarget;
8
9 import org.junit.After;
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.wamblee.test.cdi.BeanManagerSetup;
13
14 public class WeldTest {
15
16     private BeanManagerSetup setup;
17
18     @Before
19     public void setUp() {
20         setup = new BeanManagerSetup();
21         setup.initialize();
22         MySingleton.reset();
23     }
24
25     @After
26     public void tearDown() {
27         setup.shutdown();
28     }
29
30     @Test
31     public void testGetSingleton() {
32         AnnotatedType<MyPojo> type = setup.getBeanManager()
33             .createAnnotatedType(MyPojo.class);
34         InjectionTarget<MyPojo> target = setup.getBeanManager()
35             .createInjectionTarget(type);
36         CreationalContext<MyPojo> ctx = setup.getBeanManager()
37             .createCreationalContext(null);
38
39         MyPojo pojo = new MyPojo();
40
41         target.inject(pojo, ctx);
42
43         MySingleton obj = pojo.getSingleton();
44
45         assertNotNull(obj);
46
47         MyPojo pojo2 = new MyPojo();
48         target.inject(pojo2, ctx);
49
50         // Objects will not be the same as they are contextual references to the
51         // same object.
52         // assertSame(pojo2, pojo);
53
54         assertEquals(1, MySingleton.getInstances());
55     }
56
57     @Test
58     public void testAgain() {
59         testGetSingleton();
60     }
61 }