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