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