View Javadoc
1   package org.apache.maven.plugins.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.StringReader;
23  
24  import com.thoughtworks.qdox.JavaProjectBuilder;
25  import com.thoughtworks.qdox.model.JavaClass;
26  import com.thoughtworks.qdox.model.JavaSource;
27  
28  import junit.framework.TestCase;
29  
30  public class AbstractFixJavadocMojoTest
31      extends TestCase
32  {
33      private JavaSource getJavaSource( String source )
34      {
35          return new JavaProjectBuilder().addSource( new StringReader( source ) );
36      }
37  
38      public void testReplaceLinkTags_noLinkTag()
39          throws Throwable
40      {
41          String comment = "/** @see ConnectException */";
42          String source = "import java.net.ConnectException;\n"
43                          + comment + "\n"
44                          + "public class NoLinkTag {}";
45                      
46          JavaClass clazz = getJavaSource( source ).getClassByName( "NoLinkTag" );
47  
48          String newComment = AbstractFixJavadocMojo.replaceLinkTags( comment, clazz );
49              
50          assertEquals( "/** @see ConnectException */", newComment );
51      }
52  
53      public void testReplaceLinkTags_oneLinkTag()
54          throws Throwable
55      {
56          String comment = "/** {@link ConnectException} */";
57          String source = "import java.net.ConnectException;\n"
58                          + comment + "\n"
59                          + "public class OneLinkTag {}";
60          
61          JavaClass clazz = getJavaSource( source ).getClassByName( "OneLinkTag" );
62  
63          String newComment = AbstractFixJavadocMojo.replaceLinkTags( comment, clazz );
64          assertEquals( "/** {@link java.net.ConnectException} */", newComment );
65      }
66  
67      public void testReplaceLinkTags_missingEndBrace()
68          throws Throwable
69      {
70          String comment = "/** {@link ConnectException */";
71          String source = "import java.net.ConnectException;\n"
72                          + comment + "\n"
73                          + "public class MissingEndBrace {}";
74                      
75          JavaClass clazz = getJavaSource( source ).getClassByName( "MissingEndBrace" );
76                      
77          String newComment = AbstractFixJavadocMojo.replaceLinkTags( comment, clazz );
78          assertEquals( "/** {@link ConnectException */", newComment );
79      }
80  
81      public void testReplaceLinkTags_spacesAfterLinkTag()
82          throws Throwable
83      {
84          String comment = "/** {@link     ConnectException} */";
85          String source = "import java.net.ConnectException;\n"
86                          + comment + "\n"
87                          + "public class SpacesAfterLinkTag {}";
88          
89          JavaClass clazz = getJavaSource( source ).getClassByName( "SpacesAfterLinkTag" );
90          
91          String newComment = AbstractFixJavadocMojo.replaceLinkTags( comment, clazz );
92          assertEquals( "/** {@link java.net.ConnectException} */", newComment );
93      }
94  
95      public void testReplaceLinkTags_spacesAfterClassName()
96          throws Throwable
97      {
98          String comment = "/** {@link ConnectException       } */";
99          String source = "import java.net.ConnectException;\n"
100                         + comment + "\n"
101                         + "public class SpacesAfterClassName {}";
102         
103         JavaClass clazz = getJavaSource( source ).getClassByName( "SpacesAfterClassName" );
104         
105         String newComment = AbstractFixJavadocMojo.replaceLinkTags( comment, clazz );
106         assertEquals( "/** {@link java.net.ConnectException} */", newComment );
107     }
108 
109     public void testReplaceLinkTags_spacesAfterMethod()
110         throws Throwable
111     {
112         String comment = "/** {@link ConnectException#getMessage()       } */";
113         String source = "import java.net.ConnectException;\n"
114                         + comment + "\n"
115                         + "public class SpacesAfterMethod {}";
116         
117         JavaClass clazz = getJavaSource( source ).getClassByName( "SpacesAfterMethod" );
118 
119         String newComment = AbstractFixJavadocMojo.replaceLinkTags( comment, clazz );
120         assertEquals( "/** {@link java.net.ConnectException#getMessage()} */", newComment );
121     }
122 
123     public void testReplaceLinkTags_containingHash()
124         throws Throwable
125     {
126         String comment = "/** {@link ConnectException#getMessage()} */";
127         String source = "import java.net.ConnectException;\n"
128                         + comment + "\n"
129                         + "public class ContainingHashes {}";
130         
131         JavaClass clazz = getJavaSource( source ).getClassByName( "ContainingHashes" );
132 
133         String newComment = AbstractFixJavadocMojo.replaceLinkTags( comment, clazz );
134         assertEquals( "/** {@link java.net.ConnectException#getMessage()} */", newComment );
135     }
136 
137     public void testReplaceLinkTags_followedByHash()
138         throws Throwable
139     {
140         String comment = "/** {@link ConnectException} ##important## */";
141         String source = "import java.net.ConnectException;\n"
142                         + comment + "\n"
143                         + "public class FollowedByHash {}";
144         
145         JavaClass clazz = getJavaSource( source ).getClassByName( "FollowedByHash" );
146 
147         String newComment = AbstractFixJavadocMojo.replaceLinkTags( comment, clazz );
148         assertEquals( "/** {@link java.net.ConnectException} ##important## */", newComment );
149     }
150 
151     public void testReplaceLinkTags_twoLinks()
152         throws Throwable
153     {
154         String comment = "/** Use {@link ConnectException} instead of {@link Exception} */";
155         String source = "import java.net.ConnectException;\n"
156                         + comment + "\n"
157                         + "public class TwoLinks {}";
158         
159         JavaClass clazz = getJavaSource( source ).getClassByName( "TwoLinks" );
160 
161         String newComment = AbstractFixJavadocMojo.replaceLinkTags( comment, clazz );
162         assertEquals( "/** Use {@link java.net.ConnectException} instead of {@link java.lang.Exception} */", newComment );
163     }
164 
165     public void testReplaceLinkTags_OnlyAnchor()
166         throws Throwable
167     {
168         String comment = "/** There's a {@link #getClass()} but no setClass() */";
169         String source = "import java.net.ConnectException;\n"
170                         + comment + "\n"
171                         + "public class OnlyAnchor {}";
172         
173         JavaClass clazz = getJavaSource( source ).getClassByName( "OnlyAnchor" );
174 
175         String newComment = AbstractFixJavadocMojo.replaceLinkTags( comment, clazz );
176         assertEquals( "/** There's a {@link #getClass()} but no setClass() */", newComment );
177     }
178 }