Updated Branches: refs/heads/master a78ce4ce4 -> 9db8ce813
[SUREFIRE-998] Convert byte arrays to be written as CDATA to the XML file from Charset.defaultEncoding() to UTF-8 Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/9db8ce81 Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/9db8ce81 Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/9db8ce81 Branch: refs/heads/master Commit: 9db8ce8132f7fadbb90180ffe350087dc1361f51 Parents: a78ce4c Author: Andreas Gudian <agud...@apache.org> Authored: Sat Aug 10 23:12:27 2013 +0200 Committer: Andreas Gudian <agud...@apache.org> Committed: Sat Aug 10 23:12:27 2013 +0200 ---------------------------------------------------------------------- .../surefire/report/StatelessXmlReporter.java | 37 ++++---- .../surefire/report/TestSetRunListener.java | 9 +- .../Utf8RecodingDeferredFileOutputStream.java | 91 ++++++++++++++++++++ .../surefire/report/WrappedReportEntry.java | 17 ++-- .../report/StatelessXMLReporterTest.java | 30 +++---- 5 files changed, 134 insertions(+), 50 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/9db8ce81/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporter.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporter.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporter.java index b1fc83b..a3e4455 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporter.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporter.java @@ -19,13 +19,6 @@ package org.apache.maven.plugin.surefire.report; * under the License. */ -import org.apache.commons.io.output.DeferredFileOutputStream; -import org.apache.maven.shared.utils.io.IOUtil; -import org.apache.maven.shared.utils.xml.XMLWriter; -import org.apache.maven.surefire.report.ReportEntry; -import org.apache.maven.surefire.report.ReporterException; -import org.apache.maven.surefire.report.SafeThrowable; - import java.io.File; import java.io.FileOutputStream; import java.io.FilterOutputStream; @@ -33,10 +26,17 @@ import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; import java.util.Enumeration; import java.util.Properties; import java.util.StringTokenizer; +import org.apache.maven.shared.utils.io.IOUtil; +import org.apache.maven.shared.utils.xml.XMLWriter; +import org.apache.maven.surefire.report.ReportEntry; +import org.apache.maven.surefire.report.ReporterException; +import org.apache.maven.surefire.report.SafeThrowable; + import static org.apache.maven.plugin.surefire.report.FileReporterUtils.stripIllegalFilenameChars; /** @@ -76,6 +76,8 @@ public class StatelessXmlReporter private static final String ENCODING = "UTF-8"; + private static final Charset ENCODING_CS = Charset.forName( ENCODING ); + private final File reportsDirectory; private final String reportNameSuffix; @@ -131,15 +133,7 @@ public class StatelessXmlReporter private OutputStreamWriter getWriter( FileOutputStream fos ) { - try - { - - return new OutputStreamWriter( fos, ENCODING ); - } - catch ( IOException e ) - { - throw new ReporterException( "When writing report", e ); - } + return new OutputStreamWriter( fos, ENCODING_CS ); } private FileOutputStream getOutputStream( WrappedReportEntry testSetReportEntry ) @@ -277,10 +271,11 @@ public class StatelessXmlReporter } private void addOutputStreamElement( OutputStreamWriter outputStreamWriter, OutputStream fw, - EncodingOutputStream eos, XMLWriter xmlWriter, DeferredFileOutputStream stdOut, + EncodingOutputStream eos, XMLWriter xmlWriter, + Utf8RecodingDeferredFileOutputStream utf8RecodingDeferredFileOutputStream, String name ) { - if ( stdOut != null && stdOut.getByteCount() > 0 ) + if ( utf8RecodingDeferredFileOutputStream != null && utf8RecodingDeferredFileOutputStream.getByteCount() > 0 ) { xmlWriter.startElement( name ); @@ -289,9 +284,9 @@ public class StatelessXmlReporter { xmlWriter.writeText( "" ); // Cheat sax to emit element outputStreamWriter.flush(); - stdOut.close(); + utf8RecodingDeferredFileOutputStream.close(); eos.getUnderlying().write( ByteConstantsHolder.CDATA_START_BYTES ); // emit cdata - stdOut.writeTo( eos ); + utf8RecodingDeferredFileOutputStream.writeTo( eos ); eos.getUnderlying().write( ByteConstantsHolder.CDATA_END_BYTES ); eos.flush(); } @@ -403,7 +398,7 @@ public class StatelessXmlReporter } else { - out.write( b ); //To change body of overridden methods use File | Settings | File Templates. + out.write( b ); } c1 = c2; c2 = b; http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/9db8ce81/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java index 50eaa38..027c326 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java @@ -45,16 +45,15 @@ public class TestSetRunListener private final TestSetStats detailsForThis; - private DeferredFileOutputStream testStdOut = initDeferred( "stdout" ); + private Utf8RecodingDeferredFileOutputStream testStdOut = initDeferred( "stdout" ); - private DeferredFileOutputStream testStdErr = initDeferred( "stderr" ); + private Utf8RecodingDeferredFileOutputStream testStdErr = initDeferred( "stderr" ); - private DeferredFileOutputStream initDeferred( String channel ) + private Utf8RecodingDeferredFileOutputStream initDeferred( String channel ) { - return new DeferredFileOutputStream( 1000000, channel , "deferred", null ); + return new Utf8RecodingDeferredFileOutputStream( channel ); } - private final TestcycleConsoleOutputReceiver consoleOutputReceiver; private final boolean briefOrPlainFormat; http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/9db8ce81/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/Utf8RecodingDeferredFileOutputStream.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/Utf8RecodingDeferredFileOutputStream.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/Utf8RecodingDeferredFileOutputStream.java new file mode 100644 index 0000000..f36a4bd --- /dev/null +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/Utf8RecodingDeferredFileOutputStream.java @@ -0,0 +1,91 @@ +package org.apache.maven.plugin.surefire.report; + +/* + * 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. + */ + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; + +import org.apache.commons.io.output.DeferredFileOutputStream; + +/** + * A deferred file output stream decorator that recodes the bytes written into the stream from the VM default encoding + * to UTF-8. + * + * @author Andreas Gudian + */ +class Utf8RecodingDeferredFileOutputStream +{ + private DeferredFileOutputStream deferredFileOutputStream; + + private static final Charset UTF8 = Charset.forName( "UTF-8" ); + + public Utf8RecodingDeferredFileOutputStream( String channel ) + { + this.deferredFileOutputStream = new DeferredFileOutputStream( 1000000, channel, "deferred", null ); + } + + public void write( byte[] buf, int off, int len ) + throws IOException + { + if ( !Charset.defaultCharset().equals( UTF8 ) ) + { + CharBuffer decodedFromDefaultCharset = Charset.defaultCharset().decode( ByteBuffer.wrap( buf, off, len ) ); + ByteBuffer utf8Encoded = UTF8.encode( decodedFromDefaultCharset ); + + if ( utf8Encoded.hasArray() ) + { + byte[] convertedBytes = utf8Encoded.array(); + + deferredFileOutputStream.write( convertedBytes, utf8Encoded.position(), utf8Encoded.remaining() ); + } + else + { + byte[] convertedBytes = new byte[utf8Encoded.remaining()]; + utf8Encoded.get( convertedBytes, 0, utf8Encoded.remaining() ); + + deferredFileOutputStream.write( convertedBytes, 0, convertedBytes.length ); + } + } + else + { + deferredFileOutputStream.write( buf, off, len ); + } + } + + public long getByteCount() + { + return deferredFileOutputStream.getByteCount(); + } + + public void close() + throws IOException + { + deferredFileOutputStream.close(); + } + + public void writeTo( OutputStream out ) + throws IOException + { + deferredFileOutputStream.writeTo( out ); + } +} http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/9db8ce81/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/WrappedReportEntry.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/WrappedReportEntry.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/WrappedReportEntry.java index cd7a956..d38ae76 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/WrappedReportEntry.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/WrappedReportEntry.java @@ -19,13 +19,12 @@ package org.apache.maven.plugin.surefire.report; * under the License. */ -import org.apache.commons.io.output.DeferredFileOutputStream; -import org.apache.maven.surefire.report.ReportEntry; -import org.apache.maven.surefire.report.StackTraceWriter; - import java.text.NumberFormat; import java.util.Locale; +import org.apache.maven.surefire.report.ReportEntry; +import org.apache.maven.surefire.report.StackTraceWriter; + /** * @author Kristian Rosenvold */ @@ -38,9 +37,9 @@ public class WrappedReportEntry private final Integer elapsed; - private final DeferredFileOutputStream stdout; + private final Utf8RecodingDeferredFileOutputStream stdout; - private final DeferredFileOutputStream stdErr; + private final Utf8RecodingDeferredFileOutputStream stdErr; private final NumberFormat numberFormat = NumberFormat.getInstance( Locale.ENGLISH ); @@ -49,7 +48,7 @@ public class WrappedReportEntry static final String NL = System.getProperty( "line.separator" ); public WrappedReportEntry( ReportEntry original, ReportEntryType reportEntryType, Integer estimatedElapsed, - DeferredFileOutputStream stdout, DeferredFileOutputStream stdErr ) + Utf8RecodingDeferredFileOutputStream stdout, Utf8RecodingDeferredFileOutputStream stdErr ) { this.original = original; this.reportEntryType = reportEntryType; @@ -68,12 +67,12 @@ public class WrappedReportEntry return reportEntryType; } - public DeferredFileOutputStream getStdout() + public Utf8RecodingDeferredFileOutputStream getStdout() { return stdout; } - public DeferredFileOutputStream getStdErr() + public Utf8RecodingDeferredFileOutputStream getStdErr() { return stdErr; } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/9db8ce81/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/report/StatelessXMLReporterTest.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/report/StatelessXMLReporterTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/report/StatelessXMLReporterTest.java index a99b749..8ed78c2 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/report/StatelessXMLReporterTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/report/StatelessXMLReporterTest.java @@ -19,9 +19,15 @@ package org.apache.maven.plugin.surefire.report; * under the License. */ +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.Charset; + import junit.framework.AssertionFailedError; import junit.framework.TestCase; -import org.apache.commons.io.output.DeferredFileOutputStream; + import org.apache.maven.plugin.surefire.booterclient.output.DeserializedStacktraceWriter; import org.apache.maven.shared.utils.StringUtils; import org.apache.maven.shared.utils.xml.Xpp3Dom; @@ -31,12 +37,6 @@ import org.apache.maven.surefire.report.ReportEntry; import org.apache.maven.surefire.report.SimpleReportEntry; import org.apache.maven.surefire.report.StackTraceWriter; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStreamReader; - @SuppressWarnings( "ResultOfMethodCallIgnored" ) public class StatelessXMLReporterTest extends TestCase @@ -88,15 +88,15 @@ public class StatelessXMLReporterTest stats.testSucceeded( testSetReportEntry ); StackTraceWriter stackTraceWriter = new DeserializedStacktraceWriter( "A fud msg", "trimmed", "fail at foo" ); - DeferredFileOutputStream s = new DeferredFileOutputStream( 1000000, "fds", "fdx", new File( "" ) ); - String expected = "st]]>d-o\u00DCt<null>!\u0020\u0000\u001F"; - s.write( expected.getBytes( "UTF-8" ) ); - DeferredFileOutputStream s1 = new DeferredFileOutputStream( 1000000, "fds", "fdx", new File( "" ) ); - byte[] bytes = "std-\u0115rr?&-&£\u0020\u0000\u001F".getBytes("UTF-8"); - s1.write( bytes ); + Utf8RecodingDeferredFileOutputStream stdOut = new Utf8RecodingDeferredFileOutputStream( "fds" ); + byte[] stdOutBytes = "st]]>d-o\u00DCt<null>!\u0020\u0000\u001F".getBytes(); + stdOut.write( stdOutBytes, 0, stdOutBytes.length ); + Utf8RecodingDeferredFileOutputStream stdErr = new Utf8RecodingDeferredFileOutputStream( "fds" ); + byte[] stdErrBytes = "std-örr?&-&£\u0020\u0000\u001F".getBytes(); + stdErr.write( stdErrBytes, 0, stdErrBytes.length ); WrappedReportEntry t2 = new WrappedReportEntry( new SimpleReportEntry( Inner.class.getName(), testName2, stackTraceWriter, 13 ), - ReportEntryType.error, 13, s, s1 ); + ReportEntryType.error, 13, stdOut, stdErr ); stats.testSucceeded( t2 ); StatelessXmlReporter reporter = new StatelessXmlReporter( new File( "." ), null, false ); @@ -127,7 +127,7 @@ public class StatelessXMLReporterTest assertEquals( "A fud msg", errorNode.getAttribute( "message" ) ); assertEquals( "fail at foo", errorNode.getAttribute( "type" ) ); assertEquals( "st]]>d-o\u00DCt<null>! &#0;&#31;", tcb.getChild( "system-out" ).getValue() ); - assertEquals( "std-\u0115rr?&-&£ &#0;&#31;", tcb.getChild( "system-err" ).getValue() ); + assertEquals( "std-örr?&-&£ &#0;&#31;", tcb.getChild( "system-err" ).getValue() ); expectedReportFile.delete(); }