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: PdfMojoTest.java 811849 2009-09-06 18:08:36Z hboutemy $
34   */
35  public class PdfMojoTest
36      extends AbstractMojoTestCase
37  {
38      /** {@inheritDoc} */
39      protected void setUp() throws Exception
40      {
41          // required for mojo lookups to work
42          super.setUp();
43      }
44  
45      /**
46       * Tests the basic functioning of the pdf generation using the FO implementation.
47       *
48       * @throws Exception if any.
49       */
50      public void testPdfMojo() throws Exception
51      {
52          File testPom = new File( getBasedir(), "/target/test-classes/unit/pdf/pom.xml" );
53          assertTrue( "testPom does not exist!", testPom.exists() );
54  
55          PdfMojo mojo = (PdfMojo) lookupMojo( "pdf", testPom );
56          assertNotNull( "pdf mojo not found!", mojo );
57  
58          File pdfFile = new File( getBasedir(), "/target/test-output/pdf/fo/maven-pdf-plugin-doc.pdf" );
59          if ( pdfFile.exists() )
60          {
61              pdfFile.delete();
62          }
63  
64          mojo.execute();
65  
66          assertTrue( "FO: Pdf file not created!", pdfFile.exists() );
67          assertTrue( "FO: Pdf file has no content!", pdfFile.length() > 0 );
68      }
69  
70      /**
71       * Tests the basic functioning of the pdf generation with iText.
72       *
73       * @throws Exception if any.
74       */
75      public void testITextImpl() throws Exception
76      {
77          File testPom = new File( getBasedir(), "/target/test-classes/unit/pdf/iText_pom.xml" );
78          assertTrue( "testPom does not exist!", testPom.exists() );
79  
80          PdfMojo mojo = (PdfMojo) lookupMojo( "pdf", testPom );
81          assertNotNull( "pdf mojo not found!", mojo );
82  
83          File pdfFile = new File( getBasedir(), "/target/test-output/pdf/itext/maven-pdf-plugin-doc.pdf" );
84          if ( pdfFile.exists() )
85          {
86              pdfFile.delete();
87          }
88  
89          mojo.execute();
90  
91          assertTrue( "iText: Pdf file not created!", pdfFile.exists() );
92          assertTrue( "iText: Pdf file has no content!", pdfFile.length() > 0 );
93       }
94  
95      /**
96       * @throws Exception if any.
97       */
98      public void testPdfFilterMojo() throws Exception
99      {
100         File testPom = new File( getBasedir(), "/target/test-classes/unit/pdf/pom_filtering.xml" );
101         assertTrue( "testPom does not exist!", testPom.exists() );
102 
103         PdfMojo mojo = (PdfMojo) lookupMojo( "pdf", testPom );
104         assertNotNull( "pdf mojo not found!", mojo );
105 
106         File pdfFile = new File( getBasedir(), "/target/test-output/pdf/filtering/maven-pdf-plugin-doc-1.0-SNAPSHOT.pdf" );
107         if ( pdfFile.exists() )
108         {
109             pdfFile.delete();
110         }
111 
112         mojo.execute();
113 
114         assertTrue( "FO: Pdf file not created!", pdfFile.exists() );
115         assertTrue( "FO: Pdf file has no content!", pdfFile.length() > 0 );
116 
117         File foFile = new File( getBasedir(), "/target/test-output/pdf/filtering/maven-pdf-plugin-doc-1.0-SNAPSHOT.fo" );
118         assertTrue( "FO: Fo file not created!", foFile.exists() );
119         assertTrue( "FO: Fo file has no content!", foFile.length() > 0 );
120 
121         Reader reader = null;
122         String foContent;
123         try
124         {
125             reader = ReaderFactory.newXmlReader( foFile );
126             foContent = IOUtil.toString( reader );
127         }
128         finally
129         {
130             IOUtil.close( reader );
131         }
132         // ${pom.name}
133         assertTrue( foContent.indexOf( "Test filtering" ) > 0 );
134         assertTrue( foContent.indexOf( "1.0-SNAPSHOT" ) > 0 );
135         // env ${M2_HOME}
136         String m2Home = CommandLineUtils.getSystemEnvVars().getProperty( "M2_HOME" );
137         if ( StringUtils.isNotEmpty( m2Home ) )
138         {
139             assertTrue( foContent.indexOf( m2Home ) > 0 );
140         }
141         // ${project.developers[0].email}
142         assertTrue( foContent.indexOf( "vsiveton@apache.org ltheussl@apache.org" ) > 0 );
143         // ${date}
144         // TODO: this might fail on NewYear's eve! :)
145         assertTrue( foContent.indexOf( new DateBean().getDate() ) > 0 );
146     }
147 
148     /**
149      * Tests the basic functioning of the pdf generation using the FO implementation.
150      *
151      * @throws Exception if any.
152      */
153     public void testPdfMojoNoDocDesriptor() throws Exception
154     {
155         File testPom = new File( getBasedir(), "/target/test-classes/unit/pdf/no_docdescriptor_pom.xml" );
156         assertTrue( "testPom does not exist!", testPom.exists() );
157 
158         PdfMojo mojo = (PdfMojo) lookupMojo( "pdf", testPom );
159         assertNotNull( "pdf mojo not found!", mojo );
160 
161         File pdfFile = new File( getBasedir(), "/target/test-output/pdf/no/unnamed.pdf" );
162         if ( pdfFile.exists() )
163         {
164             pdfFile.delete();
165         }
166 
167         mojo.execute();
168 
169         assertTrue( "FO: Pdf file not created!", pdfFile.exists() );
170         assertTrue( "FO: Pdf file has no content!", pdfFile.length() > 0 );
171     }
172 }