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