1 package test.net.sourceforge.pmd.ast;
2
3 import net.sourceforge.pmd.ast.ASTAnnotation;
4 import net.sourceforge.pmd.ast.ASTAnnotationTypeDeclaration;
5 import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
6 import net.sourceforge.pmd.ast.ASTCompilationUnit;
7 import net.sourceforge.pmd.ast.ASTEqualityExpression;
8 import net.sourceforge.pmd.ast.ASTInstanceOfExpression;
9 import net.sourceforge.pmd.ast.ASTModifiers;
10 import net.sourceforge.pmd.ast.ASTRelationalExpression;
11 import net.sourceforge.pmd.ast.ASTTypeDeclaration;
12 import net.sourceforge.pmd.ast.DiscardableNodeCleaner;
13 import test.net.sourceforge.pmd.testframework.ParserTst;
14
15 public class DiscardableNodeCleanerTest extends ParserTst {
16
17 public void testRemoveDiscardNodes() throws Throwable {
18 ASTCompilationUnit cu = new ASTCompilationUnit(1);
19 ASTEqualityExpression ee = new ASTEqualityExpression(2);
20 ee.jjtSetParent(cu);
21 cu.jjtAddChild(ee, 0);
22 ASTInstanceOfExpression io1 = new ASTInstanceOfExpression(3);
23 io1.setDiscardable();
24 io1.jjtSetParent(ee);
25 ASTRelationalExpression re = new ASTRelationalExpression(4);
26 re.jjtSetParent(ee);
27 ee.jjtAddChild(io1, 0);
28 io1.jjtAddChild(re, 0);
29 assertEquals(cu.findChildrenOfType(ASTInstanceOfExpression.class).size(), 1);
30 DiscardableNodeCleaner c = new DiscardableNodeCleaner();
31 c.clean(cu);
32 assertEquals(cu.findChildrenOfType(ASTInstanceOfExpression.class).size(), 0);
33 }
34
35 public void testRemoveModifierNodesWithClass() throws Throwable {
36 ASTCompilationUnit cu = new ASTCompilationUnit(1);
37 ASTTypeDeclaration td = new ASTTypeDeclaration(2);
38 td.jjtSetParent(cu);
39 cu.jjtAddChild(td, 0);
40 ASTModifiers m = new ASTModifiers(3);
41 m.setDiscardable();
42 m.jjtSetParent(td);
43 td.jjtAddChild(m, 0);
44 ASTClassOrInterfaceDeclaration cd = new ASTClassOrInterfaceDeclaration(4);
45 cd.jjtSetParent(td);
46 td.jjtAddChild(cd, 1);
47 assertEquals(cu.findChildrenOfType(ASTModifiers.class).size(), 1);
48 DiscardableNodeCleaner c = new DiscardableNodeCleaner();
49 c.clean(cu);
50 assertTrue(cu.findChildrenOfType(ASTModifiers.class).isEmpty());
51 }
52
53 public void testRemoveModifierNodesWithAnnotation() throws Throwable {
54 ASTCompilationUnit cu = new ASTCompilationUnit(1);
55 ASTTypeDeclaration td = new ASTTypeDeclaration(2);
56 td.jjtSetParent(cu);
57 cu.jjtAddChild(td, 0);
58 ASTAnnotationTypeDeclaration atd = new ASTAnnotationTypeDeclaration(5);
59 atd.jjtSetParent(td);
60 td.jjtAddChild(atd, 0);
61 ASTModifiers m = new ASTModifiers(3);
62 m.setDiscardable();
63 m.jjtSetParent(td);
64 td.jjtAddChild(m, 1);
65 ASTAnnotation ann = new ASTAnnotation(4);
66 ann.jjtSetParent(m);
67 m.jjtAddChild(ann, 0);
68 assertEquals(cu.findChildrenOfType(ASTModifiers.class).size(), 1);
69 DiscardableNodeCleaner c = new DiscardableNodeCleaner();
70 c.clean(cu);
71 assertTrue(cu.findChildrenOfType(ASTModifiers.class).isEmpty());
72 assertFalse(cu.findChildrenOfType(ASTAnnotation.class).isEmpty());
73 }
74
75 }