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