Skip to content

Commit fddb1ed

Browse files
authored
Convert HelperReferenceWrapperTest to groovy (#14903)
1 parent a8b77e1 commit fddb1ed

File tree

3 files changed

+155
-154
lines changed

3 files changed

+155
-154
lines changed

muzzle/src/test/groovy/io/opentelemetry/javaagent/tooling/muzzle/HelperReferenceWrapperTest.groovy

Lines changed: 0 additions & 152 deletions
This file was deleted.
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.tooling.muzzle;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
import io.opentelemetry.javaagent.tooling.muzzle.references.ClassRef;
11+
import io.opentelemetry.javaagent.tooling.muzzle.references.Flag;
12+
import io.opentelemetry.javaagent.tooling.muzzle.references.Flag.ManifestationFlag;
13+
import io.opentelemetry.javaagent.tooling.muzzle.references.Source;
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
import muzzle.HelperReferenceWrapperTestClasses;
17+
import net.bytebuddy.pool.TypePool;
18+
import org.junit.jupiter.api.BeforeAll;
19+
import org.junit.jupiter.api.Test;
20+
import org.objectweb.asm.Type;
21+
22+
class HelperReferenceWrapperTest {
23+
24+
private static ClassRef baseHelperClass;
25+
private static ClassRef helperClass;
26+
27+
@BeforeAll
28+
static void setup() {
29+
baseHelperClass =
30+
ClassRef.builder(HelperReferenceWrapperTest.class.getName() + "$BaseHelper")
31+
.setSuperClassName(
32+
HelperReferenceWrapperTestClasses.AbstractClasspathType.class.getName())
33+
.addFlag(ManifestationFlag.ABSTRACT)
34+
.addMethod(new Source[0], new Flag[0], "foo", Type.VOID_TYPE)
35+
.addMethod(
36+
new Source[0], new Flag[] {ManifestationFlag.ABSTRACT}, "abstract", Type.INT_TYPE)
37+
.build();
38+
39+
helperClass =
40+
ClassRef.builder(HelperReferenceWrapperTest.class.getName() + "$Helper")
41+
.setSuperClassName(baseHelperClass.getClassName())
42+
.addInterfaceName(HelperReferenceWrapperTestClasses.Interface2.class.getName())
43+
.addMethod(new Source[0], new Flag[0], "bar", Type.VOID_TYPE)
44+
.addField(
45+
new Source[0], new Flag[0], "field", Type.getType("Ljava/lang/Object;"), false)
46+
.addField(
47+
new Source[0],
48+
new Flag[0],
49+
"declaredField",
50+
Type.getType("Ljava/lang/Object;"),
51+
true)
52+
.addField(
53+
new Source[0],
54+
new Flag[] {Flag.VisibilityFlag.PRIVATE},
55+
"privateFieldsAreSkipped",
56+
Type.getType("Ljava/lang/Object;"),
57+
true)
58+
.build();
59+
}
60+
61+
@Test
62+
void shouldWrapHelperTypes() {
63+
TypePool typePool = TypePool.Default.of(HelperReferenceWrapperTest.class.getClassLoader());
64+
Map<String, ClassRef> references = new HashMap<>();
65+
references.put(helperClass.getClassName(), helperClass);
66+
references.put(baseHelperClass.getClassName(), baseHelperClass);
67+
68+
HelperClassPredicate helperClassPredicate = new HelperClassPredicate(cls -> false);
69+
HelperReferenceWrapper helperWrapper =
70+
new HelperReferenceWrapper.Factory(typePool, references, helperClassPredicate)
71+
.create(helperClass);
72+
73+
assertThat(helperWrapper.isAbstract()).isFalse();
74+
75+
assertThat(helperWrapper.getMethods())
76+
.satisfiesExactly(
77+
method -> {
78+
assertThat(method.isAbstract()).isFalse();
79+
assertThat(method.getName()).isEqualTo("bar");
80+
assertThat(method.getDescriptor()).isEqualTo("()V");
81+
});
82+
83+
assertThat(helperWrapper.getFields())
84+
.satisfiesExactly(
85+
field -> {
86+
assertThat(field.getName()).isEqualTo("declaredField");
87+
assertThat(field.getDescriptor()).isEqualTo("Ljava/lang/Object;");
88+
});
89+
90+
assertThat(helperWrapper.hasSuperTypes()).isTrue();
91+
assertThat(helperWrapper.getSuperTypes())
92+
.satisfiesExactly(
93+
baseHelper -> {
94+
assertThat(baseHelper.isAbstract()).isTrue();
95+
assertThat(baseHelper.getMethods())
96+
.satisfiesExactly(
97+
method -> {
98+
assertThat(method.isAbstract()).isFalse();
99+
assertThat(method.getName()).isEqualTo("foo");
100+
assertThat(method.getDescriptor()).isEqualTo("()V");
101+
},
102+
method -> {
103+
assertThat(method.isAbstract()).isTrue();
104+
assertThat(method.getName()).isEqualTo("abstract");
105+
assertThat(method.getDescriptor()).isEqualTo("()I");
106+
});
107+
108+
assertThat(baseHelper.hasSuperTypes()).isTrue();
109+
assertThat(baseHelper.getSuperTypes())
110+
.satisfiesExactly(
111+
helperReferenceWrapper -> {
112+
assertThat(helperReferenceWrapper.isAbstract()).isTrue();
113+
assertThat(helperReferenceWrapper.getMethods()).isEmpty();
114+
115+
assertThat(helperReferenceWrapper.getFields())
116+
.satisfiesExactly(
117+
field -> {
118+
assertThat(field.getName()).isEqualTo("field");
119+
assertThat(field.getDescriptor()).isEqualTo("Ljava/lang/Object;");
120+
});
121+
122+
assertThat(helperReferenceWrapper.hasSuperTypes()).isTrue();
123+
assertThat(helperReferenceWrapper.getSuperTypes())
124+
.satisfiesExactly(
125+
wrapper -> assertThat(wrapper.hasSuperTypes()).isFalse(),
126+
wrapper -> {
127+
assertThat(wrapper.isAbstract()).isTrue();
128+
assertThat(wrapper.getMethods())
129+
.satisfiesExactly(
130+
method -> {
131+
assertThat(method.isAbstract()).isTrue();
132+
assertThat(method.getName()).isEqualTo("foo");
133+
assertThat(method.getDescriptor()).isEqualTo("()V");
134+
});
135+
assertThat(wrapper.hasSuperTypes()).isFalse();
136+
assertThat(wrapper.getSuperTypes()).isEmpty();
137+
});
138+
});
139+
},
140+
wrapper -> {
141+
assertThat(wrapper.isAbstract()).isTrue();
142+
assertThat(wrapper.getMethods())
143+
.satisfiesExactly(
144+
method -> {
145+
assertThat(method.isAbstract()).isTrue();
146+
assertThat(method.getName()).isEqualTo("bar");
147+
assertThat(method.getDescriptor()).isEqualTo("()V");
148+
});
149+
assertThat(wrapper.hasSuperTypes()).isFalse();
150+
assertThat(wrapper.getSuperTypes()).isEmpty();
151+
});
152+
}
153+
}

muzzle/src/test/java/muzzle/HelperReferenceWrapperTestClasses.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ interface Interface1 {
1212
void foo();
1313
}
1414

15-
interface Interface2 {
15+
public interface Interface2 {
1616
void bar();
1717
}
1818

19-
abstract static class AbstractClasspathType implements Interface1 {
19+
public abstract static class AbstractClasspathType implements Interface1 {
2020
private Object privateFieldsAreIgnored;
2121
protected Object field;
2222

0 commit comments

Comments
 (0)