|
| 1 | +package io.opentelemetry.javaagent.tooling.bytebuddy.matcher; |
| 2 | + |
| 3 | +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface; |
| 4 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 5 | +import static org.assertj.core.api.Assertions.assertThat; |
| 6 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 7 | +import static org.mockito.Mockito.mock; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import io.opentelemetry.javaagent.tooling.muzzle.AgentTooling; |
| 11 | +import java.util.Iterator; |
| 12 | +import net.bytebuddy.description.type.TypeDescription; |
| 13 | +import net.bytebuddy.description.type.TypeList; |
| 14 | +import net.bytebuddy.pool.TypePool; |
| 15 | +import org.junit.jupiter.api.BeforeAll; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | +import org.junit.jupiter.params.ParameterizedTest; |
| 18 | +import org.junit.jupiter.params.provider.CsvSource; |
| 19 | + |
| 20 | +class ImplementsInterfaceMatcherTest { |
| 21 | + |
| 22 | + private static TypePool typePool; |
| 23 | + |
| 24 | + @BeforeAll |
| 25 | + static void setUp() { |
| 26 | + typePool = AgentTooling.poolStrategy() |
| 27 | + .typePool(AgentTooling.locationStrategy().classFileLocator(ImplementsInterfaceMatcherTest.class.getClassLoader(), null), |
| 28 | + ImplementsInterfaceMatcherTest.class.getClassLoader()); |
| 29 | + } |
| 30 | + |
| 31 | + @ParameterizedTest |
| 32 | + @CsvSource({ |
| 33 | + "io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.A, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.A, true", |
| 34 | + "io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.A, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.B, true", |
| 35 | + "io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.B, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.A, false", |
| 36 | + "io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.A, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.E, true", |
| 37 | + "io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.A, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.F, true", |
| 38 | + "io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.A, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.G, true", |
| 39 | + "io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.F, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.A, false", |
| 40 | + "io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.F, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.F, false", |
| 41 | + "io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.F, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.G, false" |
| 42 | + }) |
| 43 | + void testMatcher(String matcherClassName, String typeClassName, boolean expectedResult) { |
| 44 | + TypeDescription argument = typePool.describe(typeClassName).resolve(); |
| 45 | + |
| 46 | + boolean result = implementsInterface(named(matcherClassName)).matches(argument); |
| 47 | + |
| 48 | + assertThat(result).isEqualTo(expectedResult); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void testExceptionGettingInterfaces() { |
| 53 | + TypeDescription type = mock(TypeDescription.class); |
| 54 | + TypeDescription.Generic typeGeneric = mock(TypeDescription.Generic.class); |
| 55 | + |
| 56 | + when(type.isInterface()).thenReturn(true); |
| 57 | + when(type.asGenericType()).thenReturn(typeGeneric); |
| 58 | + when(typeGeneric.asErasure()).thenThrow(new RuntimeException("asErasure exception")); |
| 59 | + when(type.getInterfaces()).thenThrow(new RuntimeException("getInterfaces exception")); |
| 60 | + when(type.getSuperClass()).thenThrow(new RuntimeException("getSuperClass exception")); |
| 61 | + |
| 62 | + boolean result = implementsInterface(named(Object.class.getName())).matches(type); |
| 63 | + |
| 64 | + assertThat(result).isFalse(); |
| 65 | + assertThatCode(() -> implementsInterface(named(Object.class.getName())).matches(type)) |
| 66 | + .doesNotThrowAnyException(); |
| 67 | + } |
| 68 | + |
| 69 | + @SuppressWarnings({"unchecked", "rawtypes"}) |
| 70 | + @Test |
| 71 | + void testTraversalExceptions() { |
| 72 | + TypeDescription type = mock(TypeDescription.class); |
| 73 | + TypeDescription.Generic typeGeneric = mock(TypeDescription.Generic.class); |
| 74 | + TypeList.Generic interfaces = mock(TypeList.Generic.class); |
| 75 | + Iterator iterator = new ThrowOnFirstElement(); |
| 76 | + |
| 77 | + when(type.isInterface()).thenReturn(true); |
| 78 | + when(type.asGenericType()).thenReturn(typeGeneric); |
| 79 | + when(typeGeneric.asErasure()).thenThrow(new RuntimeException("asErasure exception")); |
| 80 | + when(type.getInterfaces()).thenReturn(interfaces); |
| 81 | + when(interfaces.iterator()).thenReturn(iterator); |
| 82 | + when(type.getSuperClass()).thenThrow(new RuntimeException("getSuperClass exception")); |
| 83 | + |
| 84 | + boolean result = implementsInterface(named(Object.class.getName())).matches(type); |
| 85 | + |
| 86 | + assertThat(result).isFalse(); |
| 87 | + assertThatCode(() -> implementsInterface(named(Object.class.getName())).matches(type)) |
| 88 | + .doesNotThrowAnyException(); |
| 89 | + } |
| 90 | +} |
0 commit comments