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   * @since 1.1.1
34   */
35  public class DocumentRendererTest
36      extends PlexusTestCase
37  {
38      private PdfRenderer docRenderer;
39  
40      private File siteDirectoryFile;
41  
42      /** @throws java.lang.Exception */
43      @Override
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      @Override
54      protected void tearDown()
55          throws Exception
56      {
57          release( docRenderer );
58          super.tearDown();
59      }
60  
61      /** @throws java.lang.Exception */
62      public void testFo()
63          throws Exception
64      {
65          renderImpl( "fo" );
66      }
67  
68      /** @throws java.lang.Exception */
69      public void testFoAggregate()
70          throws Exception
71      {
72          renderAggregatedImpl( "fo" );
73      }
74  
75      /** @throws java.lang.Exception */
76      public void testIText()
77          throws Exception
78      {
79          renderImpl( "itext" );
80      }
81  
82      /** @throws java.lang.Exception */
83      public void testITextAggregate()
84          throws Exception
85      {
86          renderAggregatedImpl( "itext" );
87      }
88  
89      private void renderImpl( String implementation )
90          throws Exception
91      {
92          File outputDirectory = getTestFile( "target/output/" + implementation );
93          if ( outputDirectory.exists() )
94          {
95              FileUtils.deleteDirectory( outputDirectory );
96          }
97          outputDirectory.mkdirs();
98  
99          docRenderer = (PdfRenderer) lookup( PdfRenderer.ROLE, implementation );
100         assertNotNull( docRenderer );
101 
102         docRenderer.render( siteDirectoryFile, outputDirectory, null );
103 
104         List<String> files =
105             FileUtils.getFileNames( new File( siteDirectoryFile, "apt" ), "**/*.apt",
106                                     FileUtils.getDefaultExcludesAsString(), false );
107         files.addAll( FileUtils.getFileNames( new File( siteDirectoryFile, "fml" ), "**/*.fml",
108                                               FileUtils.getDefaultExcludesAsString(), false ) );
109         files.addAll( FileUtils.getFileNames( new File( siteDirectoryFile, "xdoc" ), "**/*.xml",
110                                               FileUtils.getDefaultExcludesAsString(), false ) );
111 
112         for ( String relativeFile : files )
113         {
114             String relativePdf = StringUtils.replace( relativeFile, FileUtils.getExtension( relativeFile ), "pdf" );
115             File pdf = new File( outputDirectory, relativePdf );
116 
117             assertTrue( pdf.exists() );
118             assertTrue( pdf.length() > 0 );
119         }
120     }
121 
122     private void renderAggregatedImpl( String implementation )
123         throws Exception
124     {
125         File outputDirectory = getTestFile( "target/output/" + implementation + "-aggregated" );
126         if ( outputDirectory.exists() )
127         {
128             FileUtils.deleteDirectory( outputDirectory );
129         }
130         outputDirectory.mkdirs();
131 
132         docRenderer = (PdfRenderer) lookup( PdfRenderer.ROLE, implementation );
133         assertNotNull( docRenderer );
134 
135         DocumentModel descriptor = docRenderer.readDocumentModel( new File( siteDirectoryFile, "pdf.xml" ) );
136         assertNotNull( descriptor );
137 
138         docRenderer.render( siteDirectoryFile, outputDirectory, descriptor );
139 
140         File pdf = new File( outputDirectory, descriptor.getOutputName() + ".pdf" );
141 
142         assertTrue( pdf.exists() );
143         assertTrue( pdf.length() > 0 );
144     }
145 }