    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package test.net.sourceforge.pmd.rules.javabeans;
    5
    6   import net.sourceforge.pmd.PMD;
    7   import net.sourceforge.pmd.Rule;
    8   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    9   import test.net.sourceforge.pmd.testframework.TestDescriptor;
    10
    11  public class BeanMembersShouldSerializeRuleTest extends SimpleAggregatorTst {
    12
    13      private Rule rule;
    14
    15      public void setUp() throws Exception {
    16          rule = findRule("javabeans", "BeanMembersShouldSerialize");
    17      }
    18
    19      public void testAll() {
    20          runTests(new TestDescriptor[]{
    21              new TestDescriptor(TEST1, "private String, no accessor", 1, rule),
    22              new TestDescriptor(TEST2, "private static String", 0, rule),
    23              new TestDescriptor(TEST3, "private transient String", 0, rule),
    24              new TestDescriptor(TEST4, "getter, no setter", 1, rule),
    25              new TestDescriptor(TEST5, "setter, no getter", 1, rule),
    26              new TestDescriptor(TEST6, "both accessors, yay!", 0, rule),
    27              new TestDescriptor(TEST7, "setFoo and isFoo is OK for booleans", 0, rule),
    28              new TestDescriptor(TEST8, "setFoo and isFoo is not OK for Strings", 1, rule),
    29              new TestDescriptor(TEST9, "prefix is off by default", 1, rule),
    30          });
    31      }
    32
    33      public void testPrefixProperty() throws Throwable {
    34          Rule lclrule = findRule("javabeans", "BeanMembersShouldSerialize");
    35          lclrule.addProperty("prefix", "m_");
    36          runTestFromString(TEST10, 0, lclrule);
    37      }
    38
    39      private static final String TEST1 =
    40              "public class Foo {" + PMD.EOL +
    41              " private String foo;" + PMD.EOL +
    42              " private String bar = foo;" + PMD.EOL +
    43              "}";
    44
    45      private static final String TEST2 =
    46              "public class Foo {" + PMD.EOL +
    47              " private static String foo;" + PMD.EOL +
    48              " private String bar = Foo.foo;" + PMD.EOL +
    49              "}";
    50
    51      private static final String TEST3 =
    52              "public class Foo {" + PMD.EOL +
    53              " private transient String foo;" + PMD.EOL +
    54              " private String bar = Foo.foo;" + PMD.EOL +
    55              "}";
    56
    57      private static final String TEST4 =
    58              "public class Foo {" + PMD.EOL +
    59              " private String foo;" + PMD.EOL +
    60              " private String bar = Foo.foo;" + PMD.EOL +
    61              " public String getFoo() {return foo;}" + PMD.EOL +
    62              "}";
    63
    64      private static final String TEST5 =
    65              "public class Foo {" + PMD.EOL +
    66              " private String foo;" + PMD.EOL +
    67              " private String bar = Foo.foo;" + PMD.EOL +
    68              " public void setFoo(Foo foo) {this.foo = foo;}" + PMD.EOL +
    69              "}";
    70
    71      private static final String TEST6 =
    72              "public class Foo {" + PMD.EOL +
    73              " private String foo;" + PMD.EOL +
    74              " private String bar = Foo.foo;" + PMD.EOL +
    75              " public void setFoo(Foo foo) {this.foo = foo;}" + PMD.EOL +
    76              " public String getFoo() {return foo;}" + PMD.EOL +
    77              "}";
    78
    79      private static final String TEST7 =
    80              "public class Foo {" + PMD.EOL +
    81              " private boolean foo;" + PMD.EOL +
    82              " public void setFoo(boolean foo) {this.foo = foo;}" + PMD.EOL +
    83              " public boolean isFoo() {return foo;}" + PMD.EOL +
    84              "}";
    85
    86      private static final String TEST8 =
    87              "public class Foo {" + PMD.EOL +
    88              " private String foo;" + PMD.EOL +
    89              " public void setFoo(String foo) {this.foo = foo;}" + PMD.EOL +
    90              " public String isFoo() {return foo;}" + PMD.EOL +
    91              "}";
    92
    93      private static final String TEST9 =
    94              "public class Foo {" + PMD.EOL +
    95              " private String m_foo;" + PMD.EOL +
    96              " public void setFoo(String foo) {m_foo = foo;}" + PMD.EOL +
    97              " public String getFoo() {return m_foo;}" + PMD.EOL +
    98              "}";
    99
    100     private static final String TEST10 =
    101             "public class Foo {" + PMD.EOL +
    102             " private String m_foo;" + PMD.EOL +
    103             " public void setFoo(String foo) {m_foo = foo;}" + PMD.EOL +
    104             " public String getFoo() {return m_foo;}" + PMD.EOL +
    105             "}";
    106
    107 }