KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > apps > rasterizer > SVGConverterTest


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

16
17 package org.apache.batik.apps.rasterizer;
18
19 import org.apache.batik.test.*;
20 import org.apache.batik.test.util.ImageCompareTest;
21 import org.apache.batik.transcoder.Transcoder;
22 import org.apache.batik.transcoder.image.ImageTranscoder;
23 import org.apache.batik.transcoder.image.JPEGTranscoder;
24 import org.apache.batik.transcoder.image.PNGTranscoder;
25
26 import java.awt.*;
27 import java.io.*;
28 import java.util.*;
29
30 /**
31  * Validates the operation of the <tt>SVGRasterizer</tt>.
32  * It validates the option setting and the manipulation
33  * of source and destination sources.
34  *
35  * @author <a HREF="mailto:vhardy@apache.org">Vincent Hardy</a>
36  * @version $Id: SVGConverterTest.java,v 1.20 2005/04/01 02:28:16 deweese Exp $
37  */

38 public class SVGConverterTest extends DefaultTestSuite {
39     public SVGConverterTest(){
40         ///////////////////////////////////////////////////////////////////////
41
// Add configuration tests
42
///////////////////////////////////////////////////////////////////////
43
AbstractTest t = null;
44
45         //
46
// Test Trancoder usage
47
//
48
t = new TranscoderConfigTest(DestinationType.PNG,
49                                      org.apache.batik.transcoder.image.PNGTranscoder.class);
50         addTest(t);
51         t.setId("TranscoderConfigTest.PNG");
52
53         t = new TranscoderConfigTest(DestinationType.JPEG,
54                                      org.apache.batik.transcoder.image.JPEGTranscoder.class);
55         addTest(t);
56         t.setId("TranscoderConfigTest.JPEG");
57
58         t = new TranscoderConfigTest(DestinationType.TIFF,
59                                      org.apache.batik.transcoder.image.TIFFTranscoder.class);
60         addTest(t);
61         t.setId("TranscoderConfigTest.TIFF");
62
63         try {
64             Class JavaDoc pdfClass = Class.forName("org.apache.fop.svg.PDFTranscoder");
65             t = new TranscoderConfigTest(DestinationType.PDF, pdfClass);
66             t.setId("TranscoderConfigTest.PDF");
67             addTest(t);
68         } catch (Exception JavaDoc e) {
69         }
70
71         //
72
// Checks that the proper hints are used
73
//
74
t = new HintsConfigTest(new Object JavaDoc[][]{
75             {ImageTranscoder.KEY_AOI, new Rectangle(40, 50, 40, 80)}}){
76                 protected void deltaConfigure(SVGConverter c){
77                     c.setArea(new Rectangle(40, 50, 40, 80));
78                 }
79             };
80         
81         addTest(t);
82         t.setId("HintsConfigTest.KEY_AOI");
83         
84         t = new HintsConfigTest(new Object JavaDoc[][]{
85             {JPEGTranscoder.KEY_QUALITY, new Float JavaDoc(.5)}}){
86                 protected void deltaConfigure(SVGConverter c){
87                     c.setQuality(.5f);
88                 }
89             };
90
91         addTest(t);
92         t.setId("HintsConfigTest.KEY_QUALITY");
93
94         t = new HintsConfigTest(new Object JavaDoc[][]{
95             {PNGTranscoder.KEY_INDEXED, new Integer JavaDoc(8)}}){
96                 protected void deltaConfigure(SVGConverter c){
97                     c.setIndexed(8);
98                 }
99             };
100         addTest(t);
101         t.setId("HintsConfigTest.KEY_INDEXED");
102
103         t = new HintsConfigTest(new Object JavaDoc[][]{
104             {ImageTranscoder.KEY_BACKGROUND_COLOR, Color.pink}}){
105                 protected void deltaConfigure(SVGConverter c){
106                     c.setBackgroundColor(Color.pink);
107                 }
108             };
109
110         addTest(t);
111         t.setId("HintsConfigTest.KEY_BACKGROUND_COLOR");
112
113         t = new HintsConfigTest(new Object JavaDoc[][]{
114             {ImageTranscoder.KEY_HEIGHT, new Float JavaDoc(50)}}){
115                 protected void deltaConfigure(SVGConverter c){
116                     c.setHeight(50);
117                 }
118             };
119
120         addTest(t);
121         t.setId("HintsConfigTest.KEY_HEIGHT");
122
123         t = new HintsConfigTest(new Object JavaDoc[][]{
124             {ImageTranscoder.KEY_WIDTH, new Float JavaDoc(50)}}){
125                 protected void deltaConfigure(SVGConverter c){
126                     c.setWidth(50);
127                 }
128             };
129
130         addTest(t);
131         t.setId("HintsConfigTest.KEY_WIDTH");
132
133         t = new HintsConfigTest(new Object JavaDoc[][]{
134             {ImageTranscoder.KEY_MAX_HEIGHT, new Float JavaDoc(50)}}){
135                 protected void deltaConfigure(SVGConverter c){
136                     c.setMaxHeight(50);
137                 }
138             };
139         addTest(t);
140         t.setId("HintsConfigTest.KEY_MAX_HEIGHT");
141
142         t = new HintsConfigTest(new Object JavaDoc[][]{
143             {ImageTranscoder.KEY_MAX_WIDTH, new Float JavaDoc(50)}}){
144                 protected void deltaConfigure(SVGConverter c){
145                     c.setMaxWidth(50);
146                 }
147             };
148         addTest(t);
149         t.setId("HintsConfigTest.KEY_MAX_WIDTH");
150
151         t = new HintsConfigTest(new Object JavaDoc[][]{
152             {ImageTranscoder.KEY_MEDIA, "print"}}){
153                 protected void deltaConfigure(SVGConverter c){
154                     c.setMediaType("print");
155                 }
156             };
157
158         addTest(t);
159         t.setId("HintsConfigTest.KEY_MEDIA");
160
161         t = new HintsConfigTest(new Object JavaDoc[][]{
162             {ImageTranscoder.KEY_DEFAULT_FONT_FAMILY, "Times"}}){
163                 protected void deltaConfigure(SVGConverter c){
164                     c.setDefaultFontFamily("Times");
165                 }
166             };
167
168         addTest(t);
169         t.setId("HintsConfigTest.KEY_DEFAULT_FONT_FAMILY");
170
171         t = new HintsConfigTest(new Object JavaDoc[][]{
172             {ImageTranscoder.KEY_ALTERNATE_STYLESHEET, "myStyleSheet"}}){
173                 protected void deltaConfigure(SVGConverter c){
174                     c.setAlternateStylesheet("myStyleSheet");
175                 }
176             };
177         addTest(t);
178         t.setId("HintsConfigTest.KEY_ALTERNATE_STYLESHEET");
179
180         t = new HintsConfigTest(new Object JavaDoc[][]{
181             {ImageTranscoder.KEY_USER_STYLESHEET_URI, "userStylesheet.css"}}){
182                 protected void deltaConfigure(SVGConverter c){
183                     c.setUserStylesheet("userStylesheet.css");
184                 }
185             };
186         addTest(t);
187         t.setId("HintsConfigTest.KEY_USER_STYLESHEET_URI");
188
189         t = new HintsConfigTest(new Object JavaDoc[][]{
190             {ImageTranscoder.KEY_LANGUAGE, "fr"}}){
191                 protected void deltaConfigure(SVGConverter c){
192                     c.setLanguage("fr");
193                 }
194             };
195         addTest(t);
196         t.setId("HintsConfigTest.KEY_LANGUAGE");
197
198         t = new HintsConfigTest(new Object JavaDoc[][]{
199             {ImageTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER, new Float JavaDoc(.5f)}}){
200                 protected void deltaConfigure(SVGConverter c){
201                     c.setPixelUnitToMillimeter(.5f);
202                 }
203             };
204         addTest(t);
205         t.setId("HintsConfigTest.KEY_PIXEL_UNIT_TO_MILLIMETER");
206
207         t = new HintsConfigTest(new Object JavaDoc[][]{
208             {ImageTranscoder.KEY_XML_PARSER_VALIDATING, new Boolean JavaDoc(true)}}){
209                 protected void deltaConfigure(SVGConverter c){
210                     c.setValidate(true);
211                 }
212             };
213         addTest(t);
214         t.setId("HintsConfigTest.KEY_XML_PARSER_VALIDATING");
215
216
217         //
218
// Check sources
219
//
220
t = new SourcesConfigTest(new String JavaDoc[] { "samples/anne", "samples/batikFX", "samples/tests/spec/styling/smiley" }){
221                 protected void setSources(SVGConverter c){
222                     c.setSources(new String JavaDoc[] {"samples/anne.svg", "samples/batikFX.svg", "samples/tests/spec/styling/smiley.svg"});
223                 }
224             };
225             
226         addTest(t);
227         t.setId("SourcesConfigTest.SimpleList");
228         
229
230         //
231
// Check destination
232
//
233
t = new DestConfigTest(new String JavaDoc[] { "samples/anne.svg" },
234                                new String JavaDoc[] { "test-reports/anne.png"}){
235                 protected void setDestination(SVGConverter c){
236                     c.setDst(new File("test-reports/anne.png"));
237                 }
238             };
239         addTest(t);
240         t.setId("DestConfigTest.DstFile");
241
242         t = new DestConfigTest(new String JavaDoc[] { "samples/anne.svg", "samples/tests/spec/styling/smiley.svg" },
243                                new String JavaDoc[] { "test-resources/anne.png", "test-resources/smiley.png"}){
244                 protected void setDestination(SVGConverter c){
245                     c.setDst(new File("test-resources"));
246                 }
247             };
248         addTest(t);
249         t.setId("DestConfigTest.DstDir");
250
251         //
252
// Check that complete process goes without error
253
//
254
t = new OperationTest(){
255                 protected void configure(SVGConverter c){
256                     c.setSources(new String JavaDoc[]{"samples/anne.svg"});
257                     c.setDst(new File("anne.png"));
258                     File file = new File("anne.png");
259                     file.deleteOnExit();
260                 }
261             };
262         addTest(t);
263         t.setId("OperationTest.Bug4888");
264
265         t = new OperationTest(){
266                 protected void configure(SVGConverter c){
267                     c.setDestinationType(DestinationType.PDF);
268                     c.setSources(new String JavaDoc[]{"samples/anne.svg"});
269                 }
270             };
271         addTest(t);
272         t.setId("Operationtest.PDFTranscoding");
273         
274         ///////////////////////////////////////////////////////////////////////
275
// Add configuration error test. These tests check that the expected
276
// error gets reported for a given mis-configuration
277
///////////////////////////////////////////////////////////////////////
278
t = new ConfigErrorTest(SVGConverter.ERROR_NO_SOURCES_SPECIFIED) {
279                 protected void configure(SVGConverter c){
280                     c.setSources(null);
281                 }
282             };
283         addTest(t);
284         t.setId("ConfigErrorTest.ERROR_NO_SOURCES_SPECIFIED");
285
286         t = new ConfigErrorTest(SVGConverter.ERROR_CANNOT_COMPUTE_DESTINATION){
287                 protected void configure(SVGConverter c){
288                     // Do not set destination file or destination directory
289
c.setSources(new String JavaDoc[]{"http://xml.apache.org/batik/dummy.svg"});
290                 }
291             };
292         addTest(t);
293         t.setId("ConfigErrorTest.ERROR_CANNOT_COMPUTE_DESTINATION");
294
295         t = new ConfigErrorTest(SVGConverter.ERROR_CANNOT_USE_DST_FILE){
296                 protected void configure(SVGConverter c){
297                     File dummy = null;
298                     try {
299                         dummy = File.createTempFile("dummyPNG", ".png");
300                     } catch(IOException e){
301                         throw new Error JavaDoc();
302                     }
303                     c.setSources(new String JavaDoc[]{"samples/anne.svg", "samples/batikFX.svg"});
304                     c.setDst(dummy);
305                     dummy.deleteOnExit();
306                 }
307             };
308         addTest(t);
309         t.setId("ConfigErrorTest.ERROR_CANNOT_USE_DST_FILE");
310
311         t = new ConfigErrorTest(SVGConverter.ERROR_SOURCE_SAME_AS_DESTINATION){
312                 protected void configure(SVGConverter c){
313                     c.setSources(new String JavaDoc[]{ "samples/anne.svg" });
314                     c.setDst(new File("samples/anne.svg"));
315                 }
316             };
317         addTest(t);
318         t.setId("ConfigErrorTest.ERROR_SOURCE_SAME_AS_DESTINATION");
319
320         t = new ConfigErrorTest(SVGConverter.ERROR_CANNOT_READ_SOURCE){
321                 protected void configure(SVGConverter c){
322                     c.setSources(new String JavaDoc[]{ "test-resources/org/apache/batik/apps/rasterizer/notReadable.svg" });
323                     c.setDst(new File("test-reports"));
324                 }
325
326                 public boolean proceedWithSourceTranscoding(SVGConverterSource source,
327                                                             File dest){
328                     // Big hack to simulate a non-readable SVG file
329
File hackedFile = new File(((SVGConverterFileSource)source).file.getPath()){
330                             public boolean canRead(){
331                                 System.out.println("Yahoooooooo! In canRead");
332                                 return false;
333                             }
334                         };
335                     ((SVGConverterFileSource)source).file = hackedFile;
336                     return true;
337                 }
338             };
339         addTest(t);
340         t.setId("ConfigErrorTest.ERROR_CANNOT_READ_SOURCE");
341
342         t = new ConfigErrorTest(SVGConverter.ERROR_CANNOT_OPEN_SOURCE){
343                 protected void configure(SVGConverter c){
344                     c.setSources(new String JavaDoc[]{ "test-resources/org/apache/batik/apps/rasterizer/notReadable.svg" });
345                 }
346
347                 public boolean proceedWithComputedTask(Transcoder transcoder,
348                                                        Map hints,
349                                                        Vector sources,
350                                                        Vector dest){
351                     System.out.println("==================> Hacked Starting to process Task <=========================");
352                     SVGConverterFileSource source = (SVGConverterFileSource)sources.elementAt(0);
353                     source = new SVGConverterFileSource(source.file){
354                             public InputStream openStream() throws FileNotFoundException {
355                                 throw new FileNotFoundException("Simulated FileNotFoundException");
356                             }
357                         };
358
359                     sources.setElementAt(source, 0);
360                     return true;
361                 }
362
363             };
364         addTest(t);
365         t.setId("ConfigErrorTest.ERROR_CANNOT_OPEN_SOURCE");
366
367         t = new ConfigErrorTest(SVGConverter.ERROR_OUTPUT_NOT_WRITEABLE){
368                 protected void configure(SVGConverter c){
369                     c.setSources(new String JavaDoc[]{ "samples/anne.svg" });
370                     File o = new File("test-resources/org/apache/batik/apps/rasterizer/readOnly.png");
371                     o.setReadOnly();
372                     c.setDst(o);
373                 }
374             };
375         addTest(t);
376         t.setId("ConfigErrorTest.ERROR_OUTPUT_NOT_WRITEABLE");
377                    
378         t = new ConfigErrorTest(SVGConverter.ERROR_UNABLE_TO_CREATE_OUTPUT_DIR){
379                 protected void configure(SVGConverter c){
380                     c.setDst(new File("ZYZ::/cannotCreate"));
381                 }
382             };
383         addTest(t);
384         t.setId("ConfigErrorTest.ERROR_UNABLE_TO_CREATE_OUTPUT_DIR");
385
386         t = new ConfigErrorTest(SVGConverter.ERROR_WHILE_RASTERIZING_FILE){
387                 protected void configure(SVGConverter c){
388                     c.setSources(new String JavaDoc[]{ "test-resources/org/apache/batik/apps/rasterizer/invalidSVG.svg"});
389                 }
390             };
391         addTest(t);
392         t.setId("ConfigErrorTest(SVGConverter.ERROR_WHILE_RASTERIZING_FILE");
393         
394         //
395
// Test that files are created as expected and are producing the
396
// expected result.
397
//
398

399         // Plain file
400
t = new ConverterOutputTest("samples/anne.svg", // File to convert
401
"test-reports/anne.png", // Output
402
"test-references/samples/anne.png"); // reference
403
addTest(t);
404         t.setId("OutputTest.plain");
405
406         // File with reference
407
t = new ConverterOutputTest("samples/anne.svg#svgView(viewBox(0,0,100,200))", // File to convert
408
"test-reports/anne.png", // Output
409
"test-references/samples/anneViewBox1.png"); // reference
410
addTest(t);
411         t.setId("OutputTest.reference");
412
413     }
414 }
415
416 /**
417  * A ConfigTest builds an SVGConverter, configures it,
418  * sets itself as the SVGConverterController and checks that
419  * the computed task is as expected (i.e., right set of
420  * hints).
421  */

422 abstract class AbstractConfigTest extends AbstractTest implements SVGConverterController {
423     /**
424      * The 'proceedWithComputedTask' handler was not called
425      */

426     public static final String JavaDoc ERROR_NO_COMPUTED_TASK
427         = "ConfigTest.error.no.computed.task";
428
429     /**
430      * The transcoderClass is not the one expected.
431      */

432     public static final String JavaDoc ERROR_UNEXPECTED_TRANSCODER_CLASS
433         = "ConfigTest.error.unexpected.transcoder.class";
434
435     public static final String JavaDoc ENTRY_KEY_EXPECTED_TRANSCODER_CLASS
436         = "ConfigTest.entry.key.expected.transcoder.class";
437
438     public static final String JavaDoc ENTRY_KEY_COMPUTED_TRANSCODER_CLASS
439         = "ConfigTest.entry.key.computed.trancoder.class";
440
441     /**
442      * Error if the hints do not match
443      */

444     public static final String JavaDoc ERROR_UNEXPECTED_NUMBER_OF_HINTS
445         = "ConfigTest.error.unexpected.number.of.hints";
446
447     public static final String JavaDoc ENTRY_KEY_EXPECTED_NUMBER_OF_HINTS
448         = "ConfigTest.entry.key.expected.number.of.hints";
449
450     public static final String JavaDoc ENTRY_KEY_COMPUTED_NUMBER_OF_HINTS
451         = "ConfigTest.entry.key.computed.number.of.hints";
452
453     public static final String JavaDoc ENTRY_KEY_EXPECTED_HINTS
454         = "ConfigTest.entry.key.expected.hints";
455
456     public static final String JavaDoc ENTRY_KEY_COMPUTED_HINTS
457         = "ConfigTest.entry.key.computed.hints";
458
459     public static final String JavaDoc ERROR_UNEXPECTED_TRANSCODING_HINT
460         = "ConfigTest.error.unexpected.transcoding.hint";
461
462     public static final String JavaDoc ENTRY_KEY_EXPECTED_HINT_KEY
463         = "ConfigTest.entry.key.expected.hint.key";
464
465     public static final String JavaDoc ENTRY_KEY_COMPUTED_HINT_VALUE
466         = "ConfigTest.entry.key.computed.hint.value";
467
468     public static final String JavaDoc ENTRY_KEY_EXPECTED_HINT_VALUE
469         = "ConfigTest.entry.key.expected.hint.value";
470
471     /**
472      * Error if the sources do not match
473      */

474     public static final String JavaDoc ERROR_UNEXPECTED_SOURCES_LIST
475         = "ConfigTest.error.unexpected.sources.list";
476
477     public static final String JavaDoc ENTRY_KEY_EXPECTED_NUMBER_OF_SOURCES
478         = "ConfigTest.entry.key.expected.number.of.sources";
479
480     public static final String JavaDoc ENTRY_KEY_COMPUTED_NUMBER_OF_SOURCES
481         = "ConfigTest.entry.key.computed.number.of.sources";
482
483     public static final String JavaDoc ENTRY_KEY_EXPECTED_SOURCE_AT_INDEX
484         = "ConfigTest.entry.key.expected.source.at.index";
485
486     public static final String JavaDoc ENTRY_KEY_COMPUTED_SOURCE_AT_INDEX
487         = "ConfigTest.entry.key.computed.source.at.index";
488
489     public static final String JavaDoc ENTRY_KEY_COMPUTED_SOURCES_LIST
490         = "ConfigTest.entry.key.computed.sources.list";
491
492     public static final String JavaDoc ENTRY_KEY_EXPECTED_SOURCES_LIST
493         = "ConfigTest.entry.key.expected.sources.list";
494
495     /**
496      * Error if the dest do not match
497      */

498     public static final String JavaDoc ERROR_UNEXPECTED_DEST_LIST
499         = "ConfigTest.error.unexpected.dest.list";
500
501     public static final String JavaDoc ENTRY_KEY_EXPECTED_NUMBER_OF_DEST
502         = "ConfigTest.entry.key.expected.number.of.dest";
503
504     public static final String JavaDoc ENTRY_KEY_COMPUTED_NUMBER_OF_DEST
505         = "ConfigTest.entry.key.computed.number.of.dest";
506
507     public static final String JavaDoc ENTRY_KEY_EXPECTED_DEST_AT_INDEX
508         = "ConfigTest.entry.key.expected.dest.at.index";
509
510     public static final String JavaDoc ENTRY_KEY_COMPUTED_DEST_AT_INDEX
511         = "ConfigTest.entry.key.computed.dest.at.index";
512
513     public static final String JavaDoc ENTRY_KEY_COMPUTED_DEST_LIST
514         = "ConfigTest.entry.key.computed.dest.list";
515
516     public static final String JavaDoc ENTRY_KEY_EXPECTED_DEST_LIST
517         = "ConfigTest.entry.key.expected.dest.list";
518
519
520     /**
521      * Configuration Description
522      */

523     static class Config {
524         Class JavaDoc transcoderClass;
525         HashMap hints;
526         Vector sources, dest;
527     }
528             
529     protected Config expectedConfig;
530     protected Config computedConfig;
531
532     protected AbstractConfigTest(){
533     }
534
535     protected void setExpectedConfig(Config expectedConfig){
536         this.expectedConfig = expectedConfig;
537     }
538
539     protected abstract void configure(SVGConverter c);
540
541     protected String JavaDoc makeSourceList(Vector v){
542         int n = v.size();
543         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
544         for (int i=0; i<n; i++){
545             sb.append(v.elementAt(i).toString());
546         }
547
548         return sb.toString();
549     }
550
551     protected String JavaDoc makeHintsString(HashMap map){
552         Iterator iter = map.keySet().iterator();
553         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
554         while (iter.hasNext()){
555             Object JavaDoc key = iter.next();
556             sb.append(key.toString());
557             sb.append("(");
558             sb.append(map.get(key).toString());
559             sb.append(") -- ");
560         }
561
562         return sb.toString();
563     }
564
565
566     public String JavaDoc getName(){
567         return getId();
568     }
569
570     public TestReport runImpl() throws Exception JavaDoc {
571         SVGConverter c = new SVGConverter(this);
572         configure(c);
573         c.execute();
574
575         //
576
// Now, check that the expectedConfig and the
577
// computedConfig are identical
578
//
579
if (computedConfig == null){
580             return reportError(ERROR_NO_COMPUTED_TASK);
581         }
582
583         if (!expectedConfig.transcoderClass.equals
584             (computedConfig.transcoderClass)){
585             TestReport report = reportError(ERROR_UNEXPECTED_TRANSCODER_CLASS);
586             report.addDescriptionEntry(ENTRY_KEY_EXPECTED_TRANSCODER_CLASS,
587                                        expectedConfig.transcoderClass);
588             report.addDescriptionEntry(ENTRY_KEY_COMPUTED_TRANSCODER_CLASS,
589                                        computedConfig.transcoderClass);
590
591             return report;
592         }
593
594         // Compare sources
595
int en = expectedConfig.sources.size();
596         int cn = computedConfig.sources.size();
597         
598         if (en != cn){
599             TestReport report = reportError(ERROR_UNEXPECTED_SOURCES_LIST);
600             report.addDescriptionEntry(ENTRY_KEY_EXPECTED_NUMBER_OF_SOURCES,
601                             "" + en);
602             report.addDescriptionEntry(ENTRY_KEY_COMPUTED_NUMBER_OF_SOURCES,
603                             "" + cn);
604             report.addDescriptionEntry(ENTRY_KEY_EXPECTED_SOURCES_LIST,
605                             makeSourceList(expectedConfig.sources));
606
607             report.addDescriptionEntry(ENTRY_KEY_COMPUTED_SOURCES_LIST,
608                             makeSourceList(computedConfig.sources));
609
610             return report;
611         }
612
613         for (int i=0; i<en; i++){
614             Object JavaDoc es = expectedConfig.sources.elementAt(i);
615             Object JavaDoc cs = computedConfig.sources.elementAt(i);
616             if (!computedConfig.sources.contains(es)){
617                 TestReport report = reportError(ERROR_UNEXPECTED_SOURCES_LIST);
618                 report.addDescriptionEntry(ENTRY_KEY_EXPECTED_SOURCE_AT_INDEX,
619                                            "[" + i + "] = -" + es + "- (" + es.getClass().getName() + ")");
620                 report.addDescriptionEntry(ENTRY_KEY_COMPUTED_SOURCE_AT_INDEX,
621                                 "[" + i + "] = -" + cs + "- (" + es.getClass().getName() + ")");
622                 report.addDescriptionEntry(ENTRY_KEY_EXPECTED_SOURCES_LIST,
623                                 makeSourceList(expectedConfig.sources));
624                 report.addDescriptionEntry(ENTRY_KEY_COMPUTED_SOURCES_LIST,
625                                 makeSourceList(computedConfig.sources));
626                 
627                 return report;
628             }
629         }
630
631         // Compare dest
632
en = expectedConfig.dest.size();
633         cn = computedConfig.dest.size();
634
635         if (en != cn){
636             TestReport report = reportError(ERROR_UNEXPECTED_DEST_LIST);
637             report.addDescriptionEntry(ENTRY_KEY_EXPECTED_NUMBER_OF_DEST,
638                             "" + en);
639             report.addDescriptionEntry(ENTRY_KEY_COMPUTED_NUMBER_OF_DEST,
640                             "" + cn);
641             report.addDescriptionEntry(ENTRY_KEY_EXPECTED_DEST_LIST,
642                             makeSourceList(expectedConfig.dest));
643
644             report.addDescriptionEntry(ENTRY_KEY_COMPUTED_DEST_LIST,
645                             makeSourceList(computedConfig.dest));
646
647             return report;
648         }
649
650         for (int i=0; i<en; i++){
651             Object JavaDoc es = expectedConfig.dest.elementAt(i);
652             Object JavaDoc cs = computedConfig.dest.elementAt(i);
653             if (!computedConfig.dest.contains(cs)){
654                 TestReport report = reportError(ERROR_UNEXPECTED_DEST_LIST);
655                 report.addDescriptionEntry(ENTRY_KEY_EXPECTED_DEST_AT_INDEX,
656                                 "[" + i + "] = " + es);
657                 report.addDescriptionEntry(ENTRY_KEY_COMPUTED_DEST_AT_INDEX,
658                                 "[" + i + "] = " + cs);
659                 report.addDescriptionEntry(ENTRY_KEY_EXPECTED_DEST_LIST,
660                                 makeSourceList(expectedConfig.dest));
661                 report.addDescriptionEntry(ENTRY_KEY_COMPUTED_DEST_LIST,
662                                 makeSourceList(computedConfig.dest));
663                 
664                 return report;
665             }
666         }
667
668         //
669
// Finally, check the hints
670
//
671
en = expectedConfig.hints.size();
672         cn = computedConfig.hints.size();
673         
674         if (en != cn){
675             TestReport report = reportError(ERROR_UNEXPECTED_NUMBER_OF_HINTS);
676             report.addDescriptionEntry(ENTRY_KEY_EXPECTED_NUMBER_OF_HINTS,
677                                        "" + en);
678             report.addDescriptionEntry(ENTRY_KEY_COMPUTED_NUMBER_OF_HINTS,
679                                        "" + cn);
680
681             report.addDescriptionEntry(ENTRY_KEY_EXPECTED_HINTS,
682                                        makeHintsString(expectedConfig.hints));
683             report.addDescriptionEntry(ENTRY_KEY_COMPUTED_HINTS,
684                                        makeHintsString(computedConfig.hints));
685             
686             return report;
687         }
688
689         Iterator iter = expectedConfig.hints.keySet().iterator();
690         while (iter.hasNext()){
691             Object JavaDoc hintKey = iter.next();
692             Object JavaDoc expectedHintValue = expectedConfig.hints.get(hintKey);
693             
694             Object JavaDoc computedHintValue = computedConfig.hints.get(hintKey);
695             
696             if (!expectedHintValue.equals(computedHintValue)){
697                 TestReport report = reportError(ERROR_UNEXPECTED_TRANSCODING_HINT);
698                 report.addDescriptionEntry(ENTRY_KEY_EXPECTED_HINT_KEY,
699                                            hintKey.toString());
700                 report.addDescriptionEntry(ENTRY_KEY_EXPECTED_HINT_VALUE,
701                                            expectedHintValue.toString());
702                 report.addDescriptionEntry(ENTRY_KEY_COMPUTED_HINT_VALUE,
703                                            "" + computedHintValue);
704                 report.addDescriptionEntry(ENTRY_KEY_EXPECTED_HINTS,
705                                            makeHintsString(expectedConfig.hints));
706                 report.addDescriptionEntry(ENTRY_KEY_COMPUTED_HINTS,
707                                            makeHintsString(computedConfig.hints));
708
709                 return report;
710             }
711         }
712                                             
713         return reportSuccess();
714     
715     }
716
717     public boolean proceedWithComputedTask(Transcoder transcoder,
718                                            Map hints,
719                                            Vector sources,
720                                            Vector dest){
721         computedConfig = new Config();
722         computedConfig.transcoderClass = transcoder.getClass();
723         computedConfig.sources = (Vector)sources.clone();
724         computedConfig.dest = (Vector)dest.clone();
725         computedConfig.hints = new HashMap(hints);
726         return false; // Do not proceed with the convertion process,
727
// we are only checking the config in this test.
728
}
729
730     public boolean proceedWithSourceTranscoding(SVGConverterSource source,
731                                                 File dest) {
732         return true;
733     }
734         
735     public boolean proceedOnSourceTranscodingFailure(SVGConverterSource source,
736                                                      File dest,
737                                                      String JavaDoc errorCode){
738         return true;
739     }
740
741     public void onSourceTranscodingSuccess(SVGConverterSource source,
742                                            File dest){
743     }
744 }
745
746 /**
747  * Tests that a convertion task goes without exception.
748  */

749 class OperationTest extends AbstractTest{
750     public TestReport runImpl() throws Exception JavaDoc {
751         SVGConverter c = new SVGConverter();
752         configure(c);
753         c.execute();
754         return reportSuccess();
755     }
756
757     protected void configure(SVGConverter c){
758         // Should be overridden by subclasses.
759
}
760 }
761
762 /**
763  * Provides a simple string constructor which allows the user to
764  * create a given test to check that a specific transcoder class is
765  * used for a given mime type.
766  */

767 class TranscoderConfigTest extends AbstractConfigTest {
768     static final String JavaDoc SOURCE_FILE = "samples/anne.svg";
769     static final String JavaDoc DEST_FILE_NAME = "samples/anne";
770
771     protected DestinationType dstType;
772     /**
773      * @param dstType type of result image
774      * @param expectedTranscoderClass class for the Transcoder expected to perform
775      * the convertion.
776      */

777     public TranscoderConfigTest(DestinationType dstType,
778                                 Class JavaDoc expectedTranscoderClass){
779         this.dstType = dstType;
780
781         Config config = new Config();
782         config.transcoderClass = expectedTranscoderClass;
783         
784         Vector sources = new Vector();
785         sources.addElement(new SVGConverterFileSource(new File(SOURCE_FILE)));
786         config.sources = sources;
787
788         Vector dest = new Vector();
789         dest.addElement(new File(DEST_FILE_NAME + dstType.getExtension()));
790         config.dest = dest;
791
792         HashMap hints = new HashMap();
793         config.hints = hints;
794                   
795         setExpectedConfig(config);
796     }
797
798     /**
799      * Configures the test with the given mime type
800      */

801     public void configure(SVGConverter c){
802         c.setSources(new String JavaDoc[] { SOURCE_FILE });
803         c.setDst(new File(DEST_FILE_NAME + dstType.getExtension()));
804         c.setDestinationType(dstType);
805     }
806 }
807
808
809 /**
810  * Provides a simple string array constructor which allows the user to
811  * create a test checking for a specific hint configuration.
812  */

813 class HintsConfigTest extends AbstractConfigTest {
814     static final String JavaDoc SOURCE_FILE = "samples/anne.svg";
815     static final String JavaDoc DEST_FILE_NAME = "samples/anne";
816     static final Class JavaDoc EXPECTED_TRANSCODER_CLASS = org.apache.batik.transcoder.image.PNGTranscoder.class;
817     static final DestinationType DST_TYPE = DestinationType.PNG;
818
819     /**
820      */

821     public HintsConfigTest(Object JavaDoc[][] hintsMap){
822         Config config = new Config();
823         config.transcoderClass = EXPECTED_TRANSCODER_CLASS;
824         
825         Vector sources = new Vector();
826         sources.addElement(new SVGConverterFileSource(new File(SOURCE_FILE)));
827         config.sources = sources;
828
829         Vector dest = new Vector();
830         dest.addElement(new File(DEST_FILE_NAME + DST_TYPE.getExtension()));
831         config.dest = dest;
832
833         HashMap hints = new HashMap();
834
835         //
836
// Add hints from constructor argument
837
//
838
int n = hintsMap.length;
839         for (int i=0; i<n; i++){
840             hints.put(hintsMap[i][0], hintsMap[i][1]);
841         }
842         config.hints = hints;
843                   
844         setExpectedConfig(config);
845     }
846
847     /**
848      * Configures the test with the given mime type
849      */

850     public void configure(SVGConverter c){
851         c.setSources(new String JavaDoc[] { SOURCE_FILE });
852         c.setDst(new File(DEST_FILE_NAME + DST_TYPE.getExtension()));
853         c.setDestinationType(DST_TYPE);
854         deltaConfigure(c);
855     }
856
857     protected void deltaConfigure(SVGConverter c){
858     }
859 }
860
861 /**
862  * Provides a simple string array constructor which allows the user to
863  * create a test checking for a specific source configuration.
864  * The constructor argument takes the list of expected files and the
865  * deltaConfigure method should set the sources which is expected to
866  * produce that list of sources. The sources should be file names
867  * which ommit the ".svg" extension.
868  */

869 class SourcesConfigTest extends AbstractConfigTest {
870     static final Class JavaDoc EXPECTED_TRANSCODER_CLASS = org.apache.batik.transcoder.image.PNGTranscoder.class;
871     static final DestinationType DST_TYPE = DestinationType.PNG;
872     static final String JavaDoc SVG_EXTENSION = ".svg";
873
874     /**
875      */

876     public SourcesConfigTest(Object JavaDoc[] expectedSources){
877         Config config = new Config();
878         config.transcoderClass = EXPECTED_TRANSCODER_CLASS;
879         
880         Vector sources = new Vector();
881         Vector dest = new Vector();
882         for (int i=0; i<expectedSources.length; i++){
883             sources.addElement(new SVGConverterFileSource(new File(expectedSources[i] + SVG_EXTENSION)));
884             dest.addElement(new File(expectedSources[i] + DST_TYPE.getExtension()));
885         }
886         config.sources = sources;
887         config.dest = dest;
888
889         HashMap hints = new HashMap();
890         config.hints = hints;
891                   
892         setExpectedConfig(config);
893     }
894
895     /**
896      * Configures the test with the given mime type
897      */

898     public void configure(SVGConverter c){
899         c.setDestinationType(DST_TYPE);
900         setSources(c);
901     }
902
903     protected void setSources(SVGConverter c){
904     }
905 }
906
907 /**
908  * Provides a simple string array constructor which allows the user to
909  * create a test checking for a specific destination configuration.
910  * The constructor argument takes the list of sources and the list of
911  * expected configuration which is influenced by the 'setDestination'
912  * content.
913  */

914 class DestConfigTest extends AbstractConfigTest {
915     static final Class JavaDoc EXPECTED_TRANSCODER_CLASS = org.apache.batik.transcoder.image.PNGTranscoder.class;
916     static final DestinationType DST_TYPE = DestinationType.PNG;
917     String JavaDoc[] sourcesStrings;
918     /**
919      */

920     public DestConfigTest(String JavaDoc[] sourcesStrings,
921                           String JavaDoc[] expectedDest){
922         this.sourcesStrings = sourcesStrings;
923         Config config = new Config();
924         config.transcoderClass = EXPECTED_TRANSCODER_CLASS;
925         
926         Vector sources = new Vector();
927         Vector dest = new Vector();
928         for (int i=0; i<sourcesStrings.length; i++){
929             sources.addElement(new SVGConverterFileSource(new File(sourcesStrings[i])));
930         }
931
932         for (int i=0; i<expectedDest.length; i++){
933             dest.addElement(new File(expectedDest[i]));
934         }
935
936         config.sources = sources;
937         config.dest = dest;
938
939         HashMap hints = new HashMap();
940         config.hints = hints;
941                   
942         setExpectedConfig(config);
943     }
944
945     /**
946      * Configures the test with the given mime type
947      */

948     public void configure(SVGConverter c){
949         c.setDestinationType(DST_TYPE);
950         c.setSources(sourcesStrings);
951         setDestination(c);
952     }
953
954     protected void setDestination(SVGConverter c){
955     }
956 }
957
958 /**
959  * This test lously checks that errors are reported as expected. It
960  * checks that the error code given at construction time is reported
961  * either my an exception thrown from the execute method or during the
962  * processing of single files in the SVGConverterController handler.
963  */

964 class ConfigErrorTest extends AbstractTest implements SVGConverterController{
965     String JavaDoc errorCode;
966
967     String JavaDoc foundErrorCode = null;
968
969     public static final String JavaDoc ERROR_DID_NOT_GET_ERROR
970         = "ConfigErrorTest.error.did.not.get.error";
971
972     public static final String JavaDoc ERROR_UNEXPECTED_ERROR_CODE
973         = "ConfigErrorTest.error.unexpected.error.code";
974
975     public static final String JavaDoc ENTRY_KEY_EXPECTED_ERROR_CODE
976         = "ConfigErrorTest.entry.key.expected.error.code";
977
978     public static final String JavaDoc ENTRY_KEY_GOT_ERROR_CODE
979         = "ConfigErrorTest.entry.key.got.error.code";
980
981     public ConfigErrorTest(String JavaDoc expectedErrorCode){
982         this.errorCode = expectedErrorCode;
983     }
984
985     public String JavaDoc getName(){
986         return getId();
987     }
988
989     public TestReport runImpl() throws Exception JavaDoc {
990         SVGConverter c = new SVGConverter(this);
991
992         c.setDestinationType(DestinationType.PNG);
993         c.setSources(new String JavaDoc[]{ "samples/anne.svg" });
994
995         configure(c);
996
997         try {
998             c.execute();
999         } catch(SVGConverterException e){
1000            e.printStackTrace();
1001            foundErrorCode = e.getErrorCode();
1002        }
1003
1004        if (foundErrorCode == null){
1005            TestReport report = reportError(ERROR_DID_NOT_GET_ERROR);
1006            report.addDescriptionEntry(ENTRY_KEY_EXPECTED_ERROR_CODE,
1007                                       errorCode);
1008            return report;
1009        }
1010
1011        if (foundErrorCode.equals(errorCode)){
1012            return reportSuccess();
1013        }
1014
1015        TestReport report = reportError(ERROR_UNEXPECTED_ERROR_CODE);
1016        report.addDescriptionEntry(ENTRY_KEY_EXPECTED_ERROR_CODE,
1017                                   errorCode);
1018        report.addDescriptionEntry(ENTRY_KEY_GOT_ERROR_CODE,
1019                                   foundErrorCode);
1020        return report;
1021    }
1022
1023    protected void configure(SVGConverter c){
1024    }
1025
1026    public boolean proceedWithComputedTask(Transcoder transcoder,
1027                                           Map hints,
1028                                           Vector sources,
1029                                           Vector dest){
1030        System.out.println("==================> Starting to process Task <=========================");
1031        return true;
1032    }
1033    
1034    public boolean proceedWithSourceTranscoding(SVGConverterSource source,
1035                                                File dest) {
1036        System.out.print("Transcoding " + source + " to " + dest + " ... ");
1037        return true;
1038    }
1039    
1040    public boolean proceedOnSourceTranscodingFailure(SVGConverterSource source,
1041                                                     File dest,
1042                                                     String JavaDoc errorCode){
1043        System.out.println(" ... FAILURE");
1044        foundErrorCode = errorCode;
1045        return true;
1046    }
1047
1048    public void onSourceTranscodingSuccess(SVGConverterSource source,
1049                                           File dest){
1050        System.out.println(" ... SUCCESS");
1051    }
1052}
1053
1054/**
1055 * This test checks that a file is indeed created and that it is identical to
1056 * an expected reference.
1057 */

1058class ConverterOutputTest extends AbstractTest {
1059    String JavaDoc svgSource;
1060    String JavaDoc pngDest;
1061    String JavaDoc pngRef;
1062
1063    public ConverterOutputTest(String JavaDoc svgSource,
1064                      String JavaDoc pngDest,
1065                      String JavaDoc pngRef){
1066        this.svgSource = svgSource;
1067        this.pngDest = pngDest;
1068        this.pngRef = pngRef;
1069    }
1070
1071    public TestReport runImpl() throws Exception JavaDoc {
1072        SVGConverter c = new SVGConverter();
1073        System.out.println("Converting : " + svgSource);
1074        c.setSources(new String JavaDoc[]{svgSource});
1075        c.setDst(new File(pngDest));
1076
1077        c.setDestinationType(DestinationType.PNG);
1078        
1079        c.execute();
1080
1081        ImageCompareTest t = new ImageCompareTest(pngDest,
1082                                                  pngRef);
1083        TestReport r = t.run();
1084        (new File(pngDest)).delete();
1085        return r;
1086    }
1087}
1088
Popular Tags