Update to Gradle 7.3.3

Issue gh-610
Closes gh-613
Closes gh-581
This commit is contained in:
Josh Cummings
2022-01-13 16:16:50 -07:00
parent 02ee328e6c
commit 28f2ca6ee0
221 changed files with 10092 additions and 2337 deletions

View File

@@ -1,17 +1,21 @@
plugins {
id 'io.spring.convention.spring-module'
}
dependencies {
implementation project(":spring-ldap-core"),
project(":spring-ldap-ldif-core"),
"com.google.code.typica:typica:1.3",
"commons-io:commons-io:$commonsIoVersion",
"commons-io:commons-io",
"javax.activation:activation:1.1",
"org.springframework:spring-core:$springVersion",
"org.springframework:spring-beans:$springVersion",
"org.springframework:spring-context:$springVersion",
"org.springframework:spring-test:$springVersion"
"org.springframework:spring-core",
"org.springframework:spring-beans",
"org.springframework:spring-context",
"org.springframework:spring-test"
optional apachedsDependencies
optional "com.unboundid:unboundid-ldapsdk"
provided "junit:junit:$junitVersion"
provided "junit:junit"
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2005-2016 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
*
* https://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.unboundid;
import com.unboundid.ldap.listener.InMemoryDirectoryServer;
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
import com.unboundid.ldap.listener.InMemoryListenerConfig;
import com.unboundid.ldap.sdk.DN;
import com.unboundid.ldap.sdk.Entry;
/**
* Helper class for embedded Unboundid ldap server.
*
* @author Eddu Melendez
* @since 2.1.0
*/
public final class EmbeddedLdapServer {
private InMemoryDirectoryServer directoryServer;
private EmbeddedLdapServer(InMemoryDirectoryServer directoryServer) {
this.directoryServer = directoryServer;
}
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));
config.setEnforceSingleStructuralObjectClass(false);
config.setEnforceAttributeSyntaxCompliance(true);
Entry entry = new Entry(new DN(defaultPartitionSuffix));
entry.addAttribute("objectClass", "top", "domain", "extensibleObject");
entry.addAttribute("dc", defaultPartitionName);
InMemoryDirectoryServer directoryServer = new InMemoryDirectoryServer(config);
directoryServer.add(entry);
directoryServer.startListening();
return new EmbeddedLdapServer(directoryServer);
}
public void shutdown() throws Exception {
this.directoryServer.shutDown(true);
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2005-2016 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
*
* https://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.unboundid;
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
public Class<?> getObjectType() {
return EmbeddedLdapServer.class;
}
public void setPartitionName(String partitionName) {
this.partitionName = partitionName;
}
public void setPartitionSuffix(String partitionSuffix) {
this.partitionSuffix = partitionSuffix;
}
public void setPort(int port) {
this.port = port;
}
@Override
protected EmbeddedLdapServer createInstance() throws Exception {
return EmbeddedLdapServer.newEmbeddedServer(this.partitionName, this.partitionSuffix, this.port);
}
@Override
protected void destroyInstance(EmbeddedLdapServer instance) throws Exception {
instance.shutdown();
}
}

View File

@@ -0,0 +1,235 @@
/*
* Copyright 2005-2016 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
*
* https://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.unboundid;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.naming.Binding;
import javax.naming.ContextNotEmptyException;
import javax.naming.Name;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.ldap.LdapName;
import com.unboundid.ldif.LDIFReader;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.ldap.UncategorizedLdapException;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.LdapAttributes;
import org.springframework.ldap.core.support.DefaultDirObjectFactory;
import org.springframework.ldap.ldif.parser.LdifParser;
import org.springframework.ldap.support.LdapUtils;
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.
*
* @author Mattias Hellborg Arthursson
*/
public final class LdapTestUtils {
private final static Logger LOGGER = LoggerFactory.getLogger(LdapTestUtils.class);
private static EmbeddedLdapServer embeddedServer;
/**
* Not to be instantiated.
*/
private 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.
*
* @throws IllegalStateException if an embedded server is already started.
*/
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 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.
*
* @throws Exception
*/
public static void shutdownEmbeddedServer() throws Exception {
if(embeddedServer != null) {
embeddedServer.shutdown();
embeddedServer = null;
}
}
/**
* 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();
Name childName = LdapUtils.newLdapName(element.getName());
childName = LdapUtils.prepend(childName, name);
try {
ctx.unbind(childName);
} catch (ContextNotEmptyException e) {
clearSubContexts(ctx, childName);
ctx.unbind(childName);
}
}
} catch (NamingException e) {
LOGGER.debug("Error cleaning sub-contexts", e);
} 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, Name rootNode, Resource ldifFile)
throws NamingException, IOException {
clearSubContexts(contextSource, rootNode);
loadLdif(contextSource, ldifFile);
}
private static void loadLdif(DirContext context, Resource ldifFile) throws IOException {
loadLdif(context, LdapUtils.emptyLdapName(), ldifFile);
}
@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);
LdifParser parser = new LdifParser(ldifFile);
parser.open();
while (parser.hasMoreRecords()) {
LdapAttributes record = parser.getRecord();
LdapName dn = record.getName();
if(baseDn != null) {
dn = LdapUtils.removeFirst(dn, baseDn);
}
if(!rootNode.isEmpty()) {
dn = LdapUtils.prepend(dn, rootNode);
}
context.bind(dn, null, record);
}
} catch (Exception e) {
throw new UncategorizedLdapException("Failed to populate LDIF", e);
}
}
public static void loadLdif(InMemoryDirectoryServer directoryServer, Resource ldifFile) throws IOException {
File tempFile = File.createTempFile("spring_ldap_test", ".ldif");
try {
InputStream inputStream = ldifFile.getInputStream();
IOUtils.copy(inputStream, new FileOutputStream(tempFile));
directoryServer.importFromLDIF(true, new LDIFReader(tempFile));
directoryServer.restartServer();
} catch (LDAPException e) {
e.printStackTrace();
} finally {
try {
tempFile.delete();
} catch (Exception e) {
// Ignore this
}
}
}
}

View File

@@ -0,0 +1,89 @@
/*
* Copyright 2005-2016 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
*
* https://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.unboundid;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.support.LdapUtils;
import org.springframework.util.Assert;
/**
* @author Mattias Hellborg Arthursson
* @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) {
this.contextSource = contextSource;
}
public void setResource(Resource resource) {
this.resource = resource;
}
public void setBase(String base) {
this.base = base;
}
public void setClean(boolean clean) {
this.clean = clean;
}
public void setDefaultBase(String defaultBase) {
this.defaultBase = defaultBase;
}
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(contextSource, "ContextSource must be specified");
Assert.notNull(resource, "Resource must be specified");
if(!LdapUtils.newLdapName(base).equals(LdapUtils.newLdapName(defaultBase))) {
List<String> lines = IOUtils.readLines(resource.getInputStream());
StringWriter sw = new StringWriter();
PrintWriter writer = new PrintWriter(sw);
for (String line : lines) {
writer.println(StringUtils.replace(line, defaultBase, base));
}
writer.flush();
resource = new ByteArrayResource(sw.toString().getBytes("UTF8"));
}
if(clean) {
LdapTestUtils.clearSubContexts(contextSource, LdapUtils.emptyLdapName());
}
LdapTestUtils.loadLdif(contextSource, resource);
}
}

View File

@@ -0,0 +1,149 @@
/*
* Copyright 2005-2016 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
*
* https://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.unboundid;
import javax.naming.spi.DirObjectFactory;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.core.io.Resource;
import org.springframework.ldap.core.AuthenticationSource;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.support.DefaultDirObjectFactory;
import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.ldap.support.LdapUtils;
/**
* @author Mattias Hellborg Arthursson
*/
public class TestContextSourceFactoryBean extends AbstractFactoryBean<ContextSource> {
private int port;
private String defaultPartitionSuffix;
private String defaultPartitionName;
private String principal;
private String password;
private boolean baseOnTarget = true;
private Resource ldifFile;
private Class<? extends DirObjectFactory> dirObjectFactory = DefaultDirObjectFactory.class;
private boolean pooled = true;
private AuthenticationSource authenticationSource;
private ContextSource contextSource;
public void setAuthenticationSource(AuthenticationSource authenticationSource) {
this.authenticationSource = authenticationSource;
}
public void setPooled(boolean pooled) {
this.pooled = pooled;
}
public void setDirObjectFactory(Class<? extends DirObjectFactory> dirObjectFactory) {
this.dirObjectFactory = dirObjectFactory;
}
public void setLdifFile(Resource ldifFile) {
this.ldifFile = ldifFile;
}
public void setBaseOnTarget(boolean baseOnTarget) {
this.baseOnTarget = baseOnTarget;
}
public void setDefaultPartitionSuffix(String defaultPartitionSuffix) {
this.defaultPartitionSuffix = defaultPartitionSuffix;
}
public void setPrincipal(String principal) {
this.principal = principal;
}
public void setPassword(String password) {
this.password = password;
}
public void setDefaultPartitionName(String defaultPartitionName) {
this.defaultPartitionName = defaultPartitionName;
}
public void setPort(int port) {
this.port = port;
}
public void setContextSource(ContextSource contextSource) {
this.contextSource = contextSource;
}
protected ContextSource createInstance() throws Exception {
LdapTestUtils.startEmbeddedServer(port,
defaultPartitionSuffix, defaultPartitionName);
if (contextSource == null) {
// If not explicitly configured, create a new instance.
LdapContextSource targetContextSource = new LdapContextSource();
if (baseOnTarget) {
targetContextSource.setBase(defaultPartitionSuffix);
}
targetContextSource.setUrl("ldap://localhost:" + port);
targetContextSource.setUserDn(principal);
targetContextSource.setPassword(password);
targetContextSource.setDirObjectFactory(dirObjectFactory);
targetContextSource.setPooled(pooled);
if (authenticationSource != null) {
targetContextSource.setAuthenticationSource(authenticationSource);
}
targetContextSource.afterPropertiesSet();
contextSource = targetContextSource;
}
Thread.sleep(1000);
if (baseOnTarget) {
LdapTestUtils.clearSubContexts(contextSource, LdapUtils.emptyLdapName());
}
else {
LdapTestUtils.clearSubContexts(contextSource, LdapUtils.newLdapName(defaultPartitionSuffix));
}
if (ldifFile != null) {
LdapTestUtils.loadLdif(contextSource, ldifFile);
}
return contextSource;
}
public Class<ContextSource> getObjectType() {
return ContextSource.class;
}
protected void destroyInstance(ContextSource instance) throws Exception {
super.destroyInstance(instance);
LdapTestUtils.shutdownEmbeddedServer();
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2005-2016 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
*
* https://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.unboundid;
import java.util.List;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import org.junit.After;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
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) {
ctx.close();
}
}
@Test
public void testServerStartup() throws Exception {
ctx = new ClassPathXmlApplicationContext("/applicationContext-ldifPopulator.xml");
LdapTemplate ldapTemplate = ctx.getBean(LdapTemplate.class);
assertThat(ldapTemplate).isNotNull();
List<String> list = ldapTemplate.search(
LdapQueryBuilder.query().where("objectclass").is("person"),
new AttributesMapper<String>() {
public String mapFromAttributes(Attributes attrs)
throws NamingException {
return (String) attrs.get("cn").get();
}
});
assertThat(list.size()).isEqualTo(5);
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2005-2016 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
*
* https://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.unboundid;
import java.util.List;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import org.junit.After;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
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) {
ctx.close();
}
}
@Test
public void testServerStartup() throws Exception {
ctx = new ClassPathXmlApplicationContext("/applicationContext-testContextSource.xml");
LdapTemplate ldapTemplate = ctx.getBean(LdapTemplate.class);
assertThat(ldapTemplate).isNotNull();
List<String> list = ldapTemplate.search(
LdapQueryBuilder.query().where("objectclass").is("person"),
new AttributesMapper<String>() {
public String mapFromAttributes(Attributes attrs)
throws NamingException {
return (String) attrs.get("cn").get();
}
});
assertThat(list.size()).isEqualTo(5);
}
}

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
<property name="contextSource" ref="contextSource" />
</bean>
<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource" depends-on="embeddedLdapServer">
<property name="base" value="dc=jayway,dc=se" />
<property name="url" value="ldap://127.0.0.1:18880" />
<property name="userDn" value="uid=admin,ou=system" />
<property name="password" value="secret" />
</bean>
<bean id="ldifPopulator" class="org.springframework.ldap.test.unboundid.LdifPopulator" depends-on="embeddedLdapServer">
<property name="contextSource" ref="contextSource" />
<property name="resource" value="setup_data.ldif" />
<property name="clean" value="true" />
<property name="base" value="dc=jayway,dc=se" />
<property name="defaultBase" value="dc=jayway,dc=se" />
</bean>
<bean id="embeddedLdapServer" class="org.springframework.ldap.test.unboundid.EmbeddedLdapServerFactoryBean">
<property name="partitionName" value="jayway"/>
<property name="partitionSuffix" value="dc=jayway,dc=se" />
<property name="port" value="18880" />
</bean>
</beans>

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
<property name="contextSource" ref="contextSource" />
</bean>
<bean id="contextSource" class="org.springframework.ldap.test.unboundid.TestContextSourceFactoryBean">
<property name="defaultPartitionSuffix" value="dc=jayway,dc=se" />
<property name="defaultPartitionName" value="jayway" />
<property name="principal" value="uid=admin,ou=system" />
<property name="password" value="secret" />
<property name="ldifFile" value="setup_data.ldif" />
<property name="port" value="18881" />
</bean>
</beans>