001: /**
002: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003: */package test.net.sourceforge.pmd.symboltable;
004:
005: import static org.junit.Assert.assertEquals;
006: import static org.junit.Assert.assertFalse;
007: import static org.junit.Assert.assertTrue;
008: import net.sourceforge.pmd.PMD;
009: import net.sourceforge.pmd.ast.ASTPrimaryExpression;
010: import net.sourceforge.pmd.symboltable.NameFinder;
011: import net.sourceforge.pmd.symboltable.NameOccurrence;
012:
013: import org.junit.Test;
014:
015: import java.util.List;
016:
017: public class NameOccurrencesTest extends STBBaseTst {
018:
019: @Test
020: public void testSuper() {
021: parseCode(TEST1);
022: List nodes = acu.findChildrenOfType(ASTPrimaryExpression.class);
023: NameFinder occs = new NameFinder((ASTPrimaryExpression) nodes
024: .get(0));
025: assertEquals("super", occs.getNames().get(0).getImage());
026: }
027:
028: @Test
029: public void testThis() {
030: parseCode(TEST2);
031: List nodes = acu.findChildrenOfType(ASTPrimaryExpression.class);
032: NameFinder occs = new NameFinder((ASTPrimaryExpression) nodes
033: .get(0));
034: assertEquals("this", occs.getNames().get(0).getImage());
035: assertEquals("x", occs.getNames().get(1).getImage());
036: }
037:
038: @Test
039: public void testNameLinkage() {
040: parseCode(TEST2);
041: List nodes = acu.findChildrenOfType(ASTPrimaryExpression.class);
042: NameFinder occs = new NameFinder((ASTPrimaryExpression) nodes
043: .get(0));
044: NameOccurrence this NameOccurrence = occs.getNames().get(0);
045: assertEquals(this NameOccurrence
046: .getNameForWhichThisIsAQualifier(), occs.getNames()
047: .get(1));
048: }
049:
050: @Test
051: public void testSimpleVariableOccurrence() {
052: parseCode(TEST3);
053: List nodes = acu.findChildrenOfType(ASTPrimaryExpression.class);
054: NameFinder occs = new NameFinder((ASTPrimaryExpression) nodes
055: .get(0));
056: assertEquals("x", occs.getNames().get(0).getImage());
057: assertFalse(occs.getNames().get(0).isThisOrSuper());
058: assertFalse(occs.getNames().get(0)
059: .isMethodOrConstructorInvocation());
060: assertTrue(occs.getNames().get(0).isOnLeftHandSide());
061: }
062:
063: @Test
064: public void testQualifiedOccurrence() {
065: parseCode(TEST4);
066: List nodes = acu.findChildrenOfType(ASTPrimaryExpression.class);
067: NameFinder occs = new NameFinder((ASTPrimaryExpression) nodes
068: .get(0));
069: assertEquals("b", occs.getNames().get(0).getImage());
070: assertEquals("x", occs.getNames().get(1).getImage());
071: }
072:
073: @Test
074: public void testIsSelfAssignment() {
075: parseCode(TEST5);
076: List nodes = acu.findChildrenOfType(ASTPrimaryExpression.class);
077: NameFinder occs = new NameFinder((ASTPrimaryExpression) nodes
078: .get(2));
079: assertTrue(occs.getNames().get(0).isSelfAssignment());
080:
081: parseCode(TEST6);
082: nodes = acu.findChildrenOfType(ASTPrimaryExpression.class);
083: occs = new NameFinder((ASTPrimaryExpression) nodes.get(2));
084: assertTrue(occs.getNames().get(0).isSelfAssignment());
085: }
086:
087: public static final String TEST1 = "public class Foo {" + PMD.EOL
088: + " void foo() {" + PMD.EOL + " super.x = 2;" + PMD.EOL
089: + " }" + PMD.EOL + "}";
090:
091: public static final String TEST2 = "public class Foo {" + PMD.EOL
092: + " void foo() {" + PMD.EOL + " this.x = 2;" + PMD.EOL
093: + " }" + PMD.EOL + "}";
094:
095: public static final String TEST3 = "public class Foo {" + PMD.EOL
096: + " void foo() {" + PMD.EOL + " x = 2;" + PMD.EOL + " }"
097: + PMD.EOL + "}";
098:
099: public static final String TEST4 = "public class Foo {" + PMD.EOL
100: + " void foo() {" + PMD.EOL + " b.x = 2;" + PMD.EOL + " }"
101: + PMD.EOL + "}";
102:
103: public static final String TEST5 = "public class Foo{" + PMD.EOL
104: + " private int counter;" + PMD.EOL
105: + " private Foo(){" + PMD.EOL + " counter = 0;"
106: + PMD.EOL + " }" + PMD.EOL + " private int foo(){"
107: + PMD.EOL + " if (++counter < 3) {" + PMD.EOL
108: + " return 0;" + PMD.EOL + " }" + PMD.EOL
109: + " return 1;" + PMD.EOL + " }" + PMD.EOL + "}";
110:
111: public static final String TEST6 = "public class Foo{" + PMD.EOL
112: + " private int counter;" + PMD.EOL
113: + " private Foo(){" + PMD.EOL + " counter = 0;"
114: + PMD.EOL + " }" + PMD.EOL + " private int foo(){"
115: + PMD.EOL + " if (++this.counter < 3) {" + PMD.EOL
116: + " return 0;" + PMD.EOL + " }" + PMD.EOL
117: + " return 1;" + PMD.EOL + " }" + PMD.EOL + "}";
118:
119: public static junit.framework.Test suite() {
120: return new junit.framework.JUnit4TestAdapter(
121: NameOccurrencesTest.class);
122: }
123: }
|