View Javadoc
1   package org.apache.maven.plugins.pdf;
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 org.apache.maven.plugin.testing.AbstractMojoTestCase;
23  import org.codehaus.plexus.util.IOUtil;
24  import org.codehaus.plexus.util.ReaderFactory;
25  import org.codehaus.plexus.util.StringUtils;
26  import org.codehaus.plexus.util.cli.CommandLineUtils;
27  
28  import java.io.File;
29  import java.io.Reader;
30  
31  /**
32   * @author ltheussl
33   * @version $Id$
34   */
35  public class PdfMojoTest
36      extends AbstractMojoTestCase
37  {
38      /**
39       * Tests the basic functioning of the pdf generation using the FO implementation.
40       *
41       * @throws Exception if any.
42       */
43      public void testPdfMojo()
44          throws Exception
45      {
46          executePdfMojo( "pom.xml", "fo/maven-pdf-plugin-doc.pdf" );
47      }
48  
49      /**
50       * Tests the basic functioning of the pdf generation with iText.
51       *
52       * @throws Exception if any.
53       */
54      public void testITextImpl()
55          throws Exception
56      {
57          executePdfMojo( "iText_pom.xml", "itext/maven-pdf-plugin-doc.pdf" );
58       }
59  
60      /**
61       * Tests the basic functioning of the pdf generation using the FO implementation.
62       *
63       * @throws Exception if any.
64       */
65      public void testPdfMojoNoDocDesriptor()
66          throws Exception
67      {
68          executePdfMojo( "no_docdescriptor_pom.xml", "no/unnamed.pdf" );
69      }
70  
71      /**
72       * @throws Exception if any.
73       */
74      public void _testPdfFilterMojo() // MPDF-78: test desactivated because injection of PlexusContainer fails
75          throws Exception
76      {
77          executePdfMojo( "pom_filtering.xml", "filtering/maven-pdf-plugin-doc-1.0-SNAPSHOT.pdf" );
78  
79          File foFile = new File( getBasedir(), "/target/test-output/pdf/filtering/maven-pdf-plugin-doc-1.0-SNAPSHOT.fo" );
80          assertTrue( "FO: Fo file not created!", foFile.exists() );
81          assertTrue( "FO: Fo file has no content!", foFile.length() > 0 );
82  
83          Reader reader = null;
84          String foContent;
85          try
86          {
87              reader = ReaderFactory.newXmlReader( foFile );
88              foContent = IOUtil.toString( reader );
89              reader.close();
90              reader = null;
91          }
92          finally
93          {
94              IOUtil.close( reader );
95          }
96          // ${pom.name}
97          assertTrue( foContent.indexOf( "Test filtering" ) > 0 );
98          assertTrue( foContent.indexOf( "1.0-SNAPSHOT" ) > 0 );
99          // env ${M2_HOME}
100         String m2Home = CommandLineUtils.getSystemEnvVars().getProperty( "M2_HOME" );
101         if ( StringUtils.isNotEmpty( m2Home ) )
102         {
103             assertTrue( foContent.indexOf( m2Home ) > 0 );
104         }
105         // ${project.developers[0].email}
106         assertTrue( foContent.indexOf( "vsiveton@apache.org ltheussl@apache.org" ) > 0 );
107         // ${date}
108         // TODO: this might fail on NewYear's eve! :)
109         assertTrue( foContent.indexOf( new DateBean().getDate() ) > 0 );
110     }
111 
112     protected PdfMojo lookupPdfMojo( String pom )
113         throws Exception
114     {
115         File testPom = new File( getBasedir(), "target/test-classes/unit/pdf/" + pom );
116         assertTrue( "testPom does not exist!", testPom.exists() );
117         PdfMojo mojo = (PdfMojo) lookupMojo( "pdf", testPom );
118         assertNotNull( "pdf mojo not found!", mojo );
119         return mojo;
120     }
121 
122     protected File prepareOutputPdf( String filename )
123     {
124         File pdfFile = new File( getBasedir(), "target/test-output/pdf/" + filename );
125         if ( pdfFile.exists() )
126         {
127             pdfFile.delete();
128         }
129         return pdfFile;
130     }
131 
132     protected void executePdfMojo( String pom, String pdfFilename )
133         throws Exception
134     {
135         // MPDF-78: test desactivated because injection of PlexusContainer fails
136         return;
137         /*
138         File pdfFile = prepareOutputPdf( pdfFilename );
139 
140         PdfMojo mojo = lookupPdfMojo( pom );
141         mojo.execute();
142 
143         assertTrue( "FO: Pdf file not created!", pdfFile.exists() );
144         assertTrue( "FO: Pdf file has no content!", pdfFile.length() > 0 );
145         */
146     }
147 }