    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package test.net.sourceforge.pmd.rules;
    5
    6   import net.sourceforge.pmd.PMD;
    7   import net.sourceforge.pmd.Rule;
    8   import net.sourceforge.pmd.RuleSetNotFoundException;
    9   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    10  import test.net.sourceforge.pmd.testframework.TestDescriptor;
    11
    12  public class UnusedModifierRuleTest extends SimpleAggregatorTst {
    13
    14      private Rule rule;
    15
    16      public void setUp() throws RuleSetNotFoundException {
    17          rule = findRule("controversial", "UnusedModifier");
    18      }
    19
    20      public void testAll() {
    21          runTests(new TestDescriptor[]{
    22              new TestDescriptor(TEST1, "Unneeded 'public' in interface method", 1, rule),
    23              new TestDescriptor(TEST2, "class, no problem", 0, rule),
    24              new TestDescriptor(TEST3, "Unneeded 'abstract' in interface method", 1, rule),
    25              new TestDescriptor(TEST4, "all is well in interface method", 0, rule),
    26              new TestDescriptor(TEST5, "Unneeded 'public' in interface field", 1, rule),
    27              new TestDescriptor(TEST6, "Unneeded 'static' in interface field", 1, rule),
    28              new TestDescriptor(TEST7, "Unneeded 'final' in interface field", 1, rule),
    29              new TestDescriptor(TEST8, "Unneeded 'public static final' in interface field", 1, rule),
    30              new TestDescriptor(TEST9, "OK in interface field", 0, rule),
    31              new TestDescriptor(TEST10, "Unneeded 'public' in class nested in interface", 1, rule),
    32              new TestDescriptor(TEST11, "Unneeded 'static' in class nested in interface", 1, rule),
    33              new TestDescriptor(TEST12, "OK in class nested in interface", 0, rule),
    34              new TestDescriptor(TEST13, "Unneeded 'public' in interface nested in interface", 1, rule),
    35              new TestDescriptor(TEST14, "Unneeded 'static' in interface nested in interface", 1, rule),
    36              new TestDescriptor(TEST15, "OK in interface nested in interface", 0, rule),
    37              new TestDescriptor(TEST16, "Unneeded 'static' in interface nested in class", 1, rule),
    38              new TestDescriptor(TEST17, "OK in interface nested in class", 0, rule),
    39              new TestDescriptor(TEST18, "Unneeded 'public static final' in interface field inside another interface", 2, rule),
    40              new TestDescriptor(TEST19, "OK in interface field inside another interface", 0, rule),
    41              new TestDescriptor(TEST20, "Don't check methods in nested classes", 0, rule),
    42              new TestDescriptor(TEST21, "Don't check fields in nested classes", 0, rule),
    43              new TestDescriptor(TEST22, "Don't check fields that are anonymous inner classes", 0, rule),
    44          });
    45      }
    46
    47      private static final String TEST1 =
    48              "public interface Foo {" + PMD.EOL +
    49              " public void bar();" + PMD.EOL +
    50              "}";
    51
    52      private static final String TEST2 =
    53              "public abstract class Foo {" + PMD.EOL +
    54              " public abstract void bar();" + PMD.EOL +
    55              "}";
    56
    57      private static final String TEST3 =
    58              "public interface Foo {" + PMD.EOL +
    59              " abstract void bar();" + PMD.EOL +
    60              "}";
    61
    62      private static final String TEST4 =
    63              "public interface Foo {" + PMD.EOL +
    64              " void bar();" + PMD.EOL +
    65              "}";
    66
    67      private static final String TEST5 =
    68              "public interface Foo {" + PMD.EOL +
    69              " public int X = 0;" + PMD.EOL +
    70              "}";
    71
    72      private static final String TEST6 =
    73              "public interface Foo {" + PMD.EOL +
    74              " static int X = 0;" + PMD.EOL +
    75              "}";
    76
    77      private static final String TEST7 =
    78              "public interface Foo {" + PMD.EOL +
    79              " final int X = 0;" + PMD.EOL +
    80              "}";
    81
    82      private static final String TEST8 =
    83              "public interface Foo {" + PMD.EOL +
    84              " public static final int X = 0;" + PMD.EOL +
    85              "}";
    86
    87      private static final String TEST9 =
    88              "public interface Foo {" + PMD.EOL +
    89              " int X = 0;" + PMD.EOL +
    90              "}";
    91
    92      private static final String TEST10 =
    93              "public interface Foo {" + PMD.EOL +
    94              " public class Bar {}" + PMD.EOL +
    95              "}";
    96
    97      private static final String TEST11 =
    98              "public interface Foo {" + PMD.EOL +
    99              " static class Bar {}" + PMD.EOL +
    100             "}";
    101
    102     private static final String TEST12 =
    103             "public interface Foo {" + PMD.EOL +
    104             " class Bar {}" + PMD.EOL +
    105             "}";
    106
    107     private static final String TEST13 =
    108             "public interface Foo {" + PMD.EOL +
    109             " public interface Baz {}" + PMD.EOL +
    110             "}";
    111
    112     private static final String TEST14 =
    113             "public interface Foo {" + PMD.EOL +
    114             " static interface Baz {}" + PMD.EOL +
    115             "}";
    116
    117     private static final String TEST15 =
    118             "public interface Foo {" + PMD.EOL +
    119             " interface Baz {}" + PMD.EOL +
    120             "}";
    121
    122     private static final String TEST16 =
    123             "public class Foo {" + PMD.EOL +
    124             " public static interface Bar {}" + PMD.EOL +
    125             "}";
    126
    127     private static final String TEST17 =
    128             "public class Foo {" + PMD.EOL +
    129             " public interface Bar {}" + PMD.EOL +
    130             "}";
    131
    132     private static final String TEST18 =
    133             "public interface Foo {" + PMD.EOL +
    134             " public interface Bar {" + PMD.EOL +
    135             "  public static final int X = 0;" + PMD.EOL +
    136             " }" + PMD.EOL +
    137             "}";
    138
    139     private static final String TEST19 =
    140             "interface Foo {" + PMD.EOL +
    141             " interface Bar {" + PMD.EOL +
    142             "  int X = 0;" + PMD.EOL +
    143             " }" + PMD.EOL +
    144             "}";
    145
    146     private static final String TEST20 =
    147             "interface Foo {" + PMD.EOL +
    148             " class Bar {" + PMD.EOL +
    149             "  public void foo() {}" + PMD.EOL +
    150             " }" + PMD.EOL +
    151             "}";
    152
    153     private static final String TEST21 =
    154             "interface Foo {" + PMD.EOL +
    155             " class Bar {" + PMD.EOL +
    156             "  public int buz;" + PMD.EOL +
    157             " }" + PMD.EOL +
    158             "}";
    159
    160     private static final String TEST22 =
    161             "interface Foo {" + PMD.EOL +
    162             "   Test DFLT_IMPL = new Test() {" + PMD.EOL +
    163             "   public int size() { return 0;}" + PMD.EOL +
    164             " };" + PMD.EOL +
    165             "}";
    166 }