View Javadoc

1   package org.apache.maven.doxia.docrenderer;
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.util.List;
24  
25  import org.apache.maven.doxia.docrenderer.pdf.PdfRenderer;
26  import org.apache.maven.doxia.document.DocumentModel;
27  import org.codehaus.plexus.PlexusTestCase;
28  import org.codehaus.plexus.util.FileUtils;
29  import org.codehaus.plexus.util.StringUtils;
30  
31  /**
32   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
33   * @version $Id: DocumentRendererTest.java 1087124 2011-03-30 22:45:41Z hboutemy $
34   * @since 1.1.1
35   */
36  public class DocumentRendererTest
37      extends PlexusTestCase
38  {
39      private PdfRenderer docRenderer;
40  
41      private File siteDirectoryFile;
42  
43      /** @throws java.lang.Exception */
44      protected void setUp()
45          throws Exception
46      {
47          super.setUp();
48  
49          siteDirectoryFile = getTestFile( "src/test/resources/site" );
50      }
51  
52      /** @throws java.lang.Exception */
53      protected void tearDown()
54          throws Exception
55      {
56          release( docRenderer );
57          super.tearDown();
58      }
59  
60      /** @throws java.lang.Exception */
61      public void testFo()
62          throws Exception
63      {
64          renderImpl( "fo" );
65      }
66  
67      /** @throws java.lang.Exception */
68      public void testFoAggregate()
69          throws Exception
70      {
71          renderAggregatedImpl( "fo" );
72      }
73  
74      /** @throws java.lang.Exception */
75      public void testIText()
76          throws Exception
77      {
78          renderImpl( "itext" );
79      }
80  
81      /** @throws java.lang.Exception */
82      public void testITextAggregate()
83          throws Exception
84      {
85          renderAggregatedImpl( "itext" );
86      }
87  
88      private void renderImpl( String implementation )
89          throws Exception
90      {
91          File outputDirectory = getTestFile( "target/output/" + implementation );
92          if ( outputDirectory.exists() )
93          {
94              FileUtils.deleteDirectory( outputDirectory );
95          }
96          outputDirectory.mkdirs();
97  
98          docRenderer = (PdfRenderer) lookup( PdfRenderer.ROLE, implementation );
99          assertNotNull( docRenderer );
100 
101         docRenderer.render( siteDirectoryFile, outputDirectory, null );
102 
103         List<String> files =
104             FileUtils.getFileNames( new File( siteDirectoryFile, "apt" ), "**/*.apt",
105                                     FileUtils.getDefaultExcludesAsString(), false );
106         files.addAll( FileUtils.getFileNames( new File( siteDirectoryFile, "fml" ), "**/*.fml",
107                                               FileUtils.getDefaultExcludesAsString(), false ) );
108         files.addAll( FileUtils.getFileNames( new File( siteDirectoryFile, "xdoc" ), "**/*.xml",
109                                               FileUtils.getDefaultExcludesAsString(), false ) );
110 
111         for ( String relativeFile : files )
112         {
113             String relativePdf = StringUtils.replace( relativeFile, FileUtils.getExtension( relativeFile ), "pdf" );
114             File pdf = new File( outputDirectory, relativePdf );
115 
116             assertTrue( pdf.exists() );
117             assertTrue( pdf.length() > 0 );
118         }
119     }
120 
121     private void renderAggregatedImpl( String implementation )
122         throws Exception
123     {
124         File outputDirectory = getTestFile( "target/output/" + implementation + "-aggregated" );
125         if ( outputDirectory.exists() )
126         {
127             FileUtils.deleteDirectory( outputDirectory );
128         }
129         outputDirectory.mkdirs();
130 
131         docRenderer = (PdfRenderer) lookup( PdfRenderer.ROLE, implementation );
132         assertNotNull( docRenderer );
133 
134         DocumentModel descriptor = docRenderer.readDocumentModel( new File( siteDirectoryFile, "pdf.xml" ) );
135         assertNotNull( descriptor );
136 
137         docRenderer.render( siteDirectoryFile, outputDirectory, descriptor );
138 
139         File pdf = new File( outputDirectory, descriptor.getOutputName() + ".pdf" );
140 
141         assertTrue( pdf.exists() );
142         assertTrue( pdf.length() > 0 );
143     }
144 }