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.Iterator;
24  import java.util.List;
25  
26  import org.apache.maven.doxia.docrenderer.pdf.PdfRenderer;
27  import org.apache.maven.doxia.document.DocumentModel;
28  import org.codehaus.plexus.PlexusTestCase;
29  import org.codehaus.plexus.util.FileUtils;
30  import org.codehaus.plexus.util.StringUtils;
31  
32  /**
33   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
34   * @version $Id: DocumentRendererTest.java 782949 2009-06-09 10:48:57Z vsiveton $
35   * @since 1.1.1
36   */
37  public class DocumentRendererTest
38      extends PlexusTestCase
39  {
40      private PdfRenderer docRenderer;
41  
42      private File siteDirectoryFile;
43  
44      /** @throws java.lang.Exception */
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      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 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 ( Iterator it = files.iterator(); it.hasNext(); )
113         {
114             String relativeFile = it.next().toString();
115             String relativePdf = StringUtils.replace( relativeFile, FileUtils.getExtension( relativeFile ), "pdf" );
116             File pdf = new File( outputDirectory, relativePdf );
117 
118             assertTrue( pdf.exists() );
119             assertTrue( pdf.length() > 0 );
120         }
121     }
122 
123     private void renderAggregatedImpl( String implementation )
124         throws Exception
125     {
126         File outputDirectory = getTestFile( "target/output/" + implementation + "-aggregated" );
127         if ( outputDirectory.exists() )
128         {
129             FileUtils.deleteDirectory( outputDirectory );
130         }
131         outputDirectory.mkdirs();
132 
133         docRenderer = (PdfRenderer) lookup( PdfRenderer.ROLE, implementation );
134         assertNotNull( docRenderer );
135 
136         DocumentModel descriptor = docRenderer.readDocumentModel( new File( siteDirectoryFile, "pdf.xml" ) );
137         assertNotNull( descriptor );
138 
139         docRenderer.render( siteDirectoryFile, outputDirectory, descriptor );
140 
141         File pdf = new File( outputDirectory, descriptor.getOutputName() + ".pdf" );
142 
143         assertTrue( pdf.exists() );
144         assertTrue( pdf.length() > 0 );
145     }
146 }