View Javadoc

    1   package net.sourceforge.pmd.ast;
    2
    3   import static org.junit.Assert.assertEquals;
    4   import net.sourceforge.pmd.PMD;
    5   import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
    6   import net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration;
    7   import net.sourceforge.pmd.testframework.ParserTst;
    8
    9   import org.junit.Test;
    10
    11
    12  public class ASTLocalVariableDeclarationTest extends ParserTst {
    13
    14      @Test
    15      public void testSingleDimArray() {
    16          ASTCompilationUnit cu = parseJava14(TEST1);
    17          ASTLocalVariableDeclaration node = cu.findDescendantsOfType(ASTLocalVariableDeclaration.class).get(0);
    18          assertEquals(1, node.getArrayDepth());
    19      }
    20
    21      @Test
    22      public void testMultDimArray() {
    23          ASTCompilationUnit cu = parseJava14(TEST2);
    24          ASTLocalVariableDeclaration node = cu.findDescendantsOfType(ASTLocalVariableDeclaration.class).get(0);
    25          assertEquals(2, node.getArrayDepth());
    26      }
    27
    28      @Test
    29      public void testMultDimArraySplitBraces() {
    30          ASTCompilationUnit cu = parseJava14(TEST3);
    31          ASTLocalVariableDeclaration node = cu.findDescendantsOfType(ASTLocalVariableDeclaration.class).get(0);
    32          assertEquals(3, node.getArrayDepth());
    33      }
    34
    35      private static final String TEST1 =
    36              "class Foo {" + PMD.EOL +
    37              " void bar() {int x[] = null;}" + PMD.EOL +
    38              "}";
    39
    40      private static final String TEST2 =
    41              "class Foo {" + PMD.EOL +
    42              " void bar() {int x[][] = null;}" + PMD.EOL +
    43              "}";
    44
    45      private static final String TEST3 =
    46              "class Foo {" + PMD.EOL +
    47              " void bar() {int[] x[][] = null;}" + PMD.EOL +
    48              "}";
    49
    50      public static junit.framework.Test suite() {
    51          return new junit.framework.JUnit4TestAdapter(ASTLocalVariableDeclarationTest.class);
    52      }
    53  }