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.nio.charset.Charset;
23  import java.util.ArrayList;
24  
25  import junit.framework.TestCase;
26  import org.apache.maven.plugin.surefire.report.FileReporter;
27  import org.apache.maven.plugin.surefire.report.ReportEntryType;
28  import org.apache.maven.plugin.surefire.report.TestSetStats;
29  import org.apache.maven.plugin.surefire.report.WrappedReportEntry;
30  import org.apache.maven.surefire.api.report.ReportEntry;
31  import org.apache.maven.surefire.api.report.SimpleReportEntry;
32  
33  import static org.apache.maven.surefire.api.report.RunMode.NORMAL_RUN;
34  
35  /**
36   *
37   */
38  public class FileReporterTest extends TestCase {
39  
40      private FileReporter reporter;
41  
42      private ReportEntry reportEntry;
43  
44      private static final String TEST_NAME = "org.apache.maven.surefire.report.FileReporterTest";
45  
46      public void testFileNameWithoutSuffix() {
47          File reportDir = new File("target");
48          reportEntry = new SimpleReportEntry(NORMAL_RUN, 1L, getClass().getName(), null, TEST_NAME, null);
49          WrappedReportEntry wrappedReportEntry =
50                  new WrappedReportEntry(reportEntry, ReportEntryType.SUCCESS, 12, null, null);
51          reporter = new FileReporter(reportDir, null, Charset.defaultCharset(), false, false, false);
52          reporter.testSetCompleted(wrappedReportEntry, createTestSetStats(), new ArrayList<>());
53  
54          File expectedReportFile = new File(reportDir, TEST_NAME + ".txt");
55          assertTrue(
56                  "Report file (" + expectedReportFile.getAbsolutePath() + ") doesn't exist",
57                  expectedReportFile.exists());
58          //noinspection ResultOfMethodCallIgnored
59          expectedReportFile.delete();
60      }
61  
62      private TestSetStats createTestSetStats() {
63          return new TestSetStats(true, true);
64      }
65  
66      public void testFileNameWithSuffix() {
67          File reportDir = new File("target");
68          String suffixText = "sampleSuffixText";
69          reportEntry = new SimpleReportEntry(NORMAL_RUN, 1L, getClass().getName(), null, TEST_NAME, null);
70          WrappedReportEntry wrappedReportEntry =
71                  new WrappedReportEntry(reportEntry, ReportEntryType.SUCCESS, 12, null, null);
72          reporter = new FileReporter(reportDir, suffixText, Charset.defaultCharset(), false, false, false);
73          reporter.testSetCompleted(wrappedReportEntry, createTestSetStats(), new ArrayList<>());
74  
75          File expectedReportFile = new File(reportDir, TEST_NAME + "-" + suffixText + ".txt");
76          assertTrue(
77                  "Report file (" + expectedReportFile.getAbsolutePath() + ") doesn't exist",
78                  expectedReportFile.exists());
79          //noinspection ResultOfMethodCallIgnored
80          expectedReportFile.delete();
81      }
82  }