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