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 static org.junit.Assert.assertEquals;
7   import static org.junit.Assert.assertTrue;
8   import net.sourceforge.pmd.PMD;
9   import net.sourceforge.pmd.lang.java.ast.ASTBlock;
10  import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
11  import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
12  import net.sourceforge.pmd.lang.java.ast.ASTTryStatement;
13  import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
14  import net.sourceforge.pmd.testframework.ParserTst;
15  
16  import org.junit.Test;
17  
18  
19  public class ASTVariableDeclaratorIdTest extends ParserTst {
20  
21      @Test
22      public void testIsExceptionBlockParameter() {
23          ASTTryStatement tryNode = new ASTTryStatement(1);
24          ASTBlock block = new ASTBlock(2);
25          ASTVariableDeclaratorId v = new ASTVariableDeclaratorId(3);
26          v.jjtSetParent(block);
27          block.jjtSetParent(tryNode);
28          assertTrue(v.isExceptionBlockParameter());
29      }
30  
31      @Test
32      public void testTypeNameNode() throws Throwable {
33          ASTCompilationUnit acu = super.getNodes(ASTCompilationUnit.class, TYPE_NAME_NODE).iterator().next();
34          ASTVariableDeclaratorId id = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(0);
35  
36          ASTClassOrInterfaceType name = (ASTClassOrInterfaceType) id.getTypeNameNode().jjtGetChild(0);
37          assertEquals("String", name.getImage());
38      }
39  
40      @Test
41      public void testAnnotations() throws Throwable {
42          ASTCompilationUnit acu = super.getNodes(ASTCompilationUnit.class, TEST_ANNOTATIONS).iterator().next();
43          ASTVariableDeclaratorId id = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(0);
44  
45          ASTClassOrInterfaceType name = (ASTClassOrInterfaceType) id.getTypeNameNode().jjtGetChild(0);
46          assertEquals("String", name.getImage());
47      }
48  
49      private static final String TYPE_NAME_NODE =
50              "public class Test {" + PMD.EOL +
51              "  private String bar;" + PMD.EOL +
52              "}";
53  
54      private static final String TEST_ANNOTATIONS =
55              "public class Foo {" + PMD.EOL +
56              "    public void bar(@A1 @A2 String s) {}" + PMD.EOL +
57              "}";
58  
59      public static junit.framework.Test suite() {
60          return new junit.framework.JUnit4TestAdapter(ASTVariableDeclaratorIdTest.class);
61      }
62  }