View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.report;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.concurrent.Callable;
25  import java.util.concurrent.ExecutorService;
26  import java.util.concurrent.Executors;
27  
28  import junit.framework.TestCase;
29  import org.apache.maven.plugin.surefire.report.ConsoleOutputFileReporter;
30  import org.apache.maven.surefire.api.report.SimpleReportEntry;
31  import org.apache.maven.surefire.api.report.TestOutputReportEntry;
32  import org.apache.maven.surefire.api.report.TestSetReportEntry;
33  import org.apache.maven.surefire.shared.utils.io.FileUtils;
34  
35  import static java.nio.charset.StandardCharsets.US_ASCII;
36  import static org.apache.maven.surefire.api.report.RunMode.NORMAL_RUN;
37  import static org.apache.maven.surefire.api.report.TestOutputReportEntry.stdOut;
38  import static org.apache.maven.surefire.api.report.TestOutputReportEntry.stdOutln;
39  import static org.assertj.core.api.Assertions.assertThat;
40  
41  /**
42   *
43   */
44  public class ConsoleOutputFileReporterTest extends TestCase {
45      /*
46       * Test method for 'org.codehaus.surefire.report.ConsoleOutputFileReporter.testSetCompleted(ReportEntry report)'
47       */
48      public void testFileNameWithoutSuffix() throws IOException {
49          File reportDir = new File(new File(System.getProperty("user.dir"), "target"), "tmp1");
50          //noinspection ResultOfMethodCallIgnored
51          reportDir.mkdirs();
52          TestSetReportEntry reportEntry = new SimpleReportEntry(
53                  NORMAL_RUN, 1L, getClass().getName(), null, getClass().getName(), null);
54          ConsoleOutputFileReporter reporter = new ConsoleOutputFileReporter(reportDir, null, false, null, "UTF-8");
55          reporter.testSetStarting(reportEntry);
56          reporter.writeTestOutput((TestOutputReportEntry) stdOut("some "));
57          reporter.testSetCompleted(reportEntry);
58          reporter.close();
59  
60          File expectedReportFile = new File(reportDir, getClass().getName() + "-output.txt");
61  
62          assertTrue(
63                  "Report file (" + expectedReportFile.getAbsolutePath() + ") doesn't exist",
64                  expectedReportFile.exists());
65  
66          assertThat(FileUtils.fileRead(expectedReportFile, US_ASCII.name())).contains("some ");
67  
68          //noinspection ResultOfMethodCallIgnored
69          expectedReportFile.delete();
70      }
71  
72      /*
73       * Test method for 'org.codehaus.surefire.report.ConsoleOutputFileReporter.testSetCompleted(ReportEntry report)'
74       */
75      public void testFileNameWithSuffix() throws IOException {
76          File reportDir = new File(new File(System.getProperty("user.dir"), "target"), "tmp2");
77          String suffixText = "sampleSuffixText";
78          TestSetReportEntry reportEntry = new SimpleReportEntry(
79                  NORMAL_RUN, 1L, getClass().getName(), null, getClass().getName(), null);
80          ConsoleOutputFileReporter reporter = new ConsoleOutputFileReporter(reportDir, suffixText, false, null, "UTF-8");
81          reporter.testSetStarting(reportEntry);
82          reporter.writeTestOutput(stdOutln(null));
83          reporter.writeTestOutput(stdOutln("some "));
84          reporter.testSetCompleted(reportEntry);
85          reporter.close();
86  
87          File expectedReportFile = new File(reportDir, getClass().getName() + "-" + suffixText + "-output.txt");
88  
89          assertTrue(
90                  "Report file (" + expectedReportFile.getAbsolutePath() + ") doesn't exist",
91                  expectedReportFile.exists());
92  
93          assertThat(FileUtils.fileRead(expectedReportFile, US_ASCII.name())).contains("some ");
94  
95          assertThat(expectedReportFile).hasSize(9 + 2 * System.lineSeparator().length());
96  
97          //noinspection ResultOfMethodCallIgnored
98          expectedReportFile.delete();
99      }
100 
101     public void testNullReportFile() throws IOException {
102         File reportDir = new File(new File(System.getProperty("user.dir"), "target"), "tmp3");
103         ConsoleOutputFileReporter reporter = new ConsoleOutputFileReporter(reportDir, null, false, null, "UTF-8");
104         reporter.writeTestOutput((TestOutputReportEntry) stdOut("some text"));
105         reporter.testSetCompleted(new SimpleReportEntry(
106                 NORMAL_RUN, 1L, getClass().getName(), null, getClass().getName(), null));
107         reporter.close();
108 
109         File expectedReportFile = new File(reportDir, "null-output.txt");
110 
111         assertTrue(
112                 "Report file (" + expectedReportFile.getAbsolutePath() + ") doesn't exist",
113                 expectedReportFile.exists());
114 
115         assertThat(FileUtils.fileRead(expectedReportFile, US_ASCII.name())).contains("some ");
116 
117         //noinspection ResultOfMethodCallIgnored
118         expectedReportFile.delete();
119     }
120 
121     public void testConcurrentAccessReportFile() throws Exception {
122         File reportDir = new File(new File(System.getProperty("user.dir"), "target"), "tmp4");
123         final ConsoleOutputFileReporter reporter = new ConsoleOutputFileReporter(reportDir, null, false, null, "UTF-8");
124         reporter.testSetStarting(new SimpleReportEntry(
125                 NORMAL_RUN, 1L, getClass().getName(), null, getClass().getName(), null));
126         ExecutorService scheduler = Executors.newFixedThreadPool(10);
127         final ArrayList<Callable<Void>> jobs = new ArrayList<>();
128         for (int i = 0; i < 10; i++) {
129             jobs.add(new Callable<Void>() {
130                 @Override
131                 public Void call() {
132                     reporter.writeTestOutput((TestOutputReportEntry) stdOut("some text\n"));
133                     return null;
134                 }
135             });
136         }
137         scheduler.invokeAll(jobs);
138         scheduler.shutdown();
139         reporter.close();
140 
141         File expectedReportFile = new File(reportDir, getClass().getName() + "-output.txt");
142 
143         assertTrue(
144                 "Report file (" + expectedReportFile.getAbsolutePath() + ") doesn't exist",
145                 expectedReportFile.exists());
146 
147         assertThat(FileUtils.fileRead(expectedReportFile, US_ASCII.name())).contains("some text");
148 
149         StringBuilder expectedText = new StringBuilder();
150         for (int i = 0; i < 10; i++) {
151             expectedText.append("some text\n");
152         }
153 
154         assertThat(FileUtils.fileRead(expectedReportFile, US_ASCII.name())).isEqualTo(expectedText.toString());
155 
156         //noinspection ResultOfMethodCallIgnored
157         expectedReportFile.delete();
158     }
159 }