Author: julien(a)jboss.com
Date: 2007-10-20 18:33:04 -0400 (Sat, 20 Oct 2007)
New Revision: 8728
Added:
modules/test/trunk/unit/src/main/org/jboss/unit/tooling/ByteArrayOutputStreamExt.java
modules/test/trunk/unit/src/main/org/jboss/unit/tooling/ForkedOutputStream.java
modules/test/trunk/unit/src/main/org/jboss/unit/tooling/SystemOutputManager.java
modules/test/trunk/unit/src/main/org/jboss/unit/tooling/SystemOutputView.java
Modified:
modules/test/trunk/unit/src/main/org/jboss/test/unit/sample/SampleTests.java
modules/test/trunk/unit/src/main/org/jboss/unit/report/impl/junit/JUnitReporter.java
modules/test/trunk/unit/src/main/org/jboss/unit/tooling/Main.java
Log:
- Handle System.out / System.err at the Main level
- added SystemOutputManager and SystemOutputView for managing System.out / System.err
Modified: modules/test/trunk/unit/src/main/org/jboss/test/unit/sample/SampleTests.java
===================================================================
---
modules/test/trunk/unit/src/main/org/jboss/test/unit/sample/SampleTests.java 2007-10-20
21:13:45 UTC (rev 8727)
+++
modules/test/trunk/unit/src/main/org/jboss/test/unit/sample/SampleTests.java 2007-10-20
22:33:04 UTC (rev 8728)
@@ -31,6 +31,7 @@
import org.jboss.unit.runner.model.ModelBuilder;
import org.jboss.unit.runner.model.TestSuiteDef;
import org.jboss.unit.runner.model.BuilderException;
+import org.jboss.unit.tooling.SystemOutputManager;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -72,21 +73,32 @@
private static void test2()
throws ParserConfigurationException, IOException, SAXException, BuilderException
{
- InputStream in = SampleTests.class.getResourceAsStream("f1.xml");
- InputSource source = new InputSource(in);
- ModelBuilder builder = new ModelBuilder(source);
- TestSuiteDef suite = builder.build();
+ SystemOutputManager outputManager = new SystemOutputManager();
- TestRunner runner = suite.createRunner();
+ try
+ {
+ outputManager.start();
- TestRunnerEventListener junitReporter = new JUnitReporter("output/tests",
SampleTests.class.getName());
- // add listeners
+ InputStream in = SampleTests.class.getResourceAsStream("f1.xml");
+ InputSource source = new InputSource(in);
+ ModelBuilder builder = new ModelBuilder(source);
+ TestSuiteDef suite = builder.build();
- TestRunnerEventBroadcaster broadcaster = new TestRunnerEventBroadcaster();
- broadcaster.addListener(junitReporter);
- broadcaster.addListener(new PrintListener());
+ TestRunner runner = suite.createRunner();
- // run the test
- runner.run(new TestRunnerContextSupport(broadcaster));
+ TestRunnerEventListener junitReporter = new
JUnitReporter("output/tests", SampleTests.class.getName());
+ // add listeners
+
+ TestRunnerEventBroadcaster broadcaster = new TestRunnerEventBroadcaster();
+ broadcaster.addListener(junitReporter);
+ broadcaster.addListener(new PrintListener());
+
+ // run the test
+ runner.run(new TestRunnerContextSupport(broadcaster));
+ }
+ finally
+ {
+ outputManager.stop();
+ }
}
}
Modified:
modules/test/trunk/unit/src/main/org/jboss/unit/report/impl/junit/JUnitReporter.java
===================================================================
---
modules/test/trunk/unit/src/main/org/jboss/unit/report/impl/junit/JUnitReporter.java 2007-10-20
21:13:45 UTC (rev 8727)
+++
modules/test/trunk/unit/src/main/org/jboss/unit/report/impl/junit/JUnitReporter.java 2007-10-20
22:33:04 UTC (rev 8728)
@@ -22,13 +22,8 @@
******************************************************************************/
package org.jboss.unit.report.impl.junit;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.PrintStream;
-
-import org.jboss.unit.runner.TestRunnerEventListener;
import org.jboss.unit.runner.TestRunnerEvent;
+import org.jboss.unit.runner.TestRunnerEventListener;
import org.jboss.unit.runner.event.EndRunnerEvent;
import org.jboss.unit.runner.event.EndTestCaseEvent;
import org.jboss.unit.runner.event.EndTestSuiteEvent;
@@ -36,7 +31,12 @@
import org.jboss.unit.runner.event.StartRunnerEvent;
import org.jboss.unit.runner.event.StartTestCaseEvent;
import org.jboss.unit.runner.event.StartTestSuiteEvent;
+import org.jboss.unit.tooling.SystemOutputManager;
+import org.jboss.unit.tooling.SystemOutputView;
+import java.io.StringWriter;
+import java.io.IOException;
+
/**
*
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
@@ -47,24 +47,16 @@
{
/** The JUnit test report */
private JUnitTestReport report;
-
- /** The system error printStream */
- private ByteArrayOutputStream systemError;
-
- /** The system out printStream */
- private ByteArrayOutputStream systemOut;
-
+
/** To dir */
- private static String toDir;
+ private String toDir;
+ private SystemOutputView view;
public JUnitReporter(String reportToDir)
{
toDir = reportToDir;
// redirecting system out and err
- this.systemError = new ByteArrayOutputStream();
- this.systemOut = new ByteArrayOutputStream();
- this.setStreams(systemError, systemOut);
this.report = new JUnitTestReport();
}
@@ -84,13 +76,24 @@
if (event instanceof StartRunnerEvent)
{
report.startJUnitTestSuite((StartRunnerEvent) event);
+ view = SystemOutputManager.getInstance().createView();
}
else if (event instanceof EndRunnerEvent)
{
// append system out and system err
- report.appendSystemError(systemError.toString());
- report.appendSystemOutput(systemOut.toString());
-
+ try
+ {
+ StringWriter out = new StringWriter();
+ StringWriter err = new StringWriter();
+ view.writeTo(out, err);
+ report.appendSystemError(err.toString());
+ report.appendSystemOutput(out.toString());
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+
// Testsuite End
report.endTestSuite((EndRunnerEvent) event);
@@ -118,67 +121,4 @@
report.endTest((EndTestCaseEvent) event);
}
}
-
- private void setStreams(ByteArrayOutputStream a, ByteArrayOutputStream b)
- {
- PrintStream savedError = System.err;
- PrintStream savedOut = System.out;
-
- System.setErr(new PrintStream(new CaptureOutputStream(savedError, a)));
- System.setOut(new PrintStream(new CaptureOutputStream(savedOut, b)));
-
- }
-
- private class CaptureOutputStream extends OutputStream
- {
- private OutputStream s1;
- private OutputStream s2;
-
- public CaptureOutputStream(OutputStream s1, OutputStream s2)
- {
- this.s1 = s1;
- this.s2 = s2;
- }
-
- @Override
- public void close() throws IOException
- {
- try
- {
- s1.close();
- }
- finally
- {
- s2.close();
- }
- }
-
- @Override
- public void flush() throws IOException
- {
- s1.flush();
- s2.flush();
- }
-
- @Override
- public void write(int b) throws IOException
- {
- s1.write(b);
- s2.write(b);
- }
-
- @Override
- public void write(byte[] b) throws IOException
- {
- s1.write(b);
- s2.write(b);
- }
-
- @Override
- public void write(byte[] b, int off, int len) throws IOException
- {
- s1.write(b, off, len);
- s2.write(b, off, len);
- }
- }
}
Added:
modules/test/trunk/unit/src/main/org/jboss/unit/tooling/ByteArrayOutputStreamExt.java
===================================================================
--- modules/test/trunk/unit/src/main/org/jboss/unit/tooling/ByteArrayOutputStreamExt.java
(rev 0)
+++
modules/test/trunk/unit/src/main/org/jboss/unit/tooling/ByteArrayOutputStreamExt.java 2007-10-20
22:33:04 UTC (rev 8728)
@@ -0,0 +1,49 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.unit.tooling;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.Writer;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+class ByteArrayOutputStreamExt extends ByteArrayOutputStream
+{
+
+ public int writeTo(OutputStream out, int from) throws IOException,
ArrayIndexOutOfBoundsException
+ {
+ out.write(buf, from, count);
+ return count;
+ }
+
+ public int writeTo(Writer out, int from) throws IOException,
ArrayIndexOutOfBoundsException
+ {
+ String s = new String(buf, from, count);
+ out.write(s);
+ return count;
+ }
+}
Added: modules/test/trunk/unit/src/main/org/jboss/unit/tooling/ForkedOutputStream.java
===================================================================
--- modules/test/trunk/unit/src/main/org/jboss/unit/tooling/ForkedOutputStream.java
(rev 0)
+++
modules/test/trunk/unit/src/main/org/jboss/unit/tooling/ForkedOutputStream.java 2007-10-20
22:33:04 UTC (rev 8728)
@@ -0,0 +1,80 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.unit.tooling;
+
+import java.io.OutputStream;
+import java.io.IOException;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+class ForkedOutputStream extends OutputStream
+{
+
+ /** . */
+ private final OutputStream out1;
+
+ /** . */
+ private final OutputStream out2;
+
+ public ForkedOutputStream(OutputStream out1, OutputStream out2)
+ {
+ this.out1 = out1;
+ this.out2 = out2;
+ }
+
+ public void write(int i) throws IOException
+ {
+ safeWrite(out1, i);
+ safeWrite(out2, i);
+ }
+
+ public void flush() throws IOException
+ {
+ safeFlush(out1);
+ safeFlush(out2);
+ }
+
+ private void safeWrite(OutputStream out, int i)
+ {
+ try
+ {
+ out.write(i);
+ }
+ catch (IOException ignore)
+ {
+ }
+ }
+
+ private void safeFlush(OutputStream out)
+ {
+ try
+ {
+ out.flush();
+ }
+ catch (IOException ignore)
+ {
+ }
+ }
+}
Modified: modules/test/trunk/unit/src/main/org/jboss/unit/tooling/Main.java
===================================================================
--- modules/test/trunk/unit/src/main/org/jboss/unit/tooling/Main.java 2007-10-20 21:13:45
UTC (rev 8727)
+++ modules/test/trunk/unit/src/main/org/jboss/unit/tooling/Main.java 2007-10-20 22:33:04
UTC (rev 8728)
@@ -153,12 +153,20 @@
this.broadcaster = broadcaster;
}
+ private SystemOutputManager systemManager;
+
public void execute() throws Exception
{
- // For now lets just be verbose. Later we need some real logging + 'debug'
switch
- System.out.println("TestRunnerContext contains: ");
- System.out.println("Properties: " + properties.toString());
+ systemManager = new SystemOutputManager();
+ systemManager.start();
+ //
+ try
+ {
+ // For now lets just be verbose. Later we need some real logging +
'debug' switch
+ System.out.println("TestRunnerContext contains: ");
+ System.out.println("Properties: " + properties.toString());
+
// System.out.println("System properties:");
// for (Object key : System.getProperties().keySet())
// {
@@ -166,15 +174,21 @@
// System.out.println(key + "=" + System.getProperty((String)key));
// }
- StringWriter sw = new StringWriter();
- broadcaster.addListener(new PrintListener(sw));
+ StringWriter sw = new StringWriter();
+ broadcaster.addListener(new PrintListener(sw));
- TestRunnerContextSupport runnerContext = new TestRunnerContextSupport(properties,
new ParametrizationSet(), mainFilter, broadcaster);
- runner.run(runnerContext);
+ TestRunnerContextSupport runnerContext = new
TestRunnerContextSupport(properties, new ParametrizationSet(), mainFilter, broadcaster);
+ runner.run(runnerContext);
- sw.close();
+ sw.close();
- System.out.println(sw.toString());
+ System.out.println(sw.toString());
+ }
+ finally
+ {
+ systemManager.stop();
+ }
+
}
public static void main(String[] args) throws Exception
Added: modules/test/trunk/unit/src/main/org/jboss/unit/tooling/SystemOutputManager.java
===================================================================
--- modules/test/trunk/unit/src/main/org/jboss/unit/tooling/SystemOutputManager.java
(rev 0)
+++
modules/test/trunk/unit/src/main/org/jboss/unit/tooling/SystemOutputManager.java 2007-10-20
22:33:04 UTC (rev 8728)
@@ -0,0 +1,102 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.unit.tooling;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class SystemOutputManager
+{
+
+ /** . */
+ private static SystemOutputManager instance;
+
+ /** . */
+ private PrintStream originalOut;
+
+ /** . */
+ private PrintStream originalErr;
+
+ /** . */
+ private ByteArrayOutputStreamExt baosOut;
+
+ /** . */
+ private ByteArrayOutputStreamExt baosErr;
+
+ public SystemOutputManager()
+ {
+ }
+
+ public static SystemOutputManager getInstance()
+ {
+ return instance;
+ }
+
+ public SystemOutputView createView()
+ {
+ return new SystemOutputView(baosOut, baosErr);
+ }
+
+ public void start()
+ {
+ synchronized (SystemOutputManager.class)
+ {
+ if (instance != null)
+ {
+ throw new IllegalStateException();
+ }
+
+ //
+ baosOut = new ByteArrayOutputStreamExt();
+ baosErr = new ByteArrayOutputStreamExt();
+ originalOut = System.out;
+ originalErr = System.err;
+ ForkedOutputStream fOut = new ForkedOutputStream(originalOut, baosOut);
+ ForkedOutputStream fErr = new ForkedOutputStream(originalErr, baosErr);
+
+ //
+ System.setOut(new PrintStream(fOut, true));
+ System.setErr(new PrintStream(fErr, true));
+ instance = this;
+ }
+ }
+
+ public void stop()
+ {
+ synchronized (SystemOutputManager.class)
+ {
+ if (instance != null)
+ {
+ System.out.flush();
+ System.err.flush();
+ instance = null;
+ System.setOut(originalOut);
+ System.setErr(originalErr);
+ }
+ }
+ }
+}
Added: modules/test/trunk/unit/src/main/org/jboss/unit/tooling/SystemOutputView.java
===================================================================
--- modules/test/trunk/unit/src/main/org/jboss/unit/tooling/SystemOutputView.java
(rev 0)
+++
modules/test/trunk/unit/src/main/org/jboss/unit/tooling/SystemOutputView.java 2007-10-20
22:33:04 UTC (rev 8728)
@@ -0,0 +1,73 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.unit.tooling;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.Writer;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class SystemOutputView
+{
+
+ /** . */
+ private ByteArrayOutputStreamExt out;
+
+ /** . */
+ private ByteArrayOutputStreamExt err;
+
+ /** . */
+ private int outCount;
+
+ /** . */
+ private int errCount;
+
+ public SystemOutputView(ByteArrayOutputStreamExt out, ByteArrayOutputStreamExt err)
+ {
+ this.out = out;
+ this.err = err;
+ this.outCount = 0;
+ this.errCount = 0;
+ }
+
+ public void writeTo(OutputStream toOut, OutputStream toErr) throws IOException
+ {
+ outCount = out.writeTo(toOut, outCount);
+ errCount = err.writeTo(toErr, errCount);
+ }
+
+ public void writeTo(Writer toOut, Writer toErr) throws IOException
+ {
+ outCount = out.writeTo(toOut, outCount);
+ errCount = err.writeTo(toErr, errCount);
+ }
+
+ public void reset()
+ {
+ outCount = 0;
+ errCount = 0;
+ }
+}