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      @Test
20      public final void testGetFirstASTNameImageNull() throws Throwable {
21          parseJava14(ABSTRACT_METHOD_LEVEL_CLASS_DECL);
22      }
23  
24      @Test
25      public final void testCastLookaheadProblem() throws Throwable {
26          parseJava14(CAST_LOOKAHEAD_PROBLEM);
27      }
28      
29      /**
30       * Tests a specific generic notation for calling methods.
31       * See: https://jira.codehaus.org/browse/MPMD-139
32       */
33      @Test
34      public void testGenericsProblem() {
35      	parseJava15(GENERICS_PROBLEM);
36      	parseJava17(GENERICS_PROBLEM);
37      }
38      
39      @Test
40      public void testParsersCases() {
41      	String test15 = readAsString("/net/sourceforge/pmd/ast/ParserCornerCases.java");
42      	parseJava15(test15);
43      	
44      	String test17 = readAsString("/net/sourceforge/pmd/ast/ParserCornerCases17.java");
45      	parseJava17(test17);
46      }
47      
48      private String readAsString(String resource) {
49      	InputStream in = ParserCornersTest.class.getResourceAsStream(resource);
50      	StringBuilder sb = new StringBuilder();
51      	int c;
52      	try {
53          	while((c = in.read()) != -1) {
54          		sb.append((char)c);
55          	}
56      	} catch (IOException e) {
57      		// ignored
58      	} finally {
59      		IOUtil.closeQuietly(in);
60      	}
61      	return sb.toString();
62      }
63      
64      private static final String GENERICS_PROBLEM =
65      		"public class Test {" + PMD.EOL +
66      		" public void test() {" + PMD.EOL +
67      		"   String o = super.<String> doStuff(\"\");" + PMD.EOL +
68      		" }" + PMD.EOL +
69      		"}";
70  
71      private static final String ABSTRACT_METHOD_LEVEL_CLASS_DECL =
72              "public class Test {" + PMD.EOL +
73              "  void bar() {" + PMD.EOL +
74              "   abstract class X { public abstract void f(); }" + PMD.EOL +
75              "   class Y extends X { public void f() {" + PMD.EOL +
76              "    new Y().f();" + PMD.EOL +
77              "   }}" + PMD.EOL +
78              "  }" + PMD.EOL +
79              "}";
80  
81      private static final String CAST_LOOKAHEAD_PROBLEM =
82          "public class BadClass {" + PMD.EOL +
83          "  public Class foo() {" + PMD.EOL +
84          "    return (byte[].class);" + PMD.EOL +
85          "  }" + PMD.EOL +
86          "}";
87  
88      public static junit.framework.Test suite() {
89          return new junit.framework.JUnit4TestAdapter(ParserCornersTest.class);
90      }
91  }