View Javadoc
1   package org.apache.maven.doxia.module.fo;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.StringWriter;
25  import java.io.Writer;
26  
27  import javax.xml.transform.TransformerException;
28  
29  import org.apache.maven.doxia.document.DocumentCover;
30  import org.apache.maven.doxia.document.DocumentModel;
31  import org.apache.maven.doxia.markup.Markup;
32  import org.codehaus.plexus.util.WriterFactory;
33  import org.xml.sax.SAXParseException;
34  
35  import junit.framework.TestCase;
36  import org.xmlunit.matchers.CompareMatcher;
37  
38  import static org.junit.Assert.assertThat;
39  
40  /**
41   * Test FoAggregateSink.
42   *
43   * @author ltheussl
44   * @version $Id$
45   */
46  public class FoAggregateSinkTest
47      extends TestCase
48  {
49      private FoAggregateSink sink;
50  
51      private Writer writer;
52  
53      @Override
54      protected void setUp()
55          throws Exception
56      {
57          super.setUp();
58          writer = new StringWriter();
59      }
60      
61      protected String wrapXml( String xmlFragment )
62      {
63          return "<fo:fo xmlns:fo=\"" + FoMarkup.FO_NAMESPACE+ "\">" + xmlFragment + "</fo:fo>";
64      }
65  
66  
67      /**
68       * Test of body method, of class FoAggregateSink.
69       */
70      public void testBody()
71      {
72          try
73          {
74              sink = new FoAggregateSink( writer );
75  
76              sink.setDocumentName( "folder/documentName.apt" );
77              sink.setDocumentTitle( "documentTitle" );
78              sink.body();
79              sink.body_();
80          }
81          finally
82          {
83              sink.close();
84          }
85  
86          assertTrue( writer.toString().indexOf( "<fo:block id=\"./folder/documentName\">" ) != -1 );
87      }
88  
89      /**
90       * Test of setDocumentName method, of class FoAggregateSink.
91       */
92      public void testSetDocumentName()
93      {
94          try
95          {
96              sink = new FoAggregateSink( writer );
97  
98              sink.setDocumentName( "folder\\documentName.boo" );
99              sink.body();
100         }
101         finally
102         {
103             sink.close();
104         }
105 
106         assertTrue( writer.toString().indexOf( "<fo:block id=\"./folder/documentName\">" ) != -1 );
107     }
108     
109     /**
110      * Test the FO PDF generation with some special characters in company name.
111      */
112     public void testSpecialCharacters()
113         throws IOException, TransformerException
114     {
115         DocumentModel model = new DocumentModel();
116         DocumentCover cover = new DocumentCover();
117 
118         cover.setCompanyName( "Partner & Friends" );
119         cover.setCoverTitle( "A Masterpice in Encoding Theory <>&" );
120         cover.setCoverSubTitle( "Some nice Encodings & <METHODS>" );
121         cover.setProjectName( "A Masterpice in Encoding Theory <>&" );
122         cover.setAuthor( "Partner & Friends" );
123         model.setCover( cover );
124 
125         File foFile = File.createTempFile( "fo-test", ".fo" );
126         File pdfFile = File.createTempFile( "fo-test", ".pdf" );
127         try
128         {
129 
130             sink = new FoAggregateSink( WriterFactory.newXmlWriter( foFile ) );
131 
132             sink.setDocumentModel( model );
133             sink.setDocumentTitle( "A Masterpice in Encoding Theory <>&" );
134             sink.beginDocument();
135             sink.coverPage();
136             // sink.toc();
137             sink.endDocument();
138         }
139         finally
140         {
141             sink.close();
142         }
143 
144         try
145         {
146             FoUtils.convertFO2PDF( foFile, pdfFile, null, model );
147         }
148         catch ( TransformerException e )
149         {
150             if ( ( e.getCause() != null ) && ( e.getCause() instanceof SAXParseException ) )
151             {
152                 SAXParseException sax = (SAXParseException) e.getCause();
153 
154                 StringBuffer sb = new StringBuffer();
155                 sb.append( "Error creating PDF from " ).append( foFile.getAbsolutePath() ).append( ":" ).append( sax.getLineNumber() ).append( ":" ).append( sax.getColumnNumber() ).append( "\n" );
156                 sb.append( e.getMessage() );
157 
158                 throw new RuntimeException( sb.toString() );
159             }
160 
161             throw new TransformerException( "Error creating PDF from " + foFile + ": " + e.getMessage() );
162         }
163     }
164 
165     /**
166      * Test of figureGraphics method, of class FoAggregateSink.
167      */
168     public void testFigureGraphics() throws Exception
169     {
170         try
171         {
172             sink = new FoAggregateSink( writer );
173             sink.setDocumentName( "./folder\\docName.xml" );
174             sink.figureGraphics( "./../images/fig.png", null );
175         }
176         finally
177         {
178             sink.close();
179         }
180 
181         String expected = "<fo:external-graphic src=\"./images/fig.png\" "
182                         + "content-width=\"scale-down-to-fit\" "
183                         + "content-height=\"scale-down-to-fit\" "
184                         + "height=\"100%\" "
185                         + "width=\"100%\"/>" + Markup.EOL;
186         String actual = writer.toString();
187 
188         assertThat ( wrapXml( actual ), CompareMatcher.isIdenticalTo( wrapXml( expected ) ));
189     }
190 
191     /**
192      * Test of anchor method, of class FoAggregateSink.
193      */
194     public void testAnchor()
195     {
196         try
197         {
198             sink = new FoAggregateSink( writer );
199             sink.anchor( "invalid Anchor" );
200             sink.setDocumentName( "./folder\\docName.xml" );
201             sink.anchor( "validAnchor" );
202         }
203         finally
204         {
205             sink.close();
206         }
207 
208         assertTrue( writer.toString().indexOf( "<fo:inline id=\"#invalid_Anchor\">" ) != -1 );
209         assertTrue( writer.toString().indexOf( "<fo:inline id=\"./folder/docName#validAnchor\">" ) != -1 );
210     }
211 
212     /**
213      * Test of link method, of class FoAggregateSink.
214      */
215     public void testLink()
216     {
217         try
218         {
219             sink = new FoAggregateSink( writer );
220             sink.link( "http://www.example.com/" );
221             sink.text( "http://www.example.com/" );
222             sink.link_();
223             sink.setDocumentName( "./folder\\docName.xml" );
224             sink.link( "#anchor" );
225             sink.text( "#anchor" );
226             sink.link_();
227             sink.link( "./././index.html" );
228             sink.text( "./././index.html" );
229             sink.link_();
230             sink.link( "./../download.html" );
231             sink.text( "./../download.html" );
232             sink.link_();
233             sink.link( ".///test.html" );
234             sink.text( "./test.html" );
235             sink.link_();
236             sink.link( "./whatsnew-1.1.html" );
237             sink.text( "./whatsnew-1.1.html" );
238             sink.link_();
239             sink.setDocumentName( ".///whatsnew-1.1.html" );
240             sink.body();
241             sink.body_();
242         }
243         finally
244         {
245             sink.close();
246         }
247 
248         String result = writer.toString();
249 
250         assertTrue( result.indexOf( "<fo:basic-link external-destination=\"http://www.example.com/\">" ) != -1 );
251         assertTrue( result.indexOf( "<fo:basic-link internal-destination=\"./folder/docName#anchor\">" ) != -1 );
252         assertTrue( result.indexOf( "<fo:basic-link internal-destination=\"./folder/index\">" ) != -1 );
253         assertTrue( result.indexOf( "<fo:basic-link internal-destination=\"./download\">" ) != -1 );
254         assertTrue( result.indexOf( "<fo:basic-link internal-destination=\"./folder/test\">" ) != -1 );
255         assertTrue( result.indexOf( "<fo:basic-link internal-destination=\"./folder/whatsnew-1.1\">" ) != -1 );
256         assertTrue( result.indexOf( "<fo:block id=\"./whatsnew-1.1\">" ) != -1 );
257 
258         writer = new StringWriter();
259         try
260         {
261             sink = new FoAggregateSink( writer );
262             sink.setDocumentName( "./subdir/dir/index.html" );
263             sink.link( "../../root.html" );
264             sink.text( "../../root.html" );
265             sink.link_();
266             sink.link( "../../../outside.html" );
267             sink.text( "../../../outside.html" );
268             sink.link_();
269             sink.body();
270             sink.body_();
271         }
272         finally
273         {
274             sink.close();
275         }
276 
277         result = writer.toString();
278 
279         assertTrue( result.indexOf( "<fo:basic-link internal-destination=\"./root\">" ) != -1 );
280         assertTrue( result.indexOf( "<fo:basic-link internal-destination=\"./outside\">" ) != -1 );
281     }
282 }