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.ASTBlock;
010: import net.sourceforge.pmd.ast.ASTCatchStatement;
011: import net.sourceforge.pmd.ast.ASTEqualityExpression;
012: import net.sourceforge.pmd.ast.ASTInitializer;
013: import net.sourceforge.pmd.ast.ASTMethodDeclaration;
014: import net.sourceforge.pmd.ast.SimpleNode;
015: import net.sourceforge.pmd.symboltable.Scope;
016: import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
017:
018: import org.junit.Ignore;
019: import org.junit.Test;
020:
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: public class AcceptanceTest extends STBBaseTst {
026:
027: @Ignore
028: @Test
029: public void testClashingSymbols() {
030: parseCode(TEST1);
031: }
032:
033: @Ignore
034: @Test
035: public void testInitializer() {
036: parseCode(TEST_INITIALIZERS);
037: ASTInitializer a = acu.findChildrenOfType(ASTInitializer.class)
038: .get(0);
039: assertFalse(a.isStatic());
040: a = acu.findChildrenOfType(ASTInitializer.class).get(1);
041: assertTrue(a.isStatic());
042: }
043:
044: @Ignore
045: @Test
046: public void testCatchBlocks() {
047: parseCode(TEST_CATCH_BLOCKS);
048: ASTCatchStatement c = acu.findChildrenOfType(
049: ASTCatchStatement.class).get(0);
050: ASTBlock a = c.findChildrenOfType(ASTBlock.class).get(0);
051: Scope s = a.getScope();
052: Map vars = s.getParent().getVariableDeclarations();
053: assertEquals(1, vars.size());
054: VariableNameDeclaration v = (VariableNameDeclaration) vars
055: .keySet().iterator().next();
056: assertEquals("e", v.getImage());
057: assertEquals(1, ((List) vars.get(v)).size());
058: }
059:
060: @Ignore
061: @Test
062: public void testEq() {
063: parseCode(TEST_EQ);
064: ASTEqualityExpression e = acu.findChildrenOfType(
065: ASTEqualityExpression.class).get(0);
066: ASTMethodDeclaration method = e
067: .getFirstParentOfType(ASTMethodDeclaration.class);
068: Scope s = method.getScope();
069: Map m = s.getVariableDeclarations();
070: for (Iterator i = m.keySet().iterator(); i.hasNext();) {
071: VariableNameDeclaration vnd = (VariableNameDeclaration) i
072: .next();
073: SimpleNode node = vnd.getNode();
074: //System.out.println();
075: }
076: //System.out.println(m.size());
077: }
078:
079: @Test
080: public void testFieldFinder() {
081: //FIXME - Does this test do anything?
082: //Not really, I think it's just a demo -- Tom
083:
084: /*
085: System.out.println(TEST_FIELD);
086: parseCode(TEST_FIELD);
087:
088: List<ASTVariableDeclaratorId> variableDeclaratorIds = acu.findChildrenOfType(ASTVariableDeclaratorId.class);
089: ASTVariableDeclaratorId declaration = null;
090: for (Iterator iter = variableDeclaratorIds.iterator(); iter.hasNext();) {
091: declaration = (ASTVariableDeclaratorId) iter.next();
092: if ("b".equals(declaration.getImage()))
093: break;
094: }
095: NameOccurrence no = declaration.getUsages().iterator().next();
096: SimpleNode location = no.getLocation();
097: System.out.println("variable " + declaration.getImage() + " is used here: " + location.getImage());
098: */
099: }
100:
101: @Ignore
102: @Test
103: public void testDemo() {
104: parseCode(TEST_DEMO);
105: System.out.println(TEST_DEMO);
106: ASTMethodDeclaration node = acu.findChildrenOfType(
107: ASTMethodDeclaration.class).get(0);
108: Scope s = node.getScope();
109: Map m = s.getVariableDeclarations();
110: for (Iterator i = m.keySet().iterator(); i.hasNext();) {
111: VariableNameDeclaration d = (VariableNameDeclaration) i
112: .next();
113: System.out.println("Variable: " + d.getImage());
114: System.out.println("Type: " + d.getTypeImage());
115: }
116: }
117:
118: /*
119: List u = (List)m.get(d);
120: System.out.println("Usages: " + u.size());
121: NameOccurrence o = (NameOccurrence)u.get(0);
122: int beginLine = o.getLocation().getBeginLine();
123: System.out.println("Used in line " + beginLine);
124: */
125:
126: private static final String TEST_DEMO = "public class Foo {"
127: + PMD.EOL + " void bar(ArrayList buz) { " + PMD.EOL + " } "
128: + PMD.EOL + "}" + PMD.EOL;
129:
130: private static final String TEST_EQ = "public class Foo {"
131: + PMD.EOL + " boolean foo(String a, String b) { " + PMD.EOL
132: + " return a == b; " + PMD.EOL + " } " + PMD.EOL + "}"
133: + PMD.EOL;
134:
135: private static final String TEST1 = "import java.io.*;" + PMD.EOL
136: + "public class Foo {" + PMD.EOL + " void buz( ) {"
137: + PMD.EOL + " Object o = new Serializable() { int x; };"
138: + PMD.EOL + " Object o1 = new Serializable() { int x; };"
139: + PMD.EOL + " }" + PMD.EOL + "}" + PMD.EOL;
140:
141: private static final String TEST_INITIALIZERS = "public class Foo {"
142: + PMD.EOL
143: + " {} "
144: + PMD.EOL
145: + " static {} "
146: + PMD.EOL
147: + "}" + PMD.EOL;
148:
149: private static final String TEST_CATCH_BLOCKS = "public class Foo {"
150: + PMD.EOL
151: + " void foo() { "
152: + PMD.EOL
153: + " try { "
154: + PMD.EOL
155: + " } catch (Exception e) { "
156: + PMD.EOL
157: + " e.printStackTrace(); "
158: + PMD.EOL
159: + " } "
160: + PMD.EOL
161: + " } " + PMD.EOL + "}" + PMD.EOL;
162:
163: private static final String TEST_FIELD = "public class MyClass {"
164: + PMD.EOL + " private int a; " + PMD.EOL
165: + " boolean b = MyClass.ASCENDING; " + PMD.EOL + "}"
166: + PMD.EOL;
167:
168: public static junit.framework.Test suite() {
169: return new junit.framework.JUnit4TestAdapter(
170: AcceptanceTest.class);
171: }
172: }
|