View Javadoc

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