View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.symboltable;
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.ASTFormalParameter;
10  import net.sourceforge.pmd.lang.java.ast.ASTTryStatement;
11  import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
12  import net.sourceforge.pmd.lang.java.symboltable.Scope;
13  import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
14  
15  import org.junit.Test;
16  
17  import java.util.List;
18  public class VariableNameDeclarationTest extends STBBaseTst {
19  
20      @Test
21      public void testConstructor() {
22          parseCode(TEST1);
23          List nodes = acu.findDescendantsOfType(ASTVariableDeclaratorId.class);
24          Scope s = ((ASTVariableDeclaratorId) nodes.get(0)).getScope();
25          VariableNameDeclaration decl = s.getVariableDeclarations().keySet().iterator().next();
26          assertEquals("bar", decl.getImage());
27          assertEquals(3, decl.getNode().getBeginLine());
28      }
29  
30      @Test
31      public void testExceptionBlkParam() {
32          ASTVariableDeclaratorId id = new ASTVariableDeclaratorId(3);
33          id.testingOnly__setBeginLine(10);
34          id.setImage("foo");
35          ASTFormalParameter param = new ASTFormalParameter(2);
36          id.jjtSetParent(param);
37          param.jjtSetParent(new ASTTryStatement(1));
38          VariableNameDeclaration decl = new VariableNameDeclaration(id);
39          assertTrue(decl.isExceptionBlockParameter());
40      }
41  
42      @Test
43      public void testIsArray() {
44          parseCode(TEST3);
45          VariableNameDeclaration decl = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(0).getScope().getVariableDeclarations().keySet().iterator().next();
46          assertTrue(decl.isArray());
47      }
48  
49      @Test
50      public void testPrimitiveType() {
51          parseCode(TEST1);
52          VariableNameDeclaration decl = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(0).getScope().getVariableDeclarations().keySet().iterator().next();
53          assertTrue(decl.isPrimitiveType());
54      }
55  
56      @Test
57      public void testArrayIsReferenceType() {
58          parseCode(TEST3);
59          VariableNameDeclaration decl = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(0).getScope().getVariableDeclarations().keySet().iterator().next();
60          assertTrue(decl.isReferenceType());
61      }
62  
63      @Test
64      public void testPrimitiveTypeImage() {
65          parseCode(TEST3);
66          VariableNameDeclaration decl = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(0).getScope().getVariableDeclarations().keySet().iterator().next();
67          assertEquals("int", decl.getTypeImage());
68      }
69  
70      @Test
71      public void testRefTypeImage() {
72          parseCode(TEST4);
73          VariableNameDeclaration decl = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(0).getScope().getVariableDeclarations().keySet().iterator().next();
74          assertEquals("String", decl.getTypeImage());
75      }
76  
77      @Test
78      public void testParamTypeImage() {
79          parseCode(TEST5);
80          VariableNameDeclaration decl = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(0).getScope().getVariableDeclarations().keySet().iterator().next();
81          assertEquals("String", decl.getTypeImage());
82      }
83  
84      public static final String TEST1 =
85              "public class Foo {" + PMD.EOL +
86              " void foo() {" + PMD.EOL +
87              "  int bar = 42;" + PMD.EOL +
88              " }" + PMD.EOL +
89              "}";
90  
91      public static final String TEST2 =
92              "public class Foo {" + PMD.EOL +
93              " void foo() {" + PMD.EOL +
94              "  try {} catch(Exception e) {}" + PMD.EOL +
95              " }" + PMD.EOL +
96              "}";
97  
98      public static final String TEST3 =
99              "public class Foo {" + PMD.EOL +
100             " void foo() {" + PMD.EOL +
101             "  int[] x;" + PMD.EOL +
102             " }" + PMD.EOL +
103             "}";
104 
105     public static final String TEST4 =
106             "public class Foo {" + PMD.EOL +
107             " void foo() {" + PMD.EOL +
108             "  String x;" + PMD.EOL +
109             " }" + PMD.EOL +
110             "}";
111     public static final String TEST5 =
112             "public class Foo {" + PMD.EOL +
113             " void foo(String x) {}" + PMD.EOL +
114             "}";
115 
116     public static junit.framework.Test suite() {
117         return new junit.framework.JUnit4TestAdapter(VariableNameDeclarationTest.class);
118     }
119 }