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