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.assertFalse;
    7   import static org.junit.Assert.assertTrue;
    8
    9   import java.util.Set;
    10
    11  import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
    12  import net.sourceforge.pmd.lang.java.ast.AbstractJavaAccessNode;
    13  import net.sourceforge.pmd.lang.java.ast.AccessNode;
    14  import net.sourceforge.pmd.lang.java.ast.JavaParser;
    15  import net.sourceforge.pmd.testframework.ParserTst;
    16
    17  import org.junit.Test;
    18
    19
    20  public class AccessNodeTest extends ParserTst {
    21
    22      public static class MyAccessNode extends AbstractJavaAccessNode {
    23  	public MyAccessNode(int i) {
    24  	    super(i);
    25  	}
    26
    27  	public MyAccessNode(JavaParser parser, int i) {
    28  	    super(parser, i);
    29  	}
    30      }
    31
    32      @Test
    33      public void testModifiersOnClassDecl() throws Throwable {
    34          Set ops = getNodes(ASTClassOrInterfaceDeclaration.class, TEST1);
    35          assertTrue(((ASTClassOrInterfaceDeclaration) ops.iterator().next()).isPublic());
    36      }
    37
    38      private static final String TEST1 =
    39              "public class Foo {}";
    40
    41
    42      @Test
    43      public void testStatic() {
    44          AccessNode node = new MyAccessNode(1);
    45          assertFalse("Node should default to not static.", node.isStatic());
    46          node.setStatic(true);
    47          assertTrue("Node set to static, not static.", node.isStatic());
    48      }
    49
    50      @Test
    51      public void testPublic() {
    52          AccessNode node = new MyAccessNode(1);
    53          assertFalse("Node should default to not public.", node.isPublic());
    54          node.setPublic(true);
    55          assertTrue("Node set to public, not public.", node.isPublic());
    56      }
    57
    58      @Test
    59      public void testProtected() {
    60          AccessNode node = new MyAccessNode(1);
    61          assertFalse("Node should default to not protected.", node.isProtected());
    62          node.setProtected(true);
    63          assertTrue("Node set to protected, not protected.", node.isProtected());
    64      }
    65
    66      @Test
    67      public void testPrivate() {
    68          AccessNode node = new MyAccessNode(1);
    69          assertFalse("Node should default to not private.", node.isPrivate());
    70          node.setPrivate(true);
    71          assertTrue("Node set to private, not private.", node.isPrivate());
    72      }
    73
    74      @Test
    75      public void testFinal() {
    76          AccessNode node = new MyAccessNode(1);
    77          assertFalse("Node should default to not final.", node.isFinal());
    78          node.setFinal(true);
    79          assertTrue("Node set to final, not final.", node.isFinal());
    80      }
    81
    82      @Test
    83      public void testSynchronized() {
    84          AccessNode node = new MyAccessNode(1);
    85          assertFalse("Node should default to not synchronized.", node.isSynchronized());
    86          node.setSynchronized(true);
    87          assertTrue("Node set to synchronized, not synchronized.", node.isSynchronized());
    88      }
    89
    90      @Test
    91      public void testVolatile() {
    92          AccessNode node = new MyAccessNode(1);
    93          assertFalse("Node should default to not volatile.", node.isVolatile());
    94          node.setVolatile(true);
    95          assertTrue("Node set to volatile, not volatile.", node.isVolatile());
    96      }
    97
    98      @Test
    99      public void testTransient() {
    100         AccessNode node = new MyAccessNode(1);
    101         assertFalse("Node should default to not transient.", node.isTransient());
    102         node.setTransient(true);
    103         assertTrue("Node set to transient, not transient.", node.isTransient());
    104     }
    105
    106     @Test
    107     public void testNative() {
    108         AccessNode node = new MyAccessNode(1);
    109         assertFalse("Node should default to not native.", node.isNative());
    110         node.setNative(true);
    111         assertTrue("Node set to native, not native.", node.isNative());
    112     }
    113
    114     @Test
    115     public void testAbstract() {
    116         AccessNode node = new MyAccessNode(1);
    117         assertFalse("Node should default to not abstract.", node.isAbstract());
    118         node.setAbstract(true);
    119         assertTrue("Node set to abstract, not abstract.", node.isAbstract());
    120     }
    121
    122     @Test
    123     public void testStrict() {
    124         AccessNode node = new MyAccessNode(1);
    125         assertFalse("Node should default to not strict.", node.isStrictfp());
    126         node.setStrictfp(true);
    127         assertTrue("Node set to strict, not strict.", node.isStrictfp());
    128     }
    129
    130     @Test
    131     public void testPackagePrivate() {
    132         AccessNode node = new MyAccessNode(1);
    133         assertTrue("Node should default to package private.", node.isPackagePrivate());
    134         node.setPrivate(true);
    135         assertFalse("Node set to private, still package private.", node.isPackagePrivate());
    136         node = new MyAccessNode(1);
    137         node.setPublic(true);
    138         assertFalse("Node set to public, still package private.", node.isPackagePrivate());
    139         node = new MyAccessNode(1);
    140         node.setProtected(true);
    141         assertFalse("Node set to protected, still package private.", node.isPackagePrivate());
    142     }
    143
    144     public static junit.framework.Test suite() {
    145         return new junit.framework.JUnit4TestAdapter(AccessNodeTest.class);
    146     }
    147 }