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      @SuppressWarnings ( "unchecked" )
90      private void renderImpl( String implementation )
91          throws Exception
92      {
93          File outputDirectory = getTestFile( "target/output/" + implementation );
94          if ( outputDirectory.exists() )
95          {
96              FileUtils.deleteDirectory( outputDirectory );
97          }
98          outputDirectory.mkdirs();
99  
100         docRenderer = (PdfRenderer) lookup( PdfRenderer.ROLE, implementation );
101         assertNotNull( docRenderer );
102 
103         docRenderer.render( siteDirectoryFile, outputDirectory, null );
104 
105         @SuppressWarnings ( "unchecked" )
106         List<String> files =
107             FileUtils.getFileNames( new File( siteDirectoryFile, "apt" ), "**/*.apt",
108                                     FileUtils.getDefaultExcludesAsString(), false );
109         files.addAll( FileUtils.getFileNames( new File( siteDirectoryFile, "fml" ), "**/*.fml",
110                                               FileUtils.getDefaultExcludesAsString(), false ) );
111         files.addAll( FileUtils.getFileNames( new File( siteDirectoryFile, "xdoc" ), "**/*.xml",
112                                               FileUtils.getDefaultExcludesAsString(), false ) );
113 
114         for ( String relativeFile : files )
115         {
116             String relativePdf = StringUtils.replace( relativeFile, FileUtils.getExtension( relativeFile ), "pdf" );
117             File pdf = new File( outputDirectory, relativePdf );
118 
119             assertTrue( pdf.exists() );
120             assertTrue( pdf.length() > 0 );
121         }
122     }
123 
124     private void renderAggregatedImpl( String implementation )
125         throws Exception
126     {
127         File outputDirectory = getTestFile( "target/output/" + implementation + "-aggregated" );
128         if ( outputDirectory.exists() )
129         {
130             FileUtils.deleteDirectory( outputDirectory );
131         }
132         outputDirectory.mkdirs();
133 
134         docRenderer = (PdfRenderer) lookup( PdfRenderer.ROLE, implementation );
135         assertNotNull( docRenderer );
136 
137         DocumentModel descriptor = docRenderer.readDocumentModel( new File( siteDirectoryFile, "pdf.xml" ) );
138         assertNotNull( descriptor );
139 
140         docRenderer.render( siteDirectoryFile, outputDirectory, descriptor );
141 
142         File pdf = new File( outputDirectory, descriptor.getOutputName() + ".pdf" );
143 
144         assertTrue( pdf.exists() );
145         assertTrue( pdf.length() > 0 );
146     }
147 }