|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.tooling.bytebuddy.matcher; |
| 7 | + |
| 8 | +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.extendsClass; |
| 9 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 10 | +import static org.assertj.core.api.Assertions.assertThat; |
| 11 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 12 | +import static org.mockito.Mockito.mock; |
| 13 | +import static org.mockito.Mockito.when; |
| 14 | + |
| 15 | +import io.opentelemetry.javaagent.tooling.muzzle.AgentTooling; |
| 16 | +import net.bytebuddy.description.type.TypeDescription; |
| 17 | +import net.bytebuddy.pool.TypePool; |
| 18 | +import org.junit.jupiter.api.BeforeAll; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.params.ParameterizedTest; |
| 21 | +import org.junit.jupiter.params.provider.CsvSource; |
| 22 | +import org.objectweb.asm.Opcodes; |
| 23 | + |
| 24 | +class ExtendsClassMatcherTest { |
| 25 | + |
| 26 | + private static TypePool typePool; |
| 27 | + |
| 28 | + @BeforeAll |
| 29 | + static void setUp() { |
| 30 | + typePool = AgentTooling.poolStrategy() |
| 31 | + .typePool(AgentTooling.locationStrategy().classFileLocator(ExtendsClassMatcherTest.class.getClassLoader(), null), |
| 32 | + ExtendsClassMatcherTest.class.getClassLoader()); |
| 33 | + } |
| 34 | + |
| 35 | + @ParameterizedTest |
| 36 | + @CsvSource({ |
| 37 | + "io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.A, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.B, false", |
| 38 | + "io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.A, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.F, false", |
| 39 | + "io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.G, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.F, false", |
| 40 | + "io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.F, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.F, true", |
| 41 | + "io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.F, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.G, true" |
| 42 | + }) |
| 43 | + void testMatcher(String matcherClassName, String typeClassName, boolean expectedResult) { |
| 44 | + TypeDescription argument = typePool.describe(typeClassName).resolve(); |
| 45 | + |
| 46 | + boolean result = extendsClass(named(matcherClassName)).matches(argument); |
| 47 | + |
| 48 | + assertThat(result).isEqualTo(expectedResult); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void testTraversalExceptions() { |
| 53 | + TypeDescription type = mock(TypeDescription.class); |
| 54 | + TypeDescription.Generic typeGeneric = mock(TypeDescription.Generic.class); |
| 55 | + |
| 56 | + when(type.getModifiers()).thenReturn(Opcodes.ACC_ABSTRACT); |
| 57 | + when(type.asGenericType()).thenReturn(typeGeneric); |
| 58 | + when(typeGeneric.asErasure()).thenThrow(new RuntimeException("asErasure exception")); |
| 59 | + when(type.getSuperClass()).thenThrow(new RuntimeException("getSuperClass exception")); |
| 60 | + |
| 61 | + boolean result = extendsClass(named(Object.class.getName())).matches(type); |
| 62 | + |
| 63 | + assertThat(result).isFalse(); |
| 64 | + assertThatCode(() -> extendsClass(named(Object.class.getName())).matches(type)) |
| 65 | + .doesNotThrowAnyException(); |
| 66 | + } |
| 67 | +} |
0 commit comments