单元测试中的Spring嵌入式ldap服务器与之类似,但是没有给出适合我的答案。
我可以使用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;)
Run Code Online (Sandbox Code Playgroud)
我只想要一种清除嵌入式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
}
}
}
Run Code Online (Sandbox Code Playgroud)