KickJava   Java API By Example, From Geeks To Geeks.

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


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

18 package org.apache.batik.svggen;
19
20 import java.awt.Dimension JavaDoc;
21 import java.awt.Font JavaDoc;
22 import java.io.StringWriter JavaDoc;
23
24 import org.w3c.dom.DOMImplementation JavaDoc;
25 import org.w3c.dom.Document JavaDoc;
26
27 import org.apache.batik.dom.GenericDOMImplementation;
28 import org.apache.batik.svggen.SVGGeneratorContext.GraphicContextDefaults;
29 import org.apache.batik.test.AbstractTest;
30 import org.apache.batik.test.TestReport;
31 import org.apache.batik.util.SVGConstants;
32
33 /**
34  * This test validates that the SVGGraphics2D generates the same result
35  * with the two versions of its getRoot method.
36  *
37  * @author <a HREF="mailto:vhardy@apache.org">Vincent Hardy</a>
38  * @version $Id: GetRootTest.java,v 1.4 2004/08/18 07:16:44 vhardy Exp $
39  */

40 public class GetRootTest extends AbstractTest implements SVGConstants {
41     public static final Dimension JavaDoc CANVAS_SIZE
42         = new Dimension JavaDoc(300, 400);
43
44     public static final String JavaDoc ERROR_DIFFERENT_SVG_OUTPUT
45         = "GetRootTest.error.different.svg.output";
46
47     public static final String JavaDoc ENTRY_KEY_NO_ARG_OUTPUT
48         = "GetRootTest.entry.key.no.arg.output";
49
50     public static final String JavaDoc ENTRY_KEY_SVG_ARG_OUTPUT
51         = "GetRootTest.entry.key.svg.arg.output";
52
53     public TestReport runImpl() throws Exception JavaDoc {
54         // First, use the no-argument getRoot
55

56         DOMImplementation JavaDoc impl = GenericDOMImplementation.getDOMImplementation();
57         String JavaDoc namespaceURI = SVGConstants.SVG_NAMESPACE_URI;
58         Document JavaDoc domFactory = impl.createDocument(namespaceURI, SVG_SVG_TAG, null);
59         SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(domFactory);
60         GraphicContextDefaults defaults
61             = new GraphicContextDefaults();
62         defaults.font = new Font JavaDoc("Arial", Font.PLAIN, 12);
63         ctx.setGraphicContextDefaults(defaults);
64         SVGGraphics2D g2d = new SVGGraphics2D(ctx, false);
65
66         g2d.setSVGCanvasSize(CANVAS_SIZE);
67
68         Painter painter = new BasicShapes();
69         painter.paint(g2d);
70
71         StringWriter JavaDoc swA = new StringWriter JavaDoc();
72         g2d.stream(g2d.getRoot(), swA);
73
74         // Now, use the getRoot with argument
75
domFactory = impl.createDocument(namespaceURI, SVG_SVG_TAG, null);
76         ctx = SVGGeneratorContext.createDefault(domFactory);
77         ctx.setGraphicContextDefaults(defaults);
78         g2d = new SVGGraphics2D(ctx, false);
79
80         g2d.setSVGCanvasSize(CANVAS_SIZE);
81
82         painter.paint(g2d);
83
84         StringWriter JavaDoc swB = new StringWriter JavaDoc();
85         g2d.stream(g2d.getRoot(domFactory.getDocumentElement()),
86                    swB);
87
88         // Compare the two output: they should be identical
89
if (swA.toString().equals(swB.toString())) {
90             return reportSuccess();
91         } else {
92             TestReport report = reportError(ERROR_DIFFERENT_SVG_OUTPUT);
93             report.addDescriptionEntry(ENTRY_KEY_NO_ARG_OUTPUT,
94                                        swA.toString());
95             report.addDescriptionEntry(ENTRY_KEY_SVG_ARG_OUTPUT,
96                                        swB.toString());
97             return report;
98         }
99     }
100 }
101
Popular Tags