Spring在单元测试中嵌入了ldap服务器类似,但没有给出适合我的答案。
我可以使用 spring 和 spring-security 的嵌入式 ldap 服务器运行集成测试,没有任何问题。
但是,我还没有找到一种方法来清除嵌入式 ldap 服务器并再次加载 ldif 以提供通用的测试环境。
spring-ldap 的 LdapTestUtils 提供了 cleanAndSetup() 方法。但是,这不适用于建议的 apache-ds 版本 (1.5.5),因为 LdifFileLoader 现在需要核心会话而不是目录上下文由 LdapTestUtils 提供。这会导致
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
解决方案(感谢卢克·泰勒):
@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
}
}
}