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.assertFalse;
007: import net.sourceforge.pmd.PMD;
008: import net.sourceforge.pmd.ast.ASTFormalParameter;
009: import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration;
010: import net.sourceforge.pmd.ast.ASTName;
011: import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
012: import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
013: import net.sourceforge.pmd.symboltable.LocalScope;
014: import net.sourceforge.pmd.symboltable.MethodScope;
015: import net.sourceforge.pmd.symboltable.NameDeclaration;
016: import net.sourceforge.pmd.symboltable.NameOccurrence;
017: import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
018:
019: import org.junit.Test;
020:
021: import java.util.List;
022: import java.util.Map;
023:
024: public class LocalScopeTest extends STBBaseTst {
025:
026: @Test
027: public void testNameWithThisOrSuperIsNotFlaggedAsUnused() {
028: LocalScope scope = new LocalScope();
029: ASTName name = new ASTName(1);
030: name.setImage("foo");
031: ASTPrimaryPrefix prefix = new ASTPrimaryPrefix(2);
032: prefix.setUsesThisModifier();
033: name.jjtAddChild(prefix, 1);
034: NameOccurrence occ = new NameOccurrence(name, "foo");
035: scope.addVariableNameOccurrence(occ);
036: assertFalse(scope.getVariableDeclarations().keySet().iterator()
037: .hasNext());
038: }
039:
040: @Test
041: public void testNameWithSuperIsNotFlaggedAsUnused() {
042: LocalScope scope = new LocalScope();
043: ASTName name = new ASTName(1);
044: name.setImage("foo");
045: ASTPrimaryPrefix prefix = new ASTPrimaryPrefix(2);
046: prefix.setUsesSuperModifier();
047: name.jjtAddChild(prefix, 1);
048: NameOccurrence occ = new NameOccurrence(name, "foo");
049: scope.addVariableNameOccurrence(occ);
050: assertFalse(scope.getVariableDeclarations().keySet().iterator()
051: .hasNext());
052: }
053:
054: @Test
055: public void testLocalVariableDeclarationFound() {
056: parseCode(TEST1);
057: List nodes = acu
058: .findChildrenOfType(ASTVariableDeclaratorId.class);
059: ASTVariableDeclaratorId node = (ASTVariableDeclaratorId) nodes
060: .get(0);
061: Map vars = node.getScope().getVariableDeclarations();
062: assertEquals(1, vars.size());
063: NameDeclaration decl = (NameDeclaration) vars.keySet()
064: .iterator().next();
065: assertEquals("b", decl.getImage());
066: }
067:
068: @Test
069: public void testQualifiedNameOccurrence() {
070: parseCode(TEST2);
071: List nodes = acu
072: .findChildrenOfType(ASTVariableDeclaratorId.class);
073: ASTVariableDeclaratorId node = (ASTVariableDeclaratorId) nodes
074: .get(0);
075: Map vars = node.getScope().getVariableDeclarations();
076: NameDeclaration decl = (NameDeclaration) vars.keySet()
077: .iterator().next();
078: NameOccurrence occ = (NameOccurrence) ((List) vars.get(decl))
079: .get(0);
080: assertEquals("b", occ.getImage());
081: }
082:
083: @Test
084: public void testPostfixUsageIsRecorded() {
085: parseCode(TEST3);
086: List nodes = acu
087: .findChildrenOfType(ASTVariableDeclaratorId.class);
088: ASTVariableDeclaratorId node = (ASTVariableDeclaratorId) nodes
089: .get(0);
090: Map vars = node.getScope().getVariableDeclarations();
091: NameDeclaration decl = (NameDeclaration) vars.keySet()
092: .iterator().next();
093: List usages = (List) vars.get(decl);
094: NameOccurrence occ = (NameOccurrence) usages.get(0);
095: assertEquals(4, occ.getLocation().getBeginLine());
096: }
097:
098: @Test
099: public void testLocalVariableTypesAreRecorded() {
100: parseCode(TEST1);
101: List nodes = acu
102: .findChildrenOfType(ASTVariableDeclaratorId.class);
103: Map vars = ((ASTVariableDeclaratorId) nodes.get(0)).getScope()
104: .getVariableDeclarations();
105: VariableNameDeclaration decl = (VariableNameDeclaration) vars
106: .keySet().iterator().next();
107: assertEquals("Bar", decl.getTypeImage());
108: }
109:
110: @Test
111: public void testMethodArgumentTypesAreRecorded() {
112: parseCode(TEST5);
113: List nodes = acu.findChildrenOfType(ASTFormalParameter.class);
114: Map vars = ((ASTFormalParameter) nodes.get(0)).getScope()
115: .getVariableDeclarations();
116: VariableNameDeclaration decl = (VariableNameDeclaration) vars
117: .keySet().iterator().next();
118: assertEquals("String", decl.getTypeImage());
119: }
120:
121: @Test
122: public void testgetEnclosingMethodScope() {
123: parseCode(TEST4);
124: ASTLocalVariableDeclaration node = acu.findChildrenOfType(
125: ASTLocalVariableDeclaration.class).get(0);
126: LocalScope scope = (LocalScope) node.getScope();
127: MethodScope ms = scope.getEnclosingMethodScope();
128: assertEquals(2, ms.getVariableDeclarations().size());
129: }
130:
131: public static final String TEST1 = "public class Foo {" + PMD.EOL
132: + " void foo() {" + PMD.EOL + " Bar b = new Bar();"
133: + PMD.EOL + " }" + PMD.EOL + "}";
134:
135: public static final String TEST2 = "public class Foo {" + PMD.EOL
136: + " void foo() {" + PMD.EOL + " Bar b = new Bar();"
137: + PMD.EOL + " b.buz = 2;" + PMD.EOL + " }" + PMD.EOL + "}";
138:
139: public static final String TEST3 = "public class Foo {" + PMD.EOL
140: + " void foo() {" + PMD.EOL + " int x = 2;" + PMD.EOL
141: + " x++;" + PMD.EOL + " }" + PMD.EOL + "}";
142:
143: public static final String TEST4 = "public class Foo {" + PMD.EOL
144: + " void foo(String x, String z) { int y; }" + PMD.EOL
145: + "}";
146:
147: public static final String TEST5 = "public class Foo {" + PMD.EOL
148: + " void foo(String x);" + PMD.EOL + "}";
149:
150: public static junit.framework.Test suite() {
151: return new junit.framework.JUnit4TestAdapter(
152: LocalScopeTest.class);
153: }
154: }
|