测试

使用内置服务器

spring-ldap-test 提供了一个基于 ApacheDSUnboundID 的嵌入式LDAP服务器。spring-doc.cadn.net.cn

spring-ldap-test 与 ApacheDS 1.5.5 兼容。不支持 ApacheDS 更新版本。

开始时,你需要包含 spring-ldap-test 依赖项。spring-doc.cadn.net.cn

以下代码示例显示如何在 Maven 中包含 spring-ldap-testspring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.ldap</groupId>
    <artifactId>spring-ldap-test</artifactId>
    <version>4.0.2</version>
    <scope>test</scope>
</dependency>

以下代码示例显示如何在 Gradle 中包含 spring-ldap-testspring-doc.cadn.net.cn

testCompile "org.springframework.ldap:spring-ldap-test:4.0.2"

ApacheDS

要使用ApacheDS,你需要包含一组ApacheDS依赖项。spring-doc.cadn.net.cn

以下示例显示了如何在 Maven 中包含 ApacheDS 依赖项:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.apache.directory.server</groupId>
    <artifactId>apacheds-core</artifactId>
    <version>1.5.5</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.directory.server</groupId>
    <artifactId>apacheds-core-entry</artifactId>
    <version>1.5.5</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.directory.server</groupId>
    <artifactId>apacheds-protocol-shared</artifactId>
    <version>1.5.5</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.directory.server</groupId>
    <artifactId>apacheds-protocol-ldap</artifactId>
    <version>1.5.5</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.directory.server</groupId>
    <artifactId>apacheds-server-jndi</artifactId>
    <version>1.5.5</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.directory.shared</groupId>
    <artifactId>shared-ldap</artifactId>
    <version>0.9.15</version>
    <scope>test</scope>
</dependency>

以下示例显示如何在 Gradle 中包含 ApacheDS 依赖项:spring-doc.cadn.net.cn

testCompile "org.apache.directory.server:apacheds-core:1.5.5",
            "org.apache.directory.server:apacheds-core-entry:1.5.5",
            "org.apache.directory.server:apacheds-protocol-shared:1.5.5",
            "org.apache.directory.server:apacheds-protocol-ldap:1.5.5",
            "org.apache.directory.server:apacheds-server-jndi:1.5.5",
            "org.apache.directory.shared:shared-ldap:0.9.15"

以下的 bean 定义创建了一个嵌入式的 LDAP 服务器:spring-doc.cadn.net.cn

<bean id="embeddedLdapServer" class="org.springframework.ldap.test.EmbeddedLdapServerFactoryBean">
    <property name="partitionName" value="example"/>
    <property name="partitionSuffix" value="dc=261consulting,dc=com" />
    <property name="port" value="9321" />
</bean>

spring-ldap-test 提供了一种使用 org.springframework.ldap.test.LdifPopulator 通过 LDAP 服务器填充数据的机制。要使用它,请创建一个类似的 bean:spring-doc.cadn.net.cn

<bean class="org.springframework.ldap.test.LdifPopulator" depends-on="embeddedLdapServer">
    <property name="contextSource" ref="contextSource" />
    <property name="resource" value="classpath:/setup_data.ldif" />
    <property name="base" value="dc=jayway,dc=se" />
    <property name="clean" value="true" />
    <property name="defaultBase" value="dc=jayway,dc=se" />
</bean>

另一种对抗嵌入式LDAP服务器的方法是使用org.springframework.ldap.test.TestContextSourceFactoryBean,如下所示:spring-doc.cadn.net.cn

<bean id="contextSource" class="org.springframework.ldap.test.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="classpath:/setup_data.ldif" />
    <property name="port" value="1888" />
</bean>

也,org.springframework.ldap.test.LdapTestUtils 提供了程序化地与嵌入式 LDAP 服务器交互的方法。spring-doc.cadn.net.cn

未绑定ID

要使用 UnboundID,需要包含一个 UnboundID 依赖项。spring-doc.cadn.net.cn

以下示例显示了如何在 Maven 中包含 UnboundID 依赖项:spring-doc.cadn.net.cn

<dependency>
    <groupId>com.unboundid</groupId>
    <artifactId>unboundid-ldapsdk</artifactId>
    <version>3.1.1</version>
    <scope>test</scope>
</dependency>

以下示例显示了如何在 Gradle 中包含 UnboundID 依赖项:spring-doc.cadn.net.cn

testCompile "com.unboundid:unboundid-ldapsdk:3.1.1"

以下的 bean 定义创建了一个嵌入式的 LDAP 服务器:spring-doc.cadn.net.cn

@Bean
EmbeddedLdapServer embeddedLdapServer() {
    return EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se")
        .partitionName("jayway")
        .port(18881)
        .configurationCustomizer((config) -> config.setCodeLogDetails(tempLogFile, true))
        .build();
}

或者,你可以使用 org.springframework.ldap.test.unboundid.LdifPopulator 来创建并填充LDAP服务器。要使用它,请创建一个类似的bean:spring-doc.cadn.net.cn

<bean class="org.springframework.ldap.test.unboundid.LdifPopulator" depends-on="embeddedLdapServer">
    <property name="contextSource" ref="contextSource" />
    <property name="resource" value="classpath:/setup_data.ldif" />
    <property name="base" value="dc=jayway,dc=se" />
    <property name="clean" value="true" />
    <property name="defaultBase" value="dc=jayway,dc=se" />
</bean>

另一种对抗嵌入式LDAP服务器的方法是使用org.springframework.ldap.test.unboundid.TestContextSourceFactoryBean。 要使用它,请创建一个类似于以下内容的bean:spring-doc.cadn.net.cn

<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="classpath:/setup_data.ldif" />
    <property name="port" value="1888" />
</bean>

也,org.springframework.ldap.test.unboundid.LdapTestUtils 提供了程序化地操作嵌入式 LDAP 服务器的方法。spring-doc.cadn.net.cn