import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import org.wamblee.test.jndi.JndiProxyFactory;
-import org.wamblee.test.jndi.StubInitialContextFactory;
-import org.wamblee.test.jndi.JndiProxyFactory.JndiWiringException;
+import org.wamblee.test.jndi.LookupProxyFactory.Lookup;
+import org.wamblee.test.jndi.LookupProxyFactory.LookupException;
-public class JndiProxyFactoryTest {
+public class LookupProxyFactoryTest {
private static final String NAA_NA_NA_NAA_NA = "naa, na, na, naa, na";
int execute();
}
- private static final String JNDI = "jndi/xyz";
private MyInterface intf;
- private InitialContext ctx;
+ private Lookup lookup;
private MyInterface proxy;
@Before
public void setUp() throws Exception {
- StubInitialContextFactory.register();
intf = mock(MyInterface.class);
- ctx = new InitialContext();
- JndiProxyFactory<MyInterface> factory = new JndiProxyFactory<MyInterface>(
- MyInterface.class, JNDI);
+ lookup = mock(Lookup.class);
+
+ LookupProxyFactory<MyInterface> factory = new LookupProxyFactory<MyInterface>(
+ MyInterface.class, lookup);
proxy = factory.getProxy();
}
@After
public void tearDown() throws Exception {
- StubInitialContextFactory.unregister();
+ // Empty.
}
@Test
- public void testFoundAtJndi() throws Exception {
- ctx.bind(JNDI, intf);
+ public void testFound() throws Exception {
when(intf.execute()).thenReturn(1);
+ when(lookup.lookup()).thenReturn(intf);
+
assertEquals(1, proxy.execute());
}
- @Test(expected = JndiWiringException.class)
- public void testNotFoundAtJndi() {
+ @Test(expected = LookupException.class)
+ public void testNotFoundAtJndi() throws Exception {
+ when(lookup.lookup()).thenThrow(new RuntimeException("Object not found"));
proxy.execute();
}
- @Test(expected = JndiWiringException.class)
+ @Test(expected = LookupException.class)
public void testWrongTypeAtJndi() throws Exception {
- ctx.bind(JNDI, "a string type");
+ when(lookup.lookup()).thenReturn("my string");
when(intf.execute()).thenReturn(1);
- assertEquals(1, proxy.execute());
+ proxy.execute();
}
- @Test(expected = JndiWiringException.class )
+ @Test(expected = LookupException.class)
public void testNullAtJndi() throws Exception {
- ctx.bind(JNDI, null);
+ when(lookup.lookup()).thenReturn(null);
when(intf.execute()).thenReturn(1);
- assertEquals(1, proxy.execute());
+ proxy.execute();
}
@Test
public void testFoundButInvocationThrowsException() throws Exception {
- ctx.bind(JNDI, intf);
when(intf.execute()).thenThrow(new RuntimeException(NAA_NA_NA_NAA_NA));
- try{
- proxy.execute();
- } catch (RuntimeException e) {
- assertEquals(NAA_NA_NA_NAA_NA, e.getMessage());
+ when(lookup.lookup()).thenReturn(intf);
+ try {
+ proxy.execute();
+ } catch (RuntimeException e) {
+ assertEquals(NAA_NA_NA_NAA_NA, e.getMessage());
}
}