KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > test > svg > SelfContainedSVGOnLoadTestValidator


1 /*
2
3    Copyright 2002-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  */
package org.apache.batik.test.svg;
18
19 import java.util.Vector JavaDoc;
20
21 import org.apache.batik.test.TestReport;
22
23 import org.apache.batik.test.AbstractTest;
24 import org.apache.batik.test.DefaultTestSuite;
25
26 /**
27  * This test validates the operation of SelfContainedSVGOnLoadTest.
28  * It is a test agregation which points to several tests which
29  * correspond to expected error conditions. The test simply
30  * validates that the expected error conditions are properly
31  * detected and reported.
32  *
33  * @author <a HREF="mailto:vhardy@apache.org">Vincent Hardy</a>
34  * @version $Id: SelfContainedSVGOnLoadTestValidator.java,v 1.4 2004/08/18 07:17:03 vhardy Exp $
35  */

36 public class SelfContainedSVGOnLoadTestValidator extends DefaultTestSuite {
37     /**
38      * Error codes
39      */

40     public static final String JavaDoc ERROR_UNEXPECTED_TEST_RESULT =
41         "SelfContainedSVGOnLoadTestValidator.error.unexpected.test.result";
42
43     public static final String JavaDoc ERROR_UNEXPECTED_ERROR_CODE =
44         "SelfContainedSVGOnLoadTestValidator.error.unexpected.error.code";
45
46     public static final String JavaDoc ERROR_UNEXPECTED_NUMBER_OF_DESCRIPTION_ENTRIES
47         = "SelfContainedSVGOnLoadTestValidator.error.unexpected.number.of.description.entries";
48
49     public static final String JavaDoc ERROR_UNEXPECTED_TEST_FAILURE
50         = "SelfContainedSVGOnLoadTestValidator.error.unexpected.test.failure";
51
52     public static final String JavaDoc ERROR_UNEXPECTED_DESCRIPTION_ENTRY
53         = "SelfContainedSVGOnLoadTestValidator.error.unexpected.description.entry";
54
55     public static final String JavaDoc ENTRY_ERROR_CODE =
56         "SelfContainedSVGOnLoadTestValidator.entry.error.code";
57
58     public static final String JavaDoc ENTRY_EXPECTED_ERROR_CODE =
59         "SelfContainedSVGOnLoadTestValidator.entry.expected.error.code";
60
61     public static final String JavaDoc ENTRY_NUMBER_OF_DESCRIPTION =
62         "SelfContainedSVGOnLoadTestValidator.entry.number.of.description";
63
64     public static final String JavaDoc ENTRY_EXPECTED_NUMBER_OF_DESCRIPTION =
65         "SelfContainedSVGOnLoadTestValidator.entry.expected.number.of.description";
66
67     public static final String JavaDoc ENTRY_KEY =
68         "SelfContainedSVGOnLoadTestValidator.entry.key";
69
70     public static final String JavaDoc ENTRY_EXPECTED_KEY =
71         "SelfContainedSVGOnLoadTestValidator.entry.expected.key";
72
73     
74     /**
75      * URLs for children tests
76      */

77     public static final String JavaDoc invalidURL
78         = "invalidURL";
79     public static final String JavaDoc processingErrorURL
80         = "test-resources/org/apache/batik/test/svg/processingError.svg";
81     public static final String JavaDoc invalidTestResultElementsNumberURL
82         = "test-resources/org/apache/batik/test/svg/invalidTestResultElementsNumber.svg";
83     public static final String JavaDoc unexpectedResultValueURL
84         = "test-resources/org/apache/batik/test/svg/unexpectedResultValue.svg";
85     public static final String JavaDoc missingOrEmptyErrorCodeURL
86         = "test-resources/org/apache/batik/test/svg/missingOrEmptyErrorCode.svg";
87     public static final String JavaDoc errorURL
88         = "test-resources/org/apache/batik/test/svg/error.svg";
89     public static final String JavaDoc errorAndEntriesURL
90         = "test-resources/org/apache/batik/test/svg/errorAndEntries.svg";
91     public static final String JavaDoc successURL
92         = "test-resources/org/apache/batik/test/svg/success.svg";
93
94     
95     public SelfContainedSVGOnLoadTestValidator(){
96         addTest(new CannotLoadSVGDocument());
97         addTest(new ProcessingError());
98         addTest(new InvalidTestResultElementsNumber());
99         addTest(new UnexpectedResultValue());
100         addTest(new MissingOrEmptyErrorCode());
101         addTest(new ReportError());
102         addTest(new ReportSuccess());
103         addTest(new ReportErrorAndEntries());
104     }
105
106     static class ReportErrorAndEntries extends DefaultErrorTest {
107         public ReportErrorAndEntries() {
108             super(errorAndEntriesURL,
109                   "can.you.read.this.error.code",
110                   new String JavaDoc[] { "can.you.read.this.first.entry",
111                                  "can.you.read.this.second.entry" });
112         }
113     }
114
115     static class ReportError extends DefaultErrorTest {
116         public ReportError(){
117             super(errorURL,
118                    "can.you.read.this.error.code",
119                    null);
120         }
121     }
122
123     static class ReportSuccess extends AbstractTest {
124         public TestReport rumImpl() throws Exception JavaDoc {
125             SelfContainedSVGOnLoadTest t = new SelfContainedSVGOnLoadTest(successURL);
126             TestReport tr = t.run();
127
128             if (!tr.hasPassed()) {
129                 return reportError(ERROR_UNEXPECTED_TEST_FAILURE);
130             }
131
132             return reportSuccess();
133         }
134     }
135     
136     static class MissingOrEmptyErrorCode extends DefaultErrorTest {
137         public MissingOrEmptyErrorCode(){
138             super(missingOrEmptyErrorCodeURL,
139                   SelfContainedSVGOnLoadTest.ERROR_MISSING_OR_EMPTY_ERROR_CODE_ON_FAILED_TEST,
140                   null);
141         }
142     }
143
144     static class UnexpectedResultValue extends DefaultErrorTest{
145         public UnexpectedResultValue(){
146             super(unexpectedResultValueURL,
147                   SelfContainedSVGOnLoadTest.ERROR_UNEXPECTED_RESULT_VALUE,
148                   new String JavaDoc[]{SelfContainedSVGOnLoadTest.ENTRY_KEY_RESULT_VALUE});
149         }
150     }
151
152
153     static class InvalidTestResultElementsNumber extends DefaultErrorTest{
154         public InvalidTestResultElementsNumber(){
155             super(invalidTestResultElementsNumberURL,
156                   SelfContainedSVGOnLoadTest.ERROR_UNEXPECTED_NUMBER_OF_TEST_RESULT_ELEMENTS,
157                   new String JavaDoc[]{SelfContainedSVGOnLoadTest.ENTRY_KEY_NUMBER_OF_TEST_RESULT_ELEMENTS});
158         }
159     }
160
161     static class ProcessingError extends DefaultErrorTest{
162         public ProcessingError(){
163             super(processingErrorURL,
164                   SelfContainedSVGOnLoadTest.ERROR_WHILE_PROCESSING_SVG_DOCUMENT,
165                   new String JavaDoc[]{SelfContainedSVGOnLoadTest.ENTRY_KEY_ERROR_DESCRIPTION});
166         }
167     }
168
169     static class CannotLoadSVGDocument extends DefaultErrorTest {
170         public CannotLoadSVGDocument(){
171             super(invalidURL,
172                   SelfContainedSVGOnLoadTest.ERROR_CANNOT_LOAD_SVG_DOCUMENT,
173                   new String JavaDoc[] {SelfContainedSVGOnLoadTest.ENTRY_KEY_ERROR_DESCRIPTION});
174         }
175     }
176     
177     static class DefaultErrorTest extends AbstractTest{
178         String JavaDoc svgURL;
179         String JavaDoc expectedErrorCode;
180         String JavaDoc[] expectedEntryCodes;
181         
182         public DefaultErrorTest(String JavaDoc svgURL,
183                                 String JavaDoc expectedErrorCode,
184                                 String JavaDoc[] expectedEntryCodes
185                                 ){
186             this.svgURL = svgURL;
187             this.expectedErrorCode = expectedErrorCode;
188             this.expectedEntryCodes = expectedEntryCodes;
189         }
190         
191         public TestReport runImpl() throws Exception JavaDoc {
192             SelfContainedSVGOnLoadTest t
193                 = new SelfContainedSVGOnLoadTest(svgURL);
194             TestReport tr = t.run();
195             
196             if(tr.hasPassed()){
197                 return reportError(ERROR_UNEXPECTED_TEST_RESULT);
198             }
199             
200             if(tr.getErrorCode() != expectedErrorCode){
201                 TestReport r = reportError(ERROR_UNEXPECTED_ERROR_CODE);
202                 r.addDescriptionEntry(ENTRY_ERROR_CODE, tr.getErrorCode());
203                 r.addDescriptionEntry(ENTRY_EXPECTED_ERROR_CODE,
204                                        expectedErrorCode);
205                 return r;
206             }
207             
208             // Check that there is a description entry
209
TestReport.Entry[] desc = tr.getDescription();
210             int nDesc = 0, enDesc = 0;
211             if (desc != null){
212                 nDesc = desc.length;
213             }
214             if (expectedEntryCodes != null){
215                 enDesc = expectedEntryCodes.length;
216             }
217             
218             if (nDesc != enDesc){
219                 TestReport r = reportError(ERROR_UNEXPECTED_NUMBER_OF_DESCRIPTION_ENTRIES);
220                 
221                 r.addDescriptionEntry(ENTRY_NUMBER_OF_DESCRIPTION,
222                                       "" + nDesc);
223                 r.addDescriptionEntry(ENTRY_EXPECTED_NUMBER_OF_DESCRIPTION,
224                                       "" + enDesc);
225                 return r;
226             }
227             
228             if (nDesc > 0){
229                 Vector JavaDoc veDesc = new Vector JavaDoc();
230                 for(int i=0; i<nDesc; i++){
231                     veDesc.add(expectedEntryCodes[i]);
232                 }
233
234                 for(int i=0; i<nDesc; i++){
235                     String JavaDoc key = desc[i].getKey();
236                     if (key == null || !veDesc.contains(key)){
237                         TestReport r = reportError(ERROR_UNEXPECTED_DESCRIPTION_ENTRY);
238                         if (key != null){
239                             r.addDescriptionEntry(ENTRY_KEY, key);
240                             r.addDescriptionEntry(ENTRY_EXPECTED_KEY,
241                               SelfContainedSVGOnLoadTest.ENTRY_KEY_NUMBER_OF_TEST_RESULT_ELEMENTS);
242                         }
243                         return r;
244                     }
245                 }
246             }
247             
248             return reportSuccess();
249         }
250     }
251     
252 }
253
254
Popular Tags