    1   package test.net.sourceforge.pmd.symboltable;
    2
    3   import net.sourceforge.pmd.PMD;
    4   import net.sourceforge.pmd.ast.ASTCompilationUnit;
    5   import net.sourceforge.pmd.symboltable.ClassNameDeclaration;
    6
    7   import java.util.Iterator;
    8   import java.util.Map;
    9
    10  public class SourceFileScopeTest extends STBBaseTst {
    11
    12      public void testClassDeclAppears() {
    13          parseCode(TEST1);
    14          Map m = acu.getScope().getClassDeclarations();
    15          ClassNameDeclaration classNameDeclaration = (ClassNameDeclaration) m.keySet().iterator().next();
    16          assertEquals(classNameDeclaration.getImage(), "Foo");
    17      }
    18
    19      public void testPackageIsEmptyString() {
    20          parseCode(TEST1);
    21          ASTCompilationUnit decl = (ASTCompilationUnit) (acu.findChildrenOfType(ASTCompilationUnit.class)).get(0);
    22          assertEquals(decl.getScope().getEnclosingSourceFileScope().getPackageName(), "");
    23      }
    24
    25      public void testPackageNameFound() {
    26          parseCode(TEST2);
    27          ASTCompilationUnit decl = (ASTCompilationUnit) (acu.findChildrenOfType(ASTCompilationUnit.class)).get(0);
    28          assertEquals(decl.getScope().getEnclosingSourceFileScope().getPackageName(), "foo.bar");
    29      }
    30
    31      public void testNestedClasses() {
    32          parseCode(TEST3);
    33          Map m = acu.getScope().getClassDeclarations();
    34          Iterator iterator = m.keySet().iterator();
    35          ClassNameDeclaration classNameDeclaration = (ClassNameDeclaration) iterator.next();
    36          assertEquals(classNameDeclaration.getImage(), "Foo");
    37          assertFalse(iterator.hasNext());
    38      }
    39
    40      private static final String TEST1 =
    41              "public class Foo {}" + PMD.EOL;
    42
    43      private static final String TEST2 =
    44              "package foo.bar;" + PMD.EOL +
    45              "public class Foo {" + PMD.EOL +
    46              "}" + PMD.EOL;
    47
    48      private static final String TEST3 =
    49              "public class Foo {" + PMD.EOL +
    50              " public class Bar {" + PMD.EOL +
    51              " }" + PMD.EOL +
    52              "}" + PMD.EOL;
    53
    54  }