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