001: /**
002: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003: */package test.net.sourceforge.pmd.symboltable;
004:
005: import static org.junit.Assert.assertEquals;
006: import static org.junit.Assert.assertTrue;
007: import net.sourceforge.pmd.PMD;
008: import net.sourceforge.pmd.ast.ASTFormalParameter;
009: import net.sourceforge.pmd.ast.ASTTryStatement;
010: import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
011: import net.sourceforge.pmd.symboltable.Scope;
012: import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
013:
014: import org.junit.Test;
015:
016: import java.util.List;
017:
018: public class VariableNameDeclarationTest extends STBBaseTst {
019:
020: @Test
021: public void testConstructor() {
022: parseCode(TEST1);
023: List nodes = acu
024: .findChildrenOfType(ASTVariableDeclaratorId.class);
025: Scope s = ((ASTVariableDeclaratorId) nodes.get(0)).getScope();
026: VariableNameDeclaration decl = s.getVariableDeclarations()
027: .keySet().iterator().next();
028: assertEquals("bar", decl.getImage());
029: assertEquals(3, decl.getNode().getBeginLine());
030: }
031:
032: @Test
033: public void testExceptionBlkParam() {
034: ASTVariableDeclaratorId id = new ASTVariableDeclaratorId(3);
035: id.testingOnly__setBeginLine(10);
036: id.setImage("foo");
037: ASTFormalParameter param = new ASTFormalParameter(2);
038: id.jjtSetParent(param);
039: param.jjtSetParent(new ASTTryStatement(1));
040: VariableNameDeclaration decl = new VariableNameDeclaration(id);
041: assertTrue(decl.isExceptionBlockParameter());
042: }
043:
044: @Test
045: public void testIsArray() {
046: parseCode(TEST3);
047: VariableNameDeclaration decl = acu.findChildrenOfType(
048: ASTVariableDeclaratorId.class).get(0).getScope()
049: .getVariableDeclarations().keySet().iterator().next();
050: assertTrue(decl.isArray());
051: }
052:
053: @Test
054: public void testPrimitiveType() {
055: parseCode(TEST1);
056: VariableNameDeclaration decl = acu.findChildrenOfType(
057: ASTVariableDeclaratorId.class).get(0).getScope()
058: .getVariableDeclarations().keySet().iterator().next();
059: assertTrue(decl.isPrimitiveType());
060: }
061:
062: @Test
063: public void testArrayIsReferenceType() {
064: parseCode(TEST3);
065: VariableNameDeclaration decl = acu.findChildrenOfType(
066: ASTVariableDeclaratorId.class).get(0).getScope()
067: .getVariableDeclarations().keySet().iterator().next();
068: assertTrue(decl.isReferenceType());
069: }
070:
071: @Test
072: public void testPrimitiveTypeImage() {
073: parseCode(TEST3);
074: VariableNameDeclaration decl = acu.findChildrenOfType(
075: ASTVariableDeclaratorId.class).get(0).getScope()
076: .getVariableDeclarations().keySet().iterator().next();
077: assertEquals("int", decl.getTypeImage());
078: }
079:
080: @Test
081: public void testRefTypeImage() {
082: parseCode(TEST4);
083: VariableNameDeclaration decl = acu.findChildrenOfType(
084: ASTVariableDeclaratorId.class).get(0).getScope()
085: .getVariableDeclarations().keySet().iterator().next();
086: assertEquals("String", decl.getTypeImage());
087: }
088:
089: @Test
090: public void testParamTypeImage() {
091: parseCode(TEST5);
092: VariableNameDeclaration decl = acu.findChildrenOfType(
093: ASTVariableDeclaratorId.class).get(0).getScope()
094: .getVariableDeclarations().keySet().iterator().next();
095: assertEquals("String", decl.getTypeImage());
096: }
097:
098: public static final String TEST1 = "public class Foo {" + PMD.EOL
099: + " void foo() {" + PMD.EOL + " int bar = 42;" + PMD.EOL
100: + " }" + PMD.EOL + "}";
101:
102: public static final String TEST2 = "public class Foo {" + PMD.EOL
103: + " void foo() {" + PMD.EOL
104: + " try {} catch(Exception e) {}" + PMD.EOL + " }"
105: + PMD.EOL + "}";
106:
107: public static final String TEST3 = "public class Foo {" + PMD.EOL
108: + " void foo() {" + PMD.EOL + " int[] x;" + PMD.EOL + " }"
109: + PMD.EOL + "}";
110:
111: public static final String TEST4 = "public class Foo {" + PMD.EOL
112: + " void foo() {" + PMD.EOL + " String x;" + PMD.EOL
113: + " }" + PMD.EOL + "}";
114: public static final String TEST5 = "public class Foo {" + PMD.EOL
115: + " void foo(String x) {}" + PMD.EOL + "}";
116:
117: public static junit.framework.Test suite() {
118: return new junit.framework.JUnit4TestAdapter(
119: VariableNameDeclarationTest.class);
120: }
121: }
|