首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与spring安全性和ldap的集成测试

与spring安全性和ldap的集成测试
EN

Stack Overflow用户
提问于 2012-11-15 11:35:11
回答 1查看 9.5K关注 0票数 5

单元测试中的Spring嵌入式ldap服务器是相似的,但是没有人给我答案。

我可以使用spring和spring安全的嵌入式ldap服务器运行我的集成测试,没有任何问题。但是,我还没有找到一种方法来清除嵌入式ldap服务器并再次加载ldif来提供一个通用的测试环境。

LdapTestUtils of spring提供了一个cleanAndSetup()方法。但是,这并不适用于建议的apache版本(1.5.5),因为LdifFileLoader现在需要一个CoreSession,而不是LdapTestUtils提供的DirContext。这将导致

代码语言:javascript
复制
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

解决方案(感谢卢克·泰勒):

代码语言:javascript
复制
@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
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-16 18:16:26

为什么不看一下Security的LDAP集成测试并使用它们作为指南呢?

目前,这些只需要清除每个测试创建的数据(为了速度),但是也有一个注释掉的使用LDAP模板,它可以重新加载LDIF文件。CoreSession是通过在服务器实例( DefaultDirectoryService)上调用getAdminSession()来获得的。

如果您真的必须使用XML应用程序上下文使用<ldap-server />元素运行测试,则可以使用:

代码语言:javascript
复制
getBeanByName(BeanIds.EMBEDDED_APACHE_DS).getService()

以获得对DefaultDirectoryService实例的访问。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13396708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档