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