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