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