Skip to content

Commit fa5ec0c

Browse files
committed
convert to java
1 parent 3a5a025 commit fa5ec0c

File tree

2 files changed

+93
-88
lines changed

2 files changed

+93
-88
lines changed

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

Lines changed: 0 additions & 88 deletions
This file was deleted.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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.hasSuperType;
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.description.type.TypeList;
18+
import net.bytebuddy.pool.TypePool;
19+
import org.junit.jupiter.api.BeforeAll;
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.CsvSource;
23+
24+
import java.util.Iterator;
25+
26+
@SuppressWarnings({"unchecked", "rawtypes"})
27+
class SafeHasSuperTypeMatcherTest {
28+
29+
private static TypePool typePool;
30+
31+
@BeforeAll
32+
static void setUp() {
33+
typePool = AgentTooling.poolStrategy()
34+
.typePool(AgentTooling.locationStrategy().classFileLocator(SafeHasSuperTypeMatcherTest.class.getClassLoader(), null),
35+
SafeHasSuperTypeMatcherTest.class.getClassLoader());
36+
}
37+
38+
@ParameterizedTest
39+
@CsvSource({
40+
"io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.A, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.A, true",
41+
"io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.A, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.B, true",
42+
"io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.B, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.A, false",
43+
"io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.A, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.E, true",
44+
"io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.A, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.F, true",
45+
"io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.B, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.G, true",
46+
"io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.F, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.A, false",
47+
"io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.F, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.F, true",
48+
"io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.F, io.opentelemetry.javaagent.tooling.bytebuddy.matcher.testclasses.G, true"
49+
})
50+
void testMatcher(String matcherClassName, String typeClassName, boolean expectedResult) {
51+
TypeDescription argument = typePool.describe(typeClassName).resolve();
52+
53+
boolean result = hasSuperType(named(matcherClassName)).matches(argument);
54+
55+
assertThat(result).isEqualTo(expectedResult);
56+
}
57+
58+
@Test
59+
void testExceptionGettingInterfaces() throws Exception {
60+
TypeDescription type = mock(TypeDescription.class);
61+
TypeDescription.Generic typeGeneric = mock(TypeDescription.Generic.class);
62+
63+
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"));
67+
68+
boolean result = hasSuperType(named(Object.class.getName())).matches(type);
69+
70+
assertThat(result).isFalse();
71+
assertThatCode(() -> hasSuperType(named(Object.class.getName())).matches(type))
72+
.doesNotThrowAnyException();
73+
}
74+
75+
@Test
76+
void testTraversalExceptions() throws Exception {
77+
TypeDescription type = mock(TypeDescription.class);
78+
TypeDescription.Generic typeGeneric = mock(TypeDescription.Generic.class);
79+
TypeList.Generic interfaces = mock(TypeList.Generic.class);
80+
Iterator iterator = new ThrowOnFirstElement();
81+
82+
when(type.getInterfaces()).thenReturn(interfaces);
83+
when(interfaces.iterator()).thenReturn(iterator);
84+
when(type.asGenericType()).thenReturn(typeGeneric);
85+
when(typeGeneric.asErasure()).thenThrow(new Exception("asErasure exception"));
86+
87+
boolean result = hasSuperType(named(Object.class.getName())).matches(type);
88+
89+
assertThat(result).isFalse();
90+
assertThatCode(() -> hasSuperType(named(Object.class.getName())).matches(type))
91+
.doesNotThrowAnyException();
92+
}
93+
}

0 commit comments

Comments
 (0)