Skip to content

Commit e76195b

Browse files
committed
convert to java
1 parent fa5ec0c commit e76195b

File tree

3 files changed

+73
-69
lines changed

3 files changed

+73
-69
lines changed

javaagent-tooling/src/test/groovy/io/opentelemetry/javaagent/tooling/bytebuddy/matcher/ExtendsClassMatcherTest.groovy

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
}

javaagent-tooling/src/test/java/io/opentelemetry/javaagent/tooling/bytebuddy/matcher/SafeHasSuperTypeMatcherTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ void testMatcher(String matcherClassName, String typeClassName, boolean expected
5656
}
5757

5858
@Test
59-
void testExceptionGettingInterfaces() throws Exception {
59+
void testExceptionGettingInterfaces() {
6060
TypeDescription type = mock(TypeDescription.class);
6161
TypeDescription.Generic typeGeneric = mock(TypeDescription.Generic.class);
6262

6363
when(type.asGenericType()).thenReturn(typeGeneric);
64-
when(typeGeneric.asErasure()).thenThrow(new Exception("asErasure exception"));
65-
when(type.getInterfaces()).thenThrow(new Exception("getInterfaces exception"));
66-
when(type.getSuperClass()).thenThrow(new Exception("getSuperClass exception"));
64+
when(typeGeneric.asErasure()).thenThrow(new RuntimeException("asErasure exception"));
65+
when(type.getInterfaces()).thenThrow(new RuntimeException("getInterfaces exception"));
66+
when(type.getSuperClass()).thenThrow(new RuntimeException("getSuperClass exception"));
6767

6868
boolean result = hasSuperType(named(Object.class.getName())).matches(type);
6969

@@ -73,7 +73,7 @@ void testExceptionGettingInterfaces() throws Exception {
7373
}
7474

7575
@Test
76-
void testTraversalExceptions() throws Exception {
76+
void testTraversalExceptions() {
7777
TypeDescription type = mock(TypeDescription.class);
7878
TypeDescription.Generic typeGeneric = mock(TypeDescription.Generic.class);
7979
TypeList.Generic interfaces = mock(TypeList.Generic.class);
@@ -82,7 +82,7 @@ void testTraversalExceptions() throws Exception {
8282
when(type.getInterfaces()).thenReturn(interfaces);
8383
when(interfaces.iterator()).thenReturn(iterator);
8484
when(type.asGenericType()).thenReturn(typeGeneric);
85-
when(typeGeneric.asErasure()).thenThrow(new Exception("asErasure exception"));
85+
when(typeGeneric.asErasure()).thenThrow(new RuntimeException("asErasure exception"));
8686

8787
boolean result = hasSuperType(named(Object.class.getName())).matches(type);
8888

0 commit comments

Comments
 (0)