View Javadoc

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