View Javadoc

    1   /**
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.symboltable;
    5
    6   import static org.junit.Assert.assertEquals;
    7   import net.sourceforge.pmd.PMD;
    8   import net.sourceforge.pmd.lang.java.symboltable.TypeSet;
    9
    10  import org.junit.Test;
    11
    12  import java.io.File;
    13  import java.util.HashSet;
    14  import java.util.Set;
    15
    16  public class TypeSetTest {
    17
    18      @Test
    19      public void testASTCompilationUnitPackage() {
    20          TypeSet t = new TypeSet();
    21          t.setASTCompilationUnitPackage("java.lang.");
    22          assertEquals("java.lang.", t.getASTCompilationUnitPackage());
    23      }
    24
    25      @Test
    26      public void testAddImport() {
    27          TypeSet t = new TypeSet();
    28          t.addImport("java.io.File");
    29          assertEquals(1, t.getImportsCount());
    30      }
    31
    32      @Test
    33      public void testFindClassImplicitImport() throws Throwable {
    34          TypeSet t = new TypeSet();
    35          Class clazz = t.findClass("String");
    36          assertEquals(String.class, clazz);
    37      }
    38
    39      @Test
    40      public void testFindClassSamePackage() throws Throwable {
    41          TypeSet t = new TypeSet();
    42          t.setASTCompilationUnitPackage("net.sourceforge.pmd.");
    43          Class clazz = t.findClass("PMD");
    44          assertEquals(PMD.class, clazz);
    45      }
    46
    47      @Test
    48      public void testFindClassExplicitImport() throws Throwable {
    49          TypeSet t = new TypeSet();
    50          t.addImport("java.io.File");
    51          Class clazz = t.findClass("File");
    52          assertEquals(File.class, clazz);
    53      }
    54
    55      @Test
    56      public void testFindClassImportOnDemand() throws Throwable {
    57          TypeSet t = new TypeSet();
    58          t.addImport("java.io.*");
    59          Class clazz = t.findClass("File");
    60          assertEquals(File.class, clazz);
    61      }
    62
    63      @Test
    64      public void testFindClassPrimitive() throws Throwable {
    65          TypeSet t = new TypeSet();
    66          assertEquals(int.class, t.findClass("int"));
    67      }
    68
    69      @Test
    70      public void testFindClassVoid() throws Throwable {
    71          TypeSet t = new TypeSet();
    72          assertEquals(void.class, t.findClass("void"));
    73      }
    74
    75      @Test
    76      public void testFindFullyQualified() throws Throwable {
    77          TypeSet t = new TypeSet();
    78          assertEquals(String.class, t.findClass("java.lang.String"));
    79          assertEquals(Set.class, t.findClass("java.util.Set"));
    80      }
    81
    82      // inner class tests
    83      @Test
    84      public void testPrimitiveTypeResolver() throws Throwable {
    85          TypeSet.Resolver r = new TypeSet.PrimitiveTypeResolver();
    86          assertEquals(int.class, r.resolve("int"));
    87          assertEquals(byte.class, r.resolve("byte"));
    88          assertEquals(long.class, r.resolve("long"));
    89      }
    90
    91      @Test
    92      public void testVoidTypeResolver() throws Throwable {
    93          TypeSet.Resolver r = new TypeSet.VoidResolver();
    94          assertEquals(void.class, r.resolve("void"));
    95      }
    96
    97      @Test
    98      public void testExplicitImportResolver() throws Throwable {
    99          Set<String> imports = new HashSet<String>();
    100         imports.add("java.io.File");
    101         TypeSet.Resolver r = new TypeSet.ExplicitImportResolver(imports);
    102         assertEquals(File.class, r.resolve("File"));
    103     }
    104
    105     @Test
    106     public void testImplicitImportResolverPass() throws Throwable {
    107         TypeSet.Resolver r = new TypeSet.ImplicitImportResolver();
    108         assertEquals(String.class, r.resolve("String"));
    109     }
    110
    111     @Test(expected=ClassNotFoundException.class)
    112     public void testImplicitImportResolverPassFail() throws Throwable {
    113         TypeSet.Resolver r = new TypeSet.ImplicitImportResolver();
    114         r.resolve("PMD");
    115     }
    116
    117     @Test
    118     public void testCurrentPackageResolverPass() throws Throwable {
    119         TypeSet.Resolver r = new TypeSet.CurrentPackageResolver("net.sourceforge.pmd.");
    120         assertEquals(PMD.class, r.resolve("PMD"));
    121     }
    122
    123     @Test
    124     public void testImportOnDemandResolverPass() throws Throwable {
    125         TypeSet.Resolver r = getResolver();
    126         assertEquals(Set.class, r.resolve("Set"));
    127         assertEquals(File.class, r.resolve("File"));
    128     }
    129
    130     @Test(expected = ClassNotFoundException.class)
    131     public void importOnDemandResolverFail1() throws Throwable {
    132         TypeSet.Resolver r = getResolver();
    133         r.resolve("foo");
    134     }
    135
    136     @Test(expected = ClassNotFoundException.class)
    137     public void importOnDemandResolverFail2() throws Throwable {
    138         TypeSet.Resolver r = getResolver();
    139         r.resolve("String");
    140     }
    141
    142     private TypeSet.Resolver getResolver() {
    143         Set<String> imports = new HashSet<String>();
    144         imports.add("java.io.*");
    145         imports.add("java.util.*");
    146         TypeSet.Resolver r = new TypeSet.ImportOnDemandResolver(imports);
    147         return r;
    148     }
    149
    150     public static junit.framework.Test suite() {
    151         return new junit.framework.JUnit4TestAdapter(TypeSetTest.class);
    152     }
    153 }