View Javadoc

    1   package net.sourceforge.pmd.ast;
    2
    3   import static org.junit.Assert.assertEquals;
    4   import static org.junit.Assert.assertFalse;
    5   import static org.junit.Assert.assertTrue;
    6
    7   import java.util.Iterator;
    8   import java.util.Set;
    9
    10  import net.sourceforge.pmd.PMD;
    11  import net.sourceforge.pmd.lang.LanguageVersion;
    12  import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter;
    13  import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
    14  import net.sourceforge.pmd.testframework.ParserTst;
    15
    16  import org.junit.Test;
    17
    18
    19  public class ASTFormalParameterTest extends ParserTst {
    20
    21      @Test
    22      public void testVarargs() throws Throwable {
    23          int nrOfVarArgs   = 0;
    24          int nrOfNoVarArgs = 0;
    25
    26          Set<ASTFormalParameter> ops = getNodes(LanguageVersion.JAVA_15, ASTFormalParameter.class, TEST1);
    27          for (Iterator<ASTFormalParameter> iter = ops.iterator(); iter.hasNext();) {
    28              ASTFormalParameter b = iter.next();
    29              ASTVariableDeclaratorId variableDeclId = b.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
    30              if (!"x".equals(variableDeclId.getImage())) {
    31                  assertTrue(b.isVarargs());
    32                  nrOfVarArgs++;
    33              } else {
    34                  assertFalse(b.isVarargs());
    35                  nrOfNoVarArgs++;
    36              }
    37          }
    38
    39          //Ensure that both possibilities are tested
    40          assertEquals(1, nrOfVarArgs);
    41          assertEquals(1, nrOfNoVarArgs);
    42      }
    43
    44      private static final String TEST1 =
    45              "class Foo {" + PMD.EOL +
    46              " void bar(int x, int... others) {}" + PMD.EOL +
    47              "}";
    48
    49      public static junit.framework.Test suite() {
    50          return new junit.framework.JUnit4TestAdapter(ASTFormalParameterTest.class);
    51      }
    52  }