1
2
3
4 package net.sourceforge.pmd.ast;
5
6 import static org.junit.Assert.assertEquals;
7 import static org.junit.Assert.assertNull;
8 import static org.junit.Assert.assertSame;
9 import static org.junit.Assert.assertTrue;
10 import net.sourceforge.pmd.PMD;
11 import net.sourceforge.pmd.lang.java.ast.ASTBlock;
12 import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
13 import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
14 import net.sourceforge.pmd.lang.java.ast.ASTTryStatement;
15 import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
16 import net.sourceforge.pmd.testframework.ParserTst;
17
18 import org.junit.Test;
19
20
21 public class ASTVariableDeclaratorIdTest extends ParserTst {
22
23 @Test
24 public void testIsExceptionBlockParameter() {
25 ASTTryStatement tryNode = new ASTTryStatement(1);
26 ASTBlock block = new ASTBlock(2);
27 ASTVariableDeclaratorId v = new ASTVariableDeclaratorId(3);
28 v.jjtSetParent(block);
29 block.jjtSetParent(tryNode);
30 assertTrue(v.isExceptionBlockParameter());
31 }
32
33 @Test
34 public void testTypeNameNode() throws Throwable {
35 ASTCompilationUnit acu = super.getNodes(ASTCompilationUnit.class, TYPE_NAME_NODE).iterator().next();
36 ASTVariableDeclaratorId id = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(0);
37
38 ASTClassOrInterfaceType name = (ASTClassOrInterfaceType) id.getTypeNameNode().jjtGetChild(0);
39 assertEquals("String", name.getImage());
40 }
41
42 @Test
43 public void testAnnotations() throws Throwable {
44 ASTCompilationUnit acu = super.getNodes(ASTCompilationUnit.class, TEST_ANNOTATIONS).iterator().next();
45 ASTVariableDeclaratorId id = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(0);
46
47 ASTClassOrInterfaceType name = (ASTClassOrInterfaceType) id.getTypeNameNode().jjtGetChild(0);
48 assertEquals("String", name.getImage());
49 }
50
51 @Test
52 public void testLambdaWithType() throws Exception {
53 ASTCompilationUnit acu = parseJava18(TEST_LAMBDA_WITH_TYPE);
54 ASTVariableDeclaratorId f = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(1);
55 assertEquals("File", f.getTypeNode().getTypeImage());
56 assertEquals("File", f.getTypeNameNode().jjtGetChild(0).getImage());
57 }
58
59 @Test
60 public void testLambdaWithoutType() throws Exception {
61 ASTCompilationUnit acu = parseJava18(TEST_LAMBDA_WITHOUT_TYPE);
62 ASTVariableDeclaratorId f = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(1);
63 assertNull(f.getTypeNode());
64 assertNull(f.getTypeNameNode());
65 }
66
67 private static final String TYPE_NAME_NODE =
68 "public class Test {" + PMD.EOL +
69 " private String bar;" + PMD.EOL +
70 "}";
71 private static final String TEST_ANNOTATIONS =
72 "public class Foo {" + PMD.EOL +
73 " public void bar(@A1 @A2 String s) {}" + PMD.EOL +
74 "}";
75 private static final String TEST_LAMBDA_WITH_TYPE =
76 "public class Foo {\n" +
77 " public void bar() {\n" +
78 " FileFilter java = (File f) -> f.getName().endsWith(\".java\");\n" +
79 " }\n" +
80 "}\n";
81 private static final String TEST_LAMBDA_WITHOUT_TYPE =
82 "public class Foo {\n" +
83 " public void bar() {\n" +
84 " FileFilter java2 = f -> f.getName().endsWith(\".java\");\n" +
85 " }\n" +
86 "}\n";
87
88 public static junit.framework.Test suite() {
89 return new junit.framework.JUnit4TestAdapter(ASTVariableDeclaratorIdTest.class);
90 }
91 }