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