You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by jo...@apache.org on 2021/07/20 10:28:41 UTC
[sling-org-apache-sling-jcr-resource] branch master updated:
SLING-10642 update unittests (#15)
This is an automated email from the ASF dual-hosted git repository.
joerghoh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-resource.git
The following commit(s) were added to refs/heads/master by this push:
new f959a93 SLING-10642 update unittests (#15)
f959a93 is described below
commit f959a933558572f8ab15493934c4ba151e87b3aa
Author: Jörg Hoh <jo...@users.noreply.github.com>
AuthorDate: Tue Jul 20 12:28:33 2021 +0200
SLING-10642 update unittests (#15)
Remove the reliance on commons-testing.
Co-authored-by: Konrad Windszus <kw...@apache.org>
---
pom.xml | 29 +++++--
.../internal/JcrModifiableValueMapTest.java | 4 +-
.../JcrResourceListenerScalabilityTest.java | 6 +-
.../resource/internal/JcrResourceListenerTest.java | 95 +++++++++++-----------
.../helper/JcrNodeResourceIteratorTest.java | 26 ++++--
.../helper/jcr/JcrItemResourceFactoryTest.java | 3 +-
.../helper/jcr/JcrItemResourceTestBase.java | 3 +-
.../JcrResourceProviderSessionHandlingTest.java | 3 +-
.../helper/jcr/JcrResourceProviderTest.java | 3 +-
.../helper/jcr/SlingRepositoryProvider.java | 60 ++++++++++++++
.../helper/jcr/SlingRepositoryTestBase.java | 69 ++++++++++++++++
11 files changed, 224 insertions(+), 77 deletions(-)
diff --git a/pom.xml b/pom.xml
index aa47b79..75ecf91 100644
--- a/pom.xml
+++ b/pom.xml
@@ -226,18 +226,18 @@
<!-- Testing -->
<dependency>
- <groupId>org.apache.sling</groupId>
- <artifactId>org.apache.sling.commons.testing</artifactId>
- <version>2.0.24</version>
- <scope>test</scope>
- </dependency>
- <dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>hamcrest</artifactId>
+ <version>2.2</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.sling-mock</artifactId>
<version>2.6.2</version>
@@ -255,11 +255,28 @@
<scope>test</scope>
</dependency>
<dependency>
+ <groupId>org.jmock</groupId>
+ <artifactId>jmock-junit4</artifactId>
+ <version>2.12.0</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>junit-addons</groupId>
<artifactId>junit-addons</artifactId>
<version>1.4</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.testing.jcr-mock</artifactId>
+ <version>1.5.2</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-simple</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/src/test/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMapTest.java b/src/test/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMapTest.java
index a590ac1..ac9e476 100644
--- a/src/test/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMapTest.java
+++ b/src/test/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMapTest.java
@@ -51,9 +51,9 @@ import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.resource.external.URIProvider;
import org.apache.sling.commons.classloader.DynamicClassLoaderManager;
-import org.apache.sling.commons.testing.jcr.RepositoryTestBase;
+import org.apache.sling.jcr.resource.internal.helper.jcr.SlingRepositoryTestBase;
-public class JcrModifiableValueMapTest extends RepositoryTestBase {
+public class JcrModifiableValueMapTest extends SlingRepositoryTestBase {
private String rootPath;
diff --git a/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerScalabilityTest.java b/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerScalabilityTest.java
index 1d2d341..9a896ed 100644
--- a/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerScalabilityTest.java
+++ b/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerScalabilityTest.java
@@ -35,8 +35,8 @@ import javax.jcr.observation.ObservationManager;
import org.apache.sling.api.resource.observation.ResourceChange;
import org.apache.sling.api.resource.observation.ResourceChange.ChangeType;
import org.apache.sling.api.resource.path.PathSet;
-import org.apache.sling.commons.testing.jcr.RepositoryUtil;
import org.apache.sling.jcr.api.SlingRepository;
+import org.apache.sling.jcr.resource.internal.helper.jcr.SlingRepositoryProvider;
import org.apache.sling.spi.resource.provider.ObservationReporter;
import org.apache.sling.spi.resource.provider.ObserverConfiguration;
import org.apache.sling.spi.resource.provider.ProviderContext;
@@ -59,7 +59,7 @@ public class JcrResourceListenerScalabilityTest {
@SuppressWarnings("deprecation")
@Before
- public void setUp() throws RepositoryException, InvalidSyntaxException {
+ public void setUp() throws Exception {
ObservationManager observationManager = mock(ObservationManager.class);
Workspace workspace = mock(Workspace.class);
@@ -73,7 +73,7 @@ public class JcrResourceListenerScalabilityTest {
final ProviderContext ctx = new SimpleProviderContext();
this.config = new JcrListenerBaseConfig(ctx.getObservationReporter(),
- RepositoryUtil.getRepository());
+ SlingRepositoryProvider.getRepository());
jcrResourceListener = new JcrResourceListener(this.config, ctx.getObservationReporter().getObserverConfigurations().get(0));
Event event = mock(MockEvent.class);
diff --git a/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerTest.java b/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerTest.java
index 6262d73..7bf62d9 100644
--- a/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerTest.java
+++ b/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerTest.java
@@ -40,8 +40,8 @@ import javax.jcr.Value;
import org.apache.sling.api.resource.observation.ResourceChange;
import org.apache.sling.api.resource.observation.ResourceChange.ChangeType;
import org.apache.sling.api.resource.path.PathSet;
-import org.apache.sling.commons.testing.jcr.RepositoryUtil;
import org.apache.sling.jcr.api.SlingRepository;
+import org.apache.sling.jcr.resource.internal.helper.jcr.SlingRepositoryProvider;
import org.apache.sling.spi.resource.provider.ObservationReporter;
import org.apache.sling.spi.resource.provider.ObserverConfiguration;
import org.junit.After;
@@ -67,85 +67,86 @@ public class JcrResourceListenerTest {
private final List<ResourceChange> events = synchronizedList(new ArrayList<ResourceChange>());
+ SlingRepository repository;
+
@SuppressWarnings("deprecation")
@Before
public void setUp() throws Exception {
- RepositoryUtil.startRepository();
- this.adminSession = RepositoryUtil.getRepository().loginAdministrative(null);
- RepositoryUtil.registerSlingNodeTypes(adminSession);
- final SlingRepository repo = RepositoryUtil.getRepository();
- this.config = new JcrListenerBaseConfig(getObservationReporter(),
+ repository = SlingRepositoryProvider.getRepository();
+ this.adminSession = repository.loginAdministrative(null);
+ ObservationReporter observationReporter = getObservationReporter();
+ this.config = new JcrListenerBaseConfig(observationReporter,
new SlingRepository() {
@Override
public Session login(Credentials credentials, String workspaceName)
throws LoginException, NoSuchWorkspaceException, RepositoryException {
- return repo.login(credentials, workspaceName);
+ return repository.login(credentials, workspaceName);
}
@Override
public Session login(String workspaceName) throws LoginException, NoSuchWorkspaceException, RepositoryException {
- return repo.login(workspaceName);
+ return repository.login(workspaceName);
}
@Override
public Session login(Credentials credentials) throws LoginException, RepositoryException {
- return repo.login(credentials);
+ return repository.login(credentials);
}
@Override
public Session login() throws LoginException, RepositoryException {
- return repo.login();
+ return repository.login();
}
@Override
public boolean isStandardDescriptor(String key) {
- return repo.isStandardDescriptor(key);
+ return repository.isStandardDescriptor(key);
}
@Override
public boolean isSingleValueDescriptor(String key) {
- return repo.isSingleValueDescriptor(key);
+ return repository.isSingleValueDescriptor(key);
}
@Override
public Value[] getDescriptorValues(String key) {
- return repo.getDescriptorValues(key);
+ return repository.getDescriptorValues(key);
}
@Override
public Value getDescriptorValue(String key) {
- return repo.getDescriptorValue(key);
+ return repository.getDescriptorValue(key);
}
@Override
public String[] getDescriptorKeys() {
- return repo.getDescriptorKeys();
+ return repository.getDescriptorKeys();
}
@Override
public String getDescriptor(String key) {
- return repo.getDescriptor(key);
+ return repository.getDescriptor(key);
}
@Override
public Session loginService(String subServiceName, String workspace) throws LoginException, RepositoryException {
- return repo.loginAdministrative(workspace);
+ return repository.loginAdministrative(workspace);
}
@Override
public Session loginAdministrative(String workspace) throws LoginException, RepositoryException {
- return repo.loginAdministrative(workspace);
+ return repository.loginAdministrative(workspace);
}
@Override
public String getDefaultWorkspace() {
// TODO Auto-generated method stub
- return repo.getDefaultWorkspace();
+ return repository.getDefaultWorkspace();
}
});
this.listener = new JcrResourceListener(this.config,
- getObservationReporter().getObserverConfigurations().get(0));
+ observationReporter.getObserverConfigurations().get(0));
}
@After
@@ -154,7 +155,6 @@ public class JcrResourceListenerTest {
adminSession.logout();
adminSession = null;
}
- RepositoryUtil.stopRepository();
if ( listener != null ) {
listener.close();
listener = null;
@@ -165,9 +165,9 @@ public class JcrResourceListenerTest {
}
}
- @Test public void testSimpleOperations() throws Exception {
- generateEvents();
-
+ @Test
+ public void testSimpleOperations() throws Exception {
+ generateEvents(adminSession);
assertEquals("Received: " + events, 5, events.size());
final Set<String> addPaths = new HashSet<String>();
final Set<String> modifyPaths = new HashSet<String>();
@@ -185,6 +185,7 @@ public class JcrResourceListenerTest {
}
assertNotNull(event.getUserId());
}
+
assertEquals(3, addPaths.size());
assertTrue("Added set should contain " + createdPath, addPaths.contains(createdPath));
assertTrue("Added set should contain " + pathToDelete, addPaths.contains(pathToDelete));
@@ -267,7 +268,8 @@ public class JcrResourceListenerTest {
node.remove();
session.save();
}
- assertEquals("Received: " + events, 6, events.size());
+ System.out.println("Events = " + events);
+ assertEquals("Received: " + events, 7, events.size());
final Set<String> addPaths = new HashSet<String>();
final Set<String> modifyPaths = new HashSet<String>();
final Set<String> removePaths = new HashSet<String>();
@@ -292,9 +294,12 @@ public class JcrResourceListenerTest {
assertTrue("Modified set should contain /libs/" + rootName, modifyPaths.contains("/libs/" + rootName));
assertTrue("Modified set should contain /apps/" + rootName, modifyPaths.contains("/apps/" + rootName));
- assertEquals("Received: " + removePaths, 2, removePaths.size());
+ // The OakEventFilter is using withIncludeAncestorsRemove, so we get also "removed"
+ // events for all ancestors of /apps and /libs;
+ assertEquals("Received: " + removePaths, 3, removePaths.size());
assertTrue("Removed set should contain /libs/" + rootName, removePaths.contains("/libs/" + rootName));
assertTrue("Removed set should contain /apps/" + rootName, removePaths.contains("/apps/" + rootName));
+ assertTrue("Removed set should contain /" + rootName, removePaths.contains("/" + rootName));
}
}
@@ -304,34 +309,26 @@ public class JcrResourceListenerTest {
return n;
}
- private void generateEvents() throws Exception {
- @SuppressWarnings("deprecation")
- final Session session = RepositoryUtil.getRepository().loginAdministrative(null);
-
- try {
- // create the nodes
- createNode(session, createdPath);
- createNode(session, pathToModify);
- createNode(session, pathToDelete);
+ private void generateEvents(Session session) throws Exception {
+ // create the nodes
+ createNode(session, createdPath);
+ createNode(session, pathToModify);
+ createNode(session, pathToDelete);
- Thread.sleep(1000);
+ Thread.sleep(1000);
- // modify
- final Node modified = session.getNode(pathToModify);
- modified.setProperty("foo", "bar");
+ // modify
+ final Node modified = session.getNode(pathToModify);
+ modified.setProperty("foo", "bar");
- session.save();
-
- // delete
- final Node deleted = session.getNode(pathToDelete);
- deleted.remove();
- session.save();
+ session.save();
- Thread.sleep(3500);
+ // delete
+ final Node deleted = session.getNode(pathToDelete);
+ deleted.remove();
+ session.save();
- } finally {
- session.logout();
- }
+ Thread.sleep(3500);
}
protected ObservationReporter getObservationReporter() {
diff --git a/src/test/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResourceIteratorTest.java b/src/test/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResourceIteratorTest.java
index 700d47a..1e93ed8 100644
--- a/src/test/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResourceIteratorTest.java
+++ b/src/test/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResourceIteratorTest.java
@@ -18,20 +18,24 @@
*/
package org.apache.sling.jcr.resource.internal.helper;
+import java.util.Arrays;
+import java.util.Collections;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicReference;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import org.apache.jackrabbit.commons.JcrUtils;
+import org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.external.URIProvider;
import org.apache.sling.commons.classloader.DynamicClassLoaderManager;
-import org.apache.sling.commons.testing.jcr.MockNode;
-import org.apache.sling.commons.testing.jcr.MockNodeIterator;
import org.apache.sling.jcr.resource.internal.HelperData;
import org.apache.sling.jcr.resource.internal.helper.jcr.JcrNodeResourceIterator;
+import org.apache.sling.testing.mock.jcr.MockJcr;
import junit.framework.TestCase;
@@ -42,7 +46,8 @@ public class JcrNodeResourceIteratorTest extends TestCase {
}
public void testEmpty() {
- NodeIterator ni = new MockNodeIterator(null);
+ NodeIterator ni = new NodeIteratorAdapter(Collections.emptyIterator());
+
JcrNodeResourceIterator ri = new JcrNodeResourceIterator(null, null, null, ni, getHelperData(), null);
assertFalse(ri.hasNext());
@@ -57,8 +62,9 @@ public class JcrNodeResourceIteratorTest extends TestCase {
public void testSingle() throws RepositoryException {
String path = "/parent/path/node";
- Node node = new MockNode(path);
- NodeIterator ni = new MockNodeIterator(new Node[] { node });
+ Session session = MockJcr.newSession();
+ Node node = JcrUtils.getOrCreateByPath(path, "nt:folder", session);
+ NodeIterator ni = new NodeIteratorAdapter(Collections.singleton(node));
JcrNodeResourceIterator ri = new JcrNodeResourceIterator(null, null, null, ni, getHelperData(), null);
assertTrue(ri.hasNext());
@@ -79,11 +85,12 @@ public class JcrNodeResourceIteratorTest extends TestCase {
public void testMulti() throws RepositoryException {
int numNodes = 10;
String pathBase = "/parent/path/node/";
+ Session session = MockJcr.newSession();
Node[] nodes = new Node[numNodes];
for (int i=0; i < nodes.length; i++) {
- nodes[i] = new MockNode(pathBase + i, "some:type" + i);
+ nodes[i] = JcrUtils.getOrCreateByPath(pathBase + i, "nt:folder", session);
}
- NodeIterator ni = new MockNodeIterator(nodes);
+ NodeIterator ni = new NodeIteratorAdapter(Arrays.asList(nodes));
JcrNodeResourceIterator ri = new JcrNodeResourceIterator(null, null, null, ni, getHelperData(), null);
for (int i=0; i < nodes.length; i++) {
@@ -105,8 +112,9 @@ public class JcrNodeResourceIteratorTest extends TestCase {
public void testRoot() throws RepositoryException {
String path = "/child";
- Node node = new MockNode(path);
- NodeIterator ni = new MockNodeIterator(new Node[] { node });
+ Session session = MockJcr.newSession();
+ Node node = session.getRootNode().addNode("child");
+ NodeIterator ni = new NodeIteratorAdapter(Collections.singleton(node));
JcrNodeResourceIterator ri = new JcrNodeResourceIterator(null, "/", null, ni, getHelperData(), null);
assertTrue(ri.hasNext());
diff --git a/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResourceFactoryTest.java b/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResourceFactoryTest.java
index 7467e3c..3d65ebd 100644
--- a/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResourceFactoryTest.java
+++ b/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResourceFactoryTest.java
@@ -31,10 +31,9 @@ import javax.jcr.Session;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.sling.api.resource.external.URIProvider;
import org.apache.sling.commons.classloader.DynamicClassLoaderManager;
-import org.apache.sling.commons.testing.jcr.RepositoryTestBase;
import org.apache.sling.jcr.resource.internal.HelperData;
-public class JcrItemResourceFactoryTest extends RepositoryTestBase {
+public class JcrItemResourceFactoryTest extends SlingRepositoryTestBase {
public static final String EXISTING_NODE_PATH = "/existing";
public static final String NON_EXISTING_NODE_PATH = "/nonexisting";
diff --git a/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResourceTestBase.java b/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResourceTestBase.java
index 1bf688d..ba57e11 100644
--- a/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResourceTestBase.java
+++ b/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResourceTestBase.java
@@ -25,12 +25,11 @@ import javax.jcr.NamespaceRegistry;
import javax.jcr.Node;
import org.apache.sling.api.SlingConstants;
-import org.apache.sling.commons.testing.jcr.RepositoryTestBase;
import org.apache.sling.jcr.resource.api.JcrResourceConstants;
import org.junit.Ignore;
@Ignore
-public class JcrItemResourceTestBase extends RepositoryTestBase {
+public class JcrItemResourceTestBase extends SlingRepositoryTestBase {
protected static final long TEST_MODIFIED = System.currentTimeMillis();
diff --git a/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProviderSessionHandlingTest.java b/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProviderSessionHandlingTest.java
index ba06259..5dbdf7c 100644
--- a/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProviderSessionHandlingTest.java
+++ b/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProviderSessionHandlingTest.java
@@ -45,7 +45,6 @@ import javax.jcr.Value;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
-import org.apache.sling.commons.testing.jcr.RepositoryProvider;
import org.apache.sling.jcr.api.SlingRepository;
import org.apache.sling.jcr.resource.api.JcrResourceConstants;
import org.apache.sling.spi.resource.provider.ResolveContext;
@@ -200,7 +199,7 @@ public class JcrResourceProviderSessionHandlingTest {
@Before
public void setUp() throws Exception {
- final SlingRepository repo = new SlingRepositoryWithDummyServiceUsers(RepositoryProvider.instance().getRepository());
+ final SlingRepository repo = new SlingRepositoryWithDummyServiceUsers(SlingRepositoryProvider.getRepository());
footInDoor = repo.loginAdministrative(null);
diff --git a/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProviderTest.java b/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProviderTest.java
index 32c6bfb..b7641bc 100644
--- a/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProviderTest.java
+++ b/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProviderTest.java
@@ -23,14 +23,13 @@ import java.security.Principal;
import javax.jcr.Repository;
import javax.jcr.Session;
-import org.apache.sling.commons.testing.jcr.RepositoryTestBase;
import org.apache.sling.spi.resource.provider.ResolveContext;
import org.junit.Assert;
import org.mockito.Mockito;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
-public class JcrResourceProviderTest extends RepositoryTestBase {
+public class JcrResourceProviderTest extends SlingRepositoryTestBase {
JcrResourceProvider jcrResourceProvider;
Session session;
diff --git a/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/SlingRepositoryProvider.java b/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/SlingRepositoryProvider.java
new file mode 100644
index 0000000..4dee137
--- /dev/null
+++ b/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/SlingRepositoryProvider.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.jcr.resource.internal.helper.jcr;
+
+import java.lang.reflect.Method;
+
+import org.apache.sling.jcr.api.SlingRepository;
+import org.apache.sling.testing.mock.sling.oak.OakMockSlingRepository;
+import org.mockito.Mockito;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.component.ComponentContext;
+
+public class SlingRepositoryProvider {
+
+ private static OakMockSlingRepository INSTANCE;
+
+ private SlingRepositoryProvider() {
+ }
+
+ public synchronized static SlingRepository getRepository() throws Exception {
+ if(INSTANCE == null) {
+ OakMockSlingRepository r = new OakMockSlingRepository();
+ Method activateMethod = OakMockSlingRepository.class.getDeclaredMethod("activate", BundleContext.class);
+ activateMethod.setAccessible(true);
+ activateMethod.invoke(r, getFakeContext());
+ INSTANCE = r;
+ }
+ return INSTANCE;
+ }
+
+
+
+ public static void shutdown() throws Exception {
+ Method deactivateMethod = OakMockSlingRepository.class.getDeclaredMethod("deactivate",ComponentContext.class);
+ deactivateMethod.setAccessible(true);
+ deactivateMethod.invoke(getRepository(),(ComponentContext) null);
+ }
+
+
+ private static BundleContext getFakeContext() {
+ BundleContext mockContext = Mockito.mock(BundleContext.class);
+ return mockContext;
+ }
+}
diff --git a/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/SlingRepositoryTestBase.java b/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/SlingRepositoryTestBase.java
new file mode 100644
index 0000000..babdc20
--- /dev/null
+++ b/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/SlingRepositoryTestBase.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.jcr.resource.internal.helper.jcr;
+
+import javax.jcr.Node;
+import javax.jcr.Session;
+
+import org.apache.sling.jcr.api.SlingRepository;
+
+import junit.framework.TestCase;
+
+public class SlingRepositoryTestBase extends TestCase {
+
+ protected Node testRoot;
+ protected Session session;
+ private int counter;
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ if(session != null) {
+ session.logout();
+ }
+ }
+
+ /** Return a JCR Session, initialized on demand */
+ protected Session getSession() throws Exception {
+ if(session == null) {
+ session = getRepository().loginAdministrative(null);
+ }
+ return session;
+ }
+
+ /** Return a test root node, created on demand, with a unique path */
+ protected Node getTestRootNode() throws Exception {
+ if(testRoot==null) {
+ final Node root = getSession().getRootNode();
+ final Node classRoot = root.addNode(getClass().getSimpleName());
+ testRoot = classRoot.addNode(System.currentTimeMillis() + "_" + (++counter));
+ }
+ return testRoot;
+ }
+
+ /** Return a Repository
+ * @throws Exception */
+ protected SlingRepository getRepository() throws Exception {
+ return SlingRepositoryProvider.getRepository();
+ }
+
+
+
+
+}