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.ASTBlock;
11  import net.sourceforge.pmd.ast.ASTCatchStatement;
12  import net.sourceforge.pmd.ast.ASTEqualityExpression;
13  import net.sourceforge.pmd.ast.ASTInitializer;
14  import net.sourceforge.pmd.ast.ASTMethodDeclaration;
15  import net.sourceforge.pmd.ast.SimpleNode;
16  import net.sourceforge.pmd.symboltable.Scope;
17  import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
18  
19  import org.junit.Ignore;
20  import org.junit.Test;
21  
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  public class AcceptanceTest extends STBBaseTst {
26  
27      @Ignore
28      @Test
29      public void testClashingSymbols() {
30          parseCode(TEST1);
31      }
32  
33      @Ignore
34      @Test
35      public void testInitializer() {
36          parseCode(TEST_INITIALIZERS);
37          ASTInitializer a = acu.findChildrenOfType(ASTInitializer.class).get(0);
38          assertFalse(a.isStatic());
39          a = acu.findChildrenOfType(ASTInitializer.class).get(1);
40          assertTrue(a.isStatic());
41      }
42  
43      @Ignore
44      @Test
45      public void testCatchBlocks() {
46          parseCode(TEST_CATCH_BLOCKS);
47          ASTCatchStatement c = acu.findChildrenOfType(ASTCatchStatement.class).get(0);
48          ASTBlock a = c.findChildrenOfType(ASTBlock.class).get(0);
49          Scope s = a.getScope();
50          Map vars = s.getParent().getVariableDeclarations();
51          assertEquals(1, vars.size());
52          VariableNameDeclaration v = (VariableNameDeclaration)vars.keySet().iterator().next();
53          assertEquals("e", v.getImage());
54          assertEquals(1, ((List)vars.get(v)).size());
55      }
56  
57      @Ignore
58      @Test
59      public void testEq() {
60          parseCode(TEST_EQ);
61          ASTEqualityExpression e = acu.findChildrenOfType(ASTEqualityExpression.class).get(0);
62          ASTMethodDeclaration method = e.getFirstParentOfType(ASTMethodDeclaration.class);
63          Scope s = method.getScope();
64          Map m = s.getVariableDeclarations();
65          for (Iterator i = m.keySet().iterator(); i.hasNext();) {
66              VariableNameDeclaration vnd = (VariableNameDeclaration)i.next();
67              SimpleNode node = vnd.getNode();
68              //System.out.println();
69          }
70          //System.out.println(m.size());
71      }
72  
73      @Test
74      public void testFieldFinder() {
75          //FIXME - Does this test do anything?
76          //Not really, I think it's just a demo -- Tom
77  
78  /*
79          System.out.println(TEST_FIELD);
80          parseCode(TEST_FIELD);
81  
82          List<ASTVariableDeclaratorId> variableDeclaratorIds = acu.findChildrenOfType(ASTVariableDeclaratorId.class);
83          ASTVariableDeclaratorId declaration = null;
84          for (Iterator iter = variableDeclaratorIds.iterator(); iter.hasNext();) {
85              declaration = (ASTVariableDeclaratorId) iter.next();
86              if ("b".equals(declaration.getImage()))
87                  break;
88          }
89          NameOccurrence no = declaration.getUsages().iterator().next();
90          SimpleNode location = no.getLocation();
91          System.out.println("variable " + declaration.getImage() + " is used here: " + location.getImage());
92  */
93      }
94  
95      @Ignore
96      @Test
97      public void testDemo() {
98          parseCode(TEST_DEMO);
99          System.out.println(TEST_DEMO);
100         ASTMethodDeclaration node = acu.findChildrenOfType(ASTMethodDeclaration.class).get(0);
101         Scope s = node.getScope();
102         Map m = s.getVariableDeclarations();
103         for (Iterator i = m.keySet().iterator(); i.hasNext();) {
104             VariableNameDeclaration d = (VariableNameDeclaration) i.next();
105             System.out.println("Variable: " + d.getImage());
106             System.out.println("Type: " + d.getTypeImage());
107         }
108     }
109 /*
110             List u = (List)m.get(d);
111             System.out.println("Usages: " + u.size());
112             NameOccurrence o = (NameOccurrence)u.get(0);
113             int beginLine = o.getLocation().getBeginLine();
114             System.out.println("Used in line " + beginLine);
115 */
116 
117     private static final String TEST_DEMO =
118             "public class Foo  {" + PMD.EOL +
119             " void bar(ArrayList buz) { " + PMD.EOL +
120             " } " + PMD.EOL +
121             "}" + PMD.EOL;
122 
123     private static final String TEST_EQ =
124             "public class Foo  {" + PMD.EOL +
125             " boolean foo(String a, String b) { " + PMD.EOL +
126             "  return a == b; " + PMD.EOL +
127             " } " + PMD.EOL +
128             "}" + PMD.EOL;
129 
130     private static final String TEST1 =
131             "import java.io.*;" + PMD.EOL +
132             "public class Foo  {" + PMD.EOL +
133             " void buz( ) {" + PMD.EOL +
134             "  Object o = new Serializable() { int x; };" + PMD.EOL +
135             "  Object o1 = new Serializable() { int x; };" + PMD.EOL +
136             " }" + PMD.EOL +
137             "}" + PMD.EOL;
138 
139     private static final String TEST_INITIALIZERS =
140             "public class Foo  {" + PMD.EOL +
141             " {} " + PMD.EOL +
142             " static {} " + PMD.EOL +
143             "}" + PMD.EOL;
144 
145     private static final String TEST_CATCH_BLOCKS =
146             "public class Foo  {" + PMD.EOL +
147             " void foo() { " + PMD.EOL +
148             "  try { " + PMD.EOL +
149             "  } catch (Exception e) { " + PMD.EOL +
150             "   e.printStackTrace(); " + PMD.EOL +
151             "  } " + PMD.EOL +
152             " } " + PMD.EOL +
153             "}" + PMD.EOL;
154 
155     private static final String TEST_FIELD =
156     "public class MyClass {" + PMD.EOL +
157     " private int a; " + PMD.EOL +
158     " boolean b = MyClass.ASCENDING; " + PMD.EOL +
159     "}" + PMD.EOL;
160 
161     public static junit.framework.Test suite() {
162         return new junit.framework.JUnit4TestAdapter(AcceptanceTest.class);
163     }
164 }