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.plugin.jxr;
20  
21  import java.io.File;
22  import java.nio.file.Files;
23  import java.nio.file.Path;
24  import java.util.Locale;
25  
26  import org.codehaus.plexus.util.FileUtils;
27  
28  /**
29   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
30   * @author <a href="mailto:dennisl@apache.org">Dennis Lundberg</a>
31   */
32  public class JxrReportTest extends AbstractJxrTestCase {
33      /**
34       * Test the plugin with original configuration
35       *
36       * @throws Exception
37       */
38      public void testDefaultConfiguration() throws Exception {
39          File resourcesDir = new File(getBasedir(), "src/test/resources/unit/default-configuration");
40  
41          File outputDir = new File(getBasedir(), "target/test/unit/default-configuration/target/site");
42          File xrefDir = new File(outputDir, "xref");
43  
44          FileUtils.copyDirectory(new File(resourcesDir, "javadoc-files"), outputDir);
45  
46          generateReport("jxr", "default-configuration/default-configuration-plugin-config.xml");
47  
48          // check if xref files were generated
49          assertTrue(new File(xrefDir, "allclasses-frame.html").exists());
50          assertTrue(new File(xrefDir, "index.html").exists());
51          assertTrue(new File(xrefDir, "overview-frame.html").exists());
52          assertTrue(new File(xrefDir, "overview-summary.html").exists());
53          assertTrue(new File(xrefDir, "stylesheet.css").exists());
54          assertTrue(new File(xrefDir, "def/configuration/App.html").exists());
55          assertTrue(new File(xrefDir, "def/configuration/AppSample.html").exists());
56          assertTrue(new File(xrefDir, "def/configuration/package-frame.html").exists());
57          assertTrue(new File(xrefDir, "def/configuration/package-summary.html").exists());
58  
59          // check if there's a link to the javadoc files
60          String str = readFile(xrefDir, "def/configuration/AppSample.html");
61          assertTrue(str.toLowerCase().contains("/apidocs/def/configuration/appsample.html\""));
62  
63          str = readFile(xrefDir, "def/configuration/App.html");
64          assertTrue(str.toLowerCase().contains("/apidocs/def/configuration/app.html\"".toLowerCase()));
65  
66          // check if encoding is UTF-8, the default value
67          assertTrue(str.contains("text/html; charset=UTF-8"));
68      }
69  
70      /**
71       * Test the plugin with jdk-4 configuration
72       *
73       * @throws Exception
74       */
75      public void testJdk4Configuration() throws Exception {
76          File resourcesDir = new File(getBasedir(), "src/test/resources/unit/default-configuration");
77  
78          File outputDir = new File(getBasedir(), "target/test/unit/default-configuration/target/site/4");
79          File xrefDir = new File(outputDir, "xref");
80  
81          FileUtils.copyDirectory(new File(resourcesDir, "javadoc-files"), outputDir);
82  
83          generateReport("jxr", "default-configuration/default-configuration-plugin-config-4.xml");
84  
85          // check if xref files were generated
86          assertTrue(new File(xrefDir, "allclasses-frame.html").exists());
87          assertTrue(new File(xrefDir, "index.html").exists());
88          assertTrue(new File(xrefDir, "overview-frame.html").exists());
89          assertTrue(new File(xrefDir, "overview-summary.html").exists());
90          assertTrue(new File(xrefDir, "stylesheet.css").exists());
91          assertTrue(new File(xrefDir, "def/configuration/App.html").exists());
92          assertTrue(new File(xrefDir, "def/configuration/AppSample.html").exists());
93          assertTrue(new File(xrefDir, "def/configuration/package-frame.html").exists());
94          assertTrue(new File(xrefDir, "def/configuration/package-summary.html").exists());
95  
96          // check if there's a link to the javadoc files
97          String str = readFile(xrefDir, "def/configuration/AppSample.html");
98          assertTrue(str.toLowerCase(Locale.US).contains("/apidocs/def/configuration/appsample.html\""));
99  
100         str = readFile(xrefDir, "def/configuration/App.html");
101         assertTrue(str.toLowerCase(Locale.US).contains("/apidocs/def/configuration/app.html\""));
102 
103         // check if encoding is UTF-8, the default value
104         assertTrue(str.contains("text/html; charset=UTF-8"));
105     }
106 
107     /**
108      * Test the plugin with jdk-6 configuration
109      *
110      * @throws Exception
111      */
112     public void testJdk6Configuration() throws Exception {
113         File resourcesDir = new File(getBasedir(), "src/test/resources/unit/default-configuration");
114 
115         File outputDir = new File(getBasedir(), "target/test/unit/default-configuration/target/site/6");
116         File xrefDir = new File(outputDir, "xref");
117 
118         FileUtils.copyDirectory(new File(resourcesDir, "javadoc-files"), outputDir);
119 
120         generateReport("jxr", "default-configuration/default-configuration-plugin-config-6.xml");
121 
122         // check if xref files were generated
123         assertTrue(new File(xrefDir, "allclasses-frame.html").exists());
124         assertTrue(new File(xrefDir, "index.html").exists());
125         assertTrue(new File(xrefDir, "overview-frame.html").exists());
126         assertTrue(new File(xrefDir, "overview-summary.html").exists());
127         assertTrue(new File(xrefDir, "stylesheet.css").exists());
128         assertTrue(new File(xrefDir, "def/configuration/App.html").exists());
129         assertTrue(new File(xrefDir, "def/configuration/AppSample.html").exists());
130         assertTrue(new File(xrefDir, "def/configuration/package-frame.html").exists());
131         assertTrue(new File(xrefDir, "def/configuration/package-summary.html").exists());
132 
133         // check if there's a link to the javadoc files
134         String str = readFile(xrefDir, "def/configuration/AppSample.html");
135         assertTrue(str.toLowerCase(Locale.US).contains("/apidocs/def/configuration/appsample.html\""));
136 
137         str = readFile(xrefDir, "def/configuration/App.html");
138         assertTrue(str.toLowerCase(Locale.US).contains("/apidocs/def/configuration/app.html\""));
139 
140         // check if encoding is UTF-8, the default value
141         assertTrue(str.contains("text/html; charset=UTF-8"));
142     }
143 
144     /**
145      * Test the plugin with jdk-7 configuration
146      *
147      * @throws Exception
148      */
149     public void testJdk7Configuration() throws Exception {
150         File resourcesDir = new File(getBasedir(), "src/test/resources/unit/default-configuration");
151 
152         File outputDir = new File(getBasedir(), "target/test/unit/default-configuration/target/site/7");
153         File xrefDir = new File(outputDir, "xref");
154 
155         FileUtils.copyDirectory(new File(resourcesDir, "javadoc-files"), outputDir);
156 
157         generateReport("jxr", "default-configuration/default-configuration-plugin-config-7.xml");
158 
159         // check if xref files were generated
160         assertTrue(new File(xrefDir, "allclasses-frame.html").exists());
161         assertTrue(new File(xrefDir, "index.html").exists());
162         assertTrue(new File(xrefDir, "overview-frame.html").exists());
163         assertTrue(new File(xrefDir, "overview-summary.html").exists());
164         assertTrue(new File(xrefDir, "stylesheet.css").exists());
165         assertTrue(new File(xrefDir, "resources/background.gif").exists());
166         assertTrue(new File(xrefDir, "resources/tab.gif").exists());
167         assertTrue(new File(xrefDir, "resources/titlebar.gif").exists());
168         assertTrue(new File(xrefDir, "resources/titlebar_end.gif").exists());
169         assertTrue(new File(xrefDir, "def/configuration/App.html").exists());
170         assertTrue(new File(xrefDir, "def/configuration/AppSample.html").exists());
171         assertTrue(new File(xrefDir, "def/configuration/package-frame.html").exists());
172         assertTrue(new File(xrefDir, "def/configuration/package-summary.html").exists());
173 
174         // check if there's a link to the javadoc files
175         String str = readFile(xrefDir, "def/configuration/AppSample.html");
176         assertTrue(str.toLowerCase(Locale.US).contains("/apidocs/def/configuration/appsample.html\""));
177 
178         str = readFile(xrefDir, "def/configuration/App.html");
179         assertTrue(str.toLowerCase(Locale.US).contains("/apidocs/def/configuration/app.html\""));
180 
181         // check if encoding is UTF-8, the default value
182         assertTrue(str.contains("text/html; charset=UTF-8"));
183     }
184 
185     /**
186      * Test the plugin with jdk-8 configuration
187      *
188      * @throws Exception
189      */
190     public void testJdk8Configuration() throws Exception {
191         File resourcesDir = new File(getBasedir(), "src/test/resources/unit/default-configuration");
192 
193         File outputDir = new File(getBasedir(), "target/test/unit/default-configuration/target/site/8");
194         File xrefDir = new File(outputDir, "xref");
195 
196         FileUtils.copyDirectory(new File(resourcesDir, "javadoc-files"), outputDir);
197 
198         generateReport("jxr", "default-configuration/default-configuration-plugin-config-8.xml");
199 
200         // check if xref files were generated
201         assertTrue(new File(xrefDir, "allclasses-frame.html").exists());
202         assertTrue(new File(xrefDir, "index.html").exists());
203         assertTrue(new File(xrefDir, "overview-frame.html").exists());
204         assertTrue(new File(xrefDir, "overview-summary.html").exists());
205         assertTrue(new File(xrefDir, "stylesheet.css").exists());
206         assertTrue(new File(xrefDir, "def/configuration/App.html").exists());
207         assertTrue(new File(xrefDir, "def/configuration/AppSample.html").exists());
208         assertTrue(new File(xrefDir, "def/configuration/package-frame.html").exists());
209         assertTrue(new File(xrefDir, "def/configuration/package-summary.html").exists());
210 
211         // check if there's a link to the javadoc files
212         String str = readFile(xrefDir, "def/configuration/AppSample.html");
213         assertTrue(str.toLowerCase().contains("/apidocs/def/configuration/appsample.html\""));
214 
215         str = readFile(xrefDir, "def/configuration/App.html");
216         assertTrue(str.toLowerCase().contains("/apidocs/def/configuration/app.html\"".toLowerCase()));
217 
218         // check if encoding is UTF-8, the default value
219         assertTrue(str.contains("text/html; charset=UTF-8"));
220     }
221 
222     /**
223      * Test when javadocLink is disabled in the configuration
224      *
225      * @throws Exception
226      */
227     public void testNoJavadocLink() throws Exception {
228         generateReport("jxr", "nojavadoclink-configuration/nojavadoclink-configuration-plugin-config.xml");
229 
230         File xrefDir = new File(getBasedir(), "target/test/unit/nojavadoclink-configuration/target/site/xref");
231 
232         // check if xref files were generated
233         assertTrue(new File(xrefDir, "allclasses-frame.html").exists());
234         assertTrue(new File(xrefDir, "index.html").exists());
235         assertTrue(new File(xrefDir, "overview-frame.html").exists());
236         assertTrue(new File(xrefDir, "overview-summary.html").exists());
237         assertTrue(new File(xrefDir, "stylesheet.css").exists());
238         assertTrue(new File(xrefDir, "nojavadoclink/configuration/App.html").exists());
239         assertTrue(new File(xrefDir, "nojavadoclink/configuration/AppSample.html").exists());
240         assertTrue(new File(xrefDir, "nojavadoclink/configuration/package-frame.html").exists());
241         assertTrue(new File(xrefDir, "nojavadoclink/configuration/package-summary.html").exists());
242         assertTrue(new File(xrefDir, "nojavadoclink/configuration/sample/package-summary.html").exists());
243         assertTrue(new File(xrefDir, "nojavadoclink/configuration/sample/package-frame.html").exists());
244         assertTrue(new File(xrefDir, "nojavadoclink/configuration/sample/Sample.html").exists());
245 
246         // check if there's a link to the javadoc files
247         String str = readFile(xrefDir, "nojavadoclink/configuration/AppSample.html");
248         assertEquals(str.toLowerCase(Locale.US).indexOf("/apidocs/nojavadoclink/configuration/appsample.html\""), -1);
249 
250         str = readFile(xrefDir, "nojavadoclink/configuration/App.html");
251         assertEquals(str.toLowerCase(Locale.US).indexOf("/apidocs/nojavadoclink/configuration/app.html\""), -1);
252 
253         str = readFile(xrefDir, "nojavadoclink/configuration/sample/Sample.html");
254         assertEquals(str.toLowerCase().indexOf("/apidocs/nojavadoclink/configuration/sample/sample.html\""), -1);
255 
256         // check if encoding is ISO-8859-1, like specified in the plugin configuration
257         assertTrue(str.contains("text/html; charset=ISO-8859-1"));
258     }
259 
260     /**
261      * Method for testing plugin when aggregate parameter is set to true
262      *
263      * @throws Exception
264      */
265     public void testAggregate() throws Exception {
266         generateReport("jxr", "aggregate-test/aggregate-test-plugin-config.xml");
267 
268         File xrefDir = new File(getBasedir(), "target/test/unit/aggregate-test/target/site/xref");
269 
270         // check if xref files were generated for submodule1
271         assertTrue(new File(xrefDir, "aggregate/test/submodule1/package-frame.html").exists());
272         assertTrue(new File(xrefDir, "aggregate/test/submodule1/package-summary.html").exists());
273         assertTrue(new File(xrefDir, "aggregate/test/submodule1/Submodule1App.html").exists());
274         assertTrue(new File(xrefDir, "aggregate/test/submodule1/Submodule1AppSample.html").exists());
275 
276         // check if xref files were generated for submodule2
277         assertTrue(new File(xrefDir, "aggregate/test/submodule2/package-frame.html").exists());
278         assertTrue(new File(xrefDir, "aggregate/test/submodule2/package-summary.html").exists());
279         assertTrue(new File(xrefDir, "aggregate/test/submodule2/Submodule2App.html").exists());
280         assertTrue(new File(xrefDir, "aggregate/test/submodule2/Submodule2AppSample.html").exists());
281     }
282 
283     /**
284      * Method for testing plugin when the specified javadocDir does not exist
285      *
286      * @throws Exception
287      */
288     public void testNoJavadocDir() throws Exception {
289         generateReport("jxr", "nojavadocdir-test/nojavadocdir-test-plugin-config.xml");
290 
291         File xrefDir = new File(getBasedir(), "target/test/unit/nojavadocdir-test/target/site/xref");
292 
293         // check if there's a link to the javadoc files
294         String str = readFile(xrefDir, "nojavadocdir/test/AppSample.html");
295         assertTrue(str.toLowerCase(Locale.US).contains("/apidocs/nojavadocdir/test/appsample.html"));
296 
297         str = readFile(xrefDir, "nojavadocdir/test/App.html");
298         assertTrue(str.toLowerCase(Locale.US).contains("/apidocs/nojavadocdir/test/app.html"));
299     }
300 
301     /**
302      * Test the plugin with an exclude configuration.
303      *
304      * @throws Exception
305      */
306     public void testExclude() throws Exception {
307         generateReport("jxr", "exclude-configuration/exclude-configuration-plugin-config.xml");
308 
309         Path xrefDir = new File(getBasedir(), "target/test/unit/exclude-configuration/target/site/xref").toPath();
310 
311         // check that the non-excluded xref files were generated
312         assertTrue(Files.exists(xrefDir.resolve("exclude/configuration/App.html")));
313 
314         // check that the excluded xref files were not generated
315         assertFalse(Files.exists(xrefDir.resolve("exclude/configuration/AppSample.html")));
316     }
317 
318     /**
319      * Test the plugin with an include configuration.
320      *
321      * @throws Exception
322      */
323     public void testInclude() throws Exception {
324         generateReport("jxr", "include-configuration/include-configuration-plugin-config.xml");
325 
326         Path xrefDir = new File(getBasedir(), "target/test/unit/include-configuration/target/site/xref").toPath();
327 
328         // check that the included xref files were generated
329         assertTrue(Files.exists(xrefDir.resolve("include/configuration/App.html")));
330 
331         // check that the non-included xref files were not generated
332         assertFalse(Files.exists(xrefDir.resolve("include/configuration/AppSample.html")));
333     }
334 
335     public void testExceptions() {
336         try {
337             generateReport("jxr", "default-configuration/exception-test-plugin-config.xml");
338 
339             fail("Must throw exception");
340         } catch (Exception e) {
341             assertTrue(true);
342         }
343     }
344 
345     /**
346      * Test the jxr for a POM project.
347      *
348      * @throws Exception
349      */
350     public void testPom() throws Exception {
351         generateReport("jxr", "pom-test/pom-test-plugin-config.xml");
352 
353         assertFalse(new File(getBasedir(), "target/test/unit/pom-test").exists());
354     }
355 }