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