|
| 1 | +package io.opentelemetry.javaagent.tooling.bytebuddy.matcher; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 5 | +import static org.mockito.ArgumentMatchers.any; |
| 6 | +import static org.mockito.Mockito.mock; |
| 7 | +import static org.mockito.Mockito.when; |
| 8 | + |
| 9 | +import io.opentelemetry.javaagent.tooling.bytebuddy.LoggingFailSafeMatcher; |
| 10 | +import net.bytebuddy.matcher.ElementMatcher; |
| 11 | +import org.junit.jupiter.params.ParameterizedTest; |
| 12 | +import org.junit.jupiter.params.provider.ValueSource; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +@SuppressWarnings({"unchecked", "rawtypes"}) |
| 16 | +class LoggingFailSafeMatcherTest { |
| 17 | + |
| 18 | + @ParameterizedTest |
| 19 | + @ValueSource(booleans = {true, false}) |
| 20 | + void testMatcher(boolean match) { |
| 21 | + ElementMatcher mockMatcher = mock(ElementMatcher.class); |
| 22 | + when(mockMatcher.matches(any())).thenReturn(match); |
| 23 | + |
| 24 | + LoggingFailSafeMatcher<Object> matcher = new LoggingFailSafeMatcher<>(mockMatcher, "test"); |
| 25 | + |
| 26 | + boolean result = matcher.matches(new Object()); |
| 27 | + |
| 28 | + assertThat(result).isEqualTo(match); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + void testMatcherException() { |
| 33 | + ElementMatcher mockMatcher = mock(ElementMatcher.class); |
| 34 | + when(mockMatcher.matches(any())).thenThrow(new RuntimeException("matcher exception")); |
| 35 | + |
| 36 | + LoggingFailSafeMatcher<Object> matcher = new LoggingFailSafeMatcher<>(mockMatcher, "test"); |
| 37 | + |
| 38 | + assertThatCode(() -> matcher.matches(new Object())).doesNotThrowAnyException(); |
| 39 | + assertThat(matcher.matches(new Object())).isFalse(); |
| 40 | + } |
| 41 | +} |
0 commit comments