測試

本節介紹使用 Spring LDAP 進行測試。它包含以下主題:

使用嵌入式伺服器

spring-ldap-test 提供了一個基於 ApacheDSUnboundID 的嵌入式 LDAP 伺服器。

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 伺服器互動的方法。

© . This site is unofficial and not affiliated with VMware.