View Javadoc
1   package org.apache.maven.plugin.surefire.extensions.junit5;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.surefire.extensions.SurefireStatelessTestsetInfoReporter;
23  import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
24  import org.apache.maven.plugin.surefire.report.ConsoleReporter;
25  import org.apache.maven.plugin.surefire.report.FileReporter;
26  import org.apache.maven.plugin.surefire.report.TestSetStats;
27  import org.apache.maven.plugin.surefire.report.WrappedReportEntry;
28  import org.apache.maven.surefire.extensions.StatelessTestsetInfoConsoleReportEventListener;
29  import org.apache.maven.surefire.extensions.StatelessTestsetInfoFileReportEventListener;
30  
31  import java.io.File;
32  import java.nio.charset.Charset;
33  
34  /**
35   * Extension of {@link StatelessTestsetInfoConsoleReportEventListener file and console reporter of test-set} for JUnit5.
36   * Signatures can be changed between major, minor versions or milestones.
37   *
38   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
39   * @since 3.0.0-M4
40   */
41  public class JUnit5StatelessTestsetInfoReporter
42          extends SurefireStatelessTestsetInfoReporter
43  {
44      /**
45       * {@code false} by default.
46       * <br>
47       * This action takes effect only in JUnit5 provider together with a test class annotated <em>DisplayName</em>.
48       */
49      private boolean usePhrasedFileName;
50  
51      /**
52       * Phrased class name of test case in the console log (see xxx)
53       * <em>Running xxx</em> or file report log <em>Test set: xxx</em>.
54       * {@code false} by default.
55       * <br>
56       * This action takes effect only in JUnit5 provider together with a test class annotated <em>DisplayName</em>.
57       */
58      private boolean usePhrasedClassNameInRunning;
59  
60      /**
61       * Phrased class name of test case in the log (see xxx)
62       * <em>Tests run: ., Failures: ., Errors: ., Skipped: ., Time elapsed: . s, - in xxx</em>.
63       * {@code false} by default.
64       * <br>
65       * This action takes effect only in JUnit5 provider together with a test class annotated <em>DisplayName</em>.
66       */
67      private boolean usePhrasedClassNameInTestCaseSummary;
68  
69      public boolean isUsePhrasedFileName()
70      {
71          return usePhrasedFileName;
72      }
73  
74      public void setUsePhrasedFileName( boolean usePhrasedFileName )
75      {
76          this.usePhrasedFileName = usePhrasedFileName;
77      }
78  
79      public boolean isUsePhrasedClassNameInRunning()
80      {
81          return usePhrasedClassNameInRunning;
82      }
83  
84      public void setUsePhrasedClassNameInRunning( boolean usePhrasedClassNameInRunning )
85      {
86          this.usePhrasedClassNameInRunning = usePhrasedClassNameInRunning;
87      }
88  
89      public boolean isUsePhrasedClassNameInTestCaseSummary()
90      {
91          return usePhrasedClassNameInTestCaseSummary;
92      }
93  
94      public void setUsePhrasedClassNameInTestCaseSummary( boolean usePhrasedClassNameInTestCaseSummary )
95      {
96          this.usePhrasedClassNameInTestCaseSummary = usePhrasedClassNameInTestCaseSummary;
97      }
98  
99      @Override
100     public StatelessTestsetInfoConsoleReportEventListener<WrappedReportEntry, TestSetStats> createListener(
101             ConsoleLogger logger )
102     {
103         return new ConsoleReporter( logger, isUsePhrasedClassNameInRunning(),
104                                     isUsePhrasedClassNameInTestCaseSummary() );
105     }
106 
107     @Override
108     public StatelessTestsetInfoFileReportEventListener<WrappedReportEntry, TestSetStats> createListener(
109             File reportsDirectory, String reportNameSuffix, Charset encoding )
110     {
111         return new FileReporter( reportsDirectory, reportNameSuffix, encoding, isUsePhrasedFileName(),
112                                  isUsePhrasedClassNameInRunning(), isUsePhrasedClassNameInTestCaseSummary() );
113     }
114 
115     @Override
116     public Object clone( ClassLoader target )
117     {
118         try
119         {
120             Object clone = super.clone( target );
121 
122             Class<?> cls = clone.getClass();
123             cls.getMethod( "setUsePhrasedFileName", boolean.class )
124                     .invoke( clone, isUsePhrasedFileName() );
125             cls.getMethod( "setUsePhrasedClassNameInTestCaseSummary", boolean.class )
126                     .invoke( clone, isUsePhrasedFileName() );
127             cls.getMethod( "setUsePhrasedClassNameInRunning", boolean.class )
128                     .invoke( clone, isUsePhrasedFileName() );
129 
130             return clone;
131         }
132         catch ( ReflectiveOperationException e )
133         {
134             throw new IllegalStateException( e.getLocalizedMessage() );
135         }
136     }
137 
138     @Override
139     public String toString()
140     {
141         return "JUnit5StatelessTestsetInfoReporter{"
142                 + "disable=" + isDisable()
143                 + ", usePhrasedFileName=" + isUsePhrasedFileName()
144                 + ", usePhrasedClassNameInRunning=" + isUsePhrasedClassNameInRunning()
145                 + ", usePhrasedClassNameInTestCaseSummary=" + isUsePhrasedClassNameInTestCaseSummary()
146                 + "}";
147     }
148 }