【发布时间】:2012-11-15 11:35:11
【问题描述】:
Spring embedded ldap server in unit tests 类似,但没有给出适合我的答案。
我可以毫无问题地使用 spring 和 spring-security 的嵌入式 ldap 服务器运行我的集成测试。 但是,我还没有找到清除嵌入式 ldap 服务器并重新加载 ldif 以提供通用测试环境的方法。
spring-ldap 的 LdapTestUtils 提供了一个 cleanAndSetup() 方法。但是,这不适用于建议的 apache-ds 版本 (1.5.5),因为 LdifFileLoader 现在需要 CoreSession 而不是 LdapTestUtils 提供的 DirContext。这会导致
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
}
}
}
【问题讨论】:
-
这能回答你的问题吗? Spring embedded ldap server in unit tests
标签: spring-security integration-testing spring-ldap