(no commit message)
[utils] / support / general / src / test / java / org / wamblee / general / LookupProxyFactoryTest.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.general;
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.general.LookupProxyFactory;
27 import org.wamblee.general.LookupProxyFactory.Lookup;
28 import org.wamblee.general.LookupProxyFactory.LookupException;
29
30 public class LookupProxyFactoryTest {
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 MyInterface intf;
39     private Lookup lookup;
40     private MyInterface proxy;
41
42     @Before
43     public void setUp() throws Exception {
44         intf = mock(MyInterface.class);
45         lookup = mock(Lookup.class);
46         
47         LookupProxyFactory<MyInterface> factory = new LookupProxyFactory<MyInterface>(
48             MyInterface.class, lookup); 
49         proxy = factory.getProxy();
50     }
51
52     @After
53     public void tearDown() throws Exception {
54         // Empty.
55     }
56
57     @Test
58     public void testFound() throws Exception {
59         when(intf.execute()).thenReturn(1);
60         when(lookup.lookup()).thenReturn(intf);
61
62         assertEquals(1, proxy.execute());
63
64     }
65
66     @Test(expected = LookupException.class)
67     public void testNotFoundAtJndi() throws Exception {
68         when(lookup.lookup()).thenThrow(new RuntimeException("Object not found"));
69         proxy.execute();
70     }
71
72     @Test(expected = LookupException.class)
73     public void testWrongTypeAtJndi() throws Exception {
74         when(lookup.lookup()).thenReturn("my string");
75         when(intf.execute()).thenReturn(1);
76         proxy.execute();
77     }
78
79     @Test(expected = LookupException.class)
80     public void testNullAtJndi() throws Exception {
81         when(lookup.lookup()).thenReturn(null);
82         when(intf.execute()).thenReturn(1);
83         proxy.execute();
84     }
85
86     @Test
87     public void testFoundButInvocationThrowsException() throws Exception {
88         when(intf.execute()).thenThrow(new RuntimeException(NAA_NA_NA_NAA_NA));
89         when(lookup.lookup()).thenReturn(intf);
90         try {
91             proxy.execute();
92         } catch (RuntimeException e) {
93             assertEquals(NAA_NA_NA_NAA_NA, e.getMessage());
94         }
95     }
96
97 }