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.SurefireConsoleOutputReporter;
23  import org.apache.maven.plugin.surefire.report.ConsoleOutputFileReporter;
24  import org.apache.maven.plugin.surefire.report.StatelessXmlReporter;
25  import org.apache.maven.surefire.extensions.ConsoleOutputReportEventListener;
26  
27  import java.io.File;
28  
29  /**
30   * The extension of {@link StatelessXmlReporter logger} for JUnit5.
31   * Selectively enables report files upon JUnit5 annotation <em>DisplayName</em>.
32   *
33   * author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
34   * @since 3.0.0-M4
35   */
36  public class JUnit5ConsoleOutputReporter
37          extends SurefireConsoleOutputReporter
38  {
39      /**
40       * {@code false} by default.
41       * <br>
42       * This action takes effect only in JUnit5 provider together with a test class annotated <em>DisplayName</em>.
43       */
44      private boolean usePhrasedFileName;
45  
46      public boolean isUsePhrasedFileName()
47      {
48          return usePhrasedFileName;
49      }
50  
51      public void setUsePhrasedFileName( boolean usePhrasedFileName )
52      {
53          this.usePhrasedFileName = usePhrasedFileName;
54      }
55  
56      @Override
57      public ConsoleOutputReportEventListener createListener( File reportsDirectory, String reportNameSuffix,
58                                                              Integer forkNumber )
59      {
60          return new ConsoleOutputFileReporter( reportsDirectory, reportNameSuffix, isUsePhrasedFileName(), forkNumber,
61                  getEncoding() );
62      }
63  
64      @Override
65      public Object clone( ClassLoader target )
66      {
67          try
68          {
69              Object clone = super.clone( target );
70  
71              Class<?> cls = clone.getClass();
72              cls.getMethod( "setUsePhrasedFileName", boolean.class )
73                      .invoke( clone, isUsePhrasedFileName() );
74  
75              return clone;
76          }
77          catch ( ReflectiveOperationException e )
78          {
79              throw new IllegalStateException( e.getLocalizedMessage() );
80          }
81      }
82  
83      @Override
84      public String toString()
85      {
86          return "JUnit5ConsoleOutputReporter{"
87                  + "disable=" + isDisable()
88                  + ", encoding=" + getEncoding()
89                  + ", usePhrasedFileName=" + isUsePhrasedFileName()
90                  + '}';
91      }
92  }