Source code: com/puppycrawl/tools/checkstyle/checks/JavadocMethodCheckTest.java
1 package com.puppycrawl.tools.checkstyle.checks;
2
3 import com.puppycrawl.tools.checkstyle.BaseCheckTestCase;
4 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
5 import com.puppycrawl.tools.checkstyle.api.Scope;
6
7
8 public class JavadocMethodCheckTest
9 extends BaseCheckTestCase
10 {
11 public void testTags()
12 throws Exception
13 {
14 final DefaultConfiguration checkConfig =
15 createCheckConfig(JavadocMethodCheck.class);
16 final String[] expected = {
17 "14:5: Missing a Javadoc comment.",
18 "18: Unused @param tag for 'unused'.",
19 "24: Expected an @return tag.",
20 "33: Expected an @return tag.",
21 "40:16: Expected @throws tag for 'Exception'.",
22 "49:16: Expected @throws tag for 'Exception'.",
23 "53: Unused @throws tag for 'WrongException'.",
24 "55:16: Expected @throws tag for 'Exception'.",
25 "55:27: Expected @throws tag for 'NullPointerException'.",
26 "60:22: Expected @param tag for 'aOne'.",
27 "68:22: Expected @param tag for 'aOne'.",
28 "72: Unused @param tag for 'WrongParam'.",
29 "73:23: Expected @param tag for 'aOne'.",
30 "73:33: Expected @param tag for 'aTwo'.",
31 "78: Unused @param tag for 'Unneeded'.",
32 "79: Unused Javadoc tag.",
33 "87: Duplicate @return tag.",
34 "109:23: Expected @param tag for 'aOne'.",
35 "109:55: Expected @param tag for 'aFour'.",
36 "109:66: Expected @param tag for 'aFive'.",
37 "178: Unused @throws tag for 'ThreadDeath'.",
38 "179: Unused @throws tag for 'ArrayStoreException'.",
39 };
40
41 verify(checkConfig, getPath("InputTags.java"), expected);
42 }
43
44 public void testTagsWithResolver()
45 throws Exception
46 {
47 final DefaultConfiguration checkConfig =
48 createCheckConfig(JavadocMethodCheck.class);
49 checkConfig.addAttribute("allowUndeclaredRTE", "true");
50 final String[] expected = {
51 "14:5: Missing a Javadoc comment.",
52 "18: Unused @param tag for 'unused'.",
53 "24: Expected an @return tag.",
54 "33: Expected an @return tag.",
55 "40:16: Expected @throws tag for 'Exception'.",
56 "49:16: Expected @throws tag for 'Exception'.",
57 "53: Unable to get class information for @throws tag 'WrongException'.",
58 "53: Unused @throws tag for 'WrongException'.",
59 "55:16: Expected @throws tag for 'Exception'.",
60 "55:27: Expected @throws tag for 'NullPointerException'.",
61 "60:22: Expected @param tag for 'aOne'.",
62 "68:22: Expected @param tag for 'aOne'.",
63 "72: Unused @param tag for 'WrongParam'.",
64 "73:23: Expected @param tag for 'aOne'.",
65 "73:33: Expected @param tag for 'aTwo'.",
66 "78: Unused @param tag for 'Unneeded'.",
67 "79: Unused Javadoc tag.",
68 "87: Duplicate @return tag.",
69 "109:23: Expected @param tag for 'aOne'.",
70 "109:55: Expected @param tag for 'aFour'.",
71 "109:66: Expected @param tag for 'aFive'.",
72 };
73 verify(checkConfig, getPath("InputTags.java"), expected);
74 }
75
76 public void testStrictJavadoc()
77 throws Exception
78 {
79 final DefaultConfiguration checkConfig =
80 createCheckConfig(JavadocMethodCheck.class);
81 final String[] expected = {
82 "12:9: Missing a Javadoc comment.",
83 "18:13: Missing a Javadoc comment.",
84 "25:13: Missing a Javadoc comment.",
85 "38:9: Missing a Javadoc comment.",
86 "49:5: Missing a Javadoc comment.",
87 "54:5: Missing a Javadoc comment.",
88 "59:5: Missing a Javadoc comment.",
89 "64:5: Missing a Javadoc comment.",
90 "69:5: Missing a Javadoc comment.",
91 "74:5: Missing a Javadoc comment.",
92 "79:5: Missing a Javadoc comment.",
93 "84:5: Missing a Javadoc comment.",
94 "94:32: Expected @param tag for 'aA'."
95 };
96 verify(checkConfig, getPath("InputPublicOnly.java"), expected);
97 }
98
99 public void testNoJavadoc()
100 throws Exception
101 {
102 final DefaultConfiguration checkConfig =
103 createCheckConfig(JavadocMethodCheck.class);
104 checkConfig.addAttribute("scope", Scope.NOTHING.getName());
105 final String[] expected = {
106 };
107 verify(checkConfig, getPath("InputPublicOnly.java"), expected);
108 }
109
110 // pre 1.4 relaxed mode is roughly equivalent with check=protected
111 public void testRelaxedJavadoc()
112 throws Exception
113 {
114
115 final DefaultConfiguration checkConfig =
116 createCheckConfig(JavadocMethodCheck.class);
117 checkConfig.addAttribute("scope", Scope.PROTECTED.getName());
118 final String[] expected = {
119 "59:5: Missing a Javadoc comment.",
120 "64:5: Missing a Javadoc comment.",
121 "79:5: Missing a Javadoc comment.",
122 "84:5: Missing a Javadoc comment."
123 };
124 verify(checkConfig, getPath("InputPublicOnly.java"), expected);
125 }
126
127
128 public void testScopeInnerInterfacesPublic()
129 throws Exception
130 {
131 final DefaultConfiguration checkConfig =
132 createCheckConfig(JavadocMethodCheck.class);
133 checkConfig.addAttribute("scope", Scope.PUBLIC.getName());
134 final String[] expected = {
135 "43:9: Missing a Javadoc comment.",
136 "44:9: Missing a Javadoc comment."
137 };
138 verify(checkConfig, getPath("InputScopeInnerInterfaces.java"), expected);
139 }
140
141 public void testScopeAnonInnerPrivate()
142 throws Exception
143 {
144 final DefaultConfiguration checkConfig =
145 createCheckConfig(JavadocMethodCheck.class);
146 checkConfig.addAttribute("scope", Scope.PRIVATE.getName());
147 final String[] expected = {
148 };
149 verify(checkConfig, getPath("InputScopeAnonInner.java"), expected);
150 }
151
152 public void testScopeAnonInnerAnonInner()
153 throws Exception
154 {
155 final DefaultConfiguration checkConfig =
156 createCheckConfig(JavadocMethodCheck.class);
157 checkConfig.addAttribute("scope", Scope.ANONINNER.getName());
158 final String[] expected = {
159 "26:9: Missing a Javadoc comment.",
160 "39:17: Missing a Javadoc comment.",
161 "53:17: Missing a Javadoc comment.",
162 };
163 verify(checkConfig, getPath("InputScopeAnonInner.java"), expected);
164 }
165
166 public void testScopeAnonInnerWithResolver()
167 throws Exception
168 {
169 final DefaultConfiguration checkConfig =
170 createCheckConfig(JavadocMethodCheck.class);
171 checkConfig.addAttribute("allowUndeclaredRTE", "true");
172 final String[] expected = {
173 };
174 verify(checkConfig, getPath("InputScopeAnonInner.java"), expected);
175 }
176 }