|
1 | 1 | /*
|
2 |
| - * Copyright 2021 the original author or authors. |
| 2 | + * Copyright 2021-2022 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.
|
|
18 | 18 |
|
19 | 19 | import static org.mockito.ArgumentMatchers.any;
|
20 | 20 | import static org.mockito.Mockito.mock;
|
| 21 | +import static org.mockito.Mockito.never; |
21 | 22 | import static org.mockito.Mockito.verify;
|
22 | 23 |
|
23 | 24 | import java.io.IOException;
|
|
31 | 32 | import org.springframework.kafka.KafkaException;
|
32 | 33 |
|
33 | 34 | /**
|
34 |
| - * Tests for {@link CommonDelegatingErrorHandler}. Copied from |
35 |
| - * {@link ConditionalDelegatingErrorHandlerTests} with changed handler type. |
| 35 | + * Tests for {@link CommonDelegatingErrorHandler}. |
36 | 36 | *
|
37 | 37 | * @author Gary Russell
|
| 38 | + * @author Adrian Chlebosz |
38 | 39 | * @since 2.8
|
39 | 40 | *
|
40 | 41 | */
|
@@ -88,6 +89,46 @@ void testBatchDelegates() {
|
88 | 89 | verify(one).handleBatch(any(), any(), any(), any(), any());
|
89 | 90 | }
|
90 | 91 |
|
| 92 | + @Test |
| 93 | + void testDelegateForInitialCauseOfThrowableIsAppliedWhenDeepTraversingFlagIsSet() { |
| 94 | + var defaultHandler = mock(CommonErrorHandler.class); |
| 95 | + |
| 96 | + var initErrorHandler = mock(CommonErrorHandler.class); |
| 97 | + var initExc = new IllegalStateException(); |
| 98 | + var directCauseErrorHandler = mock(CommonErrorHandler.class); |
| 99 | + var directCauseExc = new IllegalArgumentException(initExc); |
| 100 | + var errorHandler = mock(CommonErrorHandler.class); |
| 101 | + var exc = new UnsupportedOperationException(directCauseExc); |
| 102 | + |
| 103 | + var delegatingErrorHandler = new CommonDelegatingErrorHandler(defaultHandler); |
| 104 | + delegatingErrorHandler.setDeepCauseChainTraversing(true); |
| 105 | + delegatingErrorHandler.setErrorHandlers(Map.of( |
| 106 | + initExc.getClass(), initErrorHandler, |
| 107 | + directCauseExc.getClass(), directCauseErrorHandler, |
| 108 | + exc.getClass(), errorHandler |
| 109 | + )); |
| 110 | + |
| 111 | + delegatingErrorHandler.handleRemaining(exc, Collections.emptyList(), mock(Consumer.class), |
| 112 | + mock(MessageListenerContainer.class)); |
| 113 | + |
| 114 | + verify(initErrorHandler).handleRemaining(any(), any(), any(), any()); |
| 115 | + verify(directCauseErrorHandler, never()).handleRemaining(any(), any(), any(), any()); |
| 116 | + verify(errorHandler, never()).handleRemaining(any(), any(), any(), any()); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + @SuppressWarnings("ConstantConditions") |
| 121 | + void testDefaultDelegateIsAppliedWhenDeepTraversingFlagIsSetAndNullThrowableHasBeenPassed() { |
| 122 | + var defaultHandler = mock(CommonErrorHandler.class); |
| 123 | + var delegatingErrorHandler = new CommonDelegatingErrorHandler(defaultHandler); |
| 124 | + delegatingErrorHandler.setDeepCauseChainTraversing(true); |
| 125 | + |
| 126 | + delegatingErrorHandler.handleRemaining(null, Collections.emptyList(), mock(Consumer.class), |
| 127 | + mock(MessageListenerContainer.class)); |
| 128 | + |
| 129 | + verify(defaultHandler).handleRemaining(any(), any(), any(), any()); |
| 130 | + } |
| 131 | + |
91 | 132 | private Exception wrap(Exception ex) {
|
92 | 133 | return new ListenerExecutionFailedException("test", ex);
|
93 | 134 | }
|
|
0 commit comments