    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.ASTFormalParameter;
    8   import net.sourceforge.pmd.ast.ASTTryStatement;
    9   import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
    10  import net.sourceforge.pmd.symboltable.Scope;
    11  import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
    12
    13  import java.util.List;
    14
    15  public class VariableNameDeclarationTest extends STBBaseTst {
    16
    17      public void testConstructor() {
    18          parseCode(TEST1);
    19          List nodes = acu.findChildrenOfType(ASTVariableDeclaratorId.class);
    20          Scope s = ((ASTVariableDeclaratorId) nodes.get(0)).getScope();
    21          VariableNameDeclaration decl = (VariableNameDeclaration) s.getVariableDeclarations().keySet().iterator().next();
    22          assertEquals("bar", decl.getImage());
    23          assertEquals(3, decl.getNode().getBeginLine());
    24      }
    25
    26      public void testExceptionBlkParam() {
    27          ASTVariableDeclaratorId id = new ASTVariableDeclaratorId(3);
    28          id.testingOnly__setBeginLine(10);
    29          id.setImage("foo");
    30          ASTFormalParameter param = new ASTFormalParameter(2);
    31          id.jjtSetParent(param);
    32          param.jjtSetParent(new ASTTryStatement(1));
    33          VariableNameDeclaration decl = new VariableNameDeclaration(id);
    34          assertTrue(decl.isExceptionBlockParameter());
    35      }
    36
    37      public void testIsArray() {
    38          parseCode(TEST3);
    39          VariableNameDeclaration decl = (VariableNameDeclaration) ((ASTVariableDeclaratorId) acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0)).getScope().getVariableDeclarations().keySet().iterator().next();
    40          assertTrue(decl.isArray());
    41      }
    42
    43      public void testPrimitiveType() {
    44          parseCode(TEST1);
    45          VariableNameDeclaration decl = (VariableNameDeclaration) ((ASTVariableDeclaratorId) acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0)).getScope().getVariableDeclarations().keySet().iterator().next();
    46          assertTrue(decl.isPrimitiveType());
    47      }
    48
    49      public void testArrayIsReferenceType() {
    50          parseCode(TEST3);
    51          VariableNameDeclaration decl = (VariableNameDeclaration) ((ASTVariableDeclaratorId) acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0)).getScope().getVariableDeclarations().keySet().iterator().next();
    52          assertTrue(decl.isReferenceType());
    53      }
    54
    55      public void testPrimitiveTypeImage() {
    56          parseCode(TEST3);
    57          VariableNameDeclaration decl = (VariableNameDeclaration) ((ASTVariableDeclaratorId) acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0)).getScope().getVariableDeclarations().keySet().iterator().next();
    58          assertEquals("int", decl.getTypeImage());
    59      }
    60
    61      public void testRefTypeImage() {
    62          parseCode(TEST4);
    63          VariableNameDeclaration decl = (VariableNameDeclaration) ((ASTVariableDeclaratorId) acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0)).getScope().getVariableDeclarations().keySet().iterator().next();
    64          assertEquals("String", decl.getTypeImage());
    65      }
    66
    67      public void testParamTypeImage() {
    68          parseCode(TEST5);
    69          VariableNameDeclaration decl = (VariableNameDeclaration) ((ASTVariableDeclaratorId) acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0)).getScope().getVariableDeclarations().keySet().iterator().next();
    70          assertEquals("String", decl.getTypeImage());
    71      }
    72
    73      public static final String TEST1 =
    74              "public class Foo {" + PMD.EOL +
    75              " void foo() {" + PMD.EOL +
    76              "  int bar = 42;" + PMD.EOL +
    77              " }" + PMD.EOL +
    78              "}";
    79
    80      public static final String TEST2 =
    81              "public class Foo {" + PMD.EOL +
    82              " void foo() {" + PMD.EOL +
    83              "  try {} catch(Exception e) {}" + PMD.EOL +
    84              " }" + PMD.EOL +
    85              "}";
    86
    87      public static final String TEST3 =
    88              "public class Foo {" + PMD.EOL +
    89              " void foo() {" + PMD.EOL +
    90              "  int[] x;" + PMD.EOL +
    91              " }" + PMD.EOL +
    92              "}";
    93
    94      public static final String TEST4 =
    95              "public class Foo {" + PMD.EOL +
    96              " void foo() {" + PMD.EOL +
    97              "  String x;" + PMD.EOL +
    98              " }" + PMD.EOL +
    99              "}";
    100     public static final String TEST5 =
    101             "public class Foo {" + PMD.EOL +
    102             " void foo(String x) {}" + PMD.EOL +
    103             "}";
    104 }