View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.ast;
5   
6   import static org.junit.Assert.assertEquals;
7   import static org.junit.Assert.assertTrue;
8   
9   import java.util.Set;
10  
11  import net.sourceforge.pmd.PMD;
12  import net.sourceforge.pmd.lang.LanguageVersion;
13  import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
14  import net.sourceforge.pmd.lang.java.ast.ParseException;
15  import net.sourceforge.pmd.testframework.ParserTst;
16  
17  import org.junit.Test;
18  
19  
20  public class ASTImportDeclarationTest extends ParserTst {
21  
22      @Test
23      public void testImportOnDemand() throws Throwable {
24          Set<ASTImportDeclaration> ops = getNodes(ASTImportDeclaration.class, TEST1);
25          assertTrue(ops.iterator().next().isImportOnDemand());
26      }
27  
28      @Test
29      public void testGetImportedNameNode() throws Throwable {
30          ASTImportDeclaration i = getNodes(ASTImportDeclaration.class, TEST2).iterator().next();
31          assertEquals("foo.bar.Baz", i.getImportedName());
32      }
33  
34      @Test
35      public void testStaticImport() throws Throwable {
36          Set<ASTImportDeclaration> ops = getNodes(ASTImportDeclaration.class, TEST3);
37          ASTImportDeclaration i = ops.iterator().next();
38          assertTrue(i.isStatic());
39      }
40  
41      @Test(expected = ParseException.class)
42      public void testStaticImportFailsWithJDK14() throws Throwable {
43          getNodes(LanguageVersion.JAVA_14, ASTImportDeclaration.class, TEST3);
44      }
45  
46      private static final String TEST1 =
47              "import foo.bar.*;" + PMD.EOL +
48              "public class Foo {}";
49  
50      private static final String TEST2 =
51              "import foo.bar.Baz;" + PMD.EOL +
52              "public class Foo {}";
53  
54      private static final String TEST3 =
55              "import static foo.bar.Baz;" + PMD.EOL +
56              "public class Foo {}";
57  
58      public static junit.framework.Test suite() {
59          return new junit.framework.JUnit4TestAdapter(ASTImportDeclarationTest.class);
60      }
61  }