View Javadoc
1   package org.apache.maven.plugin.jxr;
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.FileUtils;
24  
25  import java.io.BufferedReader;
26  import java.io.File;
27  import java.io.FileReader;
28  import java.io.IOException;
29  
30  /**
31   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
32   * @author <a href="mailto:dennisl@apache.org">Dennis Lundberg</a>
33   */
34  public class JxrReportTest
35      extends AbstractMojoTestCase
36  {
37      /**
38       * @see org.apache.maven.plugin.testing.AbstractMojoTestCase#setUp()
39       */
40      protected void setUp()
41          throws Exception
42      {
43          super.setUp();
44      }
45  
46      /**
47       * @see org.codehaus.plexus.PlexusTestCase#tearDown()
48       */
49      protected void tearDown()
50          throws Exception
51      {
52          // nop
53      }
54  
55      /**
56       * Test the plugin with default configuration
57       *
58       * @throws Exception
59       */
60      public void testDefaultConfiguration()
61          throws Exception
62      {
63      	File resourcesDir = new File( getBasedir(), "src/test/resources/unit/default-configuration" );
64  
65      	File outputDir = new File( getBasedir(), "target/test/unit/default-configuration/target/site" );
66      	File xrefDir = new File( outputDir, "xref" );
67  
68      	copyFilesFromDirectory( new File( resourcesDir, "javadoc-files" ), outputDir );
69  
70          File testPom = new File( resourcesDir, "default-configuration-plugin-config.xml" );
71          JxrReport mojo = (JxrReport) lookupMojo( "jxr", testPom );
72          mojo.execute();
73  
74          //check if xref files were generated
75          assertTrue( new File( xrefDir, "allclasses-frame.html" ).exists() );
76          assertTrue( new File( xrefDir, "index.html" ).exists() );
77          assertTrue( new File( xrefDir, "overview-frame.html" ).exists() );
78          assertTrue( new File( xrefDir, "overview-summary.html" ).exists() );
79          assertTrue( new File( xrefDir, "stylesheet.css" ).exists() );
80          assertTrue( new File( xrefDir, "def/configuration/App.html" ).exists() );
81          assertTrue( new File( xrefDir, "def/configuration/AppSample.html" ).exists() );
82          assertTrue( new File( xrefDir, "def/configuration/package-frame.html" ).exists() );
83          assertTrue( new File( xrefDir, "def/configuration/package-summary.html" ).exists() );
84  
85          //check if there's a link to the javadoc files
86          String str = readFile( new File( xrefDir, "def/configuration/AppSample.html" ) );
87          assertTrue( str.toLowerCase().indexOf( "/apidocs/def/configuration/AppSample.html\"".toLowerCase() ) != -1 );
88  
89          str = readFile( new File( xrefDir, "def/configuration/App.html" ) );
90          assertTrue( str.toLowerCase().indexOf( "/apidocs/def/configuration/app.html\"".toLowerCase() ) != -1 );
91  
92          // check if encoding is UTF-8, the default value
93          assertTrue( str.indexOf( "text/html; charset=UTF-8" ) != -1 );
94      }
95  
96      /**
97       * Test when javadocLink is disabled in the configuration
98       *
99       * @throws Exception
100      */
101     public void testNoJavadocLink()
102         throws Exception
103     {
104         File testPom = new File( getBasedir(),
105                                  "src/test/resources/unit/nojavadoclink-configuration/nojavadoclink-configuration-plugin-config.xml" );
106         JxrReport mojo = (JxrReport) lookupMojo( "jxr", testPom );
107         mojo.execute();
108 
109     	File xrefDir = new File( getBasedir(), "target/test/unit/nojavadoclink-configuration/target/site/xref" );
110 
111     	//check if xref files were generated
112         assertTrue( new File( xrefDir, "allclasses-frame.html" ).exists() );
113         assertTrue( new File( xrefDir, "index.html" ).exists() );
114         assertTrue( new File( xrefDir, "overview-frame.html" ).exists() );
115         assertTrue( new File( xrefDir, "overview-summary.html" ).exists() );
116         assertTrue( new File( xrefDir, "stylesheet.css" ).exists() );
117         assertTrue( new File( xrefDir, "nojavadoclink/configuration/App.html" ).exists() );
118         assertTrue( new File( xrefDir, "nojavadoclink/configuration/AppSample.html" ).exists() );
119         assertTrue( new File( xrefDir, "nojavadoclink/configuration/package-frame.html" ).exists() );
120         assertTrue( new File( xrefDir, "nojavadoclink/configuration/package-summary.html" ).exists() );
121         assertTrue( new File( xrefDir, "nojavadoclink/configuration/sample/package-summary.html" ).exists() );
122         assertTrue( new File( xrefDir, "nojavadoclink/configuration/sample/package-frame.html" ).exists() );
123         assertTrue( new File( xrefDir, "nojavadoclink/configuration/sample/Sample.html" ).exists() );
124 
125         //check if there's a link to the javadoc files
126         String str = readFile( new File( xrefDir, "nojavadoclink/configuration/AppSample.html" ) );
127         assertTrue(
128             str.toLowerCase().indexOf( "/apidocs/nojavadoclink/configuration/AppSample.html\"".toLowerCase() ) == -1 );
129 
130         str = readFile( new File( xrefDir, "nojavadoclink/configuration/App.html" ) );
131         assertTrue(
132             str.toLowerCase().indexOf( "/apidocs/nojavadoclink/configuration/app.html\"".toLowerCase() ) == -1 );
133 
134         str = readFile( new File( xrefDir, "nojavadoclink/configuration/sample/Sample.html" ) );
135         assertTrue( str.toLowerCase().indexOf(
136             "/apidocs/nojavadoclink/configuration/sample/sample.html\"".toLowerCase() ) == -1 );
137 
138         // check if encoding is ISO-8859-1, like specified in the plugin configuration
139         assertTrue( str.indexOf( "text/html; charset=ISO-8859-1" ) != -1 );
140     }
141 
142     /**
143      * Method for testing plugin when aggregate parameter is set to true
144      *
145      * @throws Exception
146      */
147     public void testAggregate()
148         throws Exception
149     {
150         File testPom =
151             new File( getBasedir(), "src/test/resources/unit/aggregate-test/aggregate-test-plugin-config.xml" );
152         JxrReport mojo = (JxrReport) lookupMojo( "jxr", testPom );
153         mojo.execute();
154 
155     	File xrefDir = new File( getBasedir(), "target/test/unit/aggregate-test/target/site/xref" );
156 
157         //check if xref files were generated for submodule1
158         assertTrue( new File( xrefDir, "aggregate/test/submodule1/package-frame.html" ).exists() );
159         assertTrue( new File( xrefDir, "aggregate/test/submodule1/package-summary.html" ).exists() );
160         assertTrue( new File( xrefDir, "aggregate/test/submodule1/Submodule1App.html" ).exists() );
161         assertTrue( new File( xrefDir, "aggregate/test/submodule1/Submodule1AppSample.html" ).exists() );
162 
163         //check if xref files were generated for submodule2
164         assertTrue( new File( xrefDir, "aggregate/test/submodule2/package-frame.html" ).exists() );
165         assertTrue( new File( xrefDir, "aggregate/test/submodule2/package-summary.html" ).exists() );
166         assertTrue( new File( xrefDir, "aggregate/test/submodule2/Submodule2App.html" ).exists() );
167         assertTrue( new File( xrefDir, "aggregate/test/submodule2/Submodule2AppSample.html" ).exists() );
168 
169     }
170 
171     /**
172      * Method for testing plugin when the specified javadocDir does not exist
173      *
174      * @throws Exception
175      */
176     public void testNoJavadocDir()
177         throws Exception
178     {
179         File testPom =
180             new File( getBasedir(), "src/test/resources/unit/nojavadocdir-test/nojavadocdir-test-plugin-config.xml" );
181         JxrReport mojo = (JxrReport) lookupMojo( "jxr", testPom );
182         mojo.execute();
183 
184     	File xrefDir = new File( getBasedir(), "target/test/unit/nojavadocdir-test/target/site/xref" );
185 
186     	//check if there's a link to the javadoc files
187         String str = readFile( new File( xrefDir, "nojavadocdir/test/AppSample.html" ) );
188         assertTrue( str.toLowerCase().indexOf( "/apidocs/nojavadocdir/test/AppSample.html".toLowerCase() ) != -1 );
189 
190         str = readFile( new File( xrefDir, "nojavadocdir/test/App.html" ) );
191         assertTrue( str.toLowerCase().indexOf( "/apidocs/nojavadocdir/test/app.html".toLowerCase() ) != -1 );
192 
193     }
194 
195     /**
196      * Test the plugin with an exclude configuration.
197      *
198      * @throws Exception
199      */
200     public void testExclude()
201         throws Exception
202     {
203         File testPom = new File( getBasedir(),
204                                  "src/test/resources/unit/exclude-configuration/exclude-configuration-plugin-config.xml" );
205         JxrReport mojo = (JxrReport) lookupMojo( "jxr", testPom );
206         mojo.execute();
207 
208     	File xrefDir = new File( getBasedir(), "target/test/unit/exclude-configuration/target/site/xref" );
209 
210     	// check that the non-excluded xref files were generated
211         File generatedFile = new File( xrefDir, "exclude/configuration/App.html" );
212         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
213 
214         // check that the excluded xref files were not generated
215         generatedFile = new File( xrefDir, "exclude/configuration/AppSample.html" );
216         assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
217     }
218 
219     /**
220      * Test the plugin with an include configuration.
221      *
222      * @throws Exception
223      */
224     public void testInclude()
225         throws Exception
226     {
227         File testPom = new File( getBasedir(),
228                                  "src/test/resources/unit/include-configuration/include-configuration-plugin-config.xml" );
229         JxrReport mojo = (JxrReport) lookupMojo( "jxr", testPom );
230         mojo.execute();
231 
232     	File xrefDir = new File( getBasedir(), "target/test/unit/include-configuration/target/site/xref" );
233 
234         // check that the included xref files were generated
235         File generatedFile = new File( xrefDir, "include/configuration/App.html" );
236         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
237 
238         // check that the non-included xref files were not generated
239         generatedFile = new File( xrefDir, "include/configuration/AppSample.html" );
240         assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
241     }
242 
243     public void testExceptions()
244         throws Exception
245     {
246         try
247         {
248             File testPom =
249                 new File( getBasedir(), "src/test/resources/unit/default-configuration/exception-test-plugin-config.xml" );
250             JxrReport mojo = (JxrReport) lookupMojo( "jxr", testPom );
251             mojo.execute();
252 
253             fail( "Must throw exception");
254         }
255         catch ( Exception e )
256         {
257             assertTrue( true );
258         }
259     }
260 
261     /**
262      * Test the jxr for a POM project.
263      *
264      * @throws Exception
265      */
266     public void testPom()
267         throws Exception
268     {
269         File testPom = new File( getBasedir(),
270                                  "src/test/resources/unit/pom-test/pom-test-plugin-config.xml" );
271         JxrReport mojo = (JxrReport) lookupMojo( "jxr", testPom );
272         mojo.execute();
273 
274         assertFalse( new File( getBasedir(), "target/test/unit/pom-test" ).exists() );
275     }
276 
277     /**
278      * Copy files from the specified source directory to the specified destination directory
279      *
280      * @param srcDir
281      * @param destDir
282      * @throws IOException
283      */
284     private static void copyFilesFromDirectory( File srcDir, File destDir )
285         throws IOException
286     {
287         FileUtils.copyDirectoryStructure( srcDir, destDir );
288     }
289 
290     /**
291      * Read the contents of the specified file object into a string
292      *
293      * @param file the file to be read
294      * @return a String object that contains the contents of the file
295      * @throws IOException
296      */
297     private static String readFile( File file )
298         throws IOException
299     {
300         String str = "", strTmp = "";
301         BufferedReader in = new BufferedReader( new FileReader( file ) );
302 
303         while ( ( strTmp = in.readLine() ) != null )
304         {
305             str = str + " " + strTmp;
306         }
307         in.close();
308 
309         return str;
310     }
311 }