From ee07cb0c5f5416f9a71214c4ce9418f401a6e76c Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Thu, 9 Jan 2025 15:20:49 +0200 Subject: [PATCH] Test early and latest version in same test (#8) --- .../library-autoconfigure/build.gradle.kts | 18 +------- .../v2_7/DubboHeadersGetterTest.java | 21 +++++++-- .../v2_7/DubboHeadersGetterTest.java | 44 ------------------- 3 files changed, 18 insertions(+), 65 deletions(-) delete mode 100644 instrumentation/apache-dubbo-2.7/library-autoconfigure/src/testLatestDepDubbo/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/build.gradle.kts b/instrumentation/apache-dubbo-2.7/library-autoconfigure/build.gradle.kts index 52c5e5cb08ac..0fba704c6ef4 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/build.gradle.kts +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/build.gradle.kts @@ -13,27 +13,11 @@ dependencies { testLibrary("org.apache.dubbo:dubbo-config-api:2.7.0") } -testing { - suites { - val testLatestDepDubbo by registering(JvmTestSuite::class) { - dependencies { - implementation(project(":instrumentation:apache-dubbo-2.7:library-autoconfigure")) - implementation("org.apache.dubbo:dubbo:+") - } - } - } -} - tasks.withType().configureEach { + systemProperty("testLatestDeps", findProperty("testLatestDeps") as Boolean) jvmArgs("-XX:+IgnoreUnrecognizedVMOptions") // to suppress non-fatal errors on jdk17 jvmArgs("--add-opens=java.base/java.math=ALL-UNNAMED") // required on jdk17 jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED") } - -if (findProperty("testLatestDeps") as Boolean) { - tasks.check { - dependsOn(testing.suites) - } -} diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java index 3c92605f2dd1..627e5e5d5403 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/test/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java @@ -9,7 +9,9 @@ import static org.mockito.Mockito.when; import java.net.InetSocketAddress; +import java.util.Collections; import java.util.Iterator; +import java.util.Map; import org.apache.dubbo.common.URL; import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.rpc.RpcInvocation; @@ -22,21 +24,32 @@ class DubboHeadersGetterTest { @Mock RpcContext context; + @Mock RpcInvocation rpcInvocation; @Test @SuppressWarnings("deprecation") // deprecation for RpcInvocation() - void testKeys() { + void testKeys() throws Exception { when(context.getUrl()).thenReturn(new URL("http", "localhost", 1)); when(context.getRemoteAddress()).thenReturn(new InetSocketAddress(1)); when(context.getLocalAddress()).thenReturn(new InetSocketAddress(1)); - RpcInvocation invocation = new RpcInvocation(); - invocation.setAttachment("key", "value"); - DubboRequest request = DubboRequest.create(invocation, context); + // for latest dep tests call getObjectAttachments, otherwise call getAttachments + if (Boolean.getBoolean("testLatestDeps")) { + when(getObjectAttachments()).thenReturn(Collections.singletonMap("key", "value")); + } else { + when(rpcInvocation.getAttachments()).thenReturn(Collections.singletonMap("key", "value")); + } + DubboRequest request = DubboRequest.create(rpcInvocation, context); Iterator iterator = DubboHeadersGetter.INSTANCE.keys(request).iterator(); assertThat(iterator.hasNext()).isTrue(); assertThat(iterator.next()).isEqualTo("key"); assertThat(iterator.hasNext()).isFalse(); } + + @SuppressWarnings("unchecked") + private Map getObjectAttachments() throws Exception { + return (Map) + RpcInvocation.class.getMethod("getObjectAttachments").invoke(rpcInvocation); + } } diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/testLatestDepDubbo/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/testLatestDepDubbo/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java deleted file mode 100644 index 0848a9db6844..000000000000 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/testLatestDepDubbo/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboHeadersGetterTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.apachedubbo.v2_7; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.when; - -import java.net.InetSocketAddress; -import java.util.Collections; -import java.util.Iterator; -import org.apache.dubbo.common.URL; -import org.apache.dubbo.rpc.RpcContext; -import org.apache.dubbo.rpc.RpcInvocation; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -@ExtendWith(MockitoExtension.class) -class DubboHeadersGetterTest { - - @Mock RpcContext context; - - @Mock RpcInvocation rpcInvocation; - - @Test - @SuppressWarnings("deprecation") // deprecation for RpcInvocation() - void testKeys() { - when(context.getUrl()).thenReturn(new URL("http", "localhost", 1)); - when(context.getRemoteAddress()).thenReturn(new InetSocketAddress(1)); - when(context.getLocalAddress()).thenReturn(new InetSocketAddress(1)); - - when(rpcInvocation.getObjectAttachments()).thenReturn(Collections.singletonMap("key", "value")); - DubboRequest request = DubboRequest.create(rpcInvocation, context); - - Iterator iterator = DubboHeadersGetter.INSTANCE.keys(request).iterator(); - assertThat(iterator.hasNext()).isTrue(); - assertThat(iterator.next()).isEqualTo("key"); - assertThat(iterator.hasNext()).isFalse(); - } -}