Skip to content

Commit ae00228

Browse files
committed
Merge pull request #27862 from smoothbear
* pr/27862: Polish "Add testing for AbstractFailureAnalyzer.findCause" Add testing for AbstractFailureAnalyzer.findCause Closes gh-27862
2 parents d06ebce + d68b6bb commit ae00228

File tree

2 files changed

+97
-5
lines changed

2 files changed

+97
-5
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/AbstractFailureAnalyzer.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,10 +31,7 @@ public abstract class AbstractFailureAnalyzer<T extends Throwable> implements Fa
3131
@Override
3232
public FailureAnalysis analyze(Throwable failure) {
3333
T cause = findCause(failure, getCauseType());
34-
if (cause != null) {
35-
return analyze(failure, cause);
36-
}
37-
return null;
34+
return (cause != null) ? analyze(failure, cause) : null;
3835
}
3936

4037
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.diagnostics;
18+
19+
import java.io.IOException;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* Tests for {@link AbstractFailureAnalyzer}.
27+
*
28+
* @author Kim Jung Bin
29+
* @author Stephane Nicoll
30+
*/
31+
class AbstractFailureAnalyzerTests {
32+
33+
private final TestFailureAnalyzer failureAnalyzer = new TestFailureAnalyzer();
34+
35+
@Test
36+
void findCauseWithNullException() {
37+
assertThat(this.failureAnalyzer.findCause(null, Throwable.class)).isNull();
38+
}
39+
40+
@Test
41+
void findCauseWithDirectExactMatch() {
42+
TestException ex = new TestException();
43+
assertThat(this.failureAnalyzer.findCause(ex, TestException.class)).isEqualTo(ex);
44+
}
45+
46+
@Test
47+
void findCauseWithDirectSubClass() {
48+
SpecificTestException ex = new SpecificTestException();
49+
assertThat(this.failureAnalyzer.findCause(ex, TestException.class)).isEqualTo(ex);
50+
}
51+
52+
@Test
53+
void findCauseWitNestedAndExactMatch() {
54+
TestException ex = new TestException();
55+
assertThat(this.failureAnalyzer.findCause(new IllegalArgumentException(new IllegalStateException(ex)),
56+
TestException.class)).isEqualTo(ex);
57+
}
58+
59+
@Test
60+
void findCauseWitNestedAndSubClass() {
61+
SpecificTestException ex = new SpecificTestException();
62+
assertThat(this.failureAnalyzer.findCause(new IOException(new IllegalStateException(ex)), TestException.class))
63+
.isEqualTo(ex);
64+
}
65+
66+
@Test
67+
void findCauseWithUnrelatedException() {
68+
IOException ex = new IOException();
69+
assertThat(this.failureAnalyzer.findCause(ex, TestException.class)).isNull();
70+
}
71+
72+
@Test
73+
void findCauseWithMoreSpecificException() {
74+
TestException ex = new TestException();
75+
assertThat(this.failureAnalyzer.findCause(ex, SpecificTestException.class)).isNull();
76+
}
77+
78+
static class TestFailureAnalyzer extends AbstractFailureAnalyzer<Throwable> {
79+
80+
@Override
81+
protected FailureAnalysis analyze(Throwable rootFailure, Throwable cause) {
82+
return null;
83+
}
84+
85+
}
86+
87+
static class TestException extends Exception {
88+
89+
}
90+
91+
static class SpecificTestException extends TestException {
92+
93+
}
94+
95+
}

0 commit comments

Comments
 (0)