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