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 net.sourceforge.pmd.PMD;
7   import net.sourceforge.pmd.ast.ASTMethodDeclaration;
8   import net.sourceforge.pmd.symboltable.Scope;
9   import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
10  
11  import java.util.Iterator;
12  import java.util.Map;
13  
14  public class AcceptanceTest extends STBBaseTst {
15  
16  /*
17      public void testClashingSymbols() {
18          parseCode(TEST1);
19      }
20  
21      public void testInitializer() {
22          parseCode(TEST_INITIALIZERS);
23          ASTInitializer a = (ASTInitializer)(acu.findChildrenOfType(ASTInitializer.class)).get(0);
24          assertFalse(a.isStatic());
25          a = (ASTInitializer)(acu.findChildrenOfType(ASTInitializer.class)).get(1);
26          assertTrue(a.isStatic());
27      }
28  
29      public void testCatchBlocks() {
30          parseCode(TEST_CATCH_BLOCKS);
31          ASTCatchStatement c = (ASTCatchStatement)(acu.findChildrenOfType(ASTCatchStatement.class)).get(0);
32          ASTBlock a = (ASTBlock)(c.findChildrenOfType(ASTBlock.class)).get(0);
33          Scope s = a.getScope();
34          Map vars = s.getParent().getVariableDeclarations();
35          assertEquals(1, vars.size());
36          VariableNameDeclaration v = (VariableNameDeclaration)vars.keySet().iterator().next();
37          assertEquals("e", v.getImage());
38          assertEquals(1, ((List)vars.get(v)).size());
39      }
40  
41      public void testEq() {
42          parseCode(TEST_EQ);
43          ASTEqualityExpression e = (ASTEqualityExpression)(acu.findChildrenOfType(ASTEqualityExpression.class)).get(0);
44          ASTMethodDeclaration method = (ASTMethodDeclaration)e.getFirstParentOfType(ASTMethodDeclaration.class);
45          Scope s = method.getScope();
46          Map m = s.getVariableDeclarations();
47          for (Iterator i = m.keySet().iterator(); i.hasNext();) {
48              VariableNameDeclaration vnd = (VariableNameDeclaration)i.next();
49              SimpleNode node = vnd.getNode();
50              //System.out.println();
51          }
52          //System.out.println(m.size());
53  
54      }
55  */
56  
57      public void testDemo() {
58          parseCode(TEST_DEMO);
59          System.out.println(TEST_DEMO);
60          ASTMethodDeclaration node = (ASTMethodDeclaration)acu.findChildrenOfType(ASTMethodDeclaration.class).get(0);
61          Scope s = node.getScope();
62          Map m = s.getVariableDeclarations();
63          for (Iterator i = m.keySet().iterator(); i.hasNext();) {
64              VariableNameDeclaration d = (VariableNameDeclaration)i.next();
65              System.out.println("Variable: " + d.getImage());
66              System.out.println("Type: " + d.getTypeImage());
67          }
68      }
69  /*
70              List u = (List)m.get(d);
71              System.out.println("Usages: " + u.size());
72              NameOccurrence o = (NameOccurrence)u.get(0);
73              int beginLine = o.getLocation().getBeginLine();
74              System.out.println("Used in line " + beginLine);
75  */
76  
77      private static final String TEST_DEMO =
78      "public class Foo  {" + PMD.EOL +
79      " void bar(ArrayList buz) { " + PMD.EOL +
80      " } " + PMD.EOL +
81      "}" + PMD.EOL;
82  
83      private static final String TEST_EQ =
84      "public class Foo  {" + PMD.EOL +
85      " boolean foo(String a, String b) { " + PMD.EOL +
86      "  return a == b; " + PMD.EOL +
87      " } " + PMD.EOL +
88      "}" + PMD.EOL;
89  
90      private static final String TEST1 =
91      "import java.io.*;" + PMD.EOL +
92      "public class Foo  {" + PMD.EOL +
93      " void buz( ) {" + PMD.EOL +
94      "  Object o = new Serializable() { int x; };" + PMD.EOL +
95      "  Object o1 = new Serializable() { int x; };" + PMD.EOL +
96      " }" + PMD.EOL  +
97      "}" + PMD.EOL;
98  
99      private static final String TEST_INITIALIZERS =
100     "public class Foo  {" + PMD.EOL +
101     " {} " + PMD.EOL +
102     " static {} " + PMD.EOL +
103     "}" + PMD.EOL;
104 
105     private static final String TEST_CATCH_BLOCKS =
106     "public class Foo  {" + PMD.EOL +
107     " void foo() { " + PMD.EOL +
108     "  try { " + PMD.EOL +
109     "  } catch (Exception e) { " + PMD.EOL +
110     "   e.printStackTrace(); " + PMD.EOL +
111     "  } " + PMD.EOL +
112     " } " + PMD.EOL +
113     "}" + PMD.EOL;
114 
115 
116 }