1 package test.net.sourceforge.pmd.ast;
2
3 import net.sourceforge.pmd.PMD;
4 import net.sourceforge.pmd.TargetJDK1_3;
5 import net.sourceforge.pmd.TargetJDK1_4;
6 import net.sourceforge.pmd.TargetJDK1_5;
7 import net.sourceforge.pmd.TargetJDKVersion;
8 import net.sourceforge.pmd.ast.JavaParser;
9 import net.sourceforge.pmd.ast.ParseException;
10
11 import org.junit.Test;
12
13 import java.io.StringReader;
14
15 public class JDKVersionTest {
16
17
18 @Test(expected = ParseException.class)
19 public void testEnumAsKeywordShouldFailWith14() throws Throwable {
20 JavaParser p = new TargetJDK1_4().createParser(new StringReader(JDK15_ENUM));
21 p.CompilationUnit();
22 }
23
24 @Test
25 public void testEnumAsIdentifierShouldPassWith14() throws Throwable {
26 JavaParser p = new TargetJDK1_4().createParser(new StringReader(JDK14_ENUM));
27 p.CompilationUnit();
28 }
29
30 @Test
31 public void testEnumAsKeywordShouldPassWith15() throws Throwable {
32 JavaParser p = new TargetJDK1_5().createParser(new StringReader(JDK15_ENUM));
33 p.CompilationUnit();
34 }
35
36 @Test(expected = ParseException.class)
37 public void testEnumAsIdentifierShouldFailWith15() throws Throwable {
38 TargetJDKVersion jdk = new TargetJDK1_5();
39 JavaParser p = jdk.createParser(new StringReader(JDK14_ENUM));
40 p.CompilationUnit();
41 }
42
43
44
45 @Test
46 public void testAssertAsKeywordVariantsSucceedWith1_4() {
47 (new TargetJDK1_4()).createParser(new StringReader(ASSERT_TEST1)).CompilationUnit();
48 (new TargetJDK1_4()).createParser(new StringReader(ASSERT_TEST2)).CompilationUnit();
49 (new TargetJDK1_4()).createParser(new StringReader(ASSERT_TEST3)).CompilationUnit();
50 (new TargetJDK1_4()).createParser(new StringReader(ASSERT_TEST4)).CompilationUnit();
51 }
52
53 @Test(expected = ParseException.class)
54 public void testAssertAsVariableDeclIdentifierFailsWith1_4() {
55 (new TargetJDK1_4()).createParser(new StringReader(ASSERT_TEST5)).CompilationUnit();
56 }
57
58 @Test(expected = ParseException.class)
59 public void testAssertAsMethodNameIdentifierFailsWith1_4() {
60 (new TargetJDK1_4()).createParser(new StringReader(ASSERT_TEST7)).CompilationUnit();
61 }
62
63 @Test
64 public void testAssertAsIdentifierSucceedsWith1_3() {
65 JavaParser jp = (new TargetJDK1_3()).createParser(new StringReader(ASSERT_TEST5));
66 jp.CompilationUnit();
67 }
68
69 @Test(expected = ParseException.class)
70 public void testAssertAsKeywordFailsWith1_3() {
71 JavaParser jp = (new TargetJDK1_3()).createParser(new StringReader(ASSERT_TEST6));
72 jp.CompilationUnit();
73 }
74
75
76 @Test
77 public void testVarargsShouldPassWith15() throws Throwable {
78 JavaParser p = new TargetJDK1_5().createParser(new StringReader(JDK15_VARARGS));
79 p.CompilationUnit();
80 }
81
82 @Test(expected = ParseException.class)
83 public void testVarargsShouldFailWith14() throws Throwable {
84 JavaParser p = new TargetJDK1_4().createParser(new StringReader(JDK15_VARARGS));
85 p.CompilationUnit();
86 }
87
88 @Test
89 public void testJDK15ForLoopSyntaxShouldPassWith15() throws Throwable {
90 JavaParser p = new TargetJDK1_5().createParser(new StringReader(JDK15_FORLOOP));
91 p.CompilationUnit();
92 }
93
94 @Test
95 public void testJDK15ForLoopSyntaxWithModifiers() throws Throwable {
96 JavaParser p = new TargetJDK1_5().createParser(new StringReader(JDK15_FORLOOP_WITH_MODIFIER));
97 p.CompilationUnit();
98 }
99
100 @Test(expected = ParseException.class)
101 public void testJDK15ForLoopShouldFailWith14() throws Throwable {
102 JavaParser p = new TargetJDK1_4().createParser(new StringReader(JDK15_FORLOOP));
103 p.CompilationUnit();
104 }
105
106 @Test
107 public void testJDK15GenericsSyntaxShouldPassWith15() throws Throwable {
108 JavaParser p = new TargetJDK1_5().createParser(new StringReader(JDK15_GENERICS));
109 p.CompilationUnit();
110 }
111
112 @Test
113 public void testVariousParserBugs() throws Throwable {
114 JavaParser p = new TargetJDK1_5().createParser(new StringReader(FIELDS_BUG));
115 p.CompilationUnit();
116 p = new TargetJDK1_5().createParser(new StringReader(GT_BUG));
117 p.CompilationUnit();
118 p = new TargetJDK1_5().createParser(new StringReader(ANNOTATIONS_BUG));
119 p.CompilationUnit();
120 p = new TargetJDK1_5().createParser(new StringReader(CONSTANT_FIELD_IN_ANNOTATION_BUG));
121 p.CompilationUnit();
122 p = new TargetJDK1_5().createParser(new StringReader(GENERIC_IN_FIELD));
123 p.CompilationUnit();
124 }
125
126 @Test
127 public void testNestedClassInMethodBug() throws Throwable {
128 JavaParser p = new TargetJDK1_5().createParser(new StringReader(INNER_BUG));
129 p.CompilationUnit();
130 p = new TargetJDK1_5().createParser(new StringReader(INNER_BUG2));
131 p.CompilationUnit();
132 }
133
134 @Test
135 public void testGenericsInMethodCall() throws Throwable {
136 JavaParser p = new TargetJDK1_5().createParser(new StringReader(GENERIC_IN_METHOD_CALL));
137 p.CompilationUnit();
138 }
139
140 @Test
141 public void testGenericINAnnotation() throws Throwable {
142 JavaParser p = new TargetJDK1_5().createParser(new StringReader(GENERIC_IN_ANNOTATION));
143 p.CompilationUnit();
144 }
145
146 @Test
147 public void testGenericReturnType() throws Throwable {
148 JavaParser p = new TargetJDK1_5().createParser(new StringReader(GENERIC_RETURN_TYPE));
149 p.CompilationUnit();
150 }
151
152 @Test
153 public void testMultipleGenerics() throws Throwable {
154 JavaParser p = new TargetJDK1_5().createParser(new StringReader(FUNKY_GENERICS));
155 p.CompilationUnit();
156 p = new TargetJDK1_5().createParser(new StringReader(MULTIPLE_GENERICS));
157 p.CompilationUnit();
158 }
159
160 @Test
161 public void testAnnotatedParams() throws Throwable {
162 JavaParser p = new TargetJDK1_5().createParser(new StringReader(ANNOTATED_PARAMS));
163 p.CompilationUnit();
164 }
165
166 @Test
167 public void testAnnotatedLocals() throws Throwable {
168 JavaParser p = new TargetJDK1_5().createParser(new StringReader(ANNOTATED_LOCALS));
169 p.CompilationUnit();
170 }
171
172 @Test
173 public void testAssertAsIdentifierSucceedsWith1_3_test2() {
174 JavaParser jp = (new TargetJDK1_3()).createParser(new StringReader(ASSERT_TEST5_a));
175 jp.CompilationUnit();
176 }
177
178
179 private static final String ANNOTATED_LOCALS =
180 "public class Foo {" + PMD.EOL +
181 " void bar() {" + PMD.EOL +
182 " @SuppressWarnings(\"foo\") int y = 5;" + PMD.EOL +
183 " }" + PMD.EOL +
184 "}";
185
186 private static final String ANNOTATED_PARAMS =
187 "public class Foo {" + PMD.EOL +
188 " void bar(@SuppressWarnings(\"foo\") int x) {}" + PMD.EOL +
189 "}";
190
191 private static final String ASSERT_TEST1 =
192 "public class Foo {" + PMD.EOL +
193 " void bar() {" + PMD.EOL +
194 " assert x == 2;" + PMD.EOL +
195 " }" + PMD.EOL +
196 "}";
197
198 private static final String ASSERT_TEST2 =
199 "public class Foo {" + PMD.EOL +
200 " void bar() {" + PMD.EOL +
201 " assert (x == 2);" + PMD.EOL +
202 " }" + PMD.EOL +
203 "}";
204
205 private static final String ASSERT_TEST3 =
206 "public class Foo {" + PMD.EOL +
207 " void bar() {" + PMD.EOL +
208 " assert (x==2) : \"hi!\";" + PMD.EOL +
209 " }" + PMD.EOL +
210 "}";
211
212 private static final String ASSERT_TEST4 =
213 "public class Foo {" + PMD.EOL +
214 " void bar() {" + PMD.EOL +
215 " assert (x==2) : \"hi!\";" + PMD.EOL +
216 " }" + PMD.EOL +
217 "}";
218
219 private static final String ASSERT_TEST5 =
220 "public class Foo {" + PMD.EOL +
221 " int assert = 2;" + PMD.EOL +
222 "}";
223
224
225 private static final String ASSERT_TEST5_a =
226 "public class Foo {" + PMD.EOL +
227 " void bar() { assert(); }" + PMD.EOL +
228 "}";
229
230 private static final String ASSERT_TEST6 =
231 "public class Foo {" + PMD.EOL +
232 " void foo() {" + PMD.EOL +
233 " assert (x == 2) : \"hi!\";" + PMD.EOL +
234 " }" + PMD.EOL +
235 "}";
236
237 private static final String ASSERT_TEST7 =
238 "public class Foo {" + PMD.EOL +
239 " void assert() {}" + PMD.EOL +
240 "}";
241
242 private static final String JDK15_ENUM =
243 "public class Test {" + PMD.EOL +
244 " enum Season { winter, spring, summer, fall };" + PMD.EOL +
245 "}";
246
247 private static final String JDK14_ENUM =
248 "public class Test {" + PMD.EOL +
249 " int enum;" + PMD.EOL +
250 "}";
251
252 private static final String JDK15_VARARGS =
253 "public class Test {" + PMD.EOL +
254 " void bar(Object ... args) {}" + PMD.EOL +
255 "}";
256
257 private static final String JDK15_FORLOOP =
258 "public class Test {" + PMD.EOL +
259 " void foo(List list) {" + PMD.EOL +
260 " for (Integer i : list) {}" + PMD.EOL +
261 " }" + PMD.EOL +
262 "}";
263
264 private static final String JDK15_FORLOOP_WITH_MODIFIER =
265 "public class Test {" + PMD.EOL +
266 " void foo(List list) {" + PMD.EOL +
267 " for (final Integer i : list) {}" + PMD.EOL +
268 " }" + PMD.EOL +
269 "}";
270
271 private static final String JDK15_GENERICS =
272 "public class Test {" + PMD.EOL +
273 " ArrayList<Integer> list = new ArrayList<Integer>();" + PMD.EOL +
274 "}";
275
276 private static final String FIELDS_BUG =
277 "public class Test {" + PMD.EOL +
278 " private Foo bar;" + PMD.EOL +
279 "}";
280
281 private static final String GT_BUG =
282 "public class Test {" + PMD.EOL +
283 " int y = x > 32;" + PMD.EOL +
284 "}";
285
286 private static final String ANNOTATIONS_BUG =
287 "@Target(ElementType.METHOD)" + PMD.EOL +
288 "public @interface Foo {" + PMD.EOL +
289 "}";
290
291 private static final String CONSTANT_FIELD_IN_ANNOTATION_BUG =
292 "public @interface Foo {" + PMD.EOL +
293 " String CONST = \"foo\";" + PMD.EOL +
294 "}";
295
296 private static final String GENERIC_IN_FIELD =
297 "public class Foo {" + PMD.EOL +
298 " Class<Double> foo = (Class<Double>)clazz;" + PMD.EOL +
299 "}";
300
301 private static final String GENERIC_IN_ANNOTATION =
302 "public class Foo {" + PMD.EOL +
303 " public <A extends Annotation> A foo(Class<A> c) {" + PMD.EOL +
304 " return null;" + PMD.EOL +
305 " }" + PMD.EOL +
306 "}";
307
308 private static final String INNER_BUG =
309 "public class Test {" + PMD.EOL +
310 " void bar() {" + PMD.EOL +
311 " final class Inner {};" + PMD.EOL +
312 " Inner i = new Inner();" + PMD.EOL +
313 " }" + PMD.EOL +
314 "}";
315
316 private static final String INNER_BUG2 =
317 "public class Test {" + PMD.EOL +
318 " void bar() {" + PMD.EOL +
319 " class Inner {};" + PMD.EOL +
320 " Inner i = new Inner();" + PMD.EOL +
321 " }" + PMD.EOL +
322 "}";
323
324 private static final String GENERIC_IN_METHOD_CALL =
325 "public class Test {" + PMD.EOL +
326 " List<String> test() {" + PMD.EOL +
327 " return Collections.<String>emptyList();" + PMD.EOL +
328 " }" + PMD.EOL +
329 "}";
330
331 private static final String GENERIC_RETURN_TYPE =
332 "public class Test {" + PMD.EOL +
333 " public static <String> String test(String x) {" + PMD.EOL +
334 " return x;" + PMD.EOL +
335 " }" + PMD.EOL +
336 "}";
337
338
339 private static final String MULTIPLE_GENERICS =
340 "public class Foo<K,V> {" + PMD.EOL +
341 " public <A extends K, B extends V> Foo(Bar<A,B> t) {}" + PMD.EOL +
342 "}";
343
344
345 private static final String FUNKY_GENERICS =
346 "public class Foo {" + PMD.EOL +
347 " public <T extends E> Foo() {}" + PMD.EOL +
348 "}";
349
350 public static junit.framework.Test suite() {
351 return new junit.framework.JUnit4TestAdapter(JDKVersionTest.class);
352 }
353 }