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

This page was automatically generated by Maven