    1   package test.net.sourceforge.pmd.ast;
    2
    3   import net.sourceforge.pmd.PMD;
    4   import net.sourceforge.pmd.TargetJDK1_4;
    5   import net.sourceforge.pmd.TargetJDK1_5;
    6   import net.sourceforge.pmd.ast.ASTCompilationUnit;
    7   import net.sourceforge.pmd.ast.ASTFieldDeclaration;
    8   import net.sourceforge.pmd.ast.ASTType;
    9   import net.sourceforge.pmd.ast.ASTVariableDeclarator;
    10  import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
    11  import net.sourceforge.pmd.ast.Dimensionable;
    12  import net.sourceforge.pmd.ast.JavaParser;
    13  import test.net.sourceforge.pmd.testframework.ParserTst;
    14
    15  import java.io.StringReader;
    16
    17  public class ASTFieldDeclarationTest extends ParserTst {
    18
    19      public void testIsArray() {
    20          JavaParser parser = (new TargetJDK1_4()).createParser(new StringReader(TEST1));
    21          ASTCompilationUnit cu = parser.CompilationUnit();
    22          Dimensionable node = (Dimensionable) cu.findChildrenOfType(ASTFieldDeclaration.class).get(0);
    23          assertTrue(node.isArray());
    24          assertEquals(1, node.getArrayDepth());
    25      }
    26
    27      public void testMultiDimensionalArray() {
    28          JavaParser parser = (new TargetJDK1_4()).createParser(new StringReader(TEST2));
    29          ASTCompilationUnit cu = parser.CompilationUnit();
    30          Dimensionable node = (Dimensionable) cu.findChildrenOfType(ASTFieldDeclaration.class).get(0);
    31          assertEquals(3, node.getArrayDepth());
    32      }
    33
    34      public void testIsSyntacticallyPublic() {
    35          JavaParser parser = (new TargetJDK1_4()).createParser(new StringReader(TEST3));
    36          ASTCompilationUnit cu = parser.CompilationUnit();
    37          ASTFieldDeclaration node = (ASTFieldDeclaration) cu.findChildrenOfType(ASTFieldDeclaration.class).get(0);
    38          assertFalse(node.isSyntacticallyPublic());
    39          assertFalse(node.isPackagePrivate());
    40          assertFalse(node.isPrivate());
    41          assertFalse(node.isProtected());
    42          assertTrue(node.isFinal());
    43          assertTrue(node.isStatic());
    44          assertTrue(node.isPublic());
    45      }
    46
    47      public void testWithEnum() {
    48          JavaParser parser = (new TargetJDK1_5()).createParser(new StringReader(TEST4));
    49          ASTCompilationUnit cu = parser.CompilationUnit();
    50          ASTFieldDeclaration node = (ASTFieldDeclaration) cu.findChildrenOfType(ASTFieldDeclaration.class).get(0);
    51          assertFalse(node.isInterfaceMember());
    52      }
    53
    54      private static final String TEST1 =
    55              "class Foo {" + PMD.EOL +
    56              " String[] foo;" + PMD.EOL +
    57              "}";
    58
    59      private static final String TEST2 =
    60              "class Foo {" + PMD.EOL +
    61              " String[][][] foo;" + PMD.EOL +
    62              "}";
    63
    64      private static final String TEST3 =
    65              "interface Foo {" + PMD.EOL +
    66              " int BAR = 6;" + PMD.EOL +
    67              "}";
    68
    69      private static final String TEST4 =
    70              "public enum Foo {" + PMD.EOL +
    71              " FOO(1);" + PMD.EOL +
    72              " private int x;" + PMD.EOL +
    73              "}";
    74
    75      public void testGetVariableName() {
    76          int id = 0;
    77          ASTFieldDeclaration n = new ASTFieldDeclaration(id++);
    78          ASTType t = new ASTType(id++);
    79          ASTVariableDeclarator decl = new ASTVariableDeclarator(id++);
    80          ASTVariableDeclaratorId declid = new ASTVariableDeclaratorId(id++);
    81          n.jjtAddChild(t, 0);
    82          t.jjtAddChild(decl, 0);
    83          decl.jjtAddChild(declid, 0);
    84          declid.setImage("foo");
    85
    86          assertEquals("foo", n.getVariableName());
    87
    88      }
    89  }