ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1506071 - /ace/trunk/org.apache.ace.test/src/org/apache/ace/it/IntegrationTestBase.java
Date Tue, 23 Jul 2013 14:39:46 GMT
Author: marrs
Date: Tue Jul 23 14:39:46 2013
New Revision: 1506071

URL: http://svn.apache.org/r1506071
Log:
Added a few convenience methods to enable (event) logging for debugging.

Modified:
    ace/trunk/org.apache.ace.test/src/org/apache/ace/it/IntegrationTestBase.java

Modified: ace/trunk/org.apache.ace.test/src/org/apache/ace/it/IntegrationTestBase.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.test/src/org/apache/ace/it/IntegrationTestBase.java?rev=1506071&r1=1506070&r2=1506071&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.test/src/org/apache/ace/it/IntegrationTestBase.java (original)
+++ ace/trunk/org.apache.ace.test/src/org/apache/ace/it/IntegrationTestBase.java Tue Jul 23
14:39:46 2013
@@ -42,8 +42,13 @@ import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.FrameworkUtil;
 import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
 import org.osgi.service.cm.Configuration;
 import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventConstants;
+import org.osgi.service.event.EventHandler;
+import org.osgi.service.log.LogService;
 import org.osgi.util.tracker.ServiceTracker;
 
 /**
@@ -346,4 +351,87 @@ public class IntegrationTestBase extends
             fail("Interrupted while waiting for services to get started.");
         }
     }
+    
+    protected Component m_eventLoggingComponent;
+    protected Component m_loggingComponent;
+
+    /**
+     * Enables logging events to the console. Mainly useful when debugging tests.
+     */
+    protected synchronized void enableEventLogging() {
+        DependencyManager dm = m_dependencyManager;
+        m_eventLoggingComponent = dm.createComponent()
+            .setInterface(EventHandler.class.getName(), new Properties() {{ put(EventConstants.EVENT_TOPIC,
"*"); }})
+            .setImplementation(new EventHandler() {
+                @Override
+                public void handleEvent(Event event) {
+                    System.out.print("[EVENT] " + event.getTopic());
+                    for (String key : event.getPropertyNames()) {
+                        System.out.print(" " + key + "=" + event.getProperty(key));
+                    }
+                    System.out.println();
+                }
+            });
+        dm.add(m_eventLoggingComponent);
+    }
+
+    /**
+     * Disables logging to the console.
+     */
+    protected synchronized void disableEventLogging() {
+        if (m_eventLoggingComponent != null) {
+            DependencyManager dm = m_dependencyManager;
+            dm.remove(m_eventLoggingComponent);
+            m_eventLoggingComponent = null;
+        }
+    }
+    
+    /**
+     * Enables logging to the console. Mainly useful when debugging tests.
+     */
+    protected synchronized void enableLogging() {
+        if (m_loggingComponent == null) {
+            DependencyManager dm = m_dependencyManager;
+            m_loggingComponent = dm.createComponent()
+                .setInterface(LogService.class.getName(), new Properties() {{ put(Constants.SERVICE_RANKING,
Integer.valueOf(1000)); }})
+                .setImplementation(new LogService() {
+                    @Override
+                    public void log(ServiceReference sr, int level, String message, Throwable
exception) {
+                        System.out.println("[LOG] " +
+                            (sr == null ? "" : sr + " ") + 
+                            level + " " + 
+                            message + " " + 
+                            (exception == null ? "" : exception));
+                    }
+                    
+                    @Override
+                    public void log(ServiceReference sr, int level, String message) {
+                        log(sr, level, message, null);
+                    }
+                    
+                    @Override
+                    public void log(int level, String message, Throwable exception) {
+                        log(null, level, message, exception);
+                    }
+                    
+                    @Override
+                    public void log(int level, String message) {
+                        log(null, level, message, null);
+                    }
+                });
+            dm.add(m_loggingComponent);
+        }
+    }
+    
+    /**
+     * Disables logging to the console.
+     */
+    protected synchronized void disableLogging() {
+        if (m_loggingComponent != null) {
+            DependencyManager dm = m_dependencyManager;
+            dm.remove(m_loggingComponent);
+            m_loggingComponent = null;
+        }
+    }
+
 }



Mime
View raw message