单元测试中的Spring嵌入式ldap服务器是相似的,但是没有人给我答案。
我可以使用spring和spring安全的嵌入式ldap服务器运行我的集成测试,没有任何问题。但是,我还没有找到一种方法来清除嵌入式ldap服务器并再次加载ldif来提供一个通用的测试环境。
LdapTestUtils of spring提供了一个cleanAndSetup()方法。但是,这并不适用于建议的apache版本(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 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
}
}
}发布于 2012-11-16 18:16:26
为什么不看一下Security的LDAP集成测试并使用它们作为指南呢?
目前,这些只需要清除每个测试创建的数据(为了速度),但是也有一个注释掉的使用LDAP模板,它可以重新加载LDIF文件。CoreSession是通过在服务器实例( DefaultDirectoryService)上调用getAdminSession()来获得的。
如果您真的必须使用XML应用程序上下文使用<ldap-server />元素运行测试,则可以使用:
getBeanByName(BeanIds.EMBEDDED_APACHE_DS).getService()以获得对DefaultDirectoryService实例的访问。
https://stackoverflow.com/questions/13396708
复制相似问题