KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > test > SimpleTestReportProcessor


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.test;
19
20 import java.io.OutputStreamWriter JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.io.StringWriter JavaDoc;
23
24 /**
25  * A simple implementation of the <tt>TestReportProcessor</tt> interface
26  * that prints out the <tt>TestReport</tt> to the standard output.
27  *
28  * @author <a HREF="mailto:vhardy@apache.org">Vincent Hardy</a>
29  * @version $Id: SimpleTestReportProcessor.java,v 1.4 2004/08/18 07:16:57 vhardy Exp $
30  */

31 public class SimpleTestReportProcessor implements TestReportProcessor {
32     /**
33      * Message keys
34      */

35     public static final String JavaDoc MESSAGES_TEST_SUITE_STATUS_TEST_PASSED
36         = "SimpleTestReportProcessor.messages.test.suite.status.testPassed";
37
38     public static final String JavaDoc MESSAGES_TEST_SUITE_STATUS_TEST_FAILED
39         = "SimpleTestReportProcessor.messages.test.suite.status.testFailed";
40
41     public static final String JavaDoc MESSAGES_TEST_SUITE_STATUS
42         = "SimpleTestReportProcessor.messages.test.suite.status";
43
44     public static final String JavaDoc MESSAGES_TEST_SUITE_ERROR_CODE
45         = "SimpleTestReportProcessor.messages.test.suite.error.code";
46
47     /**
48      * Default output writer
49      */

50     private PrintWriter JavaDoc printWriter;
51
52     /**
53      * Sets the <tt>PrintWriter</tt> this processor should use
54      */

55     public void setPrintWriter(PrintWriter JavaDoc printWriter){
56         this.printWriter = printWriter;
57     }
58
59     /**
60      * Recursively prints out the entries of the input
61      * report and its children reports, if any.
62      */

63     public void processReport(TestReport report)
64         throws TestException{
65         try{
66             PrintWriter JavaDoc out = printWriter;
67             if(printWriter == null){
68                 out = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(System.out));
69             }
70             processReport(report, "", out);
71             out.flush();
72         }catch(Exception JavaDoc e){
73             StringWriter JavaDoc sw = new StringWriter JavaDoc();
74             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
75             e.printStackTrace(pw);
76             throw new TestException(INTERNAL_ERROR,
77                                     new Object JavaDoc[] { e.getClass().getName(),
78                                                    e.getMessage(),
79                                                    sw.toString() },
80                                     e);
81         }
82     }
83
84     /**
85      * Prints out the input report, prefixing all output
86      * with the input string
87      */

88     public void processReport(TestReport report, String JavaDoc prefix, PrintWriter JavaDoc out){
89         String JavaDoc status = report.hasPassed()
90             ? Messages.formatMessage(MESSAGES_TEST_SUITE_STATUS_TEST_PASSED, null)
91             : Messages.formatMessage(MESSAGES_TEST_SUITE_STATUS_TEST_FAILED, null);
92
93         out.println(Messages.formatMessage(MESSAGES_TEST_SUITE_STATUS,
94                                                   new Object JavaDoc[]{ report.getTest().getName(),
95                                                                 status }));
96
97         if(!report.hasPassed()){
98             out.println(Messages.formatMessage(MESSAGES_TEST_SUITE_ERROR_CODE,
99                                                       new Object JavaDoc[]{report.getErrorCode()}));
100         }
101         
102         TestReport.Entry[] entries = report.getDescription();
103         int n = entries != null ? entries.length : 0;
104         for(int i=0; i<n; i++){
105             out.print(prefix + entries[i].getKey() + " : " );
106             printValue(entries[i].getValue(), prefix + " ", out);
107         }
108     }
109
110     /**
111      * Prints out the input value depending on its
112      * type.
113      */

114     protected void printValue(Object JavaDoc value, String JavaDoc prefix, PrintWriter JavaDoc out){
115         if(!(value instanceof TestReport)){
116             out.println(value);
117         }
118         else{
119             out.println();
120             processReport((TestReport)value, prefix, out);
121         }
122     }
123 }
124
125
126
Popular Tags