単体テストでの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
}
}
}