01: package test.net.sourceforge.pmd.symboltable;
02:
03: import static org.junit.Assert.assertEquals;
04: import static org.junit.Assert.assertFalse;
05: import net.sourceforge.pmd.PMD;
06: import net.sourceforge.pmd.ast.ASTCompilationUnit;
07: import net.sourceforge.pmd.symboltable.ClassNameDeclaration;
08:
09: import org.junit.Test;
10:
11: import java.util.Iterator;
12: import java.util.Map;
13:
14: public class SourceFileScopeTest extends STBBaseTst {
15:
16: @Test
17: public void testClassDeclAppears() {
18: parseCode(TEST1);
19: Map m = acu.getScope().getClassDeclarations();
20: ClassNameDeclaration classNameDeclaration = (ClassNameDeclaration) m
21: .keySet().iterator().next();
22: assertEquals(classNameDeclaration.getImage(), "Foo");
23: }
24:
25: @Test
26: public void testPackageIsEmptyString() {
27: parseCode(TEST1);
28: ASTCompilationUnit decl = acu.findChildrenOfType(
29: ASTCompilationUnit.class).get(0);
30: assertEquals(decl.getScope().getEnclosingSourceFileScope()
31: .getPackageName(), "");
32: }
33:
34: @Test
35: public void testPackageNameFound() {
36: parseCode(TEST2);
37: ASTCompilationUnit decl = acu.findChildrenOfType(
38: ASTCompilationUnit.class).get(0);
39: assertEquals(decl.getScope().getEnclosingSourceFileScope()
40: .getPackageName(), "foo.bar");
41: }
42:
43: @Test
44: public void testNestedClasses() {
45: parseCode(TEST3);
46: Map m = acu.getScope().getClassDeclarations();
47: Iterator iterator = m.keySet().iterator();
48: ClassNameDeclaration classNameDeclaration = (ClassNameDeclaration) iterator
49: .next();
50: assertEquals(classNameDeclaration.getImage(), "Foo");
51: assertFalse(iterator.hasNext());
52: }
53:
54: private static final String TEST1 = "public class Foo {}" + PMD.EOL;
55:
56: private static final String TEST2 = "package foo.bar;" + PMD.EOL
57: + "public class Foo {" + PMD.EOL + "}" + PMD.EOL;
58:
59: private static final String TEST3 = "public class Foo {" + PMD.EOL
60: + " public class Bar {" + PMD.EOL + " }" + PMD.EOL + "}"
61: + PMD.EOL;
62:
63: public static junit.framework.Test suite() {
64: return new junit.framework.JUnit4TestAdapter(
65: SourceFileScopeTest.class);
66: }
67: }
|