1
1
/*
2
- * Copyright 2012-2020 the original author or authors.
2
+ * Copyright 2012-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .boot .build .testing ;
18
18
19
19
import java .util .ArrayList ;
20
- import java .util .Collections ;
21
20
import java .util .List ;
22
- import java .util .Map ;
23
- import java .util .TreeMap ;
24
21
25
- import org .gradle .BuildResult ;
26
22
import org .gradle .api .Plugin ;
27
23
import org .gradle .api .Project ;
24
+ import org .gradle .api .provider .Provider ;
28
25
import org .gradle .api .tasks .testing .Test ;
29
26
import org .gradle .api .tasks .testing .TestDescriptor ;
30
27
import org .gradle .api .tasks .testing .TestListener ;
@@ -39,46 +36,37 @@ public class TestFailuresPlugin implements Plugin<Project> {
39
36
40
37
@ Override
41
38
public void apply (Project project ) {
42
- TestResultsExtension testResults = getOrCreateTestResults (project );
39
+ Provider <TestResultsOverview > testResultsOverview = project .getGradle ().getSharedServices ()
40
+ .registerIfAbsent ("testResultsOverview" , TestResultsOverview .class , (spec ) -> {
41
+ });
43
42
project .getTasks ().withType (Test .class ,
44
- (test ) -> test .addTestListener (new FailureRecordingTestListener (testResults , test )));
45
- }
46
-
47
- private TestResultsExtension getOrCreateTestResults (Project project ) {
48
- TestResultsExtension testResults = project .getRootProject ().getExtensions ()
49
- .findByType (TestResultsExtension .class );
50
- if (testResults == null ) {
51
- testResults = project .getRootProject ().getExtensions ().create ("testResults" , TestResultsExtension .class );
52
- project .getRootProject ().getGradle ().buildFinished (testResults ::buildFinished );
53
- }
54
- return testResults ;
43
+ (test ) -> test .addTestListener (new FailureRecordingTestListener (testResultsOverview , test )));
55
44
}
56
45
57
46
private final class FailureRecordingTestListener implements TestListener {
58
47
59
- private final List <TestFailure > failures = new ArrayList <>();
48
+ private final List <TestDescriptor > failures = new ArrayList <>();
60
49
61
- private final TestResultsExtension testResults ;
50
+ private final Provider < TestResultsOverview > testResultsOverview ;
62
51
63
52
private final Test test ;
64
53
65
- private FailureRecordingTestListener (TestResultsExtension testResults , Test test ) {
66
- this .testResults = testResults ;
54
+ private FailureRecordingTestListener (Provider < TestResultsOverview > testResultOverview , Test test ) {
55
+ this .testResultsOverview = testResultOverview ;
67
56
this .test = test ;
68
57
}
69
58
70
59
@ Override
71
60
public void afterSuite (TestDescriptor descriptor , TestResult result ) {
72
61
if (!this .failures .isEmpty ()) {
73
- Collections .sort (this .failures );
74
- this .testResults .addFailures (this .test , this .failures );
62
+ this .testResultsOverview .get ().addFailures (this .test , this .failures );
75
63
}
76
64
}
77
65
78
66
@ Override
79
67
public void afterTest (TestDescriptor descriptor , TestResult result ) {
80
68
if (result .getFailedTestCount () > 0 ) {
81
- this .failures .add (new TestFailure ( descriptor ) );
69
+ this .failures .add (descriptor );
82
70
}
83
71
}
84
72
@@ -94,55 +82,4 @@ public void beforeTest(TestDescriptor descriptor) {
94
82
95
83
}
96
84
97
- private static final class TestFailure implements Comparable <TestFailure > {
98
-
99
- private final TestDescriptor descriptor ;
100
-
101
- private TestFailure (TestDescriptor descriptor ) {
102
- this .descriptor = descriptor ;
103
- }
104
-
105
- @ Override
106
- public int compareTo (TestFailure other ) {
107
- int comparison = this .descriptor .getClassName ().compareTo (other .descriptor .getClassName ());
108
- if (comparison == 0 ) {
109
- comparison = this .descriptor .getName ().compareTo (other .descriptor .getName ());
110
- }
111
- return comparison ;
112
- }
113
-
114
- }
115
-
116
- public static class TestResultsExtension {
117
-
118
- private final Map <Test , List <TestFailure >> testFailures = new TreeMap <>(
119
- (one , two ) -> one .getPath ().compareTo (two .getPath ()));
120
-
121
- private final Object monitor = new Object ();
122
-
123
- void addFailures (Test test , List <TestFailure > testFailures ) {
124
- synchronized (this .monitor ) {
125
- this .testFailures .put (test , testFailures );
126
- }
127
- }
128
-
129
- public void buildFinished (BuildResult result ) {
130
- synchronized (this .monitor ) {
131
- if (this .testFailures .isEmpty ()) {
132
- return ;
133
- }
134
- System .err .println ();
135
- System .err .println ("Found test failures in " + this .testFailures .size () + " test task"
136
- + ((this .testFailures .size () == 1 ) ? ":" : "s:" ));
137
- this .testFailures .forEach ((task , failures ) -> {
138
- System .err .println ();
139
- System .err .println (task .getPath ());
140
- failures .forEach ((failure ) -> System .err .println (
141
- " " + failure .descriptor .getClassName () + " > " + failure .descriptor .getName ()));
142
- });
143
- }
144
- }
145
-
146
- }
147
-
148
85
}
0 commit comments