Added javadocs for integration test support.

This commit is contained in:
Mattias Arthursson
2008-06-05 06:58:15 +00:00
parent b712a21f04
commit e946ae0d00
3 changed files with 131 additions and 76 deletions

View File

@@ -1,3 +1,18 @@
/*
* Copyright 2005-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.test;
import java.io.File;
@@ -31,6 +46,12 @@ import org.springframework.core.io.Resource;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.DistinguishedName;
/**
* Utilities for starting, stopping and populating an in-process Apache
* Directory Server to use for integration testing purposes.
*
* @author Mattias Hellborg Arthursson
*/
public class LdapTestUtils {
/**
@@ -39,9 +60,23 @@ public class LdapTestUtils {
private LdapTestUtils() {
}
public static DirContext startApacheDirectoryServer(int port,
String defaultPartitionSuffix, String defaultPartitionName)
throws NamingException {
/**
* Start an in-process Apache Directory Server.
*
* @param port the port on which the server will be listening.
* @param defaultPartitionSuffix The default base suffix that will be used
* for the LDAP server.
* @param defaultPartitionName The name to use in the directory server
* configuration for the default base suffix.
* @param principal The principal to use when starting the directory server.
* @param credentials The credentials to use when starting the directory
* server.
* @return A DirContext to be used for working against the started directory
* server.
* @throws NamingException If anything goes wrong when starting the server.
*/
public static DirContext startApacheDirectoryServer(int port, String defaultPartitionSuffix,
String defaultPartitionName, String principal, String credentials) throws NamingException {
MutableServerStartupConfiguration cfg = new MutableServerStartupConfiguration();
@@ -53,24 +88,27 @@ public class LdapTestUtils {
MutableBTreePartitionConfiguration partitionConfiguration = new MutableBTreePartitionConfiguration();
partitionConfiguration.setSuffix(defaultPartitionSuffix);
partitionConfiguration
.setContextEntry(getRootPartitionAttributes(defaultPartitionName));
partitionConfiguration.setContextEntry(getRootPartitionAttributes(defaultPartitionName));
partitionConfiguration.setName(defaultPartitionName);
cfg.setPartitionConfigurations(Collections
.singleton(partitionConfiguration));
cfg.setPartitionConfigurations(Collections.singleton(partitionConfiguration));
// Start the Server
Hashtable env = createEnv();
Hashtable env = createEnv(principal, credentials);
env.putAll(cfg.toJndiEnvironment());
return new InitialDirContext(env);
}
public static void destroyApacheDirectoryServer(String principal,
String credentials) throws Exception {
/**
* Shut down the in-process Apache Directory Server.
*
* @param principal the principal to be used for authentication.
* @param credentials the credentials to be used for authentication.
* @throws Exception If anything goes wrong when shutting down the server.
*/
public static void destroyApacheDirectoryServer(String principal, String credentials) throws Exception {
Properties env = new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
ServerContextFactory.class.getName());
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, ServerContextFactory.class.getName());
env.setProperty(Context.SECURITY_AUTHENTICATION, "simple");
env.setProperty(Context.SECURITY_PRINCIPAL, principal);
env.setProperty(Context.SECURITY_CREDENTIALS, credentials);
@@ -81,116 +119,111 @@ public class LdapTestUtils {
new InitialContext(env);
}
//
// public void cleanAndSetup(ContextSource contextSource, Resource ldifFile,
// DistinguishedName baseLdapPath)
// throws Exception {
// DirContext ctx = contextSource.getReadWriteContext();
//
// // First of all, make sure the database is empty.
// Name startingPoint = null;
//
// // Different test cases have different base paths. This means that the
// // starting point will be different.
// if (baseLdapPath.size() != 0) {
// startingPoint = DistinguishedName.EMPTY_PATH;
// } else {
// startingPoint = new DistinguishedName(baseLdapPath);
// }
//
// try {
// clearSubContexts(ctx, startingPoint);
// // Load the ldif to the recently started server
// LdifFileLoader loader = new LdifFileLoader(ctx, ldifFile);
// loader.execute();
// } finally {
// ctx.close();
// }
// }
//
public static void clearSubContexts(ContextSource contextSource, Name name)
throws NamingException {
/**
* Clear the directory sub-tree starting with the node represented by the
* supplied distinguished name.
*
* @param contextSource the ContextSource to use for getting a DirContext.
* @param name the distinguished name of the root node.
* @throws NamingException if anything goes wrong removing the sub-tree.
*/
public static void clearSubContexts(ContextSource contextSource, Name name) throws NamingException {
DirContext ctx = null;
try {
ctx = contextSource.getReadWriteContext();
clearSubContexts(ctx, name);
} finally {
}
finally {
ctx.close();
}
}
public static void clearSubContexts(DirContext ctx, Name name)
throws NamingException {
/**
* Clear the directory sub-tree starting with the node represented by the
* supplied distinguished name.
*
* @param contextSource The DirContext to use for cleaning the tree.
* @param name the distinguished name of the root node.
* @throws NamingException if anything goes wrong removing the sub-tree.
*/
public static void clearSubContexts(DirContext ctx, Name name) throws NamingException {
NamingEnumeration enumeration = null;
try {
enumeration = ctx.listBindings(name);
while (enumeration.hasMore()) {
Binding element = (Binding) enumeration.next();
DistinguishedName childName = new DistinguishedName(element
.getName());
DistinguishedName childName = new DistinguishedName(element.getName());
childName.prepend((DistinguishedName) name);
try {
ctx.destroySubcontext(childName);
} catch (ContextNotEmptyException e) {
}
catch (ContextNotEmptyException e) {
clearSubContexts(ctx, childName);
ctx.destroySubcontext(childName);
}
}
} catch (NamingException e) {
}
catch (NamingException e) {
e.printStackTrace();
} finally {
}
finally {
try {
enumeration.close();
} catch (Exception e) {
}
catch (Exception e) {
// Never mind this
}
}
}
public static void loadLdif(ContextSource contextSource, Resource ldifFile)
throws IOException {
/**
* Load an Ldif file into an LDAP server.
*
* @param contextSource ContextSource to use for getting a DirContext to
* interact with the LDAP server.
* @param ldifFile a Resource representing a valid LDIF file.
* @throws IOException if the Resource cannot be read.
*/
public static void loadLdif(ContextSource contextSource, Resource ldifFile) throws IOException {
DirContext context = contextSource.getReadWriteContext();
try {
loadLdif(context, ldifFile);
} finally {
}
finally {
try {
context.close();
} catch (Exception e) {
}
catch (Exception e) {
// This is not the exception we are interested in.
}
}
}
private static void loadLdif(DirContext context, Resource ldifFile)
throws IOException {
private static void loadLdif(DirContext context, Resource ldifFile) throws IOException {
File tempFile = File.createTempFile("spring_ldap_test", ".ldif");
InputStream inputStream = ldifFile.getInputStream();
IOUtils.copy(inputStream, new FileOutputStream(tempFile));
LdifFileLoader fileLoader = new LdifFileLoader(context, tempFile
.getAbsolutePath());
LdifFileLoader fileLoader = new LdifFileLoader(context, tempFile.getAbsolutePath());
fileLoader.execute();
}
private static Hashtable createEnv() {
private static Hashtable createEnv(String principal, String credentials) {
Hashtable env = new Properties();
env.put(Context.PROVIDER_URL, "");
env.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.directory.server.jndi.ServerContextFactory");
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.directory.server.jndi.ServerContextFactory");
env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
env.put(Context.SECURITY_CREDENTIALS, "secret");
env.put(Context.SECURITY_PRINCIPAL, principal);
env.put(Context.SECURITY_CREDENTIALS, credentials);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
return env;
}
private static Attributes getRootPartitionAttributes(
String defaultPartitionName) {
private static Attributes getRootPartitionAttributes(String defaultPartitionName) {
BasicAttributes attributes = new BasicAttributes();
BasicAttribute objectClassAttribute = new BasicAttribute("objectClass");
objectClassAttribute.add("top");

View File

@@ -1,6 +1,20 @@
/*
* Copyright 2005-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.test;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.core.io.Resource;
import org.springframework.ldap.core.AuthenticationSource;
@@ -9,6 +23,9 @@ import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.support.DefaultDirObjectFactory;
import org.springframework.ldap.core.support.LdapContextSource;
/**
* @author Mattias Hellborg Arthursson
*/
public class TestContextSourceFactoryBean extends AbstractFactoryBean {
private int port;
@@ -30,8 +47,7 @@ public class TestContextSourceFactoryBean extends AbstractFactoryBean {
private AuthenticationSource authenticationSource;
public void setAuthenticationSource(
AuthenticationSource authenticationSource) {
public void setAuthenticationSource(AuthenticationSource authenticationSource) {
this.authenticationSource = authenticationSource;
}
@@ -72,8 +88,8 @@ public class TestContextSourceFactoryBean extends AbstractFactoryBean {
}
protected Object createInstance() throws Exception {
LdapTestUtils.startApacheDirectoryServer(port, defaultPartitionSuffix,
defaultPartitionName);
LdapTestUtils.startApacheDirectoryServer(port, defaultPartitionSuffix, defaultPartitionName, principal,
password);
LdapContextSource targetContextSource = new LdapContextSource();
if (baseOnTarget) {
@@ -91,11 +107,10 @@ public class TestContextSourceFactoryBean extends AbstractFactoryBean {
targetContextSource.afterPropertiesSet();
if (baseOnTarget) {
LdapTestUtils.clearSubContexts(targetContextSource,
DistinguishedName.EMPTY_PATH);
} else {
LdapTestUtils.clearSubContexts(targetContextSource,
new DistinguishedName(defaultPartitionSuffix));
LdapTestUtils.clearSubContexts(targetContextSource, DistinguishedName.EMPTY_PATH);
}
else {
LdapTestUtils.clearSubContexts(targetContextSource, new DistinguishedName(defaultPartitionSuffix));
}
if (ldifFile != null) {

View File

@@ -0,0 +1,7 @@
<html>
<body>
Utilities to simplify integration testing against LDAP targets.
</body>
</html>