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