package rename for test libraries.
[utils] / support / cdi / src / test / java / org / wamblee / cdi / BeanManagerLookupTest.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.inject.spi.BeanManager;
22 import javax.naming.InitialContext;
23
24 import org.junit.Test;
25 import org.wamblee.test.jndi.StubInitialContextFactory;
26
27 public class BeanManagerLookupTest extends BaseTestFixture {
28   
29     @Test
30     public void testLookupInJndi() throws Exception {
31         BeanManagerSetup setup = new BeanManagerSetup();
32         setup.initialize();
33         StubInitialContextFactory.register();
34         InitialContext ctx = new InitialContext(); 
35         ctx.bind(BeanManagerLookup.BEAN_MANAGER_JNDI, setup.getBeanManager());
36         try {
37             assertSame(setup.getBeanManager(), BeanManagerLookup.lookup());
38         } finally {
39             setup.shutdown();
40             StubInitialContextFactory.unregister();
41         }
42     }
43     
44     @Test
45     public void testWithJndiButWithOverride() throws Exception { 
46         BeanManagerSetup setup = new BeanManagerSetup();
47         setup.initialize();
48         StubInitialContextFactory.register();
49         InitialContext ctx = new InitialContext(); 
50         ctx.bind(BeanManagerLookup.BEAN_MANAGER_JNDI, setup.getBeanManager());
51         try {
52             BeanManager override = mock(BeanManager.class);
53             BeanManagerLookup.setBeanManager(override);
54             assertSame(override, BeanManagerLookup.lookup());
55         } finally {
56             setup.shutdown();
57             StubInitialContextFactory.unregister();
58         }
59     }
60     
61     @Test
62     public void testNoJndiButWithOverride() throws Exception { 
63         BeanManager override = mock(BeanManager.class);
64         BeanManagerLookup.setBeanManager(override);
65         assertSame(override, BeanManagerLookup.lookup());
66     }
67 }