KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > GenericFOPTestCase


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 /* $Id: GenericFOPTestCase.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.StringReader JavaDoc;
24 import java.security.DigestOutputStream JavaDoc;
25 import java.security.MessageDigest JavaDoc;
26 import java.util.Date JavaDoc;
27
28 import javax.xml.parsers.SAXParserFactory JavaDoc;
29
30 import junit.framework.Test;
31 import junit.framework.TestCase;
32 import junit.framework.TestSuite;
33
34 import org.apache.fop.apps.Fop;
35 import org.apache.fop.apps.FOUserAgent;
36 import org.apache.fop.apps.FopFactory;
37 import org.apache.fop.apps.MimeConstants;
38 import org.apache.fop.util.DigestFilter;
39 import org.xml.sax.InputSource JavaDoc;
40
41 /**
42  * Framework for simple regression testing.
43  * The testcase reads a control XML file which specifies a FO source,
44  * a MD5 for the source to help diferentiating failures caused by causal
45  * source modification from failures caused by regression, a renderer (only
46  * PDF currently supported) and a MD5 for the result.
47  *
48  */

49 public final class GenericFOPTestCase extends TestCase {
50
51     // configure fopFactory as desired
52
private FopFactory fopFactory = FopFactory.newInstance();
53     
54     protected SAXParserFactory JavaDoc parserFactory;
55
56     public static Test suite() {
57         TestSuite suite = new TestSuite(GenericFOPTestCase.class);
58         suite.setName("Fop regression tests");
59         return suite;
60     }
61
62     /**
63      * Constructor for FopTest.
64      * @param name the name of the test suite
65      */

66     public GenericFOPTestCase(String JavaDoc name) {
67         super(name);
68     }
69
70     /** @see junit.framework.TestCase#setUp() */
71     protected void setUp() throws Exception JavaDoc {
72         parserFactory = SAXParserFactory.newInstance();
73         parserFactory.setNamespaceAware(true);
74     }
75
76     public void testSimple() throws Exception JavaDoc {
77         final String JavaDoc digestIn = "17bf13298796065f7775db8707133aeb";
78         final String JavaDoc digestOut = "e2761f51152f6663911e567901596707";
79         final String JavaDoc fo =
80             "<fo:root xmlns:fo='http://www.w3.org/1999/XSL/Format'>"
81                 + " <fo:layout-master-set>"
82                 + " <fo:simple-page-master master-name='simple'"
83                 + " page-height='25cm' page-width='20cm'>"
84                 + " <fo:region-body/>"
85                 + " </fo:simple-page-master>"
86                 + " </fo:layout-master-set>"
87                 + " <fo:page-sequence master-reference='simple'>"
88                 + " <fo:flow flow-name='xsl-region-body'>"
89                 + " <fo:block>This is a blind text.</fo:block>"
90                 + " </fo:flow>"
91                 + " </fo:page-sequence>"
92                 + "</fo:root>";
93         renderPDF(fo, digestIn, digestOut);
94     }
95
96     private String JavaDoc digestToString(byte[] value) {
97         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(2 * value.length);
98         for (int i = 0; i < value.length; i++) {
99             int val = value[i];
100             int hi = (val >> 4) & 0xF;
101             int lo = val & 0xF;
102             if (hi < 10) {
103                 buffer.append((char) (hi + 0x30));
104             } else {
105                 buffer.append((char) (hi + 0x61 - 10));
106             }
107             if (lo < 10) {
108                 buffer.append((char) (lo + 0x30));
109             } else {
110                 buffer.append((char) (lo + 0x61 - 10));
111             }
112         }
113         return buffer.toString();
114     }
115
116     private void renderPDF(String JavaDoc fo, String JavaDoc digestIn, String JavaDoc digestOut)
117         throws Exception JavaDoc {
118         FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
119         foUserAgent.setCreationDate(new Date JavaDoc(10000));
120         MessageDigest JavaDoc outDigest = MessageDigest.getInstance("MD5");
121         DigestOutputStream JavaDoc out =
122             new DigestOutputStream JavaDoc(new ByteArrayOutputStream JavaDoc(), outDigest);
123         Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
124         InputSource JavaDoc source = new InputSource JavaDoc(new StringReader JavaDoc(fo));
125         DigestFilter filter = new DigestFilter("MD5");
126         filter.setParent(parserFactory.newSAXParser().getXMLReader());
127         filter.setContentHandler(fop.getDefaultHandler());
128         filter.parse(source);
129         String JavaDoc digestInActual = digestToString(filter.getDigestValue());
130         if (!digestIn.equals(digestInActual)) {
131             fail("input MD5: was " + digestInActual + ", expected " + digestIn);
132         }
133         String JavaDoc digestOutActual = digestToString(outDigest.digest());
134         if (!digestOut.equals(digestOutActual)) {
135             fail(
136                 "output MD5: was "
137                     + digestOutActual
138                     + ", expected "
139                     + digestOut);
140         }
141     }
142
143 }
144
Popular Tags