aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/java_tools/junitrunner/java/com/google/testing/junit/runner/model/TestCaseNode.java
blob: cdaaf953fe4d82af1adb7903ced8af3919f03d32 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// 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.model;

import static com.google.testing.junit.runner.util.TestPropertyExporter.INITIAL_INDEX_FOR_REPEATED_PROPERTY;

import com.google.testing.junit.runner.model.TestResult.Status;
import com.google.testing.junit.runner.util.TestIntegration;
import com.google.testing.junit.runner.util.TestIntegrationsExporter;
import com.google.testing.junit.runner.util.TestPropertyExporter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
import javax.annotation.Nullable;
import org.junit.runner.Description;

/** A leaf in the test suite model. */
class TestCaseNode extends TestNode
    implements TestPropertyExporter.Callback, TestIntegrationsExporter.Callback {
  private static final Set<State> INITIAL_STATES = Collections.unmodifiableSet(
      EnumSet.of(State.INITIAL, State.PENDING));
  private final TestSuiteNode parent;
  private final Map<String, String> properties = new ConcurrentHashMap<>();
  private final Map<String, Integer> repeatedPropertyNamesToRepetitions = new HashMap<>();
  private final Queue<Throwable> globalFailures = new ConcurrentLinkedQueue<>();
  private final ConcurrentMap<Description, List<Throwable>> dynamicTestToFailures =
      new ConcurrentHashMap<>();
  private final Set<TestIntegration> integrations =
      Collections.newSetFromMap(new ConcurrentHashMap<TestIntegration, Boolean>());

  @Nullable private volatile TestInterval runTimeInterval = null;
  private volatile State state = State.INITIAL;

  TestCaseNode(Description description, TestSuiteNode parent) {
    super(description);
    this.parent = parent;
  }

  // VisibleForTesting
  @Override
  public List<TestNode> getChildren() {
    return Collections.emptyList();
  }

  /**
   * Indicates that the test represented by this node is scheduled to start.
   */
  void pending() {
    compareAndSetState(State.INITIAL, State.PENDING, -1);
  }

  /**
   * Indicates that the test represented by this node has started.
   *
   * @param now Time that the test started
   */
  public void started(long now) {
    compareAndSetState(INITIAL_STATES, State.STARTED, now);
  }

  @Override
  public void testInterrupted(long now) {
    if (compareAndSetState(State.STARTED, State.INTERRUPTED, now)) {
      return;
    }
    compareAndSetState(INITIAL_STATES, State.CANCELLED, now);
  }

  @Override
  public void exportProperty(String name, String value) {
    properties.put(name, value);
  }

  @Override
  public String exportRepeatedProperty(String name, String value) {
    String propertyName = getRepeatedPropertyName(name);
    properties.put(propertyName, value);
    return propertyName;
  }

  @Override
  public void exportTestIntegration(TestIntegration testIntegration) {
    integrations.add(testIntegration);
  }

  @Override
  public void testSkipped(long now) {
    compareAndSetState(State.STARTED, State.SKIPPED, now);
  }


  @Override
  public void testSuppressed(long now) {
    compareAndSetState(INITIAL_STATES, State.SUPPRESSED, now);
  }

  /**
   * Indicates that the test represented by this node has finished.
   *
   * @param now Time that the test finished
   */
  public void finished(long now) {
    compareAndSetState(State.STARTED, State.FINISHED, now);
  }

  @Override
  public void testFailure(Throwable throwable, long now) {
    compareAndSetState(INITIAL_STATES, State.FINISHED, now);
    globalFailures.add(throwable);
  }

  @Override
  public void dynamicTestFailure(Description test, Throwable throwable, long now) {
    compareAndSetState(INITIAL_STATES, State.FINISHED, now);
    addThrowableToDynamicTestToFailures(test, throwable);
  }

  private String getRepeatedPropertyName(String name) {
    int index = addNameToRepeatedPropertyNamesAndGetRepetitionsNr(name)
        + INITIAL_INDEX_FOR_REPEATED_PROPERTY;
    return name + index;
  }

  @Override
  public boolean isTestCase() {
    return true;
  }

  private synchronized void addThrowableToDynamicTestToFailures(
      Description test, Throwable throwable) {
    List<Throwable> throwables = dynamicTestToFailures.get(test);
    if (throwables == null) {
      throwables = new ArrayList<>();
      dynamicTestToFailures.put(test, throwables);
    }
    throwables.add(throwable);
  }

  private synchronized int addNameToRepeatedPropertyNamesAndGetRepetitionsNr(String name) {
    Integer previousRepetitionsNr = repeatedPropertyNamesToRepetitions.get(name);
    if (previousRepetitionsNr == null) {
      previousRepetitionsNr = 0;
    }
    repeatedPropertyNamesToRepetitions.put(name, previousRepetitionsNr + 1);
    return previousRepetitionsNr;
  }

  private boolean compareAndSetState(State fromState, State toState, long now) {
    if (fromState == null) {
      throw new NullPointerException();
    }
    return compareAndSetState(Collections.singleton(fromState), toState, now);
  }

  // TODO(bazel-team): Use AtomicReference instead of a synchronized method.
  private synchronized boolean compareAndSetState(Set<State> fromStates, State toState, long now) {
    if (fromStates == null || toState == null || state == null) {
      throw new NullPointerException();
    }
    if (fromStates.isEmpty()) {
      throw new IllegalArgumentException();
    }

    if (fromStates.contains(state) && toState != state) {
      state = toState;
      if (toState != State.PENDING) {
        runTimeInterval =
            runTimeInterval == null
            ? new TestInterval(now, now)
            : runTimeInterval.withEndMillis(now);
      }
      return true;
    }
    return false;
  }

  @Nullable
  public TestInterval getRuntime() {
    return runTimeInterval;
  }

  /**
   * @return The equivalent {@link TestResult.Status} if the test execution ends with the FSM
   *      at this state.
   */
  public TestResult.Status getTestResultStatus() {
    return state.getTestResultStatus();
  }

  @Override
  protected TestResult buildResult() {
    // Some test descriptions, like those provided by JavaScript tests, are
    // constructed by Description.createSuiteDescription, not
    // createTestDescription, because they don't have a "class" per se.
    // In this case, getMethodName returns null and we fill in the className
    // attribute with the name of the parent test suite.
    String name = getDescription().getMethodName();
    String className = getDescription().getClassName();
    if (name == null) {
      name = className;
      className = parent.getDescription().getDisplayName();
    }

    // For now, we give each dynamic test an empty properties map and the same
    // run time and status as its parent test case, but this may change.
    List<TestResult> childResults = new ArrayList<>();
    for (Description dynamicTest : getDescription().getChildren()) {
      childResults.add(buildDynamicResult(dynamicTest, getRuntime(), getTestResultStatus()));
    }

    int numTests = getDescription().isTest() ? 1 : getDescription().getChildren().size();
    int numFailures = globalFailures.isEmpty() ? dynamicTestToFailures.keySet().size() : numTests;
    return new TestResult.Builder()
        .name(name)
        .className(className)
        .properties(properties)
        .failures(new ArrayList<>(globalFailures))
        .runTimeInterval(getRuntime())
        .status(getTestResultStatus())
        .numTests(numTests)
        .numFailures(numFailures)
        .childResults(childResults)
        .integrations(integrations)
        .build();
  }

  private TestResult buildDynamicResult(
      Description test, @Nullable TestInterval runTime, TestResult.Status status) {
    // The dynamic test fails if the testcase itself fails or there is
    // a dynamic failure specifically for the dynamic test.
    List<Throwable> dynamicFailures = dynamicTestToFailures.get(test);
    if (dynamicFailures == null) {
      dynamicFailures = new ArrayList<>();
    }
    boolean failed = !globalFailures.isEmpty() || !dynamicFailures.isEmpty();
    return new TestResult.Builder()
        .name(test.getDisplayName())
        .className(getDescription().getDisplayName())
        .properties(Collections.<String, String>emptyMap())
        .failures(dynamicFailures)
        .runTimeInterval(runTime)
        .status(status)
        .numTests(1)
        .numFailures(failed ? 1 : 0)
        .childResults(Collections.<TestResult>emptyList())
        .integrations(Collections.<TestIntegration>emptySet())
        .build();
  }

  /**
   * States of a TestCaseNode (see (link) for all the transitions and states descriptions).
   */
  private static enum State {
    INITIAL(TestResult.Status.FILTERED),
    PENDING(TestResult.Status.CANCELLED),
    STARTED(TestResult.Status.INTERRUPTED),
    SKIPPED(TestResult.Status.SKIPPED),
    SUPPRESSED(TestResult.Status.SUPPRESSED),
    CANCELLED(TestResult.Status.CANCELLED),
    INTERRUPTED(TestResult.Status.INTERRUPTED),
    FINISHED(TestResult.Status.COMPLETED);

    private final Status status;

    State(TestResult.Status status) {
      this.status = status;
    }

    /**
     * @return The equivalent {@link TestResult.Status} if the test execution ends with the FSM
     *      at this state.
     */
    public TestResult.Status getTestResultStatus() {
      return status;
    }
  }
}