Testing
本部分介绍使用 Spring LDAP 进行测试的内容。它包含以下主题:
This section covers testing with Spring LDAP. It contains the following topics:
Using an Embedded Server
@"28" 提供了一个基于 @"29" 或 @"30" 的嵌入式 LDAP 服务器。
|
|
要开始使用,您需要包含 spring-ldap-test
依赖项。
To get started, you need to include the spring-ldap-test
dependency.
以下清单显示如何为 Maven 包含 spring-ldap-test
:
The following listing shows how to include the spring-ldap-test
for Maven:
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-test</artifactId>
<version>{project-version}</version>
<scope>test</scope>
</dependency>
以下清单显示如何为 Gradle 包含 spring-ldap-test
:
The following listing shows how to include the spring-ldap-test
for Gradle:
testCompile "org.springframework.ldap:spring-ldap-test:{project-version}"
ApacheDS
要使用 ApacheDS,您需要包含多个 ApacheDS 依赖项。
To use ApacheDS, you need to include a number of ApacheDS dependencies.
以下示例展示如何为 Maven 包含 ApacheDS 依赖项:
The following example shows how to include the ApacheDS dependencies for Maven:
<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 依赖项:
The following example shows how to include the ApacheDS dependencies for Gradle:
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 服务器:
The following bean definition creates an embedded LDAP server:
<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-ldap-test
provides a mechanism to populate the LDAP server by using org.springframework.ldap.test.LdifPopulator
. To use it, create a bean similar to the following:
<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
,如下所示:
Another way to work against an embedded LDAP server is by using org.springframework.ldap.test.TestContextSourceFactoryBean
, as follows:
<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 服务器的方法。
Also, org.springframework.ldap.test.LdapTestUtils
provides methods to programmatically work with an embedded LDAP server.
UnboundID
要使用 UnboundID,您需要包含一个 UnboundID 依赖项。
To use UnboundID, you need to include an UnboundID dependency.
以下示例展示如何为 Maven 包含 UnboundID 依赖项:
The following example shows how to include the UnboundID dependency for Maven:
<dependency>
<groupId>com.unboundid</groupId>
<artifactId>unboundid-ldapsdk</artifactId>
<version>3.1.1</version>
<scope>test</scope>
</dependency>
以下示例展示如何为 Gradle 包含 UnboundID 依赖项:
The following example shows how to include the UnboundID dependency for Gradle:
testCompile "com.unboundid:unboundid-ldapsdk:3.1.1"
以下 Bean 定义创建一个嵌入式 LDAP 服务器:
The following bean definition creates an embedded LDAP server:
<bean id="embeddedLdapServer" class="org.springframework.ldap.test.unboundid.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.unboundid.LdifPopulator
来填充 LDAP 服务器的方法。若要使用它,创建类似于以下内容的 Bean:
spring-ldap-test
provides a way to populate the LDAP server by using org.springframework.ldap.test.unboundid.LdifPopulator
. To use it, create a bean similar to the following:
<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:
Another way to work against an embedded LDAP server is by using org.springframework.ldap.test.unboundid.TestContextSourceFactoryBean
.
To use it, create a bean similar to the following:
<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 服务器的方法。
Also, org.springframework.ldap.test.unboundid.LdapTestUtils
provide methods to programmatically work with an embedded LDAP server.