简体   繁体   中英

JCR Repository available for Junit tests

I have a code accessing to content repositories through JCR API. My code is being developed in OSGI environment. What I want is to have a JCR content repository which should be available while running the unit tests regardless from the environment. I mean it should be available for any user who builds the project. I don't want to depend on some external content repository running in a different virtual machine.

Is it possible to do such a thing? Thanks in advance.

You may also look at the transient repository provided by the Jackrabbit implementation. (assuming you are using Jackrabbit implementation) A Transient Repository is

a repository proxy that automatically initializes and shuts down the underlying repository instance when the first session is opened or the last one closed. - API doc

Please look at the examples here - it is really easy to create an instance of it. Since it is a local repository, you will never need to go over the network.

I normally create a transient repository and a session once per JUnit test class in setUpClass/BeforeClass hook and reuse it across all the test cases, cleaning up the session ( eg, removing the nodes I added during a test case, etc.) after each test case.

One drawback, however, using transient repository, is that it will create several repository specific files/directory in your base directory. If you are using maven, then you can use maven-clean-plugin to clean up the unwanted files as a solution.

Apache Sling's RepositoryTestBase class provides a repository that can be used in "unit" testing. It does have some Sling dependencies, so you might not be able to use it as is but it's probably a good starting point.

ModeShape can easily be used within unit tests, and for unit tests our In-Memory connector and Disk connector work great and are very fast (and the in-memory doesn't require cleanup after each test). Our JcrEngineTest shows how easy this is, with each method configuring and starting an engine instance within a few lines. Many of these test complete in under 100ms .

TransientRepositoryMemoryFileSystem内存中PM或数据库持久性管理器一起使用,它可以处理像H2这样的内存数据库。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM