maven-surefire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dfabul...@apache.org
Subject svn commit: r620660 - in /maven/surefire/trunk: surefire-api/src/main/java/org/apache/maven/surefire/report/ surefire-api/src/main/java/org/apache/maven/surefire/util/ surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/
Date Mon, 11 Feb 2008 22:56:08 GMT
Author: dfabulich
Date: Mon Feb 11 14:56:07 2008
New Revision: 620660

URL: http://svn.apache.org/viewvc?rev=620660&view=rev
Log:
[SUREFIRE-455] XML delimiter characters in attribute and text content are escaped twice. 
I also added a test case and tweaked this patch to deliberately introduce SUREFIRE-456 to
handle null characters.
Submitted by: Todor Todorov
Reviewed by: Dan Fabulich

Modified:
    maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/XMLReporter.java
    maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/PrettyPrintXMLWriter.java
    maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/Surefire224WellFormedXmlFailuresTest.java

Modified: maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/XMLReporter.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/XMLReporter.java?rev=620660&r1=620659&r2=620660&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/XMLReporter.java
(original)
+++ maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/XMLReporter.java
Mon Feb 11 14:56:07 2008
@@ -218,9 +218,9 @@
 
             String message = t.getMessage();
 
-            if ( message != null && message.trim().length() > 0 )
+            if ( message != null )
             {
-                element.setAttribute( "message", escapeAttribute( message ) );
+                element.setAttribute( "message", message );
 
                 element.setAttribute( "type", ( stackTrace.indexOf( ":" ) > -1 ? stackTrace.substring(
0,
                                                                                         
              stackTrace.indexOf(
@@ -235,7 +235,7 @@
 
         if (stackTrace != null)
         {
-            element.setValue( escapeAttribute(stackTrace) );
+            element.setValue( stackTrace );
         }
 
         addOutputStreamElement( stdOut, "system-out", testCase );
@@ -292,35 +292,10 @@
 
                 property.setAttribute( "name", key );
 
-                property.setAttribute( "value", escapeAttribute( value ) );
+                property.setAttribute( "value", value );
 
             }
         }
-    }
-
-    private static String escapeAttribute( String attribute )
-    {
-        // Shouldn't Xpp3Dom do this itself?
-        StringBuffer sb = new StringBuffer ( attribute.length() * 2 );
-        for (int i = 0; i < attribute.length(); i++ ) {
-            char c = attribute.charAt( i );
-            if ( c < 32 ) {
-                if ( c == '\n' || c == '\r' || c == '\t') {
-                    sb.append( c );
-                } else {
-                    sb.append( "&#" ).append( (int) c).append( ';' );
-                }
-            } else if ( c == '<') {
-                sb.append( "&lt;" );
-            } else if ( c == '>') {
-                sb.append( "&gt;" );
-            }
-            else {
-                sb.append( c );
-            }
-        }
-        return sb.toString();
-
     }
 
     public Iterator getResults()

Modified: maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/PrettyPrintXMLWriter.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/PrettyPrintXMLWriter.java?rev=620660&r1=620659&r2=620660&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/PrettyPrintXMLWriter.java
(original)
+++ maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/PrettyPrintXMLWriter.java
Mon Feb 11 14:56:07 2008
@@ -147,13 +147,35 @@
 
     private static String escapeXml( String text )
     {
-        text = StringUtils.replace( text, "&", "&amp;" );
-        text = StringUtils.replace( text, "<", "&amp;" );
-        text = StringUtils.replace( text, ">", "&amp;" );
-        text = StringUtils.replace( text, "\"", "&quot;" );
-        text = StringUtils.replace( text, "\'", "&apos;" );
-
-        return text;
+        StringBuffer sb = new StringBuffer ( text.length() * 2 );
+        for (int i = 0; i < text.length(); i++ ) {
+            char c = text.charAt( i );
+            if ( c < 32 ) {
+                if ( c == '\n' || c == '\r' || c == '\t') {
+                    sb.append( c );
+                } else {
+                    // uh-oh!  This character is illegal in XML 1.0!
+                    // http://www.w3.org/TR/1998/REC-xml-19980210#charsets
+                    // we're going to deliberately doubly-XML escape it...
+                    // there's nothing better we can do! :-(
+                    // SUREFIRE-456
+                    sb.append( "&amp;#" ).append( (int) c).append( ';' );
+                }
+            } else if ( c == '<') {
+                sb.append( "&lt;" );
+            } else if ( c == '>') {
+                sb.append( "&gt;" );
+            } else if (c == '&') {
+                sb.append("&amp;");
+            } else if (c == '"') {
+                sb.append("&quot;");
+            } else if (c == '\'') {
+                sb.append("&apos;");
+            } else {
+                sb.append( c );
+            }
+        }
+        return sb.toString();
     }
 
     public void addAttribute( String key, String value )

Modified: maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/Surefire224WellFormedXmlFailuresTest.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/Surefire224WellFormedXmlFailuresTest.java?rev=620660&r1=620659&r2=620660&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/Surefire224WellFormedXmlFailuresTest.java
(original)
+++ maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/Surefire224WellFormedXmlFailuresTest.java
Mon Feb 11 14:56:07 2008
@@ -4,8 +4,11 @@
 import junit.framework.TestCase;
 import org.apache.maven.it.Verifier;
 import org.apache.maven.it.util.ResourceExtractor;
+import org.apache.maven.plugins.surefire.report.ReportTestCase;
+import org.apache.maven.plugins.surefire.report.ReportTestSuite;
 
 import java.io.File;
+import java.util.List;
 
 /**
  * Test Surefire-224 (XML test reports are not well-formed when failure message contains
quotes)
@@ -29,5 +32,22 @@
         
         HelperAssertions.assertTestSuiteResults( 4, 0, 4, 0, testDir );
         
+        ReportTestSuite suite = (ReportTestSuite) HelperAssertions.extractReports( (new File[]
{ testDir }) ).get( 0 );
+        List testCases = suite.getTestCases();
+        assertEquals( "Wrong number of test case objects", 4, testCases.size() );
+        ReportTestCase current, testQuote = null, testLower = null, testGreater = null, testU0000
= null;
+        for ( int i = 0; i < testCases.size(); i++ )
+        {
+            current = (ReportTestCase) testCases.get( i );
+            if ( "testQuote".equals( current.getName() ) ) testQuote = current;
+            else if ( "testLower".equals( current.getName() ) ) testLower = current;
+            else if ( "testGreater".equals( current.getName() ) ) testGreater = current;
+            else if ( "testU0000".equals( current.getName() ) ) testU0000 = current;
+        }
+        assertEquals("Wrong error message", "\"", testQuote.getFailure().get( "message" )
);
+        assertEquals("Wrong error message", "<", testLower.getFailure().get( "message"
) );
+        assertEquals("Wrong error message", ">", testGreater.getFailure().get( "message"
) );
+        // SUREFIRE-456 we have to doubly-escape non-visible control characters like \u0000
+        assertEquals("Wrong error message", "&#0;", testU0000.getFailure().get( "message"
) );
     }
 }



Mime
View raw message