KickJava   Java API By Example, From Geeks To Geeks.

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


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.ASTPrimaryExpression;
8 import net.sourceforge.pmd.symboltable.NameFinder;
9 import net.sourceforge.pmd.symboltable.NameOccurrence;
10
11 import java.util.List JavaDoc;
12
13 public class NameOccurrencesTest extends STBBaseTst {
14
15     public void testSuper() {
16         parseCode(TEST1);
17         List JavaDoc nodes = acu.findChildrenOfType(ASTPrimaryExpression.class);
18         NameFinder occs = new NameFinder((ASTPrimaryExpression) nodes.get(0));
19         assertEquals("super", ((NameOccurrence) occs.getNames().get(0)).getImage());
20     }
21
22     public void testThis() {
23         parseCode(TEST2);
24         List JavaDoc nodes = acu.findChildrenOfType(ASTPrimaryExpression.class);
25         NameFinder occs = new NameFinder((ASTPrimaryExpression) nodes.get(0));
26         assertEquals("this", ((NameOccurrence) occs.getNames().get(0)).getImage());
27         assertEquals("x", ((NameOccurrence) occs.getNames().get(1)).getImage());
28     }
29
30     public void testNameLinkage() {
31         parseCode(TEST2);
32         List JavaDoc nodes = acu.findChildrenOfType(ASTPrimaryExpression.class);
33         NameFinder occs = new NameFinder((ASTPrimaryExpression) nodes.get(0));
34         NameOccurrence thisNameOccurrence = (NameOccurrence) occs.getNames().get(0);
35         assertEquals(thisNameOccurrence.getNameForWhichThisIsAQualifier(), occs.getNames().get(1));
36     }
37
38     public void testSimpleVariableOccurrence() {
39         parseCode(TEST3);
40         List JavaDoc nodes = acu.findChildrenOfType(ASTPrimaryExpression.class);
41         NameFinder occs = new NameFinder((ASTPrimaryExpression) nodes.get(0));
42         assertEquals("x", ((NameOccurrence) occs.getNames().get(0)).getImage());
43         assertFalse(((NameOccurrence) occs.getNames().get(0)).isThisOrSuper());
44         assertFalse(((NameOccurrence) occs.getNames().get(0)).isMethodOrConstructorInvocation());
45         assertTrue(((NameOccurrence) occs.getNames().get(0)).isOnLeftHandSide());
46     }
47
48     public void testQualifiedOccurrence() {
49         parseCode(TEST4);
50         List JavaDoc nodes = acu.findChildrenOfType(ASTPrimaryExpression.class);
51         NameFinder occs = new NameFinder((ASTPrimaryExpression) nodes.get(0));
52         assertEquals("b", ((NameOccurrence) occs.getNames().get(0)).getImage());
53         assertEquals("x", ((NameOccurrence) occs.getNames().get(1)).getImage());
54     }
55
56     public static final String JavaDoc TEST1 =
57             "public class Foo {" + PMD.EOL +
58             " void foo() {" + PMD.EOL +
59             " super.x = 2;" + PMD.EOL +
60             " }" + PMD.EOL +
61             "}";
62
63     public static final String JavaDoc TEST2 =
64             "public class Foo {" + PMD.EOL +
65             " void foo() {" + PMD.EOL +
66             " this.x = 2;" + PMD.EOL +
67             " }" + PMD.EOL +
68             "}";
69
70     public static final String JavaDoc TEST3 =
71             "public class Foo {" + PMD.EOL +
72             " void foo() {" + PMD.EOL +
73             " x = 2;" + PMD.EOL +
74             " }" + PMD.EOL +
75             "}";
76
77     public static final String JavaDoc TEST4 =
78             "public class Foo {" + PMD.EOL +
79             " void foo() {" + PMD.EOL +
80             " b.x = 2;" + PMD.EOL +
81             " }" + PMD.EOL +
82             "}";
83 }
84
Popular Tags