KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 2001 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 /**
21  * This <tt>Test</tt> implementation can be used to validate the
22  * operation of a specific test. A typical use is to create
23  * known error conditions and check that the input <tt>Test</tt>
24  * reports these errors properly.
25  * <p>
26  * This test checks that a given test status (passed or not)
27  * and a given error code is returned by a <tt>Test</tt>.
28  * A <tt>TestReportValidator</tt> is built with the <tt>Test</tt> to
29  * run, the expected status (passed or failed) and the expected
30  * error code. The <tt>TestReportValidator</tt> will pass if the
31  * expected values are produced by the <tt>TestReport</tt>
32  * created by the associated <tt>Test</tt>. Otherwise, it will
33  * fail with one of two error codes:<br />
34  * + if the status is not the one expected, then the
35  * ERROR_UNEXPECTED_TEST_STATUS code is used.. The report
36  * description will have two entries: ENTRY_KEY_EXPECTED_STATUS
37  * and ENTRY_KEY_RECEIVED_STATUS, both of which are Strings.
38  * + if the status is the one expected, but if the error code
39  * differs from the expected one, then the
40  * ERROR_UNEXPECTED_ERROR_CODE code is used. The report
41  * description will have two entries: ENTRY_KEY_EXPECTED_ERROR_CODE
42  * and ENTRY_KEY_RECEIVED_ERROR_CODE.
43  *
44  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
45  * @version $Id: TestReportValidator.java,v 1.4 2004/08/18 07:16:58 vhardy Exp $
46  */

47 public class TestReportValidator extends AbstractTest {
48     /**
49      * Test that this validator checks
50      */

51     private Test test;
52     
53     /**
54      * Status expected from the <tt>TestReport</tt>
55      */

56     private boolean expectedStatus;
57
58     /**
59      * Error code expected from the <tt>TestReport</tt>
60      */

61     private String JavaDoc expectedErrorCode;
62
63     /**
64      * Error code used when the test status is
65      * is different from the expected status.
66      */

67     static final String JavaDoc ERROR_UNEXPECTED_TEST_STATUS
68         = "TestReportValidator.error.unexpected.test.status";
69     
70     /**
71      * Error code used when the test error code is
72      * different from the expected error code.
73      */

74     static final String JavaDoc ERROR_UNEXPECTED_ERROR_CODE
75         = "TestReportValidator.error.unexpected.error.code";
76
77     /**
78      * The error description entry when the test fails
79      */

80     public static final String JavaDoc ENTRY_KEY_EXPECTED_ERROR_CODE
81         = "TestReportValidator.entry.key.expected.error.code";
82     
83     /**
84      * Entry describing the received error code which is
85      * different from the expected one.
86      */

87     public static final String JavaDoc ENTRY_KEY_RECEIVED_ERROR_CODE
88         = "TestReportValidator.entry.key.received.error.code";
89     
90     /**
91      * The entry describing the expected status when the test status
92      * is unexpected.
93      */

94     public static final String JavaDoc ENTRY_KEY_EXPECTED_STATUS
95         = "TestReportValidator.entry.key.expected.status";
96     
97     /**
98      * Entry describing the received status which is
99      * different from the expected one.
100      */

101     public static final String JavaDoc ENTRY_KEY_RECEIVED_STATUS
102         = "TestReportValidator.entry.key.received.status";
103     
104     /**
105      * Constructor
106      *
107      */

108     public TestReportValidator(Test test,
109                                boolean expectedStatus,
110                                String JavaDoc expectedErrorCode){
111         setConfig(test,
112                   expectedStatus,
113                   expectedErrorCode);
114     }
115
116     /**
117      * Protected constructor, for use by derived classes
118      */

119     protected TestReportValidator(){
120     }
121
122     /**
123      * Lets derived classes set the configuration parameters
124      * for this test.
125      */

126     protected void setConfig(Test test,
127                              boolean expectedStatus,
128                              String JavaDoc expectedErrorCode){
129         this.expectedErrorCode = expectedErrorCode;
130         this.test = test;
131         this.expectedStatus = expectedStatus;
132     }
133
134     public TestReport runImpl() throws Exception JavaDoc {
135         TestReport tr = test.run();
136         
137         //
138
// Check test output
139
//
140
DefaultTestReport r = new DefaultTestReport(this);
141         
142         if( tr.hasPassed() != expectedStatus ){
143             TestReport.Entry expectedStatusEntry
144                 = new TestReport.Entry(Messages.formatMessage(ENTRY_KEY_EXPECTED_STATUS, null),
145                                        (new Boolean JavaDoc(expectedStatus)).toString());
146             TestReport.Entry receivedStatusEntry
147                 = new TestReport.Entry(Messages.formatMessage(ENTRY_KEY_RECEIVED_STATUS, null),
148                                        (new Boolean JavaDoc(tr.hasPassed())).toString());
149             r.setDescription(new TestReport.Entry[]{ expectedStatusEntry, receivedStatusEntry });
150             r.setErrorCode(ERROR_UNEXPECTED_TEST_STATUS);
151             r.setPassed(false);
152         }
153         else if( tr.getErrorCode() != expectedErrorCode ){
154             TestReport.Entry expectedErrorCodeEntry
155                 = new TestReport.Entry(Messages.formatMessage(ENTRY_KEY_EXPECTED_ERROR_CODE, null),
156                                        expectedErrorCode);
157             TestReport.Entry receivedErrorCodeEntry
158                 = new TestReport.Entry(Messages.formatMessage(ENTRY_KEY_RECEIVED_ERROR_CODE, null),
159                                        tr.getErrorCode());
160             
161             r.setDescription(new TestReport.Entry[]{ expectedErrorCodeEntry, receivedErrorCodeEntry });
162             r.setErrorCode(ERROR_UNEXPECTED_ERROR_CODE);
163             r.setPassed(false);
164         }
165         
166         return r;
167     }
168 }
169
Popular Tags