aboutsummaryrefslogtreecommitdiffstats
path: root/test/java/org/apache/fop/GenericFOPTestCase.java
blob: 26fa296a7e86f5623c6424a44a5ee13a1f65af58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

package org.apache.fop;
import static org.junit.Assert.fail;

import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.util.Date;

import javax.xml.parsers.SAXParserFactory;

import org.junit.Before;
import org.junit.Test;
import org.xml.sax.InputSource;

import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import org.apache.fop.util.DigestFilter;

/**
 * Framework for simple regression testing.
 * The testcase reads a control XML file which specifies a FO source,
 * a MD5 for the source to help diferentiating failures caused by causal
 * source modification from failures caused by regression, a renderer (only
 * PDF currently supported) and a MD5 for the result.
 *
 */
public final class GenericFOPTestCase {

    // configure fopFactory as desired
    private FopFactory fopFactory = FopFactory.newInstance();

    private SAXParserFactory parserFactory;

    @Before
    public void setUp() throws Exception {
        parserFactory = SAXParserFactory.newInstance();
        parserFactory.setNamespaceAware(true);
    }

    @org.junit.Test
    public void testSimple() throws Exception {
        final String digestIn = "17bf13298796065f7775db8707133aeb";
        final String digestOut = "e2761f51152f6663911e567901596707";
        final String fo
            = "<fo:root xmlns:fo='http://www.w3.org/1999/XSL/Format'>"
                + "  <fo:layout-master-set>"
                + "    <fo:simple-page-master master-name='simple'"
                + "       page-height='25cm' page-width='20cm'>"
                + "       <fo:region-body/>"
                + "    </fo:simple-page-master>"
                + "  </fo:layout-master-set>"
                + "  <fo:page-sequence master-reference='simple'>"
                + "     <fo:flow flow-name='xsl-region-body'>"
                + "        <fo:block>This is a blind text.</fo:block>"
                + "     </fo:flow>"
                + "   </fo:page-sequence>"
                + "</fo:root>";
        renderPDF(fo, digestIn, digestOut);
    }

    private String digestToString(byte[] value) {
        StringBuffer buffer = new StringBuffer(2 * value.length);
        for (int i = 0; i < value.length; i++) {
            int val = value[i];
            int hi = (val >> 4) & 0xF;
            int lo = val & 0xF;
            if (hi < 10) {
                buffer.append((char) (hi + 0x30));
            } else {
                buffer.append((char) (hi + 0x61 - 10));
            }
            if (lo < 10) {
                buffer.append((char) (lo + 0x30));
            } else {
                buffer.append((char) (lo + 0x61 - 10));
            }
        }
        return buffer.toString();
    }

    private void renderPDF(String fo, String digestIn, String digestOut)
        throws Exception {
        FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
        foUserAgent.setCreationDate(new Date(10000));
        MessageDigest outDigest = MessageDigest.getInstance("MD5");
        DigestOutputStream out = new DigestOutputStream(new ByteArrayOutputStream(), outDigest);
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
        InputSource source = new InputSource(new StringReader(fo));
        DigestFilter filter = new DigestFilter("MD5");
        filter.setParent(parserFactory.newSAXParser().getXMLReader());
        filter.setContentHandler(fop.getDefaultHandler());
        filter.parse(source);
        String digestInActual = digestToString(filter.getDigestValue());
        if (!digestIn.equals(digestInActual)) {
            fail("input MD5: was " + digestInActual + ", expected " + digestIn);
        }
        String digestOutActual = digestToString(outDigest.digest());
        if (!digestOut.equals(digestOutActual)) {
            fail(
                "output MD5: was "
                    + digestOutActual
                    + ", expected "
                    + digestOut);
        }
    }

}