    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package test.net.sourceforge.pmd.ast;
    5
    6   import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
    7   import test.net.sourceforge.pmd.testframework.ParserTst;
    8
    9   import java.util.Iterator;
    10  import java.util.Set;
    11
    12  public class ClassDeclTest extends ParserTst {
    13
    14      public void testPublic() throws Throwable {
    15          String access[] = {"public"};
    16          ASTClassOrInterfaceDeclaration acd = getClassDecl(access);
    17          verifyFlags(acd, true, false, false, false);
    18      }
    19
    20      public void testAbstract() throws Throwable {
    21          String access[] = {"abstract"};
    22          ASTClassOrInterfaceDeclaration acd = getClassDecl(access);
    23          verifyFlags(acd, false, true, false, false);
    24      }
    25
    26      public void testFinal() throws Throwable {
    27          String access[] = {"final"};
    28          ASTClassOrInterfaceDeclaration acd = getClassDecl(access);
    29          verifyFlags(acd, false, false, true, false);
    30      }
    31
    32      public void testStrict() throws Throwable {
    33          String access[] = {"strictfp"};
    34          ASTClassOrInterfaceDeclaration acd = getClassDecl(access);
    35          verifyFlags(acd, false, false, false, true);
    36      }
    37
    38      public void testPublicFinal() throws Throwable {
    39          String access[] = {"public", "final"};
    40          ASTClassOrInterfaceDeclaration acd = getClassDecl(access);
    41          verifyFlags(acd, true, false, true, false);
    42      }
    43
    44      public void verifyFlags(ASTClassOrInterfaceDeclaration acd, boolean bPublic, boolean bAbstract, boolean bFinal, boolean bStrict) {
    45          assertEquals("Public: ", bPublic, acd.isPublic());
    46          assertEquals("Abstract: ", bAbstract, acd.isAbstract());
    47          assertEquals("Final: ", bFinal, acd.isFinal());
    48          assertEquals("Strict: ", bStrict, acd.isStrictfp());
    49      }
    50
    51      public ASTClassOrInterfaceDeclaration getClassDecl(String access[]) throws Throwable {
    52          String javaCode = "";
    53
    54          for (int i = 0; i < access.length; i++) {
    55              javaCode += access[i] + " ";
    56          }
    57
    58          javaCode += " class Test { } ";
    59
    60          Set classes = getNodes(ASTClassOrInterfaceDeclaration.class, javaCode);
    61
    62          assertEquals("Wrong number of classes", 1, classes.size());
    63          Iterator i = classes.iterator();
    64          return (ASTClassOrInterfaceDeclaration) i.next();
    65      }
    66  }