    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package test.net.sourceforge.pmd.rules.junit;
    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 JUnitTestsShouldContainAssertsTest extends SimpleAggregatorTst {
    13      private Rule rule;
    14
    15      public void setUp() throws RuleSetNotFoundException {
    16          rule = findRule("junit", "JUnitTestsShouldIncludeAssert");
    17      }
    18
    19      public void testAll() throws Throwable {
    20
    21          runTests(new TestDescriptor[]{
    22              new TestDescriptor(TEST1, "Contains assert", 0, rule),
    23              new TestDescriptor(TEST2, "Missing assert", 1, rule),
    24              new TestDescriptor(TEST3, "All ok", 0, rule),
    25              new TestDescriptor(TEST4, "Two wrong", 2, rule),
    26              new TestDescriptor(TEST5, "Contains fail", 0, rule),
    27              new TestDescriptor(TEST6, "One wrong", 1, rule),
    28              new TestDescriptor(TEST7, "Skip interfaces", 0, rule),
    29              new TestDescriptor(TEST8, "Skip abstract methods", 0, rule),
    30              new TestDescriptor(TEST9, "Another fail() case", 0, rule),
    31              new TestDescriptor(TEST10, "BUG 1105633 - False +: JUnit testcases could have fail() instead of assert", 0, rule),
    32              new TestDescriptor(BUG_1146116, "BUG 1146116 PMDException with inner interfaces", 0, rule),
    33              new TestDescriptor(TEST12, "skip static test methods", 0, rule),
    34              new TestDescriptor(TEST13, "exceptions shouldn't block it", 1, rule),
    35          });
    36      }
    37
    38      private static final String TEST1 =
    39              "public class Foo {" + PMD.EOL +
    40              " public void test1() {" + PMD.EOL +
    41              "  assertEquals(\"1 == 1\", 1, 1);" + PMD.EOL +
    42              " }" + PMD.EOL +
    43              "}";
    44
    45      private static final String TEST2 =
    46              "public class Foo {" + PMD.EOL +
    47              " public void test1() {" + PMD.EOL +
    48              " }" + PMD.EOL +
    49              "}";
    50
    51      private static final String TEST3 =
    52              "public class Foo {" + PMD.EOL +
    53              " public void setUp() {" + PMD.EOL +
    54              " }" + PMD.EOL +
    55              " public void test1() {" + PMD.EOL +
    56              "  assertTrue(\"foo\", \"foo\".equals(\"foo\"));" + PMD.EOL +
    57              " }" + PMD.EOL +
    58              " public void test2() {" + PMD.EOL +
    59              "  assertEquals(\"foo\", \"foo\");" + PMD.EOL +
    60              " }" + PMD.EOL +
    61              "}";
    62
    63      private static final String TEST4 =
    64              "public class Foo {" + PMD.EOL +
    65              " public void setUp() {" + PMD.EOL +
    66              " }" + PMD.EOL +
    67              " public void test1() {" + PMD.EOL +
    68              " 	int a;" + PMD.EOL +
    69              " 	callMethod(a);" + PMD.EOL +
    70              " }" + PMD.EOL +
    71              " public void test2() {" + PMD.EOL +
    72              " }" + PMD.EOL +
    73              "}";
    74
    75
    76      private static final String TEST5 =
    77              "public class Foo {" + PMD.EOL +
    78              " public void test1() {" + PMD.EOL +
    79              "  fail(\"1 == 1\");" + PMD.EOL +
    80              " }" + PMD.EOL +
    81              "}";
    82
    83
    84      private static final String TEST6 =
    85              "public class Foo {" + PMD.EOL +
    86              " public void setUp() {" + PMD.EOL +
    87              " }" + PMD.EOL +
    88              " public void test1() {" + PMD.EOL +
    89              " 	int a;" + PMD.EOL +
    90              " 	callMethod(a);" + PMD.EOL +
    91              " }" + PMD.EOL +
    92              " public void test2() {" + PMD.EOL +
    93              " 	fail();" + PMD.EOL +
    94              " }" + PMD.EOL +
    95              "}";
    96
    97      private static final String TEST7 =
    98              "public interface Foo {" + PMD.EOL +
    99              " public void setUp() throws Exception;" + PMD.EOL +
    100             "}";
    101
    102     private static final String TEST8 =
    103             "public class Foo {" + PMD.EOL +
    104             " public abstract void setUp() throws Exception;" + PMD.EOL +
    105             "}";
    106
    107     private static final String TEST9 =
    108             "public abstract class AbstractAggregateCreator {" + PMD.EOL +
    109             "    public abstract int getType();" + PMD.EOL +
    110             "    public abstract ProfileAggregate create(DatabaseTransaction db," + PMD.EOL +
    111             "        DailyProfileList profiles, ProfileType type, ProfileStatus status)" + PMD.EOL +
    112             "        throws VixenException;" + PMD.EOL +
    113             "}";
    114
    115     private static final String TEST10 =
    116             "public class FooTest {" + PMD.EOL +
    117             " public void testNPEThrown() {" + PMD.EOL +
    118             "  try {" + PMD.EOL +
    119             "   methodCall(null);" + PMD.EOL +
    120             "   fail(\"Expected NullPointerException to be thrown.\");" + PMD.EOL +
    121             "  } catch (NullPointerException npe) {" + PMD.EOL +
    122             "   // Caught expected exception" + PMD.EOL +
    123             "  }" + PMD.EOL +
    124             " }" + PMD.EOL +
    125             "}";
    126
    127     private static final String BUG_1146116 = "package at.herold.anthilltest;" + PMD.EOL +
    128             "public class TestJunitRuleException {" + PMD.EOL +
    129             "   interface I1 { " + PMD.EOL +
    130             "      public void meth(); // this is ok" + PMD.EOL +
    131             "   }" + PMD.EOL +
    132             "   interface I2 {" + PMD.EOL +
    133             "      public void meth() throws Exception; // this causes PMDException" + PMD.EOL +
    134             "   }" + PMD.EOL +
    135             "}";
    136
    137     private static final String TEST12 =
    138             "public class Foo {" + PMD.EOL +
    139             " public static void testfoo() {}" + PMD.EOL +
    140             "}";
    141
    142     private static final String TEST13 =
    143             "public class Foo {" + PMD.EOL +
    144             " public void testfoo() throws Exception {}" + PMD.EOL +
    145             "}";
    146
    147 }