View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.ast;
5   
6   import static org.junit.Assert.assertEquals;
7   import static org.junit.Assert.assertNull;
8   import static org.junit.Assert.assertTrue;
9   import net.sourceforge.pmd.PMD;
10  import net.sourceforge.pmd.lang.java.ParserTst;
11  
12  import org.junit.Test;
13  
14  
15  public class ASTVariableDeclaratorIdTest extends ParserTst {
16  
17      @Test
18      public void testIsExceptionBlockParameter() {
19          ASTTryStatement tryNode = new ASTTryStatement(1);
20          ASTBlock block = new ASTBlock(2);
21          ASTVariableDeclaratorId v = new ASTVariableDeclaratorId(3);
22          v.jjtSetParent(block);
23          block.jjtSetParent(tryNode);
24          assertTrue(v.isExceptionBlockParameter());
25      }
26  
27      @Test
28      public void testTypeNameNode() throws Throwable {
29          ASTCompilationUnit acu = super.getNodes(ASTCompilationUnit.class, TYPE_NAME_NODE).iterator().next();
30          ASTVariableDeclaratorId id = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(0);
31  
32          ASTClassOrInterfaceType name = (ASTClassOrInterfaceType) id.getTypeNameNode().jjtGetChild(0);
33          assertEquals("String", name.getImage());
34      }
35  
36      @Test
37      public void testAnnotations() throws Throwable {
38          ASTCompilationUnit acu = super.getNodes(ASTCompilationUnit.class, TEST_ANNOTATIONS).iterator().next();
39          ASTVariableDeclaratorId id = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(0);
40  
41          ASTClassOrInterfaceType name = (ASTClassOrInterfaceType) id.getTypeNameNode().jjtGetChild(0);
42          assertEquals("String", name.getImage());
43      }
44      
45      @Test
46      public void testLambdaWithType() throws Exception {
47          ASTCompilationUnit acu = parseJava18(TEST_LAMBDA_WITH_TYPE);
48          ASTVariableDeclaratorId f = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(1);
49          assertEquals("File", f.getTypeNode().getTypeImage());
50          assertEquals("File", f.getTypeNameNode().jjtGetChild(0).getImage());
51      }
52  
53      @Test
54      public void testLambdaWithoutType() throws Exception {
55          ASTCompilationUnit acu = parseJava18(TEST_LAMBDA_WITHOUT_TYPE);
56          ASTVariableDeclaratorId f = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(1);
57          assertNull(f.getTypeNode());
58          assertNull(f.getTypeNameNode());
59      }
60  
61      private static final String TYPE_NAME_NODE =
62              "public class Test {" + PMD.EOL +
63              "  private String bar;" + PMD.EOL +
64              "}";
65      private static final String TEST_ANNOTATIONS =
66              "public class Foo {" + PMD.EOL +
67              "    public void bar(@A1 @A2 String s) {}" + PMD.EOL +
68              "}";
69      private static final String TEST_LAMBDA_WITH_TYPE =
70              "public class Foo {\n" +
71              "    public void bar() {\n" +
72              "        FileFilter java = (File f) -> f.getName().endsWith(\".java\");\n" +
73              "    }\n" +
74              "}\n";
75      private static final String TEST_LAMBDA_WITHOUT_TYPE =
76              "public class Foo {\n" +
77              "    public void bar() {\n" +
78              "        FileFilter java2 = f -> f.getName().endsWith(\".java\");\n" +
79              "    }\n" +
80              "}\n";
81  
82      public static junit.framework.Test suite() {
83          return new junit.framework.JUnit4TestAdapter(ASTVariableDeclaratorIdTest.class);
84      }
85  }