aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestXmlListener.java
blob: 5e572e1e107ac9d378247a7020b9e542c6cee0a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2012 The Bazel Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.testing.junit.runner.junit4;

import com.google.common.base.Supplier;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.testing.junit.runner.internal.SignalHandlers;
import com.google.testing.junit.runner.internal.Stderr;
import com.google.testing.junit.runner.model.TestSuiteModel;

import org.junit.Ignore;
import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

import sun.misc.Signal;
import sun.misc.SignalHandler;

import java.io.OutputStream;
import java.io.PrintStream;

/**
 * A listener that writes the test output as XML.
 */
@Singleton
class JUnit4TestXmlListener extends RunListener  {
  
  private final Supplier<TestSuiteModel> modelSupplier;
  private final CancellableRequestFactory requestFactory;
  private final SignalHandlers signalHandlers;
  private final OutputStream xmlStream;
  private final PrintStream errPrintStream;
  private volatile TestSuiteModel model;

  @Inject
  public JUnit4TestXmlListener(Supplier<TestSuiteModel> modelSupplier,
      CancellableRequestFactory requestFactory, SignalHandlers signalHandlers,
      @Xml OutputStream xmlStream, @Stderr PrintStream errPrintStream) {
    this.modelSupplier = modelSupplier;
    this.requestFactory = requestFactory;
    this.signalHandlers = signalHandlers;
    this.xmlStream = xmlStream;
    this.errPrintStream = errPrintStream;
  }

  @Override
  public void testRunStarted(Description description) throws Exception {
    model = modelSupplier.get();
    signalHandlers.installHandler(new Signal("TERM"), new WriteXmlSignalHandler());
  }

  @Override
  public void testStarted(Description description) throws Exception {
    model.testStarted(description);
  }

  @Override
  public void testAssumptionFailure(Failure failure) {
    model.testSkipped(failure.getDescription());
  }

  @Override
  public void testFailure(Failure failure) throws Exception {
    model.testFailure(failure.getDescription(), failure.getException());
  }

  @Override
  public void testIgnored(Description description) throws Exception {
    // TODO(bazel-team) There's a known issue in the JUnit4 ParentRunner that
    // fires testIgnored on test suites that are being skipped due to an
    // assumption failure.
    if (isSuiteAssumptionFailure(description)) {
      model.testSkipped(description);
    } else {
      model.testSuppressed(description);
    }
  }

  private boolean isSuiteAssumptionFailure(Description description) {
    return description.isSuite() && description.getAnnotation(Ignore.class) == null;
  }

  @Override
  public void testFinished(Description description) throws Exception {
    model.testFinished(description);
  }

  @Override
  public void testRunFinished(Result result) throws Exception {
    model.writeAsXml(xmlStream);
  }

  private class WriteXmlSignalHandler implements SignalHandler {

    @Override
    public void handle(Signal signal) {
      try {
        errPrintStream.printf("%nReceived %s; writing test XML%n", signal.toString());
        requestFactory.cancelRun();
        model.testRunInterrupted();
        model.writeAsXml(xmlStream);
        errPrintStream.println("Done writing test XML");
      } catch (Exception e) {
        errPrintStream.println("Could not write test XML");
        e.printStackTrace(errPrintStream);
      }
    }
  }
}