X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=test%2Fenterprise%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Ftest%2Fjndi%2FLookupProxyFactoryTest.java;fp=test%2Fenterprise%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Ftest%2Fjndi%2FLookupProxyFactoryTest.java;h=755769d5deccdf29e71627a942f26f18968f2d98;hb=a79d50a65191abb5d38ec6729758ea6e7ffa6952;hp=0000000000000000000000000000000000000000;hpb=90588ca47c0a3fa9e4f46d506a6e36fbbda5aeaf;p=utils diff --git a/test/enterprise/src/test/java/org/wamblee/test/jndi/LookupProxyFactoryTest.java b/test/enterprise/src/test/java/org/wamblee/test/jndi/LookupProxyFactoryTest.java new file mode 100644 index 00000000..755769d5 --- /dev/null +++ b/test/enterprise/src/test/java/org/wamblee/test/jndi/LookupProxyFactoryTest.java @@ -0,0 +1,96 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.test.jndi; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import javax.naming.InitialContext; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.wamblee.test.jndi.LookupProxyFactory.Lookup; +import org.wamblee.test.jndi.LookupProxyFactory.LookupException; + +public class LookupProxyFactoryTest { + + private static final String NAA_NA_NA_NAA_NA = "naa, na, na, naa, na"; + + private static interface MyInterface { + int execute(); + } + + private MyInterface intf; + private Lookup lookup; + private MyInterface proxy; + + @Before + public void setUp() throws Exception { + intf = mock(MyInterface.class); + lookup = mock(Lookup.class); + + LookupProxyFactory factory = new LookupProxyFactory( + MyInterface.class, lookup); + proxy = factory.getProxy(); + } + + @After + public void tearDown() throws Exception { + // Empty. + } + + @Test + public void testFound() throws Exception { + when(intf.execute()).thenReturn(1); + when(lookup.lookup()).thenReturn(intf); + + assertEquals(1, proxy.execute()); + + } + + @Test(expected = LookupException.class) + public void testNotFoundAtJndi() throws Exception { + when(lookup.lookup()).thenThrow(new RuntimeException("Object not found")); + proxy.execute(); + } + + @Test(expected = LookupException.class) + public void testWrongTypeAtJndi() throws Exception { + when(lookup.lookup()).thenReturn("my string"); + when(intf.execute()).thenReturn(1); + proxy.execute(); + } + + @Test(expected = LookupException.class) + public void testNullAtJndi() throws Exception { + when(lookup.lookup()).thenReturn(null); + when(intf.execute()).thenReturn(1); + proxy.execute(); + } + + @Test + public void testFoundButInvocationThrowsException() throws Exception { + when(intf.execute()).thenThrow(new RuntimeException(NAA_NA_NA_NAA_NA)); + when(lookup.lookup()).thenReturn(intf); + try { + proxy.execute(); + } catch (RuntimeException e) { + assertEquals(NAA_NA_NA_NAA_NA, e.getMessage()); + } + } + +}