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.ASTMethodDeclaration;
    9   import net.sourceforge.pmd.testframework.ParserTst;
    10
    11  import org.junit.Test;
    12
    13  import java.util.Set;
    14
    15  public class MethodDeclTest extends ParserTst {
    16
    17      @Test
    18      public void testPublic() throws Throwable {
    19          String[] access = {"public"};
    20          ASTMethodDeclaration amd = getMethodDecl(access);
    21          assertTrue("Expecting method to be public.", amd.isPublic());
    22      }
    23
    24      @Test
    25      public void testPrivate() throws Throwable {
    26          String[] access = {"private"};
    27          ASTMethodDeclaration amd = getMethodDecl(access);
    28          assertTrue("Expecting method to be private.", amd.isPrivate());
    29      }
    30
    31      @Test
    32      public void testProtected() throws Throwable {
    33          String[] access = {"protected"};
    34          ASTMethodDeclaration amd = getMethodDecl(access);
    35          assertTrue("Expecting method to be protected.", amd.isProtected());
    36      }
    37
    38      @Test
    39      public void testFinal() throws Throwable {
    40          String[] access = {"public", "final"};
    41          ASTMethodDeclaration amd = getMethodDecl(access);
    42          assertTrue("Expecting method to be final.", amd.isFinal());
    43          assertTrue("Expecting method to be public.", amd.isPublic());
    44      }
    45
    46      @Test
    47      public void testSynchronized() throws Throwable {
    48          String[] access = {"public", "synchronized"};
    49          ASTMethodDeclaration amd = getMethodDecl(access);
    50          assertTrue("Expecting method to be synchronized.", amd.isSynchronized());
    51          assertTrue("Expecting method to be public.", amd.isPublic());
    52      }
    53
    54      @Test
    55      public void testAbstract() throws Throwable {
    56          String[] access = {"public", "abstract"};
    57          ASTMethodDeclaration amd = getMethodDecl(access);
    58          assertTrue("Expecting method to be abstract.", amd.isAbstract());
    59          assertTrue("Expecting method to be public.", amd.isPublic());
    60      }
    61
    62      @Test
    63      public void testNative() throws Throwable {
    64          String[] access = {"private", "native"};
    65          ASTMethodDeclaration amd = getMethodDecl(access);
    66          assertTrue("Expecting method to be native.", amd.isNative());
    67          assertTrue("Expecting method to be private.", amd.isPrivate());
    68      }
    69
    70      @Test
    71      public void testStrict() throws Throwable {
    72          String[] access = {"public", "strictfp"};
    73          ASTMethodDeclaration amd = getMethodDecl(access);
    74          assertTrue("Expecting method to be strict.", amd.isStrictfp());
    75          assertTrue("Expecting method to be public.", amd.isPublic());
    76      }
    77
    78      public ASTMethodDeclaration getMethodDecl(String[] access) throws Throwable {
    79          String javaCode = "public class Test { ";
    80          for (int i = 0; i < access.length; i++) {
    81              javaCode += access[i] + " ";
    82          }
    83
    84          javaCode += " void stuff() { } }";
    85
    86          Set<ASTMethodDeclaration> methods = getNodes(ASTMethodDeclaration.class, javaCode);
    87
    88          assertEquals("Wrong number of methods", 1, methods.size());
    89
    90          return methods.iterator().next();
    91      }
    92
    93      public static junit.framework.Test suite() {
    94          return new junit.framework.JUnit4TestAdapter(MethodDeclTest.class);
    95      }
    96  }