Browse Source

I don't have any test built using these yet, but both should server as decent bases from which to build tests for LDAP functionality.

Robert Sanders 20 years ago
parent
commit
3e63685776

+ 37 - 0
sandbox/src/test/java/org/acegisecurity/providers/dao/ldap/BaseLdapTestCase.java

@@ -0,0 +1,37 @@
+package net.sf.acegisecurity.providers.dao.ldap;
+
+import java.util.Hashtable;
+
+import javax.naming.Context;
+import javax.naming.NamingException;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.InitialDirContext;
+
+import org.apache.ldap.server.jndi.EnvKeys;
+
+import junit.framework.TestCase;
+
+/** Important note: this class merely defines certain 
+ *  base properties needed by all LDAP unit tests.
+ */
+public class BaseLdapTestCase extends TestCase {
+
+	protected static LdapTestHelper ldapTestHelper = new LdapTestHelper();
+	
+	/** Create and return a Hashtable with standard JNDI settings for our tests. */
+	protected Hashtable getEnvironment() {
+		Hashtable env = new Hashtable();
+		env.put( Context.PROVIDER_URL, "ou=system" );
+		env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.ServerContextFactory" );
+		env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
+		env.put( Context.SECURITY_CREDENTIALS, "secret" );
+		env.put( EnvKeys.WKDIR, ldapTestHelper.getTempDirectoryPath() );
+		return env;
+	}
+	
+	/** Create and return a Hashtable with standard JNDI settings for our tests. 
+	 * @throws NamingException */
+	protected DirContext getInitialDirContext() throws NamingException {
+		 return new InitialDirContext( getEnvironment() );
+	}
+}

+ 49 - 0
sandbox/src/test/java/org/acegisecurity/providers/dao/ldap/LdapTestHelper.java

@@ -0,0 +1,49 @@
+package net.sf.acegisecurity.providers.dao.ldap;
+
+import java.io.File;
+
+/** 
+ * LdapTestHelper - used as static field in BaseLdapTestCase;
+ *  responsible for global state during JUnit tests - since 
+ *  JUnit reinstantiates the test class for every method.
+ *
+ */
+public class LdapTestHelper {
+	
+	private File tempDirectory;
+	
+	/**
+	 * 
+	 */
+	public LdapTestHelper() {
+		String tmpDir = System.getProperty("java.io.tmpdir");
+		File dir = new File(tmpDir);
+		tempDirectory = new File(dir, "apacheds_tmp");
+		if (!tempDirectory.exists()) {
+			tempDirectory.mkdir();
+			//tempDirectory.deleteOnExit();
+		}
+	}
+	
+	/** since file..deleteOnExit() isn't working for me, explicitly force cleanup. */
+	protected void finalize() throws Throwable {
+		File[] files = tempDirectory.listFiles();
+		for (int i = 0; i < files.length; i++) {
+			files[i].delete();
+		}
+		tempDirectory.delete();
+		super.finalize();
+	}
+	
+
+	public File getTempDirectory() {
+		return tempDirectory;
+	}
+	
+	public String getTempDirectoryPath() {
+		return tempDirectory.getAbsolutePath();
+	}
+	
+	
+
+}