1 /***
2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 */
4 package test.net.sourceforge.pmd.symboltable;
5
6 import net.sourceforge.pmd.PMD;
7 import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
8 import net.sourceforge.pmd.ast.SimpleNode;
9 import net.sourceforge.pmd.symboltable.NameOccurrence;
10
11 public class AcceptanceTest extends STBBaseTst {
12
13 /*
14 public void testClashingSymbols() {
15 parseCode(TEST1);
16 }
17
18 public void testInitializer() {
19 parseCode(TEST_INITIALIZERS);
20 ASTInitializer a = (ASTInitializer)(acu.findChildrenOfType(ASTInitializer.class)).get(0);
21 assertFalse(a.isStatic());
22 a = (ASTInitializer)(acu.findChildrenOfType(ASTInitializer.class)).get(1);
23 assertTrue(a.isStatic());
24 }
25
26 public void testCatchBlocks() {
27 parseCode(TEST_CATCH_BLOCKS);
28 ASTCatchStatement c = (ASTCatchStatement)(acu.findChildrenOfType(ASTCatchStatement.class)).get(0);
29 ASTBlock a = (ASTBlock)(c.findChildrenOfType(ASTBlock.class)).get(0);
30 Scope s = a.getScope();
31 Map vars = s.getParent().getVariableDeclarations();
32 assertEquals(1, vars.size());
33 VariableNameDeclaration v = (VariableNameDeclaration)vars.keySet().iterator().next();
34 assertEquals("e", v.getImage());
35 assertEquals(1, ((List)vars.get(v)).size());
36 }
37
38 public void testEq() {
39 parseCode(TEST_EQ);
40 ASTEqualityExpression e = (ASTEqualityExpression)(acu.findChildrenOfType(ASTEqualityExpression.class)).get(0);
41 ASTMethodDeclaration method = (ASTMethodDeclaration)e.getFirstParentOfType(ASTMethodDeclaration.class);
42 Scope s = method.getScope();
43 Map m = s.getVariableDeclarations();
44 for (Iterator i = m.keySet().iterator(); i.hasNext();) {
45 VariableNameDeclaration vnd = (VariableNameDeclaration)i.next();
46 SimpleNode node = vnd.getNode();
47 //System.out.println();
48 }
49 //System.out.println(m.size());
50 }
51 */
52
53 public void testFieldFinder() {
54 System.out.println(TEST_FIELD);
55 parseCode(TEST_FIELD);
56 ASTVariableDeclaratorId declaration = (ASTVariableDeclaratorId)acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0);
57 NameOccurrence no = (NameOccurrence)declaration.getUsages().iterator().next();
58 SimpleNode location = no.getLocation();
59 System.out.println("variable " + declaration.getImage() + " is used here: " + location.getImage());
60 }
61
62 /*
63 public void testDemo() {
64 parseCode(TEST_DEMO);
65 System.out.println(TEST_DEMO);
66 ASTMethodDeclaration node = (ASTMethodDeclaration) acu.findChildrenOfType(ASTMethodDeclaration.class).get(0);
67 Scope s = node.getScope();
68 Map m = s.getVariableDeclarations();
69 for (Iterator i = m.keySet().iterator(); i.hasNext();) {
70 VariableNameDeclaration d = (VariableNameDeclaration) i.next();
71 System.out.println("Variable: " + d.getImage());
72 System.out.println("Type: " + d.getTypeImage());
73 }
74 }
75 */
76 /*
77 List u = (List)m.get(d);
78 System.out.println("Usages: " + u.size());
79 NameOccurrence o = (NameOccurrence)u.get(0);
80 int beginLine = o.getLocation().getBeginLine();
81 System.out.println("Used in line " + beginLine);
82 */
83
84 private static final String TEST_DEMO =
85 "public class Foo {" + PMD.EOL +
86 " void bar(ArrayList buz) { " + PMD.EOL +
87 " } " + PMD.EOL +
88 "}" + PMD.EOL;
89
90 private static final String TEST_EQ =
91 "public class Foo {" + PMD.EOL +
92 " boolean foo(String a, String b) { " + PMD.EOL +
93 " return a == b; " + PMD.EOL +
94 " } " + PMD.EOL +
95 "}" + PMD.EOL;
96
97 private static final String TEST1 =
98 "import java.io.*;" + PMD.EOL +
99 "public class Foo {" + PMD.EOL +
100 " void buz( ) {" + PMD.EOL +
101 " Object o = new Serializable() { int x; };" + PMD.EOL +
102 " Object o1 = new Serializable() { int x; };" + PMD.EOL +
103 " }" + PMD.EOL +
104 "}" + PMD.EOL;
105
106 private static final String TEST_INITIALIZERS =
107 "public class Foo {" + PMD.EOL +
108 " {} " + PMD.EOL +
109 " static {} " + PMD.EOL +
110 "}" + PMD.EOL;
111
112 private static final String TEST_CATCH_BLOCKS =
113 "public class Foo {" + PMD.EOL +
114 " void foo() { " + PMD.EOL +
115 " try { " + PMD.EOL +
116 " } catch (Exception e) { " + PMD.EOL +
117 " e.printStackTrace(); " + PMD.EOL +
118 " } " + PMD.EOL +
119 " } " + PMD.EOL +
120 "}" + PMD.EOL;
121
122 private static final String TEST_FIELD =
123 "public class MyClass {" + PMD.EOL +
124 " private int a; " + PMD.EOL +
125 " boolean b = MyClass.ASCENDING; " + PMD.EOL +
126 "}" + PMD.EOL;
127
128
129 }