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(new RuntimeException("Object not found"));
74 @Test(expected = LookupException.class)
75 public void testWrongTypeAtJndi() throws Exception {
76 when(lookup.lookup()).thenReturn("my string");
77 when(intf.execute()).thenReturn(1);
81 @Test(expected = LookupException.class)
82 public void testNullAtJndi() throws Exception {
83 when(lookup.lookup()).thenReturn(null);
84 when(intf.execute()).thenReturn(1);
89 public void testFoundButInvocationThrowsException() throws Exception {
90 when(intf.execute()).thenThrow(new RuntimeException(NAA_NA_NA_NAA_NA));
91 when(lookup.lookup()).thenReturn(intf);
94 } catch (RuntimeException e) {
95 assertEquals(NAA_NA_NA_NAA_NA, e.getMessage());
99 private static final class MyLookup implements Lookup {
102 public Object lookup() throws Exception {
103 return new MyInterface() {
106 public int execute() {
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();
120 assertEquals(10, proxy.execute());
122 assertTrue(proxy instanceof Serializable);
124 proxy = ObjectSerializationUtils.deserialize(ObjectSerializationUtils.serialize(proxy),
127 // and it should still work
128 assertEquals(10, proxy.execute());