93f260e2d3f170ba889a83640b9919820e577bed
[utils] / support / cdi / src / test / java / org / wamblee / cdi / SimpleInjectorTest.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.cdi;
17
18 import static junit.framework.Assert.*;
19 import static org.mockito.Mockito.*;
20
21 import javax.enterprise.context.RequestScoped;
22 import javax.inject.Inject;
23 import javax.naming.InitialContext;
24
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.wamblee.inject.Injector;
29 import org.wamblee.inject.InjectorFactory;
30 import org.wamblee.inject.InjectorBuilder;
31 import org.wamblee.inject.SimpleInjector;
32 import org.wamblee.test.jndi.StubInitialContextFactory;
33
34 public class SimpleInjectorTest extends BaseTestFixture {
35
36     private BeanManagerSetup setup;
37
38     @Before
39     public void setUp() throws Exception {
40         super.setUp();
41         setup = new BeanManagerSetup();
42         setup.initialize();
43         StubInitialContextFactory.register();
44         InitialContext ctx = new InitialContext();
45         ctx.bind(BeanManagerLookup.BEAN_MANAGER_JNDI, setup.getBeanManager());
46         MySingleton.reset();        
47     }
48
49     @After
50     public void tearDown() throws Exception {
51         StubInitialContextFactory.unregister();
52         setup.shutdown();
53         super.tearDown();
54     }
55
56     @Test
57     public void testGetSingleton() {
58         MyPojo pojo = new MyPojo();
59         SimpleInjector injector = new SimpleInjector(new CdiInjectorFactory(BeanManagerLookup.lookup()));
60         injector.inject(pojo);
61
62         MySingleton obj = pojo.getSingleton();
63         assertNotNull(obj);
64
65         MyPojo pojo2 = new MyPojo();
66         injector.inject(pojo2);
67
68         // Objects will not be the same as they are contextual references to the
69         // same object.
70         // assertSame(pojo2, pojo);
71
72         assertEquals(1, MySingleton.getInstances());
73     }
74
75     @Test
76     public void testGetSingletonCustomInjector() {
77         MyPojo pojo = new MyPojo();
78         InjectorFactory factory = mock(InjectorFactory.class);
79         SimpleInjector injector = new SimpleInjector(factory);
80         final MySingleton singleton = new MySingleton();
81         when(factory.create(MyPojo.class)).thenReturn(new Injector() {
82             @Override
83             public void inject(Object aComponent) {
84                 MyPojo pojo2 = (MyPojo) aComponent;
85                 pojo2.setSingleton(singleton); 
86             }
87         });
88         
89         injector.inject(pojo);
90         // verify the custom injector was called. 
91         assertSame(singleton, pojo.getSingleton());
92     }
93     
94     @RequestScoped
95     public static class Y { 
96         
97     }
98     public static class X { 
99         @Inject
100         private Y y; 
101         
102     }
103     
104     @Test 
105     public void testInjectStorage() throws Exception { 
106         X x = new X(); 
107         InjectorBuilder.setInjectorFactory(null);
108         InjectorBuilder.getInjector().inject(x);
109     }
110     
111     @Test 
112     public void testInjectStorage2() { 
113         X x = new X(); 
114         InjectorBuilder.setInjectorFactory(null);
115         InjectorBuilder.getInjector().inject(x);
116     }
117     
118 }