1
2
3
4 package net.sourceforge.pmd.symboltable;
5
6 import static org.junit.Assert.assertEquals;
7 import static org.junit.Assert.assertFalse;
8
9 import java.util.List;
10 import java.util.Map;
11
12 import net.sourceforge.pmd.PMD;
13 import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter;
14 import net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration;
15 import net.sourceforge.pmd.lang.java.ast.ASTName;
16 import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
17 import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
18 import net.sourceforge.pmd.lang.java.symboltable.LocalScope;
19 import net.sourceforge.pmd.lang.java.symboltable.MethodScope;
20 import net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence;
21 import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
22 import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
23
24 import org.junit.Test;
25 public class LocalScopeTest extends STBBaseTst {
26
27 @Test
28 public void testNameWithThisOrSuperIsNotFlaggedAsUnused() {
29 LocalScope scope = new LocalScope();
30 ASTName name = new ASTName(1);
31 name.setImage("foo");
32 ASTPrimaryPrefix prefix = new ASTPrimaryPrefix(2);
33 prefix.setUsesThisModifier();
34 name.jjtAddChild(prefix, 1);
35 JavaNameOccurrence occ = new JavaNameOccurrence(name, "foo");
36 scope.addNameOccurrence(occ);
37 assertFalse(scope.getDeclarations().keySet().iterator().hasNext());
38 }
39
40 @Test
41 public void testNameWithSuperIsNotFlaggedAsUnused() {
42 LocalScope scope = new LocalScope();
43 ASTName name = new ASTName(1);
44 name.setImage("foo");
45 ASTPrimaryPrefix prefix = new ASTPrimaryPrefix(2);
46 prefix.setUsesSuperModifier();
47 name.jjtAddChild(prefix, 1);
48 JavaNameOccurrence occ = new JavaNameOccurrence(name, "foo");
49 scope.addNameOccurrence(occ);
50 assertFalse(scope.getDeclarations().keySet().iterator().hasNext());
51 }
52
53 @Test
54 public void testLocalVariableDeclarationFound() {
55 parseCode(TEST1);
56 List nodes = acu.findDescendantsOfType(ASTVariableDeclaratorId.class);
57 ASTVariableDeclaratorId node = (ASTVariableDeclaratorId) nodes.get(0);
58 Map vars = node.getScope().getDeclarations();
59 assertEquals(1, vars.size());
60 NameDeclaration decl = (NameDeclaration) vars.keySet().iterator().next();
61 assertEquals("b", decl.getImage());
62 }
63
64 @Test
65 public void testQualifiedNameOccurrence() {
66 parseCode(TEST2);
67 List nodes = acu.findDescendantsOfType(ASTVariableDeclaratorId.class);
68 ASTVariableDeclaratorId node = (ASTVariableDeclaratorId) nodes.get(0);
69 Map vars = node.getScope().getDeclarations();
70 NameDeclaration decl = (NameDeclaration) vars.keySet().iterator().next();
71 JavaNameOccurrence occ = (JavaNameOccurrence) ((List) vars.get(decl)).get(0);
72 assertEquals("b", occ.getImage());
73 }
74
75 @Test
76 public void testPostfixUsageIsRecorded() {
77 parseCode(TEST3);
78 List nodes = acu.findDescendantsOfType(ASTVariableDeclaratorId.class);
79 ASTVariableDeclaratorId node = (ASTVariableDeclaratorId) nodes.get(0);
80 Map vars = node.getScope().getDeclarations();
81 NameDeclaration decl = (NameDeclaration) vars.keySet().iterator().next();
82 List usages = (List) vars.get(decl);
83 JavaNameOccurrence occ = (JavaNameOccurrence) usages.get(0);
84 assertEquals(4, occ.getLocation().getBeginLine());
85 }
86
87 @Test
88 public void testLocalVariableTypesAreRecorded() {
89 parseCode(TEST1);
90 List nodes = acu.findDescendantsOfType(ASTVariableDeclaratorId.class);
91 Map vars = ((ASTVariableDeclaratorId) nodes.get(0)).getScope().getDeclarations();
92 VariableNameDeclaration decl = (VariableNameDeclaration) vars.keySet().iterator().next();
93 assertEquals("Bar", decl.getTypeImage());
94 }
95
96 @Test
97 public void testMethodArgumentTypesAreRecorded() {
98 parseCode(TEST5);
99 List nodes = acu.findDescendantsOfType(ASTFormalParameter.class);
100 Map vars = ((ASTFormalParameter) nodes.get(0)).getScope().getDeclarations();
101 VariableNameDeclaration decl = (VariableNameDeclaration) vars.keySet().iterator().next();
102 assertEquals("String", decl.getTypeImage());
103 }
104
105 @Test
106 public void testgetEnclosingMethodScope() {
107 parseCode(TEST4);
108 ASTLocalVariableDeclaration node = acu.findDescendantsOfType(ASTLocalVariableDeclaration.class).get(0);
109 LocalScope scope = (LocalScope) node.getScope();
110 MethodScope ms = scope.getEnclosingScope(MethodScope.class);
111 assertEquals(2, ms.getDeclarations().size());
112 }
113
114
115 public static final String TEST1 =
116 "public class Foo {" + PMD.EOL +
117 " void foo() {" + PMD.EOL +
118 " Bar b = new Bar();" + PMD.EOL +
119 " }" + PMD.EOL +
120 "}";
121
122 public static final String TEST2 =
123 "public class Foo {" + PMD.EOL +
124 " void foo() {" + PMD.EOL +
125 " Bar b = new Bar();" + PMD.EOL +
126 " b.buz = 2;" + PMD.EOL +
127 " }" + PMD.EOL +
128 "}";
129
130 public static final String TEST3 =
131 "public class Foo {" + PMD.EOL +
132 " void foo() {" + PMD.EOL +
133 " int x = 2;" + PMD.EOL +
134 " x++;" + PMD.EOL +
135 " }" + PMD.EOL +
136 "}";
137
138 public static final String TEST4 =
139 "public class Foo {" + PMD.EOL +
140 " void foo(String x, String z) { int y; }" + PMD.EOL +
141 "}";
142
143 public static final String TEST5 =
144 "public class Foo {" + PMD.EOL +
145 " void foo(String x);" + PMD.EOL +
146 "}";
147
148 public static junit.framework.Test suite() {
149 return new junit.framework.JUnit4TestAdapter(LocalScopeTest.class);
150 }
151 }