You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cl...@apache.org on 2008/11/23 17:42:27 UTC

svn commit: r720003 - in /felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin: Junit4osgiPlugin.java StringOutputStream.java XMLReport.java log/ log/LogServiceImpl.java

Author: clement
Date: Sun Nov 23 08:42:27 2008
New Revision: 720003

URL: http://svn.apache.org/viewvc?rev=720003&view=rev
Log:
The plugin provides its own log service implementation used to collect logged messages during tests.
Moreover, messages printed on System.out and System.err are also collected.

Added:
    felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/StringOutputStream.java
    felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/log/
    felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/log/LogServiceImpl.java
Modified:
    felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/Junit4osgiPlugin.java
    felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/XMLReport.java

Modified: felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/Junit4osgiPlugin.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/Junit4osgiPlugin.java?rev=720003&r1=720002&r2=720003&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/Junit4osgiPlugin.java (original)
+++ felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/Junit4osgiPlugin.java Sun Nov 23 08:42:27 2008
@@ -33,6 +33,7 @@
  */
 
 import java.io.File;
+import java.io.PrintStream;
 import java.lang.reflect.Method;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -52,6 +53,7 @@
 import junit.framework.TestResult;
 
 import org.apache.felix.framework.Felix;
+import org.apache.felix.ipojo.junit4osgi.plugin.log.LogServiceImpl;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoFailureException;
@@ -120,6 +122,11 @@
     List failures = new ArrayList();
     List results = new ArrayList();
     
+    /**
+     * Log Service exposed by the plugin framework.
+     */
+    private LogServiceImpl logService;
+    
    
     public void execute() throws MojoFailureException {
         
@@ -127,12 +134,14 @@
         bundles.addAll(getTestBundle());
         
         List activators = new ArrayList();
+        logService = new LogServiceImpl();
+        activators.add(logService);
         activators.add(new Installer(pluginArtifacts, bundles, project, deployProjectArtifact));
         Map map = new HashMap();
         map.put("felix.systembundle.activators", activators);
         map.put("org.osgi.framework.storage.clean", "onFirstInit");
         map.put("ipojo.log.level", "WARNING");
-        map.put("org.osgi.framework.bootdelegation", "junit.framework");
+        map.put("org.osgi.framework.bootdelegation", "junit.framework, org.osgi.service.log");
         map.put("org.osgi.framework.storage", targetDir.getAbsolutePath() + "/felix-cache"); 
 
         
@@ -147,14 +156,13 @@
         } catch (BundleException e) {
             e.printStackTrace();
         }
-        // dumpBundles(felix.getBundleContext());
         
         waitForStability(felix.getBundleContext());
         
+
         Object runner = waitForRunnerService(felix.getBundleContext());
-        
         invokeRun(runner, felix.getBundleContext());
-        
+                
         try {
             felix.stop();
             felix.waitForStop(5000);
@@ -365,8 +373,6 @@
         test.run(tr);        
         results.add(tr);
         
-        
-        
         if (tr.wasSuccessful()) {
             System.out.println("Tests run: " + tr.runCount() + ", Failures: " + tr.failureCount() + ", Errors: " + tr.errorCount() + ", Time elapsed: " + report.elapsedTimeAsString(report.endTime - report.endTime) + " sec");
         } else {
@@ -401,24 +407,34 @@
         }
     }
     
+    public LogServiceImpl getLogService() {
+        return logService;
+    }
+    
     
     private class ResultListener implements TestListener {
         
         private XMLReport report;
-        boolean abort;
+        private boolean abort;
+        
+        private PrintStream outBackup = System.out;
+        private PrintStream errBackup = System.err;
+        
+        private StringOutputStream out = new StringOutputStream();
+        private StringOutputStream err = new StringOutputStream();;
         
         public ResultListener(XMLReport report) {
             this.report = report;
         }
 
         public void addError(Test test, Throwable throwable) {
-            report.testError(test, throwable);
+            report.testError(test, throwable, out.toString(), err.toString(), getLogService().getLoggedMessages());
             abort = true;
         }
 
         public void addFailure(Test test,
                 AssertionFailedError assertionfailederror) {
-            report.testFailed(test, assertionfailederror);
+            report.testFailed(test, assertionfailederror, out.toString(), err.toString(), getLogService().getLoggedMessages());
             abort = true;
             
         }
@@ -427,12 +443,19 @@
            if (!abort) {
                report.testSucceeded(test);
            }
+           System.setErr(errBackup);
+           System.setOut(outBackup);
+           getLogService().reset();
         }
 
         public void startTest(Test test) {
             abort = false;
             report.testStarting();
+            System.setErr(new PrintStream(err));
+            System.setOut(new PrintStream(out));
+            getLogService().enableOutputStream();
         }
         
     }
+    
 }

Added: felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/StringOutputStream.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/StringOutputStream.java?rev=720003&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/StringOutputStream.java (added)
+++ felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/StringOutputStream.java Sun Nov 23 08:42:27 2008
@@ -0,0 +1,99 @@
+package org.apache.felix.ipojo.junit4osgi.plugin;
+
+import java.io.OutputStream;
+import java.io.Serializable;
+
+/**
+ * Provides an OutputStream to an internal String. Internally converts bytes to
+ * a Strings and stores them in an internal StringBuffer.
+ */
+public class StringOutputStream extends OutputStream implements Serializable {
+
+    /**
+     * Id.
+     */
+    private static final long serialVersionUID = -5912060965986156224L;
+    
+    /**
+     * The internal destination StringBuffer.
+     */
+    protected StringBuffer buf = null;
+
+    /**
+     * Creates new StringOutputStream, makes a new internal StringBuffer.
+     */
+    public StringOutputStream() {
+        super();
+        buf = new StringBuffer();
+    }
+
+    /**
+     * Returns the content of the internal StringBuffer as a String, the result
+     * of all writing to this OutputStream.
+     * 
+     * @return returns the content of the internal StringBuffer
+     */
+    public String toString() {
+        return buf.toString();
+    }
+
+    /**
+     * Sets the internal StringBuffer to null.
+     */
+    public void close() {
+        buf = null;
+
+    }
+
+    /**
+     * Writes and appends a byte array to StringOutputStream.
+     * 
+     * @param b the byte array
+     * @param off the byte array starting index
+     * @param len the number of bytes from byte array to write to the stream
+     */
+    public void write(byte[] b, int off, int len) {
+        if ((off < 0) || (len < 0) || (off + len) > b.length) {
+            throw new IndexOutOfBoundsException(
+                    "StringOutputStream.write: Parameters out of bounds.");
+        }
+        byte[] bytes = new byte[len];
+        for (int i = 0; i < len; i++) {
+            bytes[i] = b[off];
+            off++;
+        }
+        buf.append(toCharArray(bytes));
+    }
+
+    /**
+     * Writes and appends a single byte to StringOutputStream.
+     * 
+     * @param b the byte as an int to add
+     */
+    public void write(int b) {
+        buf.append((char) b);
+    }
+    
+    /**
+     * Writes and appends a String to StringOutputStream.
+     * 
+     * @param s the String to add
+     */
+    public void write(String s) {
+        buf.append(s);
+    }
+    
+    /**
+     * Converts byte array to char array.
+     */
+    public static char[] toCharArray(byte[] barr) {
+        if (barr == null) {
+            return null;
+        }
+        char[] carr = new char[barr.length];
+        for (int i = 0; i < barr.length; i++) {
+            carr[i] = (char) barr[i];
+        }
+        return carr;
+    }
+}

Modified: felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/XMLReport.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/XMLReport.java?rev=720003&r1=720002&r2=720003&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/XMLReport.java (original)
+++ felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/XMLReport.java Sun Nov 23 08:42:27 2008
@@ -72,10 +72,10 @@
      * @param test the test in error
      * @param e the thrown exception
      */
-    public void testError(Test test, Throwable e) {
+    public void testError(Test test, Throwable e, String out, String err, String log) {
         super.testError(test);
 
-        writeTestProblems(test, e, "error");
+        writeTestProblems(test, e, "error", out, err, log);
     }
 
     /**
@@ -83,10 +83,10 @@
      * @param test the failing test
      * @param e the thrown failure
      */
-    public void testFailed(Test test, Throwable e) {
+    public void testFailed(Test test, Throwable e, String out, String err, String log) {
         super.testFailed(test);
 
-        writeTestProblems(test, e, "failure");
+        writeTestProblems(test, e, "failure", out, err, log);
     }
 
     /**
@@ -95,7 +95,7 @@
      * @param e the thrown error
      * @param name type of failure ("error" or "failure")
      */
-    private void writeTestProblems(Test test, Throwable e, String name) {
+    private void writeTestProblems(Test test, Throwable e, String name, String out, String err, String log) {
 
         long runTime = endTime - startTime;
 
@@ -126,6 +126,12 @@
         if (stackTrace != null) {
             element.setValue(stackTrace);
         }
+        
+        addOutputStreamElement( out, "system-out", testCase );
+
+        addOutputStreamElement( err, "system-err", testCase );
+        
+        addOutputStreamElement( log, "log-service", testCase );
 
         results.add(testCase);
     }
@@ -295,5 +301,12 @@
         }
 
     }
+    
+    private void addOutputStreamElement(String stdOut, String name,
+            Xpp3Dom testCase) {
+        if (stdOut != null && stdOut.trim().length() > 0) {
+            createElement(testCase, name).setValue(stdOut);
+        }
+    }
 
 }

Added: felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/log/LogServiceImpl.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/log/LogServiceImpl.java?rev=720003&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/log/LogServiceImpl.java (added)
+++ felix/sandbox/clement/ipojo-utils/maven-junit4osgi-plugin/src/main/java/org/apache/felix/ipojo/junit4osgi/plugin/log/LogServiceImpl.java Sun Nov 23 08:42:27 2008
@@ -0,0 +1,185 @@
+/*
+ * 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.felix.ipojo.junit4osgi.plugin.log;
+
+import org.apache.felix.ipojo.junit4osgi.plugin.StringOutputStream;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.log.LogService;
+
+/**
+ * An implementation of the log service to collect logged messages.
+ * This service implementation is also {@link BundleActivator} and is
+ * activated when the embedded OSGi platform starts.
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+public class LogServiceImpl implements LogService, BundleActivator {
+    
+    /**
+     * Default output stream (not collected).
+     */
+    private StringOutputStream defaultStream;
+  
+    /**
+     * Collected output stream.
+     */
+    private StringOutputStream outputStream; 
+    
+    /**
+     * Creates the log service object.
+     */
+    public LogServiceImpl() {
+        defaultStream = new StringOutputStream();
+    }
+   
+    /**
+     * Enables the log messages collection.
+     */
+    public void enableOutputStream() {
+        outputStream = new StringOutputStream();
+    }
+    
+    /**
+     * Get collected log messages.
+     * @return the String containing the logged messages.
+     */
+    public String getLoggedMessages() {
+        return outputStream.toString();
+    }
+    
+    /**
+     * Re-initializes the collected message list.
+     */
+    public void reset() {
+        outputStream = null;
+    }
+
+    /**
+     * Logs a message.
+     * @param arg0 the log level
+     * @param arg1 the message
+     * @see org.osgi.service.log.LogService#log(int, java.lang.String)
+     */
+    public void log(int arg0, String arg1) {
+        write(computeLogMessage(arg0, arg1, null));
+    }
+
+    /**
+     * Logs a message with an attached exception.
+     * @param arg0 the log level
+     * @param arg1 the message
+     * @param arg2 the associated exception
+     * @see org.osgi.service.log.LogService#log(int, java.lang.String, java.lang.Throwable)
+     */
+    public void log(int arg0, String arg1, Throwable arg2) {
+        write(computeLogMessage(arg0, arg1, arg2));
+    }
+
+    /**
+     * Logs a message raised by the given service reference.
+     * @param arg0 the service reference
+     * @param arg1 the log level
+     * @param arg2 the message
+     * @see org.osgi.service.log.LogService#log(org.osgi.framework.ServiceReference, int, java.lang.String)
+     */
+    public void log(ServiceReference arg0, int arg1, String arg2) {
+        write(computeLogMessage(arg1, arg2, null));
+    }
+
+    /**
+     * Logs a message raised by the given service reference
+     * associated with an exception.
+     * @param arg0 the service reference
+     * @param arg1 the log level
+     * @param arg2 the message
+     * @param arg3 the exception
+     * @see org.osgi.service.log.LogService#log(org.osgi.framework.ServiceReference, int, java.lang.String)
+     */
+    public void log(ServiceReference arg0, int arg1, String arg2, Throwable arg3) {
+        write(computeLogMessage(arg1, arg2, arg3));
+    }
+    
+    /**
+     * Computes the string from the message.
+     * @param level the log level
+     * @param msg the message
+     * @param exception the exception (can be <code>null</code>
+     * @return the resulting String
+     */
+    private String computeLogMessage(int level, String msg, Throwable exception) {
+        String message = null;
+        switch (level) {
+            case LogService.LOG_DEBUG:
+                message = "[DEBUG] " + msg;
+                break;
+            case LogService.LOG_ERROR:
+                message = "[ERROR] " + msg;
+                break;
+            case LogService.LOG_INFO:
+                message = "[INFO] " + msg;
+                break;
+            case LogService.LOG_WARNING:
+                message = "[WARNING] " + msg;
+                break;
+        }
+        
+        if (exception != null) {
+            message = message + " : " + exception.getMessage() + "\n";
+        }
+        
+        return message;
+    }
+    
+    /**
+     * Writes the given message in the adequate output stream. 
+     * @param log the message
+     */
+    public void write(String log) {
+        if (outputStream != null) {
+            outputStream.write(log);
+        } else {
+            defaultStream.write(log);
+        }
+    }
+
+    /**
+     * Stars the log service implementation:
+     * Registers the service.
+     * @param bc the bundle context.
+     * @throws Exception should not happen.
+     * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
+     */
+    public void start(BundleContext bc) throws Exception {
+        bc.registerService(LogService.class.getName(), this, null);
+    }
+
+    /**
+     * Stops the log service implementation.
+     * Does nothing.
+     * @param arg0 the bundle context
+     * @throws Exception should not happen.
+     * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
+     */
+    public void stop(BundleContext arg0) throws Exception {
+        // Nothing to do.
+        
+    }
+
+}