View Javadoc

1   package org.apache.maven.plugin.javadoc;
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 java.io.File;
23  import java.io.IOException;
24  import java.io.Reader;
25  import java.io.StringReader;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  import junitx.util.PrivateAccessor;
30  
31  import org.apache.commons.lang.SystemUtils;
32  import org.apache.maven.plugin.logging.Log;
33  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
34  import org.apache.maven.shared.invoker.MavenInvocationException;
35  import org.codehaus.plexus.util.FileUtils;
36  import org.codehaus.plexus.util.IOUtil;
37  import org.codehaus.plexus.util.ReaderFactory;
38  import org.codehaus.plexus.util.StringUtils;
39  
40  import com.thoughtworks.qdox.JavaDocBuilder;
41  import com.thoughtworks.qdox.model.AbstractInheritableJavaEntity;
42  import com.thoughtworks.qdox.model.AbstractJavaEntity;
43  import com.thoughtworks.qdox.model.DocletTag;
44  import com.thoughtworks.qdox.model.JavaClass;
45  import com.thoughtworks.qdox.model.JavaMethod;
46  
47  /**
48   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
49   * @version $Id: FixJavadocMojoTest.java 1147437 2011-07-16 15:30:31Z rfscholte $
50   */
51  public class FixJavadocMojoTest
52      extends AbstractMojoTestCase
53  {
54      /** The vm line separator */
55      private static final String EOL = System.getProperty( "line.separator" );
56  
57      /** flag to copy repo only one time */
58      private static boolean TEST_REPO_CREATED = false;
59  
60      /** {@inheritDoc} */
61      protected void setUp()
62          throws Exception
63      {
64          super.setUp();
65  
66          createTestRepo();
67      }
68  
69      /**
70       * Create test repository in target directory.
71       *
72       * @throws IOException if any
73       */
74      private void createTestRepo()
75          throws IOException
76      {
77          if ( TEST_REPO_CREATED )
78          {
79              return;
80          }
81  
82          File localRepo = new File( getBasedir(), "target/local-repo/" );
83          localRepo.mkdirs();
84  
85          // ----------------------------------------------------------------------
86          // fix-test-1.0.jar
87          // ----------------------------------------------------------------------
88  
89          File sourceDir = new File( getBasedir(), "src/test/resources/unit/fix-test/repo/" );
90          assertTrue( sourceDir.exists() );
91          FileUtils.copyDirectoryStructure( sourceDir, localRepo );
92  
93          // ----------------------------------------------------------------------
94          // fix-jdk5-test-1.0.jar
95          // ----------------------------------------------------------------------
96  
97          sourceDir = new File( getBasedir(), "src/test/resources/unit/fix-jdk5-test/repo/" );
98          assertTrue( sourceDir.exists() );
99          FileUtils.copyDirectoryStructure( sourceDir, localRepo );
100 
101         // ----------------------------------------------------------------------
102         // fix-jdk6-test-1.0.jar
103         // ----------------------------------------------------------------------
104 
105         sourceDir = new File( getBasedir(), "src/test/resources/unit/fix-jdk6-test/repo/" );
106         assertTrue( sourceDir.exists() );
107         FileUtils.copyDirectoryStructure( sourceDir, localRepo );
108 
109         // Remove SCM files
110         List<String> files =
111             FileUtils.getFileAndDirectoryNames( localRepo, FileUtils.getDefaultExcludesAsString(), null, true,
112                                                 true, true, true );
113         for ( String filename : files )
114         {
115             File file = new File( filename );
116 
117             if ( file.isDirectory() )
118             {
119                 FileUtils.deleteDirectory( file );
120             }
121             else
122             {
123                 file.delete();
124             }
125         }
126 
127         TEST_REPO_CREATED = true;
128     }
129 
130     /**
131      * @throws Exception if any
132      */
133     public void testFix()
134         throws Exception
135     {
136         File testPomBasedir = new File( getBasedir(), "target/test/unit/fix-test" );
137 
138         executeMojoAndTest( testPomBasedir, new String[] { "ClassWithJavadoc.java", "ClassWithNoJavadoc.java",
139             "InterfaceWithJavadoc.java", "InterfaceWithNoJavadoc.java" } );
140     }
141 
142     /**
143      * @throws Exception if any
144      */
145     public void testFixJdk5()
146         throws Exception
147     {
148         if ( !SystemUtils.isJavaVersionAtLeast( 1.5f ) )
149         {
150             getContainer().getLogger().warn(
151                                              "JDK 5.0 or more is required to run fix for '" + getClass().getName()
152                                                  + "#" + getName() + "()'." );
153             return;
154         }
155 
156         File testPomBasedir = new File( getBasedir(), "target/test/unit/fix-jdk5-test" );
157         executeMojoAndTest( testPomBasedir, new String[] { "ClassWithJavadoc.java", "ClassWithNoJavadoc.java",
158             "InterfaceWithJavadoc.java", "InterfaceWithNoJavadoc.java", "SubClassWithJavadoc.java" } );
159     }
160 
161     /**
162      * @throws Exception if any
163      */
164     public void testFixJdk6()
165         throws Exception
166     {
167         if ( !SystemUtils.isJavaVersionAtLeast( 1.6f ) )
168         {
169             getContainer().getLogger().warn(
170                                              "JDK 6.0 or more is required to run fix for '" + getClass().getName()
171                                                  + "#" + getName() + "()'." );
172             return;
173         }
174 
175         File testPomBasedir = new File( getBasedir(), "target/test/unit/fix-jdk6-test" );
176         executeMojoAndTest( testPomBasedir, new String[] { "ClassWithJavadoc.java", "InterfaceWithJavadoc.java" } );
177     }
178 
179     // ----------------------------------------------------------------------
180     // Test private static methods
181     // ----------------------------------------------------------------------
182 
183     /**
184      * @throws Throwable if any
185      */
186     public void testAutodetectIndentation()
187         throws Throwable
188     {
189         String s = null;
190         assertEquals( "", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "autodetectIndentation",
191                                                   new Class[] { String.class }, new Object[] { s } ) );
192 
193         s = "no indentation";
194         assertEquals( "", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "autodetectIndentation",
195                                                   new Class[] { String.class }, new Object[] { s } ) );
196 
197         s = "no indentation with right spaces  ";
198         assertEquals( "", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "autodetectIndentation",
199                                                   new Class[] { String.class }, new Object[] { s } ) );
200 
201         s = "    indentation";
202         assertEquals( "    ", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "autodetectIndentation",
203                                                       new Class[] { String.class }, new Object[] { s } ) );
204 
205         s = "    indentation with right spaces  ";
206         assertEquals( "    ", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "autodetectIndentation",
207                                                       new Class[] { String.class }, new Object[] { s } ) );
208 
209         s = "\ttab indentation";
210         assertEquals( "\t", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "autodetectIndentation",
211                                                     new Class[] { String.class }, new Object[] { s } ) );
212 
213         s = "  \n  indentation with right spaces  ";
214         assertEquals( "  \n  ", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "autodetectIndentation",
215                                                         new Class[] { String.class }, new Object[] { s } ) );
216     }
217 
218     /**
219      * @throws Throwable if any
220      */
221     public void testTrimLeft()
222         throws Throwable
223     {
224         assertEquals( "", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "trimLeft",
225                                                   new Class[] { String.class }, new Object[] { null } ) );
226         assertEquals( "", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "trimLeft",
227                                                   new Class[] { String.class }, new Object[] { "  " } ) );
228         assertEquals( "", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "trimLeft",
229                                                   new Class[] { String.class }, new Object[] { "  \t  " } ) );
230         assertEquals( "a", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "trimLeft",
231                                                    new Class[] { String.class }, new Object[] { "a" } ) );
232         assertEquals( "a", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "trimLeft",
233                                                    new Class[] { String.class }, new Object[] { "  a" } ) );
234         assertEquals( "a", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "trimLeft",
235                                                    new Class[] { String.class }, new Object[] { "\ta" } ) );
236         assertEquals( "a  ", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "trimLeft",
237                                                      new Class[] { String.class }, new Object[] { "  a  " } ) );
238         assertEquals( "a\t", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "trimLeft",
239                                                      new Class[] { String.class }, new Object[] { "\ta\t" } ) );
240     }
241 
242     /**
243      * @throws Throwable if any
244      */
245     public void testTrimRight()
246         throws Throwable
247     {
248         assertEquals( "", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "trimRight",
249                                                   new Class[] { String.class }, new Object[] { null } ) );
250         assertEquals( "", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "trimRight",
251                                                   new Class[] { String.class }, new Object[] { "  " } ) );
252         assertEquals( "", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "trimRight",
253                                                   new Class[] { String.class }, new Object[] { "  \t  " } ) );
254         assertEquals( "a", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "trimRight",
255                                                    new Class[] { String.class }, new Object[] { "a" } ) );
256         assertEquals( "a", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "trimRight",
257                                                    new Class[] { String.class }, new Object[] { "a  " } ) );
258         assertEquals( "a", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "trimRight",
259                                                    new Class[] { String.class }, new Object[] { "a\t" } ) );
260         assertEquals( "  a", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "trimRight",
261                                                      new Class[] { String.class }, new Object[] { "  a  " } ) );
262         assertEquals( "\ta", PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "trimRight",
263                                                      new Class[] { String.class }, new Object[] { "\ta\t" } ) );
264     }
265 
266     /**
267      * @throws Throwable if any
268      */
269     public void testHasInheritedTag()
270         throws Throwable
271     {
272         String content = "/** {@inheritDoc} */";
273         Boolean has =
274             (Boolean) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "hasInheritedTag",
275                                               new Class[] { String.class }, new Object[] { content } );
276         assertEquals( Boolean.TRUE, has );
277 
278         content = "/**{@inheritDoc}*/";
279         has =
280             (Boolean) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "hasInheritedTag",
281                                               new Class[] { String.class }, new Object[] { content } );
282         assertEquals( Boolean.TRUE, has );
283 
284         content = "/**{@inheritDoc  }  */";
285         has =
286             (Boolean) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "hasInheritedTag",
287                                               new Class[] { String.class }, new Object[] { content } );
288         assertEquals( Boolean.TRUE, has );
289 
290         content = "/**  {@inheritDoc  }  */";
291         has =
292             (Boolean) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "hasInheritedTag",
293                                               new Class[] { String.class }, new Object[] { content } );
294         assertEquals( Boolean.TRUE, has );
295 
296         content = "/** */";
297         has =
298             (Boolean) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "hasInheritedTag",
299                                               new Class[] { String.class }, new Object[] { content } );
300         assertEquals( Boolean.FALSE, has );
301 
302         content = "/**{  @inheritDoc  }*/";
303         has =
304             (Boolean) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "hasInheritedTag",
305                                               new Class[] { String.class }, new Object[] { content } );
306         assertEquals( Boolean.FALSE, has );
307 
308         content = "/**{@ inheritDoc}*/";
309         has =
310             (Boolean) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "hasInheritedTag",
311                                               new Class[] { String.class }, new Object[] { content } );
312         assertEquals( Boolean.FALSE, has );
313     }
314 
315     /**
316      * @throws Throwable if any
317      */
318     public void testJavadocComment()
319         throws Throwable
320     {
321         String content = "/**" + EOL +
322                 " * Dummy Class." + EOL +
323                 " */" + EOL +
324                 "public class DummyClass" + EOL +
325                 "{" + EOL +
326                 "    /**" + EOL +
327                 "     *" + EOL +
328                 "     * Dummy" + EOL +
329                 "     *" + EOL +
330                 "     *      Method." + EOL +
331                 "     *" + EOL +
332                 "     * @param args not" + EOL +
333                 "     *" + EOL +
334                 "     * null" + EOL +
335                 "     * @param i non negative" + EOL +
336                 "     * @param object could" + EOL +
337                 "     * be" + EOL +
338                 "     *      null" + EOL +
339                 "     * @return a" + EOL +
340                 "     * String" + EOL +
341                 "     *" + EOL +
342                 "     * @throws Exception if" + EOL +
343                 "     * any" + EOL +
344                 "     *" + EOL +
345                 "     */" + EOL +
346                 "    public static String dummyMethod( String[] args, int i, Object object )" + EOL +
347                 "        throws Exception" + EOL +
348                 "    {" + EOL +
349                 "        return null;" + EOL +
350                 "    }" + EOL +
351                 "}";
352 
353         JavaDocBuilder builder = new JavaDocBuilder();
354         builder.setEncoding( "UTF-8" );
355         builder.addSource( new StringReader( content ) );
356 
357         JavaClass[] classes = builder.getClasses();
358         JavaClass clazz = classes[0];
359 
360         JavaMethod javaMethod = clazz.getMethods()[0];
361 
362         String javadoc =
363             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "extractOriginalJavadoc", new Class[] {
364                 String.class, AbstractJavaEntity.class }, new Object[] { content, javaMethod } );
365         assertEquals( "    /**" + EOL +
366                 "     *" + EOL +
367                 "     * Dummy" + EOL +
368                 "     *" + EOL +
369                 "     *      Method." + EOL +
370                 "     *" + EOL +
371                 "     * @param args not" + EOL +
372                 "     *" + EOL +
373                 "     * null" + EOL +
374                 "     * @param i non negative" + EOL +
375                 "     * @param object could" + EOL +
376                 "     * be" + EOL +
377                 "     *      null" + EOL +
378                 "     * @return a" + EOL +
379                 "     * String" + EOL +
380                 "     *" + EOL +
381                 "     * @throws Exception if" + EOL +
382                 "     * any" + EOL +
383                 "     *" + EOL +
384                 "     */", javadoc );
385 
386         String javadocContent =
387             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "extractOriginalJavadocContent",
388                                              new Class[] { String.class, AbstractJavaEntity.class }, new Object[] {
389                                                  content, javaMethod } );
390         assertEquals( "     *" + EOL +
391                       "     * Dummy" + EOL +
392                       "     *" + EOL +
393                       "     *      Method." + EOL +
394                       "     *" + EOL +
395                       "     * @param args not" + EOL +
396                       "     *" + EOL +
397                       "     * null" + EOL +
398                       "     * @param i non negative" + EOL +
399                       "     * @param object could" + EOL +
400                       "     * be" + EOL +
401                       "     *      null" + EOL +
402                       "     * @return a" + EOL +
403                       "     * String" + EOL +
404                       "     *" + EOL +
405                       "     * @throws Exception if" + EOL +
406                       "     * any" + EOL +
407                       "     *", javadocContent );
408 
409         String withoutEmptyJavadocLines =
410             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "removeLastEmptyJavadocLines",
411                                              new Class[] { String.class }, new Object[] { javadocContent } );
412         assertTrue( withoutEmptyJavadocLines.endsWith( "any" ) );
413 
414         String methodJavadoc =
415             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "getJavadocComment", new Class[] {
416                 String.class, AbstractJavaEntity.class }, new Object[] { content, javaMethod } );
417         assertEquals( "     *" + EOL +
418                 "     * Dummy" + EOL +
419                 "     *" + EOL +
420                 "     *      Method." + EOL +
421                 "     *", methodJavadoc );
422         withoutEmptyJavadocLines =
423             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "removeLastEmptyJavadocLines",
424                                              new Class[] { String.class }, new Object[] { methodJavadoc } );
425         assertTrue( withoutEmptyJavadocLines.endsWith( "Method." ) );
426 
427         assertEquals( 5, javaMethod.getTags().length );
428 
429         AbstractFixJavadocMojo mojoInstance = new FixJavadocMojo();
430         setVariableValueToObject( mojoInstance, "fixTagsSplitted", new String[] { "all" } );
431 
432         DocletTag tag = javaMethod.getTags()[0];
433         String tagJavadoc =
434             (String) PrivateAccessor.invoke( mojoInstance, "getJavadocComment", new Class[] {
435                 String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content,
436                 javaMethod, tag } );
437         assertEquals( "     * @param args not" + EOL +
438                 "     *" + EOL +
439                 "     * null", tagJavadoc );
440         withoutEmptyJavadocLines =
441             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "removeLastEmptyJavadocLines",
442                                              new Class[] { String.class }, new Object[] { tagJavadoc } );
443         assertTrue( withoutEmptyJavadocLines.endsWith( "null" ) );
444 
445         tag = javaMethod.getTags()[1];
446         tagJavadoc =
447             (String) PrivateAccessor.invoke( mojoInstance, "getJavadocComment", new Class[] {
448                 String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content,
449                 javaMethod, tag } );
450         assertEquals( "     * @param i non negative", tagJavadoc );
451         withoutEmptyJavadocLines =
452             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "removeLastEmptyJavadocLines",
453                                              new Class[] { String.class }, new Object[] { tagJavadoc } );
454         assertTrue( withoutEmptyJavadocLines.endsWith( "negative" ) );
455 
456         tag = javaMethod.getTags()[2];
457         tagJavadoc =
458             (String) PrivateAccessor.invoke( mojoInstance, "getJavadocComment", new Class[] {
459                 String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content,
460                 javaMethod, tag } );
461         assertEquals( "     * @param object could" + EOL +
462                 "     * be" + EOL +
463                 "     *      null", tagJavadoc );
464         withoutEmptyJavadocLines =
465             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "removeLastEmptyJavadocLines",
466                                              new Class[] { String.class }, new Object[] { tagJavadoc } );
467         assertTrue( withoutEmptyJavadocLines.endsWith( "null" ) );
468 
469         tag = javaMethod.getTags()[3];
470         tagJavadoc =
471             (String) PrivateAccessor.invoke( mojoInstance, "getJavadocComment", new Class[] {
472                 String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content,
473                 javaMethod, tag } );
474         assertEquals( "     * @return a" + EOL +
475                 "     * String" + EOL +
476                 "     *", tagJavadoc );
477         withoutEmptyJavadocLines =
478             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "removeLastEmptyJavadocLines",
479                                              new Class[] { String.class }, new Object[] { tagJavadoc } );
480         assertTrue( withoutEmptyJavadocLines.endsWith( "String" ) );
481 
482         tag = javaMethod.getTags()[4];
483         tagJavadoc =
484             (String) PrivateAccessor.invoke( mojoInstance, "getJavadocComment", new Class[] {
485                 String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content,
486                 javaMethod, tag } );
487         assertEquals( "     * @throws Exception if" + EOL +
488                 "     * any" + EOL +
489                 "     *", tagJavadoc );
490         withoutEmptyJavadocLines =
491             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "removeLastEmptyJavadocLines",
492                                              new Class[] { String.class }, new Object[] { tagJavadoc } );
493         assertTrue( withoutEmptyJavadocLines.endsWith( "any" ) );
494     }
495 
496     /**
497      * @throws Throwable if any
498      */
499     public void testJavadocCommentJdk5()
500         throws Throwable
501     {
502         if ( !SystemUtils.isJavaVersionAtLeast( 1.5f ) )
503         {
504             getContainer().getLogger().warn(
505                                              "JDK 5.0 or more is required to run fix for '" + getClass().getName()
506                                                  + "#" + getName() + "()'." );
507             return;
508         }
509 
510         String content = "/**" + EOL +
511                 " * Dummy Class." + EOL +
512                 " */" + EOL +
513                 "public class DummyClass" + EOL +
514                 "{" + EOL +
515                 "    /**" + EOL +
516                 "     * Dummy method." + EOL +
517                 "     *" + EOL +
518                 "     * @param <K>  The Key type for the method" + EOL +
519                 "     * @param <V>  The Value type for the method" + EOL +
520                 "     * @param name The name." + EOL +
521                 "     * @return A map configured." + EOL +
522                 "     */" + EOL +
523                 "    public <K, V> java.util.Map<K, V> dummyMethod( String name )" + EOL +
524                 "    {" + EOL +
525                 "        return null;" + EOL +
526                 "    }" + EOL +
527                 "}";
528 
529         JavaDocBuilder builder = new JavaDocBuilder();
530         builder.setEncoding( "UTF-8" );
531         builder.addSource( new StringReader( content ) );
532 
533         JavaClass[] classes = builder.getClasses();
534         JavaClass clazz = classes[0];
535 
536         JavaMethod javaMethod = clazz.getMethods()[0];
537 
538         String methodJavadoc =
539             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "getJavadocComment", new Class[] {
540                 String.class, AbstractJavaEntity.class }, new Object[] { content, javaMethod } );
541         assertEquals( "     * Dummy method." + EOL +
542                 "     *", methodJavadoc );
543 
544         assertEquals( 4, javaMethod.getTags().length );
545 
546         AbstractFixJavadocMojo mojoInstance = new FixJavadocMojo();
547         setVariableValueToObject( mojoInstance, "fixTagsSplitted", new String[] { "all" } );
548 
549         DocletTag tag = javaMethod.getTags()[0];
550         String tagJavadoc =
551             (String) PrivateAccessor.invoke( mojoInstance, "getJavadocComment", new Class[] {
552                 String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content,
553                 javaMethod, tag } );
554         assertEquals( "     * @param <K>  The Key type for the method", tagJavadoc );
555 
556         tag = javaMethod.getTags()[1];
557         tagJavadoc =
558             (String) PrivateAccessor.invoke( mojoInstance, "getJavadocComment", new Class[] {
559                 String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content,
560                 javaMethod, tag } );
561         assertEquals( "     * @param <V>  The Value type for the method", tagJavadoc );
562 
563         tag = javaMethod.getTags()[2];
564         tagJavadoc =
565             (String) PrivateAccessor.invoke( mojoInstance, "getJavadocComment", new Class[] {
566                 String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content,
567                 javaMethod, tag } );
568         assertEquals( "     * @param name The name.", tagJavadoc );
569 
570         tag = javaMethod.getTags()[3];
571         tagJavadoc =
572             (String) PrivateAccessor.invoke( mojoInstance, "getJavadocComment", new Class[] {
573                 String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content,
574                 javaMethod, tag } );
575         assertEquals( "     * @return A map configured.", tagJavadoc );
576     }
577 
578     // ----------------------------------------------------------------------
579     // private methods
580     // ----------------------------------------------------------------------
581 
582     /**
583      * @param testPomBasedir the basedir for the test project
584      * @param clazzToCompare an array of the classes name to compare
585      * @throws Exception if any
586      */
587     private void executeMojoAndTest( File testPomBasedir, String[] clazzToCompare )
588         throws Exception
589     {
590         prepareTestProjects( testPomBasedir.getName() );
591 
592         File testPom = new File( testPomBasedir, "pom.xml" );
593         assertTrue( testPom.getAbsolutePath() + " should exist", testPom.exists() );
594 
595         FixJavadocMojo mojo = (FixJavadocMojo) lookupMojo( "fix", testPom );
596         assertNotNull( mojo );
597 
598         // compile the test project
599         invokeCompileGoal( testPom, mojo.getLog() );
600         assertTrue( new File( testPomBasedir, "target/classes" ).exists() );
601 
602         mojo.execute();
603 
604         File expectedDir = new File( testPomBasedir, "expected/src/main/java/fix/test" );
605         assertTrue( expectedDir.exists() );
606 
607         File generatedDir = new File( testPomBasedir, "target/generated/fix/test" );
608         assertTrue( generatedDir.exists() );
609 
610         for ( int i = 0; i < clazzToCompare.length; i++ )
611         {
612             String className = clazzToCompare[i];
613             assertEquals( new File( expectedDir, className ), new File( generatedDir, className ) );
614         }
615     }
616 
617     /**
618      * Invoke the compilation on the given pom file.
619      *
620      * @param testPom not null
621      * @param log not null
622      * @throws MavenInvocationException if any
623      */
624     private void invokeCompileGoal( File testPom, Log log )
625         throws MavenInvocationException
626     {
627         List<String> goals = new ArrayList<String>();
628         goals.add( "clean" );
629         goals.add( "compile" );
630         File invokerDir = new File( getBasedir(), "target/invoker" );
631         invokerDir.mkdirs();
632         File invokerLogFile = FileUtils.createTempFile( "FixJavadocMojoTest", ".txt", invokerDir );
633         JavadocUtil.invokeMaven( log, new File( getBasedir(), "target/local-repo" ), testPom, goals, null,
634                                  invokerLogFile );
635     }
636 
637     // ----------------------------------------------------------------------
638     // static methods
639     // ----------------------------------------------------------------------
640 
641     /**
642      * Asserts that files are equal. If they are not an AssertionFailedError is thrown.
643      *
644      * @throws IOException if any
645      */
646     private static void assertEquals( File expected, File actual )
647         throws IOException
648     {
649         assertTrue( expected.exists() );
650         String expectedContent = StringUtils.unifyLineSeparators( readFile( expected ) );
651 
652         assertTrue( actual.exists() );
653         String actualContent = StringUtils.unifyLineSeparators( readFile( actual ) );
654 
655         assertEquals( "Expected file: " + expected.getAbsolutePath() + ", actual file: "
656             + actual.getAbsolutePath(), expectedContent, actualContent );
657     }
658 
659     /**
660      * @param testProjectDirName not null
661      * @throws IOException if any
662      */
663     private static void prepareTestProjects( String testProjectDirName )
664         throws IOException
665     {
666         File testPomBasedir = new File( getBasedir(), "target/test/unit/" + testProjectDirName );
667 
668         // Using unit test dir
669         FileUtils
670                  .copyDirectoryStructure(
671                                           new File( getBasedir(), "src/test/resources/unit/" + testProjectDirName ),
672                                           testPomBasedir );
673         List<String> scmFiles = FileUtils.getDirectoryNames( testPomBasedir, "**/.svn", null, true );
674         for ( String filename : scmFiles )
675         {
676             File dir = new File( filename );
677 
678             if ( dir.isDirectory() )
679             {
680                 FileUtils.deleteDirectory( dir );
681             }
682         }
683     }
684 
685     /**
686      * @param file not null
687      * @return the content of the given file
688      * @throws IOException if any
689      */
690     private static String readFile( File file )
691         throws IOException
692     {
693         Reader fileReader = null;
694         try
695         {
696             fileReader = ReaderFactory.newReader( file, "UTF-8" );
697             return IOUtil.toString( fileReader );
698         }
699         finally
700         {
701             IOUtil.close( fileReader );
702         }
703     }
704 }