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 net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
8   import net.sourceforge.pmd.testframework.ParserTst;
9   
10  import org.junit.Test;
11  
12  import java.util.Set;
13  
14  public class ClassDeclTest extends ParserTst {
15  
16      @Test
17      public void testPublic() throws Throwable {
18          String[] access = {"public"};
19          ASTClassOrInterfaceDeclaration acd = getClassDecl(access);
20          verifyFlags(acd, true, false, false, false);
21      }
22  
23      @Test
24      public void testAbstract() throws Throwable {
25          String[] access = {"abstract"};
26          ASTClassOrInterfaceDeclaration acd = getClassDecl(access);
27          verifyFlags(acd, false, true, false, false);
28      }
29  
30      @Test
31      public void testFinal() throws Throwable {
32          String[] access = {"final"};
33          ASTClassOrInterfaceDeclaration acd = getClassDecl(access);
34          verifyFlags(acd, false, false, true, false);
35      }
36  
37      @Test
38      public void testStrict() throws Throwable {
39          String[] access = {"strictfp"};
40          ASTClassOrInterfaceDeclaration acd = getClassDecl(access);
41          verifyFlags(acd, false, false, false, true);
42      }
43  
44      @Test
45      public void testPublicFinal() throws Throwable {
46          String[] access = {"public", "final"};
47          ASTClassOrInterfaceDeclaration acd = getClassDecl(access);
48          verifyFlags(acd, true, false, true, false);
49      }
50  
51      public void verifyFlags(ASTClassOrInterfaceDeclaration acd, boolean bPublic, boolean bAbstract, boolean bFinal, boolean bStrict) {
52          assertEquals("Public: ", bPublic, acd.isPublic());
53          assertEquals("Abstract: ", bAbstract, acd.isAbstract());
54          assertEquals("Final: ", bFinal, acd.isFinal());
55          assertEquals("Strict: ", bStrict, acd.isStrictfp());
56      }
57  
58      public ASTClassOrInterfaceDeclaration getClassDecl(String[] access) throws Throwable {
59          String javaCode = "";
60  
61          for (int i = 0; i < access.length; i++) {
62              javaCode += access[i] + " ";
63          }
64  
65          javaCode += " class Test { } ";
66  
67          Set<ASTClassOrInterfaceDeclaration> classes = getNodes(ASTClassOrInterfaceDeclaration.class, javaCode);
68  
69          assertEquals("Wrong number of classes", 1, classes.size());
70          return classes.iterator().next();
71      }
72  
73      public static junit.framework.Test suite() {
74          return new junit.framework.JUnit4TestAdapter(ClassDeclTest.class);
75      }
76  }