aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4RunnerModule.java
blob: 5f15524808494e62930d8fbdcf04a2ffe875b1f1 (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
// Copyright 2015 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 static com.google.inject.multibindings.Multibinder.newSetBinder;

import com.google.common.base.Optional;
import com.google.common.base.Ticker;
import com.google.common.collect.ImmutableList;
import com.google.common.io.ByteStreams;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.multibindings.Multibinder;
import com.google.testing.junit.runner.internal.SignalHandlers;
import com.google.testing.junit.runner.util.TestNameProvider;

import org.junit.runner.notification.RunListener;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.List;

/**
 * Guice module for real test runs.
 */
public class JUnit4RunnerModule extends AbstractModule {
  private final Class<?> suite;
  private final JUnit4Config config;
  private final ImmutableList<String> unparsedArgs;

  public static JUnit4RunnerModule create(Class<?> suite, List<String> args) {
    JUnit4Options options = JUnit4Options.parse(System.getenv(), ImmutableList.copyOf(args));
    JUnit4Config config = new JUnit4Config(
        options.getTestIncludeFilter(),
        options.getTestExcludeFilter(),
        Optional.<Path>absent());
    return new JUnit4RunnerModule(suite, config, ImmutableList.copyOf(options.getUnparsedArgs()));
  }

  private JUnit4RunnerModule(
      Class<?> suite, JUnit4Config config, ImmutableList<String> unparsedArgs) {
    this.suite = suite;
    this.config = config;
    this.unparsedArgs = unparsedArgs;
  }

  @Override
  protected void configure() {
    install(new JUnit4RunnerBaseModule(suite));

    // We require explicit bindings so we don't use an unexpected just-in-time binding
    bind(SignalHandlers.class);

    // Normal bindings
    bind(JUnit4Config.class).toInstance(config);
    bind(Ticker.class).toInstance(Ticker.systemTicker());
    bind(SignalHandlers.HandlerInstaller.class).toInstance(
        SignalHandlers.createRealHandlerInstaller());

    // Bind listeners
    Multibinder<RunListener> listenerBinder = newSetBinder(binder(), RunListener.class);
    listenerBinder.addBinding().to(JUnit4TestNameListener.class);
    listenerBinder.addBinding().to(JUnit4TestXmlListener.class);
    listenerBinder.addBinding().to(JUnit4TestStackTraceListener.class);
  }

  @Provides @Singleton @Xml
  OutputStream provideXmlStream() {
    Optional<Path> path = config.getXmlOutputPath();

    if (path.isPresent()) {
      try {
        // TODO(bazel-team): Change the provider method to return ByteSink or CharSink
        return new FileOutputStream(path.get().toFile());
      } catch (FileNotFoundException e) {
        /*
         * We try to avoid throwing exceptions in the runner code. There is no
         * way to induce a test failure here, so the only thing we can do is
         * print a message and move on.
         */
        e.printStackTrace();
      }
    }

    return ByteStreams.nullOutputStream();
  }

  @Provides @Singleton
  SettableCurrentRunningTest provideCurrentRunningTest() {
    return new SettableCurrentRunningTest() {
      void setGlobalTestNameProvider(TestNameProvider provider) {
        testNameProvider = provider;
      }
    };
  }

  /**
   * Gets the list of unparsed command line arguments.
   */
  public ImmutableList<String> getUnparsedArgs() {
    return unparsedArgs;
  }
}