From df9158ca1b10bda9c680317cefed3e65e64a2b90 Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Wed, 8 Jan 2025 14:36:27 +0200 Subject: [PATCH] Add test for duplicate helper class --- .../src/main/java/helper/DuplicateHelper.java | 9 ++++++ .../DuplicateHelperInstrumentation.java | 29 +++++++++++++++++ .../DuplicateHelperInstrumentationModule.java | 32 +++++++++++++++++++ .../test/java/helper/DuplicateHelperTest.java | 14 ++++++++ .../java/helper/DuplicateHelperTestClass.java | 11 +++++++ 5 files changed, 95 insertions(+) create mode 100644 testing-common/integration-tests/src/main/java/helper/DuplicateHelper.java create mode 100644 testing-common/integration-tests/src/main/java/helper/DuplicateHelperInstrumentation.java create mode 100644 testing-common/integration-tests/src/main/java/helper/DuplicateHelperInstrumentationModule.java create mode 100644 testing-common/integration-tests/src/test/java/helper/DuplicateHelperTest.java create mode 100644 testing-common/integration-tests/src/test/java/helper/DuplicateHelperTestClass.java diff --git a/testing-common/integration-tests/src/main/java/helper/DuplicateHelper.java b/testing-common/integration-tests/src/main/java/helper/DuplicateHelper.java new file mode 100644 index 000000000000..920d5b309ced --- /dev/null +++ b/testing-common/integration-tests/src/main/java/helper/DuplicateHelper.java @@ -0,0 +1,9 @@ +package helper; + +public class DuplicateHelper { + public static String addSuffix(String string, String suffix) { + return string + suffix; + } + + private DuplicateHelper() {} +} diff --git a/testing-common/integration-tests/src/main/java/helper/DuplicateHelperInstrumentation.java b/testing-common/integration-tests/src/main/java/helper/DuplicateHelperInstrumentation.java new file mode 100644 index 000000000000..478b0399ce02 --- /dev/null +++ b/testing-common/integration-tests/src/main/java/helper/DuplicateHelperInstrumentation.java @@ -0,0 +1,29 @@ +package helper; + +import static net.bytebuddy.matcher.ElementMatchers.named; + +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; + +public class DuplicateHelperInstrumentation implements TypeInstrumentation { + @Override + public ElementMatcher typeMatcher() { + return named("helper.DuplicateHelperTestClass"); + } + + @Override + public void transform(TypeTransformer transformer) { + transformer.applyAdviceToMethod(named("transform"), this.getClass().getName() + "$TestAdvice"); + } + + @SuppressWarnings("unused") + public static class TestAdvice { + @Advice.OnMethodExit(suppress = Throwable.class) + static void addSuffix(@Advice.Return(readOnly = false) String string) { + string = DuplicateHelper.addSuffix(string, " foo"); + } + } +} diff --git a/testing-common/integration-tests/src/main/java/helper/DuplicateHelperInstrumentationModule.java b/testing-common/integration-tests/src/main/java/helper/DuplicateHelperInstrumentationModule.java new file mode 100644 index 000000000000..898acb496068 --- /dev/null +++ b/testing-common/integration-tests/src/main/java/helper/DuplicateHelperInstrumentationModule.java @@ -0,0 +1,32 @@ +package helper; + +import static java.util.Collections.singletonList; + +import com.google.auto.service.AutoService; +import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import java.util.Arrays; +import java.util.List; + +@AutoService(InstrumentationModule.class) +public class DuplicateHelperInstrumentationModule extends InstrumentationModule { + public DuplicateHelperInstrumentationModule() { + super("duplicate-helper"); + } + + @Override + public List getAdditionalHelperClassNames() { + // muzzle adds the same class as helper, listing it twice to ensure it doesn't fail + return Arrays.asList("helper.DuplicateHelper", "helper.DuplicateHelper"); + } + + @Override + public boolean isHelperClass(String className) { + return "helper.DuplicateHelper".equals(className); + } + + @Override + public List typeInstrumentations() { + return singletonList(new DuplicateHelperInstrumentation()); + } +} diff --git a/testing-common/integration-tests/src/test/java/helper/DuplicateHelperTest.java b/testing-common/integration-tests/src/test/java/helper/DuplicateHelperTest.java new file mode 100644 index 000000000000..4b5dd877f0cb --- /dev/null +++ b/testing-common/integration-tests/src/test/java/helper/DuplicateHelperTest.java @@ -0,0 +1,14 @@ +package helper; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +class DuplicateHelperTest { + + @Test + void duplicateHelper() { + String string = DuplicateHelperTestClass.transform("test"); + assertThat(string).isEqualTo("test foo"); + } +} diff --git a/testing-common/integration-tests/src/test/java/helper/DuplicateHelperTestClass.java b/testing-common/integration-tests/src/test/java/helper/DuplicateHelperTestClass.java new file mode 100644 index 000000000000..447029a4b7a9 --- /dev/null +++ b/testing-common/integration-tests/src/test/java/helper/DuplicateHelperTestClass.java @@ -0,0 +1,11 @@ +package helper; + +class DuplicateHelperTestClass { + + // this method is transformed by instrumentation + public static String transform(String string) { + return string; + } + + private DuplicateHelperTestClass() {} +}