KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > pdf > BasePDFTestCase


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: BasePDFTestCase.java 467698 2006-10-25 16:05:10Z jeremias $ */
19
20 package org.apache.fop.render.pdf;
21
22 import java.io.File JavaDoc;
23
24 import javax.xml.transform.Source JavaDoc;
25 import javax.xml.transform.Transformer JavaDoc;
26 import javax.xml.transform.TransformerFactory JavaDoc;
27 import javax.xml.transform.sax.SAXResult JavaDoc;
28 import javax.xml.transform.stream.StreamSource JavaDoc;
29
30 import org.apache.commons.io.FileUtils;
31 import org.apache.commons.io.output.ByteArrayOutputStream;
32 import org.apache.fop.apps.FOUserAgent;
33 import org.apache.fop.apps.Fop;
34 import org.apache.fop.apps.FopFactory;
35 import org.apache.fop.apps.MimeConstants;
36
37 import junit.framework.TestCase;
38
39 /**
40  * Base class for automated tests that create PDF files
41  */

42 public class BasePDFTestCase extends TestCase {
43
44     /** the FopFactory */
45     protected final FopFactory fopFactory = FopFactory.newInstance();
46
47     /** the JAXP TransformerFactory */
48     protected final TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
49
50     /**
51      * Main constructor
52      * @param name the name of the test case
53      */

54     protected BasePDFTestCase(String JavaDoc name) {
55         super(name);
56
57         final File JavaDoc uc = getUserConfigFile();
58
59         try {
60             fopFactory.setUserConfig(uc);
61         } catch (Exception JavaDoc e) {
62             throw new RuntimeException JavaDoc("fopFactory.setUserConfig ("
63                     + uc.getAbsolutePath() + ") failed: " + e.getMessage());
64         }
65     }
66
67     /**
68      * Convert a test FO file to PDF
69      * @param foFile the FO file
70      * @param ua the preconfigured user agent
71      * @param dumpPdfFile if true, dumps the generated PDF file to a file name (foFile).pdf
72      * @return the generated PDF data
73      * @throws Exception if the conversion fails
74      */

75     protected byte[] convertFO(File JavaDoc foFile, FOUserAgent ua, boolean dumpPdfFile)
76             throws Exception JavaDoc {
77         ByteArrayOutputStream baout = new ByteArrayOutputStream();
78         Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, ua, baout);
79         Transformer JavaDoc transformer = tFactory.newTransformer();
80         Source JavaDoc src = new StreamSource JavaDoc(foFile);
81         SAXResult JavaDoc res = new SAXResult JavaDoc(fop.getDefaultHandler());
82         transformer.transform(src, res);
83         final byte[] result = baout.toByteArray();
84         if (dumpPdfFile) {
85             final File JavaDoc outFile = new File JavaDoc(foFile.getParentFile(), foFile.getName() + ".pdf");
86             FileUtils.writeByteArrayToFile(outFile, result);
87         }
88         return result;
89     }
90
91     /** get FOP config File */
92     protected File JavaDoc getUserConfigFile() {
93         return new File JavaDoc("test/test.xconf");
94     }
95 }
96
Popular Tags