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   import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
    9   import net.sourceforge.pmd.testframework.ParserTst;
    10
    11  import org.junit.Test;
    12
    13  import java.util.Set;
    14
    15  public class FieldDeclTest extends ParserTst {
    16
    17      public String makeAccessJavaCode(String[] access) {
    18          String result = "public class Test { ";
    19          for (int i = 0; i < access.length; i++) {
    20              result += access[i] + " ";
    21          }
    22          return result + " int j;  }";
    23      }
    24
    25      public ASTFieldDeclaration getFieldDecl(String[] access) throws Throwable {
    26          Set<ASTFieldDeclaration> fields = getNodes(ASTFieldDeclaration.class, makeAccessJavaCode(access));
    27
    28          assertEquals("Wrong number of fields", 1, fields.size());
    29          return fields.iterator().next();
    30      }
    31
    32      @Test
    33      public void testPublic() throws Throwable {
    34          String[] access = {"public"};
    35          ASTFieldDeclaration afd = getFieldDecl(access);
    36          assertTrue("Expecting field to be public.", afd.isPublic());
    37      }
    38
    39      @Test
    40      public void testProtected() throws Throwable {
    41          String[] access = {"protected"};
    42          ASTFieldDeclaration afd = getFieldDecl(access);
    43          assertTrue("Expecting field to be protected.", afd.isProtected());
    44      }
    45
    46      @Test
    47      public void testPrivate() throws Throwable {
    48          String[] access = {"private"};
    49          ASTFieldDeclaration afd = getFieldDecl(access);
    50          assertTrue("Expecting field to be private.", afd.isPrivate());
    51      }
    52
    53      @Test
    54      public void testStatic() throws Throwable {
    55          String[] access = {"private", "static"};
    56          ASTFieldDeclaration afd = getFieldDecl(access);
    57          assertTrue("Expecting field to be static.", afd.isStatic());
    58          assertTrue("Expecting field to be private.", afd.isPrivate());
    59      }
    60
    61      @Test
    62      public void testFinal() throws Throwable {
    63          String[] access = {"public", "final"};
    64          ASTFieldDeclaration afd = getFieldDecl(access);
    65          assertTrue("Expecting field to be final.", afd.isFinal());
    66          assertTrue("Expecting field to be public.", afd.isPublic());
    67      }
    68
    69      @Test
    70      public void testTransient() throws Throwable {
    71          String[] access = {"private", "transient"};
    72          ASTFieldDeclaration afd = getFieldDecl(access);
    73          assertTrue("Expecting field to be private.", afd.isPrivate());
    74          assertTrue("Expecting field to be transient.", afd.isTransient());
    75      }
    76
    77      @Test
    78      public void testVolatile() throws Throwable {
    79          String[] access = {"private", "volatile"};
    80          ASTFieldDeclaration afd = getFieldDecl(access);
    81          assertTrue("Expecting field to be volatile.", afd.isVolatile());
    82          assertTrue("Expecting field to be private.", afd.isPrivate());
    83      }
    84
    85      public static junit.framework.Test suite() {
    86          return new junit.framework.JUnit4TestAdapter(FieldDeclTest.class);
    87      }
    88  }