View Javadoc

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