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.assertFalse;
8   import net.sourceforge.pmd.PMD;
9   import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter;
10  import net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration;
11  import net.sourceforge.pmd.lang.java.ast.ASTName;
12  import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
13  import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
14  import net.sourceforge.pmd.lang.java.symboltable.LocalScope;
15  import net.sourceforge.pmd.lang.java.symboltable.MethodScope;
16  import net.sourceforge.pmd.lang.java.symboltable.NameDeclaration;
17  import net.sourceforge.pmd.lang.java.symboltable.NameOccurrence;
18  import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
19  
20  import org.junit.Test;
21  
22  import java.util.List;
23  import java.util.Map;
24  public class LocalScopeTest extends STBBaseTst {
25  
26      @Test
27      public void testNameWithThisOrSuperIsNotFlaggedAsUnused() {
28          LocalScope scope = new LocalScope();
29          ASTName name = new ASTName(1);
30          name.setImage("foo");
31          ASTPrimaryPrefix prefix = new ASTPrimaryPrefix(2);
32          prefix.setUsesThisModifier();
33          name.jjtAddChild(prefix, 1);
34          NameOccurrence occ = new NameOccurrence(name, "foo");
35          scope.addVariableNameOccurrence(occ);
36          assertFalse(scope.getVariableDeclarations().keySet().iterator().hasNext());
37      }
38  
39      @Test
40      public void testNameWithSuperIsNotFlaggedAsUnused() {
41          LocalScope scope = new LocalScope();
42          ASTName name = new ASTName(1);
43          name.setImage("foo");
44          ASTPrimaryPrefix prefix = new ASTPrimaryPrefix(2);
45          prefix.setUsesSuperModifier();
46          name.jjtAddChild(prefix, 1);
47          NameOccurrence occ = new NameOccurrence(name, "foo");
48          scope.addVariableNameOccurrence(occ);
49          assertFalse(scope.getVariableDeclarations().keySet().iterator().hasNext());
50      }
51  
52      @Test
53      public void testLocalVariableDeclarationFound() {
54          parseCode(TEST1);
55          List nodes = acu.findDescendantsOfType(ASTVariableDeclaratorId.class);
56          ASTVariableDeclaratorId node = (ASTVariableDeclaratorId) nodes.get(0);
57          Map vars = node.getScope().getVariableDeclarations();
58          assertEquals(1, vars.size());
59          NameDeclaration decl = (NameDeclaration) vars.keySet().iterator().next();
60          assertEquals("b", decl.getImage());
61      }
62  
63      @Test
64      public void testQualifiedNameOccurrence() {
65          parseCode(TEST2);
66          List nodes = acu.findDescendantsOfType(ASTVariableDeclaratorId.class);
67          ASTVariableDeclaratorId node = (ASTVariableDeclaratorId) nodes.get(0);
68          Map vars = node.getScope().getVariableDeclarations();
69          NameDeclaration decl = (NameDeclaration) vars.keySet().iterator().next();
70          NameOccurrence occ = (NameOccurrence) ((List) vars.get(decl)).get(0);
71          assertEquals("b", occ.getImage());
72      }
73  
74      @Test
75      public void testPostfixUsageIsRecorded() {
76          parseCode(TEST3);
77          List nodes = acu.findDescendantsOfType(ASTVariableDeclaratorId.class);
78          ASTVariableDeclaratorId node = (ASTVariableDeclaratorId) nodes.get(0);
79          Map vars = node.getScope().getVariableDeclarations();
80          NameDeclaration decl = (NameDeclaration) vars.keySet().iterator().next();
81          List usages = (List) vars.get(decl);
82          NameOccurrence occ = (NameOccurrence) usages.get(0);
83          assertEquals(4, occ.getLocation().getBeginLine());
84      }
85  
86      @Test
87      public void testLocalVariableTypesAreRecorded() {
88          parseCode(TEST1);
89          List nodes = acu.findDescendantsOfType(ASTVariableDeclaratorId.class);
90          Map vars = ((ASTVariableDeclaratorId) nodes.get(0)).getScope().getVariableDeclarations();
91          VariableNameDeclaration decl = (VariableNameDeclaration) vars.keySet().iterator().next();
92          assertEquals("Bar", decl.getTypeImage());
93      }
94  
95      @Test
96      public void testMethodArgumentTypesAreRecorded() {
97          parseCode(TEST5);
98          List nodes = acu.findDescendantsOfType(ASTFormalParameter.class);
99          Map vars = ((ASTFormalParameter) nodes.get(0)).getScope().getVariableDeclarations();
100         VariableNameDeclaration decl = (VariableNameDeclaration) vars.keySet().iterator().next();
101         assertEquals("String", decl.getTypeImage());
102     }
103 
104     @Test
105     public void testgetEnclosingMethodScope() {
106         parseCode(TEST4);
107         ASTLocalVariableDeclaration node = acu.findDescendantsOfType(ASTLocalVariableDeclaration.class).get(0);
108         LocalScope scope = (LocalScope) node.getScope();
109         MethodScope ms = scope.getEnclosingMethodScope();
110         assertEquals(2, ms.getVariableDeclarations().size());
111     }
112 
113 
114     public static final String TEST1 =
115             "public class Foo {" + PMD.EOL +
116             " void foo() {" + PMD.EOL +
117             "  Bar b = new Bar();" + PMD.EOL +
118             " }" + PMD.EOL +
119             "}";
120 
121     public static final String TEST2 =
122             "public class Foo {" + PMD.EOL +
123             " void foo() {" + PMD.EOL +
124             "  Bar b = new Bar();" + PMD.EOL +
125             "  b.buz = 2;" + PMD.EOL +
126             " }" + PMD.EOL +
127             "}";
128 
129     public static final String TEST3 =
130             "public class Foo {" + PMD.EOL +
131             " void foo() {" + PMD.EOL +
132             "  int x = 2;" + PMD.EOL +
133             "  x++;" + PMD.EOL +
134             " }" + PMD.EOL +
135             "}";
136 
137     public static final String TEST4 =
138             "public class Foo {" + PMD.EOL +
139             " void foo(String x, String z) { int y; }" + PMD.EOL +
140             "}";
141 
142     public static final String TEST5 =
143             "public class Foo {" + PMD.EOL +
144             " void foo(String x);" + PMD.EOL +
145             "}";
146 
147     public static junit.framework.Test suite() {
148         return new junit.framework.JUnit4TestAdapter(LocalScopeTest.class);
149     }
150 }