    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package test.net.sourceforge.pmd.rules.design;
    5
    6   import net.sourceforge.pmd.PMD;
    7   import net.sourceforge.pmd.Report;
    8   import net.sourceforge.pmd.Rule;
    9   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    10  import test.net.sourceforge.pmd.testframework.TestDescriptor;
    11
    12  public class TooManyFieldsTest extends SimpleAggregatorTst {
    13
    14      private Rule rule;
    15
    16      public void setUp() {
    17          rule = findRule("codesize", "TooManyFields");
    18      }
    19
    20      public void testAll() {
    21          runTests(new TestDescriptor[]{
    22              new TestDescriptor(TEST1, "3 fields, max is 15", 0, rule),
    23              new TestDescriptor(TEST2, "16 fields, bad", 1, rule),
    24              new TestDescriptor(TEST3, "12 fields, but 6 in inner and 6 in outer", 0, rule),
    25              new TestDescriptor(TEST4, "outer class, inner interface, both OK", 0, rule),
    26              new TestDescriptor(TEST5, "interface with 10 fields", 0, rule),
    27              new TestDescriptor(TEST6, "2 inner classes, each with > 10 fields", 2, rule),
    28              new TestDescriptor(TEST7, "anonymous class with a field", 0, rule),
    29              new TestDescriptor(TEST9, "lots of static finals, those are ok", 0, rule),
    30          });
    31      }
    32
    33      public void testPropertySetting() throws Throwable {
    34          Report rpt = new Report();
    35          rule.addProperty("maxfields", "2");
    36          runTestFromString(TEST8, rule, rpt);
    37          assertEquals(1, rpt.size());
    38      }
    39
    40      private static final String TEST1 =
    41              "public class Foo {" + PMD.EOL +
    42              " int a1; " + PMD.EOL +
    43              " int a2; " + PMD.EOL +
    44              " int a3; " + PMD.EOL +
    45              "}";
    46
    47      private static final String TEST2 =
    48              "public class Foo {" + PMD.EOL +
    49              " int a1; " + PMD.EOL +
    50              " int a2; " + PMD.EOL +
    51              " int a3; " + PMD.EOL +
    52              " int a4; " + PMD.EOL +
    53              " int a5; " + PMD.EOL +
    54              " int a6; " + PMD.EOL +
    55              " int a7; " + PMD.EOL +
    56              " int a8; " + PMD.EOL +
    57              " int a9; " + PMD.EOL +
    58              " int a10; " + PMD.EOL +
    59              " int a11; " + PMD.EOL +
    60              " int a12; " + PMD.EOL +
    61              " int a13; " + PMD.EOL +
    62              " int a14; " + PMD.EOL +
    63              " int a15; " + PMD.EOL +
    64              " int a16; " + PMD.EOL +
    65              "}";
    66
    67      private static final String TEST3 =
    68              "public class Foo {" + PMD.EOL +
    69              " int a1; " + PMD.EOL +
    70              " int a2; " + PMD.EOL +
    71              " int a3; " + PMD.EOL +
    72              " int a4; " + PMD.EOL +
    73              " int a5; " + PMD.EOL +
    74              " int a6; " + PMD.EOL +
    75              " public class Bar {" + PMD.EOL +
    76              "  int a7; " + PMD.EOL +
    77              "  int a8; " + PMD.EOL +
    78              "  int a9; " + PMD.EOL +
    79              "  int a10; " + PMD.EOL +
    80              "  int a11; " + PMD.EOL +
    81              "  int a12; " + PMD.EOL +
    82              " }" +
    83              "}";
    84
    85      private static final String TEST4 =
    86              "public class Foo {" + PMD.EOL +
    87              " int a1; " + PMD.EOL +
    88              " int a2; " + PMD.EOL +
    89              " int a3; " + PMD.EOL +
    90              " int a4; " + PMD.EOL +
    91              " int a5; " + PMD.EOL +
    92              " interface Bar {" + PMD.EOL +
    93              "  int a6; " + PMD.EOL +
    94              "  int a7; " + PMD.EOL +
    95              "  int a8; " + PMD.EOL +
    96              "  int a9; " + PMD.EOL +
    97              "  int a10; " + PMD.EOL +
    98              " }" +
    99              "}";
    100
    101     private static final String TEST5 =
    102             "public interface Foo {" + PMD.EOL +
    103             " int a1; " + PMD.EOL +
    104             " int a2; " + PMD.EOL +
    105             " int a3; " + PMD.EOL +
    106             " int a4; " + PMD.EOL +
    107             " int a5; " + PMD.EOL +
    108             " int a6; " + PMD.EOL +
    109             " int a7; " + PMD.EOL +
    110             " int a8; " + PMD.EOL +
    111             " int a9; " + PMD.EOL +
    112             " int a10; " + PMD.EOL +
    113             "}";
    114
    115     private static final String TEST6 =
    116             "public class Foo {" + PMD.EOL +
    117             " public class Bar1 {" + PMD.EOL +
    118             "  int b1; " + PMD.EOL +
    119             "  int b2; " + PMD.EOL +
    120             "  int b3; " + PMD.EOL +
    121             "  int b4; " + PMD.EOL +
    122             "  int b5; " + PMD.EOL +
    123             "  int b6; " + PMD.EOL +
    124             "  int b7; " + PMD.EOL +
    125             "  int b8; " + PMD.EOL +
    126             "  int b9; " + PMD.EOL +
    127             "  int b10; " + PMD.EOL +
    128             "  int b11; " + PMD.EOL +
    129             "  int b12; " + PMD.EOL +
    130             "  int b13; " + PMD.EOL +
    131             "  int b14; " + PMD.EOL +
    132             "  int b15; " + PMD.EOL +
    133             "  int b16; " + PMD.EOL +
    134             " }" + PMD.EOL +
    135             " public class Bar2 {" + PMD.EOL +
    136             "  int b1; " + PMD.EOL +
    137             "  int b2; " + PMD.EOL +
    138             "  int b3; " + PMD.EOL +
    139             "  int b4; " + PMD.EOL +
    140             "  int b5; " + PMD.EOL +
    141             "  int b6; " + PMD.EOL +
    142             "  int b7; " + PMD.EOL +
    143             "  int b8; " + PMD.EOL +
    144             "  int b9; " + PMD.EOL +
    145             "  int b10; " + PMD.EOL +
    146             "  int b11; " + PMD.EOL +
    147             "  int b12; " + PMD.EOL +
    148             "  int b13; " + PMD.EOL +
    149             "  int b14; " + PMD.EOL +
    150             "  int b15; " + PMD.EOL +
    151             "  int b16; " + PMD.EOL +
    152             " }" + PMD.EOL +
    153             "}";
    154
    155     private static final String TEST7 =
    156             "public class Foo {" + PMD.EOL +
    157             " public class Bar1 {" + PMD.EOL +
    158             "  Object foo = new Object() {public int x;}; " + PMD.EOL +
    159             " }" + PMD.EOL +
    160             "}";
    161
    162     private static final String TEST8 =
    163             "public class Foo {" + PMD.EOL +
    164             " int a1; " + PMD.EOL +
    165             " int a2; " + PMD.EOL +
    166             " int a3; " + PMD.EOL +
    167             "}";
    168
    169     private static final String TEST9 =
    170             "public class Foo {" + PMD.EOL +
    171             " public static final int a1 = 2 ; " + PMD.EOL +
    172             " public static final int a2 = 2 ; " + PMD.EOL +
    173             " public static final int a3 = 2 ; " + PMD.EOL +
    174             " public static final int a4 = 2 ; " + PMD.EOL +
    175             " public static final int a5 = 2 ; " + PMD.EOL +
    176             " public static final int a6 = 2 ; " + PMD.EOL +
    177             " public static final int a7 = 2 ; " + PMD.EOL +
    178             " public static final int a8 = 2 ; " + PMD.EOL +
    179             " public static final int a9 = 2 ; " + PMD.EOL +
    180             " public static final int a10 = 2 ; " + PMD.EOL +
    181             " public static final int a11 = 2 ; " + PMD.EOL +
    182             " public static final int a12 = 2 ; " + PMD.EOL +
    183             " public static final int a13 = 2 ; " + PMD.EOL +
    184             " public static final int a14 = 2 ; " + PMD.EOL +
    185             " public static final int a15 = 2 ; " + PMD.EOL +
    186             " public static final int a16 = 2 ; " + PMD.EOL +
    187             " public static final int a17 = 2 ; " + PMD.EOL +
    188             "}";
    189
    190
    191 }