4

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.

window
  • 67
  • 5

4 Answers4

4

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 ( e.g, 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.

Amar
  • 274
  • 3
  • 12
2

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.

Bertrand Delacretaz
  • 6,100
  • 19
  • 24
0

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.

Randall Hauch
  • 7,069
  • 31
  • 28
0

Use a TransientRepository with a MemoryFileSystem and an In-Memory PM or a database persistence manager which can hanlde in-memory databases like H2.

palacsint
  • 28,416
  • 10
  • 82
  • 109