KickJava   Java API By Example, From Geeks To Geeks.

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


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.svg;
19
20 import java.io.File JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.FileWriter JavaDoc;
23 import java.io.StringReader JavaDoc;
24 import java.net.URL JavaDoc;
25
26 import org.apache.batik.test.AbstractTest;
27 import org.apache.batik.test.DefaultTestSuite;
28 import org.apache.batik.test.Test;
29 import org.apache.batik.test.TestReport;
30 import org.apache.batik.test.TestReportValidator;
31 import org.apache.batik.transcoder.TranscoderInput;
32 import org.apache.batik.transcoder.TranscoderOutput;
33 import org.apache.batik.transcoder.image.ImageTranscoder;
34 import org.apache.batik.transcoder.image.PNGTranscoder;
35
36 /**
37  * Validates the operation of <tt>SVGRenderingAccuracyTest</tt>
38  * by forcing specific test case situations and checking that
39  * they are handled properly by the class.
40  *
41  * @author <a HREF="mailto:vhardy@apache.org">Vincent Hardy</a>
42  * @version $Id: SVGRenderingAccuracyTestValidator.java,v 1.9 2004/08/18 07:17:03 vhardy Exp $
43  */

44 public class SVGRenderingAccuracyTestValidator extends DefaultTestSuite {
45     /**
46      * Simple valid SVG content used for this test
47      */

48     private static final String JavaDoc validSVG
49         = "<svg width=\"450\" height=\"500\" viewBox=\"0 0 450 500\"> \n" +
50         " <rect x=\"25\" y=\"25\" width=\"400\" height=\"450\" fill=\"blue\" /> \n" +
51         "</svg>\n";
52
53     /**
54      * Simple valid SVG content used for this test
55      */

56     private static final String JavaDoc validSVGVariation
57         = "<svg width=\"450\" height=\"500\" viewBox=\"0 0 450 500\"> \n" +
58         " <rect x=\"25\" y=\"25\" width=\"400\" height=\"450\" fill=\"#0000cc\" /> \n" +
59         "</svg>\n";
60
61     /**
62      * Simple valid SVG content used for this test, small size
63      */

64     private static final String JavaDoc validSmallSVG
65         = "<svg width=\"45\" height=\"50\" viewBox=\"0 0 45 50\"> \n" +
66         " <rect x=\"2.5\" y=\"2.5\" width=\"40\" height=\"45\" fill=\"blue\" /> \n" +
67         "</svg>\n";
68
69     /**
70      * Simple valid SVG content used for this test, red rectangle
71      */

72     private static final String JavaDoc validRedSVG
73         = "<svg width=\"450\" height=\"500\" viewBox=\"0 0 450 500\"> \n" +
74         " <rect x=\"25\" y=\"25\" width=\"400\" height=\"450\" fill=\"red\" /> \n" +
75         "</svg>\n";
76
77     /**
78      * Simple invalid SVG content used for this test
79      * (the error is that a double quote is missing at the
80      * end of the width attribute value.
81      */

82     private static final String JavaDoc invalidSVG
83         = "<svg width=\"450 height=\"500\" viewBox=\"0 0 450 500\"> \n" +
84         " <rect x=\"25\" y=\"25\" width=\"400\" height=\"450\" fill=\"blue\" /> \n" +
85         "</svg>\n";
86
87     /**
88      * This test creates a sample SVG file dynamically and
89      * valides that errors are generated for the
90      * following cases:
91      *
92      * + invalid SVG URL
93      * + corrupted SVG image (i.e., cannot be transcoded to
94      * an image).
95      * + invalid reference image URL
96      * + valid reference image URL, but inexisting resource.
97      * + reference image of different size than generated
98      * image.
99      * + reference image different than the generated image
100      *
101      * This test finally validates that the test
102      * passes if the reference image corresponds to the
103      * newly generated image.
104      */

105     public SVGRenderingAccuracyTestValidator(){
106         addTest(new InvalidSVGURL());
107         addTest(new InvalidSVGContent());
108         addTest(new InvalidReferenceImageURL());
109         addTest(new InexistingReferenceImage());
110         addTest(new DifferentSizes());
111         addTest(new SameSizeDifferentContent());
112         addTest(new AccurateRendering());
113         addTest(new AccurateRenderingWithVariation());
114         addTest(new DefaultConfigTest());
115     }
116     
117     /**
118      * Creates a SVG source URL for the given svg content
119      */

120     public static URL JavaDoc createSVGSourceURL(String JavaDoc svgContent) throws Exception JavaDoc{
121         File JavaDoc tmpFile = File.createTempFile(SVGRenderingAccuracyTest.TEMP_FILE_PREFIX,
122                                            null);
123         FileWriter JavaDoc writer = new FileWriter JavaDoc(tmpFile);
124         writer.write(svgContent);
125         writer.close();
126         return tmpFile.toURL();
127     }
128
129     /**
130      * Helper method: creates a valid reference image
131      */

132     public static URL JavaDoc createValidReferenceImage(String JavaDoc svgContent) throws Exception JavaDoc{
133         TranscoderInput validSrc = new TranscoderInput(new StringReader JavaDoc(svgContent));
134         
135         File JavaDoc tmpFile = File.createTempFile(SVGRenderingAccuracyTest.TEMP_FILE_PREFIX,
136                                            SVGRenderingAccuracyTest.TEMP_FILE_SUFFIX);
137         
138         TranscoderOutput validDst
139             = new TranscoderOutput(new FileOutputStream JavaDoc(tmpFile));
140         
141         ImageTranscoder transcoder
142             = new PNGTranscoder();
143         
144         transcoder.transcode(validSrc, validDst);
145         
146         tmpFile.deleteOnExit();
147         
148         return tmpFile.toURL();
149     }
150
151
152     /**
153      * Validates that the default parameters computation is
154      * working as expected.
155      */

156     static class DefaultConfigTest extends AbstractTest {
157         String JavaDoc svgURL = "samples/anne.svg";
158         String JavaDoc expectedRefImgURL = "test-references/samples/anne.png";
159         String JavaDoc expectedVariationURL = "test-references/samples/accepted-variation/anne.png";
160         String JavaDoc expectedCandidateURL = "test-references/samples/candidate-variation/anne.png";
161
162         String JavaDoc ERROR_EXCEPTION_WHILE_BUILDING_TEST
163             = "error.exception.while.building.test";
164
165         String JavaDoc ERROR_UNEXPECTED_REFERENCE_IMAGE_URL
166             = "error.unexpected.reference.image.url";
167
168         String JavaDoc ERROR_UNEXPECTED_VARIATION_URL
169             = "error.unexpected.variation.url";
170
171         String JavaDoc ERROR_UNEXPECTED_CANDIDATE_URL
172             = "error.unexpected.candidate.url";
173
174         String JavaDoc ENTRY_KEY_EXPECTED_VALUE
175             = "entry.key.expected.value";
176
177         String JavaDoc ENTRY_KEY_FOUND_VALUE
178             = "entry.key.found.value";
179
180         public DefaultConfigTest(){
181             super();
182             setId("defaultTest");
183         }
184
185         public TestReport runImpl() throws Exception JavaDoc {
186             SVGRenderingAccuracyTest t
187                 = new SamplesRenderingTest();
188
189             t.setId(svgURL);
190
191             if(!t.refImgURL.toString().endsWith(expectedRefImgURL)){
192                 TestReport r = reportError(ERROR_UNEXPECTED_REFERENCE_IMAGE_URL);
193                 r.addDescriptionEntry(ENTRY_KEY_EXPECTED_VALUE, expectedRefImgURL);
194                 r.addDescriptionEntry(ENTRY_KEY_FOUND_VALUE, "" + t.refImgURL);
195                 return r;
196             }
197
198             if(!t.variationURL.toString().endsWith(expectedVariationURL)){
199                 TestReport r = reportError(ERROR_UNEXPECTED_VARIATION_URL);
200                 r.addDescriptionEntry(ENTRY_KEY_EXPECTED_VALUE, expectedVariationURL);
201                 r.addDescriptionEntry(ENTRY_KEY_FOUND_VALUE, "" + t.variationURL);
202                 return r;
203             }
204
205             if(!t.saveVariation.toURL().toString().endsWith(expectedCandidateURL)){
206                 TestReport r = reportError(ERROR_UNEXPECTED_CANDIDATE_URL);
207                 r.addDescriptionEntry(ENTRY_KEY_EXPECTED_VALUE, expectedCandidateURL);
208                 r.addDescriptionEntry(ENTRY_KEY_FOUND_VALUE, "" + t.saveVariation.toURL().toString());
209                 return r;
210             }
211
212             return reportSuccess();
213         }
214     }
215
216
217
218     /**
219      * Creates an <tt>SVGRenderingAccuracyTest</tt> with an
220      * invalid URL for the source SVG. Checks that this
221      * error is reported as a failure.
222      */

223     static class InvalidSVGURL extends TestReportValidator {
224         public TestReport runImpl() throws Exception JavaDoc {
225             // Create an invalid URL for the SVG file
226
URL JavaDoc invalidSVGURL = new URL JavaDoc("http",
227                                         "dummyHost",
228                                         "dummyFile.svg");
229
230             // Create a valid reference image
231
URL JavaDoc refImgURL = createValidReferenceImage(validSVG);
232
233             Test t = new SVGRenderingAccuracyTest(invalidSVGURL.toString(),
234                                                   refImgURL.toString());
235
236             setConfig(t,
237                       false,
238                       SVGRenderingAccuracyTest.ERROR_CANNOT_TRANSCODE_SVG);
239
240             return super.runImpl();
241         }
242     }
243
244     /**
245      * Creates an <tt>SVGRenderingAccuracyTest</tt> with a
246      * valid URL pointing to an invalid SVG document. Checks that this
247      * error is reported as a failure.
248      */

249     static class InvalidSVGContent extends TestReportValidator {
250         public TestReport runImpl() throws Exception JavaDoc {
251             // Create an SVG URL from invalid SVG content.
252
URL JavaDoc validSVGURL = createSVGSourceURL(invalidSVG);
253             
254             // Create a valid reference image
255
URL JavaDoc refImgURL = createValidReferenceImage(validSVG);
256
257             Test t = new SVGRenderingAccuracyTest(validSVGURL.toString(),
258                                                   refImgURL.toString());
259             setConfig(t,
260                       false,
261                       SVGRenderingAccuracyTest.ERROR_CANNOT_TRANSCODE_SVG);
262
263             return super.runImpl();
264         }
265     }
266
267     /**
268      * Creates an <tt>SVGRenderingAccuracyTest</tt> with an
269      * valid URL for the source SVG but with an invalid
270      * URL for the reference image.
271      */

272     static class InvalidReferenceImageURL extends TestReportValidator {
273         public TestReport runImpl() throws Exception JavaDoc {
274             // Create a valid SVG URL from valid SVG content.
275
URL JavaDoc validSVGURL = createSVGSourceURL(validSVG);
276
277             // Create an invalid URL for the reference image.
278
URL JavaDoc invalidReferenceImageURL = null;
279             
280             invalidReferenceImageURL = new URL JavaDoc("http",
281                                                "dummyHost",
282                                                "dummyFile.png");
283             Test t = new SVGRenderingAccuracyTest(validSVGURL.toString(),
284                                                   invalidReferenceImageURL.toString());
285
286             setConfig(t,
287                       false,
288                       SVGRenderingAccuracyTest.ERROR_CANNOT_OPEN_REFERENCE_IMAGE);
289
290             return super.runImpl();
291         }
292     }
293
294     /**
295      * Creates an <tt>SVGRenderingAccuracyTest</tt> with an
296      * valid URL for the source SVG valid
297      * URL for the reference image, but the reference image,
298      * but the reference image does not exist
299      */

300     static class InexistingReferenceImage extends TestReportValidator {
301         public TestReport runImpl() throws Exception JavaDoc {
302             // Create a valid SVG URL from valid SVG content.
303
URL JavaDoc validSVGURL = createSVGSourceURL(validSVG);
304
305             // Create an valid URL for the reference image.
306
// We use the createSVGSourceURL method to create
307
// a File that the ImageLoader is not able to load.
308
File JavaDoc tmpFile = File.createTempFile(SVGRenderingAccuracyTest.TEMP_FILE_PREFIX,
309                                                null);
310             URL JavaDoc refImgURL = tmpFile.toURL();
311             tmpFile.delete();
312             
313             Test t = new SVGRenderingAccuracyTest(validSVGURL.toString(),
314                                                   refImgURL.toString());
315
316             setConfig(t,
317                       false,
318                       SVGRenderingAccuracyTest.ERROR_CANNOT_OPEN_REFERENCE_IMAGE);
319
320             return super.runImpl();
321         }
322     }
323
324     static class DifferentSizes extends TestReportValidator {
325         public TestReport runImpl() throws Exception JavaDoc {
326             //
327
// Create a valid SVG URL from valid SVG content.
328
//
329
URL JavaDoc validSVGURL = createSVGSourceURL(validSVG);
330
331             //
332
// Create an valid URL for the reference image.
333
//
334
URL JavaDoc validRefImageURL = createValidReferenceImage(validSmallSVG);
335             
336             //
337
// Run test and check report
338
//
339
Test t = new SVGRenderingAccuracyTest(validSVGURL.toString(),
340                                                   validRefImageURL.toString());
341
342             setConfig(t,
343                       false,
344                       SVGRenderingAccuracyTest.ERROR_SVG_RENDERING_NOT_ACCURATE);
345
346             return super.runImpl();
347         }
348     }
349
350     static class SameSizeDifferentContent extends TestReportValidator {
351         public TestReport runImpl() throws Exception JavaDoc {
352             // Create a valid SVG URL from valid SVG content.
353
URL JavaDoc validSVGURL = createSVGSourceURL(validSVG);
354
355             // Create an valid URL for the reference image.
356
URL JavaDoc validRefImageURL = createValidReferenceImage(validRedSVG);
357             
358
359             Test t = new SVGRenderingAccuracyTest(validSVGURL.toString(),
360                                                   validRefImageURL.toString());
361
362             setConfig(t,
363                       false,
364                       SVGRenderingAccuracyTest.ERROR_SVG_RENDERING_NOT_ACCURATE);
365
366             return super.runImpl();
367         }
368     }
369
370     static class AccurateRendering extends TestReportValidator {
371         public TestReport runImpl() throws Exception JavaDoc {
372             // Create a valid SVG URL from valid SVG content.
373
URL JavaDoc validSVGURL = createSVGSourceURL(validSVG);
374
375             // Create an valid URL for the reference image.
376
URL JavaDoc validRefImageURL = createValidReferenceImage(validSVG);
377
378             setConfig(new SVGRenderingAccuracyTest(validSVGURL.toString(),
379                                                    validRefImageURL.toString()),
380                       true,
381                       null);
382
383             return super.runImpl();
384         }
385     }
386
387     /**
388      * Validates that test passes if proper variation is given
389      */

390     static class AccurateRenderingWithVariation extends TestReportValidator {
391         public TestReport runImpl() throws Exception JavaDoc {
392             // Create a valid SVG URL from valid SVG content.
393
URL JavaDoc validSVGURL = createSVGSourceURL(validSVG);
394
395             // Create an valid URL for the reference image.
396
URL JavaDoc validRefImageURL = createValidReferenceImage(validSVGVariation);
397
398             SVGRenderingAccuracyTest t
399                 = new SVGRenderingAccuracyTest(validSVGURL.toString(),
400                                                validRefImageURL.toString());
401
402             File JavaDoc tmpVariationFile = File.createTempFile(SVGRenderingAccuracyTest.TEMP_FILE_PREFIX, null);
403
404             // Run the test with the tmpVariationFile
405
t.setSaveVariation(tmpVariationFile);
406
407             setConfig(t,
408                       false,
409                       SVGRenderingAccuracyTest.ERROR_SVG_RENDERING_NOT_ACCURATE);
410
411             super.runImpl();
412
413             t.setVariationURL(tmpVariationFile.toURL().toString());
414             t.setSaveVariation(null);
415
416             setConfig(t,
417                       true,
418                       null);
419
420             return super.runImpl();
421         }
422     }
423
424 }
425
Popular Tags