1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package test.net.sourceforge.pmd.ast;
5   
6   import net.sourceforge.pmd.PMD;
7   import net.sourceforge.pmd.TargetJDK1_5;
8   import net.sourceforge.pmd.ast.ASTImportDeclaration;
9   import net.sourceforge.pmd.ast.ParseException;
10  import test.net.sourceforge.pmd.testframework.ParserTst;
11  
12  import java.util.Set;
13  
14  public class ASTImportDeclarationTest extends ParserTst {
15  
16      public void testImportOnDemand() throws Throwable {
17          Set ops = getNodes(ASTImportDeclaration.class, TEST1);
18          assertTrue(((ASTImportDeclaration) (ops.iterator().next())).isImportOnDemand());
19      }
20  
21      public void testGetImportedNameNode() throws Throwable {
22          ASTImportDeclaration i = (ASTImportDeclaration) (getNodes(ASTImportDeclaration.class, TEST2).iterator().next());
23          assertEquals("foo.bar.Baz", i.getImportedName());
24      }
25  
26      public void testStaticImport() throws Throwable {
27          Set ops = getNodes(new TargetJDK1_5(), ASTImportDeclaration.class, TEST3);
28          ASTImportDeclaration i = (ASTImportDeclaration) (ops.iterator().next());
29          assertTrue(i.isStatic());
30      }
31  
32      public void testStaticImportFailsWithJDK14() throws Throwable {
33          try {
34              getNodes(ASTImportDeclaration.class, TEST3);
35              fail("Should have failed to parse a static import in JDK 1.4 mode");
36          } catch (ParseException pe) {
37              // cool
38          }
39      }
40  
41      private static final String TEST1 =
42              "import foo.bar.*;" + PMD.EOL +
43              "public class Foo {}";
44  
45      private static final String TEST2 =
46              "import foo.bar.Baz;" + PMD.EOL +
47              "public class Foo {}";
48  
49      private static final String TEST3 =
50              "import static foo.bar.Baz;" + PMD.EOL +
51              "public class Foo {}";
52  
53      private static final String TEST4 =
54              "import foo.bar.Baz;" + PMD.EOL +
55              "public class Foo {}";
56  
57  }