[Httpunit-commit] CVS: httpunit/src/com/meterware/servletunit JUnitServlet.java,1.4,1.5
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2003-05-02 22:20:43
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/servletunit
In directory sc8-pr-cvs1:/tmp/cvs-serv14937/src/com/meterware/servletunit
Modified Files:
JUnitServlet.java
Log Message:
Added xml format for JUnitServlet
Index: JUnitServlet.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/JUnitServlet.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- JUnitServlet.java 2 Apr 2003 15:28:56 -0000 1.4
+++ JUnitServlet.java 2 May 2003 22:20:39 -0000 1.5
@@ -73,6 +73,8 @@
private ResultsFormatter getResultsFormatter( String formatterName ) {
if ("text".equalsIgnoreCase( formatterName )) {
return new TextResultsFormatter();
+ } else if ("xml".equalsIgnoreCase( formatterName )) {
+ return new XMLResultsFormatter();
} else {
return new HTMLResultsFormatter();
}
@@ -158,35 +160,96 @@
static abstract class ResultsFormatter {
- String getContentType() {
- return "text/html";
- }
+
+ private static final char LF = 10;
+ private static final char CR = 13;
+
+
+ abstract String getContentType();
void displayResults( PrintWriter writer, String testClassName, String elapsedTimeString, TestResult testResult ) {
+ displayHeader( writer, testClassName, testResult, elapsedTimeString );
+ displayResults( writer, testResult );
+ displayFooter( writer );
+ }
+
+
+ protected abstract void displayHeader( PrintWriter writer, String testClassName, TestResult testResult, String elapsedTimeString );
+
+
+ protected abstract void displayResults( PrintWriter writer, TestResult testResult );
+
+
+ protected abstract void displayFooter( PrintWriter writer );
+
+
+ protected String sgmlEscape( String s ) {
+ StringBuffer result = new StringBuffer( s.length() );
+ char[] chars = s.toCharArray();
+ for (int i = 0; i < chars.length; i++) {
+ switch( chars[i] ) {
+ case '&':
+ result.append( "&" );
+ break;
+ case '<':
+ result.append( "<" );
+ break;
+ case '>':
+ result.append( ">" );
+ break;
+ case LF:
+ if (i > 0 && chars[i-1] == CR) {
+ result.append( chars[i] );
+ break;
+ }
+ case CR:
+ result.append( getLineBreak() );
+ default:
+ result.append( chars[i] );
+ }
+ }
+ return result.toString();
+ }
+
+
+ protected String getLineBreak() {
+ return "<br>";
+ }
+ }
+
+
+ abstract static class DisplayedResultsFormatter extends ResultsFormatter {
+
+ protected void displayHeader( PrintWriter writer, String testClassName, TestResult testResult, String elapsedTimeString ) {
displayHeader( writer, testClassName, getFormatted( testResult.runCount(), "test" ),
elapsedTimeString, testResult.wasSuccessful() ? "OK" : "Problems Occurred" );
+ }
+ protected void displayResults( PrintWriter writer, TestResult testResult ) {
if (!testResult.wasSuccessful()) {
displayProblems( writer, "failure", testResult.failureCount(), testResult.failures() );
displayProblems( writer, "error", testResult.errorCount(), testResult.errors() );
}
- displayFooter( writer );
}
- private String getFormatted( int count, String name ) {
- return count + " " + name + (count == 1 ? "" : "s");
- }
+ protected abstract void displayHeader( PrintWriter writer, String testClassName, String testCountText, String elapsedTimeString, String resultString );
- protected abstract void displayHeader( PrintWriter writer, String testClassName, String testCountText, String elapsedTimeString, String resultString );
+ protected abstract void displayProblemTitle( PrintWriter writer, String title );
- protected abstract void displayFooter( PrintWriter writer );
+ protected abstract void displayProblemDetailHeader( PrintWriter writer, int i, String testName );
+
+
+ protected abstract void displayProblemDetailFooter( PrintWriter writer );
- protected void displayProblems( PrintWriter writer, String kind, int count, Enumeration enumeration ) {
+ protected abstract void displayProblemDetail( PrintWriter writer, String message );
+
+
+ private void displayProblems( PrintWriter writer, String kind, int count, Enumeration enumeration ) {
if (count != 0) {
displayProblemTitle( writer, getFormatted( count, kind ) );
Enumeration e = enumeration;
@@ -204,20 +267,15 @@
}
- protected abstract void displayProblemTitle( PrintWriter writer, String title );
-
-
- protected abstract void displayProblemDetailHeader( PrintWriter writer, int i, String testName );
-
-
- protected abstract void displayProblemDetailFooter( PrintWriter writer );
+ private String getFormatted( int count, String name ) {
+ return count + " " + name + (count == 1 ? "" : "s");
+ }
- protected abstract void displayProblemDetail( PrintWriter writer, String message );
}
- static class TextResultsFormatter extends ResultsFormatter {
+ static class TextResultsFormatter extends DisplayedResultsFormatter {
String getContentType() {
return "text/plain";
@@ -256,7 +314,11 @@
}
- static class HTMLResultsFormatter extends ResultsFormatter {
+ static class HTMLResultsFormatter extends DisplayedResultsFormatter {
+
+ String getContentType() {
+ return "text/html";
+ }
protected void displayHeader( PrintWriter writer, String testClassName, String testCountText,
@@ -297,41 +359,78 @@
protected void displayProblemDetail( PrintWriter writer, String message ) {
- writer.println( htmlEscape( message ) );
+ writer.println( sgmlEscape( message ) );
}
+ }
- private static final char LF = 10;
- private static final char CR = 13;
- private String htmlEscape( String s ) {
- StringBuffer result = new StringBuffer( s.length() );
- char[] chars = s.toCharArray();
- for (int i = 0; i < chars.length; i++) {
- switch( chars[i] ) {
- case '&':
- result.append( "&" );
- break;
- case '<':
- result.append( "<" );
- break;
- case '>':
- result.append( ">" );
- break;
- case LF:
- if (i > 0 && chars[i-1] == CR) {
- result.append( chars[i] );
- break;
- }
- case CR:
- result.append( "<br>" );
- default:
- result.append( chars[i] );
+ static class XMLResultsFormatter extends ResultsFormatter {
+
+ String getContentType() {
+ return "text/xml;charset=UTF-8";
+ }
+
+
+ protected void displayHeader( PrintWriter writer, String testClassName, TestResult testResult, String elapsedTimeString ) {
+ writer.println( "<?xml version='1.0' encoding='UTF-8' ?>\n" +
+ "<testsuite name=" + asAttribute( testClassName ) +
+ " tests=" + asAttribute( testResult.runCount() ) +
+ " failures=" + asAttribute( testResult.failureCount() ) +
+ " errors=" + asAttribute( testResult.errorCount() ) +
+ " time=" + asAttribute( elapsedTimeString ) + ">" );
+ }
+
+
+ private String asAttribute( int value ) {
+ return '"' + Integer.toString( value ) + '"';
+ }
+
+
+ private String asAttribute( String value ) {
+ return '"' + sgmlEscape( value ) + '"';
+ }
+
+
+ protected void displayFooter( PrintWriter writer ) {
+ writer.println( "</testsuite>" );
+ }
+
+
+ protected void displayResults( PrintWriter writer, TestResult testResult ) {
+ displayResults( writer, "failure", testResult.failures() );
+ displayResults( writer, "error", testResult.errors() );
+ }
+
+
+ private void displayResults( PrintWriter writer, String failureNodeName, Enumeration resultsEnumeration ) {
+ for (Enumeration e = resultsEnumeration; e.hasMoreElements();) {
+ TestFailure failure = (TestFailure) e.nextElement();
+ writer.println( " <testcase name=" + asAttribute( failure.failedTest().toString() ) + ">" );
+ writer.print( " <" + failureNodeName + " type=" + asAttribute( failure.thrownException().getClass().getName() ) +
+ " message=" + asAttribute( failure.exceptionMessage() ) );
+ if (!displayException( failure )) {
+ writer.println( "/>" );
+ } else {
+ writer.println( ">" );
+ writer.print( sgmlEscape( BaseTestRunner.getFilteredTrace( failure.thrownException() ) ) );
+ writer.println( " </" + failureNodeName + ">" );
}
+ writer.println( " </testcase>" );
}
- return result.toString();
+ }
+
+
+ private boolean displayException( TestFailure failure ) {
+ return !failure.isFailure();
+ }
+
+
+ protected String getLineBreak() {
+ return "";
}
}
+
}
|