/** * Release a JNDI context as obtained from {@link #getContext()}. * @param ctx the JNDI context to release (may be {@code null}) * @see #getContext */ public void releaseContext(@Nullable Context ctx) { if (ctx != null) { try { ctx.close(); } catch (NamingException ex) { logger.debug("Could not close JNDI InitialContext", ex); } } }
@Test public void testBind() throws Exception { Object o = new Object(); String name = "foo"; final Context context = mock(Context.class); JndiTemplate jt = new JndiTemplate() { @Override protected Context createInitialContext() { return context; } }; jt.bind(name, o); verify(context).bind(name, o); verify(context).close(); }
@Test public void testRebind() throws Exception { Object o = new Object(); String name = "foo"; final Context context = mock(Context.class); JndiTemplate jt = new JndiTemplate() { @Override protected Context createInitialContext() { return context; } }; jt.rebind(name, o); verify(context).rebind(name, o); verify(context).close(); }
@Test public void testUnbind() throws Exception { String name = "something"; final Context context = mock(Context.class); JndiTemplate jt = new JndiTemplate() { @Override protected Context createInitialContext() { return context; } }; jt.unbind(name); verify(context).unbind(name); verify(context).close(); }
@Test public void testLookupSucceeds() throws Exception { Object o = new Object(); String name = "foo"; final Context context = mock(Context.class); given(context.lookup(name)).willReturn(o); JndiTemplate jt = new JndiTemplate() { @Override protected Context createInitialContext() { return context; } }; Object o2 = jt.lookup(name); assertEquals(o, o2); verify(context).close(); }
/** * Test that it performs the correct lookup. */ @Test public void testPerformsLookup() throws Exception { LocalInterfaceWithBusinessMethods ejb = mock(LocalInterfaceWithBusinessMethods.class); String jndiName= "foobar"; Context mockContext = mockContext(jndiName, ejb); configuredInterceptor(mockContext, jndiName); verify(mockContext).close(); }
@Test public void testLookupFailsWithTypeMismatch() throws Exception { Object o = new Object(); String name = "foo"; final Context context = mock(Context.class); given(context.lookup(name)).willReturn(o); JndiTemplate jt = new JndiTemplate() { @Override protected Context createInitialContext() { return context; } }; try { jt.lookup(name, String.class); fail("Should have thrown TypeMismatchNamingException"); } catch (TypeMismatchNamingException ex) { // Ok } verify(context).close(); }
@Test public void testPerformsLookup() throws Exception { RemoteInterface ejb = mock(RemoteInterface.class); String jndiName= "foobar"; Context mockContext = mockContext(jndiName, ejb); SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName); configuredProxy(si, RemoteInterface.class); verify(mockContext).close(); }
@Test public void testLookupReturnsNull() throws Exception { String name = "foo"; final Context context = mock(Context.class); given(context.lookup(name)).willReturn(null); JndiTemplate jt = new JndiTemplate() { @Override protected Context createInitialContext() { return context; } }; try { jt.lookup(name); fail("Should have thrown NamingException"); } catch (NameNotFoundException ex) { // Ok } verify(context).close(); }
@Test public void testLookupFails() throws Exception { NameNotFoundException ne = new NameNotFoundException(); String name = "foo"; final Context context = mock(Context.class); given(context.lookup(name)).willThrow(ne); JndiTemplate jt = new JndiTemplate() { @Override protected Context createInitialContext() { return context; } }; try { jt.lookup(name); fail("Should have thrown NamingException"); } catch (NameNotFoundException ex) { // Ok } verify(context).close(); }
@Test public void testInvokesMethodOnEjbInstanceWithSeparateBusinessMethods() throws Exception { Object retVal = new Object(); LocalInterface ejb = mock(LocalInterface.class); given(ejb.targetMethod()).willReturn(retVal); String jndiName= "foobar"; Context mockContext = mockContext(jndiName, ejb); LocalSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName); ProxyFactory pf = new ProxyFactory(new Class<?>[] { BusinessMethods.class }); pf.addAdvice(si); BusinessMethods target = (BusinessMethods) pf.getProxy(); assertTrue(target.targetMethod() == retVal); verify(mockContext).close(); verify(ejb).remove(); }
@Test public void testInvokesMethodOnEjbInstance() throws Exception { Object retVal = new Object(); LocalInterfaceWithBusinessMethods ejb = mock(LocalInterfaceWithBusinessMethods.class); given(ejb.targetMethod()).willReturn(retVal); String jndiName= "foobar"; Context mockContext = mockContext(jndiName, ejb); LocalSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName); ProxyFactory pf = new ProxyFactory(new Class<?>[] { BusinessMethods.class }); pf.addAdvice(si); BusinessMethods target = (BusinessMethods) pf.getProxy(); assertTrue(target.targetMethod() == retVal); verify(mockContext).close(); verify(ejb).remove(); }
@Test public void testInvokesMethodOnEjbInstanceWithBusinessInterface() throws Exception { Object retVal = new Object(); final RemoteInterface ejb = mock(RemoteInterface.class); given(ejb.targetMethod()).willReturn(retVal); final String jndiName= "foobar"; Context mockContext = mockContext(jndiName, ejb); SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName); BusinessInterface target = (BusinessInterface) configuredProxy(si, BusinessInterface.class); assertTrue(target.targetMethod() == retVal); verify(mockContext).close(); verify(ejb).remove(); }
private void testException(Exception expected) throws Exception { LocalInterfaceWithBusinessMethods ejb = mock(LocalInterfaceWithBusinessMethods.class); given(ejb.targetMethod()).willThrow(expected); String jndiName= "foobar"; Context mockContext = mockContext(jndiName, ejb); LocalSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName); ProxyFactory pf = new ProxyFactory(new Class<?>[] { LocalInterfaceWithBusinessMethods.class }); pf.addAdvice(si); LocalInterfaceWithBusinessMethods target = (LocalInterfaceWithBusinessMethods) pf.getProxy(); try { target.targetMethod(); fail("Should have thrown exception"); } catch (Exception thrown) { assertTrue(thrown == expected); } verify(mockContext).close(); }
@Test public void testLookupWithExposeAccessContext() throws Exception { JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); TestBean tb = new TestBean(); final Context mockCtx = mock(Context.class); given(mockCtx.lookup("foo")).willReturn(tb); jof.setJndiTemplate(new JndiTemplate() { @Override protected Context createInitialContext() { return mockCtx; } }); jof.setJndiName("foo"); jof.setProxyInterface(ITestBean.class); jof.setExposeAccessContext(true); jof.afterPropertiesSet(); assertTrue(jof.getObject() instanceof ITestBean); ITestBean proxy = (ITestBean) jof.getObject(); assertEquals(0, tb.getAge()); proxy.setAge(99); assertEquals(99, tb.getAge()); proxy.equals(proxy); proxy.hashCode(); proxy.toString(); verify(mockCtx, times(2)).close(); }
@Test public void testInvokesMethodOnEjbInstanceWithBusinessInterfaceWithRemoteException() throws Exception { final RemoteInterface ejb = mock(RemoteInterface.class); given(ejb.targetMethod()).willThrow(new RemoteException()); final String jndiName= "foobar"; Context mockContext = mockContext(jndiName, ejb); SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName); BusinessInterface target = (BusinessInterface) configuredProxy(si, BusinessInterface.class); try { target.targetMethod(); fail("Should have thrown RemoteAccessException"); } catch (RemoteAccessException ex) { // expected } verify(mockContext).close(); verify(ejb).remove(); }
@Test public void testPerformsLookupWithAccessContext() throws Exception { RemoteInterface ejb = mock(RemoteInterface.class); String jndiName= "foobar"; Context mockContext = mockContext(jndiName, ejb); SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName); si.setExposeAccessContext(true); RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class); assertNull(target.targetMethod()); verify(mockContext, times(2)).close(); verify(ejb).targetMethod(); }
@Test public void testInvokesMethodOnEjbInstanceWithRemoteException() throws Exception { final RemoteInterface ejb = mock(RemoteInterface.class); given(ejb.targetMethod()).willThrow(new RemoteException()); ejb.remove(); final String jndiName= "foobar"; Context mockContext = mockContext(jndiName, ejb); SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName); RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class); try { target.targetMethod(); fail("Should have thrown RemoteException"); } catch (RemoteException ex) { // expected } verify(mockContext).close(); verify(ejb, times(2)).remove(); }
private void doTestException(Exception expected) throws Exception { final RemoteInterface ejb = mock(RemoteInterface.class); given(ejb.targetMethod()).willThrow(expected); final String jndiName= "foobar"; Context mockContext = mockContext(jndiName, ejb); SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName); RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class); try { target.targetMethod(); fail("Should have thrown remote exception"); } catch (Exception thrown) { assertTrue(thrown == expected); } verify(mockContext).close(); verify(ejb).remove(); }
private void doTestInvokesMethodOnEjbInstance(boolean lookupHomeOnStartup, boolean cacheHome) throws Exception { Object retVal = new Object(); final RemoteInterface ejb = mock(RemoteInterface.class); given(ejb.targetMethod()).willReturn(retVal); int lookupCount = 1; if (!cacheHome) { lookupCount++; if (lookupHomeOnStartup) { lookupCount++; } } final String jndiName= "foobar"; Context mockContext = mockContext(jndiName, ejb); SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName); si.setLookupHomeOnStartup(lookupHomeOnStartup); si.setCacheHome(cacheHome); RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class); assertTrue(target.targetMethod() == retVal); assertTrue(target.targetMethod() == retVal); verify(mockContext, times(lookupCount)).close(); verify(ejb, times(2)).remove(); }