001: package test.net.sourceforge.pmd.ast;
002:
003: import static org.junit.Assert.assertEquals;
004: import static org.junit.Assert.assertFalse;
005: import static org.junit.Assert.assertTrue;
006: import net.sourceforge.pmd.PMD;
007: import net.sourceforge.pmd.TargetJDK1_4;
008: import net.sourceforge.pmd.TargetJDK1_5;
009: import net.sourceforge.pmd.ast.ASTCompilationUnit;
010: import net.sourceforge.pmd.ast.ASTFieldDeclaration;
011: import net.sourceforge.pmd.ast.ASTType;
012: import net.sourceforge.pmd.ast.ASTVariableDeclarator;
013: import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
014: import net.sourceforge.pmd.ast.Dimensionable;
015: import net.sourceforge.pmd.ast.JavaParser;
016:
017: import org.junit.Test;
018:
019: import test.net.sourceforge.pmd.testframework.ParserTst;
020:
021: import java.io.StringReader;
022:
023: public class ASTFieldDeclarationTest extends ParserTst {
024:
025: @Test
026: public void testIsArray() {
027: JavaParser parser = (new TargetJDK1_4())
028: .createParser(new StringReader(TEST1));
029: ASTCompilationUnit cu = parser.CompilationUnit();
030: Dimensionable node = cu.findChildrenOfType(
031: ASTFieldDeclaration.class).get(0);
032: assertTrue(node.isArray());
033: assertEquals(1, node.getArrayDepth());
034: }
035:
036: @Test
037: public void testMultiDimensionalArray() {
038: JavaParser parser = (new TargetJDK1_4())
039: .createParser(new StringReader(TEST2));
040: ASTCompilationUnit cu = parser.CompilationUnit();
041: Dimensionable node = cu.findChildrenOfType(
042: ASTFieldDeclaration.class).get(0);
043: assertEquals(3, node.getArrayDepth());
044: }
045:
046: @Test
047: public void testIsSyntacticallyPublic() {
048: JavaParser parser = (new TargetJDK1_4())
049: .createParser(new StringReader(TEST3));
050: ASTCompilationUnit cu = parser.CompilationUnit();
051: ASTFieldDeclaration node = cu.findChildrenOfType(
052: ASTFieldDeclaration.class).get(0);
053: assertFalse(node.isSyntacticallyPublic());
054: assertFalse(node.isPackagePrivate());
055: assertFalse(node.isPrivate());
056: assertFalse(node.isProtected());
057: assertTrue(node.isFinal());
058: assertTrue(node.isStatic());
059: assertTrue(node.isPublic());
060: }
061:
062: @Test
063: public void testWithEnum() {
064: JavaParser parser = (new TargetJDK1_5())
065: .createParser(new StringReader(TEST4));
066: ASTCompilationUnit cu = parser.CompilationUnit();
067: ASTFieldDeclaration node = cu.findChildrenOfType(
068: ASTFieldDeclaration.class).get(0);
069: assertFalse(node.isInterfaceMember());
070: }
071:
072: private static final String TEST1 = "class Foo {" + PMD.EOL
073: + " String[] foo;" + PMD.EOL + "}";
074:
075: private static final String TEST2 = "class Foo {" + PMD.EOL
076: + " String[][][] foo;" + PMD.EOL + "}";
077:
078: private static final String TEST3 = "interface Foo {" + PMD.EOL
079: + " int BAR = 6;" + PMD.EOL + "}";
080:
081: private static final String TEST4 = "public enum Foo {" + PMD.EOL
082: + " FOO(1);" + PMD.EOL + " private int x;" + PMD.EOL + "}";
083:
084: @Test
085: public void testGetVariableName() {
086: int id = 0;
087: ASTFieldDeclaration n = new ASTFieldDeclaration(id++);
088: ASTType t = new ASTType(id++);
089: ASTVariableDeclarator decl = new ASTVariableDeclarator(id++);
090: ASTVariableDeclaratorId declid = new ASTVariableDeclaratorId(
091: id++);
092: n.jjtAddChild(t, 0);
093: t.jjtAddChild(decl, 0);
094: decl.jjtAddChild(declid, 0);
095: declid.setImage("foo");
096:
097: assertEquals("foo", n.getVariableName());
098:
099: }
100:
101: public static junit.framework.Test suite() {
102: return new junit.framework.JUnit4TestAdapter(
103: ASTFieldDeclarationTest.class);
104: }
105: }
|