incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1524756 - in /sling/whiteboard/bdelacretaz/it-repository/src/test/java/org/apache/sling/jcr/repository/it: JackrabbitRepositoryIT.java ResourceEventListener.java SlingRepositoryITBase.java
Date Thu, 19 Sep 2013 14:39:56 GMT
Author: bdelacretaz
Date: Thu Sep 19 14:39:56 2013
New Revision: 1524756

URL: http://svn.apache.org/r1524756
Log:
SLING-2788 - testOsgiResourceEvents added

Added:
    sling/whiteboard/bdelacretaz/it-repository/src/test/java/org/apache/sling/jcr/repository/it/ResourceEventListener.java
Modified:
    sling/whiteboard/bdelacretaz/it-repository/src/test/java/org/apache/sling/jcr/repository/it/JackrabbitRepositoryIT.java
    sling/whiteboard/bdelacretaz/it-repository/src/test/java/org/apache/sling/jcr/repository/it/SlingRepositoryITBase.java

Modified: sling/whiteboard/bdelacretaz/it-repository/src/test/java/org/apache/sling/jcr/repository/it/JackrabbitRepositoryIT.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/it-repository/src/test/java/org/apache/sling/jcr/repository/it/JackrabbitRepositoryIT.java?rev=1524756&r1=1524755&r2=1524756&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/it-repository/src/test/java/org/apache/sling/jcr/repository/it/JackrabbitRepositoryIT.java
(original)
+++ sling/whiteboard/bdelacretaz/it-repository/src/test/java/org/apache/sling/jcr/repository/it/JackrabbitRepositoryIT.java
Thu Sep 19 14:39:56 2013
@@ -90,6 +90,7 @@ public class JackrabbitRepositoryIT exte
                 mavenBundle("org.apache.sling", "org.apache.sling.jcr.classloader", "3.1.12"),
                 mavenBundle("org.apache.sling", "org.apache.sling.jcr.contentloader", "2.1.2"),
                 mavenBundle("org.apache.sling", "org.apache.sling.engine", "2.2.6"),
+                mavenBundle("org.apache.sling", "org.apache.sling.event", "3.2.0"),
 
                 mavenBundle("org.apache.sling", "org.apache.sling.jcr.jcr-wrapper", "2.0.0"),
                 mavenBundle("org.apache.sling", "org.apache.sling.jcr.api", "2.1.0"),

Added: sling/whiteboard/bdelacretaz/it-repository/src/test/java/org/apache/sling/jcr/repository/it/ResourceEventListener.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/it-repository/src/test/java/org/apache/sling/jcr/repository/it/ResourceEventListener.java?rev=1524756&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/it-repository/src/test/java/org/apache/sling/jcr/repository/it/ResourceEventListener.java
(added)
+++ sling/whiteboard/bdelacretaz/it-repository/src/test/java/org/apache/sling/jcr/repository/it/ResourceEventListener.java
Thu Sep 19 14:39:56 2013
@@ -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 SF 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.repository.it;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Set;
+
+import org.apache.sling.api.SlingConstants;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventConstants;
+import org.osgi.service.event.EventHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ResourceEventListener implements EventHandler {
+    
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final Set<String> paths = new HashSet<String>();
+    
+    ServiceRegistration register(BundleContext ctx) {
+        final Hashtable<String, Object> props = new Hashtable<String, Object>();
+        props.put(EventConstants.EVENT_TOPIC, SlingConstants.TOPIC_RESOURCE_ADDED);
+        return ctx.registerService(EventHandler.class.getName(), this, props);
+    }
+    
+    @Override
+    public void handleEvent(Event event) {
+        final String path = (String) event.getProperty("path");
+        if(path != null) {
+            if(paths.isEmpty()) {
+                log.info("Got first event, path={}", path);
+            }
+            synchronized (paths) {
+                paths.add(path);
+            }
+        }
+    }
+    
+    void clear() {
+        synchronized (paths) {
+            paths.clear();
+        }
+    }
+    
+    Set<String> getPaths() {
+        synchronized (paths) {
+            return Collections.unmodifiableSet(paths);
+        }
+    }
+}
\ No newline at end of file

Modified: sling/whiteboard/bdelacretaz/it-repository/src/test/java/org/apache/sling/jcr/repository/it/SlingRepositoryITBase.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/it-repository/src/test/java/org/apache/sling/jcr/repository/it/SlingRepositoryITBase.java?rev=1524756&r1=1524755&r2=1524756&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/it-repository/src/test/java/org/apache/sling/jcr/repository/it/SlingRepositoryITBase.java
(original)
+++ sling/whiteboard/bdelacretaz/it-repository/src/test/java/org/apache/sling/jcr/repository/it/SlingRepositoryITBase.java
Thu Sep 19 14:39:56 2013
@@ -22,8 +22,10 @@ import static org.junit.Assert.assertNot
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Set;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.inject.Inject;
@@ -41,6 +43,10 @@ import org.apache.sling.jcr.api.SlingRep
 import org.junit.After;
 import org.junit.Ignore;
 import org.junit.Test;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /** Base class for SlingRepository tests, contains tests
  *  that apply to all implementations.
@@ -51,11 +57,15 @@ import org.junit.Test;
  */
 public abstract class SlingRepositoryITBase {
 
+    private final Logger log = LoggerFactory.getLogger(getClass());
     private final AtomicInteger counter = new AtomicInteger();
     
     @Inject
     protected SlingRepository repository;
     
+    @Inject
+    protected BundleContext bundleContext;
+    
     /** Check some repository descriptors to make sure we're
      *  testing the expected implementation. */ 
     protected abstract void doCheckRepositoryDescriptors();
@@ -256,7 +266,7 @@ public abstract class SlingRepositoryITB
     
     @Test
     public void testMultiValueInputStream() throws RepositoryException {
-        Session s = repository.loginAdministrative(null);
+        final Session s = repository.loginAdministrative(null);
         try {
             final String path = getClass().getSimpleName() + System.currentTimeMillis();
             final Node child = deleteAfterTests(s.getRootNode().addNode(path));
@@ -270,6 +280,49 @@ public abstract class SlingRepositoryITB
         } finally {
             s.logout();
         }
-       
+    }
+    
+    @Test
+    public void testOsgiResourceEvents() throws RepositoryException {
+        final ResourceEventListener listener = new ResourceEventListener();
+        final ServiceRegistration reg = listener.register(bundleContext);
+        final Session s = repository.loginAdministrative(null);
+        final int nPaths = 500;
+        final int timeoutMsec = 5000;
+        final String prefix = "testOsgiResourceEvents_" + counter.incrementAndGet() + "_";
+        
+        try {
+            for(int i=0; i  < nPaths; i++) {
+                s.getRootNode().addNode(prefix + i);
+            }
+            s.save();
+
+            log.info("Added {} nodes, checking what ResourceEventListener got...", nPaths);
+            final long timeout = System.currentTimeMillis() + timeoutMsec;
+            final Set<String> missing = new HashSet<String>(); 
+            while(System.currentTimeMillis() < timeout) {
+                missing.clear();
+                final Set<String> paths = listener.getPaths();
+                for(int i=0; i  < nPaths; i++) {
+                    final String path = "/" + prefix + i;
+                    if(!paths.contains(path)) {
+                        missing.add(path);
+                    }
+                }
+                
+                if(missing.isEmpty()) {
+                    break;
+                }
+            }
+            
+            if(!missing.isEmpty()) {
+                fail("OSGi add resource events are missing for " 
+                        + missing.size() + "/" + nPaths + " paths after " 
+                        + timeoutMsec + " msec: " + missing);
+            }
+        } finally {
+            reg.unregister();
+            s.logout();
+        }
     }
 }
\ No newline at end of file



Mime
View raw message