View Javadoc

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