Add JavaFormat format Gradle Task
Closes gh-744
This commit is contained in:
@@ -27,20 +27,24 @@ import org.springframework.util.Assert;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* Abstract FactoryBean superclass to use for automatically launching an EC2 instance before creating the actual target object.
|
||||
* This approach is particularly useful for integration testing purposes - the idea is to have particular EC2 images prepared
|
||||
* for running integration tests against certain server configurations, enabling integration tests aimed at e.g. a particluar
|
||||
* DB server to run transparently at the computer of each individual developer without having to have the actual server software
|
||||
* installed on their computers.
|
||||
* Abstract FactoryBean superclass to use for automatically launching an EC2 instance
|
||||
* before creating the actual target object. This approach is particularly useful for
|
||||
* integration testing purposes - the idea is to have particular EC2 images prepared for
|
||||
* running integration tests against certain server configurations, enabling integration
|
||||
* tests aimed at e.g. a particluar DB server to run transparently at the computer of each
|
||||
* individual developer without having to have the actual server software installed on
|
||||
* their computers.
|
||||
* <p>
|
||||
* Public AMIs will need to be created, bundled and registered for each server setup. A subclass of this FactoryBean
|
||||
* is then added to create the actual target object (e.g. a DataSource), implementing the {link #doCreateInstance} method.
|
||||
* This method will be supplied the IP address of the instance that was created, enabling the subclass to configure the
|
||||
* created instance appropriately.
|
||||
*
|
||||
* Public AMIs will need to be created, bundled and registered for each server setup. A
|
||||
* subclass of this FactoryBean is then added to create the actual target object (e.g. a
|
||||
* DataSource), implementing the {link #doCreateInstance} method. This method will be
|
||||
* supplied the IP address of the instance that was created, enabling the subclass to
|
||||
* configure the created instance appropriately.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public abstract class AbstractEc2InstanceLaunchingFactoryBean extends AbstractFactoryBean {
|
||||
|
||||
private static final int INSTANCE_START_SLEEP_TIME = 1000;
|
||||
|
||||
private static final long DEFAULT_PREPARATION_SLEEP_TIME = 30000;
|
||||
@@ -63,7 +67,6 @@ public abstract class AbstractEc2InstanceLaunchingFactoryBean extends AbstractFa
|
||||
|
||||
/**
|
||||
* Set the name of the AMI image to be launched.
|
||||
*
|
||||
* @param imageName the AMI image name.
|
||||
*/
|
||||
public void setImageName(String imageName) {
|
||||
@@ -72,7 +75,6 @@ public abstract class AbstractEc2InstanceLaunchingFactoryBean extends AbstractFa
|
||||
|
||||
/**
|
||||
* Set the AWS key.
|
||||
*
|
||||
* @param awsKey the AWS key.
|
||||
*/
|
||||
public void setAwsKey(String awsKey) {
|
||||
@@ -81,7 +83,6 @@ public abstract class AbstractEc2InstanceLaunchingFactoryBean extends AbstractFa
|
||||
|
||||
/**
|
||||
* Set the AWS secret key.
|
||||
*
|
||||
* @param awsSecretKey the aws secret key.
|
||||
*/
|
||||
public void setAwsSecretKey(String awsSecretKey) {
|
||||
@@ -90,7 +91,6 @@ public abstract class AbstractEc2InstanceLaunchingFactoryBean extends AbstractFa
|
||||
|
||||
/**
|
||||
* Set the name of the keypair.
|
||||
*
|
||||
* @param keypairName The keypair name.
|
||||
*/
|
||||
public void setKeypairName(String keypairName) {
|
||||
@@ -98,8 +98,8 @@ public abstract class AbstractEc2InstanceLaunchingFactoryBean extends AbstractFa
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the name of the access group. This group should be configured with the appropriate ports open for this test case to execute.
|
||||
*
|
||||
* Set the name of the access group. This group should be configured with the
|
||||
* appropriate ports open for this test case to execute.
|
||||
* @param groupName the group name.
|
||||
*/
|
||||
public void setGroupName(String groupName) {
|
||||
@@ -138,7 +138,8 @@ public abstract class AbstractEc2InstanceLaunchingFactoryBean extends AbstractFa
|
||||
LOG.info("Instance prepared - proceeding");
|
||||
}
|
||||
return doCreateInstance(instance.getDnsName());
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException("Failed to start a new instance");
|
||||
}
|
||||
|
||||
@@ -146,7 +147,6 @@ public abstract class AbstractEc2InstanceLaunchingFactoryBean extends AbstractFa
|
||||
|
||||
/**
|
||||
* Implement to create the actual target object.
|
||||
*
|
||||
* @param ip the ip address of the launched EC2 image.
|
||||
* @return the object to be returned by this FactoryBean.
|
||||
* @throws Exception if an error occurs during initialization.
|
||||
@@ -162,4 +162,5 @@ public abstract class AbstractEc2InstanceLaunchingFactoryBean extends AbstractFa
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,26 +24,29 @@ import javax.naming.directory.Attributes;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Dummy AttributesMapper for testing purposes to check that the received
|
||||
* Attributes are the expected ones.
|
||||
*
|
||||
* Dummy AttributesMapper for testing purposes to check that the received Attributes are
|
||||
* the expected ones.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public class AttributeCheckAttributesMapper implements AttributesMapper<Object> {
|
||||
|
||||
private String[] expectedAttributes = new String[0];
|
||||
|
||||
private String[] expectedValues = new String[0];;
|
||||
private String[] expectedValues = new String[0];
|
||||
|
||||
private String[] absentAttributes = new String[0];;
|
||||
;
|
||||
|
||||
public Object mapFromAttributes(Attributes attributes)
|
||||
throws NamingException {
|
||||
Assert.assertEquals("Values and attributes need to have the same length ",
|
||||
expectedAttributes.length, expectedValues.length);
|
||||
private String[] absentAttributes = new String[0];
|
||||
|
||||
;
|
||||
|
||||
public Object mapFromAttributes(Attributes attributes) throws NamingException {
|
||||
Assert.assertEquals("Values and attributes need to have the same length ", expectedAttributes.length,
|
||||
expectedValues.length);
|
||||
for (int i = 0; i < expectedAttributes.length; i++) {
|
||||
Attribute attribute = attributes.get(expectedAttributes[i]);
|
||||
Assert.assertNotNull("Attribute " + expectedAttributes[i]
|
||||
+ " was not present", attribute);
|
||||
Assert.assertNotNull("Attribute " + expectedAttributes[i] + " was not present", attribute);
|
||||
Assert.assertEquals(expectedValues[i], attribute.get());
|
||||
}
|
||||
|
||||
@@ -65,4 +68,5 @@ public class AttributeCheckAttributesMapper implements AttributesMapper<Object>
|
||||
public void setExpectedValues(String[] expectedValues) {
|
||||
this.expectedValues = Arrays.copyOf(expectedValues, expectedValues.length);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,12 +23,13 @@ import org.springframework.ldap.core.DirContextAdapter;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Dummy ContextMapper for testing purposes to check that the received
|
||||
* Attributes are the expected ones.
|
||||
*
|
||||
* Dummy ContextMapper for testing purposes to check that the received Attributes are the
|
||||
* expected ones.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public class AttributeCheckContextMapper implements ContextMapper<DirContextAdapter> {
|
||||
|
||||
private String[] expectedAttributes = new String[0];
|
||||
|
||||
private String[] expectedValues = new String[0];
|
||||
@@ -37,13 +38,11 @@ public class AttributeCheckContextMapper implements ContextMapper<DirContextAdap
|
||||
|
||||
public DirContextAdapter mapFromContext(Object ctx) {
|
||||
DirContextAdapter adapter = (DirContextAdapter) ctx;
|
||||
Assert.assertEquals("Values and attributes need to have the same length ",
|
||||
expectedAttributes.length, expectedValues.length);
|
||||
Assert.assertEquals("Values and attributes need to have the same length ", expectedAttributes.length,
|
||||
expectedValues.length);
|
||||
for (int i = 0; i < expectedAttributes.length; i++) {
|
||||
String attributeValue = adapter
|
||||
.getStringAttribute(expectedAttributes[i]);
|
||||
Assert.assertNotNull("Attribute " + expectedAttributes[i]
|
||||
+ " was not present", attributeValue);
|
||||
String attributeValue = adapter.getStringAttribute(expectedAttributes[i]);
|
||||
Assert.assertNotNull("Attribute " + expectedAttributes[i] + " was not present", attributeValue);
|
||||
Assert.assertEquals(expectedValues[i], attributeValue);
|
||||
}
|
||||
|
||||
@@ -65,4 +64,5 @@ public class AttributeCheckContextMapper implements ContextMapper<DirContextAdap
|
||||
public void setExpectedValues(String[] expectedValues) {
|
||||
this.expectedValues = Arrays.copyOf(expectedValues, expectedValues.length);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,8 +21,7 @@ import org.springframework.ldap.test.AbstractEc2InstanceLaunchingFactoryBean;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* FactoryBean to create a ContextSource using the EC2 instance created by
|
||||
* superclass.
|
||||
* FactoryBean to create a ContextSource using the EC2 instance created by superclass.
|
||||
*/
|
||||
public class ContextSourceEc2InstanceLaunchingFactoryBean extends AbstractEc2InstanceLaunchingFactoryBean {
|
||||
|
||||
@@ -60,7 +59,6 @@ public class ContextSourceEc2InstanceLaunchingFactoryBean extends AbstractEc2Ins
|
||||
|
||||
/**
|
||||
* Override to set additional properties on the ContextSource.
|
||||
*
|
||||
* @param ctx The created instance.
|
||||
* @param dnsName The dns name of the created Ec2 instance.
|
||||
*/
|
||||
|
||||
@@ -34,6 +34,7 @@ import java.util.Hashtable;
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public class DummyDirContext implements DirContext {
|
||||
|
||||
@Override
|
||||
public Attributes getAttributes(Name name) throws NamingException {
|
||||
throw new UnsupportedOperationException();
|
||||
@@ -125,12 +126,14 @@ public class DummyDirContext implements DirContext {
|
||||
}
|
||||
|
||||
@Override
|
||||
public NamingEnumeration<SearchResult> search(Name name, Attributes matchingAttributes, String[] attributesToReturn) throws NamingException {
|
||||
public NamingEnumeration<SearchResult> search(Name name, Attributes matchingAttributes, String[] attributesToReturn)
|
||||
throws NamingException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public NamingEnumeration<SearchResult> search(String name, Attributes matchingAttributes, String[] attributesToReturn) throws NamingException {
|
||||
public NamingEnumeration<SearchResult> search(String name, Attributes matchingAttributes,
|
||||
String[] attributesToReturn) throws NamingException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@@ -145,22 +148,26 @@ public class DummyDirContext implements DirContext {
|
||||
}
|
||||
|
||||
@Override
|
||||
public NamingEnumeration<SearchResult> search(Name name, String filter, SearchControls cons) throws NamingException {
|
||||
public NamingEnumeration<SearchResult> search(Name name, String filter, SearchControls cons)
|
||||
throws NamingException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public NamingEnumeration<SearchResult> search(String name, String filter, SearchControls cons) throws NamingException {
|
||||
public NamingEnumeration<SearchResult> search(String name, String filter, SearchControls cons)
|
||||
throws NamingException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public NamingEnumeration<SearchResult> search(Name name, String filterExpr, Object[] filterArgs, SearchControls cons) throws NamingException {
|
||||
public NamingEnumeration<SearchResult> search(Name name, String filterExpr, Object[] filterArgs,
|
||||
SearchControls cons) throws NamingException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public NamingEnumeration<SearchResult> search(String name, String filterExpr, Object[] filterArgs, SearchControls cons) throws NamingException {
|
||||
public NamingEnumeration<SearchResult> search(String name, String filterExpr, Object[] filterArgs,
|
||||
SearchControls cons) throws NamingException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@@ -308,4 +315,5 @@ public class DummyDirContext implements DirContext {
|
||||
public String getNameInNamespace() throws NamingException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -34,18 +34,20 @@ import java.io.File;
|
||||
* @since 1.3.2
|
||||
*/
|
||||
public final class EmbeddedLdapServer {
|
||||
|
||||
private final DirectoryService directoryService;
|
||||
|
||||
private final LdapServer ldapServer;
|
||||
|
||||
private static File workingDirectory;
|
||||
|
||||
private EmbeddedLdapServer(DirectoryService directoryService,
|
||||
LdapServer ldapServer) {
|
||||
private EmbeddedLdapServer(DirectoryService directoryService, LdapServer ldapServer) {
|
||||
this.directoryService = directoryService;
|
||||
this.ldapServer = ldapServer;
|
||||
}
|
||||
|
||||
public static EmbeddedLdapServer newEmbeddedServer(String defaultPartitionName, String defaultPartitionSuffix, int port)
|
||||
throws Exception{
|
||||
public static EmbeddedLdapServer newEmbeddedServer(String defaultPartitionName, String defaultPartitionSuffix,
|
||||
int port) throws Exception {
|
||||
workingDirectory = new File(System.getProperty("java.io.tmpdir") + "/apacheds-test1");
|
||||
FileUtils.deleteDirectory(workingDirectory);
|
||||
|
||||
@@ -54,7 +56,7 @@ public final class EmbeddedLdapServer {
|
||||
directoryService.setAllowAnonymousAccess(true);
|
||||
|
||||
directoryService.setWorkingDirectory(workingDirectory);
|
||||
directoryService.getChangeLog().setEnabled( false );
|
||||
directoryService.getChangeLog().setEnabled(false);
|
||||
|
||||
JdbmPartition partition = new JdbmPartition();
|
||||
partition.setId(defaultPartitionName);
|
||||
@@ -64,19 +66,18 @@ public final class EmbeddedLdapServer {
|
||||
directoryService.startup();
|
||||
|
||||
// Inject the apache root entry if it does not already exist
|
||||
if ( !directoryService.getAdminSession().exists( partition.getSuffixDn() ) )
|
||||
{
|
||||
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 );
|
||||
directoryService.getAdminSession().add(entry);
|
||||
}
|
||||
|
||||
LdapServer ldapServer = new LdapServer();
|
||||
ldapServer.setDirectoryService(directoryService);
|
||||
|
||||
TcpTransport ldapTransport = new TcpTransport(port);
|
||||
ldapServer.setTransports( ldapTransport );
|
||||
ldapServer.setTransports(ldapTransport);
|
||||
ldapServer.start();
|
||||
|
||||
return new EmbeddedLdapServer(directoryService, ldapServer);
|
||||
@@ -88,4 +89,5 @@ public final class EmbeddedLdapServer {
|
||||
|
||||
FileUtils.deleteDirectory(workingDirectory);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,8 +22,11 @@ import org.springframework.beans.factory.config.AbstractFactoryBean;
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public class EmbeddedLdapServerFactoryBean extends AbstractFactoryBean<EmbeddedLdapServer> {
|
||||
|
||||
private int port;
|
||||
|
||||
private String partitionName;
|
||||
|
||||
private String partitionSuffix;
|
||||
|
||||
@Override
|
||||
@@ -52,4 +55,5 @@ public class EmbeddedLdapServerFactoryBean extends AbstractFactoryBean<EmbeddedL
|
||||
protected void destroyInstance(EmbeddedLdapServer instance) throws Exception {
|
||||
instance.shutdown();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -43,12 +43,13 @@ import java.io.InputStream;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Utilities for starting, stopping and populating an in-process Apache
|
||||
* Directory Server to use for integration testing purposes.
|
||||
* Utilities for starting, stopping and populating an in-process Apache Directory Server
|
||||
* to use for integration testing purposes.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public final class LdapTestUtils {
|
||||
|
||||
private final static Logger LOGGER = LoggerFactory.getLogger(LdapTestUtils.class);
|
||||
|
||||
private static EmbeddedLdapServer embeddedServer;
|
||||
@@ -59,51 +60,49 @@ public final class LdapTestUtils {
|
||||
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 <code>null</code>.
|
||||
* @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 <code>null</code>.
|
||||
* @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 {
|
||||
String defaultPartitionName, String principal, String credentials, Set extraSchemas)
|
||||
throws NamingException {
|
||||
|
||||
startEmbeddedServer(port, defaultPartitionSuffix, defaultPartitionName);
|
||||
return new DummyDirContext();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start an embedded Apache Directory Server. Only one embedded server will be permitted in the same JVM.
|
||||
*
|
||||
* @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.
|
||||
*
|
||||
* Start an embedded Apache Directory Server. Only one embedded server will be
|
||||
* permitted in the same JVM.
|
||||
* @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.
|
||||
* @throws IllegalStateException if an embedded server is already started.
|
||||
* @since 1.3.2
|
||||
*/
|
||||
public static void startEmbeddedServer(int port, String defaultPartitionSuffix, String defaultPartitionName) {
|
||||
if(embeddedServer != null) {
|
||||
if (embeddedServer != null) {
|
||||
throw new IllegalStateException("An embedded server is already started");
|
||||
}
|
||||
|
||||
try {
|
||||
embeddedServer = EmbeddedLdapServer.newEmbeddedServer(defaultPartitionName, defaultPartitionSuffix, port);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UncategorizedLdapException("Failed to start embedded server", e);
|
||||
}
|
||||
}
|
||||
@@ -112,20 +111,19 @@ public final class LdapTestUtils {
|
||||
* @deprecated use {@link #startEmbeddedServer(int, String, String)} instead.
|
||||
*/
|
||||
public static DirContext startApacheDirectoryServer(int port, String defaultPartitionSuffix,
|
||||
String defaultPartitionName, String principal, String credentials) throws NamingException {
|
||||
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.
|
||||
*
|
||||
* 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) {
|
||||
if (embeddedServer != null) {
|
||||
embeddedServer.shutdown();
|
||||
embeddedServer = null;
|
||||
}
|
||||
@@ -133,8 +131,7 @@ public final class LdapTestUtils {
|
||||
|
||||
/**
|
||||
* Shut down the in-process Apache Directory Server.
|
||||
*
|
||||
* @param principal the principal to be used for authentication.
|
||||
* @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.
|
||||
@@ -144,11 +141,10 @@ public final class LdapTestUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the directory sub-tree starting with the node represented by the
|
||||
* supplied distinguished name.
|
||||
*
|
||||
* 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.
|
||||
* @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 {
|
||||
@@ -156,20 +152,21 @@ public final class LdapTestUtils {
|
||||
try {
|
||||
ctx = contextSource.getReadWriteContext();
|
||||
clearSubContexts(ctx, name);
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
ctx.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
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.
|
||||
* 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.
|
||||
*/
|
||||
@@ -185,17 +182,21 @@ public final class LdapTestUtils {
|
||||
|
||||
try {
|
||||
ctx.unbind(childName);
|
||||
} catch (ContextNotEmptyException e) {
|
||||
}
|
||||
catch (ContextNotEmptyException e) {
|
||||
clearSubContexts(ctx, childName);
|
||||
ctx.unbind(childName);
|
||||
}
|
||||
}
|
||||
} catch (NamingException e) {
|
||||
}
|
||||
catch (NamingException e) {
|
||||
LOGGER.debug("Error cleaning sub-contexts", e);
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
enumeration.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Never mind this
|
||||
}
|
||||
}
|
||||
@@ -203,20 +204,21 @@ public final class LdapTestUtils {
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @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.
|
||||
}
|
||||
}
|
||||
@@ -235,8 +237,7 @@ public final class LdapTestUtils {
|
||||
|
||||
private static void loadLdif(DirContext context, Name rootNode, Resource ldifFile) {
|
||||
try {
|
||||
LdapName baseDn = (LdapName)
|
||||
context.getEnvironment().get(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY);
|
||||
LdapName baseDn = (LdapName) context.getEnvironment().get(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY);
|
||||
|
||||
LdifParser parser = new LdifParser(ldifFile);
|
||||
parser.open();
|
||||
@@ -245,16 +246,17 @@ public final class LdapTestUtils {
|
||||
|
||||
LdapName dn = record.getName();
|
||||
|
||||
if(baseDn != null) {
|
||||
if (baseDn != null) {
|
||||
dn = LdapUtils.removeFirst(dn, baseDn);
|
||||
}
|
||||
|
||||
if(!rootNode.isEmpty()) {
|
||||
if (!rootNode.isEmpty()) {
|
||||
dn = LdapUtils.prepend(dn, rootNode);
|
||||
}
|
||||
context.bind(dn, null, record);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UncategorizedLdapException("Failed to populate LDIF", e);
|
||||
}
|
||||
}
|
||||
@@ -266,12 +268,15 @@ public final 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -34,11 +34,15 @@ import java.util.List;
|
||||
* @since 2.0
|
||||
*/
|
||||
public class LdifPopulator implements InitializingBean {
|
||||
|
||||
private Resource resource;
|
||||
|
||||
private ContextSource contextSource;
|
||||
|
||||
private String base = "";
|
||||
|
||||
private boolean clean = false;
|
||||
|
||||
private String defaultBase;
|
||||
|
||||
public void setContextSource(ContextSource contextSource) {
|
||||
@@ -66,7 +70,7 @@ public class LdifPopulator implements InitializingBean {
|
||||
Assert.notNull(contextSource, "ContextSource must be specified");
|
||||
Assert.notNull(resource, "Resource must be specified");
|
||||
|
||||
if(!LdapUtils.newLdapName(base).equals(LdapUtils.newLdapName(defaultBase))) {
|
||||
if (!LdapUtils.newLdapName(base).equals(LdapUtils.newLdapName(defaultBase))) {
|
||||
List<String> lines = IOUtils.readLines(resource.getInputStream());
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
@@ -79,10 +83,11 @@ public class LdifPopulator implements InitializingBean {
|
||||
resource = new ByteArrayResource(sw.toString().getBytes("UTF8"));
|
||||
}
|
||||
|
||||
if(clean) {
|
||||
if (clean) {
|
||||
LdapTestUtils.clearSubContexts(contextSource, LdapUtils.emptyLdapName());
|
||||
}
|
||||
|
||||
LdapTestUtils.loadLdif(contextSource, resource);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.springframework.ldap.support.LdapUtils;
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public class TestContextSourceFactoryBean extends AbstractFactoryBean {
|
||||
|
||||
private int port;
|
||||
|
||||
private String defaultPartitionSuffix;
|
||||
@@ -142,4 +143,5 @@ public class TestContextSourceFactoryBean extends AbstractFactoryBean {
|
||||
super.destroyInstance(instance);
|
||||
LdapTestUtils.shutdownEmbeddedServer();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -36,10 +36,9 @@ public final class EmbeddedLdapServer {
|
||||
this.directoryServer = directoryServer;
|
||||
}
|
||||
|
||||
public static EmbeddedLdapServer newEmbeddedServer(String defaultPartitionName,
|
||||
String defaultPartitionSuffix, int port) throws Exception {
|
||||
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(
|
||||
defaultPartitionSuffix);
|
||||
public static EmbeddedLdapServer newEmbeddedServer(String defaultPartitionName, String defaultPartitionSuffix,
|
||||
int port) throws Exception {
|
||||
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(defaultPartitionSuffix);
|
||||
config.addAdditionalBindCredentials("uid=admin,ou=system", "secret");
|
||||
|
||||
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", port));
|
||||
@@ -60,4 +59,5 @@ public final class EmbeddedLdapServer {
|
||||
public void shutdown() throws Exception {
|
||||
this.directoryServer.shutDown(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,8 +22,11 @@ import org.springframework.beans.factory.config.AbstractFactoryBean;
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public class EmbeddedLdapServerFactoryBean extends AbstractFactoryBean<EmbeddedLdapServer> {
|
||||
|
||||
private int port;
|
||||
|
||||
private String partitionName;
|
||||
|
||||
private String partitionSuffix;
|
||||
|
||||
@Override
|
||||
@@ -52,4 +55,5 @@ public class EmbeddedLdapServerFactoryBean extends AbstractFactoryBean<EmbeddedL
|
||||
protected void destroyInstance(EmbeddedLdapServer instance) throws Exception {
|
||||
instance.shutdown();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -46,12 +46,13 @@ import com.unboundid.ldap.listener.InMemoryDirectoryServer;
|
||||
import com.unboundid.ldap.sdk.LDAPException;
|
||||
|
||||
/**
|
||||
* Utilities for starting, stopping and populating an in-process Apache
|
||||
* Directory Server to use for integration testing purposes.
|
||||
* Utilities for starting, stopping and populating an in-process Apache Directory Server
|
||||
* to use for integration testing purposes.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public final class LdapTestUtils {
|
||||
|
||||
private final static Logger LOGGER = LoggerFactory.getLogger(LdapTestUtils.class);
|
||||
|
||||
private static EmbeddedLdapServer embeddedServer;
|
||||
@@ -63,47 +64,45 @@ public final class LdapTestUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Start an embedded Apache Directory Server. Only one embedded server will be permitted in the same JVM.
|
||||
*
|
||||
* @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.
|
||||
*
|
||||
* Start an embedded Apache Directory Server. Only one embedded server will be
|
||||
* permitted in the same JVM.
|
||||
* @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.
|
||||
* @throws IllegalStateException if an embedded server is already started.
|
||||
*/
|
||||
public static void startEmbeddedServer(int port, String defaultPartitionSuffix, String defaultPartitionName) {
|
||||
if(embeddedServer != null) {
|
||||
if (embeddedServer != null) {
|
||||
throw new IllegalStateException("An embedded server is already started");
|
||||
}
|
||||
|
||||
try {
|
||||
embeddedServer = EmbeddedLdapServer.newEmbeddedServer(defaultPartitionName, defaultPartitionSuffix, port);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UncategorizedLdapException("Failed to start embedded server", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shuts down the embedded server, if there is one. If no server was previously started in this JVM
|
||||
* this is silently ignored.
|
||||
*
|
||||
* Shuts down the embedded server, if there is one. If no server was previously
|
||||
* started in this JVM this is silently ignored.
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void shutdownEmbeddedServer() throws Exception {
|
||||
if(embeddedServer != null) {
|
||||
if (embeddedServer != null) {
|
||||
embeddedServer.shutdown();
|
||||
embeddedServer = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the directory sub-tree starting with the node represented by the
|
||||
* supplied distinguished name.
|
||||
*
|
||||
* 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.
|
||||
* @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 {
|
||||
@@ -111,20 +110,21 @@ public final class LdapTestUtils {
|
||||
try {
|
||||
ctx = contextSource.getReadWriteContext();
|
||||
clearSubContexts(ctx, name);
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
ctx.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
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.
|
||||
* 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.
|
||||
*/
|
||||
@@ -140,17 +140,21 @@ public final class LdapTestUtils {
|
||||
|
||||
try {
|
||||
ctx.unbind(childName);
|
||||
} catch (ContextNotEmptyException e) {
|
||||
}
|
||||
catch (ContextNotEmptyException e) {
|
||||
clearSubContexts(ctx, childName);
|
||||
ctx.unbind(childName);
|
||||
}
|
||||
}
|
||||
} catch (NamingException e) {
|
||||
}
|
||||
catch (NamingException e) {
|
||||
LOGGER.debug("Error cleaning sub-contexts", e);
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
enumeration.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Never mind this
|
||||
}
|
||||
}
|
||||
@@ -158,20 +162,21 @@ public final class LdapTestUtils {
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @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.
|
||||
}
|
||||
}
|
||||
@@ -191,8 +196,7 @@ public final class LdapTestUtils {
|
||||
@SuppressWarnings("deprecation")
|
||||
private static void loadLdif(DirContext context, Name rootNode, Resource ldifFile) {
|
||||
try {
|
||||
LdapName baseDn = (LdapName)
|
||||
context.getEnvironment().get(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY);
|
||||
LdapName baseDn = (LdapName) context.getEnvironment().get(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY);
|
||||
|
||||
LdifParser parser = new LdifParser(ldifFile);
|
||||
parser.open();
|
||||
@@ -201,16 +205,17 @@ public final class LdapTestUtils {
|
||||
|
||||
LdapName dn = record.getName();
|
||||
|
||||
if(baseDn != null) {
|
||||
if (baseDn != null) {
|
||||
dn = LdapUtils.removeFirst(dn, baseDn);
|
||||
}
|
||||
|
||||
if(!rootNode.isEmpty()) {
|
||||
if (!rootNode.isEmpty()) {
|
||||
dn = LdapUtils.prepend(dn, rootNode);
|
||||
}
|
||||
context.bind(dn, null, record);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UncategorizedLdapException("Failed to populate LDIF", e);
|
||||
}
|
||||
}
|
||||
@@ -222,14 +227,18 @@ public final class LdapTestUtils {
|
||||
IOUtils.copy(inputStream, new FileOutputStream(tempFile));
|
||||
directoryServer.importFromLDIF(true, new LDIFReader(tempFile));
|
||||
directoryServer.restartServer();
|
||||
} catch (LDAPException e) {
|
||||
}
|
||||
catch (LDAPException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
tempFile.delete();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Ignore this
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,11 +35,15 @@ import org.springframework.util.Assert;
|
||||
* @since 2.0
|
||||
*/
|
||||
public class LdifPopulator implements InitializingBean {
|
||||
|
||||
private Resource resource;
|
||||
|
||||
private ContextSource contextSource;
|
||||
|
||||
private String base = "";
|
||||
|
||||
private boolean clean = false;
|
||||
|
||||
private String defaultBase;
|
||||
|
||||
public void setContextSource(ContextSource contextSource) {
|
||||
@@ -67,7 +71,7 @@ public class LdifPopulator implements InitializingBean {
|
||||
Assert.notNull(contextSource, "ContextSource must be specified");
|
||||
Assert.notNull(resource, "Resource must be specified");
|
||||
|
||||
if(!LdapUtils.newLdapName(base).equals(LdapUtils.newLdapName(defaultBase))) {
|
||||
if (!LdapUtils.newLdapName(base).equals(LdapUtils.newLdapName(defaultBase))) {
|
||||
List<String> lines = IOUtils.readLines(resource.getInputStream());
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
@@ -80,10 +84,11 @@ public class LdifPopulator implements InitializingBean {
|
||||
resource = new ByteArrayResource(sw.toString().getBytes("UTF8"));
|
||||
}
|
||||
|
||||
if(clean) {
|
||||
if (clean) {
|
||||
LdapTestUtils.clearSubContexts(contextSource, LdapUtils.emptyLdapName());
|
||||
}
|
||||
|
||||
LdapTestUtils.loadLdif(contextSource, resource);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -98,8 +98,7 @@ public class TestContextSourceFactoryBean extends AbstractFactoryBean<ContextSou
|
||||
}
|
||||
|
||||
protected ContextSource createInstance() throws Exception {
|
||||
LdapTestUtils.startEmbeddedServer(port,
|
||||
defaultPartitionSuffix, defaultPartitionName);
|
||||
LdapTestUtils.startEmbeddedServer(port, defaultPartitionSuffix, defaultPartitionName);
|
||||
|
||||
if (contextSource == null) {
|
||||
// If not explicitly configured, create a new instance.
|
||||
@@ -146,4 +145,5 @@ public class TestContextSourceFactoryBean extends AbstractFactoryBean<ContextSou
|
||||
super.destroyInstance(instance);
|
||||
LdapTestUtils.shutdownEmbeddedServer();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,11 +23,9 @@ public class EmbeddedLdapServerFactoryBeanTest {
|
||||
LdapTemplate ldapTemplate = ctx.getBean(LdapTemplate.class);
|
||||
assertNotNull(ldapTemplate);
|
||||
|
||||
List<String> list = ldapTemplate.search(
|
||||
LdapQueryBuilder.query().where("objectclass").is("person"),
|
||||
List<String> list = ldapTemplate.search(LdapQueryBuilder.query().where("objectclass").is("person"),
|
||||
new AttributesMapper<String>() {
|
||||
public String mapFromAttributes(Attributes attrs)
|
||||
throws NamingException {
|
||||
public String mapFromAttributes(Attributes attrs) throws NamingException {
|
||||
return (String) attrs.get("cn").get();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -32,11 +32,12 @@ import org.springframework.ldap.query.LdapQueryBuilder;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class EmbeddedLdapServerFactoryBeanTest {
|
||||
|
||||
ClassPathXmlApplicationContext ctx;
|
||||
|
||||
@After
|
||||
public void setup() {
|
||||
if(ctx != null) {
|
||||
if (ctx != null) {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
@@ -47,11 +48,9 @@ public class EmbeddedLdapServerFactoryBeanTest {
|
||||
LdapTemplate ldapTemplate = ctx.getBean(LdapTemplate.class);
|
||||
assertThat(ldapTemplate).isNotNull();
|
||||
|
||||
List<String> list = ldapTemplate.search(
|
||||
LdapQueryBuilder.query().where("objectclass").is("person"),
|
||||
List<String> list = ldapTemplate.search(LdapQueryBuilder.query().where("objectclass").is("person"),
|
||||
new AttributesMapper<String>() {
|
||||
public String mapFromAttributes(Attributes attrs)
|
||||
throws NamingException {
|
||||
public String mapFromAttributes(Attributes attrs) throws NamingException {
|
||||
return (String) attrs.get("cn").get();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -32,11 +32,12 @@ import org.springframework.ldap.query.LdapQueryBuilder;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestContextSourceFactoryBeanTest {
|
||||
|
||||
ClassPathXmlApplicationContext ctx;
|
||||
|
||||
@After
|
||||
public void setup() {
|
||||
if(ctx != null) {
|
||||
if (ctx != null) {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
@@ -47,11 +48,9 @@ public class TestContextSourceFactoryBeanTest {
|
||||
LdapTemplate ldapTemplate = ctx.getBean(LdapTemplate.class);
|
||||
assertThat(ldapTemplate).isNotNull();
|
||||
|
||||
List<String> list = ldapTemplate.search(
|
||||
LdapQueryBuilder.query().where("objectclass").is("person"),
|
||||
List<String> list = ldapTemplate.search(LdapQueryBuilder.query().where("objectclass").is("person"),
|
||||
new AttributesMapper<String>() {
|
||||
public String mapFromAttributes(Attributes attrs)
|
||||
throws NamingException {
|
||||
public String mapFromAttributes(Attributes attrs) throws NamingException {
|
||||
return (String) attrs.get("cn").get();
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user