001: package test.net.sourceforge.pmd.ast;
002:
003: import net.sourceforge.pmd.PMD;
004: import net.sourceforge.pmd.TargetJDK1_3;
005: import net.sourceforge.pmd.TargetJDK1_4;
006: import net.sourceforge.pmd.TargetJDK1_5;
007: import net.sourceforge.pmd.TargetJDKVersion;
008: import net.sourceforge.pmd.ast.JavaParser;
009: import net.sourceforge.pmd.ast.ParseException;
010:
011: import org.junit.Test;
012:
013: import java.io.StringReader;
014:
015: public class JDKVersionTest {
016:
017: // enum keyword/identifier
018: @Test(expected=ParseException.class)
019: public void testEnumAsKeywordShouldFailWith14() throws Throwable {
020: JavaParser p = new TargetJDK1_4()
021: .createParser(new StringReader(JDK15_ENUM));
022: p.CompilationUnit();
023: }
024:
025: @Test
026: public void testEnumAsIdentifierShouldPassWith14() throws Throwable {
027: JavaParser p = new TargetJDK1_4()
028: .createParser(new StringReader(JDK14_ENUM));
029: p.CompilationUnit();
030: }
031:
032: @Test
033: public void testEnumAsKeywordShouldPassWith15() throws Throwable {
034: JavaParser p = new TargetJDK1_5()
035: .createParser(new StringReader(JDK15_ENUM));
036: p.CompilationUnit();
037: }
038:
039: @Test(expected=ParseException.class)
040: public void testEnumAsIdentifierShouldFailWith15() throws Throwable {
041: TargetJDKVersion jdk = new TargetJDK1_5();
042: JavaParser p = jdk.createParser(new StringReader(JDK14_ENUM));
043: p.CompilationUnit();
044: }
045:
046: // enum keyword/identifier
047:
048: // assert keyword/identifier
049: @Test
050: public void testAssertAsKeywordVariantsSucceedWith1_4() {
051: (new TargetJDK1_4()).createParser(
052: new StringReader(ASSERT_TEST1)).CompilationUnit();
053: (new TargetJDK1_4()).createParser(
054: new StringReader(ASSERT_TEST2)).CompilationUnit();
055: (new TargetJDK1_4()).createParser(
056: new StringReader(ASSERT_TEST3)).CompilationUnit();
057: (new TargetJDK1_4()).createParser(
058: new StringReader(ASSERT_TEST4)).CompilationUnit();
059: }
060:
061: @Test(expected=ParseException.class)
062: public void testAssertAsVariableDeclIdentifierFailsWith1_4() {
063: (new TargetJDK1_4()).createParser(
064: new StringReader(ASSERT_TEST5)).CompilationUnit();
065: }
066:
067: @Test(expected=ParseException.class)
068: public void testAssertAsMethodNameIdentifierFailsWith1_4() {
069: (new TargetJDK1_4()).createParser(
070: new StringReader(ASSERT_TEST7)).CompilationUnit();
071: }
072:
073: @Test
074: public void testAssertAsIdentifierSucceedsWith1_3() {
075: JavaParser jp = (new TargetJDK1_3())
076: .createParser(new StringReader(ASSERT_TEST5));
077: jp.CompilationUnit();
078: }
079:
080: @Test(expected=ParseException.class)
081: public void testAssertAsKeywordFailsWith1_3() {
082: JavaParser jp = (new TargetJDK1_3())
083: .createParser(new StringReader(ASSERT_TEST6));
084: jp.CompilationUnit();
085: }
086:
087: // assert keyword/identifier
088:
089: @Test
090: public void testVarargsShouldPassWith15() throws Throwable {
091: JavaParser p = new TargetJDK1_5()
092: .createParser(new StringReader(JDK15_VARARGS));
093: p.CompilationUnit();
094: }
095:
096: @Test(expected=ParseException.class)
097: public void testVarargsShouldFailWith14() throws Throwable {
098: JavaParser p = new TargetJDK1_4()
099: .createParser(new StringReader(JDK15_VARARGS));
100: p.CompilationUnit();
101: }
102:
103: @Test
104: public void testJDK15ForLoopSyntaxShouldPassWith15()
105: throws Throwable {
106: JavaParser p = new TargetJDK1_5()
107: .createParser(new StringReader(JDK15_FORLOOP));
108: p.CompilationUnit();
109: }
110:
111: @Test
112: public void testJDK15ForLoopSyntaxWithModifiers() throws Throwable {
113: JavaParser p = new TargetJDK1_5()
114: .createParser(new StringReader(
115: JDK15_FORLOOP_WITH_MODIFIER));
116: p.CompilationUnit();
117: }
118:
119: @Test(expected=ParseException.class)
120: public void testJDK15ForLoopShouldFailWith14() throws Throwable {
121: JavaParser p = new TargetJDK1_4()
122: .createParser(new StringReader(JDK15_FORLOOP));
123: p.CompilationUnit();
124: }
125:
126: @Test
127: public void testJDK15GenericsSyntaxShouldPassWith15()
128: throws Throwable {
129: JavaParser p = new TargetJDK1_5()
130: .createParser(new StringReader(JDK15_GENERICS));
131: p.CompilationUnit();
132: }
133:
134: @Test
135: public void testVariousParserBugs() throws Throwable {
136: JavaParser p = new TargetJDK1_5()
137: .createParser(new StringReader(FIELDS_BUG));
138: p.CompilationUnit();
139: p = new TargetJDK1_5().createParser(new StringReader(GT_BUG));
140: p.CompilationUnit();
141: p = new TargetJDK1_5().createParser(new StringReader(
142: ANNOTATIONS_BUG));
143: p.CompilationUnit();
144: p = new TargetJDK1_5().createParser(new StringReader(
145: GENERIC_IN_FIELD));
146: p.CompilationUnit();
147: }
148:
149: @Test
150: public void testNestedClassInMethodBug() throws Throwable {
151: JavaParser p = new TargetJDK1_5()
152: .createParser(new StringReader(INNER_BUG));
153: p.CompilationUnit();
154: p = new TargetJDK1_5()
155: .createParser(new StringReader(INNER_BUG2));
156: p.CompilationUnit();
157: }
158:
159: @Test
160: public void testGenericsInMethodCall() throws Throwable {
161: JavaParser p = new TargetJDK1_5()
162: .createParser(new StringReader(GENERIC_IN_METHOD_CALL));
163: p.CompilationUnit();
164: }
165:
166: @Test
167: public void testGenericINAnnotation() throws Throwable {
168: JavaParser p = new TargetJDK1_5()
169: .createParser(new StringReader(GENERIC_IN_ANNOTATION));
170: p.CompilationUnit();
171: }
172:
173: @Test
174: public void testGenericReturnType() throws Throwable {
175: JavaParser p = new TargetJDK1_5()
176: .createParser(new StringReader(GENERIC_RETURN_TYPE));
177: p.CompilationUnit();
178: }
179:
180: @Test
181: public void testMultipleGenerics() throws Throwable {
182: JavaParser p = new TargetJDK1_5()
183: .createParser(new StringReader(FUNKY_GENERICS));
184: p.CompilationUnit();
185: p = new TargetJDK1_5().createParser(new StringReader(
186: MULTIPLE_GENERICS));
187: p.CompilationUnit();
188: }
189:
190: @Test
191: public void testAnnotatedParams() throws Throwable {
192: JavaParser p = new TargetJDK1_5()
193: .createParser(new StringReader(ANNOTATED_PARAMS));
194: p.CompilationUnit();
195: }
196:
197: @Test
198: public void testAnnotatedLocals() throws Throwable {
199: JavaParser p = new TargetJDK1_5()
200: .createParser(new StringReader(ANNOTATED_LOCALS));
201: p.CompilationUnit();
202: }
203:
204: @Test
205: public void testAssertAsIdentifierSucceedsWith1_3_test2() {
206: JavaParser jp = (new TargetJDK1_3())
207: .createParser(new StringReader(ASSERT_TEST5_a));
208: jp.CompilationUnit();
209: }
210:
211: private static final String ANNOTATED_LOCALS = "public class Foo {"
212: + PMD.EOL + " void bar() {" + PMD.EOL
213: + " @SuppressWarnings(\"foo\") int y = 5;" + PMD.EOL
214: + " }" + PMD.EOL + "}";
215:
216: private static final String ANNOTATED_PARAMS = "public class Foo {"
217: + PMD.EOL
218: + " void bar(@SuppressWarnings(\"foo\") int x) {}"
219: + PMD.EOL + "}";
220:
221: private static final String ASSERT_TEST1 = "public class Foo {"
222: + PMD.EOL + " void bar() {" + PMD.EOL + " assert x == 2;"
223: + PMD.EOL + " }" + PMD.EOL + "}";
224:
225: private static final String ASSERT_TEST2 = "public class Foo {"
226: + PMD.EOL + " void bar() {" + PMD.EOL
227: + " assert (x == 2);" + PMD.EOL + " }" + PMD.EOL + "}";
228:
229: private static final String ASSERT_TEST3 = "public class Foo {"
230: + PMD.EOL + " void bar() {" + PMD.EOL
231: + " assert (x==2) : \"hi!\";" + PMD.EOL + " }" + PMD.EOL
232: + "}";
233:
234: private static final String ASSERT_TEST4 = "public class Foo {"
235: + PMD.EOL + " void bar() {" + PMD.EOL
236: + " assert (x==2) : \"hi!\";" + PMD.EOL + " }" + PMD.EOL
237: + "}";
238:
239: private static final String ASSERT_TEST5 = "public class Foo {"
240: + PMD.EOL + " int assert = 2;" + PMD.EOL + "}";
241:
242: private static final String ASSERT_TEST5_a = "public class Foo {"
243: + PMD.EOL + " void bar() { assert(); }" + PMD.EOL + "}";
244:
245: private static final String ASSERT_TEST6 = "public class Foo {"
246: + PMD.EOL + " void foo() {" + PMD.EOL
247: + " assert (x == 2) : \"hi!\";" + PMD.EOL + " }" + PMD.EOL
248: + "}";
249:
250: private static final String ASSERT_TEST7 = "public class Foo {"
251: + PMD.EOL + " void assert() {}" + PMD.EOL + "}";
252:
253: private static final String JDK15_ENUM = "public class Test {"
254: + PMD.EOL
255: + " enum Season { winter, spring, summer, fall };"
256: + PMD.EOL + "}";
257:
258: private static final String JDK14_ENUM = "public class Test {"
259: + PMD.EOL + " int enum;" + PMD.EOL + "}";
260:
261: private static final String JDK15_VARARGS = "public class Test {"
262: + PMD.EOL + " void bar(Object ... args) {}" + PMD.EOL + "}";
263:
264: private static final String JDK15_FORLOOP = "public class Test {"
265: + PMD.EOL + " void foo(List list) {" + PMD.EOL
266: + " for (Integer i : list) {}" + PMD.EOL + " }" + PMD.EOL
267: + "}";
268:
269: private static final String JDK15_FORLOOP_WITH_MODIFIER = "public class Test {"
270: + PMD.EOL
271: + " void foo(List list) {"
272: + PMD.EOL
273: + " for (final Integer i : list) {}"
274: + PMD.EOL
275: + " }"
276: + PMD.EOL + "}";
277:
278: private static final String JDK15_GENERICS = "public class Test {"
279: + PMD.EOL
280: + " ArrayList<Integer> list = new ArrayList<Integer>();"
281: + PMD.EOL + "}";
282:
283: private static final String FIELDS_BUG = "public class Test {"
284: + PMD.EOL + " private Foo bar;" + PMD.EOL + "}";
285:
286: private static final String GT_BUG = "public class Test {"
287: + PMD.EOL + " int y = x > 32;" + PMD.EOL + "}";
288:
289: private static final String ANNOTATIONS_BUG = "@Target(ElementType.METHOD)"
290: + PMD.EOL + "public @interface Foo {" + PMD.EOL + "}";
291:
292: private static final String GENERIC_IN_FIELD = "public class Foo {"
293: + PMD.EOL + " Class<Double> foo = (Class<Double>)clazz;"
294: + PMD.EOL + "}";
295:
296: private static final String GENERIC_IN_ANNOTATION = "public class Foo {"
297: + PMD.EOL
298: + " public <A extends Annotation> A foo(Class<A> c) {"
299: + PMD.EOL
300: + " return null;"
301: + PMD.EOL
302: + " }"
303: + PMD.EOL
304: + "}";
305:
306: private static final String INNER_BUG = "public class Test {"
307: + PMD.EOL + " void bar() {" + PMD.EOL
308: + " final class Inner {};" + PMD.EOL
309: + " Inner i = new Inner();" + PMD.EOL + " }" + PMD.EOL
310: + "}";
311:
312: private static final String INNER_BUG2 = "public class Test {"
313: + PMD.EOL + " void bar() {" + PMD.EOL
314: + " class Inner {};" + PMD.EOL
315: + " Inner i = new Inner();" + PMD.EOL + " }" + PMD.EOL
316: + "}";
317:
318: private static final String GENERIC_IN_METHOD_CALL = "public class Test {"
319: + PMD.EOL
320: + " List<String> test() {"
321: + PMD.EOL
322: + " return Collections.<String>emptyList();"
323: + PMD.EOL
324: + " }" + PMD.EOL + "}";
325:
326: private static final String GENERIC_RETURN_TYPE = "public class Test {"
327: + PMD.EOL
328: + " public static <String> String test(String x) {"
329: + PMD.EOL
330: + " return x;"
331: + PMD.EOL
332: + " }"
333: + PMD.EOL
334: + "}";
335:
336: // See java/lang/concurrent/ConcurrentHashMap
337: private static final String MULTIPLE_GENERICS = "public class Foo<K,V> {"
338: + PMD.EOL
339: + " public <A extends K, B extends V> Foo(Bar<A,B> t) {}"
340: + PMD.EOL + "}";
341:
342: // See java/lang/concurrent/CopyOnWriteArraySet
343: private static final String FUNKY_GENERICS = "public class Foo {"
344: + PMD.EOL + " public <T extends E> Foo() {}" + PMD.EOL
345: + "}";
346:
347: public static junit.framework.Test suite() {
348: return new junit.framework.JUnit4TestAdapter(
349: JDKVersionTest.class);
350: }
351: }
|