View Javadoc

1   package net.sourceforge.pmd.symboltable;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertFalse;
5   
6   import java.util.Iterator;
7   import java.util.Map;
8   
9   import net.sourceforge.pmd.PMD;
10  import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
11  import net.sourceforge.pmd.lang.java.symboltable.ClassNameDeclaration;
12  import net.sourceforge.pmd.lang.java.symboltable.SourceFileScope;
13  
14  import org.junit.Test;
15  public class SourceFileScopeTest extends STBBaseTst {
16  
17      @Test
18      public void testClassDeclAppears() {
19          parseCode(TEST1);
20          Map m = acu.getScope().getDeclarations();
21          ClassNameDeclaration classNameDeclaration = (ClassNameDeclaration) m.keySet().iterator().next();
22          assertEquals(classNameDeclaration.getImage(), "Foo");
23      }
24  
25      @Test
26      public void testPackageIsEmptyString() {
27          parseCode(TEST1);
28          ASTCompilationUnit decl = acu;
29          assertEquals(decl.getScope().getEnclosingScope(SourceFileScope.class).getPackageName(), "");
30      }
31  
32      @Test
33      public void testPackageNameFound() {
34          parseCode(TEST2);
35          ASTCompilationUnit decl = acu;
36          assertEquals(decl.getScope().getEnclosingScope(SourceFileScope.class).getPackageName(), "foo.bar");
37      }
38  
39      @Test
40      public void testNestedClasses() {
41          parseCode(TEST3);
42          Map m = acu.getScope().getDeclarations();
43          Iterator iterator = m.keySet().iterator();
44          ClassNameDeclaration classNameDeclaration = (ClassNameDeclaration) iterator.next();
45          assertEquals(classNameDeclaration.getImage(), "Foo");
46          assertFalse(iterator.hasNext());
47      }
48  
49      private static final String TEST1 =
50              "public class Foo {}" + PMD.EOL;
51  
52      private static final String TEST2 =
53              "package foo.bar;" + PMD.EOL +
54              "public class Foo {" + PMD.EOL +
55              "}" + PMD.EOL;
56  
57      private static final String TEST3 =
58              "public class Foo {" + PMD.EOL +
59              " public class Bar {" + PMD.EOL +
60              " }" + PMD.EOL +
61              "}" + PMD.EOL;
62  
63      public static junit.framework.Test suite() {
64          return new junit.framework.JUnit4TestAdapter(SourceFileScopeTest.class);
65      }
66  }