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 java.io.File;
22  import java.io.PrintStream;
23  
24  import org.apache.maven.plugin.surefire.report.ConsoleOutputFileReporter;
25  import org.apache.maven.plugin.surefire.report.DirectConsoleOutput;
26  import org.apache.maven.surefire.api.util.ReflectionUtils;
27  import org.apache.maven.surefire.extensions.ConsoleOutputReportEventListener;
28  import org.apache.maven.surefire.extensions.ConsoleOutputReporter;
29  
30  /**
31   * Default implementation for extension of console logger.
32   * Signatures can be changed between major, minor versions or milestones.
33   * <br>
34   * Builds {@link ConsoleOutputReportEventListener listeners}.
35   * The listeners handle test set events.
36   *
37   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
38   * @since 3.0.0-M4
39   */
40  public class SurefireConsoleOutputReporter extends ConsoleOutputReporter {
41      @Override
42      public ConsoleOutputReportEventListener createListener(
43              File reportsDirectory, String reportNameSuffix, Integer forkNumber) {
44          return new ConsoleOutputFileReporter(reportsDirectory, reportNameSuffix, false, forkNumber, getEncoding());
45      }
46  
47      @Override
48      public ConsoleOutputReportEventListener createListener(PrintStream out, PrintStream err) {
49          return new DirectConsoleOutput(out, err);
50      }
51  
52      @Override
53      public Object clone(ClassLoader target) {
54          try {
55              Class<?> cls = ReflectionUtils.reloadClass(target, this);
56              Object clone = cls.newInstance();
57  
58              cls.getMethod("setDisable", boolean.class).invoke(clone, isDisable());
59              cls.getMethod("setEncoding", String.class).invoke(clone, getEncoding());
60  
61              return clone;
62          } catch (ReflectiveOperationException e) {
63              throw new IllegalStateException(e.getLocalizedMessage());
64          }
65      }
66  
67      @Override
68      public String toString() {
69          return "SurefireConsoleOutputReporter{" + "disable=" + isDisable() + ", encoding=" + getEncoding() + '}';
70      }
71  }