You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2015/02/18 23:23:31 UTC

svn commit: r1660748 - in /sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers: AbstractFailOnUnexpectedEventsRule.java FailOnModificationEventsRule.java

Author: rombert
Date: Wed Feb 18 22:23:30 2015
New Revision: 1660748

URL: http://svn.apache.org/r1660748
Log:
SLING-3783 - Capture command-related Events during test execution

Extract a base AbstractFailOnUnexpectedEventsRule

Added:
    sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/AbstractFailOnUnexpectedEventsRule.java
      - copied, changed from r1660747, sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/FailOnModificationEventsRule.java
Modified:
    sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/FailOnModificationEventsRule.java

Copied: sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/AbstractFailOnUnexpectedEventsRule.java (from r1660747, sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/FailOnModificationEventsRule.java)
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/AbstractFailOnUnexpectedEventsRule.java?p2=sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/AbstractFailOnUnexpectedEventsRule.java&p1=sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/FailOnModificationEventsRule.java&r1=1660747&r2=1660748&rev=1660748&view=diff
==============================================================================
--- sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/FailOnModificationEventsRule.java (original)
+++ sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/AbstractFailOnUnexpectedEventsRule.java Wed Feb 18 22:23:30 2015
@@ -18,6 +18,7 @@ package org.apache.sling.ide.test.impl.h
 
 import static org.junit.Assert.fail;
 
+import java.util.Collections;
 import java.util.Dictionary;
 import java.util.Hashtable;
 import java.util.List;
@@ -25,7 +26,6 @@ import java.util.concurrent.CopyOnWriteA
 
 import org.apache.sling.ide.eclipse.core.internal.Activator;
 import org.apache.sling.ide.transport.CommandExecutionProperties;
-import org.apache.sling.ide.transport.Repository.CommandExecutionFlag;
 import org.junit.rules.TestRule;
 import org.junit.runner.Description;
 import org.junit.runners.model.Statement;
@@ -34,18 +34,14 @@ import org.osgi.service.event.Event;
 import org.osgi.service.event.EventHandler;
 
 /**
- * The <tt>FailOnModificationEventsRule</tt> registers an event listener and fails the test under execution if
- * modification events are fired, whether they are successful or not
- *
+ * The <tt>AbstractFailOnUnexpectedEventsRule</tt> implements the plumbing needed for rules to react on events
+ * 
  */
-public class FailOnModificationEventsRule implements EventHandler, TestRule {
+public abstract class AbstractFailOnUnexpectedEventsRule implements EventHandler, TestRule {
 
-    /**
-     * 
-     */
     private static final int SETTLE_TIMEOUT_MILLIS = 100;
     private ServiceRegistration<EventHandler> registration;
-    private List<Event> unexpectedEvents = new CopyOnWriteArrayList<Event>();
+    private final List<Event> unexpectedEvents = new CopyOnWriteArrayList<Event>();
 
     public Statement apply(Statement base, Description description) {
         return statement(base);
@@ -66,32 +62,32 @@ public class FailOnModificationEventsRul
     }
 
     protected void before() {
-
+    
         Dictionary<String, Object> props = new Hashtable<String, Object>();
         props.put("event.topics", "org/apache/sling/ide/transport");
         registration = Activator.getDefault().getBundle().getBundleContext()
                 .registerService(EventHandler.class, this, props);
-
+    
     }
 
     protected void after() throws InterruptedException {
-
+    
         if (registration != null) {
             registration.unregister();
         }
-
+    
         waitForEventsToSettle();
-
+    
         if (unexpectedEvents.isEmpty()) {
             return;
         }
-
+    
         StringBuilder desc = new StringBuilder();
-        desc.append("Unexpected events captured during import : ");
+        desc.append(getClass().getSimpleName() + " : " + unexpectedEvents.size() + " unexpected events captured:");
         for (Event event : unexpectedEvents) {
-
+    
             String flags = (String) event.getProperty(CommandExecutionProperties.ACTION_FLAGS);
-
+    
             desc.append('\n');
             desc.append(event.getProperty(CommandExecutionProperties.ACTION_TYPE));
             if (flags != null && flags.length() > 0) {
@@ -102,7 +98,7 @@ public class FailOnModificationEventsRul
             desc.append(" : ");
             desc.append(event.getProperty(CommandExecutionProperties.RESULT_TEXT));
         }
-
+    
         fail(desc.toString());
     }
 
@@ -120,14 +116,22 @@ public class FailOnModificationEventsRul
      * </p>
      */
     public void clearUnexpectedEventsAfterSettling() throws InterruptedException {
-
+    
         waitForEventsToSettle();
-
+    
         unexpectedEvents.clear();
     }
 
-    private void waitForEventsToSettle() throws InterruptedException {
+    protected void addUnexpectedEvent(Event event) {
+        unexpectedEvents.add(event);
+    }
+
+    protected List<Event> getUnexpectedEvents() {
+        return Collections.unmodifiableList(unexpectedEvents);
+    }
 
+    private void waitForEventsToSettle() throws InterruptedException {
+    
         int currentSize;
         do {
             currentSize = unexpectedEvents.size();
@@ -135,21 +139,4 @@ public class FailOnModificationEventsRul
         } while (currentSize != unexpectedEvents.size());
     }
 
-    @Override
-    public void handleEvent(Event event) {
-
-        String type = (String) event.getProperty(CommandExecutionProperties.ACTION_TYPE);
-
-        if ("AddOrUpdateNodeCommand".equals(type) || "ReorderChildNodesCommand".equals(type)
-                || "DeleteNodeCommand".equals(type)) {
-            String flags = (String) event.getProperty(CommandExecutionProperties.ACTION_FLAGS);
-
-            // it's OK to create prerequisites if needed
-            if (flags == null || !CommandExecutionFlag.CREATE_ONLY_WHEN_MISSING.toString().equals(flags)) {
-                unexpectedEvents.add(event);
-            }
-
-        }
-    }
-
 }
\ No newline at end of file

Modified: sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/FailOnModificationEventsRule.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/FailOnModificationEventsRule.java?rev=1660748&r1=1660747&r2=1660748&view=diff
==============================================================================
--- sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/FailOnModificationEventsRule.java (original)
+++ sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/FailOnModificationEventsRule.java Wed Feb 18 22:23:30 2015
@@ -16,124 +16,16 @@
  */
 package org.apache.sling.ide.test.impl.helpers;
 
-import static org.junit.Assert.fail;
-
-import java.util.Dictionary;
-import java.util.Hashtable;
-import java.util.List;
-import java.util.concurrent.CopyOnWriteArrayList;
-
-import org.apache.sling.ide.eclipse.core.internal.Activator;
 import org.apache.sling.ide.transport.CommandExecutionProperties;
 import org.apache.sling.ide.transport.Repository.CommandExecutionFlag;
-import org.junit.rules.TestRule;
-import org.junit.runner.Description;
-import org.junit.runners.model.Statement;
-import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.event.Event;
-import org.osgi.service.event.EventHandler;
 
 /**
  * The <tt>FailOnModificationEventsRule</tt> registers an event listener and fails the test under execution if
  * modification events are fired, whether they are successful or not
  *
  */
-public class FailOnModificationEventsRule implements EventHandler, TestRule {
-
-    /**
-     * 
-     */
-    private static final int SETTLE_TIMEOUT_MILLIS = 100;
-    private ServiceRegistration<EventHandler> registration;
-    private List<Event> unexpectedEvents = new CopyOnWriteArrayList<Event>();
-
-    public Statement apply(Statement base, Description description) {
-        return statement(base);
-    }
-
-    private Statement statement(final Statement base) {
-        return new Statement() {
-            @Override
-            public void evaluate() throws Throwable {
-                before();
-                try {
-                    base.evaluate();
-                } finally {
-                    after();
-                }
-            }
-        };
-    }
-
-    protected void before() {
-
-        Dictionary<String, Object> props = new Hashtable<String, Object>();
-        props.put("event.topics", "org/apache/sling/ide/transport");
-        registration = Activator.getDefault().getBundle().getBundleContext()
-                .registerService(EventHandler.class, this, props);
-
-    }
-
-    protected void after() throws InterruptedException {
-
-        if (registration != null) {
-            registration.unregister();
-        }
-
-        waitForEventsToSettle();
-
-        if (unexpectedEvents.isEmpty()) {
-            return;
-        }
-
-        StringBuilder desc = new StringBuilder();
-        desc.append("Unexpected events captured during import : ");
-        for (Event event : unexpectedEvents) {
-
-            String flags = (String) event.getProperty(CommandExecutionProperties.ACTION_FLAGS);
-
-            desc.append('\n');
-            desc.append(event.getProperty(CommandExecutionProperties.ACTION_TYPE));
-            if (flags != null && flags.length() > 0) {
-                desc.append(" (").append(flags).append(")");
-            }
-            desc.append(" -> ");
-            desc.append(event.getProperty(CommandExecutionProperties.ACTION_TARGET));
-            desc.append(" : ");
-            desc.append(event.getProperty(CommandExecutionProperties.RESULT_TEXT));
-        }
-
-        fail(desc.toString());
-    }
-
-    /**
-     * Clears the list of unexpected events after the event firing settles
-     * 
-     * <p>
-     * This can be useful for instance when you want to validate that no import events take place after a certain point
-     * in time.
-     * </p>
-     * 
-     * <p>
-     * Event firing settling is defined as no unexpected events being recorded for {@value #SETTLE_TIMEOUT_MILLIS}
-     * milliseconds
-     * </p>
-     */
-    public void clearUnexpectedEventsAfterSettling() throws InterruptedException {
-
-        waitForEventsToSettle();
-
-        unexpectedEvents.clear();
-    }
-
-    private void waitForEventsToSettle() throws InterruptedException {
-
-        int currentSize;
-        do {
-            currentSize = unexpectedEvents.size();
-            Thread.sleep(SETTLE_TIMEOUT_MILLIS);
-        } while (currentSize != unexpectedEvents.size());
-    }
+public class FailOnModificationEventsRule extends AbstractFailOnUnexpectedEventsRule {
 
     @Override
     public void handleEvent(Event event) {
@@ -146,9 +38,8 @@ public class FailOnModificationEventsRul
 
             // it's OK to create prerequisites if needed
             if (flags == null || !CommandExecutionFlag.CREATE_ONLY_WHEN_MISSING.toString().equals(flags)) {
-                unexpectedEvents.add(event);
+                addUnexpectedEvent(event);
             }
-
         }
     }