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