2 * Copyright 2005-2010 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.general;
18 import static org.junit.Assert.*;
19 import static org.mockito.Mockito.*;
21 import java.io.Serializable;
23 import javax.naming.InitialContext;
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;
32 public class LookupProxyFactoryTest {
34 private static final String NAA_NA_NA_NAA_NA = "naa, na, na, naa, na";
36 private static interface MyInterface {
40 private MyInterface intf;
41 private Lookup lookup;
42 private MyInterface proxy;
45 public void setUp() throws Exception {
46 intf = mock(MyInterface.class);
47 lookup = mock(Lookup.class);
49 LookupProxyFactory<MyInterface> factory = new LookupProxyFactory<MyInterface>(
50 MyInterface.class, lookup);
51 proxy = factory.getProxy();
55 public void tearDown() throws Exception {
60 public void testFound() throws Exception {
61 when(intf.execute()).thenReturn(1);
62 when(lookup.lookup()).thenReturn(intf);
64 assertEquals(1, proxy.execute());
68 @Test(expected = LookupException.class)
69 public void testNotFoundAtJndi() throws Exception {
70 when(lookup.lookup()).thenThrow(
71 new RuntimeException("Object not found"));
75 @Test(expected = LookupException.class)
76 public void testWrongTypeAtJndi() throws Exception {
77 when(lookup.lookup()).thenReturn("my string");
78 when(intf.execute()).thenReturn(1);
82 @Test(expected = LookupException.class)
83 public void testNullAtJndi() throws Exception {
84 when(lookup.lookup()).thenReturn(null);
85 when(intf.execute()).thenReturn(1);
90 public void testFoundButInvocationThrowsException() throws Exception {
91 when(intf.execute()).thenThrow(new RuntimeException(NAA_NA_NA_NAA_NA));
92 when(lookup.lookup()).thenReturn(intf);
95 } catch (RuntimeException e) {
96 assertEquals(NAA_NA_NA_NAA_NA, e.getMessage());
100 private static final class MyLookup implements Lookup {
103 public Object lookup() throws Exception {
104 return new MyInterface() {
107 public int execute() {
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();
121 assertEquals(10, proxy.execute());
123 assertTrue(proxy instanceof Serializable);
125 proxy = ObjectSerializationUtils.deserialize(ObjectSerializationUtils
126 .serialize(proxy), MyInterface.class);
128 // and it should still work
129 assertEquals(10, proxy.execute());