package rename for test libraries.
[utils] / test / enterprise / src / test / java / org / wamblee / test / jndi / JndiProxyFactoryTest.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.test.jndi;
17
18 import static org.junit.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.test.jndi.JndiProxyFactory;
27 import org.wamblee.test.jndi.StubInitialContextFactory;
28 import org.wamblee.test.jndi.JndiProxyFactory.JndiWiringException;
29
30 public class JndiProxyFactoryTest {
31
32     private static final String NAA_NA_NA_NAA_NA = "naa, na, na, naa, na";
33
34     private static interface MyInterface {
35         int execute();
36     }
37
38     private static final String JNDI = "jndi/xyz";
39     private MyInterface intf;
40     private InitialContext ctx;
41     private MyInterface proxy;
42
43     @Before
44     public void setUp() throws Exception {
45         StubInitialContextFactory.register();
46         intf = mock(MyInterface.class);
47         ctx = new InitialContext();
48         JndiProxyFactory<MyInterface> factory = new JndiProxyFactory<MyInterface>(
49             MyInterface.class, JNDI);
50         proxy = factory.getProxy();
51     }
52
53     @After
54     public void tearDown() throws Exception {
55         StubInitialContextFactory.unregister();
56     }
57
58     @Test
59     public void testFoundAtJndi() throws Exception {
60         ctx.bind(JNDI, intf);
61         when(intf.execute()).thenReturn(1);
62         assertEquals(1, proxy.execute());
63
64     }
65
66     @Test(expected = JndiWiringException.class)
67     public void testNotFoundAtJndi() {
68         proxy.execute();
69     }
70
71     @Test(expected = JndiWiringException.class)
72     public void testWrongTypeAtJndi() throws Exception {
73         ctx.bind(JNDI, "a string type");
74         when(intf.execute()).thenReturn(1);
75         assertEquals(1, proxy.execute());
76     }
77
78     @Test(expected = JndiWiringException.class )
79     public void testNullAtJndi() throws Exception {
80         ctx.bind(JNDI, null);
81         when(intf.execute()).thenReturn(1);
82         assertEquals(1, proxy.execute());
83     }
84
85     @Test
86     public void testFoundButInvocationThrowsException() throws Exception {
87         ctx.bind(JNDI, intf);
88         when(intf.execute()).thenThrow(new RuntimeException(NAA_NA_NA_NAA_NA));
89         try{ 
90            proxy.execute();
91         } catch (RuntimeException e) { 
92             assertEquals(NAA_NA_NA_NAA_NA, e.getMessage()); 
93         }
94     }
95
96 }