You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@directory.apache.org by Jonathan Carlson <jo...@code42.com> on 2011/09/19 21:14:33 UTC

My basic ldap client test class... if anyone wants

Thank you Stefan for your help.  In case this could be of any use to someone else, here is my working test class that creates a more complex directory structure and does a search on it.  Anyone needing to test an LDAP client API (not included) could use something like this.  ApacheDS is awesome in this regard because I don't want my test cases to rely on an external directory.

Thank you!!  Thank you!!  Thank you!!


import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

import junit.framework.Assert;

import org.apache.directory.server.annotations.CreateLdapServer;
import org.apache.directory.server.annotations.CreateTransport;
import org.apache.directory.server.core.annotations.ContextEntry;
import org.apache.directory.server.core.annotations.CreateDS;
import org.apache.directory.server.core.annotations.CreateIndex;
import org.apache.directory.server.core.annotations.CreatePartition;
import org.apache.directory.server.core.integ.AbstractLdapTestUnit;
import org.apache.directory.server.core.integ.FrameworkRunner;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Each test starts with a directory that looks like this:
 *
 * <tt>dc=example,dc=com</tt><br>
 * <tt>  ou=staff</tt><br>
 * <tt>    cn=jschmoe</tt><br>
 * <tt>    cn=jdoe</tt><br>
 * <tt>  ou=students</tt><br>
 * <tt>    cn=jblack</tt><br>
 */
@RunWith(FrameworkRunner.class)
@CreateDS(
name = "LdapSearchTestDS",
partitions = {
@CreatePartition(
name = "example",
suffix = "dc=example,dc=com",
contextEntry = @ContextEntry(
entryLdif =
"dn: dc=example,dc=com\n" +
"dc: example\n" +
"objectClass: top\n" +
"objectClass: domain\n\n" +
"dn: ou=staff,dc=example,dc=com\n" +
"ou: staff\n" +
"objectClass: organizationalUnit\n\n" +
"dn: cn=jschmoe,ou=staff,dc=example,dc=com\n" +
"objectClass: person\n" +
"cn: jschmoe\n" +
"sn: Schmoe\n\n" +
"dn: cn=jdoe,ou=staff,dc=example,dc=com\n" +
"objectClass: person\n" +
"cn: jdoe\n" +
"sn: Doe\n\n" +
"dn: ou=students,dc=example,dc=com\n" +
"ou: students\n" +
"objectClass: organizationalUnit\n\n" +
"dn: cn=jblack,ou=students,dc=example,dc=com\n" +
"objectClass: person\n" +
"cn: jblack\n" +
"sn: Black\n\n"),
indexes = {
@CreateIndex(attribute = "objectClass"),
@CreateIndex(attribute = "dc"),
@CreateIndex(attribute = "ou"),
@CreateIndex(attribute = "cn")
})
})
@CreateLdapServer(
transports = {
@CreateTransport(protocol = "LDAP"),
@CreateTransport(protocol = "LDAPS")
})
public class LdapSearchTest extends AbstractLdapTestUnit {

@Test
public void testSearch() throws NamingException {
Hashtable<String, String> env =
new Hashtable<String, String>();
env.put(Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort());
env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
env.put(Context.SECURITY_CREDENTIALS, "secret");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
InitialContext initialContext = new InitialContext(env);

// We should be able to read it
DirContext appRoot = (DirContext) initialContext.lookup("");
Assert.assertNotNull(appRoot);

SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
controls.setTimeLimit(30000);

NamingEnumeration<SearchResult> results =
appRoot.search("ou=staff,dc=example,dc=com", "(cn=jdoe)", controls);

while (results.hasMore()) {
SearchResult result = results.next();
Assert.assertNotNull(result);
}
}

}


jon carlson   |  codefortytwo software
1 Main St SE, #400     |   Minneapolis, MN 55414
Office: 612.333.4242  |   web: www.code42.com<http://www.code42.com/>