View Javadoc

1   package net.sourceforge.pmd.ast;
2   
3   import static org.junit.Assert.assertFalse;
4   import static org.junit.Assert.assertTrue;
5   import net.sourceforge.pmd.PMD;
6   import net.sourceforge.pmd.lang.java.ast.ASTLiteral;
7   import net.sourceforge.pmd.testframework.ParserTst;
8   
9   import org.junit.Test;
10  
11  
12  import java.util.Set;
13  
14  public class ASTLiteralTest extends ParserTst {
15  
16      @Test
17      public void testIsStringLiteral() throws Throwable {
18          Set literals = getNodes(ASTLiteral.class, TEST1);
19          assertTrue(((ASTLiteral)(literals.iterator().next())).isStringLiteral());
20      }
21  
22      @Test
23      public void testIsNotStringLiteral() throws Throwable {
24          Set literals = getNodes(ASTLiteral.class, TEST2);
25          assertFalse(((ASTLiteral)(literals.iterator().next())).isStringLiteral());
26      }
27  
28      @Test
29      public void testIsIntIntLiteral() throws Throwable {
30          Set literals = getNodes(ASTLiteral.class, TEST3);
31          assertTrue(((ASTLiteral)(literals.iterator().next())).isIntLiteral());
32      }
33  
34      @Test
35      public void testIsIntLongLiteral() throws Throwable {
36          Set literals = getNodes(ASTLiteral.class, TEST4);
37          assertTrue(((ASTLiteral)(literals.iterator().next())).isIntLiteral());
38      }
39  
40      @Test
41      public void testIsFloatFloatLiteral() throws Throwable {
42          Set literals = getNodes(ASTLiteral.class, TEST5);
43          assertTrue(((ASTLiteral)(literals.iterator().next())).isFloatLiteral());
44      }
45  
46      @Test
47      public void testIsFloatDoubleLiteral() throws Throwable {
48          Set literals = getNodes(ASTLiteral.class, TEST6);
49          assertTrue(((ASTLiteral)(literals.iterator().next())).isFloatLiteral());
50      }
51  
52      @Test
53      public void testIsCharLiteral() throws Throwable {
54          Set literals = getNodes(ASTLiteral.class, TEST7);
55          assertTrue(((ASTLiteral)(literals.iterator().next())).isCharLiteral());
56      }
57  
58      private static final String TEST1 =
59      "public class Foo {" + PMD.EOL +
60      "  String x = \"foo\";" + PMD.EOL +
61      "}";
62  
63      private static final String TEST2 =
64      "public class Foo {" + PMD.EOL +
65      "  int x = 42;" + PMD.EOL +
66      "}";
67  
68      private static final String TEST3 =
69      "public class Foo {" + PMD.EOL +
70      "  int x = 42;" + PMD.EOL +
71      "}";
72  
73      private static final String TEST4 =
74      "public class Foo {" + PMD.EOL +
75      "  int x = 42L;" + PMD.EOL +
76      "}";
77  
78      private static final String TEST5 =
79      "public class Foo {" + PMD.EOL +
80      "  float x = 3.14159f;" + PMD.EOL +
81      "}";
82  
83      private static final String TEST6 =
84      "public class Foo {" + PMD.EOL +
85      "  float x = 3.14159;" + PMD.EOL +
86      "}";
87  
88      private static final String TEST7 =
89      "public class Foo {" + PMD.EOL +
90      "  char x = 'x';" + PMD.EOL +
91      "}";
92  
93      public static junit.framework.Test suite() {
94          return new junit.framework.JUnit4TestAdapter(ASTLiteralTest.class);
95      }
96  }