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 org.apache.maven.plugin.surefire.extensions.DefaultStatelessReportMojoConfiguration;
22  import org.apache.maven.plugin.surefire.extensions.SurefireStatelessReporter;
23  import org.apache.maven.plugin.surefire.report.StatelessXmlReporter;
24  import org.apache.maven.plugin.surefire.report.TestSetStats;
25  import org.apache.maven.plugin.surefire.report.WrappedReportEntry;
26  import org.apache.maven.surefire.extensions.StatelessReportEventListener;
27  
28  /**
29   * The extension of {@link StatelessReportEventListener xml reporter} based on XSD version 3.0 for JUnit5.
30   * Selectively enables phrased classes, methods and 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 JUnit5Xml30StatelessReporter extends SurefireStatelessReporter {
36      /**
37       * {@code false} by default.
38       * <br>
39       * This action takes effect only in JUnit5 provider together with a test class annotated <em>DisplayName</em>.
40       */
41      private boolean usePhrasedFileName;
42  
43      /**
44       * {@code false} by default.
45       * <br>
46       * This action takes effect only in JUnit5 provider together with a test class annotated <em>DisplayName</em>.
47       */
48      private boolean usePhrasedTestSuiteClassName;
49  
50      /**
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 usePhrasedTestCaseClassName;
56  
57      /**
58       * {@code false} by default.
59       * <br>
60       * This action takes effect only in JUnit5 provider together with a test method annotated <em>DisplayName</em>.
61       */
62      private boolean usePhrasedTestCaseMethodName;
63  
64      public boolean getUsePhrasedFileName() {
65          return usePhrasedFileName;
66      }
67  
68      public void setUsePhrasedFileName(boolean usePhrasedFileName) {
69          this.usePhrasedFileName = usePhrasedFileName;
70      }
71  
72      public boolean getUsePhrasedTestSuiteClassName() {
73          return usePhrasedTestSuiteClassName;
74      }
75  
76      public void setUsePhrasedTestSuiteClassName(boolean usePhrasedTestSuiteClassName) {
77          this.usePhrasedTestSuiteClassName = usePhrasedTestSuiteClassName;
78      }
79  
80      public boolean getUsePhrasedTestCaseClassName() {
81          return usePhrasedTestCaseClassName;
82      }
83  
84      public void setUsePhrasedTestCaseClassName(boolean usePhrasedTestCaseClassName) {
85          this.usePhrasedTestCaseClassName = usePhrasedTestCaseClassName;
86      }
87  
88      public boolean getUsePhrasedTestCaseMethodName() {
89          return usePhrasedTestCaseMethodName;
90      }
91  
92      public void setUsePhrasedTestCaseMethodName(boolean usePhrasedTestCaseMethodName) {
93          this.usePhrasedTestCaseMethodName = usePhrasedTestCaseMethodName;
94      }
95  
96      @Override
97      public StatelessReportEventListener<WrappedReportEntry, TestSetStats> createListener(
98              DefaultStatelessReportMojoConfiguration configuration) {
99          return new StatelessXmlReporter(
100                 configuration.getReportsDirectory(),
101                 configuration.getReportNameSuffix(),
102                 configuration.isTrimStackTrace(),
103                 configuration.getRerunFailingTestsCount(),
104                 configuration.getTestClassMethodRunHistory(),
105                 configuration.getXsdSchemaLocation(),
106                 getVersion(),
107                 getUsePhrasedFileName(),
108                 getUsePhrasedTestSuiteClassName(),
109                 getUsePhrasedTestCaseClassName(),
110                 getUsePhrasedTestCaseMethodName());
111     }
112 
113     @Override
114     public Object clone(ClassLoader target) {
115         try {
116             Object clone = super.clone(target);
117 
118             Class<?> cls = clone.getClass();
119             cls.getMethod("setUsePhrasedFileName", boolean.class).invoke(clone, getUsePhrasedFileName());
120             cls.getMethod("setUsePhrasedTestSuiteClassName", boolean.class)
121                     .invoke(clone, getUsePhrasedTestSuiteClassName());
122             cls.getMethod("setUsePhrasedTestCaseClassName", boolean.class)
123                     .invoke(clone, getUsePhrasedTestCaseClassName());
124             cls.getMethod("setUsePhrasedTestCaseMethodName", boolean.class)
125                     .invoke(clone, getUsePhrasedTestCaseMethodName());
126 
127             return clone;
128         } catch (ReflectiveOperationException e) {
129             throw new IllegalStateException(e.getLocalizedMessage());
130         }
131     }
132 
133     @Override
134     public String toString() {
135         return "JUnit5Xml30StatelessReporter{"
136                 + "version=" + getVersion()
137                 + ", disable=" + isDisable()
138                 + ", usePhrasedFileName=" + getUsePhrasedFileName()
139                 + ", usePhrasedTestSuiteClassName=" + getUsePhrasedTestSuiteClassName()
140                 + ", usePhrasedTestCaseClassName=" + getUsePhrasedTestCaseClassName()
141                 + ", usePhrasedTestCaseMethodName=" + getUsePhrasedTestCaseMethodName()
142                 + '}';
143     }
144 }