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.test.jndi;
18 import static org.junit.Assert.*;
19 import static org.mockito.Mockito.*;
21 import javax.naming.InitialContext;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.wamblee.test.jndi.LookupProxyFactory.Lookup;
27 import org.wamblee.test.jndi.LookupProxyFactory.LookupException;
29 public class LookupProxyFactoryTest {
31 private static final String NAA_NA_NA_NAA_NA = "naa, na, na, naa, na";
33 private static interface MyInterface {
37 private MyInterface intf;
38 private Lookup lookup;
39 private MyInterface proxy;
42 public void setUp() throws Exception {
43 intf = mock(MyInterface.class);
44 lookup = mock(Lookup.class);
46 LookupProxyFactory<MyInterface> factory = new LookupProxyFactory<MyInterface>(
47 MyInterface.class, lookup);
48 proxy = factory.getProxy();
52 public void tearDown() throws Exception {
57 public void testFound() throws Exception {
58 when(intf.execute()).thenReturn(1);
59 when(lookup.lookup()).thenReturn(intf);
61 assertEquals(1, proxy.execute());
65 @Test(expected = LookupException.class)
66 public void testNotFoundAtJndi() throws Exception {
67 when(lookup.lookup()).thenThrow(new RuntimeException("Object not found"));
71 @Test(expected = LookupException.class)
72 public void testWrongTypeAtJndi() throws Exception {
73 when(lookup.lookup()).thenReturn("my string");
74 when(intf.execute()).thenReturn(1);
78 @Test(expected = LookupException.class)
79 public void testNullAtJndi() throws Exception {
80 when(lookup.lookup()).thenReturn(null);
81 when(intf.execute()).thenReturn(1);
86 public void testFoundButInvocationThrowsException() throws Exception {
87 when(intf.execute()).thenThrow(new RuntimeException(NAA_NA_NA_NAA_NA));
88 when(lookup.lookup()).thenReturn(intf);
91 } catch (RuntimeException e) {
92 assertEquals(NAA_NA_NA_NAA_NA, e.getMessage());