(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
13 public class WeldTest {
14
15     private BeanManagerSetup setup;
16
17     @Before
18     public void setUp() {
19         setup = new BeanManagerSetup();
20         setup.initialize();
21         MySingleton.reset();
22     }
23
24     @After
25     public void tearDown() {
26         setup.shutdown();
27     }
28
29     @Test
30     public void testGetSingleton() {
31         AnnotatedType<MyPojo> type = setup.getBeanManager()
32             .createAnnotatedType(MyPojo.class);
33         InjectionTarget<MyPojo> target = setup.getBeanManager()
34             .createInjectionTarget(type);
35         CreationalContext<MyPojo> ctx = setup.getBeanManager()
36             .createCreationalContext(null);
37
38         MyPojo pojo = new MyPojo();
39
40         target.inject(pojo, ctx);
41
42         MySingleton obj = pojo.getSingleton();
43
44         assertNotNull(obj);
45
46         MyPojo pojo2 = new MyPojo();
47         target.inject(pojo2, ctx);
48
49         // Objects will not be the same as they are contextual references to the
50         // same object.
51         // assertSame(pojo2, pojo);
52
53         assertEquals(1, MySingleton.getInstances());
54     }
55
56     @Test
57     public void testAgain() {
58         testGetSingleton();
59     }
60 }