    1   package test.net.sourceforge.pmd;
    2
    3   import net.sourceforge.pmd.ExcludeLines;
    4   import net.sourceforge.pmd.PMD;
    5   import net.sourceforge.pmd.Rule;
    6   import test.net.sourceforge.pmd.testframework.RuleTst;
    7
    8   import java.io.BufferedReader;
    9   import java.io.StringReader;
    10
    11  public class ExcludeLinesTest extends RuleTst {
    12
    13      public void testExcludeOne() throws Throwable {
    14          ExcludeLines e = new ExcludeLines(new StringReader(TEST1));
    15          assertFalse(e.getLinesToExclude().isEmpty());
    16          Integer i = (Integer) e.getLinesToExclude().iterator().next();
    17          assertEquals(3, i.intValue());
    18      }
    19
    20      public void testExcludeMultiple() throws Throwable {
    21          ExcludeLines e = new ExcludeLines(new StringReader(TEST2));
    22          assertEquals(3, e.getLinesToExclude().size());
    23          assertTrue(e.getLinesToExclude().contains(new Integer(3)));
    24          assertTrue(e.getLinesToExclude().contains(new Integer(4)));
    25          assertTrue(e.getLinesToExclude().contains(new Integer(5)));
    26      }
    27
    28      public void testCopyMatches() throws Throwable {
    29          ExcludeLines e = new ExcludeLines(new StringReader(TEST1));
    30          BufferedReader br = new BufferedReader(e.getCopyReader());
    31          StringBuffer copyBuffer = new StringBuffer();
    32          String tmp;
    33          while ((tmp = br.readLine()) != null) {
    34              copyBuffer.append(tmp + PMD.EOL);
    35          }
    36          copyBuffer.deleteCharAt(copyBuffer.length() - 1);
    37          if (PMD.EOL.length() == 2) {
    38              copyBuffer.deleteCharAt(copyBuffer.length() - 1);
    39          }
    40          assertEquals(TEST1, copyBuffer.toString());
    41      }
    42
    43      public void testAlternateMarker() throws Throwable {
    44          ExcludeLines e = new ExcludeLines(new StringReader(TEST4), "FOOBAR");
    45          assertFalse(e.getLinesToExclude().isEmpty());
    46      }
    47
    48      public void testAcceptance() throws Throwable {
    49          try {
    50              Rule rule = findRule("rulesets/unusedcode.xml", "UnusedLocalVariable");
    51              runTestFromString(TEST1, 0, rule);
    52              runTestFromString(TEST3, 1, rule);
    53          } catch (Exception e) {
    54              fail("Acceptance tests failed");
    55          }
    56      }
    57
    58
    59      private static final String TEST1 =
    60              "public class Foo {" + PMD.EOL +
    61              " void foo() {" + PMD.EOL +
    62              "  int x; //NOPMD " + PMD.EOL +
    63              " } " + PMD.EOL +
    64              "}";
    65
    66      private static final String TEST2 =
    67              "public class Foo {" + PMD.EOL +
    68              " void foo() {" + PMD.EOL +
    69              "  int x; //NOPMD " + PMD.EOL +
    70              "  int y; //NOPMD " + PMD.EOL +
    71              "  int z; //NOPMD " + PMD.EOL +
    72              " } " + PMD.EOL +
    73              "}";
    74
    75      private static final String TEST3 =
    76              "public class Foo {" + PMD.EOL +
    77              " void foo() {" + PMD.EOL +
    78              "  int x;" + PMD.EOL +
    79              " } " + PMD.EOL +
    80              "}";
    81
    82      private static final String TEST4 =
    83              "public class Foo {" + PMD.EOL +
    84              " void foo() {" + PMD.EOL +
    85              "  int x; // FOOBAR" + PMD.EOL +
    86              " } " + PMD.EOL +
    87              "}";
    88
    89  }