sta*_*max 5 integration-testing spring-security spring-ldap
单元测试中的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)
为什么不看看 Spring Security 的LDAP 集成测试并使用它们作为指南?
目前,这些仅使用 LDAP 模板来清除每个测试在必要时创建的数据(为了速度),但还有一个注释掉的Junit@After方法可以重新加载 LDIF 文件。这CoreSession是通过调用getAdminSession()服务器实例 (a DefaultDirectoryService) 获得的。
如果您确实必须使用 XML 应用程序上下文、使用该元素来运行测试<ldap-server />,您可以使用:
getBeanByName(BeanIds.EMBEDDED_APACHE_DS).getService()
Run Code Online (Sandbox Code Playgroud)
来访问该DefaultDirectoryService实例。
| 归档时间: |
|
| 查看次数: |
7485 次 |
| 最近记录: |