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