Spring嵌入式ldap服务器进行单元测试

时间:2022-10-15 22:14:08

I am currently trying to use an embedded ldap server for unit tests.

我目前正在尝试使用嵌入式ldap服务器进行单元测试。

In Spring Security, you can quickly define an embedded ldap server for testing with the tag with some sample data loaded from the specified ldif.

在Spring Security中,您可以快速定义嵌入式ldap服务器,以便使用从指定的ldif加载的一些示例数据对标记进行测试。

I will be using Spring Ldap to perform ldap operations, and thinking of testing the usual CRUD features of my User service object.

我将使用Spring Ldap执行ldap操作,并考虑测试用户服务对象的常用CRUD功能。

Is there, however, a way to ensure that the entries in the embedded server to be in the same consistent state (sort of like a delete all and reload the ldif entries) for each test I am running?

但是,是否有一种方法可以确保嵌入式服务器中的条目与我正在运行的每个测试处于相同的一致状态(有点像删除所有并重新加载ldif条目)?

I thought of the following: 1) Indicate that the method dirties the context, and force a recreation of the embedded ldap server, which sounds painful as it would have to restart the server for every method 2) Create the test entries in a test organization, such that I can unbind them and simply load in the ldif file again there.

我想到了以下几点:1)表明该方法污染了上下文,并强制重新创建嵌入式ldap服务器,这听起来很痛苦,因为它必须为每个方法重新启动服务器2)在测试组织中创建测试条目,这样我就可以解除绑定,然后再在那里再加载ldif文件。

I prefer 2, but it seems like the Spring LDAP has no good helpers to load and send across the content of a ldif file.

我更喜欢2,但似乎Spring LDAP没有好的帮助器来加载和发送ldif文件的内容。

Any suggestions on how you perform ldap testing with an embedded ldap server of spring, or of the two possible solutions I mention?

有关如何使用spring的嵌入式ldap服务器或我提到的两种可能解决方案执行ldap测试的任何建议?

Thanks

4 个解决方案

#1


I may be off-track here, but if you're not testing the LDAP integration itself, you could Mock out the LDAP connection with a Mock object that always returns the values you expect so that your other Unit Tests can complete.

我可能偏离此处,但如果您没有测试LDAP集成本身,您可以使用Mock对象模拟LDAP连接,该对象始终返回您期望的值,以便您的其他单元测试可以完成。

If you're testing the LDAP connection then you're really doing an integration test. In which case it's probably best to connect to a real LDAP implementation.

如果您正在测试LDAP连接,那么您实际上正在进行集成测试。在这种情况下,最好连接到真正的LDAP实现。

#2


Doesn't Spring LDAP provide transactional control around LDAP operations? If so, why not use Spring test framework with its auto-rollback capability?

Spring LDAP不提供围绕LDAP操作的事务控制吗?如果是这样,为什么不使用Spring测试框架及其自动回滚功能?

I also know of a JDBC-LDAP bridge driver that wraps an LDAP repository, presenting it as a relational database. I've used iBatis to connect to this (I've written this up at http://lokibear.blogspot.com, see articles from July). I've not yet tried applying transactional control, but the website for the driver mentions the ability to ignore transactions (which means you can also not ignore them...right?).

我还知道一个JDBC-LDAP桥驱动程序,它包装了一个LDAP存储库,并将其作为关系数据库呈现。我已经使用iBatis来连接到这个(我在http://lokibear.blogspot.com上写了这篇文章,参见7月的文章)。我还没有尝试应用事务控制,但驱动程序的网站提到了忽略事务的能力(这意味着你也不能忽略它们......对吧?)。

Like I say, I haven't tried this yet; but, if this provides transactions around LDAP, then you can again use the Spring test framework to get auto-rollback in place. I've put out a quick cheatsheet around that framework - see the September posts at my blog.

就像我说的,我还没有尝试过这个;但是,如果这提供了围绕LDAP的事务,那么您可以再次使用Spring测试框架来实现自动回滚。我已经围绕该框架发布了一个快速的备忘单 - 请参阅我博客上的9月帖子。

Sorry, I might be missing your goal here; but perhaps these suggestions are useful. Good luck!

对不起,我可能会错过你的目标;但也许这些建议很有用。祝好运!

#3


You may or may not know that the embedded LDAP functionality is not provided by Spring LDAP itself, but Apache Directory Server. Unfortunately, the LDIF loader in Apache DS (as wired by Spring, anyway) has very poor error handling and reporting capability, and as such is probably not going to behave as you really want for a unit test. Your best bet if you really want to start from a clean slate each time is to take the lead of the Spring Security LDAP unit tests and reinitialize Apache DS every time, with a clean LDIF file load.

您可能知道也可能不知道Spring LDAP本身不提供嵌入式LDAP功能,而是Apache Directory Server。不幸的是,Apache DS中的LDIF加载器(无论如何都由Spring连接)具有非常差的错误处理和报告功能,因此可能不会像您真正想要的单元测试一样。如果你真的想从每次干净的平板开始,最好的选择是带领Spring Security LDAP单元测试并每次重新初始化Apache DS,并加载干净的LDIF文件。

Alternatively, you could eschew LDIF altogether and construct your own unit test wrapper that verifies the pre- and post-conditions of the data prior to your unit tests running. This would be more work, but ultimately may work out better for you.

或者,您可以完全避开LDIF并构建自己的单元测试包装器,以在单元测试运行之前验证数据的前后条件。这将是更多的工作,但最终可能会更好地为您服务。

#4


Works fine for me:

适合我的工作:

@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
        }
    }
}

I asked something similar and got an answer from Luke Taylor: Integration tests with spring-security and ldap

我问过类似的东西,得到了Luke Taylor的回答:使用spring-security和ldap进行集成测试

#1


I may be off-track here, but if you're not testing the LDAP integration itself, you could Mock out the LDAP connection with a Mock object that always returns the values you expect so that your other Unit Tests can complete.

我可能偏离此处,但如果您没有测试LDAP集成本身,您可以使用Mock对象模拟LDAP连接,该对象始终返回您期望的值,以便您的其他单元测试可以完成。

If you're testing the LDAP connection then you're really doing an integration test. In which case it's probably best to connect to a real LDAP implementation.

如果您正在测试LDAP连接,那么您实际上正在进行集成测试。在这种情况下,最好连接到真正的LDAP实现。

#2


Doesn't Spring LDAP provide transactional control around LDAP operations? If so, why not use Spring test framework with its auto-rollback capability?

Spring LDAP不提供围绕LDAP操作的事务控制吗?如果是这样,为什么不使用Spring测试框架及其自动回滚功能?

I also know of a JDBC-LDAP bridge driver that wraps an LDAP repository, presenting it as a relational database. I've used iBatis to connect to this (I've written this up at http://lokibear.blogspot.com, see articles from July). I've not yet tried applying transactional control, but the website for the driver mentions the ability to ignore transactions (which means you can also not ignore them...right?).

我还知道一个JDBC-LDAP桥驱动程序,它包装了一个LDAP存储库,并将其作为关系数据库呈现。我已经使用iBatis来连接到这个(我在http://lokibear.blogspot.com上写了这篇文章,参见7月的文章)。我还没有尝试应用事务控制,但驱动程序的网站提到了忽略事务的能力(这意味着你也不能忽略它们......对吧?)。

Like I say, I haven't tried this yet; but, if this provides transactions around LDAP, then you can again use the Spring test framework to get auto-rollback in place. I've put out a quick cheatsheet around that framework - see the September posts at my blog.

就像我说的,我还没有尝试过这个;但是,如果这提供了围绕LDAP的事务,那么您可以再次使用Spring测试框架来实现自动回滚。我已经围绕该框架发布了一个快速的备忘单 - 请参阅我博客上的9月帖子。

Sorry, I might be missing your goal here; but perhaps these suggestions are useful. Good luck!

对不起,我可能会错过你的目标;但也许这些建议很有用。祝好运!

#3


You may or may not know that the embedded LDAP functionality is not provided by Spring LDAP itself, but Apache Directory Server. Unfortunately, the LDIF loader in Apache DS (as wired by Spring, anyway) has very poor error handling and reporting capability, and as such is probably not going to behave as you really want for a unit test. Your best bet if you really want to start from a clean slate each time is to take the lead of the Spring Security LDAP unit tests and reinitialize Apache DS every time, with a clean LDIF file load.

您可能知道也可能不知道Spring LDAP本身不提供嵌入式LDAP功能,而是Apache Directory Server。不幸的是,Apache DS中的LDIF加载器(无论如何都由Spring连接)具有非常差的错误处理和报告功能,因此可能不会像您真正想要的单元测试一样。如果你真的想从每次干净的平板开始,最好的选择是带领Spring Security LDAP单元测试并每次重新初始化Apache DS,并加载干净的LDIF文件。

Alternatively, you could eschew LDIF altogether and construct your own unit test wrapper that verifies the pre- and post-conditions of the data prior to your unit tests running. This would be more work, but ultimately may work out better for you.

或者,您可以完全避开LDIF并构建自己的单元测试包装器,以在单元测试运行之前验证数据的前后条件。这将是更多的工作,但最终可能会更好地为您服务。

#4


Works fine for me:

适合我的工作:

@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
        }
    }
}

I asked something similar and got an answer from Luke Taylor: Integration tests with spring-security and ldap

我问过类似的东西,得到了Luke Taylor的回答:使用spring-security和ldap进行集成测试