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.Iterator;
28  import java.util.List;
29  
30  import junitx.util.PrivateAccessor;
31  
32  import org.apache.commons.lang.SystemUtils;
33  import org.apache.maven.plugin.logging.Log;
34  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
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$
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 files =
111             FileUtils.getFileAndDirectoryNames( localRepo, FileUtils.getDefaultExcludesAsString(), null, true,
112                                                 true, true, true );
113         for ( Iterator it = files.iterator(); it.hasNext(); )
114         {
115             File file = new File( it.next().toString() );
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         DocletTag tag = javaMethod.getTags()[0];
430         String tagJavadoc =
431             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "getJavadocComment", new Class[] {
432                 String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content,
433                 javaMethod, tag } );
434         assertEquals( "     * @param args not" + EOL +
435                 "     *" + EOL +
436                 "     * null", tagJavadoc );
437         withoutEmptyJavadocLines =
438             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "removeLastEmptyJavadocLines",
439                                              new Class[] { String.class }, new Object[] { tagJavadoc } );
440         assertTrue( withoutEmptyJavadocLines.endsWith( "null" ) );
441 
442         tag = javaMethod.getTags()[1];
443         tagJavadoc =
444             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "getJavadocComment", new Class[] {
445                 String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content,
446                 javaMethod, tag } );
447         assertEquals( "     * @param i non negative", tagJavadoc );
448         withoutEmptyJavadocLines =
449             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "removeLastEmptyJavadocLines",
450                                              new Class[] { String.class }, new Object[] { tagJavadoc } );
451         assertTrue( withoutEmptyJavadocLines.endsWith( "negative" ) );
452 
453         tag = javaMethod.getTags()[2];
454         tagJavadoc =
455             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "getJavadocComment", new Class[] {
456                 String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content,
457                 javaMethod, tag } );
458         assertEquals( "     * @param object could" + EOL +
459                 "     * be" + EOL +
460                 "     *      null", tagJavadoc );
461         withoutEmptyJavadocLines =
462             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "removeLastEmptyJavadocLines",
463                                              new Class[] { String.class }, new Object[] { tagJavadoc } );
464         assertTrue( withoutEmptyJavadocLines.endsWith( "null" ) );
465 
466         tag = javaMethod.getTags()[3];
467         tagJavadoc =
468             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "getJavadocComment", new Class[] {
469                 String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content,
470                 javaMethod, tag } );
471         assertEquals( "     * @return a" + EOL +
472                 "     * String" + EOL +
473                 "     *", tagJavadoc );
474         withoutEmptyJavadocLines =
475             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "removeLastEmptyJavadocLines",
476                                              new Class[] { String.class }, new Object[] { tagJavadoc } );
477         assertTrue( withoutEmptyJavadocLines.endsWith( "String" ) );
478 
479         tag = javaMethod.getTags()[4];
480         tagJavadoc =
481             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "getJavadocComment", new Class[] {
482                 String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content,
483                 javaMethod, tag } );
484         assertEquals( "     * @throws Exception if" + EOL +
485                 "     * any" + EOL +
486                 "     *", tagJavadoc );
487         withoutEmptyJavadocLines =
488             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "removeLastEmptyJavadocLines",
489                                              new Class[] { String.class }, new Object[] { tagJavadoc } );
490         assertTrue( withoutEmptyJavadocLines.endsWith( "any" ) );
491     }
492 
493     /**
494      * @throws Throwable if any
495      */
496     public void testJavadocCommentJdk5()
497         throws Throwable
498     {
499         if ( !SystemUtils.isJavaVersionAtLeast( 1.5f ) )
500         {
501             getContainer().getLogger().warn(
502                                              "JDK 5.0 or more is required to run fix for '" + getClass().getName()
503                                                  + "#" + getName() + "()'." );
504             return;
505         }
506 
507         String content = "/**" + EOL +
508                 " * Dummy Class." + EOL +
509                 " */" + EOL +
510                 "public class DummyClass" + EOL +
511                 "{" + EOL +
512                 "    /**" + EOL +
513                 "     * Dummy method." + EOL +
514                 "     *" + EOL +
515                 "     * @param <K>  The Key type for the method" + EOL +
516                 "     * @param <V>  The Value type for the method" + EOL +
517                 "     * @param name The name." + EOL +
518                 "     * @return A map configured." + EOL +
519                 "     */" + EOL +
520                 "    public <K, V> java.util.Map<K, V> dummyMethod( String name )" + EOL +
521                 "    {" + EOL +
522                 "        return null;" + EOL +
523                 "    }" + EOL +
524                 "}";
525 
526         JavaDocBuilder builder = new JavaDocBuilder();
527         builder.setEncoding( "UTF-8" );
528         builder.addSource( new StringReader( content ) );
529 
530         JavaClass[] classes = builder.getClasses();
531         JavaClass clazz = classes[0];
532 
533         JavaMethod javaMethod = clazz.getMethods()[0];
534 
535         String methodJavadoc =
536             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "getJavadocComment", new Class[] {
537                 String.class, AbstractJavaEntity.class }, new Object[] { content, javaMethod } );
538         assertEquals( "     * Dummy method." + EOL +
539                 "     *", methodJavadoc );
540 
541         assertEquals( 4, javaMethod.getTags().length );
542 
543         DocletTag tag = javaMethod.getTags()[0];
544         String tagJavadoc =
545             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "getJavadocComment", new Class[] {
546                 String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content,
547                 javaMethod, tag } );
548         assertEquals( "     * @param <K>  The Key type for the method", tagJavadoc );
549 
550         tag = javaMethod.getTags()[1];
551         tagJavadoc =
552             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "getJavadocComment", new Class[] {
553                 String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content,
554                 javaMethod, tag } );
555         assertEquals( "     * @param <V>  The Value type for the method", tagJavadoc );
556 
557         tag = javaMethod.getTags()[2];
558         tagJavadoc =
559             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "getJavadocComment", new Class[] {
560                 String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content,
561                 javaMethod, tag } );
562         assertEquals( "     * @param name The name.", tagJavadoc );
563 
564         tag = javaMethod.getTags()[3];
565         tagJavadoc =
566             (String) PrivateAccessor.invoke( AbstractFixJavadocMojo.class, "getJavadocComment", new Class[] {
567                 String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content,
568                 javaMethod, tag } );
569         assertEquals( "     * @return A map configured.", tagJavadoc );
570     }
571 
572     // ----------------------------------------------------------------------
573     // private methods
574     // ----------------------------------------------------------------------
575 
576     /**
577      * @param testPomBasedir the basedir for the test project
578      * @param clazzToCompare an array of the classes name to compare
579      * @throws Exception if any
580      */
581     private void executeMojoAndTest( File testPomBasedir, String[] clazzToCompare )
582         throws Exception
583     {
584         prepareTestProjects( testPomBasedir.getName() );
585 
586         File testPom = new File( testPomBasedir, "pom.xml" );
587         assertTrue( testPom.getAbsolutePath() + " should exist", testPom.exists() );
588 
589         FixJavadocMojo mojo = (FixJavadocMojo) lookupMojo( "fix", testPom );
590         assertNotNull( mojo );
591 
592         // compile the test project
593         invokeCompileGoal( testPom, mojo.getLog() );
594         assertTrue( new File( testPomBasedir, "target/classes" ).exists() );
595 
596         mojo.execute();
597 
598         File expectedDir = new File( testPomBasedir, "expected/src/main/java/fix/test" );
599         assertTrue( expectedDir.exists() );
600 
601         File generatedDir = new File( testPomBasedir, "target/generated/fix/test" );
602         assertTrue( generatedDir.exists() );
603 
604         for ( int i = 0; i < clazzToCompare.length; i++ )
605         {
606             String className = clazzToCompare[i];
607             assertEquals( new File( expectedDir, className ), new File( generatedDir, className ) );
608         }
609     }
610 
611     /**
612      * Invoke the compilation on the given pom file.
613      *
614      * @param testPom not null
615      * @param log not null
616      */
617     private void invokeCompileGoal( File testPom, Log log )
618     {
619         List goals = new ArrayList();
620         goals.add( "clean" );
621         goals.add( "compile" );
622 
623         File invokerLogFile = new File( getBasedir(), "target/invoker-FixJavadocMojoTest.txt" );
624         JavadocUtil.invokeMaven( log, new File( getBasedir(), "target/local-repo" ), testPom, goals, null,
625                                  invokerLogFile );
626     }
627 
628     // ----------------------------------------------------------------------
629     // static methods
630     // ----------------------------------------------------------------------
631 
632     /**
633      * Asserts that files are equal. If they are not an AssertionFailedError is thrown.
634      *
635      * @throws IOException if any
636      */
637     private static void assertEquals( File expected, File actual )
638         throws IOException
639     {
640         assertTrue( expected.exists() );
641         String expectedContent = StringUtils.unifyLineSeparators( readFile( expected ) );
642 
643         assertTrue( actual.exists() );
644         String actualContent = StringUtils.unifyLineSeparators( readFile( actual ) );
645 
646         assertEquals( "Expected file: " + expected.getAbsolutePath() + ", actual file: "
647             + actual.getAbsolutePath(), expectedContent, actualContent );
648     }
649 
650     /**
651      * @param testProjectDirName not null
652      * @throws IOException if any
653      */
654     private static void prepareTestProjects( String testProjectDirName )
655         throws IOException
656     {
657         File testPomBasedir = new File( getBasedir(), "target/test/unit/" + testProjectDirName );
658 
659         // Using unit test dir
660         FileUtils
661                  .copyDirectoryStructure(
662                                           new File( getBasedir(), "src/test/resources/unit/" + testProjectDirName ),
663                                           testPomBasedir );
664         List scmFiles = FileUtils.getDirectoryNames( testPomBasedir, "**/.svn", null, true );
665         for ( Iterator it = scmFiles.iterator(); it.hasNext(); )
666         {
667             File dir = new File( it.next().toString() );
668 
669             if ( dir.isDirectory() )
670             {
671                 FileUtils.deleteDirectory( dir );
672             }
673         }
674     }
675 
676     /**
677      * @param file not null
678      * @return the content of the given file
679      * @throws IOException if any
680      */
681     private static String readFile( File file )
682         throws IOException
683     {
684         Reader fileReader = null;
685         try
686         {
687             fileReader = ReaderFactory.newReader( file, "UTF-8" );
688             return IOUtil.toString( fileReader );
689         }
690         finally
691         {
692             IOUtil.close( fileReader );
693         }
694     }
695 }