5

単体テストでのSpring組み込みLDAPサーバーも同様ですが、私に合った答えはありませんでした。

springとspring-securityの組み込みLDAPサーバーを使用して統合テストを問題なく実行できます。ただし、組み込みLDAPサーバーをクリアし、ldifを再度ロードして、共通のテスト環境を提供する方法をまだ見つけていません。

spring-ldapのLdapTestUtilsは、cleanAndSetup()メソッドを提供します。ただし、これはapache-dsの推奨バージョン(1.5.5)では機能しません。これは、 LdifFileLoaderがLdapTestUtilsによって提供されるDirContextの代わりにCoreSessionを必要とするためです。これにより、

java.lang.NoSuchMethodError:
org.apache.directory.server.protocol.shared.store.LdifFileLoader.<init>(Ljavax/naming/directory/DirContext;Ljava/lang/String;)

埋め込まれたLDAPサーバーをクリアし、(起動時に実行されるように)ldifファイルを再度入力するメソッドのみが必要です。誰かがこれについて考えを持っていますか?

バージョン:spring 3.1、spring-ldap 1.3、spring-security 3.1、apache-ds 1.5.5

解決策(Luke Taylorに感謝):

@Inject
private ApplicationContext applicationContext;

@Before
public void reloadLdapDirectory() throws NamingException, IOException{
    ApacheDSContainer apacheDSContainer = (ApacheDSContainer) applicationContext.getBean(BeanIds.EMBEDDED_APACHE_DS);
    LdapTestUtils.clearSubContexts(contextSource, DistinguishedName.EMPTY_PATH);

    ClassPathResource classPathResource = new ClassPathResource("ldap.ldif");

    File tempFile = File.createTempFile("spring_ldap_test", ".ldif");
    try {
        InputStream inputStream = classPathResource.getInputStream();
        IOUtils.copy(inputStream, new FileOutputStream(tempFile));
        LdifFileLoader fileLoader = new LdifFileLoader(apacheDSContainer.getService().getAdminSession(), tempFile.getAbsolutePath());
        fileLoader.execute();
    }
    finally {
        try {
            tempFile.delete();
        }
        catch (Exception e) {
            // Ignore this
        }
    }
}
4

1 に答える 1

4

Spring SecurityのLDAP統合テストを見て、それらをガイドとして使用してみませんか?

現時点では、これらはLDAPテンプレートを使用して、必要に応じて(速度のために)各テストが作成したデータをクリアしますが、LDIFファイルをリロードするコメントアウトされたJunit@Afterメソッドもあります。は、サーバーインスタンス(a )CoreSessionを呼び出すことによって取得されます。getAdminSession()DefaultDirectoryService

XMLアプリケーションコンテキストを使用して、要素を使用してテストを実際に実行する必要がある場合は<ldap-server />、次を使用できます。

getBeanByName(BeanIds.EMBEDDED_APACHE_DS).getService()

インスタンスにアクセスしDefaultDirectoryServiceます。

于 2012-11-16T18:16:26.273 に答える