diff --git a/sandbox/src/test/java/org/springframework/ldap/pool/AbstractPoolTestCase.java b/sandbox/src/test/java/org/springframework/ldap/pool/AbstractPoolTestCase.java new file mode 100644 index 00000000..9eceffa8 --- /dev/null +++ b/sandbox/src/test/java/org/springframework/ldap/pool/AbstractPoolTestCase.java @@ -0,0 +1,108 @@ +package org.springframework.ldap.pool; + +import javax.naming.Context; +import javax.naming.directory.DirContext; +import javax.naming.ldap.LdapContext; + +import org.apache.commons.pool.KeyedObjectPool; +import org.easymock.MockControl; +import org.springframework.ldap.core.ContextSource; +import org.springframework.ldap.validation.DirContextValidator; + +import junit.framework.TestCase; + +/** + * Contains mocks common to many tests for the connection pool. + * + * @author Ulrik Sandberg + */ +public abstract class AbstractPoolTestCase extends TestCase { + + protected MockControl contextControl; + + protected Context contextMock; + + protected MockControl dirContextControl; + + protected DirContext dirContextMock; + + protected MockControl ldapContextControl; + + protected LdapContext ldapContextMock; + + protected MockControl keyedObjectPoolControl; + + protected KeyedObjectPool keyedObjectPoolMock; + + protected MockControl contextSourceControl; + + protected ContextSource contextSourceMock; + + protected MockControl dirContextValidatorControl; + + protected DirContextValidator dirContextValidatorMock; + + protected void setUp() throws Exception { + super.setUp(); + + contextControl = MockControl.createControl(Context.class); + contextMock = (Context) contextControl.getMock(); + + dirContextControl = MockControl.createControl(DirContext.class); + dirContextMock = (DirContext) dirContextControl.getMock(); + + ldapContextControl = MockControl.createControl(LdapContext.class); + ldapContextMock = (LdapContext) ldapContextControl.getMock(); + + keyedObjectPoolControl = MockControl + .createControl(KeyedObjectPool.class); + keyedObjectPoolMock = (KeyedObjectPool) keyedObjectPoolControl + .getMock(); + + contextSourceControl = MockControl.createControl(ContextSource.class); + contextSourceMock = (ContextSource) contextSourceControl.getMock(); + + dirContextValidatorControl = MockControl.createControl(DirContextValidator.class); + dirContextValidatorMock = (DirContextValidator) dirContextValidatorControl.getMock(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + + contextControl = null; + contextMock = null; + + dirContextControl = null; + dirContextMock = null; + + ldapContextControl = null; + ldapContextMock = null; + + keyedObjectPoolControl = null; + keyedObjectPoolMock = null; + + contextSourceControl = null; + contextSourceMock = null; + + dirContextValidatorControl = null; + dirContextValidatorMock = null; + } + + protected void replay() { + contextControl.replay(); + dirContextControl.replay(); + ldapContextControl.replay(); + keyedObjectPoolControl.replay(); + contextSourceControl.replay(); + dirContextValidatorControl.replay(); + } + + protected void verify() { + contextControl.verify(); + dirContextControl.verify(); + ldapContextControl.verify(); + keyedObjectPoolControl.verify(); + contextSourceControl.verify(); + dirContextValidatorControl.verify(); + } +} diff --git a/sandbox/src/test/java/org/springframework/ldap/pool/DelegatingContextTest.java b/sandbox/src/test/java/org/springframework/ldap/pool/DelegatingContextTest.java new file mode 100644 index 00000000..cbd5397b --- /dev/null +++ b/sandbox/src/test/java/org/springframework/ldap/pool/DelegatingContextTest.java @@ -0,0 +1,425 @@ +package org.springframework.ldap.pool; + +import javax.naming.Context; +import javax.naming.Name; +import javax.naming.NamingException; + +import org.apache.commons.pool.KeyedObjectPool; +import org.easymock.MockControl; + +/** + * @author Eric Dalquist eric.dalquist@doit.wisc.edu + */ +public class DelegatingContextTest extends AbstractPoolTestCase { + + public void testConstructorAssertions() { + + replay(); + + try { + new DelegatingContext(null, contextMock, DirContextType.READ_ONLY); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + + try { + new DelegatingContext(keyedObjectPoolMock, null, + DirContextType.READ_ONLY); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + + try { + new DelegatingContext(keyedObjectPoolMock, contextMock, null); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + + verify(); + } + + public void testHelperMethods() throws Exception { + keyedObjectPoolMock.returnObject(DirContextType.READ_ONLY, contextMock); + keyedObjectPoolControl.setVoidCallable(1); + + replay(); + + // Wrap the Context once + final DelegatingContext delegatingContext = new DelegatingContext( + keyedObjectPoolMock, contextMock, DirContextType.READ_ONLY); + + final Context delegateContext = delegatingContext.getDelegateContext(); + assertEquals(contextMock, delegateContext); + + final Context innerDelegateContext = delegatingContext + .getInnermostDelegateContext(); + assertEquals(contextMock, innerDelegateContext); + + delegatingContext.assertOpen(); + + // Wrap the wrapper + MockControl secondKeyedObjectPoolControl = MockControl + .createControl(KeyedObjectPool.class); + KeyedObjectPool secondKeyedObjectPoolMock = (KeyedObjectPool) secondKeyedObjectPoolControl + .getMock(); + secondKeyedObjectPoolMock.returnObject(DirContextType.READ_ONLY, + delegatingContext); + secondKeyedObjectPoolControl.setVoidCallable(1); + secondKeyedObjectPoolControl.replay(); + + final DelegatingContext delegatingContext2 = new DelegatingContext( + secondKeyedObjectPoolMock, delegatingContext, + DirContextType.READ_ONLY); + + final Context delegateContext2 = delegatingContext2 + .getDelegateContext(); + assertEquals(delegatingContext, delegateContext2); + + final Context innerDelegateContext2 = delegatingContext2 + .getInnermostDelegateContext(); + assertEquals(contextMock, innerDelegateContext2); + + delegatingContext2.assertOpen(); + + // Close the outer wrapper + delegatingContext2.close(); + + final Context delegateContext2closed = delegatingContext2 + .getDelegateContext(); + assertNull(delegateContext2closed); + + final Context innerDelegateContext2closed = delegatingContext2 + .getInnermostDelegateContext(); + assertNull(innerDelegateContext2closed); + + try { + delegatingContext2.assertOpen(); + fail("delegatingContext2.assertOpen() should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + + // Close the outer wrapper + delegatingContext.close(); + + final Context delegateContextclosed = delegatingContext + .getDelegateContext(); + assertNull(delegateContextclosed); + + final Context innerDelegateContextclosed = delegatingContext + .getInnermostDelegateContext(); + assertNull(innerDelegateContextclosed); + + try { + delegatingContext.assertOpen(); + fail("delegatingContext.assertOpen() should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + + verify(); + secondKeyedObjectPoolControl.verify(); + } + + public void testObjectMethods() throws Exception { + keyedObjectPoolMock.returnObject(DirContextType.READ_ONLY, contextMock); + keyedObjectPoolControl.setVoidCallable(1); + + replay(); + + // Wrap the Context once + final DelegatingContext delegatingContext = new DelegatingContext( + keyedObjectPoolMock, contextMock, DirContextType.READ_ONLY); + assertEquals("EasyMock for interface javax.naming.Context", + delegatingContext.toString()); + delegatingContext.hashCode(); // Run it to make sure it doesn't fail + + assertTrue(delegatingContext.equals(delegatingContext)); + assertFalse(delegatingContext.equals(new Object())); + + final DelegatingContext delegatingContext2 = new DelegatingContext( + keyedObjectPoolMock, contextMock, DirContextType.READ_ONLY); + assertTrue(delegatingContext.equals(delegatingContext2)); + assertTrue(delegatingContext2.equals(delegatingContext)); + assertTrue(delegatingContext.equals(contextMock)); + + // Close the contextMock and try again + delegatingContext.close(); + + assertEquals("Context is closed", delegatingContext.toString()); + assertEquals(0, delegatingContext.hashCode()); // Run it to make sure + // it doesn't fail + + assertTrue(delegatingContext.equals(delegatingContext)); + assertFalse(delegatingContext.equals(new Object())); + + assertFalse(delegatingContext.equals(delegatingContext2)); + assertFalse(delegatingContext2.equals(delegatingContext)); + assertFalse(delegatingContext.equals(contextMock)); + + verify(); + } + + public void testUnsupportedMethods() throws Exception { + + replay(); + + final DelegatingContext delegatingContext = new DelegatingContext( + keyedObjectPoolMock, contextMock, DirContextType.READ_ONLY); + + try { + delegatingContext.addToEnvironment(null, null); + fail("DelegatingContext.addToEnvironment Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingContext.bind((Name) null, null); + fail("DelegatingContext.addToEnvironment Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingContext.bind((String) null, null); + fail("DelegatingContext.bind Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingContext.createSubcontext((Name) null); + fail("DelegatingContext.createSubcontext Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingContext.createSubcontext((String) null); + fail("DelegatingContext.createSubcontext Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingContext.destroySubcontext((Name) null); + fail("DelegatingContext.destroySubcontext Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingContext.destroySubcontext((String) null); + fail("DelegatingContext.destroySubcontext Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingContext.rebind((Name) null, null); + fail("DelegatingContext.rebind Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingContext.rebind((String) null, null); + fail("DelegatingContext.rebind Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingContext.removeFromEnvironment(null); + fail("DelegatingContext.removeFromEnvironment Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingContext.unbind((Name) null); + fail("DelegatingContext.unbind Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingContext.unbind((String) null); + fail("DelegatingContext.unbind Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + verify(); + } + + public void testAllMethodsOpened() throws Exception { + contextControl = MockControl.createNiceControl(Context.class); + contextMock = (Context) contextControl.getMock(); + + replay(); + + final DelegatingContext delegatingContext = new DelegatingContext( + keyedObjectPoolMock, contextMock, DirContextType.READ_ONLY); + + delegatingContext.composeName((Name) null, (Name) null); + delegatingContext.composeName((String) null, (String) null); + delegatingContext.getEnvironment(); + delegatingContext.getNameInNamespace(); + delegatingContext.getNameParser((Name) null); + delegatingContext.getNameParser((String) null); + delegatingContext.list((Name) null); + delegatingContext.list((String) null); + delegatingContext.listBindings((Name) null); + delegatingContext.listBindings((String) null); + delegatingContext.lookup((Name) null); + delegatingContext.lookup((String) null); + delegatingContext.lookupLink((Name) null); + delegatingContext.lookupLink((String) null); + delegatingContext.rename((Name) null, (Name) null); + delegatingContext.rename((String) null, (String) null); + + verify(); + } + + public void testAllMethodsClosed() throws Exception { + keyedObjectPoolMock.returnObject(DirContextType.READ_ONLY, contextMock); + keyedObjectPoolControl.setVoidCallable(1); + + replay(); + + final DelegatingContext delegatingContext = new DelegatingContext( + keyedObjectPoolMock, contextMock, DirContextType.READ_ONLY); + + delegatingContext.close(); + + try { + delegatingContext.composeName((Name) null, (Name) null); + fail("DelegatingContext.composeName should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingContext.composeName((String) null, (String) null); + fail("DelegatingContext.composeName should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingContext.getEnvironment(); + fail("DelegatingContext.getEnvironment should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingContext.getNameInNamespace(); + fail("DelegatingContext.getNameInNamespace should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingContext.getNameParser((Name) null); + fail("DelegatingContext.getNameParser should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingContext.getNameParser((String) null); + fail("DelegatingContext.getNameParser should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingContext.list((Name) null); + fail("DelegatingContext.list should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingContext.list((String) null); + fail("DelegatingContext.list should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingContext.listBindings((Name) null); + fail("DelegatingContext.listBindings should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingContext.listBindings((String) null); + fail("DelegatingContext.listBindings should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingContext.lookup((Name) null); + fail("DelegatingContext.lookup should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingContext.lookup((String) null); + fail("DelegatingContext.lookup should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingContext.lookupLink((Name) null); + fail("DelegatingContext.lookupLink should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingContext.lookupLink((String) null); + fail("DelegatingContext.lookupLink should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingContext.rename((Name) null, (Name) null); + fail("DelegatingContext.rename should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingContext.rename((String) null, (String) null); + fail("DelegatingContext.rename should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + + verify(); + } + + public void testDoubleClose() throws Exception { + keyedObjectPoolMock.returnObject(DirContextType.READ_ONLY, contextMock); + keyedObjectPoolControl.setVoidCallable(1); + + replay(); + + final DelegatingContext delegatingContext = new DelegatingContext( + keyedObjectPoolMock, contextMock, DirContextType.READ_ONLY); + + delegatingContext.close(); + + // noop close + delegatingContext.close(); + + verify(); + } + + public void testPoolExceptionOnClose() throws Exception { + keyedObjectPoolMock.returnObject(DirContextType.READ_ONLY, contextMock); + keyedObjectPoolControl.setThrowable(new Exception( + "Fake Pool returnObject Exception")); + + replay(); + + final DelegatingContext delegatingContext = new DelegatingContext( + keyedObjectPoolMock, contextMock, DirContextType.READ_ONLY); + + try { + delegatingContext.close(); + fail("DelegatingContext.close should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + + verify(); + } +} diff --git a/sandbox/src/test/java/org/springframework/ldap/pool/DelegatingDirContextTest.java b/sandbox/src/test/java/org/springframework/ldap/pool/DelegatingDirContextTest.java new file mode 100644 index 00000000..6c3e157a --- /dev/null +++ b/sandbox/src/test/java/org/springframework/ldap/pool/DelegatingDirContextTest.java @@ -0,0 +1,400 @@ +package org.springframework.ldap.pool; + +import javax.naming.Context; +import javax.naming.Name; +import javax.naming.NamingException; +import javax.naming.directory.Attributes; +import javax.naming.directory.DirContext; + +import org.apache.commons.pool.KeyedObjectPool; +import org.easymock.MockControl; + +/** + * @author Eric Dalquist eric.dalquist@doit.wisc.edu + */ +public class DelegatingDirContextTest extends AbstractPoolTestCase { + public void testConstructorAssertions() { + replay(); + + try { + new DelegatingDirContext(keyedObjectPoolMock, null, + DirContextType.READ_ONLY); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + + try { + new DelegatingDirContext(keyedObjectPoolMock, dirContextMock, null); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + + verify(); + } + + public void testHelperMethods() throws Exception { + keyedObjectPoolMock.returnObject(DirContextType.READ_ONLY, + dirContextMock); + keyedObjectPoolControl.setVoidCallable(1); + + replay(); + + // Wrap the DirContext once + final DelegatingDirContext delegatingDirContext = new DelegatingDirContext( + keyedObjectPoolMock, dirContextMock, DirContextType.READ_ONLY); + + final Context delegateContext = delegatingDirContext + .getDelegateContext(); + assertEquals(dirContextMock, delegateContext); + + final DirContext delegateDirContext = delegatingDirContext + .getDelegateDirContext(); + assertEquals(dirContextMock, delegateDirContext); + + final DirContext innerDelegateDirContext = delegatingDirContext + .getInnermostDelegateDirContext(); + assertEquals(dirContextMock, innerDelegateDirContext); + + delegatingDirContext.assertOpen(); + + // Wrap the wrapper + MockControl secondKeyedObjectPoolControl = MockControl + .createControl(KeyedObjectPool.class); + KeyedObjectPool secondKeyedObjectPoolMock = (KeyedObjectPool) secondKeyedObjectPoolControl + .getMock(); + secondKeyedObjectPoolMock.returnObject(DirContextType.READ_ONLY, + delegatingDirContext); + secondKeyedObjectPoolControl.setVoidCallable(1); + secondKeyedObjectPoolControl.replay(); + + final DelegatingDirContext delegatingDirContext2 = new DelegatingDirContext( + secondKeyedObjectPoolMock, delegatingDirContext, + DirContextType.READ_ONLY); + + final DirContext delegateDirContext2 = delegatingDirContext2 + .getDelegateDirContext(); + assertEquals(delegatingDirContext, delegateDirContext2); + + final DirContext innerDelegateDirContext2 = delegatingDirContext2 + .getInnermostDelegateDirContext(); + assertEquals(dirContextMock, innerDelegateDirContext2); + + delegatingDirContext2.assertOpen(); + + // Close the outer wrapper + delegatingDirContext2.close(); + + final DirContext delegateContext2closed = delegatingDirContext2 + .getDelegateDirContext(); + assertNull(delegateContext2closed); + + final DirContext innerDelegateContext2closed = delegatingDirContext2 + .getInnermostDelegateDirContext(); + assertNull(innerDelegateContext2closed); + + try { + delegatingDirContext2.assertOpen(); + fail("delegatingDirContext2.assertOpen() should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + + // Close the outer wrapper + delegatingDirContext.close(); + + final DirContext delegateDirContextClosed = delegatingDirContext + .getDelegateDirContext(); + assertNull(delegateDirContextClosed); + + final DirContext innerDelegateDirContextClosed = delegatingDirContext + .getInnermostDelegateDirContext(); + assertNull(innerDelegateDirContextClosed); + + try { + delegatingDirContext.assertOpen(); + fail("delegatingDirContext.assertOpen() should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + + secondKeyedObjectPoolControl.verify(); + verify(); + } + + public void testObjectMethods() throws Exception { + keyedObjectPoolMock.returnObject(DirContextType.READ_ONLY, + dirContextMock); + keyedObjectPoolControl.setVoidCallable(1); + + replay(); + + // Wrap the DirContext once + final DelegatingDirContext delegatingDirContext = new DelegatingDirContext( + keyedObjectPoolMock, dirContextMock, DirContextType.READ_ONLY); + assertEquals( + "EasyMock for interface javax.naming.directory.DirContext", + delegatingDirContext.toString()); + delegatingDirContext.hashCode(); // Run it to make sure it doesn't + // fail + + assertTrue(delegatingDirContext.equals(delegatingDirContext)); + assertFalse(delegatingDirContext.equals(new Object())); + + final DelegatingDirContext delegatingDirContext2 = new DelegatingDirContext( + keyedObjectPoolMock, dirContextMock, DirContextType.READ_ONLY); + assertTrue(delegatingDirContext.equals(delegatingDirContext2)); + assertTrue(delegatingDirContext2.equals(delegatingDirContext)); + assertTrue(delegatingDirContext.equals(dirContextMock)); + + // Close the context and try again + delegatingDirContext.close(); + + assertEquals("DirContext is closed", delegatingDirContext.toString()); + assertEquals(0, delegatingDirContext.hashCode()); // Run it to make + // sure it doesn't + // fail + + assertTrue(delegatingDirContext.equals(delegatingDirContext)); + assertFalse(delegatingDirContext.equals(new Object())); + + assertFalse(delegatingDirContext.equals(delegatingDirContext2)); + assertFalse(delegatingDirContext2.equals(delegatingDirContext)); + assertFalse(delegatingDirContext.equals(dirContextMock)); + + verify(); + } + + // nice + public void testUnsupportedMethods() throws Exception { + replay(); + + final DelegatingDirContext delegatingDirContext = new DelegatingDirContext( + keyedObjectPoolMock, dirContextMock, DirContextType.READ_ONLY); + + try { + delegatingDirContext.bind((Name) null, null, null); + fail("DelegatingDirContext.bind Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingDirContext.bind((String) null, null, null); + fail("DelegatingDirContext.bind Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingDirContext.createSubcontext((Name) null, null); + fail("DelegatingDirContext.createSubcontext Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingDirContext.createSubcontext((String) null, null); + fail("DelegatingDirContext.createSubcontext Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingDirContext.getSchema((Name) null); + fail("DelegatingDirContext.getSchema Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingDirContext.getSchema((String) null); + fail("DelegatingDirContext.getSchema Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingDirContext.getSchemaClassDefinition((Name) null); + fail("DelegatingDirContext.getSchemaClassDefinition Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingDirContext.getSchemaClassDefinition((String) null); + fail("DelegatingDirContext.getSchemaClassDefinition Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingDirContext.rebind((Name) null, null, null); + fail("DelegatingDirContext.rebind Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingDirContext.rebind((String) null, null, null); + fail("DelegatingDirContext.rebind Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + + verify(); + } + + public void testAllMethodsOpened() throws Exception { + // override with a nice control + dirContextControl = MockControl.createNiceControl(DirContext.class); + dirContextMock = (DirContext) dirContextControl.getMock(); + + replay(); + + final DelegatingDirContext delegatingDirContext = new DelegatingDirContext( + keyedObjectPoolMock, dirContextMock, DirContextType.READ_ONLY); + + delegatingDirContext.getAttributes((Name) null, null); + delegatingDirContext.getAttributes((Name) null); + delegatingDirContext.getAttributes((String) null, null); + delegatingDirContext.getAttributes((String) null); + delegatingDirContext.modifyAttributes((Name) null, 0, null); + delegatingDirContext.modifyAttributes((Name) null, null); + delegatingDirContext.modifyAttributes((String) null, 0, null); + delegatingDirContext.modifyAttributes((String) null, null); + delegatingDirContext.search((Name) null, (Attributes) null, null); + delegatingDirContext.search((Name) null, null); + delegatingDirContext.search((Name) null, null, null, null); + delegatingDirContext.search((Name) null, (String) null, null); + delegatingDirContext.search((String) null, (Attributes) null, null); + delegatingDirContext.search((String) null, null); + delegatingDirContext.search((String) null, null, null, null); + delegatingDirContext.search((String) null, (String) null, null); + + verify(); + } + + public void testAllMethodsClosed() throws Exception { + keyedObjectPoolMock.returnObject(DirContextType.READ_ONLY, + dirContextMock); + keyedObjectPoolControl.setVoidCallable(1); + + replay(); + + final DelegatingDirContext delegatingDirContext = new DelegatingDirContext( + keyedObjectPoolMock, dirContextMock, DirContextType.READ_ONLY); + + delegatingDirContext.close(); + + try { + delegatingDirContext.getAttributes((Name) null, null); + fail("DelegatingDirContext.getAttributes should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingDirContext.getAttributes((Name) null); + fail("DelegatingDirContext.getAttributes should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingDirContext.getAttributes((String) null, null); + fail("DelegatingDirContext.getAttributes should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingDirContext.getAttributes((String) null); + fail("DelegatingDirContext.getAttributes should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingDirContext.modifyAttributes((Name) null, 0, null); + fail("DelegatingDirContext.modifyAttributes should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingDirContext.modifyAttributes((Name) null, null); + fail("DelegatingDirContext.modifyAttributes should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingDirContext.modifyAttributes((String) null, 0, null); + fail("DelegatingDirContext.modifyAttributes should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingDirContext.modifyAttributes((String) null, null); + fail("DelegatingDirContext.modifyAttributes should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingDirContext.search((Name) null, (Attributes) null, null); + fail("DelegatingDirContext.search should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingDirContext.search((Name) null, null); + fail("DelegatingDirContext.search should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingDirContext.search((Name) null, null, null, null); + fail("DelegatingDirContext.search should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingDirContext.search((Name) null, (String) null, null); + fail("DelegatingDirContext.search should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingDirContext.search((String) null, (Attributes) null, null); + fail("DelegatingDirContext.search should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingDirContext.search((String) null, null); + fail("DelegatingDirContext.search should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingDirContext.search((String) null, null, null, null); + fail("DelegatingDirContext.search should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingDirContext.search((String) null, (String) null, null); + fail("DelegatingDirContext.search should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + + verify(); + } + + public void testDoubleClose() throws Exception { + keyedObjectPoolMock.returnObject(DirContextType.READ_ONLY, + dirContextMock); + keyedObjectPoolControl.setVoidCallable(1); + + replay(); + + final DelegatingDirContext delegatingDirContext = new DelegatingDirContext( + keyedObjectPoolMock, dirContextMock, DirContextType.READ_ONLY); + + delegatingDirContext.close(); + + // noop close + delegatingDirContext.close(); + + verify(); + } +} \ No newline at end of file diff --git a/sandbox/src/test/java/org/springframework/ldap/pool/DelegatingLdapContextTest.java b/sandbox/src/test/java/org/springframework/ldap/pool/DelegatingLdapContextTest.java new file mode 100644 index 00000000..c51be55c --- /dev/null +++ b/sandbox/src/test/java/org/springframework/ldap/pool/DelegatingLdapContextTest.java @@ -0,0 +1,272 @@ +package org.springframework.ldap.pool; + +import javax.naming.NamingException; +import javax.naming.directory.DirContext; +import javax.naming.ldap.LdapContext; + +import org.apache.commons.pool.KeyedObjectPool; +import org.easymock.MockControl; + +/** + * @author Eric Dalquist eric.dalquist@doit.wisc.edu + */ +public class DelegatingLdapContextTest extends AbstractPoolTestCase { + public void testConstructorAssertions() { + replay(); + + try { + new DelegatingLdapContext(keyedObjectPoolMock, null, + DirContextType.READ_ONLY); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + + try { + new DelegatingLdapContext(keyedObjectPoolMock, ldapContextMock, + null); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + + verify(); + } + + public void testHelperMethods() throws Exception { + keyedObjectPoolMock.returnObject(DirContextType.READ_ONLY, + ldapContextMock); + keyedObjectPoolControl.setVoidCallable(1); + + replay(); + + // Wrap the LdapContext once + final DelegatingLdapContext delegatingLdapContext = new DelegatingLdapContext( + keyedObjectPoolMock, ldapContextMock, DirContextType.READ_ONLY); + + final DirContext delegateDirContext = delegatingLdapContext + .getDelegateDirContext(); + assertEquals(ldapContextMock, delegateDirContext); + + final LdapContext delegateLdapContext = delegatingLdapContext + .getDelegateLdapContext(); + assertEquals(ldapContextMock, delegateLdapContext); + + final LdapContext innerDelegateLdapContext = delegatingLdapContext + .getInnermostDelegateLdapContext(); + assertEquals(ldapContextMock, innerDelegateLdapContext); + + delegatingLdapContext.assertOpen(); + + // Wrap the wrapper + MockControl secondKeyedObjectPoolControl = MockControl + .createControl(KeyedObjectPool.class); + KeyedObjectPool secondKeyedObjectPoolMock = (KeyedObjectPool) secondKeyedObjectPoolControl + .getMock(); + + secondKeyedObjectPoolMock.returnObject(DirContextType.READ_ONLY, + delegatingLdapContext); + secondKeyedObjectPoolControl.setVoidCallable(1); + secondKeyedObjectPoolControl.replay(); + + final DelegatingLdapContext delegatingLdapContext2 = new DelegatingLdapContext( + secondKeyedObjectPoolMock, delegatingLdapContext, + DirContextType.READ_ONLY); + + final LdapContext delegateLdapContext2 = delegatingLdapContext2 + .getDelegateLdapContext(); + assertEquals(delegatingLdapContext, delegateLdapContext2); + + final LdapContext innerDelegateLdapContext2 = delegatingLdapContext2 + .getInnermostDelegateLdapContext(); + assertEquals(ldapContextMock, innerDelegateLdapContext2); + + delegatingLdapContext2.assertOpen(); + + // Close the outer wrapper + delegatingLdapContext2.close(); + + final LdapContext delegateContext2closed = delegatingLdapContext2 + .getDelegateLdapContext(); + assertNull(delegateContext2closed); + + final LdapContext innerDelegateContext2closed = delegatingLdapContext2 + .getInnermostDelegateLdapContext(); + assertNull(innerDelegateContext2closed); + + try { + delegatingLdapContext2.assertOpen(); + fail("delegatingLdapContext2.assertOpen() should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + + // Close the outer wrapper + delegatingLdapContext.close(); + + final LdapContext delegateLdapContextClosed = delegatingLdapContext + .getDelegateLdapContext(); + assertNull(delegateLdapContextClosed); + + final LdapContext innerDelegateLdapContextClosed = delegatingLdapContext + .getInnermostDelegateLdapContext(); + assertNull(innerDelegateLdapContextClosed); + + try { + delegatingLdapContext.assertOpen(); + fail("delegatingLdapContext.assertOpen() should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + + secondKeyedObjectPoolControl.verify(); + verify(); + } + + public void testObjectMethods() throws Exception { + keyedObjectPoolMock.returnObject(DirContextType.READ_ONLY, + ldapContextMock); + keyedObjectPoolControl.setVoidCallable(1); + + replay(); + + // Wrap the LdapContext once + final DelegatingLdapContext delegatingLdapContext = new DelegatingLdapContext( + keyedObjectPoolMock, ldapContextMock, DirContextType.READ_ONLY); + assertEquals("EasyMock for interface javax.naming.ldap.LdapContext", + delegatingLdapContext.toString()); + delegatingLdapContext.hashCode(); // Run it to make sure it doesn't fail + + assertTrue(delegatingLdapContext.equals(delegatingLdapContext)); + assertFalse(delegatingLdapContext.equals(new Object())); + + final DelegatingLdapContext delegatingLdapContext2 = new DelegatingLdapContext( + keyedObjectPoolMock, ldapContextMock, DirContextType.READ_ONLY); + assertTrue(delegatingLdapContext.equals(delegatingLdapContext2)); + assertTrue(delegatingLdapContext2.equals(delegatingLdapContext)); + assertTrue(delegatingLdapContext.equals(ldapContextMock)); + + // Close the context and try again + delegatingLdapContext.close(); + + assertEquals("LdapContext is closed", delegatingLdapContext.toString()); + assertEquals(0, delegatingLdapContext.hashCode()); // Run it to make + // sure it doesn't + // fail + + assertTrue(delegatingLdapContext.equals(delegatingLdapContext)); + assertFalse(delegatingLdapContext.equals(new Object())); + + assertFalse(delegatingLdapContext.equals(delegatingLdapContext2)); + assertFalse(delegatingLdapContext2.equals(delegatingLdapContext)); + assertFalse(delegatingLdapContext.equals(ldapContextMock)); + + verify(); + } + + public void testUnsupportedMethods() throws Exception { + replay(); + + final DelegatingLdapContext delegatingLdapContext = new DelegatingLdapContext( + keyedObjectPoolMock, ldapContextMock, DirContextType.READ_ONLY); + + try { + delegatingLdapContext.newInstance(null); + fail("DelegatingLdapContext.newInstance Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingLdapContext.reconnect(null); + fail("DelegatingLdapContext.reconnect Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + try { + delegatingLdapContext.setRequestControls(null); + fail("DelegatingLdapContext.setRequestControls Should have thrown an UnsupportedOperationException"); + } catch (UnsupportedOperationException uoe) { + // Expected + } + + verify(); + } + + // nice + public void testAllMethodsOpened() throws Exception { + MockControl ldapContextControl = MockControl.createNiceControl(LdapContext.class); + LdapContext ldapContextMock = (LdapContext) ldapContextControl.getMock(); + + ldapContextControl.replay(); + replay(); + + final DelegatingLdapContext delegatingLdapContext = new DelegatingLdapContext( + keyedObjectPoolMock, ldapContextMock, DirContextType.READ_ONLY); + + delegatingLdapContext.extendedOperation(null); + delegatingLdapContext.getConnectControls(); + delegatingLdapContext.getRequestControls(); + delegatingLdapContext.getResponseControls(); + + ldapContextControl.verify(); + verify(); + } + + public void testAllMethodsClosed() throws Exception { + keyedObjectPoolMock.returnObject(DirContextType.READ_ONLY, + ldapContextMock); + keyedObjectPoolControl.setVoidCallable(1); + + replay(); + + final DelegatingLdapContext delegatingLdapContext = new DelegatingLdapContext( + keyedObjectPoolMock, ldapContextMock, DirContextType.READ_ONLY); + + delegatingLdapContext.close(); + + try { + delegatingLdapContext.extendedOperation(null); + fail("DelegatingLdapContext.extendedOperation should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingLdapContext.getConnectControls(); + fail("DelegatingLdapContext.getConnectControls should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingLdapContext.getRequestControls(); + fail("DelegatingLdapContext.getRequestControls should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + try { + delegatingLdapContext.getResponseControls(); + fail("DelegatingLdapContext.getResponseControls should have thrown a NamingException"); + } catch (NamingException ne) { + // Expected + } + verify(); + } + + public void testDoubleClose() throws Exception { + keyedObjectPoolMock.returnObject(DirContextType.READ_ONLY, + ldapContextMock); + keyedObjectPoolControl.setVoidCallable(1); + + replay(); + + final DelegatingLdapContext delegatingLdapContext = new DelegatingLdapContext( + keyedObjectPoolMock, ldapContextMock, DirContextType.READ_ONLY); + + delegatingLdapContext.close(); + + // noop close + delegatingLdapContext.close(); + + verify(); + } +} diff --git a/sandbox/src/test/java/org/springframework/ldap/pool/DirContextPoolableObjectFactoryTest.java b/sandbox/src/test/java/org/springframework/ldap/pool/DirContextPoolableObjectFactoryTest.java new file mode 100644 index 00000000..5b01764e --- /dev/null +++ b/sandbox/src/test/java/org/springframework/ldap/pool/DirContextPoolableObjectFactoryTest.java @@ -0,0 +1,226 @@ +package org.springframework.ldap.pool; + +import javax.naming.directory.DirContext; + +import org.easymock.MockControl; +import org.springframework.ldap.core.ContextSource; +import org.springframework.ldap.validation.DirContextValidator; + +/** + * @author Eric Dalquist eric.dalquist@doit.wisc.edu + */ +public class DirContextPoolableObjectFactoryTest extends AbstractPoolTestCase { + + public void testProperties() throws Exception { + final DirContextPoolableObjectFactory objectFactory = new DirContextPoolableObjectFactory(); + + replay(); + + try { + objectFactory.setContextSource(null); + fail("DirContextPoolableObjectFactory.setContextSource should have thrown an IllegalArgumentException"); + } + catch (IllegalArgumentException iae) { + // Expected + } + + objectFactory.setContextSource(contextSourceMock); + final ContextSource contextSource2 = objectFactory.getContextSource(); + assertEquals(contextSourceMock, contextSource2); + + + try { + objectFactory.setDirContextValidator(null); + fail("DirContextPoolableObjectFactory.setDirContextValidator should have thrown an IllegalArgumentException"); + } + catch (IllegalArgumentException iae) { + // Expected + } + + objectFactory.setDirContextValidator(dirContextValidatorMock); + final DirContextValidator dirContextValidator2 = objectFactory.getDirContextValidator(); + assertEquals(dirContextValidatorMock, dirContextValidator2); + verify(); + } + + public void testMakeObjectAssertions() throws Exception { + final DirContextPoolableObjectFactory objectFactory = new DirContextPoolableObjectFactory(); + + replay(); + + try { + objectFactory.makeObject(DirContextType.READ_ONLY); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + + objectFactory.setContextSource(contextSourceMock); + + try { + objectFactory.makeObject(null); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + + verify(); + } + + public void testMakeObjectReadOnly() throws Exception { + final DirContextPoolableObjectFactory objectFactory = new DirContextPoolableObjectFactory(); + + MockControl readOnlyContextControl = MockControl.createControl(DirContext.class); + DirContext readOnlyContextMock = (DirContext) readOnlyContextControl.getMock(); + + readOnlyContextControl.replay(); + + contextSourceControl.expectAndReturn(contextSourceMock.getReadOnlyContext(), readOnlyContextMock, 1); + objectFactory.setContextSource(contextSourceMock); + + replay(); + + final Object createdDirContext = objectFactory.makeObject(DirContextType.READ_ONLY); + + readOnlyContextControl.verify(); + verify(); + assertEquals(readOnlyContextMock, createdDirContext); + } + + public void testMakeObjectReadWrite() throws Exception { + final DirContextPoolableObjectFactory objectFactory = new DirContextPoolableObjectFactory(); + + MockControl readWriteContextControl = MockControl.createControl(DirContext.class); + DirContext readWriteContextMock = (DirContext) readWriteContextControl.getMock(); + + readWriteContextControl.replay(); + + contextSourceControl.expectAndReturn(contextSourceMock.getReadWriteContext(), readWriteContextMock, 1); + objectFactory.setContextSource(contextSourceMock); + + replay(); + + final Object createdDirContext = objectFactory.makeObject(DirContextType.READ_WRITE); + + readWriteContextControl.verify(); + verify(); + assertEquals(readWriteContextMock, createdDirContext); + } + + public void testValidateObjectAssertions() throws Exception { + final DirContextPoolableObjectFactory objectFactory = new DirContextPoolableObjectFactory(); + + replay(); + + try { + objectFactory.validateObject(DirContextType.READ_ONLY, dirContextMock); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + + objectFactory.setDirContextValidator(dirContextValidatorMock); + + try { + objectFactory.validateObject(null, dirContextMock); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + + try { + objectFactory.validateObject(new Object(), dirContextMock); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + + try { + objectFactory.validateObject(DirContextType.READ_ONLY, null); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + + try { + objectFactory.validateObject(DirContextType.READ_ONLY, new Object()); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + + verify(); + } + + public void testValidateObject() throws Exception { + dirContextValidatorControl.expectAndReturn(dirContextValidatorMock + .validateDirContext(DirContextType.READ_ONLY, dirContextMock), + true); + + replay(); + + final DirContextPoolableObjectFactory objectFactory = new DirContextPoolableObjectFactory(); + objectFactory.setDirContextValidator(dirContextValidatorMock); + + final boolean valid = objectFactory.validateObject(DirContextType.READ_ONLY, dirContextMock); + assertTrue(valid); + + //Check exception in validator + MockControl secondDirContextValidatorControl = MockControl.createControl(DirContextValidator.class); + DirContextValidator secondDirContextValidatorMock = (DirContextValidator) secondDirContextValidatorControl.getMock(); + + secondDirContextValidatorControl.expectAndThrow(secondDirContextValidatorMock.validateDirContext(DirContextType.READ_ONLY, dirContextMock), new RuntimeException("Failed to validate")); + secondDirContextValidatorControl.replay(); + objectFactory.setDirContextValidator(secondDirContextValidatorMock); + + final boolean valid2 = objectFactory.validateObject(DirContextType.READ_ONLY, dirContextMock); + assertFalse(valid2); + + secondDirContextValidatorControl.verify(); + verify(); + } + + public void testDestroyObjectAssertions() throws Exception { + final DirContextPoolableObjectFactory objectFactory = new DirContextPoolableObjectFactory(); + + replay(); + + try { + objectFactory.destroyObject(DirContextType.READ_ONLY, null); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + + try { + objectFactory.validateObject(DirContextType.READ_ONLY, new Object()); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + + verify(); + } + + public void testDestroyObject() throws Exception { + dirContextMock.close(); + dirContextControl.setVoidCallable(1); + + replay(); + + final DirContextPoolableObjectFactory objectFactory = new DirContextPoolableObjectFactory(); + + objectFactory.destroyObject(DirContextType.READ_ONLY, dirContextMock); + + MockControl throwingDirContextControl = MockControl.createControl(DirContext.class); + DirContext throwingDirContextMock = (DirContext) throwingDirContextControl.getMock(); + + throwingDirContextMock.close(); + throwingDirContextControl.setThrowable(new RuntimeException("Failed to close")); + throwingDirContextControl.replay(); + + objectFactory.destroyObject(DirContextType.READ_ONLY, throwingDirContextMock); + throwingDirContextControl.verify(); + verify(); + } +} diff --git a/sandbox/src/test/java/org/springframework/ldap/pool/PoolingContextSourceTest.java b/sandbox/src/test/java/org/springframework/ldap/pool/PoolingContextSourceTest.java new file mode 100644 index 00000000..cd9fd16a --- /dev/null +++ b/sandbox/src/test/java/org/springframework/ldap/pool/PoolingContextSourceTest.java @@ -0,0 +1,266 @@ +package org.springframework.ldap.pool; + +import javax.naming.directory.DirContext; +import javax.naming.ldap.LdapContext; + +import org.easymock.MockControl; +import org.springframework.dao.DataAccessResourceFailureException; +import org.springframework.ldap.core.ContextSource; +import org.springframework.ldap.pool.PoolingContextSource.WhenExhaustedAction; +import org.springframework.ldap.validation.DirContextValidator; + +/** + * @author Eric Dalquist eric.dalquist@doit.wisc.edu + */ +public class PoolingContextSourceTest extends AbstractPoolTestCase { + + public void testProperties() throws Exception { + final PoolingContextSource poolingContextSource = new PoolingContextSource(); + + replay(); + + try { + poolingContextSource.setContextSource(null); + fail("PoolingContextSource.setBaseName should have thrown an IllegalArgumentException"); + } + catch (IllegalArgumentException iae) { + // Expected + } + poolingContextSource.setContextSource(contextSourceMock); + final ContextSource contextSource2 = poolingContextSource.getContextSource(); + assertEquals(contextSourceMock, contextSource2); + + try { + poolingContextSource.setDirContextValidator(null); + fail("PoolingContextSource.setDirContextValidator should have thrown an IllegalArgumentException"); + } + catch (IllegalArgumentException iae) { + // Expected + } + poolingContextSource.setDirContextValidator(dirContextValidatorMock); + final DirContextValidator dirContextValidator2 = poolingContextSource.getDirContextValidator(); + assertEquals(dirContextValidatorMock, dirContextValidator2); + + poolingContextSource.setMaxActive(1000); + final int maxActive = poolingContextSource.getMaxActive(); + assertEquals(1000, maxActive); + + poolingContextSource.setMaxIdle(500); + final int maxIdle = poolingContextSource.getMaxIdle(); + assertEquals(500, maxIdle); + + poolingContextSource.setMaxTotal(5000); + final int maxTotal = poolingContextSource.getMaxTotal(); + assertEquals(5000, maxTotal); + + poolingContextSource.setMaxWait(2000L); + final long maxWait = poolingContextSource.getMaxWait(); + assertEquals(2000L, maxWait); + + poolingContextSource.setMinEvictableIdleTimeMillis(60000L); + final long minEvictableIdleTimeMillis = poolingContextSource.getMinEvictableIdleTimeMillis(); + assertEquals(60000L, minEvictableIdleTimeMillis); + + poolingContextSource.setMinIdle(100); + final int minIdle = poolingContextSource.getMinIdle(); + assertEquals(100, minIdle); + + poolingContextSource.setNumTestsPerEvictionRun(5); + final int numTestsPerEvictionRun = poolingContextSource.getNumTestsPerEvictionRun(); + assertEquals(5, numTestsPerEvictionRun); + + poolingContextSource.setTestOnBorrow(true); + final boolean testOnBorrow = poolingContextSource.getTestOnBorrow(); + assertEquals(true, testOnBorrow); + + poolingContextSource.setTestOnReturn(true); + final boolean testOnReturn = poolingContextSource.getTestOnReturn(); + assertEquals(true, testOnReturn); + + poolingContextSource.setTestWhileIdle(true); + final boolean testWhileIdle = poolingContextSource.getTestWhileIdle(); + assertEquals(true, testWhileIdle); + + poolingContextSource.setTimeBetweenEvictionRunsMillis(120000L); + final long timeBetweenEvictionRunsMillis = poolingContextSource.getTimeBetweenEvictionRunsMillis(); + assertEquals(120000L, timeBetweenEvictionRunsMillis); + + try { + poolingContextSource.setWhenExhaustedAction(null); + fail("PoolingContextSource.setWhenExhaustedAction should have thrown an IllegalArgumentException"); + } + catch (IllegalArgumentException iae) { + // Expected + } + poolingContextSource.setWhenExhaustedAction(PoolingContextSource.WhenExhaustedAction.BLOCK); + final WhenExhaustedAction whenExhaustedAction = poolingContextSource.getWhenExhaustedAction(); + assertEquals(PoolingContextSource.WhenExhaustedAction.BLOCK, whenExhaustedAction); + + assertNull(PoolingContextSource.WhenExhaustedAction.getActionForId(Byte.MAX_VALUE)); + + final int numActive = poolingContextSource.getNumActive(); + assertEquals(0, numActive); + + final int numIdle = poolingContextSource.getNumIdle(); + assertEquals(0, numIdle); + } + + public void testGetReadOnlyContextPool() throws Exception { + MockControl secondDirContextControl = MockControl.createControl(DirContext.class); + DirContext secondDirContextMock = (DirContext) secondDirContextControl.getMock(); + + contextSourceControl.expectAndReturn(contextSourceMock.getReadOnlyContext(), dirContextMock); + contextSourceControl.expectAndReturn(contextSourceMock.getReadOnlyContext(), secondDirContextMock); + + replay(); + + final PoolingContextSource poolingContextSource = new PoolingContextSource(); + poolingContextSource.setContextSource(contextSourceMock); + + //Get a context + final DirContext readOnlyContext1 = poolingContextSource.getReadOnlyContext(); + assertEquals(readOnlyContext1, dirContextMock); //Order reversed because the 'wrapper' has the needed equals logic + assertEquals(1, poolingContextSource.getNumActive()); + assertEquals(0, poolingContextSource.getNumIdle()); + + //Close the context + readOnlyContext1.close(); + assertEquals(0, poolingContextSource.getNumActive()); + assertEquals(1, poolingContextSource.getNumIdle()); + + //Get the context again + final DirContext readOnlyContext2 = poolingContextSource.getReadOnlyContext(); + assertEquals(readOnlyContext2, dirContextMock); //Order reversed because the 'wrapper' has the needed equals logic + assertEquals(1, poolingContextSource.getNumActive()); + assertEquals(0, poolingContextSource.getNumIdle()); + + //Get a new context + final DirContext readOnlyContext3 = poolingContextSource.getReadOnlyContext(); + assertEquals(readOnlyContext3, secondDirContextMock); //Order reversed because the 'wrapper' has the needed equals logic + assertEquals(2, poolingContextSource.getNumActive()); + assertEquals(0, poolingContextSource.getNumIdle()); + + //Close context + readOnlyContext2.close(); + assertEquals(1, poolingContextSource.getNumActive()); + assertEquals(1, poolingContextSource.getNumIdle()); + + //Close context + readOnlyContext3.close(); + assertEquals(0, poolingContextSource.getNumActive()); + assertEquals(2, poolingContextSource.getNumIdle()); + } + + public void testGetReadWriteContextPool() throws Exception { + MockControl secondDirContextControl = MockControl.createControl(DirContext.class); + DirContext secondDirContextMock = (DirContext) secondDirContextControl.getMock(); + + contextSourceControl.expectAndReturn(contextSourceMock.getReadWriteContext(), dirContextMock); + contextSourceControl.expectAndReturn(contextSourceMock.getReadWriteContext(), secondDirContextMock); + + replay(); + + final PoolingContextSource poolingContextSource = new PoolingContextSource(); + poolingContextSource.setContextSource(contextSourceMock); + + //Get a context + final DirContext readOnlyContext1 = poolingContextSource.getReadWriteContext(); + assertEquals(readOnlyContext1, dirContextMock); //Order reversed because the 'wrapper' has the needed equals logic + assertEquals(1, poolingContextSource.getNumActive()); + assertEquals(0, poolingContextSource.getNumIdle()); + + //Close the context + readOnlyContext1.close(); + assertEquals(0, poolingContextSource.getNumActive()); + assertEquals(1, poolingContextSource.getNumIdle()); + + //Get the context again + final DirContext readOnlyContext2 = poolingContextSource.getReadWriteContext(); + assertEquals(readOnlyContext2, dirContextMock); //Order reversed because the 'wrapper' has the needed equals logic + assertEquals(1, poolingContextSource.getNumActive()); + assertEquals(0, poolingContextSource.getNumIdle()); + + //Get a new context + final DirContext readOnlyContext3 = poolingContextSource.getReadWriteContext(); + assertEquals(readOnlyContext3, secondDirContextMock); //Order reversed because the 'wrapper' has the needed equals logic + assertEquals(2, poolingContextSource.getNumActive()); + assertEquals(0, poolingContextSource.getNumIdle()); + + //Close context + readOnlyContext2.close(); + assertEquals(1, poolingContextSource.getNumActive()); + assertEquals(1, poolingContextSource.getNumIdle()); + + //Close context + readOnlyContext3.close(); + assertEquals(0, poolingContextSource.getNumActive()); + assertEquals(2, poolingContextSource.getNumIdle()); + } + + public void testGetContextException() throws Exception { + contextSourceControl.expectAndThrow(contextSourceMock.getReadWriteContext(), new RuntimeException("Problem getting context")); + + replay(); + + final PoolingContextSource poolingContextSource = new PoolingContextSource(); + poolingContextSource.setContextSource(contextSourceMock); + + try { + poolingContextSource.getReadWriteContext(); + fail("PoolingContextSource.getReadWriteContext should have thrown DataAccessResourceFailureException"); + } + catch (DataAccessResourceFailureException darfe) { + // Expected + } + } + + public void testGetReadOnlyLdapContext() throws Exception { + MockControl secondLdapContextControl = MockControl.createControl(LdapContext.class); + LdapContext secondLdapContextMock = (LdapContext) secondLdapContextControl.getMock(); + + secondLdapContextControl.replay(); + + contextSourceControl.expectAndReturn(contextSourceMock.getReadOnlyContext(), ldapContextMock); + contextSourceControl.expectAndReturn(contextSourceMock.getReadOnlyContext(), secondLdapContextMock); + + replay(); + + final PoolingContextSource poolingContextSource = new PoolingContextSource(); + poolingContextSource.setContextSource(contextSourceMock); + + //Get a context + final DirContext readOnlyContext1 = poolingContextSource.getReadOnlyContext(); + assertEquals(readOnlyContext1, ldapContextMock); //Order reversed because the 'wrapper' has the needed equals logic + assertEquals(1, poolingContextSource.getNumActive()); + assertEquals(0, poolingContextSource.getNumIdle()); + + //Close the context + readOnlyContext1.close(); + assertEquals(0, poolingContextSource.getNumActive()); + assertEquals(1, poolingContextSource.getNumIdle()); + + //Get the context again + final DirContext readOnlyContext2 = poolingContextSource.getReadOnlyContext(); + assertEquals(readOnlyContext2, ldapContextMock); //Order reversed because the 'wrapper' has the needed equals logic + assertEquals(1, poolingContextSource.getNumActive()); + assertEquals(0, poolingContextSource.getNumIdle()); + + //Get a new context + final DirContext readOnlyContext3 = poolingContextSource.getReadOnlyContext(); + assertEquals(readOnlyContext3, secondLdapContextMock); //Order reversed because the 'wrapper' has the needed equals logic + assertEquals(2, poolingContextSource.getNumActive()); + assertEquals(0, poolingContextSource.getNumIdle()); + + //Close context + readOnlyContext2.close(); + assertEquals(1, poolingContextSource.getNumActive()); + assertEquals(1, poolingContextSource.getNumIdle()); + + //Close context + readOnlyContext3.close(); + assertEquals(0, poolingContextSource.getNumActive()); + assertEquals(2, poolingContextSource.getNumIdle()); + + secondLdapContextControl.verify(); + } +} diff --git a/sandbox/src/test/java/org/springframework/ldap/validation/DefaultDirContextValidatorTest.java b/sandbox/src/test/java/org/springframework/ldap/validation/DefaultDirContextValidatorTest.java new file mode 100644 index 00000000..80b830f6 --- /dev/null +++ b/sandbox/src/test/java/org/springframework/ldap/validation/DefaultDirContextValidatorTest.java @@ -0,0 +1,171 @@ +package org.springframework.ldap.validation; + +import javax.naming.NamingEnumeration; +import javax.naming.NamingException; +import javax.naming.directory.DirContext; +import javax.naming.directory.SearchControls; + +import junit.framework.TestCase; + +import org.easymock.MockControl; +import org.springframework.ldap.pool.DirContextType; + +/** + * @author Eric Dalquist eric.dalquist@doit.wisc.edu + */ +public class DefaultDirContextValidatorTest extends TestCase { + + private MockControl namingEnumerationControl; + + private NamingEnumeration namingEnumerationMock; + + private MockControl dirContextControl; + + private DirContext dirContextMock; + + protected void setUp() throws Exception { + super.setUp(); + namingEnumerationControl = MockControl + .createControl(NamingEnumeration.class); + namingEnumerationMock = (NamingEnumeration) namingEnumerationControl + .getMock(); + + dirContextControl = MockControl.createControl(DirContext.class); + dirContextMock = (DirContext) dirContextControl.getMock(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + namingEnumerationControl = null; + namingEnumerationMock = null; + + dirContextControl = null; + dirContextMock = null; + } + + protected void replay() { + namingEnumerationControl.replay(); + dirContextControl.replay(); + } + + protected void verify() { + namingEnumerationControl.verify(); + dirContextControl.verify(); + } + + public void testProperties() throws Exception { + final DefaultDirContextValidator dirContextValidator = new DefaultDirContextValidator(); + + dirContextValidator.setBase("baseName"); + final String baseName = dirContextValidator.getBase(); + assertEquals("baseName", baseName); + + try { + dirContextValidator.setFilter(null); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + dirContextValidator.setFilter("filter"); + final String filter = dirContextValidator.getFilter(); + assertEquals("filter", filter); + + try { + dirContextValidator.setSearchControls(null); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + final SearchControls sc = new SearchControls(); + dirContextValidator.setSearchControls(sc); + final SearchControls sc2 = dirContextValidator.getSearchControls(); + assertEquals(sc, sc2); + } + + public void testValidateDirContextAssertions() throws Exception { + final DefaultDirContextValidator dirContextValidator = new DefaultDirContextValidator(); + + try { + dirContextValidator.validateDirContext(DirContextType.READ_ONLY, + null); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + + replay(); + + try { + dirContextValidator.validateDirContext(null, dirContextMock); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + } + + public void testValidateDirContextHasResult() throws Exception { + final DefaultDirContextValidator dirContextValidator = new DefaultDirContextValidator(); + + final String baseName = dirContextValidator.getBase(); + final String filter = dirContextValidator.getFilter(); + final SearchControls searchControls = dirContextValidator + .getSearchControls(); + + namingEnumerationControl.expectAndReturn(namingEnumerationMock + .hasMore(), true); + dirContextControl.expectAndReturn(dirContextMock.search(baseName, + filter, searchControls), namingEnumerationMock); + + replay(); + + final boolean valid = dirContextValidator.validateDirContext( + DirContextType.READ_ONLY, dirContextMock); + + verify(); + assertTrue(valid); + } + + public void testValidateDirContextNoResult() throws Exception { + final DefaultDirContextValidator dirContextValidator = new DefaultDirContextValidator(); + + final String baseName = dirContextValidator.getBase(); + final String filter = dirContextValidator.getFilter(); + final SearchControls searchControls = dirContextValidator + .getSearchControls(); + + namingEnumerationControl.expectAndReturn(namingEnumerationMock + .hasMore(), false); + dirContextControl.expectAndReturn(dirContextMock.search(baseName, + filter, searchControls), namingEnumerationMock); + + replay(); + + final boolean valid = dirContextValidator.validateDirContext( + DirContextType.READ_ONLY, dirContextMock); + + verify(); + assertFalse(valid); + } + + public void testValidateDirContextException() throws Exception { + final DefaultDirContextValidator dirContextValidator = new DefaultDirContextValidator(); + + final String baseName = dirContextValidator.getBase(); + final String filter = dirContextValidator.getFilter(); + final SearchControls searchControls = dirContextValidator + .getSearchControls(); + + dirContextControl.expectAndThrow(dirContextMock.search(baseName, + filter, searchControls), + new NamingException("Failed to search")); + + replay(); + + final boolean valid = dirContextValidator.validateDirContext( + DirContextType.READ_ONLY, dirContextMock); + + verify(); + assertFalse(valid); + } +}