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 import static org.junit.Assert.assertTrue;
9 import static org.junit.Assert.fail;
10
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Map;
14
15 import net.sourceforge.pmd.PMD;
16 import net.sourceforge.pmd.lang.java.ast.ASTBlock;
17 import net.sourceforge.pmd.lang.java.ast.ASTCatchStatement;
18 import net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression;
19 import net.sourceforge.pmd.lang.java.ast.ASTInitializer;
20 import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
21 import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
22 import net.sourceforge.pmd.lang.java.ast.JavaNode;
23 import net.sourceforge.pmd.lang.java.symboltable.NameOccurrence;
24 import net.sourceforge.pmd.lang.java.symboltable.Scope;
25 import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
26
27 import org.junit.Test;
28 public class AcceptanceTest extends STBBaseTst {
29
30 @Test
31 public void testClashingSymbols() {
32 parseCode(TEST1);
33 }
34
35 @Test
36 public void testInitializer() {
37 parseCode(TEST_INITIALIZERS);
38 ASTInitializer a = acu.findDescendantsOfType(ASTInitializer.class).get(0);
39 assertFalse(a.isStatic());
40 a = acu.findDescendantsOfType(ASTInitializer.class).get(1);
41 assertTrue(a.isStatic());
42 }
43
44 @Test
45 public void testCatchBlocks() {
46 parseCode(TEST_CATCH_BLOCKS);
47 ASTCatchStatement c = acu.findDescendantsOfType(ASTCatchStatement.class).get(0);
48 ASTBlock a = c.findDescendantsOfType(ASTBlock.class).get(0);
49 Scope s = a.getScope();
50 Map<VariableNameDeclaration, List<NameOccurrence>> vars = s.getParent().getVariableDeclarations();
51 assertEquals(1, vars.size());
52 VariableNameDeclaration v = vars.keySet().iterator().next();
53 assertEquals("e", v.getImage());
54 assertEquals(1, (vars.get(v)).size());
55 }
56
57 @Test
58 public void testEq() {
59 parseCode(TEST_EQ);
60 ASTEqualityExpression e = acu.findDescendantsOfType(ASTEqualityExpression.class).get(0);
61 ASTMethodDeclaration method = e.getFirstParentOfType(ASTMethodDeclaration.class);
62 Scope s = method.getScope();
63 Map<VariableNameDeclaration, List<NameOccurrence>> m = s.getVariableDeclarations();
64 assertEquals(2, m.size());
65 for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : m.entrySet()) {
66 VariableNameDeclaration vnd = entry.getKey();
67 List<NameOccurrence> usages = entry.getValue();
68
69 if (vnd.getImage().equals("a") || vnd.getImage().equals("b")) {
70 assertEquals(1, usages.size());
71 assertEquals(3, usages.get(0).getLocation().getBeginLine());
72 } else {
73 fail("Unkown variable " + vnd);
74 }
75 }
76 }
77
78 @Test
79 public void testFieldFinder() {
80 parseCode(TEST_FIELD);
81
82
83 ASTVariableDeclaratorId declaration = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(1);
84 assertEquals(3, declaration.getBeginLine());
85 assertEquals("bbbbbbbbbb", declaration.getImage());
86 assertEquals(1, declaration.getUsages().size());
87 NameOccurrence no = declaration.getUsages().get(0);
88 JavaNode location = no.getLocation();
89 assertEquals(6, location.getBeginLine());
90
91 }
92
93 @Test
94 public void testDemo() {
95 parseCode(TEST_DEMO);
96
97 ASTMethodDeclaration node = acu.findDescendantsOfType(ASTMethodDeclaration.class).get(0);
98 Scope s = node.getScope();
99 Map<VariableNameDeclaration, List<NameOccurrence>> m = s.getVariableDeclarations();
100 for (Iterator<VariableNameDeclaration> i = m.keySet().iterator(); i.hasNext();) {
101 VariableNameDeclaration d = i.next();
102 assertEquals("buz", d.getImage());
103 assertEquals("ArrayList", d.getTypeImage());
104 List<NameOccurrence> u = m.get(d);
105 assertEquals(1, u.size());
106 NameOccurrence o = u.get(0);
107 int beginLine = o.getLocation().getBeginLine();
108 assertEquals(3, beginLine);
109
110
111
112
113
114 }
115 }
116
117 @Test
118 public void testEnum() {
119 parseCode(NameOccurrencesTest.TEST_ENUM);
120
121 ASTVariableDeclaratorId vdi = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(0);
122 List<NameOccurrence> usages = vdi.getUsages();
123 assertEquals(2, usages.size());
124 assertEquals(5, usages.get(0).getLocation().getBeginLine());
125 assertEquals(9, usages.get(1).getLocation().getBeginLine());
126 }
127
128 @Test
129 public void testInnerOuterClass() {
130 parseCode(TEST_INNER_CLASS);
131 ASTVariableDeclaratorId vdi = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(0);
132 List<NameOccurrence> usages = vdi.getUsages();
133 assertEquals(2, usages.size());
134 assertEquals(5, usages.get(0).getLocation().getBeginLine());
135 assertEquals(10, usages.get(1).getLocation().getBeginLine());
136 }
137
138 private static final String TEST_DEMO =
139 "public class Foo {" + PMD.EOL +
140 " void bar(ArrayList buz) { " + PMD.EOL +
141 " buz.add(\"foo\");" + PMD.EOL +
142 " } " + PMD.EOL +
143 "}" + PMD.EOL;
144
145 private static final String TEST_EQ =
146 "public class Foo {" + PMD.EOL +
147 " boolean foo(String a, String b) { " + PMD.EOL +
148 " return a == b; " + PMD.EOL +
149 " } " + PMD.EOL +
150 "}" + PMD.EOL;
151
152 private static final String TEST1 =
153 "import java.io.*;" + PMD.EOL +
154 "public class Foo {" + PMD.EOL +
155 " void buz( ) {" + PMD.EOL +
156 " Object o = new Serializable() { int x; };" + PMD.EOL +
157 " Object o1 = new Serializable() { int x; };" + PMD.EOL +
158 " }" + PMD.EOL +
159 "}" + PMD.EOL;
160
161 private static final String TEST_INITIALIZERS =
162 "public class Foo {" + PMD.EOL +
163 " {} " + PMD.EOL +
164 " static {} " + PMD.EOL +
165 "}" + PMD.EOL;
166
167 private static final String TEST_CATCH_BLOCKS =
168 "public class Foo {" + PMD.EOL +
169 " void foo() { " + PMD.EOL +
170 " try { " + PMD.EOL +
171 " } catch (Exception e) { " + PMD.EOL +
172 " e.printStackTrace(); " + PMD.EOL +
173 " } " + PMD.EOL +
174 " } " + PMD.EOL +
175 "}" + PMD.EOL;
176
177 private static final String TEST_FIELD =
178 "public class MyClass {" + PMD.EOL +
179 " private int aaaaaaaaaa; " + PMD.EOL +
180 " boolean bbbbbbbbbb = MyClass.ASCENDING; " + PMD.EOL +
181 " private int zzzzzzzzzz;" + PMD.EOL +
182 " private void doIt() {" + PMD.EOL +
183 " if (bbbbbbbbbb) {" + PMD.EOL +
184 " }" + PMD.EOL +
185 " }" + PMD.EOL +
186 "}" + PMD.EOL;
187
188 public static final String TEST_INNER_CLASS =
189 "public class Outer {" + PMD.EOL +
190 " private static class Inner {" + PMD.EOL +
191 " private int i;" + PMD.EOL +
192 " private Inner(int i) {" + PMD.EOL +
193 " this.i = i;" + PMD.EOL +
194 " }" + PMD.EOL +
195 " }" + PMD.EOL +
196 " public int modify(int i) {" + PMD.EOL +
197 " Inner in = new Inner(i);" + PMD.EOL +
198 " return in.i;" + PMD.EOL +
199 " }" + PMD.EOL +
200 "}" + PMD.EOL;
201
202 public static junit.framework.Test suite() {
203 return new junit.framework.JUnit4TestAdapter(AcceptanceTest.class);
204 }
205 }