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.ASTName;
8   import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
9   import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
10  import net.sourceforge.pmd.symboltable.LocalScope;
11  import net.sourceforge.pmd.symboltable.NameOccurrence;
12  import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
13  
14  public class LocalScopeTest extends TestCase {
15  
16      private class MyASTVariableDeclaratorId extends ASTVariableDeclaratorId {
17          public MyASTVariableDeclaratorId(int x) {
18              super(x);
19          }
20  
21          public boolean isExceptionBlockParameter() {
22              return true;
23          }
24      }
25  
26      public void testNameWithThisOrSuperIsNotFlaggedAsUnused() {
27          LocalScope scope = new LocalScope();
28          ASTName name = new ASTName(1);
29          name.setImage("foo");
30          ASTPrimaryPrefix prefix = new ASTPrimaryPrefix(2);
31          prefix.setUsesThisModifier();
32          name.jjtAddChild(prefix, 1);
33          NameOccurrence occ = new NameOccurrence(name, "foo");
34          scope.addVariableNameOccurrence(occ);
35          assertTrue(!scope.getVariableDeclarations(false).keySet().iterator().hasNext());
36      }
37  
38      public void testNameWithSuperIsNotFlaggedAsUnused() {
39          LocalScope scope = new LocalScope();
40          ASTName name = new ASTName(1);
41          name.setImage("foo");
42          ASTPrimaryPrefix prefix = new ASTPrimaryPrefix(2);
43          prefix.setUsesSuperModifier();
44          name.jjtAddChild(prefix, 1);
45          NameOccurrence occ = new NameOccurrence(name, "foo");
46          scope.addVariableNameOccurrence(occ);
47          assertTrue(!scope.getVariableDeclarations(false).keySet().iterator().hasNext());
48      }
49  
50      public void testExceptionParamNameIsDiscarded() {
51          ASTVariableDeclaratorId node = new MyASTVariableDeclaratorId(1);
52          VariableNameDeclaration decl = new VariableNameDeclaration(node);
53          LocalScope scope = new LocalScope();
54          scope.addDeclaration(decl);
55          assertTrue(!scope.getVariableDeclarations(false).keySet().iterator().hasNext());
56      }
57  
58  }