1 package test.net.sourceforge.pmd.symboltable;
2
3 import junit.framework.TestCase;
4 import net.sourceforge.pmd.ast.ASTClassBodyDeclaration;
5 import net.sourceforge.pmd.ast.ASTCompilationUnit;
6 import net.sourceforge.pmd.ast.ASTIfStatement;
7 import net.sourceforge.pmd.ast.ASTTryStatement;
8 import net.sourceforge.pmd.ast.SimpleNode;
9 import net.sourceforge.pmd.symboltable.BasicScopeCreationVisitor;
10 import net.sourceforge.pmd.symboltable.BasicScopeFactory;
11 import net.sourceforge.pmd.symboltable.GlobalScope;
12 import net.sourceforge.pmd.symboltable.LocalScope;
13 import net.sourceforge.pmd.symboltable.ScopeFactory;
14
15 import java.util.Stack;
16
17 public class ScopeCreationVisitorTest extends TestCase {
18
19 private class MyCB extends ASTClassBodyDeclaration {
20 public MyCB() {
21 super(1);
22 }
23 public boolean isAnonymousInnerClass() {
24 return true;
25 }
26 }
27
28 private class MySF implements ScopeFactory {
29 public boolean gotCalled;
30 public void openScope(Stack scopes, SimpleNode node) {
31 this.gotCalled = true;
32 scopes.add(new Object());
33 }
34 }
35
36 public void testScopesAreCreated() {
37 BasicScopeCreationVisitor sc = new BasicScopeCreationVisitor(new BasicScopeFactory());
38
39 ASTCompilationUnit acu = new ASTCompilationUnit(1);
40 acu.setScope(new GlobalScope());
41
42 ASTTryStatement tryNode = new ASTTryStatement(2);
43 tryNode.setScope(new LocalScope());
44 tryNode.jjtSetParent(acu);
45
46 ASTIfStatement ifNode = new ASTIfStatement(3);
47 ifNode.jjtSetParent(tryNode);
48
49 sc.visit(acu, null);
50
51 assertTrue(ifNode.getScope() instanceof LocalScope);
52 }
53
54 public void testAnonymousInnerClassIsCreated() {
55 MySF sf = new MySF();
56 BasicScopeCreationVisitor sc = new BasicScopeCreationVisitor(sf);
57 ASTClassBodyDeclaration cb = new MyCB();
58 sc.visit(cb, null);
59 assertTrue(sf.gotCalled);
60 }
61
62 public void testAnonymousInnerClassIsNotCreated() {
63 MySF sf = new MySF();
64 new BasicScopeCreationVisitor(sf).visit(new ASTClassBodyDeclaration(1), null);
65 assertFalse(sf.gotCalled);
66 }
67
68 }
This page was automatically generated by Maven