View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.pdf;
20  
21  /*
22   * Licensed to the Apache Software Foundation (ASF) under one
23   * or more contributor license agreements.  See the NOTICE file
24   * distributed with this work for additional information
25   * regarding copyright ownership.  The ASF licenses this file
26   * to you under the Apache License, Version 2.0 (the
27   * "License"); you may not use this file except in compliance
28   * with the License.  You may obtain a copy of the License at
29   *
30   *   http://www.apache.org/licenses/LICENSE-2.0
31   *
32   * Unless required by applicable law or agreed to in writing,
33   * software distributed under the License is distributed on an
34   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
35   * KIND, either express or implied.  See the License for the
36   * specific language governing permissions and limitations
37   * under the License.
38   */
39  
40  import java.io.File;
41  import java.io.Reader;
42  
43  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
44  import org.codehaus.plexus.util.IOUtil;
45  import org.codehaus.plexus.util.ReaderFactory;
46  import org.codehaus.plexus.util.cli.CommandLineUtils;
47  
48  /**
49   * @author ltheussl
50   */
51  public class PdfMojoTest extends AbstractMojoTestCase {
52      /**
53       * Tests the basic functioning of the pdf generation using the FO implementation.
54       *
55       */
56      public void testPdfMojo() {
57          executePdfMojo("pom.xml", "fo/maven-pdf-plugin-doc.pdf");
58      }
59  
60      /**
61       * Tests the basic functioning of the pdf generation with iText.
62       *
63       */
64      public void testITextImpl() {
65          executePdfMojo("iText_pom.xml", "itext/maven-pdf-plugin-doc.pdf");
66      }
67  
68      /**
69       * Tests the basic functioning of the pdf generation using the FO implementation.
70       *
71       */
72      public void testPdfMojoNoDocDesriptor() {
73          executePdfMojo("no_docdescriptor_pom.xml", "no/unnamed.pdf");
74      }
75  
76      /**
77       * @throws Exception if any.
78       */
79      public void _testPdfFilterMojo() // MPDF-78: test desactivated because injection of PlexusContainer fails
80              throws Exception {
81          executePdfMojo("pom_filtering.xml", "filtering/maven-pdf-plugin-doc-1.0-SNAPSHOT.pdf");
82  
83          File foFile = new File(getBasedir(), "/target/test-output/pdf/filtering/maven-pdf-plugin-doc-1.0-SNAPSHOT.fo");
84          assertTrue("FO: Fo file not created!", foFile.exists());
85          assertTrue("FO: Fo file has no content!", foFile.length() > 0);
86  
87          String foContent;
88          try (Reader reader = ReaderFactory.newXmlReader(foFile)) {
89              foContent = IOUtil.toString(reader);
90          }
91  
92          // ${pom.name}
93          assertTrue(foContent.indexOf("Test filtering") > 0);
94          assertTrue(foContent.indexOf("1.0-SNAPSHOT") > 0);
95          // env ${M2_HOME}
96          String m2Home = CommandLineUtils.getSystemEnvVars().getProperty("M2_HOME");
97          if (m2Home != null && !m2Home.isEmpty()) {
98              assertTrue(foContent.indexOf(m2Home) > 0);
99          }
100         // ${project.developers[0].email}
101         assertTrue(foContent.indexOf("vsiveton@apache.org ltheussl@apache.org") > 0);
102         // ${date}
103         // TODO: this might fail on NewYear's eve! :)
104         assertTrue(foContent.indexOf(new DateBean().getDate()) > 0);
105     }
106 
107     protected PdfMojo lookupPdfMojo(String pom) throws Exception {
108         File testPom = new File(getBasedir(), "target/test-classes/unit/pdf/" + pom);
109         assertTrue("testPom does not exist!", testPom.exists());
110         PdfMojo mojo = (PdfMojo) lookupMojo("pdf", testPom);
111         assertNotNull("pdf mojo not found!", mojo);
112         return mojo;
113     }
114 
115     protected File prepareOutputPdf(String filename) {
116         File pdfFile = new File(getBasedir(), "target/test-output/pdf/" + filename);
117         if (pdfFile.exists()) {
118             pdfFile.delete();
119         }
120         return pdfFile;
121     }
122 
123     protected void executePdfMojo(String pom, String pdfFilename) {
124         // MPDF-78: test desactivated because injection of PlexusContainer fails
125         return;
126         /*
127         File pdfFile = prepareOutputPdf( pdfFilename );
128 
129         PdfMojo mojo = lookupPdfMojo( pom );
130         mojo.execute();
131 
132         assertTrue( "FO: Pdf file not created!", pdfFile.exists() );
133         assertTrue( "FO: Pdf file has no content!", pdfFile.length() > 0 );
134         */
135     }
136 }