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 net.sourceforge.pmd.PMD;
10 import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
11 import net.sourceforge.pmd.lang.java.symboltable.NameFinder;
12 import net.sourceforge.pmd.lang.java.symboltable.NameOccurrence;
13
14 import org.junit.Test;
15
16 import java.util.List;
17 public class NameOccurrencesTest extends STBBaseTst {
18
19 @Test
20 public void testSuper() {
21 parseCode(TEST1);
22 List<ASTPrimaryExpression> nodes = acu.findDescendantsOfType(ASTPrimaryExpression.class);
23 NameFinder occs = new NameFinder(nodes.get(0));
24 assertEquals("super", occs.getNames().get(0).getImage());
25 }
26
27 @Test
28 public void testThis() {
29 parseCode(TEST2);
30 List<ASTPrimaryExpression> nodes = acu.findDescendantsOfType(ASTPrimaryExpression.class);
31 NameFinder occs = new NameFinder(nodes.get(0));
32 assertEquals("this", occs.getNames().get(0).getImage());
33 assertEquals("x", occs.getNames().get(1).getImage());
34 }
35
36 @Test
37 public void testNameLinkage() {
38 parseCode(TEST2);
39 List<ASTPrimaryExpression> nodes = acu.findDescendantsOfType(ASTPrimaryExpression.class);
40 NameFinder occs = new NameFinder(nodes.get(0));
41 NameOccurrence thisNameOccurrence = occs.getNames().get(0);
42 assertEquals(thisNameOccurrence.getNameForWhichThisIsAQualifier(), occs.getNames().get(1));
43 }
44
45 @Test
46 public void testSimpleVariableOccurrence() {
47 parseCode(TEST3);
48 List<ASTPrimaryExpression> nodes = acu.findDescendantsOfType(ASTPrimaryExpression.class);
49 NameFinder occs = new NameFinder(nodes.get(0));
50 assertEquals("x", occs.getNames().get(0).getImage());
51 assertFalse(occs.getNames().get(0).isThisOrSuper());
52 assertFalse(occs.getNames().get(0).isMethodOrConstructorInvocation());
53 assertTrue(occs.getNames().get(0).isOnLeftHandSide());
54 }
55
56 @Test
57 public void testQualifiedOccurrence() {
58 parseCode(TEST4);
59 List<ASTPrimaryExpression> nodes = acu.findDescendantsOfType(ASTPrimaryExpression.class);
60 NameFinder occs = new NameFinder(nodes.get(0));
61 assertEquals("b", occs.getNames().get(0).getImage());
62 assertEquals("x", occs.getNames().get(1).getImage());
63 }
64
65 @Test
66 public void testIsSelfAssignment(){
67 parseCode(TEST5);
68 List<ASTPrimaryExpression> nodes = acu.findDescendantsOfType(ASTPrimaryExpression.class);
69 NameFinder occs = new NameFinder(nodes.get(2));
70 assertTrue(occs.getNames().get(0).isSelfAssignment());
71
72 parseCode(TEST6);
73 nodes = acu.findDescendantsOfType(ASTPrimaryExpression.class);
74 occs = new NameFinder(nodes.get(2));
75 assertTrue(occs.getNames().get(0).isSelfAssignment());
76 }
77
78 @Test
79 public void testEnumStaticUsage() {
80 parseCode(TEST_ENUM);
81 List<ASTPrimaryExpression> nodes = acu.findDescendantsOfType(ASTPrimaryExpression.class);
82
83 NameFinder occs = new NameFinder(nodes.get(4));
84 List<NameOccurrence> names = occs.getNames();
85 assertEquals(3, names.size());
86 assertEquals("myEnum", names.get(0).getImage());
87 assertFalse(names.get(0).isMethodOrConstructorInvocation());
88 assertEquals("desc", names.get(1).getImage());
89 assertFalse(names.get(1).isMethodOrConstructorInvocation());
90 assertEquals("equals", names.get(2).getImage());
91 assertTrue(names.get(2).isMethodOrConstructorInvocation());
92 }
93
94 public static final String TEST1 =
95 "public class Foo {" + PMD.EOL +
96 " void foo() {" + PMD.EOL +
97 " super.x = 2;" + PMD.EOL +
98 " }" + PMD.EOL +
99 "}";
100
101 public static final String TEST2 =
102 "public class Foo {" + PMD.EOL +
103 " void foo() {" + PMD.EOL +
104 " this.x = 2;" + PMD.EOL +
105 " }" + PMD.EOL +
106 "}";
107
108 public static final String TEST3 =
109 "public class Foo {" + PMD.EOL +
110 " void foo() {" + PMD.EOL +
111 " x = 2;" + PMD.EOL +
112 " }" + PMD.EOL +
113 "}";
114
115 public static final String TEST4 =
116 "public class Foo {" + PMD.EOL +
117 " void foo() {" + PMD.EOL +
118 " b.x = 2;" + PMD.EOL +
119 " }" + PMD.EOL +
120 "}";
121
122 public static final String TEST5 =
123 "public class Foo{" + PMD.EOL +
124 " private int counter;" + PMD.EOL +
125 " private Foo(){" + PMD.EOL +
126 " counter = 0;" + PMD.EOL +
127 " }" + PMD.EOL +
128 " private int foo(){" + PMD.EOL +
129 " if (++counter < 3) {" + PMD.EOL +
130 " return 0;" + PMD.EOL +
131 " }" + PMD.EOL +
132 " return 1;" + PMD.EOL +
133 " }" + PMD.EOL +
134 "}";
135
136 public static final String TEST6 =
137 "public class Foo{" + PMD.EOL +
138 " private int counter;" + PMD.EOL +
139 " private Foo(){" + PMD.EOL +
140 " counter = 0;" + PMD.EOL +
141 " }" + PMD.EOL +
142 " private int foo(){" + PMD.EOL +
143 " if (++this.counter < 3) {" + PMD.EOL +
144 " return 0;" + PMD.EOL +
145 " }" + PMD.EOL +
146 " return 1;" + PMD.EOL +
147 " }" + PMD.EOL +
148 "}";
149
150 public static final String TEST_ENUM =
151 "public enum MyEnum {" + PMD.EOL +
152 " A(\"a\");" + PMD.EOL +
153 " private final String desc;" + PMD.EOL +
154 " private MyEnum(String desc) {" + PMD.EOL +
155 " this.desc = desc;" + PMD.EOL +
156 " }" + PMD.EOL +
157 " public static MyEnum byDesc(String desc) {" + PMD.EOL +
158 " for (MyEnum myEnum : value()) {" + PMD.EOL +
159 " if (myEnum.desc.equals(desc)) return myEnum;" + PMD.EOL +
160 " }" + PMD.EOL +
161 " return null;" + PMD.EOL +
162 " }" + PMD.EOL +
163 " }";
164
165 public static junit.framework.Test suite() {
166 return new junit.framework.JUnit4TestAdapter(NameOccurrencesTest.class);
167 }
168 }