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