KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Graphics2D JavaDoc;
21 import java.awt.Color JavaDoc;
22
23 import java.net.URL JavaDoc;
24 import java.io.File JavaDoc;
25
26 import org.apache.batik.test.DefaultTestSuite;
27 import org.apache.batik.test.Test;
28 import org.apache.batik.test.TestReportValidator;
29 import org.apache.batik.test.TestReport;
30
31 /**
32  * Validates the operation of the <tt>SVGAccuractyTest</tt> class
33  *
34  * @author <a HREF="mailto:vhardy@apache.org">Vincent Hardy</a>
35  * @version $Id: SVGAccuracyTestValidator.java,v 1.3 2004/08/18 07:16:45 vhardy Exp $
36  */

37 public class SVGAccuracyTestValidator extends DefaultTestSuite {
38     /**
39      * Checks that test fails if:
40      * + Rendering sequence generates an exception
41      * + There is no reference image
42      * + Reference SVG differs from the generated SVG
43      * Checks that test works if SVG and reference SVG
44      * are identical
45      */

46     public SVGAccuracyTestValidator(){
47         addTest(new NullPainter());
48         addTest(new PainterWithException());
49         addTest(new NullReferenceURL());
50         addTest(new InexistantReferenceURL());
51         addTest(new DiffWithReferenceImage());
52         addTest(new SameAsReferenceImage());
53     }
54
55     static class NullPainter extends TestReportValidator {
56         public TestReport runImpl() throws Exception JavaDoc {
57             Painter painter = null;
58             URL JavaDoc refURL = new URL JavaDoc("http",
59                                  "dummyHost",
60                                  "dummyFile.svg");
61
62             Test t
63                 = new SVGAccuracyTest(painter, refURL);
64             
65             setConfig(t,
66                       false,
67                       SVGAccuracyTest.ERROR_CANNOT_GENERATE_SVG);
68
69             return super.runImpl();
70         }
71     }
72
73     static class PainterWithException extends TestReportValidator
74         implements Painter {
75
76         public void paint(Graphics2D JavaDoc g){
77             g.setComposite(null); // Will cause the exception
78
g.fillRect(0, 0, 20, 20);
79         }
80
81         public TestReport runImpl() throws Exception JavaDoc {
82             Painter painter = this;
83             URL JavaDoc refURL = new URL JavaDoc("http",
84                                  "dummyHost",
85                                  "dummyFile.svg");
86             Test t = new SVGAccuracyTest(painter, refURL);
87             
88             setConfig(t,
89                       false,
90                       SVGAccuracyTest.ERROR_CANNOT_GENERATE_SVG);
91
92             return super.runImpl();
93         }
94     }
95
96     static class ValidPainterTest extends TestReportValidator
97         implements Painter{
98         
99         public void paint(Graphics2D JavaDoc g){
100             g.setPaint(Color.red);
101             g.fillRect(0, 0, 40, 40);
102         }
103     }
104
105     static class NullReferenceURL extends ValidPainterTest {
106         public TestReport runImpl() throws Exception JavaDoc {
107             Test t = new SVGAccuracyTest(this, null);
108
109             setConfig(t,
110                       false,
111                       SVGAccuracyTest.ERROR_CANNOT_OPEN_REFERENCE_SVG_FILE);
112
113             return super.runImpl();
114         }
115     }
116
117     static class InexistantReferenceURL extends ValidPainterTest {
118         public TestReport runImpl() throws Exception JavaDoc {
119             Test t = new SVGAccuracyTest(this,
120                                          new URL JavaDoc("http",
121                                                  "dummyHost",
122                                                  "dummyFile.svg"));
123
124             setConfig(t,
125                       false,
126                       SVGAccuracyTest.ERROR_CANNOT_OPEN_REFERENCE_SVG_FILE);
127
128             return super.runImpl();
129         }
130     }
131
132     static class DiffWithReferenceImage extends ValidPainterTest {
133         public TestReport runImpl() throws Exception JavaDoc {
134             File JavaDoc tmpFile = File.createTempFile("EmptySVGReference",
135                                                null);
136             tmpFile.deleteOnExit();
137
138             Test t = new SVGAccuracyTest(this,
139                                          tmpFile.toURL());
140
141             setConfig(t,
142                       false,
143                       SVGAccuracyTest.ERROR_GENERATED_SVG_INACCURATE);
144
145             return super.runImpl();
146         }
147     }
148
149     static class SameAsReferenceImage extends ValidPainterTest {
150         public TestReport runImpl() throws Exception JavaDoc {
151             File JavaDoc tmpFile = File.createTempFile("SVGReference",
152                                                null);
153             tmpFile.deleteOnExit();
154
155             SVGAccuracyTest t = new SVGAccuracyTest(this,
156                                                     tmpFile.toURL());
157             
158             t.setSaveSVG(tmpFile);
159
160             setConfig(t,
161                       false,
162                       SVGAccuracyTest.ERROR_GENERATED_SVG_INACCURATE);
163
164             // This first run should fail but it should
165
// have created the reference image in tmpFile
166
super.runImpl();
167
168             // Second run should work because the reference
169
// image should match
170
setConfig(t,
171                       true,
172                       null);
173
174             return super.runImpl();
175         }
176     }
177
178 }
179
Popular Tags