KickJava   Java API By Example, From Geeks To Geeks.

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


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: PDFEncodingTestCase.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 import java.io.IOException JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import org.apache.fop.apps.FOUserAgent;
27
28 /** Test that characters are correctly encoded in a generated PDF file */
29 public class PDFEncodingTestCase extends BasePDFTestCase {
30     private File JavaDoc foBaseDir = new File JavaDoc("test/xml/pdf-encoding");
31     private final boolean dumpPDF = Boolean.getBoolean("PDFEncodingTestCase.dumpPDF");
32     static final String JavaDoc INPUT_FILE = "test/xml/pdf-encoding/pdf-encoding-test.xconf";
33     static final String JavaDoc TEST_MARKER = "PDFE_TEST_MARK_";
34
35     /**
36      * @param name the name of the test case
37      */

38     public PDFEncodingTestCase(String JavaDoc name) {
39         super(name);
40     }
41
42     /**
43      * create an FOUserAgent for our tests
44      * @return an initialized FOUserAgent
45      */

46     protected FOUserAgent getUserAgent() {
47         final FOUserAgent a = fopFactory.newFOUserAgent();
48         return a;
49     }
50
51     /** @return our specific config */
52     protected File JavaDoc getUserConfigFile() {
53         return new File JavaDoc(INPUT_FILE);
54     }
55
56     /**
57      * Test using a standard FOP font
58      * @throws Exception checkstyle wants a comment here, even a silly one
59      */

60     public void testPDFEncodingWithStandardFont() throws Exception JavaDoc {
61
62         /* If the PDF encoding is correct, a text dump of the generated PDF file contains this (excerpts)
63          * ...Tm [(PDFE_TEST_MARK_2:) ( ) (This) ( ) (is) ...(acute:) ( ) (XX_\351_XX) ] TJ
64          * ...Tm [(PDFE_TEST_MARK_3:) ( ) (This) ( ) (is) ...(letter:) ( ) (XX_\342\352\356\364\373_XX) ] TJ
65          * The following array is used to look for these patterns
66          */

67         final String JavaDoc[] testPatterns = {
68                 TEST_MARKER + "1", "(Standard)",
69                 TEST_MARKER + "2", "XX_\\351_XX",
70                 TEST_MARKER + "3", "XX_\\342\\352\\356\\364\\373_XX"
71               };
72
73         runTest("test-standard-font.fo", testPatterns);
74     }
75
76     /**
77      * TODO test disabled for now, fails due (probably) do different PDF
78      * encoding when custom font is used
79      *
80      * @throws Exception
81      * checkstyle wants a comment here, even a silly one
82      */

83     public void DISABLEDtestPDFEncodingWithCustomFont() throws Exception JavaDoc {
84
85         /* If the PDF encoding is correct, a text dump of the generated PDF file contains this (excerpts)
86          * ...Tm [(PDFE_TEST_MARK_2:) ( ) (This) ( ) (is) ...(acute:) ( ) (XX_\351_XX) ] TJ
87          * ...Tm [(PDFE_TEST_MARK_3:) ( ) (This) ( ) (is) ...(letter:) ( ) (XX_\342\352\356\364\373_XX) ] TJ
88          * The following array is used to look for these patterns
89          */

90         final String JavaDoc[] testPatterns = {
91           TEST_MARKER + "1", "(Gladiator)",
92           TEST_MARKER + "2", "XX_\\351_XX",
93           TEST_MARKER + "3", "XX_\\342\\352\\356\\364\\373_XX"
94         };
95
96         runTest("test-custom-font.fo", testPatterns);
97     }
98
99     /** Test encoding using specified input file and test patterns array */
100     private void runTest(String JavaDoc inputFile, String JavaDoc[] testPatterns)
101             throws Exception JavaDoc {
102         File JavaDoc foFile = new File JavaDoc(foBaseDir, inputFile);
103         byte[] pdfData = convertFO(foFile, getUserAgent(), dumpPDF);
104         checkEncoding(pdfData, testPatterns);
105     }
106
107     /**
108      * Check character encodings in the generated PDF data, by reading text
109      * lines identified by markers and checking their content
110      *
111      * @throws IOException
112      */

113     private void checkEncoding(byte[] pdf, String JavaDoc[] testPattern)
114             throws IOException JavaDoc {
115
116         int markersFound = 0;
117         final String JavaDoc input = new String JavaDoc(pdf);
118         int pos = 0;
119         if ((pos = input.indexOf(TEST_MARKER)) >= 0) {
120             final StringTokenizer JavaDoc tk = new StringTokenizer JavaDoc(
121                     input.substring(pos), "\n");
122
123             while (tk.hasMoreTokens()) {
124                 final String JavaDoc line = tk.nextToken();
125                 if (line.indexOf(TEST_MARKER) >= 0) {
126                     markersFound++;
127                     for (int i = 0; i < testPattern.length; i += 2) {
128                         if (line.indexOf(testPattern[i]) >= 0) {
129                             final String JavaDoc ref = testPattern[i + 1];
130                             final boolean patternFound = line.indexOf(ref) >= 0;
131                             assertTrue("line containing '" + testPattern[i]
132                                     + "' must contain '" + ref, patternFound);
133                         }
134                     }
135                 }
136             }
137         }
138
139         final int nMarkers = testPattern.length / 2;
140         assertEquals(nMarkers + " " + TEST_MARKER + " markers must be found",
141                 nMarkers, markersFound);
142     }
143 }
144
Popular Tags