cdi project in initial revision
[utils] / support / cdi / src / test / java / org / wamblee / cdi / WeldTest.java
1 package org.wamblee.cdi;
2
3 import javax.enterprise.context.spi.CreationalContext;
4 import javax.enterprise.inject.spi.AnnotatedType;
5 import javax.enterprise.inject.spi.BeanManager;
6 import javax.enterprise.inject.spi.InjectionTarget;
7
8 import org.jboss.weld.environment.se.Weld;
9 import org.jboss.weld.environment.se.WeldContainer;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 import static junit.framework.TestCase.*;
14
15 public class WeldTest {
16     
17     private Weld weld;
18     private WeldContainer container; 
19     private BeanManager beanManager; 
20
21     @Before
22     public void setUp() {
23         weld = new Weld();
24         container = weld.initialize(); 
25         beanManager = container.getBeanManager();
26     }
27     
28     @After
29     public void tearDown() { 
30         weld.shutdown(); 
31     }
32     
33     @Test
34     public void testGetSingleton() { 
35         AnnotatedType<MyPojo> type = beanManager.createAnnotatedType(MyPojo.class);
36         InjectionTarget<MyPojo> target = beanManager.createInjectionTarget(type);
37         CreationalContext<MyPojo> ctx = beanManager.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 same object.
51         // assertSame(pojo2, pojo);
52
53         assertEquals(1, MySingleton.getInstances());
54     }
55 }