KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > svggen > SVGGeneratorTests


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

18 package org.apache.batik.svggen;
19
20 import java.io.File JavaDoc;
21 import java.net.URL JavaDoc;
22
23 import org.apache.batik.test.Test;
24 import org.apache.batik.test.DefaultTestSuite;
25 import org.apache.batik.test.svg.SVGRenderingAccuracyTest;
26 import org.apache.batik.test.util.ImageCompareTest;
27
28 /**
29  * This test validates that a given rendering sequence, modeled
30  * by a <tt>Painter</tt> by doing several subtests for a
31  * single input class:
32  * + SVGAccuracyTest with several configurations
33  * + SVGRenderingAccuracyTest
34  * + ImageComparisonTest between the rendering of the generated
35  * SVG for various configurations.
36  *
37  * @author <a HREF="mailto:vhardy@apache.org">Vincent Hardy</a>
38  * @version $Id: SVGGeneratorTests.java,v 1.6 2004/08/18 07:16:45 vhardy Exp $
39  */

40 public class SVGGeneratorTests extends DefaultTestSuite {
41     public static final String JavaDoc GENERATOR_REFERENCE_BASE
42         = "test-references/org/apache/batik/svggen/";
43
44     public static final String JavaDoc RENDERING_DIR
45         = "rendering";
46
47     public static final String JavaDoc ACCEPTED_VARIATION_DIR
48         = "accepted-variation";
49
50     public static final String JavaDoc CANDIDATE_VARIATION_DIR
51         = "candidate-variation";
52
53     public static final String JavaDoc CANDIDATE_REF_DIR
54         = "candidate-ref";
55
56     public static final String JavaDoc RENDERING_CANDIDATE_REF_DIR
57         = "candidate-reference";
58
59     public static final String JavaDoc PNG_EXTENSION
60         = ".png";
61
62     public static final String JavaDoc SVG_EXTENSION
63         = ".svg";
64
65     public static final String JavaDoc PLAIN_GENERATION_PREFIX = "";
66
67     public static final String JavaDoc CUSTOM_CONTEXT_GENERATION_PREFIX = "Context";
68
69     /**
70      * @param name of the <tt>Painter</tt> class
71      */

72     public SVGGeneratorTests(){
73     }
74
75     /**
76      * The id should be the Painter's class name
77      * prefixed with the package name defined in
78      * getPackageName
79      */

80     public void setId(String JavaDoc id){
81         super.setId(id);
82         String JavaDoc clName = getPackageName() + "." + id;
83         Class JavaDoc cl = null;
84
85         try{
86             cl = Class.forName(clName);
87         }catch(ClassNotFoundException JavaDoc e){
88             throw new IllegalArgumentException JavaDoc(clName);
89         }
90         
91         Object JavaDoc o = null;
92
93         try {
94             o = cl.newInstance();
95         }catch(Exception JavaDoc e){
96             throw new IllegalArgumentException JavaDoc(clName);
97         }
98
99         if(!(o instanceof Painter)){
100             throw new IllegalArgumentException JavaDoc(clName);
101         }
102
103         Painter painter = (Painter)o;
104
105         addTest(makeSVGAccuracyTest(painter, id));
106         addTest(makeGeneratorContext(painter, id));
107         addTest(makeSVGRenderingAccuracyTest(painter, id, PLAIN_GENERATION_PREFIX));
108         addTest(makeSVGRenderingAccuracyTest(painter, id, CUSTOM_CONTEXT_GENERATION_PREFIX));
109         addTest(makeImageCompareTest(painter, id, PLAIN_GENERATION_PREFIX,
110                                      CUSTOM_CONTEXT_GENERATION_PREFIX));
111     }
112
113     /**
114      * For the Generator test, the relevant name is the id
115      */

116     public String JavaDoc getName(){
117         return "SVGGeneratorTest - " + getId();
118     }
119         
120     protected String JavaDoc getPackageName(){
121         return "org.apache.batik.svggen";
122     }
123
124     private Test makeImageCompareTest(Painter painter,
125                                       String JavaDoc id,
126                                       String JavaDoc prefixA,
127                                       String JavaDoc prefixB){
128         String JavaDoc cl = getNonQualifiedClassName(painter);
129         String JavaDoc clA = prefixA + cl;
130         String JavaDoc clB = prefixB + cl;
131         String JavaDoc testReferenceA = GENERATOR_REFERENCE_BASE + RENDERING_DIR + "/" + clA + PNG_EXTENSION;
132         String JavaDoc testReferenceB = GENERATOR_REFERENCE_BASE + RENDERING_DIR + "/" + clB + PNG_EXTENSION;
133         ImageCompareTest t = new ImageCompareTest(testReferenceA, testReferenceB);
134         t.setName(id + "-RenderingComparison");
135         t.setId(id + ".renderingComparison");
136         return t;
137     }
138
139     private Test makeSVGRenderingAccuracyTest(Painter painter, String JavaDoc id, String JavaDoc prefix){
140         String JavaDoc cl = prefix + getNonQualifiedClassName(painter);
141         String JavaDoc testSource = GENERATOR_REFERENCE_BASE + cl + SVG_EXTENSION;
142         String JavaDoc testReference = GENERATOR_REFERENCE_BASE + RENDERING_DIR + "/" + cl + PNG_EXTENSION;
143         String JavaDoc variationURL = GENERATOR_REFERENCE_BASE + RENDERING_DIR + "/" + ACCEPTED_VARIATION_DIR + "/" + cl + PNG_EXTENSION;
144         String JavaDoc saveVariation = GENERATOR_REFERENCE_BASE + RENDERING_DIR + "/" + CANDIDATE_VARIATION_DIR + "/" + cl + PNG_EXTENSION;
145         String JavaDoc candidateReference = GENERATOR_REFERENCE_BASE + RENDERING_DIR + "/" + RENDERING_CANDIDATE_REF_DIR + "/" + cl + PNG_EXTENSION;
146
147         SVGRenderingAccuracyTest test = new SVGRenderingAccuracyTest(testSource, testReference);
148         test.setVariationURL(variationURL);
149         test.setSaveVariation(new File JavaDoc(saveVariation));
150         test.setCandidateReference(new File JavaDoc(candidateReference));
151
152         test.setName(id + "-" + prefix + "RenderingCheck");
153         test.setId(id + "." + prefix + "renderingCheck");
154         return test;
155     }
156
157     private Test makeGeneratorContext(Painter painter, String JavaDoc id){
158         String JavaDoc cl = CUSTOM_CONTEXT_GENERATION_PREFIX + getNonQualifiedClassName(painter);
159
160         GeneratorContext test
161             = new GeneratorContext(painter, makeURL(painter, CUSTOM_CONTEXT_GENERATION_PREFIX));
162
163         test.setSaveSVG(new File JavaDoc(GENERATOR_REFERENCE_BASE + CANDIDATE_REF_DIR + "/" + cl + SVG_EXTENSION));
164         test.setName(id + "-ConfiguredContextGeneration");
165         test.setId(id + ".configuredContextGeneration");
166         return test;
167     }
168
169     private Test makeSVGAccuracyTest(Painter painter, String JavaDoc id){
170         String JavaDoc cl = PLAIN_GENERATION_PREFIX + getNonQualifiedClassName(painter);
171
172         SVGAccuracyTest test
173             = new SVGAccuracyTest(painter, makeURL(painter, PLAIN_GENERATION_PREFIX));
174
175         test.setSaveSVG(new File JavaDoc(GENERATOR_REFERENCE_BASE + CANDIDATE_REF_DIR + "/" + cl + SVG_EXTENSION));
176         test.setName(id + "-DefaultContextGeneration");
177         test.setId(id + ".defaultContextGeneration");
178         return test;
179     }
180
181     private String JavaDoc getNonQualifiedClassName(Painter painter){
182         String JavaDoc cl = painter.getClass().getName();
183         int n = cl.lastIndexOf(".");
184         return cl.substring(n+1);
185     }
186
187     private URL JavaDoc makeURL(Painter painter, String JavaDoc prefix){
188         String JavaDoc urlString = "file:" + GENERATOR_REFERENCE_BASE
189             + prefix + getNonQualifiedClassName(painter) + SVG_EXTENSION;
190         URL JavaDoc url = null;
191         try{
192             url = new URL JavaDoc(urlString);
193         }catch(Exception JavaDoc e){
194             throw new Error JavaDoc(); // Should not happen
195
}
196
197         return url;
198     }
199 }
200
201
Popular Tags