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.plugin.surefire.extensions.junit5;
20  
21  import java.io.File;
22  import java.nio.charset.Charset;
23  
24  import org.apache.maven.plugin.surefire.extensions.SurefireStatelessTestsetInfoReporter;
25  import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
26  import org.apache.maven.plugin.surefire.report.ConsoleReporter;
27  import org.apache.maven.plugin.surefire.report.FileReporter;
28  import org.apache.maven.plugin.surefire.report.TestSetStats;
29  import org.apache.maven.plugin.surefire.report.WrappedReportEntry;
30  import org.apache.maven.surefire.extensions.StatelessTestsetInfoConsoleReportEventListener;
31  import org.apache.maven.surefire.extensions.StatelessTestsetInfoFileReportEventListener;
32  
33  /**
34   * Extension of {@link StatelessTestsetInfoConsoleReportEventListener file and console reporter of test-set} for JUnit5.
35   * Signatures can be changed between major, minor versions or milestones.
36   *
37   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
38   * @since 3.0.0-M4
39   */
40  public class JUnit5StatelessTestsetInfoReporter extends SurefireStatelessTestsetInfoReporter {
41      /**
42       * {@code false} by default.
43       * <br>
44       * This action takes effect only in JUnit5 provider together with a test class annotated <em>DisplayName</em>.
45       */
46      private boolean usePhrasedFileName;
47  
48      /**
49       * Phrased class name of test case in the console log (see xxx)
50       * <em>Running xxx</em> or file report log <em>Test set: xxx</em>.
51       * {@code false} by default.
52       * <br>
53       * This action takes effect only in JUnit5 provider together with a test class annotated <em>DisplayName</em>.
54       */
55      private boolean usePhrasedClassNameInRunning;
56  
57      /**
58       * Phrased class name of test case in the log (see xxx)
59       * <em>Tests run: ., Failures: ., Errors: ., Skipped: ., Time elapsed: . s, -- in xxx</em>.
60       * {@code false} by default.
61       * <br>
62       * This action takes effect only in JUnit5 provider together with a test class annotated <em>DisplayName</em>.
63       */
64      private boolean usePhrasedClassNameInTestCaseSummary;
65  
66      public boolean isUsePhrasedFileName() {
67          return usePhrasedFileName;
68      }
69  
70      public void setUsePhrasedFileName(boolean usePhrasedFileName) {
71          this.usePhrasedFileName = usePhrasedFileName;
72      }
73  
74      public boolean isUsePhrasedClassNameInRunning() {
75          return usePhrasedClassNameInRunning;
76      }
77  
78      public void setUsePhrasedClassNameInRunning(boolean usePhrasedClassNameInRunning) {
79          this.usePhrasedClassNameInRunning = usePhrasedClassNameInRunning;
80      }
81  
82      public boolean isUsePhrasedClassNameInTestCaseSummary() {
83          return usePhrasedClassNameInTestCaseSummary;
84      }
85  
86      public void setUsePhrasedClassNameInTestCaseSummary(boolean usePhrasedClassNameInTestCaseSummary) {
87          this.usePhrasedClassNameInTestCaseSummary = usePhrasedClassNameInTestCaseSummary;
88      }
89  
90      @Override
91      public StatelessTestsetInfoConsoleReportEventListener<WrappedReportEntry, TestSetStats> createListener(
92              ConsoleLogger logger) {
93          return new ConsoleReporter(logger, isUsePhrasedClassNameInRunning(), isUsePhrasedClassNameInTestCaseSummary());
94      }
95  
96      @Override
97      public StatelessTestsetInfoFileReportEventListener<WrappedReportEntry, TestSetStats> createListener(
98              File reportsDirectory, String reportNameSuffix, Charset encoding) {
99          return new FileReporter(
100                 reportsDirectory,
101                 reportNameSuffix,
102                 encoding,
103                 isUsePhrasedFileName(),
104                 isUsePhrasedClassNameInRunning(),
105                 isUsePhrasedClassNameInTestCaseSummary());
106     }
107 
108     @Override
109     public Object clone(ClassLoader target) {
110         try {
111             Object clone = super.clone(target);
112 
113             Class<?> cls = clone.getClass();
114             cls.getMethod("setUsePhrasedFileName", boolean.class).invoke(clone, isUsePhrasedFileName());
115             cls.getMethod("setUsePhrasedClassNameInTestCaseSummary", boolean.class)
116                     .invoke(clone, isUsePhrasedFileName());
117             cls.getMethod("setUsePhrasedClassNameInRunning", boolean.class).invoke(clone, isUsePhrasedFileName());
118 
119             return clone;
120         } catch (ReflectiveOperationException e) {
121             throw new IllegalStateException(e.getLocalizedMessage());
122         }
123     }
124 
125     @Override
126     public String toString() {
127         return "JUnit5StatelessTestsetInfoReporter{"
128                 + "disable=" + isDisable()
129                 + ", usePhrasedFileName=" + isUsePhrasedFileName()
130                 + ", usePhrasedClassNameInRunning=" + isUsePhrasedClassNameInRunning()
131                 + ", usePhrasedClassNameInTestCaseSummary=" + isUsePhrasedClassNameInTestCaseSummary()
132                 + "}";
133     }
134 }