View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.ast;
5   
6   import java.io.IOException;
7   import java.io.InputStream;
8   
9   import net.sourceforge.pmd.PMD;
10  import net.sourceforge.pmd.lang.java.ast.ParseException;
11  import net.sourceforge.pmd.testframework.ParserTst;
12  import net.sourceforge.pmd.util.IOUtil;
13  
14  import org.junit.Test;
15  
16  
17  public class ParserCornersTest extends ParserTst {
18  
19      /**
20       * #1107 PMD 5.0.4 couldn't parse call of parent outer java class method from inner class
21       * @throws Exception any error
22       */
23      @Test
24      public void testInnerOuterClass() throws Exception {
25          parseJava17("/**\n" +
26          " * @author azagorulko\n" +
27          " *\n" +
28          " */\n" +
29          "public class TestInnerClassCallsOuterParent {\n" +
30          "\n" +
31          "    public void test() {\n" +
32          "        new Runnable() {\n" +
33          "            @Override\n" +
34          "            public void run() {\n" +
35          "                TestInnerClassCallsOuterParent.super.toString();\n" +
36          "            }\n" +
37          "        };\n" +
38          "    }\n" +
39          "}\n"
40          );
41      }
42  
43      @Test
44      public final void testGetFirstASTNameImageNull() throws Throwable {
45          parseJava14(ABSTRACT_METHOD_LEVEL_CLASS_DECL);
46      }
47  
48      @Test
49      public final void testCastLookaheadProblem() throws Throwable {
50          parseJava14(CAST_LOOKAHEAD_PROBLEM);
51      }
52      
53      /**
54       * Tests a specific generic notation for calling methods.
55       * See: https://jira.codehaus.org/browse/MPMD-139
56       */
57      @Test
58      public void testGenericsProblem() {
59      	parseJava15(GENERICS_PROBLEM);
60      	parseJava17(GENERICS_PROBLEM);
61      }
62      
63      @Test
64      public void testParsersCases() {
65      	String test15 = readAsString("/net/sourceforge/pmd/ast/ParserCornerCases.java");
66      	parseJava15(test15);
67      	
68      	String test17 = readAsString("/net/sourceforge/pmd/ast/ParserCornerCases17.java");
69      	parseJava17(test17);
70      }
71      
72      private String readAsString(String resource) {
73      	InputStream in = ParserCornersTest.class.getResourceAsStream(resource);
74      	StringBuilder sb = new StringBuilder();
75      	int c;
76      	try {
77          	while((c = in.read()) != -1) {
78          		sb.append((char)c);
79          	}
80      	} catch (IOException e) {
81      		// ignored
82      	} finally {
83      		IOUtil.closeQuietly(in);
84      	}
85      	return sb.toString();
86      }
87      
88      private static final String GENERICS_PROBLEM =
89      		"public class Test {" + PMD.EOL +
90      		" public void test() {" + PMD.EOL +
91      		"   String o = super.<String> doStuff(\"\");" + PMD.EOL +
92      		" }" + PMD.EOL +
93      		"}";
94  
95      private static final String ABSTRACT_METHOD_LEVEL_CLASS_DECL =
96              "public class Test {" + PMD.EOL +
97              "  void bar() {" + PMD.EOL +
98              "   abstract class X { public abstract void f(); }" + PMD.EOL +
99              "   class Y extends X { public void f() {" + PMD.EOL +
100             "    new Y().f();" + PMD.EOL +
101             "   }}" + PMD.EOL +
102             "  }" + PMD.EOL +
103             "}";
104 
105     private static final String CAST_LOOKAHEAD_PROBLEM =
106         "public class BadClass {" + PMD.EOL +
107         "  public Class foo() {" + PMD.EOL +
108         "    return (byte[].class);" + PMD.EOL +
109         "  }" + PMD.EOL +
110         "}";
111 
112     public static junit.framework.Test suite() {
113         return new junit.framework.JUnit4TestAdapter(ParserCornersTest.class);
114     }
115 }