1   package test.net.sourceforge.pmd.symboltable;
2   
3   import static org.junit.Assert.assertEquals;
4   import net.sourceforge.pmd.PMD;
5   import net.sourceforge.pmd.ast.ASTMethodDeclaration;
6   import net.sourceforge.pmd.symboltable.MethodScope;
7   import net.sourceforge.pmd.symboltable.NameOccurrence;
8   import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
9   
10  import org.junit.Test;
11  
12  import java.util.List;
13  import java.util.Map;
14  
15  public class MethodScopeTest extends STBBaseTst {
16  
17      @Test
18      public void testMethodParameterOccurrenceRecorded() {
19          parseCode(TEST1);
20          Map m = acu.findChildrenOfType(ASTMethodDeclaration.class).get(0).getScope().getVariableDeclarations();
21          VariableNameDeclaration vnd = (VariableNameDeclaration) m.keySet().iterator().next();
22          assertEquals("bar", vnd.getImage());
23          List occs = (List) m.get(vnd);
24          NameOccurrence occ = (NameOccurrence) occs.get(0);
25          assertEquals(3, occ.getLocation().getBeginLine());
26      }
27  
28      @Test
29      public void testMethodName() {
30          parseCode(TEST1);
31          ASTMethodDeclaration meth = acu.findChildrenOfType(ASTMethodDeclaration.class).get(0);
32          MethodScope ms = (MethodScope) meth.getScope();
33          assertEquals(ms.getName(), "foo");
34      }
35      @Test
36      public void testGenerics() {
37          parseCode15(TEST_GENERICS);
38      }
39  
40      public static final String TEST1 =
41              "public class Foo {" + PMD.EOL +
42              " void foo(int bar) {" + PMD.EOL +
43              "  bar = 2;" + PMD.EOL +
44              " }" + PMD.EOL +
45              "}";
46  
47      private static final String TEST_GENERICS =
48          "public class Tree {" + PMD.EOL +
49          "  private List<Object> subForest;" + PMD.EOL +
50          "  public <B> Tree<B> fmap(final F<B> f) { return Tree.<B>foo(); }" + PMD.EOL +
51          "  public List<Object> subForest() { return null; }" + PMD.EOL +
52          "}" + PMD.EOL;
53  
54      public static junit.framework.Test suite() {
55          return new junit.framework.JUnit4TestAdapter(MethodScopeTest.class);
56      }
57  }