|
1 | 1 | /*
|
2 |
| - * Copyright 2021-2022 the original author or authors. |
| 2 | + * Copyright 2021-2024 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.kafka.listener;
|
18 | 18 |
|
19 | 19 | import static org.assertj.core.api.Assertions.assertThat;
|
| 20 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
20 | 21 | import static org.mockito.ArgumentMatchers.any;
|
| 22 | +import static org.mockito.BDDMockito.given; |
21 | 23 | import static org.mockito.Mockito.mock;
|
22 | 24 | import static org.mockito.Mockito.never;
|
23 | 25 | import static org.mockito.Mockito.verify;
|
|
39 | 41 | *
|
40 | 42 | * @author Gary Russell
|
41 | 43 | * @author Adrian Chlebosz
|
| 44 | + * @author Antonin Arquey |
42 | 45 | * @since 2.8
|
43 | 46 | *
|
44 | 47 | */
|
@@ -173,6 +176,48 @@ void testDefaultDelegateIsApplied() {
|
173 | 176 | verify(defaultHandler).handleRemaining(any(), any(), any(), any());
|
174 | 177 | }
|
175 | 178 |
|
| 179 | + @Test |
| 180 | + void testAddIncompatibleAckAfterHandleDelegate() { |
| 181 | + var defaultHandler = mock(CommonErrorHandler.class); |
| 182 | + given(defaultHandler.isAckAfterHandle()).willReturn(true); |
| 183 | + var delegatingErrorHandler = new CommonDelegatingErrorHandler(defaultHandler); |
| 184 | + var delegate = mock(CommonErrorHandler.class); |
| 185 | + given(delegate.isAckAfterHandle()).willReturn(false); |
| 186 | + |
| 187 | + assertThatThrownBy(() -> delegatingErrorHandler.addDelegate(IllegalStateException.class, delegate)) |
| 188 | + .isInstanceOf(IllegalArgumentException.class) |
| 189 | + .hasMessage("All delegates must return the same value when calling 'isAckAfterHandle()'"); |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + void testAddIncompatibleSeeksAfterHandlingDelegate() { |
| 194 | + var defaultHandler = mock(CommonErrorHandler.class); |
| 195 | + given(defaultHandler.seeksAfterHandling()).willReturn(true); |
| 196 | + var delegatingErrorHandler = new CommonDelegatingErrorHandler(defaultHandler); |
| 197 | + var delegate = mock(CommonErrorHandler.class); |
| 198 | + given(delegate.seeksAfterHandling()).willReturn(false); |
| 199 | + |
| 200 | + assertThatThrownBy(() -> delegatingErrorHandler.addDelegate(IllegalStateException.class, delegate)) |
| 201 | + .isInstanceOf(IllegalArgumentException.class) |
| 202 | + .hasMessage("All delegates must return the same value when calling 'seeksAfterHandling()'"); |
| 203 | + } |
| 204 | + |
| 205 | + @Test |
| 206 | + void testAddMultipleDelegatesWithOneIncompatible() { |
| 207 | + var defaultHandler = mock(CommonErrorHandler.class); |
| 208 | + given(defaultHandler.seeksAfterHandling()).willReturn(true); |
| 209 | + var delegatingErrorHandler = new CommonDelegatingErrorHandler(defaultHandler); |
| 210 | + var one = mock(CommonErrorHandler.class); |
| 211 | + given(one.seeksAfterHandling()).willReturn(true); |
| 212 | + var two = mock(CommonErrorHandler.class); |
| 213 | + given(one.seeksAfterHandling()).willReturn(false); |
| 214 | + Map<Class<? extends Throwable>, CommonErrorHandler> delegates = Map.of(IllegalStateException.class, one, IOException.class, two); |
| 215 | + |
| 216 | + assertThatThrownBy(() -> delegatingErrorHandler.setErrorHandlers(delegates)) |
| 217 | + .isInstanceOf(IllegalArgumentException.class) |
| 218 | + .hasMessage("All delegates must return the same value when calling 'seeksAfterHandling()'"); |
| 219 | + } |
| 220 | + |
176 | 221 | private Exception wrap(Exception ex) {
|
177 | 222 | return new ListenerExecutionFailedException("test", ex);
|
178 | 223 | }
|
|
0 commit comments