|
| 1 | +/* |
| 2 | + * Copyright 2021 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.kafka.support; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.mockito.ArgumentMatchers.any; |
| 21 | +import static org.mockito.BDDMockito.willAnswer; |
| 22 | +import static org.mockito.Mockito.spy; |
| 23 | + |
| 24 | +import java.util.concurrent.atomic.AtomicReference; |
| 25 | +import java.util.function.Supplier; |
| 26 | + |
| 27 | +import org.apache.kafka.clients.producer.ProducerRecord; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import org.springframework.beans.DirectFieldAccessor; |
| 31 | +import org.springframework.core.log.LogAccessor; |
| 32 | +import org.springframework.kafka.test.utils.KafkaTestUtils; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author Gary Russell |
| 36 | + * @since 2.5.16 |
| 37 | + * |
| 38 | + */ |
| 39 | +public class LoggingProducerListenerTests { |
| 40 | + |
| 41 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 42 | + @Test |
| 43 | + void noBytesInLog() { |
| 44 | + LoggingProducerListener pl = new LoggingProducerListener(); |
| 45 | + LogAccessor logger = (LogAccessor) spy(KafkaTestUtils.getPropertyValue(pl, "logger")); |
| 46 | + new DirectFieldAccessor(pl).setPropertyValue("logger", logger); |
| 47 | + AtomicReference<String> string = new AtomicReference<>(); |
| 48 | + willAnswer(inv -> { |
| 49 | + Supplier<String> stringer = inv.getArgument(1); |
| 50 | + string.set(stringer.get()); |
| 51 | + return null; |
| 52 | + }).given(logger).error(any(), any(Supplier.class)); |
| 53 | + pl.onError(new ProducerRecord("foo", 0, new byte[3], new byte[1111]), null, |
| 54 | + new RuntimeException()); |
| 55 | + assertThat(string.get()).contains("byte[3]"); |
| 56 | + assertThat(string.get()).contains("byte[1111]"); |
| 57 | + } |
| 58 | + |
| 59 | +} |
0 commit comments