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.PMD;
8 import net.sourceforge.pmd.symboltable.TypeSet;
9
10 import java.io.File;
11 import java.util.HashSet;
12 import java.util.Set;
13
14 public class TypeSetTest extends TestCase {
15
16 public void testASTCompilationUnitPackage() {
17 TypeSet t = new TypeSet();
18 t.setASTCompilationUnitPackage("java.lang.");
19 assertEquals("java.lang.", t.getASTCompilationUnitPackage());
20 }
21
22 public void testAddImport() {
23 TypeSet t = new TypeSet();
24 t.addImport("java.io.File");
25 assertEquals(1, t.getImportsCount());
26 }
27
28 public void testFindClassImplicitImport() throws Throwable {
29 TypeSet t = new TypeSet();
30 Class clazz = t.findClass("String");
31 assertEquals(String.class, clazz);
32 }
33
34 public void testFindClassSamePackage() throws Throwable {
35 TypeSet t = new TypeSet();
36 t.setASTCompilationUnitPackage("net.sourceforge.pmd.");
37 Class clazz = t.findClass("PMD");
38 assertEquals(PMD.class, clazz);
39 }
40
41 public void testFindClassExplicitImport() throws Throwable {
42 TypeSet t = new TypeSet();
43 t.addImport("java.io.File");
44 Class clazz = t.findClass("File");
45 assertEquals(File.class, clazz);
46 }
47
48 public void testFindClassImportOnDemand() throws Throwable {
49 TypeSet t = new TypeSet();
50 t.addImport("java.io.*");
51 Class clazz = t.findClass("File");
52 assertEquals(File.class, clazz);
53 }
54
55 public void testFindClassPrimitive() throws Throwable {
56 TypeSet t = new TypeSet();
57 assertEquals(int.class, t.findClass("int"));
58 }
59
60 public void testFindClassVoid() throws Throwable {
61 TypeSet t = new TypeSet();
62 assertEquals(void.class, t.findClass("void"));
63 }
64
65 public void testFindFullyQualified() throws Throwable {
66 TypeSet t = new TypeSet();
67 assertEquals(String.class, t.findClass("java.lang.String"));
68 assertEquals(Set.class, t.findClass("java.util.Set"));
69 }
70
71
72 public void testPrimitiveTypeResolver() throws Throwable {
73 TypeSet.Resolver r = new TypeSet.PrimitiveTypeResolver();
74 assertEquals(int.class, r.resolve("int"));
75 assertEquals(byte.class, r.resolve("byte"));
76 assertEquals(long.class, r.resolve("long"));
77 }
78
79 public void testVoidTypeResolver() throws Throwable {
80 TypeSet.Resolver r = new TypeSet.VoidResolver();
81 assertEquals(void.class, r.resolve("void"));
82 }
83
84 public void testExplicitImportResolver() throws Throwable {
85 Set imports = new HashSet();
86 imports.add("java.io.File");
87 TypeSet.Resolver r = new TypeSet.ExplicitImportResolver(imports);
88 assertEquals(File.class, r.resolve("File"));
89 }
90
91 public void testImplicitImportResolverPass() throws Throwable {
92 TypeSet.Resolver r = new TypeSet.ImplicitImportResolver();
93 assertEquals(String.class, r.resolve("String"));
94 }
95
96 public void testImplicitImportResolverPassFail() throws Throwable {
97 TypeSet.Resolver r = new TypeSet.ImplicitImportResolver();
98 try {
99 r.resolve("PMD");
100 fail("Should have thrown an exception");
101 } catch (ClassNotFoundException cnfe) {
102 }
103 }
104
105 public void testCurrentPackageResolverPass() throws Throwable {
106 TypeSet.Resolver r = new TypeSet.CurrentPackageResolver("net.sourceforge.pmd.");
107 assertEquals(PMD.class, r.resolve("PMD"));
108 }
109
110 public void testImportOnDemandResolverPass() throws Throwable {
111 Set imports = new HashSet();
112 imports.add("java.io.*");
113 imports.add("java.util.*");
114 TypeSet.Resolver r = new TypeSet.ImportOnDemandResolver(imports);
115 assertEquals(Set.class, r.resolve("Set"));
116 assertEquals(File.class, r.resolve("File"));
117 }
118
119 public void testImportOnDemandResolverFail() throws Throwable {
120 Set imports = new HashSet();
121 imports.add("java.io.*");
122 imports.add("java.util.*");
123 TypeSet.Resolver r = new TypeSet.ImportOnDemandResolver(imports);
124 try {
125 r.resolve("foo");
126 fail("Should have thrown a ClassNotFoundException");
127 } catch (ClassNotFoundException cnfe) {
128 }
129 try {
130 r.resolve("String");
131 fail("Should have thrown a ClassNotFoundException");
132 } catch (ClassNotFoundException cnfe) {
133 }
134 }
135
136 }
137
138
139