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/19 19:31:33 UTC

[sling-org-apache-sling-jcr-resource] branch feature/SLING-10642-update-unittests2 created (now e1ae711)

This is an automated email from the ASF dual-hosted git repository.

joerghoh pushed a change to branch feature/SLING-10642-update-unittests2
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-resource.git.


      at e1ae711  Fix unit tests

This branch includes the following new commits:

     new e1ae711  Fix unit tests

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[sling-org-apache-sling-jcr-resource] 01/01: Fix unit tests

Posted by jo...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

joerghoh pushed a commit to branch feature/SLING-10642-update-unittests2
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-resource.git

commit e1ae711013aa4584145cbe67c77d1f2b1b1a3e45
Author: Jörg Hoh <jo...@joerghoh.de>
AuthorDate: Sat Jul 17 17:05:03 2021 +0200

    Fix unit tests
    
    Here we need a direct Sling Repository, while everywhere else we should
    rely on the modern Sling Testing libraries. So I did not update the
    commons-testing libraries, but moved it locally in here.
---
 pom.xml                                            |  6 ++
 .../internal/JcrModifiableValueMapTest.java        |  4 +-
 .../JcrResourceListenerScalabilityTest.java        |  6 +-
 .../resource/internal/JcrResourceListenerTest.java | 95 +++++++++++-----------
 .../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 ++++++++++++++++
 10 files changed, 190 insertions(+), 62 deletions(-)

diff --git a/pom.xml b/pom.xml
index aa47b79..3ddf179 100644
--- a/pom.xml
+++ b/pom.xml
@@ -260,6 +260,12 @@
             <version>1.4</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.testing.sling-mock-oak</artifactId>
+            <version>2.1.10-1.16.0</version>
+            <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/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();
+    }
+
+    
+
+
+}