View Javadoc

1   package net.sourceforge.pmd.symboltable;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import java.util.ArrayList;
6   import java.util.HashMap;
7   import java.util.List;
8   import java.util.Map;
9   
10  import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
11  import net.sourceforge.pmd.lang.java.ast.DummyJavaNode;
12  import net.sourceforge.pmd.lang.java.symboltable.Applier;
13  import net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence;
14  import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
15  import net.sourceforge.pmd.lang.java.symboltable.VariableUsageFinderFunction;
16  import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
17  import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
18  
19  import org.junit.Test;
20  public class VariableUsageFinderFunctionTest {
21  
22      @Test
23      public void testLookingForUsed() {
24          ASTVariableDeclaratorId variableDeclarationIdNode = new ASTVariableDeclaratorId(1);
25          variableDeclarationIdNode.setImage("x");
26          VariableNameDeclaration nameDeclaration = new VariableNameDeclaration(variableDeclarationIdNode);
27          List<NameOccurrence> nameOccurrences = new ArrayList<NameOccurrence>();
28          nameOccurrences.add(new JavaNameOccurrence(new DummyJavaNode(2), "x"));
29  
30          Map<NameDeclaration, List<NameOccurrence>> declarations = new HashMap<NameDeclaration, List<NameOccurrence>>();
31          declarations.put(nameDeclaration, nameOccurrences);
32  
33          List<NameDeclaration> vars = new ArrayList<NameDeclaration>();
34          vars.add(nameDeclaration);
35  
36          VariableUsageFinderFunction f = new VariableUsageFinderFunction(declarations);
37          Applier.apply(f, vars.iterator());
38          Map<NameDeclaration, List<NameOccurrence>> p = f.getUsed();
39          assertEquals(1, p.size());
40      }
41  
42      public static junit.framework.Test suite() {
43          return new junit.framework.JUnit4TestAdapter(VariableUsageFinderFunctionTest.class);
44      }
45  }