package rename for test libraries.
[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.naming.InitialContext;
22
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.wamblee.inject.Injector;
27 import org.wamblee.inject.InjectorFactory;
28 import org.wamblee.inject.SimpleInjector;
29 import org.wamblee.test.jndi.StubInitialContextFactory;
30
31 public class SimpleInjectorTest extends BaseTestFixture {
32
33     private BeanManagerSetup setup;
34
35     @Before
36     public void setUp() throws Exception {
37         super.setUp();
38         setup = new BeanManagerSetup();
39         setup.initialize();
40         StubInitialContextFactory.register();
41         InitialContext ctx = new InitialContext();
42         ctx.bind(BeanManagerLookup.BEAN_MANAGER_JNDI, setup.getBeanManager());
43         MySingleton.reset();        
44     }
45
46     @After
47     public void tearDown() throws Exception {
48         StubInitialContextFactory.unregister();
49         setup.shutdown();
50         super.tearDown();
51     }
52
53     @Test
54     public void testGetSingleton() {
55         MyPojo pojo = new MyPojo();
56         SimpleInjector injector = new SimpleInjector(new CdiInjectorFactory(BeanManagerLookup.lookup()));
57         injector.inject(pojo);
58
59         MySingleton obj = pojo.getSingleton();
60         assertNotNull(obj);
61
62         MyPojo pojo2 = new MyPojo();
63         injector.inject(pojo2);
64
65         // Objects will not be the same as they are contextual references to the
66         // same object.
67         // assertSame(pojo2, pojo);
68
69         assertEquals(1, MySingleton.getInstances());
70     }
71
72     @Test
73     public void testGetSingletonCustomInjector() {
74         MyPojo pojo = new MyPojo();
75         InjectorFactory factory = mock(InjectorFactory.class);
76         SimpleInjector injector = new SimpleInjector(factory);
77         final MySingleton singleton = new MySingleton();
78         when(factory.create(MyPojo.class)).thenReturn(new Injector() {
79             @Override
80             public void inject(Object aComponent) {
81                 MyPojo pojo2 = (MyPojo) aComponent;
82                 pojo2.setSingleton(singleton); 
83             }
84         });
85         
86         injector.inject(pojo);
87         // verify the custom injector was called. 
88         assertSame(singleton, pojo.getSingleton());
89     }
90 }