JAVA-618 Move code for 3 articles from core-java-exceptions to core-java-exceptions-2
This commit is contained in:
@@ -1,64 +0,0 @@
|
||||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.classic.spi.LoggingEvent;
|
||||
import ch.qos.logback.core.Appender;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class GlobalExceptionHandlerUnitTest {
|
||||
|
||||
@Mock
|
||||
private Appender<ILoggingEvent> mockAppender;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<LoggingEvent> captorLoggingEvent;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||
logger.addAppender(mockAppender);
|
||||
|
||||
Handler globalExceptionHandler = new Handler();
|
||||
Thread.setDefaultUncaughtExceptionHandler(globalExceptionHandler);
|
||||
}
|
||||
|
||||
@After
|
||||
public void teardown() {
|
||||
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||
logger.detachAppender(mockAppender);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenArithmeticException_thenUseUncaughtExceptionHandler() throws InterruptedException {
|
||||
|
||||
Thread globalExceptionHandlerThread = new Thread() {
|
||||
public void run() {
|
||||
GlobalExceptionHandler globalExceptionHandlerObj = new GlobalExceptionHandler();
|
||||
globalExceptionHandlerObj.performArithmeticOperation(99, 0);
|
||||
}
|
||||
};
|
||||
|
||||
globalExceptionHandlerThread.start();
|
||||
globalExceptionHandlerThread.join();
|
||||
|
||||
verify(mockAppender).doAppend(captorLoggingEvent.capture());
|
||||
LoggingEvent loggingEvent = captorLoggingEvent.getValue();
|
||||
|
||||
assertThat(loggingEvent.getLevel()).isEqualTo(Level.INFO);
|
||||
assertThat(loggingEvent.getFormattedMessage()).isEqualTo("Unhandled exception caught!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
package com.baeldung.exceptions.rootcausefinder;
|
||||
|
||||
import com.baeldung.exceptions.rootcausefinder.RootCauseFinder.CalculationException;
|
||||
import com.baeldung.exceptions.rootcausefinder.RootCauseFinder.DateOutOfRangeException;
|
||||
import com.google.common.base.Throwables;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
import static com.baeldung.exceptions.rootcausefinder.RootCauseFinder.AgeCalculator;
|
||||
import static com.baeldung.exceptions.rootcausefinder.RootCauseFinder.findCauseUsingPlainJava;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests the {@link RootCauseFinder}.
|
||||
*/
|
||||
public class RootCauseFinderUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenBirthDate_whenCalculatingAge_thenAgeReturned() {
|
||||
try {
|
||||
int age = AgeCalculator.calculateAge("1990-01-01");
|
||||
Assertions.assertEquals(1990, LocalDate
|
||||
.now()
|
||||
.minus(age, ChronoUnit.YEARS)
|
||||
.getYear());
|
||||
} catch (CalculationException e) {
|
||||
Assertions.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWrongFormatDate_whenFindingRootCauseUsingJava_thenRootCauseFound() {
|
||||
try {
|
||||
AgeCalculator.calculateAge("010102");
|
||||
} catch (CalculationException ex) {
|
||||
assertTrue(findCauseUsingPlainJava(ex) instanceof DateTimeParseException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOutOfRangeDate_whenFindingRootCauseUsingJava_thenRootCauseFound() {
|
||||
try {
|
||||
AgeCalculator.calculateAge("2020-04-04");
|
||||
} catch (CalculationException ex) {
|
||||
assertTrue(findCauseUsingPlainJava(ex) instanceof DateOutOfRangeException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNullDate_whenFindingRootCauseUsingJava_thenRootCauseFound() {
|
||||
try {
|
||||
AgeCalculator.calculateAge(null);
|
||||
} catch (Exception ex) {
|
||||
assertTrue(findCauseUsingPlainJava(ex) instanceof IllegalArgumentException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWrongFormatDate_whenFindingRootCauseUsingApacheCommons_thenRootCauseFound() {
|
||||
try {
|
||||
AgeCalculator.calculateAge("010102");
|
||||
} catch (CalculationException ex) {
|
||||
assertTrue(ExceptionUtils.getRootCause(ex) instanceof DateTimeParseException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOutOfRangeDate_whenFindingRootCauseUsingApacheCommons_thenRootCauseFound() {
|
||||
try {
|
||||
AgeCalculator.calculateAge("2020-04-04");
|
||||
} catch (CalculationException ex) {
|
||||
assertTrue(ExceptionUtils.getRootCause(ex) instanceof DateOutOfRangeException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWrongFormatDate_whenFindingRootCauseUsingGuava_thenRootCauseFound() {
|
||||
try {
|
||||
AgeCalculator.calculateAge("010102");
|
||||
} catch (CalculationException ex) {
|
||||
assertTrue(Throwables.getRootCause(ex) instanceof DateTimeParseException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOutOfRangeDate_whenFindingRootCauseUsingGuava_thenRootCauseFound() {
|
||||
try {
|
||||
AgeCalculator.calculateAge("2020-04-04");
|
||||
} catch (CalculationException ex) {
|
||||
assertTrue(Throwables.getRootCause(ex) instanceof DateOutOfRangeException);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user