Skip to content

Commit

Permalink
[SUREFIRE-1006] Fixed cdata section
Browse files Browse the repository at this point in the history
  • Loading branch information
krosenvold committed Jun 24, 2013
1 parent cb908dd commit 54c46be
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ private void addOutputStreamElement( OutputStreamWriter outputStreamWriter, Outp
xmlWriter.writeText( "" ); // Cheat sax to emit element
outputStreamWriter.flush();
stdOut.close();
eos.getUnderlying().write( "<![CDATA[".getBytes() ); // emit cdata
stdOut.writeTo( eos );
eos.getUnderlying().write( "]]>".getBytes() );
eos.flush();
}
catch ( IOException e )
Expand Down Expand Up @@ -361,17 +363,36 @@ private static String extraEscape( String message, boolean attribute )
private static class EncodingOutputStream
extends FilterOutputStream
{
private int c1;

private int c2;

private static final byte[] cdataEscapeString = "]]><![CDATA[>".getBytes();

public EncodingOutputStream( OutputStream out )
{
super( out );
}

public OutputStream getUnderlying()
{
return out;
}

private boolean isCdataEndBlock( int c )
{
return c1 == ']' && c2 == ']' && c == '>';
}

@Override
public void write( int b )
throws IOException
{
if ( isIllegalEscape( b ) )
if ( isCdataEndBlock( b ) )
{
out.write( cdataEscapeString );
}
else if ( isIllegalEscape( b ) )
{
// uh-oh! This character is illegal in XML 1.0!
// http://www.w3.org/TR/1998/REC-xml-19980210#charsets
Expand All @@ -382,32 +403,12 @@ public void write( int b )
out.write( b );
out.write( ';' ); // & Will be encoded to amp inside xml encodingSHO
}
else if ( '<' == b )
{
out.write( '&' );
out.write( 'l' );
out.write( 't' );
out.write( ';' ); // & Will be encoded to amp inside xml encodingSHO
}
else if ( '>' == b )
{
out.write( '&' );
out.write( 'g' );
out.write( 't' );
out.write( ';' ); // & Will be encoded to amp inside xml encodingSHO
}
else if ( '&' == b )
{
out.write( '&' );
out.write( 'a' );
out.write( 'm' );
out.write( 'p' );
out.write( ';' ); // & Will be encoded to amp inside xml encodingSHO
}
else
{
out.write( b ); //To change body of overridden methods use File | Settings | File Templates.
}
c1 = c2;
c2 = b;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public void testAllFieldsSerialized()
stats.testSucceeded( testSetReportEntry );
StackTraceWriter stackTraceWriter = new DeserializedStacktraceWriter( "A fud msg", "trimmed", "fail at foo" );
DeferredFileOutputStream s = new DeferredFileOutputStream( 1000000, "fds", "fdx", new File( "" ) );
s.write( "std-o\u00DCt<null>!".getBytes("UTF-8") );
String expected = "st]]>d-o\u00DCt<null>!";
s.write( expected.getBytes( "UTF-8" ) );
DeferredFileOutputStream s1 = new DeferredFileOutputStream( 1000000, "fds", "fdx", new File( "" ) );
byte[] bytes = "std-\u0115rr?&-&amp;&#163;".getBytes("UTF-8");
s1.write( bytes );
Expand Down Expand Up @@ -125,7 +126,7 @@ public void testAllFieldsSerialized()
assertNotNull( errorNode );
assertEquals( "A fud msg", errorNode.getAttribute( "message" ) );
assertEquals( "fail at foo", errorNode.getAttribute( "type" ) );
assertEquals( "std-o\u00DCt<null>!", tcb.getChild( "system-out" ).getValue() );
assertEquals( expected, tcb.getChild( "system-out" ).getValue() );
assertEquals( "std-\u0115rr?&-&amp;&#163;", tcb.getChild( "system-err" ).getValue() );

expectedReportFile.delete();
Expand Down

0 comments on commit 54c46be

Please sign in to comment.