测试
使用嵌入式服务器
spring-ldap-test 兼容 ApacheDS 1.5.5。不支持更新版本的 ApacheDS。 |
首先,您需要包含 spring-ldap-test 依赖项。
以下清单展示了如何为 Maven 引入 spring-ldap-test
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-test</artifactId>
<version>4.0.0</version>
<scope>test</scope>
</dependency>
以下清单展示了如何为 Gradle 引入 spring-ldap-test
testCompile "org.springframework.ldap:spring-ldap-test:4.0.0"
ApacheDS
要使用 ApacheDS,您需要包含多个 ApacheDS 依赖项。
以下示例展示了如何为 Maven 引入 ApacheDS 依赖项
<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 依赖项
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 服务器
<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:
<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,如下所示:
<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 服务器交互的方法。
UnboundID
要使用 UnboundID,您需要包含一个 UnboundID 依赖项。
以下示例展示了如何为 Maven 引入 UnboundID 依赖项
<dependency>
<groupId>com.unboundid</groupId>
<artifactId>unboundid-ldapsdk</artifactId>
<version>3.1.1</version>
<scope>test</scope>
</dependency>
以下示例展示了如何为 Gradle 引入 UnboundID 依赖项
testCompile "com.unboundid:unboundid-ldapsdk:3.1.1"
以下 bean 定义创建了一个嵌入式 LDAP 服务器
@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:
<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:
<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 服务器交互的方法。