1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3   */
4   package test.net.sourceforge.pmd.symboltable;
5   
6   import junit.framework.TestCase;
7   import net.sourceforge.pmd.ast.ASTFormalParameter;
8   import net.sourceforge.pmd.ast.ASTName;
9   import net.sourceforge.pmd.ast.ASTTryStatement;
10  import net.sourceforge.pmd.ast.ASTType;
11  import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
12  import net.sourceforge.pmd.symboltable.LocalScope;
13  import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
14  
15  public class VariableNameDeclarationTest extends TestCase {
16  
17      public void testConstructor() {
18          ASTVariableDeclaratorId exp = createNode("foo", 10);
19          LocalScope scope = new LocalScope();
20          exp.setScope(scope);
21          VariableNameDeclaration decl = new VariableNameDeclaration(exp);
22          assertEquals("foo", decl.getImage());
23          assertEquals(10, decl.getLine());
24      }
25  
26      public void testExceptionBlkParam() {
27          ASTVariableDeclaratorId id = new ASTVariableDeclaratorId(3);
28          id.testingOnly__setBeginLine(10);
29          id.setImage("foo");
30  
31          ASTFormalParameter param = new ASTFormalParameter(2);
32          id.jjtSetParent(param);
33  
34          ASTTryStatement tryStmt = new ASTTryStatement(1);
35          param.jjtSetParent(tryStmt);
36  
37          VariableNameDeclaration decl = new VariableNameDeclaration(id);
38          assertTrue(decl.isExceptionBlockParameter());
39      }
40  
41      public void testMethodParam() {
42          ASTVariableDeclaratorId id = new ASTVariableDeclaratorId(3);
43          id.testingOnly__setBeginLine(10);
44          id.setImage("foo");
45  
46          ASTFormalParameter param = new ASTFormalParameter(2);
47          id.jjtSetParent(param);
48  
49          ASTType type = new ASTType(4);
50          param.jjtAddChild(type, 0);
51  
52          ASTName name = new ASTName(5);
53          type.jjtAddChild(name, 0);
54  
55          assertEquals(name, id.getTypeNameNode());
56      }
57  
58      private static ASTVariableDeclaratorId createNode(String image, int line) {
59          ASTVariableDeclaratorId node = new ASTVariableDeclaratorId(1);
60          node.setImage(image);
61          node.testingOnly__setBeginLine(line);
62          return node;
63      }
64  }