KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > net > sourceforge > pmd > symboltable > LocalScopeTest


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