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