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