1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.surefire.report;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.nio.file.Files;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.concurrent.Callable;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.Executors;
29
30 import org.apache.maven.plugin.surefire.report.ConsoleOutputFileReporter;
31 import org.apache.maven.surefire.api.report.SimpleReportEntry;
32 import org.apache.maven.surefire.api.report.TestOutputReportEntry;
33 import org.apache.maven.surefire.api.report.TestSetReportEntry;
34 import org.apache.maven.surefire.shared.utils.io.FileUtils;
35 import org.junit.jupiter.api.Test;
36
37 import static java.nio.charset.StandardCharsets.US_ASCII;
38 import static org.apache.maven.surefire.api.report.RunMode.NORMAL_RUN;
39 import static org.apache.maven.surefire.api.report.TestOutputReportEntry.stdOut;
40 import static org.apache.maven.surefire.api.report.TestOutputReportEntry.stdOutln;
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.junit.jupiter.api.Assertions.assertTrue;
43
44
45
46
47 public class ConsoleOutputFileReporterTest {
48
49
50
51 @Test
52 public void testFileNameWithoutSuffix() throws IOException {
53 File reportDir = new File(new File(System.getProperty("user.dir"), "target"), "tmp1");
54 if (Files.exists(reportDir.toPath())) {
55 FileUtils.deleteDirectory(reportDir);
56 }
57 Files.createDirectories(reportDir.toPath());
58 TestSetReportEntry reportEntry = new SimpleReportEntry(
59 NORMAL_RUN, 1L, getClass().getName(), null, getClass().getName(), null);
60 ConsoleOutputFileReporter reporter = new ConsoleOutputFileReporter(reportDir, null, false, null, "UTF-8");
61 reporter.testSetStarting(reportEntry);
62 reporter.writeTestOutput((TestOutputReportEntry) stdOut("some "));
63 reporter.testSetCompleted(reportEntry);
64 reporter.close();
65
66 File expectedReportFile = new File(reportDir, getClass().getName() + "-output.txt");
67
68 assertTrue(
69 expectedReportFile.exists(),
70 "Report file (" + expectedReportFile.getAbsolutePath() + ") doesn't exist");
71
72 assertThat(FileUtils.fileRead(expectedReportFile, US_ASCII.name())).contains("some ");
73
74
75 expectedReportFile.delete();
76 }
77
78
79
80
81 @Test
82 public void testFileNameWithSuffix() throws IOException {
83 File reportDir = new File(new File(System.getProperty("user.dir"), "target"), "tmp2");
84 if (Files.exists(reportDir.toPath())) {
85 FileUtils.deleteDirectory(reportDir);
86 }
87 Files.createDirectories(reportDir.toPath());
88 String suffixText = "sampleSuffixText";
89 TestSetReportEntry reportEntry = new SimpleReportEntry(
90 NORMAL_RUN, 1L, getClass().getName(), null, getClass().getName(), null);
91 ConsoleOutputFileReporter reporter = new ConsoleOutputFileReporter(reportDir, suffixText, false, null, "UTF-8");
92 reporter.testSetStarting(reportEntry);
93 reporter.writeTestOutput(stdOutln(null));
94 reporter.writeTestOutput(stdOutln("some "));
95 reporter.testSetCompleted(reportEntry);
96 reporter.close();
97
98 File expectedReportFile = new File(reportDir, getClass().getName() + "-" + suffixText + "-output.txt");
99
100 assertTrue(
101 expectedReportFile.exists(),
102 "Report file (" + expectedReportFile.getAbsolutePath() + ") doesn't exist");
103
104 assertThat(FileUtils.fileRead(expectedReportFile, US_ASCII.name())).contains("some ");
105
106 assertThat(expectedReportFile).hasSize(9 + 2 * System.lineSeparator().length());
107
108
109 expectedReportFile.delete();
110 }
111
112 @Test
113 public void testNullReportFile() throws IOException {
114 File reportDir = new File(new File(System.getProperty("user.dir"), "target"), "tmp3");
115 if (Files.exists(reportDir.toPath())) {
116 FileUtils.deleteDirectory(reportDir);
117 }
118 Files.createDirectories(reportDir.toPath());
119 ConsoleOutputFileReporter reporter = new ConsoleOutputFileReporter(reportDir, null, false, null, "UTF-8");
120 reporter.writeTestOutput((TestOutputReportEntry) stdOut("some text"));
121 reporter.testSetCompleted(new SimpleReportEntry(
122 NORMAL_RUN, 1L, getClass().getName(), null, getClass().getName(), null));
123 reporter.close();
124
125 File expectedReportFile = new File(reportDir, "null-output.txt");
126
127 assertTrue(
128 expectedReportFile.exists(),
129 "Report file (" + expectedReportFile.getAbsolutePath() + ") doesn't exist");
130
131 assertThat(FileUtils.fileRead(expectedReportFile, US_ASCII.name())).contains("some ");
132
133
134 expectedReportFile.delete();
135 }
136
137 @Test
138 public void testOutputWithoutStackTraceUsesCurrentReportFile() throws IOException {
139 File reportDir = new File(new File(System.getProperty("user.dir"), "target"), "tmp3-without-stack");
140 if (Files.exists(reportDir.toPath())) {
141 FileUtils.deleteDirectory(reportDir);
142 }
143 Files.createDirectories(reportDir.toPath());
144 TestSetReportEntry reportEntry = new SimpleReportEntry(
145 NORMAL_RUN, 1L, getClass().getName(), null, getClass().getName(), null);
146 ConsoleOutputFileReporter reporter = new ConsoleOutputFileReporter(reportDir, null, false, null, "UTF-8");
147 reporter.testSetStarting(reportEntry);
148 reporter.writeTestOutput(new TestOutputReportEntry("some text", true, false, NORMAL_RUN, 1L, null));
149 reporter.close();
150
151 File expectedReportFile = new File(reportDir, getClass().getName() + "-output.txt");
152 File nullReportFile = new File(reportDir, "null-output.txt");
153
154 assertThat(expectedReportFile).exists();
155 assertThat(FileUtils.fileRead(expectedReportFile, US_ASCII.name())).contains("some text");
156 assertThat(nullReportFile).doesNotExist();
157
158
159 expectedReportFile.delete();
160 }
161
162 @Test
163 public void testConcurrentAccessReportFile() throws Exception {
164 File reportDir = new File(new File(System.getProperty("user.dir"), "target"), "tmp4");
165 if (Files.exists(reportDir.toPath())) {
166 FileUtils.deleteDirectory(reportDir);
167 }
168 final ConsoleOutputFileReporter reporter = new ConsoleOutputFileReporter(reportDir, null, false, null, "UTF-8");
169 reporter.testSetStarting(new SimpleReportEntry(
170 NORMAL_RUN, 1L, getClass().getName(), null, getClass().getName(), null));
171 ExecutorService scheduler = Executors.newFixedThreadPool(10);
172 List<Callable<Void>> jobs = new ArrayList<>();
173 for (int i = 0; i < 10; i++) {
174 jobs.add(() -> {
175 reporter.writeTestOutput((TestOutputReportEntry) stdOut("some text\n"));
176 return null;
177 });
178 }
179 scheduler.invokeAll(jobs);
180 scheduler.shutdown();
181 reporter.close();
182
183 File expectedReportFile =
184 new File(reportDir, "org.apache.maven.surefire.report.ConsoleOutputFileReporterTest-output.txt");
185
186 assertTrue(
187 expectedReportFile.exists(),
188 "Report file (" + expectedReportFile.getAbsolutePath() + ") doesn't exist");
189
190 assertThat(FileUtils.fileRead(expectedReportFile, US_ASCII.name())).contains("some text");
191
192 StringBuilder expectedText = new StringBuilder();
193 for (int i = 0; i < 10; i++) {
194 expectedText.append("some text\n");
195 }
196
197 assertThat(FileUtils.fileRead(expectedReportFile, US_ASCII.name())).isEqualTo(expectedText.toString());
198
199
200 expectedReportFile.delete();
201 }
202 }