-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
TestClassExecutionEventGenerator.java
101 lines (90 loc) · 4.04 KB
/
TestClassExecutionEventGenerator.java
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
/*
* Copyright 2012 the original author or authors.
*
* 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 org.gradle.api.internal.tasks.testing.junit;
import org.gradle.api.internal.tasks.testing.DefaultTestClassDescriptor;
import org.gradle.api.internal.tasks.testing.DefaultTestDescriptor;
import org.gradle.api.internal.tasks.testing.TestCompleteEvent;
import org.gradle.api.internal.tasks.testing.TestDescriptorInternal;
import org.gradle.api.internal.tasks.testing.TestResultProcessor;
import org.gradle.api.internal.tasks.testing.TestStartEvent;
import org.gradle.api.tasks.testing.TestFailure;
import org.gradle.api.tasks.testing.TestOutputEvent;
import org.gradle.internal.id.IdGenerator;
import org.gradle.internal.time.Clock;
import java.util.LinkedHashSet;
import java.util.Set;
public class TestClassExecutionEventGenerator implements TestResultProcessor, TestClassExecutionListener {
private final TestResultProcessor resultProcessor;
private final IdGenerator<?> idGenerator;
private final Clock clock;
private final Set<Object> currentTests = new LinkedHashSet<Object>();
private boolean testsStarted;
private TestDescriptorInternal currentTestClass;
public TestClassExecutionEventGenerator(TestResultProcessor resultProcessor, IdGenerator<?> idGenerator, Clock clock) {
this.resultProcessor = resultProcessor;
this.idGenerator = idGenerator;
this.clock = clock;
}
@Override
public void testClassStarted(String testClassName) {
currentTestClass = new DefaultTestClassDescriptor(idGenerator.generateId(), testClassName);
resultProcessor.started(currentTestClass, new TestStartEvent(clock.getCurrentTime()));
}
@Override
public void testClassFinished(TestFailure failure) {
long now = clock.getCurrentTime();
try {
if (failure != null) {
if (currentTests.isEmpty()) {
String testName = testsStarted ? "executionError": "initializationError";
DefaultTestDescriptor initializationError = new DefaultTestDescriptor(idGenerator.generateId(), currentTestClass.getClassName(), testName);
resultProcessor.started(initializationError, new TestStartEvent(now));
resultProcessor.failure(initializationError.getId(), failure);
resultProcessor.completed(initializationError.getId(), new TestCompleteEvent(now));
} else {
for (Object test : currentTests) {
resultProcessor.failure(test, failure);
resultProcessor.completed(test, new TestCompleteEvent(now));
}
}
}
resultProcessor.completed(currentTestClass.getId(), new TestCompleteEvent(now));
} finally {
testsStarted = false;
currentTests.clear();
currentTestClass = null;
}
}
@Override
public void started(TestDescriptorInternal test, TestStartEvent event) {
resultProcessor.started(test, event);
testsStarted = true;
currentTests.add(test.getId());
}
@Override
public void completed(Object testId, TestCompleteEvent event) {
currentTests.remove(testId);
resultProcessor.completed(testId, event);
}
@Override
public void output(Object testId, TestOutputEvent event) {
resultProcessor.output(testId, event);
}
@Override
public void failure(Object testId, Throwable result) {
resultProcessor.failure(testId, result);
}
}