1
2
3
4 package net.sourceforge.pmd.ast;
5
6 import static org.junit.Assert.assertEquals;
7 import static org.junit.Assert.fail;
8
9 import java.io.IOException;
10 import java.io.InputStream;
11
12 import net.sourceforge.pmd.PMD;
13 import net.sourceforge.pmd.lang.java.ast.ParseException;
14 import net.sourceforge.pmd.testframework.ParserTst;
15
16 import org.apache.commons.io.IOUtils;
17 import org.junit.Test;
18
19
20 public class ParserCornersTest extends ParserTst {
21
22
23
24
25
26 @Test
27 public void testInnerOuterClass() throws Exception {
28 parseJava17("/**\n" +
29 " * @author azagorulko\n" +
30 " *\n" +
31 " */\n" +
32 "public class TestInnerClassCallsOuterParent {\n" +
33 "\n" +
34 " public void test() {\n" +
35 " new Runnable() {\n" +
36 " @Override\n" +
37 " public void run() {\n" +
38 " TestInnerClassCallsOuterParent.super.toString();\n" +
39 " }\n" +
40 " };\n" +
41 " }\n" +
42 "}\n"
43 );
44 }
45
46 @Test
47 public final void testGetFirstASTNameImageNull() throws Throwable {
48 parseJava14(ABSTRACT_METHOD_LEVEL_CLASS_DECL);
49 }
50
51 @Test
52 public final void testCastLookaheadProblem() throws Throwable {
53 parseJava14(CAST_LOOKAHEAD_PROBLEM);
54 }
55
56
57
58
59
60 @Test
61 public void testGenericsProblem() {
62 parseJava15(GENERICS_PROBLEM);
63 parseJava17(GENERICS_PROBLEM);
64 }
65
66 @Test
67 public void testParsersCases() {
68 String test15 = readAsString("/net/sourceforge/pmd/ast/ParserCornerCases.java");
69 parseJava15(test15);
70
71 String test17 = readAsString("/net/sourceforge/pmd/ast/ParserCornerCases17.java");
72 parseJava17(test17);
73
74 String test18 = readAsString("/net/sourceforge/pmd/ast/ParserCornerCases18.java");
75 parseJava18(test18);
76 }
77
78 @Test
79 public void testMultipleExceptionCatching() {
80 String code = "public class Foo { public void bar() { "
81 + "try { System.out.println(); } catch (RuntimeException | IOException e) {} } }";
82 try {
83 parseJava15(code);
84 fail("Expected exception");
85 } catch (ParseException e) {
86 assertEquals("Line 1, Column 94: Cannot catch multiple exceptions when running in JDK inferior to 1.7 mode!", e.getMessage());
87 }
88
89 try {
90 parseJava17(code);
91
92 } catch (ParseException e) {
93 fail();
94 }
95 }
96
97 private String readAsString(String resource) {
98 InputStream in = ParserCornersTest.class.getResourceAsStream(resource);
99 try {
100 return IOUtils.toString(in);
101 } catch (IOException e) {
102 throw new RuntimeException(e);
103 } finally {
104 IOUtils.closeQuietly(in);
105 }
106 }
107
108 private static final String GENERICS_PROBLEM =
109 "public class Test {" + PMD.EOL +
110 " public void test() {" + PMD.EOL +
111 " String o = super.<String> doStuff(\"\");" + PMD.EOL +
112 " }" + PMD.EOL +
113 "}";
114
115 private static final String ABSTRACT_METHOD_LEVEL_CLASS_DECL =
116 "public class Test {" + PMD.EOL +
117 " void bar() {" + PMD.EOL +
118 " abstract class X { public abstract void f(); }" + PMD.EOL +
119 " class Y extends X { public void f() {" + PMD.EOL +
120 " new Y().f();" + PMD.EOL +
121 " }}" + PMD.EOL +
122 " }" + PMD.EOL +
123 "}";
124
125 private static final String CAST_LOOKAHEAD_PROBLEM =
126 "public class BadClass {" + PMD.EOL +
127 " public Class foo() {" + PMD.EOL +
128 " return (byte[].class);" + PMD.EOL +
129 " }" + PMD.EOL +
130 "}";
131
132 public static junit.framework.Test suite() {
133 return new junit.framework.JUnit4TestAdapter(ParserCornersTest.class);
134 }
135 }