(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(
71             new RuntimeException("Object not found"));
72         proxy.execute();
73     }
74
75     @Test(expected = LookupException.class)
76     public void testWrongTypeAtJndi() throws Exception {
77         when(lookup.lookup()).thenReturn("my string");
78         when(intf.execute()).thenReturn(1);
79         proxy.execute();
80     }
81
82     @Test(expected = LookupException.class)
83     public void testNullAtJndi() throws Exception {
84         when(lookup.lookup()).thenReturn(null);
85         when(intf.execute()).thenReturn(1);
86         proxy.execute();
87     }
88
89     @Test
90     public void testFoundButInvocationThrowsException() throws Exception {
91         when(intf.execute()).thenThrow(new RuntimeException(NAA_NA_NA_NAA_NA));
92         when(lookup.lookup()).thenReturn(intf);
93         try {
94             proxy.execute();
95         } catch (RuntimeException e) {
96             assertEquals(NAA_NA_NA_NAA_NA, e.getMessage());
97         }
98     }
99
100     private static final class MyLookup implements Lookup {
101
102         @Override
103         public Object lookup() throws Exception {
104             return new MyInterface() {
105
106                 @Override
107                 public int execute() {
108                     return 10;
109                 }
110             };
111         }
112     }
113
114     @Test
115     public void testProxyMustBerializable() throws Exception {
116         lookup = new MyLookup();
117         LookupProxyFactory<MyInterface> factory = new LookupProxyFactory<MyInterface>(
118             MyInterface.class, lookup);
119         proxy = factory.getProxy();
120
121         assertEquals(10, proxy.execute());
122
123         assertTrue(proxy instanceof Serializable);
124
125         proxy = ObjectSerializationUtils.deserialize(ObjectSerializationUtils
126             .serialize(proxy), MyInterface.class);
127
128         // and it should still work
129         assertEquals(10, proxy.execute());
130     }
131
132 }