1 package org.apache.maven.plugins.javadoc;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 }