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 static org.junit.Assert.assertTrue;
008: import net.sourceforge.pmd.PMD;
009: import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
010: import net.sourceforge.pmd.ast.ASTMethodDeclaration;
011: import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
012: import net.sourceforge.pmd.ast.SimpleJavaNode;
013: import net.sourceforge.pmd.ast.SimpleNode;
014: import net.sourceforge.pmd.symboltable.ClassNameDeclaration;
015: import net.sourceforge.pmd.symboltable.ClassScope;
016: import net.sourceforge.pmd.symboltable.MethodNameDeclaration;
017: import net.sourceforge.pmd.symboltable.NameOccurrence;
018: import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
019:
020: import org.junit.Test;
021:
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025:
026: public class ClassScopeTest extends STBBaseTst {
027:
028: @Test
029: public void testEnumsClassScope() {
030: parseCode15(ENUM_SCOPE);
031: }
032:
033: // FIXME - these will break when this goes from Anonymous$1 to Foo$1
034: @Test
035: public void testAnonymousInnerClassName() {
036: ClassScope s = new ClassScope();
037: assertEquals("Anonymous$1", s.getClassName());
038: s = new ClassScope();
039: assertEquals("Anonymous$2", s.getClassName());
040: }
041:
042: @Test
043: public void testContains() {
044: ClassScope s = new ClassScope("Foo");
045: ASTVariableDeclaratorId node = new ASTVariableDeclaratorId(1);
046: node.setImage("bar");
047: s.addDeclaration(new VariableNameDeclaration(node));
048: assertTrue(s.getVariableDeclarations().keySet().iterator()
049: .hasNext());
050: }
051:
052: @Test
053: public void testCantContainsSuperToString() {
054: ClassScope s = new ClassScope("Foo");
055: SimpleNode node = new SimpleJavaNode(1);
056: node.setImage("super.toString");
057: assertFalse(s
058: .contains(new NameOccurrence(node, node.getImage())));
059: }
060:
061: @Test
062: public void testContainsStaticVariablePrefixedWithClassName() {
063: ClassScope s = new ClassScope("Foo");
064: ASTVariableDeclaratorId node = new ASTVariableDeclaratorId(1);
065: node.setImage("X");
066: s.addDeclaration(new VariableNameDeclaration(node));
067:
068: SimpleNode node2 = new SimpleJavaNode(2);
069: node2.setImage("Foo.X");
070: assertTrue(s.contains(new NameOccurrence(node2, node2
071: .getImage())));
072: }
073:
074: @Test
075: public void testClassName() {
076: parseCode(CLASS_NAME);
077: ASTClassOrInterfaceDeclaration n = acu.findChildrenOfType(
078: ASTClassOrInterfaceDeclaration.class).get(0);
079: assertEquals("Foo", n.getScope().getEnclosingClassScope()
080: .getClassName());
081: }
082:
083: @Test
084: public void testMethodDeclarationRecorded() {
085: parseCode(METHOD_DECLARATIONS_RECORDED);
086: ASTClassOrInterfaceDeclaration n = acu.findChildrenOfType(
087: ASTClassOrInterfaceDeclaration.class).get(0);
088: ClassScope s = (ClassScope) n.getScope();
089: Map m = s.getMethodDeclarations();
090: assertEquals(1, m.size());
091: MethodNameDeclaration mnd = (MethodNameDeclaration) m.keySet()
092: .iterator().next();
093: assertEquals("bar", mnd.getImage());
094: ASTMethodDeclaration node = (ASTMethodDeclaration) mnd
095: .getNode().jjtGetParent();
096: assertTrue(node.isPrivate());
097: }
098:
099: @Test
100: public void testTwoMethodsSameNameDiffArgs() {
101: // TODO this won't work with String and java.lang.String
102: parseCode(METHODS_WITH_DIFF_ARG);
103: ASTClassOrInterfaceDeclaration n = acu.findChildrenOfType(
104: ASTClassOrInterfaceDeclaration.class).get(0);
105: Map m = ((ClassScope) n.getScope()).getMethodDeclarations();
106: assertEquals(2, m.size());
107: Iterator i = m.keySet().iterator();
108: MethodNameDeclaration mnd = (MethodNameDeclaration) i.next();
109: assertEquals("bar", mnd.getImage());
110: assertEquals("bar", ((MethodNameDeclaration) i.next())
111: .getImage());
112: }
113:
114: @Test
115: public final void testOneParams() throws Throwable {
116: parseCode(ONE_PARAM);
117: ASTClassOrInterfaceDeclaration n = acu.findChildrenOfType(
118: ASTClassOrInterfaceDeclaration.class).get(0);
119: Map m = ((ClassScope) n.getScope()).getMethodDeclarations();
120: MethodNameDeclaration mnd = (MethodNameDeclaration) m.keySet()
121: .iterator().next();
122: assertEquals("(String)", mnd.getParameterDisplaySignature());
123: }
124:
125: @Test
126: public final void testTwoParams() throws Throwable {
127: parseCode(TWO_PARAMS);
128: ASTClassOrInterfaceDeclaration n = acu.findChildrenOfType(
129: ASTClassOrInterfaceDeclaration.class).get(0);
130: Map m = ((ClassScope) n.getScope()).getMethodDeclarations();
131: MethodNameDeclaration mnd = (MethodNameDeclaration) m.keySet()
132: .iterator().next();
133: assertEquals("(String,int)", mnd.getParameterDisplaySignature());
134: }
135:
136: @Test
137: public final void testNoParams() throws Throwable {
138: parseCode(NO_PARAMS);
139: ASTClassOrInterfaceDeclaration n = acu.findChildrenOfType(
140: ASTClassOrInterfaceDeclaration.class).get(0);
141: Map m = ((ClassScope) n.getScope()).getMethodDeclarations();
142: MethodNameDeclaration mnd = (MethodNameDeclaration) m.keySet()
143: .iterator().next();
144: assertEquals("()", mnd.getParameterDisplaySignature());
145: }
146:
147: @Test
148: public final void testNestedClassDeclFound() throws Throwable {
149: parseCode(NESTED_CLASS_FOUND);
150: ASTClassOrInterfaceDeclaration n = acu.findChildrenOfType(
151: ASTClassOrInterfaceDeclaration.class).get(0);
152: ClassScope c = (ClassScope) n.getScope();
153: Map m = c.getClassDeclarations();
154: ClassNameDeclaration cnd = (ClassNameDeclaration) m.keySet()
155: .iterator().next();
156: assertEquals("Buz", cnd.getImage());
157: }
158:
159: @Test
160: public final void testbuz() throws Throwable {
161: parseCode(METH);
162: //SymbolTableViewer st = new SymbolTableViewer();
163: //acu.jjtAccept(st, null);
164: }
165:
166: @Test
167: public void testMethodUsageSeen() {
168: parseCode(METHOD_USAGE_SEEN);
169: ASTClassOrInterfaceDeclaration n = acu.findChildrenOfType(
170: ASTClassOrInterfaceDeclaration.class).get(0);
171: Map m = ((ClassScope) n.getScope()).getMethodDeclarations();
172: Iterator i = m.entrySet().iterator();
173: MethodNameDeclaration mnd;
174: Map.Entry entry;
175:
176: do {
177: entry = (Map.Entry) i.next();
178: mnd = (MethodNameDeclaration) entry.getKey();
179: } while (!mnd.getImage().equals("bar"));
180:
181: List usages = (List) entry.getValue();
182: assertEquals(1, usages.size());
183: assertEquals("bar", ((NameOccurrence) usages.get(0)).getImage());
184: }
185:
186: @Test
187: public void testMethodUsageSeenWithThis() {
188: parseCode(METHOD_USAGE_SEEN_WITH_THIS);
189: ASTClassOrInterfaceDeclaration n = acu.findChildrenOfType(
190: ASTClassOrInterfaceDeclaration.class).get(0);
191: Map m = ((ClassScope) n.getScope()).getMethodDeclarations();
192: Iterator i = m.entrySet().iterator();
193: MethodNameDeclaration mnd;
194: Map.Entry entry;
195:
196: do {
197: entry = (Map.Entry) i.next();
198: mnd = (MethodNameDeclaration) entry.getKey();
199: } while (!mnd.getImage().equals("bar"));
200:
201: List usages = (List) entry.getValue();
202: assertEquals(1, usages.size());
203: assertEquals("bar", ((NameOccurrence) usages.get(0)).getImage());
204: }
205:
206: @Test
207: public void testMethodUsageSeen2() {
208: parseCode(METHOD_USAGE_SEEN2);
209: ASTClassOrInterfaceDeclaration n = acu.findChildrenOfType(
210: ASTClassOrInterfaceDeclaration.class).get(0);
211: Map m = ((ClassScope) n.getScope()).getMethodDeclarations();
212: Iterator i = m.entrySet().iterator();
213: Map.Entry entry = (Map.Entry) i.next();
214: MethodNameDeclaration mnd = (MethodNameDeclaration) entry
215: .getKey();
216: if (mnd.getNode().getBeginLine() == 2) {
217: List usages = (List) entry.getValue();
218: System.out.println(usages.size());
219: System.out.println(mnd);
220: mnd = (MethodNameDeclaration) i.next();
221: }
222: }
223:
224: private static final String METHOD_USAGE_SEEN2 = "public class Foo {"
225: + PMD.EOL
226: + " public void baz() {"
227: + PMD.EOL
228: + " baz(x, y);"
229: + PMD.EOL
230: + " }"
231: + PMD.EOL
232: + " private void baz(int x, int y) {}" + PMD.EOL + "}";
233:
234: private static final String METHOD_USAGE_SEEN = "public class Foo {"
235: + PMD.EOL
236: + " private void bar() {}"
237: + PMD.EOL
238: + " public void buz() {"
239: + PMD.EOL
240: + " bar();"
241: + PMD.EOL
242: + " }" + PMD.EOL + "}";
243:
244: private static final String METHOD_USAGE_SEEN_WITH_THIS = "public class Foo {"
245: + PMD.EOL
246: + " private void bar() {}"
247: + PMD.EOL
248: + " public void buz() {"
249: + PMD.EOL
250: + " this.bar();"
251: + PMD.EOL + " }" + PMD.EOL + "}";
252:
253: private static final String METH = "public class Test {" + PMD.EOL
254: + " static { " + PMD.EOL + " int y; " + PMD.EOL + " } "
255: + PMD.EOL + " void bar(int x) {} " + PMD.EOL
256: + " void baz(int x) {} " + PMD.EOL + "}";
257:
258: private static final String NESTED_CLASS_FOUND = "public class Test {"
259: + PMD.EOL + " private class Buz {} " + PMD.EOL + "}";
260:
261: private static final String ONE_PARAM = "public class Test {"
262: + PMD.EOL + " void bar(String x) {" + PMD.EOL + " }"
263: + PMD.EOL + "}";
264:
265: private static final String TWO_PARAMS = "public class Test {"
266: + PMD.EOL + " void bar(String x, int y) {" + PMD.EOL
267: + " }" + PMD.EOL + "}";
268:
269: private static final String NO_PARAMS = "public class Test {"
270: + PMD.EOL + " void bar() {" + PMD.EOL + " }" + PMD.EOL
271: + "}";
272:
273: private static final String CLASS_NAME = "public class Foo {}";
274:
275: private static final String METHOD_DECLARATIONS_RECORDED = "public class Foo {"
276: + PMD.EOL + " private void bar() {}" + PMD.EOL + "}";
277:
278: private static final String METHODS_WITH_DIFF_ARG = "public class Foo {"
279: + PMD.EOL
280: + " private void bar(String x) {}"
281: + PMD.EOL
282: + " private void bar() {}" + PMD.EOL + "}";
283:
284: private static final String ENUM_SCOPE = "public enum Foo {"
285: + PMD.EOL + " HEAP(\"foo\");" + PMD.EOL
286: + " private final String fuz;" + PMD.EOL
287: + " public String getFuz() {" + PMD.EOL + " return fuz;"
288: + PMD.EOL + " }" + PMD.EOL + "}";
289:
290: public static junit.framework.Test suite() {
291: return new junit.framework.JUnit4TestAdapter(
292: ClassScopeTest.class);
293: }
294: }
|