View Javadoc

    1   /**
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.cpd;
    5
    6   import static org.junit.Assert.assertEquals;
    7   import static org.junit.Assert.assertTrue;
    8   import net.sourceforge.pmd.cpd.FileReporter;
    9   import net.sourceforge.pmd.cpd.ReportException;
    10
    11  import org.junit.Test;
    12
    13  import java.io.BufferedReader;
    14  import java.io.File;
    15  import java.io.FileReader;
    16  import java.io.IOException;
    17
    18  /**
    19   * @author Philippe T'Seyen
    20   */
    21  public class FileReporterTest {
    22
    23      @Test
    24      public void testCreation() {
    25          new FileReporter((String)null);
    26          new FileReporter((File)null);
    27      }
    28
    29      @Test
    30      public void testEmptyReport() throws ReportException {
    31          File reportFile = new File("report.tmp");
    32          FileReporter fileReporter = new FileReporter(reportFile);
    33          fileReporter.report("");
    34          assertTrue(reportFile.exists());
    35          assertEquals(0L, reportFile.length());
    36          assertTrue(reportFile.delete());
    37      }
    38
    39      @Test
    40      public void testReport() throws ReportException, IOException {
    41          String testString = "first line\nsecond line";
    42          File reportFile = new File("report.tmp");
    43          FileReporter fileReporter = new FileReporter(reportFile);
    44
    45          fileReporter.report(testString);
    46          assertEquals(testString, readFile(reportFile));
    47          assertTrue(reportFile.delete());
    48      }
    49
    50      @Test(expected = ReportException.class)
    51      public void testInvalidFile() throws ReportException {
    52          File reportFile = new File("/invalid_folder/report.tmp");
    53          FileReporter fileReporter = new FileReporter(reportFile);
    54          fileReporter.report("");
    55      }
    56
    57      private String readFile(File file) throws IOException {
    58          BufferedReader reader = null;
    59          try {
    60              reader = new BufferedReader(new FileReader(file));
    61              StringBuffer buffer = new StringBuffer();
    62              String line = reader.readLine();
    63              while (line != null) {
    64                  buffer.append(line);
    65                  line = reader.readLine();
    66                  if (line != null) {
    67                      buffer.append('\n');
    68                  }
    69              }
    70              return buffer.toString();
    71          } finally {
    72              if (reader != null)
    73                  reader.close();
    74          }
    75      }
    76
    77      public static junit.framework.Test suite() {
    78          return new junit.framework.JUnit4TestAdapter(FileReporterTest.class);
    79      }
    80  }