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