(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 java.io.Serializable;
22
23 import javax.naming.InitialContext;
24
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.wamblee.general.LookupProxyFactory;
29 import org.wamblee.general.LookupProxyFactory.Lookup;
30 import org.wamblee.general.LookupProxyFactory.LookupException;
31
32 public class LookupProxyFactoryTest {
33
34     private static final String NAA_NA_NA_NAA_NA = "naa, na, na, naa, na";
35
36     private static interface MyInterface {
37         int execute();
38     }
39
40     private MyInterface intf;
41     private Lookup lookup;
42     private MyInterface proxy;
43
44     @Before
45     public void setUp() throws Exception {
46         intf = mock(MyInterface.class);
47         lookup = mock(Lookup.class);
48         
49         LookupProxyFactory<MyInterface> factory = new LookupProxyFactory<MyInterface>(
50             MyInterface.class, lookup); 
51         proxy = factory.getProxy();
52     }
53
54     @After
55     public void tearDown() throws Exception {
56         // Empty.
57     }
58
59     @Test
60     public void testFound() throws Exception {
61         when(intf.execute()).thenReturn(1);
62         when(lookup.lookup()).thenReturn(intf);
63
64         assertEquals(1, proxy.execute());
65
66     }
67
68     @Test(expected = LookupException.class)
69     public void testNotFoundAtJndi() throws Exception {
70         when(lookup.lookup()).thenThrow(new RuntimeException("Object not found"));
71         proxy.execute();
72     }
73
74     @Test(expected = LookupException.class)
75     public void testWrongTypeAtJndi() throws Exception {
76         when(lookup.lookup()).thenReturn("my string");
77         when(intf.execute()).thenReturn(1);
78         proxy.execute();
79     }
80
81     @Test(expected = LookupException.class)
82     public void testNullAtJndi() throws Exception {
83         when(lookup.lookup()).thenReturn(null);
84         when(intf.execute()).thenReturn(1);
85         proxy.execute();
86     }
87
88     @Test
89     public void testFoundButInvocationThrowsException() throws Exception {
90         when(intf.execute()).thenThrow(new RuntimeException(NAA_NA_NA_NAA_NA));
91         when(lookup.lookup()).thenReturn(intf);
92         try {
93             proxy.execute();
94         } catch (RuntimeException e) {
95             assertEquals(NAA_NA_NA_NAA_NA, e.getMessage());
96         }
97     }
98     
99     private static final class MyLookup implements Lookup { 
100         
101         @Override
102         public Object lookup() throws Exception {
103             return new MyInterface() {
104                 
105                 @Override
106                 public int execute() {
107                     return 10;
108                 }
109             };
110         }
111     }
112     
113     @Test 
114     public void testProxyMustBerializable() throws Exception {
115         lookup = new MyLookup();
116         LookupProxyFactory<MyInterface> factory = new LookupProxyFactory<MyInterface>(
117             MyInterface.class, lookup); 
118         proxy = factory.getProxy();
119         
120         assertEquals(10, proxy.execute());
121         
122         assertTrue(proxy instanceof Serializable); 
123         
124         proxy = ObjectSerializationUtils.deserialize(ObjectSerializationUtils.serialize(proxy), 
125             MyInterface.class);
126         
127         // and it should still work 
128         assertEquals(10, proxy.execute());     
129     }
130     
131
132 }