e.g. Calendar Search Help
You must enter a value before pressing Search
batik

Class: org.apache.batik.apps.rasterizer.SVGConverterTest   ©

 OK to copy?
0001 /*
0002 
0003  ============================================================================
0004                    The Apache Software License, Version 1.1
0005  ============================================================================
0006 
0007  Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
0008 
0009  Redistribution and use in source and binary forms, with or without modifica-
0010  tion, are permitted provided that the following conditions are met:
0011 
0012  1. Redistributions of  source code must  retain the above copyright  notice,
0013     this list of conditions and the following disclaimer.
0014 
0015  2. Redistributions in binary form must reproduce the above copyright notice,
0016     this list of conditions and the following disclaimer in the documentation
0017     and/or other materials provided with the distribution.
0018 
0019  3. The end-user documentation included with the redistribution, if any, must
0020     include  the following  acknowledgment:  "This product includes  software
0021     developed  by the  Apache Software Foundation  (http://www.apache.org/)."
0022     Alternately, this  acknowledgment may  appear in the software itself,  if
0023     and wherever such third-party acknowledgments normally appear.
0024 
0025  4. The names "Batik" and  "Apache Software Foundation" must  not  be
0026     used to  endorse or promote  products derived from  this software without
0027     prior written permission. For written permission, please contact
0028     apache@apache.org.
0029 
0030  5. Products  derived from this software may not  be called "Apache", nor may
0031     "Apache" appear  in their name,  without prior written permission  of the
0032     Apache Software Foundation.
0033 
0034  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
0035  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
0036  FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
0037  APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
0038  INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
0039  DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
0040  OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
0041  ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
0042  (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
0043  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0044 
0045  This software  consists of voluntary contributions made  by many individuals
0046  on  behalf of the Apache Software  Foundation. For more  information on the
0047  Apache Software Foundation, please see <http://www.apache.org/>.
0048 
0049 */
0050 
0051 package org.apache.batik.apps.rasterizer;
0052 
0053 import org.apache.batik.test.*;
0054 import org.apache.batik.test.util.ImageCompareTest;
0055 import org.apache.batik.transcoder.Transcoder;
0056 import org.apache.batik.transcoder.image.ImageTranscoder;
0057 import org.apache.batik.transcoder.image.JPEGTranscoder;
0058 import org.apache.batik.transcoder.image.PNGTranscoder;
0059 
0060 import java.awt.*;
0061 import java.io.*;
0062 import java.util.*;
0063 
0064 /**
0065  * Validates the operation of the <tt>SVGRasterizer</tt>.
0066  * It validates the option setting and the manipulation
0067  * of source and destination sources.
0068  *
0069  * @author <a href="mailto:vhardy@apache.org">Vincent Hardy</a>
0070  * @version $Id: SVGConverterTest.java,v 1.19 2003/08/08 11:39:37 vhardy Exp $
0071  */
0072 public class SVGConverterTest extends DefaultTestSuite {
0073     public SVGConverterTest(){
0074         ///////////////////////////////////////////////////////////////////////
0075         // Add configuration tests
0076         ///////////////////////////////////////////////////////////////////////
0077         AbstractTest t = null;
0078 
0079         //
0080         // Test Trancoder usage
0081         //
0082         t = new TranscoderConfigTest(DestinationType.PNG,
0083                                      org.apache.batik.transcoder.image.PNGTranscoder.class);
0084         addTest(t);
0085         t.setId("TranscoderConfigTest.PNG");
0086 
0087         t = new TranscoderConfigTest(DestinationType.JPEG,
0088                                      org.apache.batik.transcoder.image.JPEGTranscoder.class);
0089         addTest(t);
0090         t.setId("TranscoderConfigTest.JPEG");
0091 
0092         t = new TranscoderConfigTest(DestinationType.TIFF,
0093                                      org.apache.batik.transcoder.image.TIFFTranscoder.class);
0094         addTest(t);
0095         t.setId("TranscoderConfigTest.TIFF");
0096 
0097         try {
0098             Class pdfClass = Class.forName("org.apache.fop.svg.PDFTranscoder");
0099             t = new TranscoderConfigTest(DestinationType.PDF, pdfClass);
0100             t.setId("TranscoderConfigTest.PDF");
0101             addTest(t);
0102         } catch (Exception e) {
0103         }
0104 
0105         //
0106         // Checks that the proper hints are used
0107         //
0108         t = new HintsConfigTest(new Object[][]{ 
0109             {ImageTranscoder.KEY_AOI, new Rectangle(40, 50, 40, 80)}}){
0110                 protected void deltaConfigure(SVGConverter c){
0111                     c.setArea(new Rectangle(40, 50, 40, 80));
0112                 }
0113             };
0114         
0115         addTest(t);
0116         t.setId("HintsConfigTest.KEY_AOI");
0117         
0118         t = new HintsConfigTest(new Object[][]{
0119             {JPEGTranscoder.KEY_QUALITY, new Float(.5)}}){
0120                 protected void deltaConfigure(SVGConverter c){
0121                     c.setQuality(.5f);
0122                 }
0123             };
0124 
0125         addTest(t);
0126         t.setId("HintsConfigTest.KEY_QUALITY");
0127 
0128         t = new HintsConfigTest(new Object[][]{
0129             {PNGTranscoder.KEY_INDEXED, new Integer(8)}}){
0130                 protected void deltaConfigure(SVGConverter c){
0131                     c.setIndexed(8);
0132                 }
0133             };
0134         addTest(t);
0135         t.setId("HintsConfigTest.KEY_INDEXED");
0136 
0137         t = new HintsConfigTest(new Object[][]{
0138             {ImageTranscoder.KEY_BACKGROUND_COLOR, Color.pink}}){
0139                 protected void deltaConfigure(SVGConverter c){
0140                     c.setBackgroundColor(Color.pink);
0141                 }
0142             };
0143 
0144         addTest(t);
0145         t.setId("HintsConfigTest.KEY_BACKGROUND_COLOR");
0146 
0147         t = new HintsConfigTest(new Object[][]{
0148             {ImageTranscoder.KEY_HEIGHT, new Float(50)}}){
0149                 protected void deltaConfigure(SVGConverter c){
0150                     c.setHeight(50);
0151                 }
0152             };
0153 
0154         addTest(t);
0155         t.setId("HintsConfigTest.KEY_HEIGHT");
0156 
0157         t = new HintsConfigTest(new Object[][]{
0158             {ImageTranscoder.KEY_WIDTH, new Float(50)}}){
0159                 protected void deltaConfigure(SVGConverter c){
0160                     c.setWidth(50);
0161                 }
0162             };
0163 
0164         addTest(t);
0165         t.setId("HintsConfigTest.KEY_WIDTH");
0166 
0167         t = new HintsConfigTest(new Object[][]{
0168             {ImageTranscoder.KEY_MAX_HEIGHT, new Float(50)}}){
0169                 protected void deltaConfigure(SVGConverter c){
0170                     c.setMaxHeight(50);
0171                 }
0172             };
0173         addTest(t);
0174         t.setId("HintsConfigTest.KEY_MAX_HEIGHT");
0175 
0176         t = new HintsConfigTest(new Object[][]{
0177             {ImageTranscoder.KEY_MAX_WIDTH, new Float(50)}}){
0178                 protected void deltaConfigure(SVGConverter c){
0179                     c.setMaxWidth(50);
0180                 }
0181             };
0182         addTest(t);
0183         t.setId("HintsConfigTest.KEY_MAX_WIDTH");
0184 
0185         t = new HintsConfigTest(new Object[][]{
0186             {ImageTranscoder.KEY_MEDIA, "print"}}){
0187                 protected void deltaConfigure(SVGConverter c){
0188                     c.setMediaType("print");
0189                 }
0190             };
0191 
0192         addTest(t);
0193         t.setId("HintsConfigTest.KEY_MEDIA");
0194 
0195         t = new HintsConfigTest(new Object[][]{
0196             {ImageTranscoder.KEY_DEFAULT_FONT_FAMILY, "Times"}}){
0197                 protected void deltaConfigure(SVGConverter c){
0198                     c.setDefaultFontFamily("Times");
0199                 }
0200             };
0201 
0202         addTest(t);
0203         t.setId("HintsConfigTest.KEY_DEFAULT_FONT_FAMILY");
0204 
0205         t = new HintsConfigTest(new Object[][]{
0206             {ImageTranscoder.KEY_ALTERNATE_STYLESHEET, "myStyleSheet"}}){
0207                 protected void deltaConfigure(SVGConverter c){
0208                     c.setAlternateStylesheet("myStyleSheet");
0209                 }
0210             };
0211         addTest(t);
0212         t.setId("HintsConfigTest.KEY_ALTERNATE_STYLESHEET");
0213 
0214         t = new HintsConfigTest(new Object[][]{
0215             {ImageTranscoder.KEY_USER_STYLESHEET_URI, "userStylesheet.css"}}){
0216                 protected void deltaConfigure(SVGConverter c){
0217                     c.setUserStylesheet("userStylesheet.css");
0218                 }
0219             };
0220         addTest(t);
0221         t.setId("HintsConfigTest.KEY_USER_STYLESHEET_URI");
0222 
0223         t = new HintsConfigTest(new Object[][]{
0224             {ImageTranscoder.KEY_LANGUAGE, "fr"}}){
0225                 protected void deltaConfigure(SVGConverter c){
0226                     c.setLanguage("fr");
0227                 }
0228             };
0229         addTest(t);
0230         t.setId("HintsConfigTest.KEY_LANGUAGE");
0231 
0232         t = new HintsConfigTest(new Object[][]{
0233             {ImageTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER, new Float(.5f)}}){
0234                 protected void deltaConfigure(SVGConverter c){
0235                     c.setPixelUnitToMillimeter(.5f);
0236                 }
0237             };
0238         addTest(t);
0239         t.setId("HintsConfigTest.KEY_PIXEL_UNIT_TO_MILLIMETER");
0240 
0241         t = new HintsConfigTest(new Object[][]{
0242             {ImageTranscoder.KEY_XML_PARSER_VALIDATING, new Boolean(true)}}){
0243                 protected void deltaConfigure(SVGConverter c){
0244                     c.setValidate(true);
0245                 }
0246             };
0247         addTest(t);
0248         t.setId("HintsConfigTest.KEY_XML_PARSER_VALIDATING");
0249 
0250 
0251         //
0252         // Check sources
0253         //
0254         t = new SourcesConfigTest(new String[] { "samples/anne", "samples/batikFX", "samples/tests/spec/styling/smiley" }){
0255                 protected void setSources(SVGConverter c){
0256                     c.setSources(new String[] {"samples/anne.svg", "samples/batikFX.svg", "samples/tests/spec/styling/smiley.svg"});
0257                 }
0258             };
0259             
0260         addTest(t);
0261         t.setId("SourcesConfigTest.SimpleList");
0262         
0263 
0264         //
0265         // Check destination
0266         //
0267         t = new DestConfigTest(new String[] { "samples/anne.svg" },
0268                                new String[] { "test-reports/anne.png"}){
0269                 protected void setDestination(SVGConverter c){
0270                     c.setDst(new File("test-reports/anne.png"));
0271                 }
0272             };
0273         addTest(t);
0274         t.setId("DestConfigTest.DstFile");
0275 
0276         t = new DestConfigTest(new String[] { "samples/anne.svg", "samples/tests/spec/styling/smiley.svg" },
0277                                new String[] { "test-resources/anne.png", "test-resources/smiley.png"}){
0278                 protected void setDestination(SVGConverter c){
0279                     c.setDst(new File("test-resources"));
0280                 }
0281             };
0282         addTest(t);
0283         t.setId("DestConfigTest.DstDir");
0284 
0285         //
0286         // Check that complete process goes without error
0287         //
0288         t = new OperationTest(){
0289                 protected void configure(SVGConverter c){
0290                     c.setSources(new String[]{"samples/anne.svg"});
0291                     c.setDst(new File("anne.png"));
0292                     File file = new File("anne.png");
0293                     file.deleteOnExit();
0294                 }
0295             };
0296         addTest(t);
0297         t.setId("OperationTest.Bug4888");
0298 
0299         t = new OperationTest(){
0300                 protected void configure(SVGConverter c){
0301                     c.setDestinationType(DestinationType.PDF);
0302                     c.setSources(new String[]{"samples/anne.svg"});
0303                 }
0304             };
0305         addTest(t);
0306         t.setId("Operationtest.PDFTranscoding");
0307         
0308         ///////////////////////////////////////////////////////////////////////
0309         // Add configuration error test. These tests check that the expected
0310         // error gets reported for a given mis-configuration
0311         ///////////////////////////////////////////////////////////////////////
0312         t = new ConfigErrorTest(SVGConverter.ERROR_NO_SOURCES_SPECIFIED) {
0313                 protected void configure(SVGConverter c){
0314                     c.setSources(null);
0315                 }
0316             };
0317         addTest(t);
0318         t.setId("ConfigErrorTest.ERROR_NO_SOURCES_SPECIFIED");
0319 
0320         t = new ConfigErrorTest(SVGConverter.ERROR_CANNOT_COMPUTE_DESTINATION){
0321                 protected void configure(SVGConverter c){
0322                     // Do not set destination file or destination directory
0323                     c.setSources(new String[]{"http://xml.apache.org/batik/dummy.svg"});
0324                 }
0325             };
0326         addTest(t);
0327         t.setId("ConfigErrorTest.ERROR_CANNOT_COMPUTE_DESTINATION");
0328 
0329         t = new ConfigErrorTest(SVGConverter.ERROR_CANNOT_USE_DST_FILE){
0330                 protected void configure(SVGConverter c){
0331                     File dummy = null;
0332                     try {
0333                         dummy = File.createTempFile("dummyPNG", ".png");
0334                     } catch(IOException e){
0335                         throw new Error();
0336                     }
0337                     c.setSources(new String[]{"samples/anne.svg", "samples/batikFX.svg"});
0338                     c.setDst(dummy);
0339                     dummy.deleteOnExit();
0340                 }
0341             };
0342         addTest(t);
0343         t.setId("ConfigErrorTest.ERROR_CANNOT_USE_DST_FILE");
0344 
0345         t = new ConfigErrorTest(SVGConverter.ERROR_SOURCE_SAME_AS_DESTINATION){
0346                 protected void configure(SVGConverter c){
0347                     c.setSources(new String[]{ "samples/anne.svg" });
0348                     c.setDst(new File("samples/anne.svg"));
0349                 }
0350             };
0351         addTest(t);
0352         t.setId("ConfigErrorTest.ERROR_SOURCE_SAME_AS_DESTINATION");
0353 
0354         t = new ConfigErrorTest(SVGConverter.ERROR_CANNOT_READ_SOURCE){
0355                 protected void configure(SVGConverter c){
0356                     c.setSources(new String[]{ "test-resources/org/apache/batik/apps/rasterizer/notReadable.svg" });
0357                     c.setDst(new File("test-reports"));
0358                 }
0359 
0360                 public boolean proceedWithSourceTranscoding(SVGConverterSource source,
0361                                                             File dest){
0362                     // Big hack to simulate a non-readable SVG file
0363                     File hackedFile = new File(((SVGConverterFileSource)source).file.getPath()){
Rate0364                             public boolean canRead(){
0365                                 System.out.println("Yahoooooooo! In canRead");
0366                                 return false;
0367                             }
0368                         };
0369                     ((SVGConverterFileSource)source).file = hackedFile;
0370                     return true;
0371                 }
0372             };
0373         addTest(t);
0374         t.setId("ConfigErrorTest.ERROR_CANNOT_READ_SOURCE");
0375 
0376         t = new ConfigErrorTest(SVGConverter.ERROR_CANNOT_OPEN_SOURCE){
0377                 protected void configure(SVGConverter c){
0378                     c.setSources(new String[]{ "test-resources/org/apache/batik/apps/rasterizer/notReadable.svg" });
0379                 }
0380 
0381                 public boolean proceedWithComputedTask(Transcoder transcoder,
0382                                                        Map hints,
0383                                                        Vector sources,
0384                                                        Vector dest){
0385                     System.out.println("==================> Hacked Starting to process Task <=========================");
0386                     SVGConverterFileSource source = (SVGConverterFileSource)sources.elementAt(0);
0387                     source = new SVGConverterFileSource(source.file){
0388                             public InputStream openStream() throws FileNotFoundException {
0389                                 throw new FileNotFoundException("Simulated FileNotFoundException");
0390                             }
0391                         };
0392 
0393                     sources.setElementAt(source, 0);
0394                     return true;
0395                 }
0396 
0397             };
0398         addTest(t);
0399         t.setId("ConfigErrorTest.ERROR_CANNOT_OPEN_SOURCE");
0400 
0401         t = new ConfigErrorTest(SVGConverter.ERROR_OUTPUT_NOT_WRITEABLE){
0402                 protected void configure(SVGConverter c){
0403                     c.setSources(new String[]{ "samples/anne.svg" });
0404                     File o = new File("test-resources/org/apache/batik/apps/rasterizer/readOnly.png");
0405                     o.setReadOnly();
0406                     c.setDst(o);
0407                 }
0408             };
0409         addTest(t);
0410         t.setId("ConfigErrorTest.ERROR_OUTPUT_NOT_WRITEABLE");
0411                    
0412         t = new ConfigErrorTest(SVGConverter.ERROR_UNABLE_TO_CREATE_OUTPUT_DIR){
0413                 protected void configure(SVGConverter c){
0414                     c.setDst(new File("ZYZ::/cannotCreate"));
0415                 }
0416             };
0417         addTest(t);
0418         t.setId("ConfigErrorTest.ERROR_UNABLE_TO_CREATE_OUTPUT_DIR");
0419 
0420         t = new ConfigErrorTest(SVGConverter.ERROR_WHILE_RASTERIZING_FILE){
0421                 protected void configure(SVGConverter c){
0422                     c.setSources(new String[]{ "test-resources/org/apache/batik/apps/rasterizer/invalidSVG.svg"});
0423                 }
0424             };
0425         addTest(t);
0426         t.setId("ConfigErrorTest(SVGConverter.ERROR_WHILE_RASTERIZING_FILE");
0427         
0428         //
0429         // Test that files are created as expected and are producing the 
0430         // expected result.
0431         //
0432 
0433         // Plain file
0434         t = new ConverterOutputTest("samples/anne.svg",  // File to convert
0435                                     "test-reports/anne.png", // Output
0436                                     "test-references/samples/anne.png"); // reference
0437         addTest(t);
0438         t.setId("OutputTest.plain");
0439 
0440         // File with reference
0441         t = new ConverterOutputTest("samples/anne.svg#svgView(viewBox(0,0,100,200))",  // File to convert
0442                                     "test-reports/anne.png", // Output
0443                                     "test-references/samples/anneViewBox1.png"); // reference
0444         addTest(t);
0445         t.setId("OutputTest.reference");
0446 
0447     }
0448 }
0449 
0450 /**
0451  * A ConfigTest builds an SVGConverter, configures it,
0452  * sets itself as the SVGConverterController and checks that
0453  * the computed task is as expected (i.e., right set of 
0454  * hints).
0455  */
0456 abstract class AbstractConfigTest extends AbstractTest implements SVGConverterController {
0457     /**
0458      * The 'proceedWithComputedTask' handler was not called
0459      */
0460     public static final String ERROR_NO_COMPUTED_TASK
0461         = "ConfigTest.error.no.computed.task";
0462 
0463     /**
0464      * The transcoderClass is not the one expected.
0465      */
0466     public static final String ERROR_UNEXPECTED_TRANSCODER_CLASS
0467         = "ConfigTest.error.unexpected.transcoder.class";
0468 
0469     public static final String ENTRY_KEY_EXPECTED_TRANSCODER_CLASS
0470         = "ConfigTest.entry.key.expected.transcoder.class";
0471 
0472     public static final String ENTRY_KEY_COMPUTED_TRANSCODER_CLASS
0473         = "ConfigTest.entry.key.computed.trancoder.class";
0474 
0475     /**
0476      * Error if the hints do not match
0477      */
0478     public static final String ERROR_UNEXPECTED_NUMBER_OF_HINTS
0479         = "ConfigTest.error.unexpected.number.of.hints";
0480 
0481     public static final String ENTRY_KEY_EXPECTED_NUMBER_OF_HINTS
0482         = "ConfigTest.entry.key.expected.number.of.hints";
0483 
0484     public static final String ENTRY_KEY_COMPUTED_NUMBER_OF_HINTS
0485         = "ConfigTest.entry.key.computed.number.of.hints";
0486 
0487     public static final String ENTRY_KEY_EXPECTED_HINTS
0488         = "ConfigTest.entry.key.expected.hints";
0489 
0490     public static final String ENTRY_KEY_COMPUTED_HINTS
0491         = "ConfigTest.entry.key.computed.hints";
0492 
0493     public static final String ERROR_UNEXPECTED_TRANSCODING_HINT
0494         = "ConfigTest.error.unexpected.transcoding.hint";
0495 
0496     public static final String ENTRY_KEY_EXPECTED_HINT_KEY
0497         = "ConfigTest.entry.key.expected.hint.key";
0498 
0499     public static final String ENTRY_KEY_COMPUTED_HINT_VALUE
0500         = "ConfigTest.entry.key.computed.hint.value";
0501 
0502     public static final String ENTRY_KEY_EXPECTED_HINT_VALUE
0503         = "ConfigTest.entry.key.expected.hint.value";
0504 
0505     /**
0506      * Error if the sources do not match
0507      */
0508     public static final String ERROR_UNEXPECTED_SOURCES_LIST
0509         = "ConfigTest.error.unexpected.sources.list";
0510 
0511     public static final String ENTRY_KEY_EXPECTED_NUMBER_OF_SOURCES
0512         = "ConfigTest.entry.key.expected.number.of.sources";
0513 
0514     public static final String ENTRY_KEY_COMPUTED_NUMBER_OF_SOURCES
0515         = "ConfigTest.entry.key.computed.number.of.sources";
0516 
0517     public static final String ENTRY_KEY_EXPECTED_SOURCE_AT_INDEX
0518         = "ConfigTest.entry.key.expected.source.at.index";
0519 
0520     public static final String ENTRY_KEY_COMPUTED_SOURCE_AT_INDEX
0521         = "ConfigTest.entry.key.computed.source.at.index";
0522 
0523     public static final String ENTRY_KEY_COMPUTED_SOURCES_LIST
0524         = "ConfigTest.entry.key.computed.sources.list";
0525 
0526     public static final String ENTRY_KEY_EXPECTED_SOURCES_LIST
0527         = "ConfigTest.entry.key.expected.sources.list";
0528 
0529     /**
0530      * Error if the dest do not match
0531      */
0532     public static final String ERROR_UNEXPECTED_DEST_LIST
0533         = "ConfigTest.error.unexpected.dest.list";
0534 
0535     public static final String ENTRY_KEY_EXPECTED_NUMBER_OF_DEST
0536         = "ConfigTest.entry.key.expected.number.of.dest";
0537 
0538     public static final String ENTRY_KEY_COMPUTED_NUMBER_OF_DEST
0539         = "ConfigTest.entry.key.computed.number.of.dest";
0540 
0541     public static final String ENTRY_KEY_EXPECTED_DEST_AT_INDEX
0542         = "ConfigTest.entry.key.expected.dest.at.index";
0543 
0544     public static final String ENTRY_KEY_COMPUTED_DEST_AT_INDEX
0545         = "ConfigTest.entry.key.computed.dest.at.index";
0546 
0547     public static final String ENTRY_KEY_COMPUTED_DEST_LIST
0548         = "ConfigTest.entry.key.computed.dest.list";
0549 
0550     public static final String ENTRY_KEY_EXPECTED_DEST_LIST
0551         = "ConfigTest.entry.key.expected.dest.list";
0552 
0553 
0554     /**
0555      * Configuration Description
0556      */
0557     static class Config {
0558         Class transcoderClass;
0559         HashMap hints;
0560         Vector sources, dest;
0561     }
0562             
0563     protected Config expectedConfig;
0564     protected Config computedConfig;
0565 
0566     protected AbstractConfigTest(){
0567     }
0568 
0569     protected void setExpectedConfig(Config expectedConfig){
0570         this.expectedConfig = expectedConfig;
0571     }
0572 
0573     protected abstract void configure(SVGConverter c);
0574 
0575     protected String makeSourceList(Vector v){
0576         int n = v.size();
0577         StringBuffer sb = new StringBuffer();
0578         for (int i=0; i<n; i++){
0579             sb.append(v.elementAt(i).toString());
0580         }
0581 
0582         return sb.toString();
0583     }
0584 
0585     protected String makeHintsString(HashMap map){
0586         Iterator iter = map.keySet().iterator();
0587         StringBuffer sb = new StringBuffer();
0588         while (iter.hasNext()){
0589             Object key = iter.next();
0590             sb.append(key.toString());
0591             sb.append("(");
0592             sb.append(map.get(key).toString());
0593             sb.append(") -- ");
0594         }
0595 
0596         return sb.toString();
0597     }
0598 
0599 
0600     public String getName(){
0601         return getId();
0602     }
0603 
0604     public TestReport runImpl() throws Exception {
0605         SVGConverter c = new SVGConverter(this);
0606         configure(c);
0607         c.execute();
0608 
0609         //
0610         // Now, check that the expectedConfig and the 
0611         // computedConfig are identical
0612         //
0613         if (computedConfig == null){
0614             return reportError(ERROR_NO_COMPUTED_TASK);
0615         }
0616 
0617         if (!expectedConfig.transcoderClass.equals
0618             (computedConfig.transcoderClass)){
0619             TestReport report = reportError(ERROR_UNEXPECTED_TRANSCODER_CLASS);
0620             report.addDescriptionEntry(ENTRY_KEY_EXPECTED_TRANSCODER_CLASS,
0621                                        expectedConfig.transcoderClass);
0622             report.addDescriptionEntry(ENTRY_KEY_COMPUTED_TRANSCODER_CLASS,
0623                                        computedConfig.transcoderClass);
0624 
0625             return report;
0626         }
0627 
0628         // Compare sources
0629         int en = expectedConfig.sources.size();
0630         int cn = computedConfig.sources.size();
0631         
0632         if (en != cn){
0633             TestReport report = reportError(ERROR_UNEXPECTED_SOURCES_LIST);
0634             report.addDescriptionEntry(ENTRY_KEY_EXPECTED_NUMBER_OF_SOURCES,
0635                             "" + en);
0636             report.addDescriptionEntry(ENTRY_KEY_COMPUTED_NUMBER_OF_SOURCES,
0637                             "" + cn);
0638             report.addDescriptionEntry(ENTRY_KEY_EXPECTED_SOURCES_LIST,
0639                             makeSourceList(expectedConfig.sources));
0640 
0641             report.addDescriptionEntry(ENTRY_KEY_COMPUTED_SOURCES_LIST,
0642                             makeSourceList(computedConfig.sources));
0643 
0644             return report;
0645         }
0646 
0647         for (int i=0; i<en; i++){
0648             Object es = expectedConfig.sources.elementAt(i);
0649             Object cs = computedConfig.sources.elementAt(i);
0650             if (!computedConfig.sources.contains(es)){
0651                 TestReport report = reportError(ERROR_UNEXPECTED_SOURCES_LIST);
0652                 report.addDescriptionEntry(ENTRY_KEY_EXPECTED_SOURCE_AT_INDEX,
0653                                            "[" + i + "] = -" + es + "- (" + es.getClass().getName() + ")");
0654                 report.addDescriptionEntry(ENTRY_KEY_COMPUTED_SOURCE_AT_INDEX,
0655                                 "[" + i + "] = -" + cs + "- (" + es.getClass().getName() + ")");
0656                 report.addDescriptionEntry(ENTRY_KEY_EXPECTED_SOURCES_LIST,
0657                                 makeSourceList(expectedConfig.sources));
0658                 report.addDescriptionEntry(ENTRY_KEY_COMPUTED_SOURCES_LIST,
0659                                 makeSourceList(computedConfig.sources));
0660                 
0661                 return report;
0662             }
0663         }
0664 
0665         // Compare dest
0666         en = expectedConfig.dest.size();
0667         cn = computedConfig.dest.size();
0668 
0669         if (en != cn){
0670             TestReport report = reportError(ERROR_UNEXPECTED_DEST_LIST);
0671             report.addDescriptionEntry(ENTRY_KEY_EXPECTED_NUMBER_OF_DEST,
0672                             "" + en);
0673             report.addDescriptionEntry(ENTRY_KEY_COMPUTED_NUMBER_OF_DEST,
0674                             "" + cn);
0675             report.addDescriptionEntry(ENTRY_KEY_EXPECTED_DEST_LIST,
0676                             makeSourceList(expectedConfig.dest));
0677 
0678             report.addDescriptionEntry(ENTRY_KEY_COMPUTED_DEST_LIST,
0679                             makeSourceList(computedConfig.dest));
0680 
0681             return report;
0682         }
0683 
0684         for (int i=0; i<en; i++){
0685             Object es = expectedConfig.dest.elementAt(i);
0686             Object cs = computedConfig.dest.elementAt(i);
0687             if (!computedConfig.dest.contains(cs)){
0688                 TestReport report = reportError(ERROR_UNEXPECTED_DEST_LIST);
0689                 report.addDescriptionEntry(ENTRY_KEY_EXPECTED_DEST_AT_INDEX,
0690                                 "[" + i + "] = " + es);
0691                 report.addDescriptionEntry(ENTRY_KEY_COMPUTED_DEST_AT_INDEX,
0692                                 "[" + i + "] = " + cs);
0693                 report.addDescriptionEntry(ENTRY_KEY_EXPECTED_DEST_LIST,
0694                                 makeSourceList(expectedConfig.dest));
0695                 report.addDescriptionEntry(ENTRY_KEY_COMPUTED_DEST_LIST,
0696                                 makeSourceList(computedConfig.dest));
0697                 
0698                 return report;
0699             }
0700         }
0701 
0702         //
0703         // Finally, check the hints
0704         //
0705         en = expectedConfig.hints.size();
0706         cn = computedConfig.hints.size();
0707         
0708         if (en != cn){
0709             TestReport report = reportError(ERROR_UNEXPECTED_NUMBER_OF_HINTS);
0710             report.addDescriptionEntry(ENTRY_KEY_EXPECTED_NUMBER_OF_HINTS,
0711                                        "" + en);
0712             report.addDescriptionEntry(ENTRY_KEY_COMPUTED_NUMBER_OF_HINTS,
0713                                        "" + cn);
0714 
0715             report.addDescriptionEntry(ENTRY_KEY_EXPECTED_HINTS,
0716                                        makeHintsString(expectedConfig.hints));
0717             report.addDescriptionEntry(ENTRY_KEY_COMPUTED_HINTS,
0718                                        makeHintsString(computedConfig.hints));
0719             
0720             return report;
0721         }
0722 
0723         Iterator iter = expectedConfig.hints.keySet().iterator();
0724         while (iter.hasNext()){
0725             Object hintKey = iter.next();
0726             Object expectedHintValue = expectedConfig.hints.get(hintKey);
0727             
0728             Object computedHintValue = computedConfig.hints.get(hintKey);
0729             
0730             if (!expectedHintValue.equals(computedHintValue)){
0731                 TestReport report = reportError(ERROR_UNEXPECTED_TRANSCODING_HINT);
0732                 report.addDescriptionEntry(ENTRY_KEY_EXPECTED_HINT_KEY,
0733                                            hintKey.toString());
0734                 report.addDescriptionEntry(ENTRY_KEY_EXPECTED_HINT_VALUE,
0735                                            expectedHintValue.toString());
0736                 report.addDescriptionEntry(ENTRY_KEY_COMPUTED_HINT_VALUE,
0737                                            "" + computedHintValue);
0738                 report.addDescriptionEntry(ENTRY_KEY_EXPECTED_HINTS,
0739                                            makeHintsString(expectedConfig.hints));
0740                 report.addDescriptionEntry(ENTRY_KEY_COMPUTED_HINTS,
0741                                            makeHintsString(computedConfig.hints));
0742 
0743                 return report;
0744             }
0745         }
0746                                             
0747         return reportSuccess();
0748     
0749     }
0750 
0751     public boolean proceedWithComputedTask(Transcoder transcoder,
0752                                            Map hints,
0753                                            Vector sources,
0754                                            Vector dest){
0755         computedConfig = new Config();
0756         computedConfig.transcoderClass = transcoder.getClass();
0757         computedConfig.sources = (Vector)sources.clone();
0758         computedConfig.dest = (Vector)dest.clone();
0759         computedConfig.hints = new HashMap(hints);
0760         return false; // Do not proceed with the convertion process,
0761         // we are only checking the config in this test.
0762     }
0763 
0764     public boolean proceedWithSourceTranscoding(SVGConverterSource source,
0765                                                 File dest) {
0766         return true;
0767     }
0768         
0769     public boolean proceedOnSourceTranscodingFailure(SVGConverterSource source,
0770                                                      File dest,
0771                                                      String errorCode){
0772         return true;
0773     }
0774 
0775     public void onSourceTranscodingSuccess(SVGConverterSource source,
0776                                            File dest){
0777     }    
0778 }
0779 
0780 /**
0781  * Tests that a convertion task goes without exception.
0782  */
0783 class OperationTest extends AbstractTest{
0784     public TestReport runImpl() throws Exception {
0785         SVGConverter c = new SVGConverter();
0786         configure(c);
0787         c.execute();
0788         return reportSuccess();
0789     }
0790 
0791     protected void configure(SVGConverter c){
0792         // Should be overridden by subclasses.
0793     }
0794 }
0795 
0796 /**
0797  * Provides a simple string constructor which allows the user to 
0798  * create a given test to check that a specific transcoder class is 
0799  * used for a given mime type.
0800  */
0801 class TranscoderConfigTest extends AbstractConfigTest {
0802     static final String SOURCE_FILE = "samples/anne.svg";
0803     static final String DEST_FILE_NAME = "samples/anne";
0804 
0805     protected DestinationType dstType;
0806     /**
0807      * @param dstType type of result image
0808      * @param expectedTranscoderClass class for the Transcoder expected to perform
0809      *        the convertion.
0810      */
0811     public TranscoderConfigTest(DestinationType dstType,
0812                                 Class expectedTranscoderClass){
0813         this.dstType = dstType;
0814 
0815         Config config = new Config();
0816         config.transcoderClass = expectedTranscoderClass;
0817         
0818         Vector sources = new Vector();
0819         sources.addElement(new SVGConverterFileSource(new File(SOURCE_FILE)));
0820         config.sources = sources;
0821 
0822         Vector dest = new Vector();
0823         dest.addElement(new File(DEST_FILE_NAME + dstType.getExtension()));
0824         config.dest = dest;
0825 
0826         HashMap hints = new HashMap();
0827         config.hints = hints;
0828                   
0829         setExpectedConfig(config);
0830     }
0831 
0832     /**
0833      * Configures the test with the given mime type 
0834      */
0835     public void configure(SVGConverter c){
0836         c.setSources(new String[] { SOURCE_FILE });
0837         c.setDst(new File(DEST_FILE_NAME + dstType.getExtension()));
0838         c.setDestinationType(dstType);
0839     }
0840 }
0841 
0842 
0843 /**
0844  * Provides a simple string array constructor which allows the user to 
0845  * create a test checking for a specific hint configuration.
0846  */
0847 class HintsConfigTest extends AbstractConfigTest {
0848     static final String SOURCE_FILE = "samples/anne.svg";
0849     static final String DEST_FILE_NAME = "samples/anne";
0850     static final Class EXPECTED_TRANSCODER_CLASS = org.apache.batik.transcoder.image.PNGTranscoder.class;
0851     static final DestinationType DST_TYPE = DestinationType.PNG;
0852 
0853     /**
0854      */
0855     public HintsConfigTest(Object[][] hintsMap){
0856         Config config = new Config();
0857         config.transcoderClass = EXPECTED_TRANSCODER_CLASS;
0858         
0859         Vector sources = new Vector();
0860         sources.addElement(new SVGConverterFileSource(new File(SOURCE_FILE)));
0861         config.sources = sources;
0862 
0863         Vector dest = new Vector();
0864         dest.addElement(new File(DEST_FILE_NAME + DST_TYPE.getExtension()));
0865         config.dest = dest;
0866 
0867         HashMap hints = new HashMap();
0868 
0869         //
0870         // Add hints from constructor argument
0871         //
0872         int n = hintsMap.length;
0873         for (int i=0; i<n; i++){
0874             hints.put(hintsMap[i][0], hintsMap[i][1]);
0875         }
0876         config.hints = hints;
0877                   
0878         setExpectedConfig(config);
0879     }
0880 
0881     /**
0882      * Configures the test with the given mime type 
0883      */
0884     public void configure(SVGConverter c){
0885         c.setSources(new String[] { SOURCE_FILE });
0886         c.setDst(new File(DEST_FILE_NAME + DST_TYPE.getExtension()));
0887         c.setDestinationType(DST_TYPE);
0888         deltaConfigure(c);
0889     }
0890 
0891     protected void deltaConfigure(SVGConverter c){
0892     }
0893 }
0894 
0895 /**
0896  * Provides a simple string array constructor which allows the user to 
0897  * create a test checking for a specific source configuration.
0898  * The constructor argument takes the list of expected files and the 
0899  * deltaConfigure method should set the sources which is expected to 
0900  * produce that list of sources. The sources should be file names 
0901  * which ommit the ".svg" extension.
0902  */
0903 class SourcesConfigTest extends AbstractConfigTest {
0904     static final Class EXPECTED_TRANSCODER_CLASS = org.apache.batik.transcoder.image.PNGTranscoder.class;
0905     static final DestinationType DST_TYPE = DestinationType.PNG;
0906     static final String SVG_EXTENSION = ".svg";
0907 
0908     /**
0909      */
0910     public SourcesConfigTest(Object[] expectedSources){
0911         Config config = new Config();
0912         config.transcoderClass = EXPECTED_TRANSCODER_CLASS;
0913         
0914         Vector sources = new Vector();
0915         Vector dest = new Vector();
0916         for (int i=0; i<expectedSources.length; i++){
0917             sources.addElement(new SVGConverterFileSource(new File(expectedSources[i] + SVG_EXTENSION)));
0918             dest.addElement(new File(expectedSources[i] + DST_TYPE.getExtension()));
0919         }
0920         config.sources = sources;
0921         config.dest = dest;
0922 
0923         HashMap hints = new HashMap();
0924         config.hints = hints;
0925                   
0926         setExpectedConfig(config);
0927     }
0928 
0929     /**
0930      * Configures the test with the given mime type 
0931      */
0932     public void configure(SVGConverter c){
0933         c.setDestinationType(DST_TYPE);
0934         setSources(c);
0935     }
0936 
0937     protected void setSources(SVGConverter c){
0938     }
0939 }
0940 
0941 /**
0942  * Provides a simple string array constructor which allows the user to 
0943  * create a test checking for a specific destination configuration.
0944  * The constructor argument takes the list of sources and the list of
0945  * expected configuration which is influenced by the 'setDestination'
0946  * content.
0947  */
0948 class DestConfigTest extends AbstractConfigTest {
0949     static final Class EXPECTED_TRANSCODER_CLASS = org.apache.batik.transcoder.image.PNGTranscoder.class;
0950     static final DestinationType DST_TYPE = DestinationType.PNG;
0951     String[] sourcesStrings;
0952     /**
0953      */
0954     public DestConfigTest(String[] sourcesStrings,
0955                           String[] expectedDest){
0956         this.sourcesStrings = sourcesStrings;
0957         Config config = new Config();
0958         config.transcoderClass = EXPECTED_TRANSCODER_CLASS;
0959         
0960         Vector sources = new Vector();
0961         Vector dest = new Vector();
0962         for (int i=0; i<sourcesStrings.length; i++){
0963             sources.addElement(new SVGConverterFileSource(new File(sourcesStrings[i])));
0964         }
0965 
0966         for (int i=0; i<expectedDest.length; i++){
0967             dest.addElement(new File(expectedDest[i]));
0968         }
0969 
0970         config.sources = sources;
0971         config.dest = dest;
0972 
0973         HashMap hints = new HashMap();
0974         config.hints = hints;
0975                   
0976         setExpectedConfig(config);
0977     }
0978 
0979     /**
0980      * Configures the test with the given mime type 
0981      */
0982     public void configure(SVGConverter c){
0983         c.setDestinationType(DST_TYPE);
0984         c.setSources(sourcesStrings);
0985         setDestination(c);
0986     }
0987 
0988     protected void setDestination(SVGConverter c){
0989     }
0990 }
0991 
0992 /**
0993  * This test lously checks that errors are reported as expected. It
0994  * checks that the error code given at construction time is reported
0995  * either my an exception thrown from the execute method or during the
0996  * processing of single files in the SVGConverterController handler.
0997  */
0998 class ConfigErrorTest extends AbstractTest implements SVGConverterController{
0999     String errorCode;
1000 
1001     String foundErrorCode = null;
1002 
1003     public static final String ERROR_DID_NOT_GET_ERROR
1004         = "ConfigErrorTest.error.did.not.get.error";
1005 
1006     public static final String ERROR_UNEXPECTED_ERROR_CODE
1007         = "ConfigErrorTest.error.unexpected.error.code";
1008 
1009     public static final String ENTRY_KEY_EXPECTED_ERROR_CODE
1010         = "ConfigErrorTest.entry.key.expected.error.code";
1011 
1012     public static final String ENTRY_KEY_GOT_ERROR_CODE
1013         = "ConfigErrorTest.entry.key.got.error.code";
1014 
1015     public ConfigErrorTest(String expectedErrorCode){
1016         this.errorCode = expectedErrorCode;
1017     }
1018 
1019     public String getName(){
1020         return getId();
1021     }
1022 
1023     public TestReport runImpl() throws Exception {
1024         SVGConverter c = new SVGConverter(this);
1025 
1026         c.setDestinationType(DestinationType.PNG);
1027         c.setSources(new String[]{ "samples/anne.svg" });
1028 
1029         configure(c);
1030 
1031         try {
1032             c.execute();
1033         } catch(SVGConverterException e){
1034             e.printStackTrace();
1035             foundErrorCode = e.getErrorCode();
1036         }
1037 
1038         if (foundErrorCode == null){
1039             TestReport report = reportError(ERROR_DID_NOT_GET_ERROR);
1040             report.addDescriptionEntry(ENTRY_KEY_EXPECTED_ERROR_CODE,
1041                                        errorCode);
1042             return report;
1043         }
1044 
1045         if (foundErrorCode.equals(errorCode)){
1046             return reportSuccess();
1047         }
1048 
1049         TestReport report = reportError(ERROR_UNEXPECTED_ERROR_CODE);
1050         report.addDescriptionEntry(ENTRY_KEY_EXPECTED_ERROR_CODE,
1051                                    errorCode);
1052         report.addDescriptionEntry(ENTRY_KEY_GOT_ERROR_CODE,
1053                                    foundErrorCode);
1054         return report;
1055     }
1056 
1057     protected void configure(SVGConverter c){
1058     }
1059 
1060     public boolean proceedWithComputedTask(Transcoder transcoder,
1061                                            Map hints,
1062                                            Vector sources,
1063                                            Vector dest){
1064         System.out.println("==================> Starting to process Task <=========================");
1065         return true;
1066     }
1067     
1068     public boolean proceedWithSourceTranscoding(SVGConverterSource source,
1069                                                 File dest) {
1070         System.out.print("Transcoding " + source + " to " + dest + " ... ");
1071         return true;
1072     }
1073     
1074     public boolean proceedOnSourceTranscodingFailure(SVGConverterSource source,
1075                                                      File dest,
1076                                                      String errorCode){
1077         System.out.println(" ... FAILURE");
1078         foundErrorCode = errorCode;
1079         return true;
1080     }
1081 
1082     public void onSourceTranscodingSuccess(SVGConverterSource source,
1083                                            File dest){
1084         System.out.println(" ... SUCCESS");
1085     }
1086 }
1087 
1088 /**
1089  * This test checks that a file is indeed created and that it is identical to
1090  * an expected reference.
1091  */
1092 class ConverterOutputTest extends AbstractTest {
1093     String svgSource;
1094     String pngDest;
1095     String pngRef;
1096 
1097     public ConverterOutputTest(String svgSource,
1098                       String pngDest,
1099                       String pngRef){
1100         this.svgSource = svgSource;
1101         this.pngDest = pngDest;
1102         this.pngRef = pngRef;
1103     }
1104 
1105     public TestReport runImpl() throws Exception {
1106         SVGConverter c = new SVGConverter();
1107         System.out.println("Converting : " + svgSource);
1108         c.setSources(new String[]{svgSource});
1109         c.setDst(new File(pngDest));
1110 
1111         c.setDestinationType(DestinationType.PNG);
1112         
1113         c.execute();
1114 
1115         ImageCompareTest t = new ImageCompareTest(pngDest,
1116                                                   pngRef);
1117         TestReport r = t.run();
1118         (new File(pngDest)).delete();
1119         return r;
1120     }
1121 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
364 0% of 0