diff --git a/test-support/build.gradle b/test-support/build.gradle index a8dd21d3..f2240ab6 100644 --- a/test-support/build.gradle +++ b/test-support/build.gradle @@ -1,6 +1,7 @@ dependencies { compile project(":spring-ldap-core"), + project(":spring-ldap-ldif-core"), "com.google.code.typica:typica:1.3", "commons-io:commons-io:1.4", "javax.xml:jsr173:1.0", diff --git a/test-support/src/main/java/org/springframework/ldap/test/DummyDirContext.java b/test-support/src/main/java/org/springframework/ldap/test/DummyDirContext.java new file mode 100644 index 00000000..3e706eb5 --- /dev/null +++ b/test-support/src/main/java/org/springframework/ldap/test/DummyDirContext.java @@ -0,0 +1,310 @@ +/* + * Copyright 2005-2010 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 javax.naming.Binding; +import javax.naming.Context; +import javax.naming.Name; +import javax.naming.NameClassPair; +import javax.naming.NameParser; +import javax.naming.NamingEnumeration; +import javax.naming.NamingException; +import javax.naming.directory.Attributes; +import javax.naming.directory.DirContext; +import javax.naming.directory.ModificationItem; +import javax.naming.directory.SearchControls; +import javax.naming.directory.SearchResult; +import java.util.Hashtable; + +/** + * @author Mattias Hellborg Arthursson + */ +public class DummyDirContext implements DirContext { + @Override + public Attributes getAttributes(Name name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public Attributes getAttributes(String name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public Attributes getAttributes(Name name, String[] attrIds) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public Attributes getAttributes(String name, String[] attrIds) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public void modifyAttributes(Name name, int mod_op, Attributes attrs) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public void modifyAttributes(String name, int mod_op, Attributes attrs) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public void modifyAttributes(Name name, ModificationItem[] mods) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public void modifyAttributes(String name, ModificationItem[] mods) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public void bind(Name name, Object obj, Attributes attrs) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public void bind(String name, Object obj, Attributes attrs) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public void rebind(Name name, Object obj, Attributes attrs) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public void rebind(String name, Object obj, Attributes attrs) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public DirContext createSubcontext(Name name, Attributes attrs) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public DirContext createSubcontext(String name, Attributes attrs) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public DirContext getSchema(Name name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public DirContext getSchema(String name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public DirContext getSchemaClassDefinition(Name name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public DirContext getSchemaClassDefinition(String name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public NamingEnumeration search(Name name, Attributes matchingAttributes, String[] attributesToReturn) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public NamingEnumeration search(String name, Attributes matchingAttributes, String[] attributesToReturn) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public NamingEnumeration search(Name name, Attributes matchingAttributes) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public NamingEnumeration search(String name, Attributes matchingAttributes) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public NamingEnumeration search(Name name, String filter, SearchControls cons) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public NamingEnumeration search(String name, String filter, SearchControls cons) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public NamingEnumeration search(Name name, String filterExpr, Object[] filterArgs, SearchControls cons) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public NamingEnumeration search(String name, String filterExpr, Object[] filterArgs, SearchControls cons) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public Object lookup(Name name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public Object lookup(String name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public void bind(Name name, Object obj) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public void bind(String name, Object obj) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public void rebind(Name name, Object obj) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public void rebind(String name, Object obj) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public void unbind(Name name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public void unbind(String name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public void rename(Name oldName, Name newName) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public void rename(String oldName, String newName) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public NamingEnumeration list(Name name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public NamingEnumeration list(String name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public NamingEnumeration listBindings(Name name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public NamingEnumeration listBindings(String name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public void destroySubcontext(Name name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public void destroySubcontext(String name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public Context createSubcontext(Name name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public Context createSubcontext(String name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public Object lookupLink(Name name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public Object lookupLink(String name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public NameParser getNameParser(Name name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public NameParser getNameParser(String name) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public Name composeName(Name name, Name prefix) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public String composeName(String name, String prefix) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public Object addToEnvironment(String propName, Object propVal) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public Object removeFromEnvironment(String propName) throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public Hashtable getEnvironment() throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public void close() throws NamingException { + throw new UnsupportedOperationException(); + } + + @Override + public String getNameInNamespace() throws NamingException { + throw new UnsupportedOperationException(); + } +} diff --git a/test-support/src/main/java/org/springframework/ldap/test/EmbeddedLdapServer.java b/test-support/src/main/java/org/springframework/ldap/test/EmbeddedLdapServer.java index 86c7489a..2aca7024 100644 --- a/test-support/src/main/java/org/springframework/ldap/test/EmbeddedLdapServer.java +++ b/test-support/src/main/java/org/springframework/ldap/test/EmbeddedLdapServer.java @@ -1,25 +1,83 @@ +/* + * Copyright 2005-2010 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.apache.directory.server.core.DefaultDirectoryService; import org.apache.directory.server.core.DirectoryService; +import org.apache.directory.server.core.entry.ServerEntry; +import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition; import org.apache.directory.server.ldap.LdapServer; +import org.apache.directory.server.protocol.shared.transport.TcpTransport; +import org.apache.directory.shared.ldap.name.LdapDN; + +import java.io.File; /** + * Helper class for embedded Apache Directory Server. + * * @author Mattias Hellborg Arthursson + * @since 1.3.2 */ public class EmbeddedLdapServer { - private final String principal; - private final String password; private final DirectoryService directoryService; private final LdapServer ldapServer; - private EmbeddedLdapServer(String principal, - String password, - DirectoryService directoryService, + private EmbeddedLdapServer(DirectoryService directoryService, LdapServer ldapServer) { - this.principal = principal; - this.password = password; this.directoryService = directoryService; this.ldapServer = ldapServer; } + public static EmbeddedLdapServer newEmbeddedServer(String defaultPartitionName, String defaultPartitionSuffix, int port) + throws Exception{ + + DefaultDirectoryService directoryService = new DefaultDirectoryService(); + directoryService.setShutdownHookEnabled(true); + directoryService.setAllowAnonymousAccess(true); + directoryService.setWorkingDirectory(new File(System.getProperty("java.io.tmpdir") + "/apacheds-test")); + directoryService.getChangeLog().setEnabled( false ); + + JdbmPartition partition = new JdbmPartition(); + partition.setId(defaultPartitionName); + partition.setSuffix(defaultPartitionSuffix); + directoryService.addPartition(partition); + + directoryService.startup(); + + // Inject the apache root entry if it does not already exist + if ( !directoryService.getAdminSession().exists( partition.getSuffixDn() ) ) + { + ServerEntry entry = directoryService.newEntry(new LdapDN(defaultPartitionSuffix)); + entry.add("objectClass", "top", "domain", "extensibleObject"); + entry.add("dc", defaultPartitionName); + directoryService.getAdminSession().add( entry ); + } + + LdapServer ldapServer = new LdapServer(); + ldapServer.setDirectoryService(directoryService); + + TcpTransport ldapTransport = new TcpTransport(port); + ldapServer.setTransports( ldapTransport ); + ldapServer.start(); + + return new EmbeddedLdapServer(directoryService, ldapServer); + } + + public void shutdown() throws Exception { + ldapServer.stop(); + directoryService.shutdown(); + } } diff --git a/test-support/src/main/java/org/springframework/ldap/test/LdapTestUtils.java b/test-support/src/main/java/org/springframework/ldap/test/LdapTestUtils.java index 0ddffc4e..495ba37a 100644 --- a/test-support/src/main/java/org/springframework/ldap/test/LdapTestUtils.java +++ b/test-support/src/main/java/org/springframework/ldap/test/LdapTestUtils.java @@ -21,6 +21,9 @@ import org.apache.directory.server.protocol.shared.store.LdifFileLoader; import org.springframework.core.io.Resource; import org.springframework.ldap.core.ContextSource; import org.springframework.ldap.core.DistinguishedName; +import org.springframework.ldap.core.LdapAttributes; +import org.springframework.ldap.core.support.DefaultDirObjectFactory; +import org.springframework.ldap.ldif.parser.LdifParser; import javax.naming.Binding; import javax.naming.Context; @@ -43,203 +46,221 @@ import java.util.Set; /** * 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 { - public static final String DEFAULT_PRINCIPAL = "uid=admin,ou=system"; + public static final String DEFAULT_PRINCIPAL = "uid=admin,ou=system"; + public static final String DEFAULT_PASSWORD = "secret"; - public static final String DEFAULT_PASSWORD = "secret"; - static final String DIRECTORY_SERVICE_KEY = "org.springldap.test.directory.service"; + private static EmbeddedLdapServer embeddedServer; /** - * Not to be instantiated. - */ - private LdapTestUtils() { - } - - /** - * 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. - * @param extraSchemas Set of extra schemas to add to the bootstrap schemas - * of ApacheDS. May be null. - * @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, Set extraSchemas) throws NamingException { + * Not to be instantiated. + */ + private LdapTestUtils() { + } -// -// MutableServerStartupConfiguration cfg = new MutableServerStartupConfiguration(); -// -// // Determine an appropriate working directory -// String tempDir = System.getProperty("java.io.tmpdir"); -// cfg.setWorkingDirectory(new File(tempDir)); -// -// cfg.setLdapPort(port); -// -// if (extraSchemas != null) { -// Set schemas = cfg.getBootstrapSchemas(); -// schemas.addAll(extraSchemas); -// cfg.setBootstrapSchemas(schemas); -// } -// -// MutableBTreePartitionConfiguration partitionConfiguration = new MutableBTreePartitionConfiguration(); -// partitionConfiguration.setSuffix(defaultPartitionSuffix); -// partitionConfiguration.setContextEntry(getRootPartitionAttributes(defaultPartitionName)); -// partitionConfiguration.setName(defaultPartitionName); -// -// cfg.setContextPartitionConfigurations(Collections.singleton(partitionConfiguration)); -// // Start the Server -// -// Hashtable env = createEnv(principal, credentials); -// env.putAll(cfg.toJndiEnvironment()); - return null; - } + /** + * 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. + * @param extraSchemas Set of extra schemas to add to the bootstrap schemas + * of ApacheDS. May be null. + * @return An unusable DirContext instance. + * @throws NamingException If anything goes wrong when starting the server. + * @deprecated use {@link #startEmbeddedServer(int, String, String)} instead. + */ + public static DirContext startApacheDirectoryServer(int port, String defaultPartitionSuffix, + String defaultPartitionName, String principal, String credentials, Set extraSchemas) throws NamingException { - public static DirContext startApacheDirectoryServer(int port, String defaultPartitionSuffix, - String defaultPartitionName, String principal, String credentials) throws NamingException { - return LdapTestUtils.startApacheDirectoryServer(port, defaultPartitionSuffix, defaultPartitionName, principal, - credentials, null); - } + startEmbeddedServer(port, defaultPartitionSuffix, defaultPartitionName); + return new DummyDirContext(); + } - /** - * 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.SECURITY_AUTHENTICATION, "simple"); -// env.setProperty(Context.SECURITY_PRINCIPAL, principal); -// env.setProperty(Context.SECURITY_CREDENTIALS, credentials); -// -// ShutdownConfiguration configuration = new ShutdownConfiguration(); -// env.putAll(configuration.toJndiEnvironment()); -// -// new InitialContext(env); - } - - /** - * 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 { - try { - ctx.close(); - } - catch (Exception e) { - // Never mind this - } - } - } - - /** - * Clear the directory sub-tree starting with the node represented by the - * supplied distinguished name. - * - * @param ctx 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()); - childName.prepend((DistinguishedName) name); - - try { - ctx.destroySubcontext(childName); - } - catch (ContextNotEmptyException e) { - clearSubContexts(ctx, childName); - ctx.destroySubcontext(childName); - } - } - } - catch (NamingException e) { - e.printStackTrace(); - } - finally { - try { - enumeration.close(); - } - catch (Exception e) { - // Never mind this - } - } - } - - /** - * 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 { - try { - context.close(); - } - catch (Exception e) { - // This is not the exception we are interested in. - } - } - } - - public static void cleanAndSetup(ContextSource contextSource, DistinguishedName rootNode, Resource ldifFile) - throws NamingException, IOException { - - clearSubContexts(contextSource, rootNode); - loadLdif(contextSource, ldifFile); - } - - private static void loadLdif(DirContext context, Resource ldifFile) throws IOException { - try { - DefaultDirectoryService directoryService = - (DefaultDirectoryService) context.getEnvironment().get(DIRECTORY_SERVICE_KEY); - if(directoryService == null) { - throw new IllegalStateException("The specified context does not appear to have been created by LdapTestUtils"); - } - loadLdif(directoryService, ldifFile); - } catch (NamingException e) { - throw new RuntimeException("Failed to get environment", e); + /** + * Start an embedded 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. + * + * @since 1.3.2 + */ + public static void startEmbeddedServer(int port, String defaultPartitionSuffix, String defaultPartitionName) { + if(embeddedServer != null) { + throw new IllegalStateException("An embedded server is already started"); } - } + + try { + embeddedServer = EmbeddedLdapServer.newEmbeddedServer(defaultPartitionName, defaultPartitionSuffix, port); + } catch (Exception e) { + throw new RuntimeException("Failed to start embedded server"); + } + } + + public static DirContext startApacheDirectoryServer(int port, String defaultPartitionSuffix, + String defaultPartitionName, String principal, String credentials) throws NamingException { + return LdapTestUtils.startApacheDirectoryServer(port, defaultPartitionSuffix, defaultPartitionName, principal, + credentials, null); + } + + /** + * Shuts down the embedded server, if there is one. If no server was previously started in this JVM + * this is silently ignored. + * + * @throws Exception + * @since 1.3.2 + */ + public static void shutdownEmbeddedServer() throws Exception { + if(embeddedServer != null) { + embeddedServer.shutdown(); + embeddedServer = null; + } + } + + /** + * 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. + * @deprecated use {@link #shutdownEmbeddedServer()} instead. + */ + public static void destroyApacheDirectoryServer(String principal, String credentials) throws Exception { + shutdownEmbeddedServer(); + } + + /** + * 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 { + try { + ctx.close(); + } catch (Exception e) { + // Never mind this + } + } + } + + /** + * Clear the directory sub-tree starting with the node represented by the + * supplied distinguished name. + * + * @param ctx 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()); + childName.prepend((DistinguishedName) name); + + try { + ctx.destroySubcontext(childName); + } catch (ContextNotEmptyException e) { + clearSubContexts(ctx, childName); + ctx.destroySubcontext(childName); + } + } + } catch (NamingException e) { + e.printStackTrace(); + } finally { + try { + enumeration.close(); + } catch (Exception e) { + // Never mind this + } + } + } + + /** + * 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 { + try { + context.close(); + } catch (Exception e) { + // This is not the exception we are interested in. + } + } + } + + public static void cleanAndSetup(ContextSource contextSource, DistinguishedName rootNode, Resource ldifFile) + throws NamingException, IOException { + + clearSubContexts(contextSource, rootNode); + loadLdif(contextSource, ldifFile); + } + + private static void loadLdif(DirContext context, Resource ldifFile) throws IOException { + try { + DistinguishedName baseDn = (DistinguishedName) + context.getEnvironment().get(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY); + + LdifParser parser = new LdifParser(ldifFile); + parser.open(); + while (parser.hasMoreRecords()) { + LdapAttributes record = parser.getRecord(); + + DistinguishedName dn = record.getDN(); + if(baseDn != null) { + dn.removeFirst(baseDn); + } + context.bind(dn, null, record); + } + } catch (NamingException e) { + throw new RuntimeException("Failed to populate LDIF", e); + } + + +// +// try { +// DefaultDirectoryService directoryService = +// (DefaultDirectoryService) context.getEnvironment().get(DIRECTORY_SERVICE_KEY); +// if(directoryService == null) { +// throw new IllegalStateException("The specified context does not appear to have been created by LdapTestUtils"); +// } +// loadLdif(directoryService, ldifFile); +// } catch (NamingException e) { +// throw new RuntimeException("Failed to get environment", e); +// } + } public static void loadLdif(DefaultDirectoryService directoryService, Resource ldifFile) throws IOException { File tempFile = File.createTempFile("spring_ldap_test", ".ldif"); @@ -248,41 +269,39 @@ public class LdapTestUtils { IOUtils.copy(inputStream, new FileOutputStream(tempFile)); LdifFileLoader fileLoader = new LdifFileLoader(directoryService.getSession(), tempFile.getAbsolutePath()); fileLoader.execute(); - } - finally { + } finally { try { tempFile.delete(); - } - catch (Exception e) { + } catch (Exception e) { // Ignore this } } } - private static Hashtable createEnv(String principal, String credentials) { - Hashtable env = new Properties(); + 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.PROVIDER_URL, ""); + env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.directory.server.jndi.ServerContextFactory"); - env.put(Context.SECURITY_PRINCIPAL, principal); - env.put(Context.SECURITY_CREDENTIALS, credentials); - env.put(Context.SECURITY_AUTHENTICATION, "simple"); + env.put(Context.SECURITY_PRINCIPAL, principal); + env.put(Context.SECURITY_CREDENTIALS, credentials); + env.put(Context.SECURITY_AUTHENTICATION, "simple"); - return env; - } + return env; + } - private static Attributes getRootPartitionAttributes(String defaultPartitionName) { - BasicAttributes attributes = new BasicAttributes(); - BasicAttribute objectClassAttribute = new BasicAttribute("objectClass"); - objectClassAttribute.add("top"); - objectClassAttribute.add("domain"); - objectClassAttribute.add("extensibleObject"); - attributes.put(objectClassAttribute); - attributes.put("dc", defaultPartitionName); + private static Attributes getRootPartitionAttributes(String defaultPartitionName) { + BasicAttributes attributes = new BasicAttributes(); + BasicAttribute objectClassAttribute = new BasicAttribute("objectClass"); + objectClassAttribute.add("top"); + objectClassAttribute.add("domain"); + objectClassAttribute.add("extensibleObject"); + attributes.put(objectClassAttribute); + attributes.put("dc", defaultPartitionName); - return attributes; - } + return attributes; + } } diff --git a/test-support/src/main/java/org/springframework/ldap/test/TestContextSourceFactoryBean.java b/test-support/src/main/java/org/springframework/ldap/test/TestContextSourceFactoryBean.java index 6be86089..53eeacde 100644 --- a/test-support/src/main/java/org/springframework/ldap/test/TestContextSourceFactoryBean.java +++ b/test-support/src/main/java/org/springframework/ldap/test/TestContextSourceFactoryBean.java @@ -15,12 +15,6 @@ */ package org.springframework.ldap.test; -import org.apache.directory.server.core.DefaultDirectoryService; -import org.apache.directory.server.core.entry.ServerEntry; -import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition; -import org.apache.directory.server.ldap.LdapServer; -import org.apache.directory.server.protocol.shared.transport.TcpTransport; -import org.apache.directory.shared.ldap.name.LdapDN; import org.springframework.beans.factory.config.AbstractFactoryBean; import org.springframework.core.io.Resource; import org.springframework.ldap.core.AuthenticationSource; @@ -29,9 +23,6 @@ import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.support.DefaultDirObjectFactory; import org.springframework.ldap.core.support.LdapContextSource; -import java.io.File; -import java.util.HashMap; - /** * @author Mattias Hellborg Arthursson */ @@ -55,8 +46,6 @@ public class TestContextSourceFactoryBean extends AbstractFactoryBean { private boolean pooled = true; private AuthenticationSource authenticationSource; - private DefaultDirectoryService directoryService; - private LdapServer ldapServer; public void setAuthenticationSource(AuthenticationSource authenticationSource) { this.authenticationSource = authenticationSource; @@ -99,34 +88,7 @@ public class TestContextSourceFactoryBean extends AbstractFactoryBean { } protected Object createInstance() throws Exception { - directoryService = new DefaultDirectoryService(); - directoryService.setShutdownHookEnabled(true); - directoryService.setAllowAnonymousAccess(true); - directoryService.setWorkingDirectory(new File(System.getProperty("java.io.tmpdir") + "/apacheds-test")); - directoryService.getChangeLog().setEnabled( false ); - - JdbmPartition partition = new JdbmPartition(); - partition.setId(defaultPartitionName); - partition.setSuffix(defaultPartitionSuffix); - directoryService.addPartition(partition); - - directoryService.startup(); - - // Inject the apache root entry if it does not already exist - if ( !directoryService.getAdminSession().exists( partition.getSuffixDn() ) ) - { - ServerEntry entry = directoryService.newEntry(new LdapDN(defaultPartitionSuffix)); - entry.add("objectClass", "top", "domain", "extensibleObject"); - entry.add("dc", defaultPartitionName); - directoryService.getAdminSession().add( entry ); - } - - ldapServer = new LdapServer(); - ldapServer.setDirectoryService(directoryService); - - TcpTransport ldapTransport = new TcpTransport(port); - ldapServer.setTransports( ldapTransport ); - ldapServer.start(); + LdapTestUtils.startEmbeddedServer(port, defaultPartitionSuffix, defaultPartitionName); LdapContextSource targetContextSource = new LdapContextSource(); if (baseOnTarget) { @@ -138,9 +100,6 @@ public class TestContextSourceFactoryBean extends AbstractFactoryBean { targetContextSource.setPassword(password); targetContextSource.setDirObjectFactory(dirObjectFactory); targetContextSource.setPooled(pooled); - targetContextSource.setBaseEnvironmentProperties(new HashMap(){{ - put(LdapTestUtils.DIRECTORY_SERVICE_KEY, directoryService); - }}); if (authenticationSource != null) { targetContextSource.setAuthenticationSource(authenticationSource); @@ -155,7 +114,7 @@ public class TestContextSourceFactoryBean extends AbstractFactoryBean { } if (ldifFile != null) { - LdapTestUtils.loadLdif(directoryService, ldifFile); + LdapTestUtils.loadLdif(targetContextSource, ldifFile); } return targetContextSource; @@ -167,8 +126,6 @@ public class TestContextSourceFactoryBean extends AbstractFactoryBean { protected void destroyInstance(Object instance) throws Exception { super.destroyInstance(instance); - - ldapServer.stop(); - directoryService.shutdown(); + LdapTestUtils.shutdownEmbeddedServer(); } }