1 /***
2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 */
4 package test.net.sourceforge.pmd.cpd;
5
6 import junit.framework.TestCase;
7 import net.sourceforge.pmd.cpd.FileReporter;
8 import net.sourceforge.pmd.cpd.ReportException;
9
10 import java.io.BufferedReader;
11 import java.io.File;
12 import java.io.FileReader;
13 import java.io.IOException;
14
15 /***
16 * @author Philippe T'Seyen
17 */
18 public class FileReporterTest extends TestCase
19 {
20 public void testCreation() {
21 try {
22 FileReporter fileReporter = new FileReporter(null);
23 fail("expected NullPointerException");
24 } catch (NullPointerException npe) {
25 }
26 }
27
28 public void testEmptyReport() throws ReportException {
29 File reportFile = new File("report.tmp");
30 FileReporter fileReporter = new FileReporter(reportFile);
31 fileReporter.report("");
32 assertTrue(reportFile.exists());
33 assertEquals(0, reportFile.length());
34 assertTrue(reportFile.delete());
35 }
36
37 public void testReport() throws ReportException, IOException {
38 String testString = "first line\nsecond line";
39 File reportFile = new File("report.tmp");
40 FileReporter fileReporter = new FileReporter(reportFile);
41
42 fileReporter.report(testString);
43 assertEquals(testString, readFile(reportFile));
44 assertTrue(reportFile.delete());
45 }
46
47 public void testInvalidFile() {
48 File reportFile = new File("/invalid_folder/report.tmp");
49 FileReporter fileReporter = new FileReporter(reportFile);
50 try {
51 fileReporter.report("");
52 fail("expected ReportException");
53 } catch (ReportException re) {
54 }
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) reader.close();
73 }
74 }
75 }
This page was automatically generated by Maven