View Javadoc
1   package org.apache.maven.plugin.surefire.extensions;
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.report.StatelessXmlReporter;
23  import org.apache.maven.plugin.surefire.report.TestSetStats;
24  import org.apache.maven.plugin.surefire.report.WrappedReportEntry;
25  import org.apache.maven.surefire.extensions.StatelessReportEventListener;
26  import org.apache.maven.surefire.extensions.StatelessReporter;
27  import org.apache.maven.surefire.api.util.ReflectionUtils;
28  
29  /**
30   * Default implementation for extension of {@link StatelessXmlReporter} in plugin.
31   * Signatures can be changed between major, minor versions or milestones.
32   * <br>
33   * This is a builder of {@link StatelessReportEventListener listener}.
34   * The listener handles <em>testSetCompleted</em> event.
35   *
36   * author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
37   * @since 3.0.0-M4
38   */
39  public class SurefireStatelessReporter
40          extends StatelessReporter<WrappedReportEntry, TestSetStats, DefaultStatelessReportMojoConfiguration>
41  {
42      /**
43       * Activated in the injection point of MOJO.
44       */
45      public SurefireStatelessReporter()
46      {
47          this( false, "3.0" );
48      }
49  
50      /**
51       * Activated if null injection point in MOJO.
52       * @param disable             {@code true} to disable performing the report
53       * @param version             (xsd 3.0) version of the schema
54       */
55      public SurefireStatelessReporter( boolean disable, String version )
56      {
57          setDisable( disable );
58          setVersion( version );
59      }
60  
61      @Override
62      public StatelessReportEventListener<WrappedReportEntry, TestSetStats> createListener(
63              DefaultStatelessReportMojoConfiguration configuration )
64      {
65          return new StatelessXmlReporter( configuration.getReportsDirectory(),
66                  configuration.getReportNameSuffix(),
67                  configuration.isTrimStackTrace(),
68                  configuration.getRerunFailingTestsCount(),
69                  configuration.getTestClassMethodRunHistory(),
70                  configuration.getXsdSchemaLocation(),
71                  getVersion(),
72                  false,
73                  false,
74                  false,
75                  false );
76      }
77  
78      @Override
79      public Object clone( ClassLoader target )
80      {
81          try
82          {
83              Class<?> cls = ReflectionUtils.reloadClass( target, this );
84              Object clone = cls.newInstance();
85  
86              cls.getMethod( "setDisable", boolean.class )
87                      .invoke( clone, isDisable() );
88              cls.getMethod( "setVersion", String.class )
89                      .invoke( clone, getVersion() );
90  
91              return clone;
92          }
93          catch ( ReflectiveOperationException e )
94          {
95              throw new IllegalStateException( e.getLocalizedMessage() );
96          }
97      }
98  }