Polish "Unwrap InvocationTargetException in isLogConfigurationMessage"

Closes gh-12958
pull/12071/merge
Andy Wilkinson 7 years ago
parent 2953ed1dca
commit 678e125720

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2016 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -89,7 +89,6 @@ class SpringBootExceptionHandler implements UncaughtExceptionHandler {
if (ex instanceof InvocationTargetException) { if (ex instanceof InvocationTargetException) {
return isLogConfigurationMessage(ex.getCause()); return isLogConfigurationMessage(ex.getCause());
} }
String message = ex.getMessage(); String message = ex.getMessage();
if (message != null) { if (message != null) {
for (String candidate : LOG_CONFIGURATION_MESSAGES) { for (String candidate : LOG_CONFIGURATION_MESSAGES) {

@ -16,17 +16,13 @@
package org.springframework.boot; package org.springframework.boot;
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import static org.mockito.Matchers.same;
import static org.mockito.ArgumentMatchers.same; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.verifyZeroInteractions;
@ -34,48 +30,42 @@ import static org.mockito.Mockito.verifyZeroInteractions;
* Tests for {@link SpringBootExceptionHandler}. * Tests for {@link SpringBootExceptionHandler}.
* *
* @author Henri Tremblay * @author Henri Tremblay
* @author Andy Wilkinson
*/ */
public class SpringBootExceptionHandlerTest { public class SpringBootExceptionHandlerTests {
@Rule
public MockitoRule rule = MockitoJUnit.rule();
@Mock private final UncaughtExceptionHandler parent = mock(UncaughtExceptionHandler.class);
private Thread.UncaughtExceptionHandler parent;
@InjectMocks private final SpringBootExceptionHandler handler = new SpringBootExceptionHandler(
private SpringBootExceptionHandler handler; this.parent);
@Test @Test
public void uncaughtException_shouldNotForwardLoggedErrorToParent() { public void uncaughtExceptionDoesNotForwardLoggedErrorToParent() {
Thread thread = Thread.currentThread(); Thread thread = Thread.currentThread();
Exception ex = new Exception(); Exception ex = new Exception();
this.handler.registerLoggedException(ex); this.handler.registerLoggedException(ex);
this.handler.uncaughtException(thread, ex); this.handler.uncaughtException(thread, ex);
verifyZeroInteractions(this.parent); verifyZeroInteractions(this.parent);
} }
@Test @Test
public void uncaughtException_shouldForwardLogConfigurationErrorToParent() { public void uncaughtExceptionForwardsLogConfigurationErrorToParent() {
Thread thread = Thread.currentThread(); Thread thread = Thread.currentThread();
Exception ex = new Exception("[stuff] Logback configuration error detected [stuff]"); Exception ex = new Exception(
"[stuff] Logback configuration error detected [stuff]");
this.handler.registerLoggedException(ex); this.handler.registerLoggedException(ex);
this.handler.uncaughtException(thread, ex); this.handler.uncaughtException(thread, ex);
verify(this.parent).uncaughtException(same(thread), same(ex)); verify(this.parent).uncaughtException(same(thread), same(ex));
} }
@Test @Test
public void uncaughtException_shouldForwardLogConfigurationErrorToParentEvenWhenWrapped() { public void uncaughtExceptionForwardsWrappedLogConfigurationErrorToParent() {
Thread thread = Thread.currentThread(); Thread thread = Thread.currentThread();
Exception ex = new InvocationTargetException(new Exception("[stuff] Logback configuration error detected [stuff]", new Exception())); Exception ex = new InvocationTargetException(new Exception(
"[stuff] Logback configuration error detected [stuff]", new Exception()));
this.handler.registerLoggedException(ex); this.handler.registerLoggedException(ex);
this.handler.uncaughtException(thread, ex); this.handler.uncaughtException(thread, ex);
verify(this.parent).uncaughtException(same(thread), same(ex)); verify(this.parent).uncaughtException(same(thread), same(ex));
} }
} }
Loading…
Cancel
Save