diff --git a/instrumentation/apache-httpclient/apache-httpclient-2.0/javaagent/src/test/groovy/AbstractCommonsHttpClientTest.groovy b/instrumentation/apache-httpclient/apache-httpclient-2.0/javaagent/src/test/groovy/AbstractCommonsHttpClientTest.groovy deleted file mode 100644 index 41452ae2c9..0000000000 --- a/instrumentation/apache-httpclient/apache-httpclient-2.0/javaagent/src/test/groovy/AbstractCommonsHttpClientTest.groovy +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -import io.opentelemetry.instrumentation.test.AgentTestTrait -import io.opentelemetry.instrumentation.test.base.HttpClientTest -import org.apache.commons.httpclient.HttpClient -import org.apache.commons.httpclient.HttpConnectionManager -import org.apache.commons.httpclient.HttpMethod -import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager -import org.apache.commons.httpclient.methods.DeleteMethod -import org.apache.commons.httpclient.methods.GetMethod -import org.apache.commons.httpclient.methods.HeadMethod -import org.apache.commons.httpclient.methods.OptionsMethod -import org.apache.commons.httpclient.methods.PostMethod -import org.apache.commons.httpclient.methods.PutMethod -import org.apache.commons.httpclient.methods.TraceMethod -import spock.lang.Shared - -abstract class AbstractCommonsHttpClientTest extends HttpClientTest implements AgentTestTrait { - @Shared - HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager() - @Shared - HttpClient client = buildClient(false) - @Shared - HttpClient clientWithReadTimeout = buildClient(true) - - def buildClient(boolean readTimeout) { - HttpClient client = new HttpClient(connectionManager) - client.setConnectionTimeout(CONNECT_TIMEOUT_MS) - if (readTimeout) { - client.setTimeout(READ_TIMEOUT_MS) - } - return client - } - - HttpClient getClient(URI uri) { - if (uri.toString().contains("/read-timeout")) { - return clientWithReadTimeout - } - return client - } - - @Override - HttpMethod buildRequest(String method, URI uri, Map headers) { - def request - switch (method) { - case "GET": - request = new GetMethod(uri.toString()) - break - case "PUT": - request = new PutMethod(uri.toString()) - break - case "POST": - request = new PostMethod(uri.toString()) - break - case "HEAD": - request = new HeadMethod(uri.toString()) - break - case "DELETE": - request = new DeleteMethod(uri.toString()) - break - case "OPTIONS": - request = new OptionsMethod(uri.toString()) - break - case "TRACE": - request = new TraceMethod(uri.toString()) - break - default: - throw new IllegalStateException("Unsupported method: " + method) - } - headers.each { request.setRequestHeader(it.key, it.value) } - return request - } - - @Override - boolean testCircularRedirects() { - false - } - - @Override - boolean testReusedRequest() { - // apache commons throws an exception if the request is reused without being recycled first - // at which point this test is not useful (and requires re-populating uri) - false - } - - @Override - boolean testCallback() { - false - } - - @Override - boolean testNonStandardHttpMethod() { - false - } -} diff --git a/instrumentation/apache-httpclient/apache-httpclient-2.0/javaagent/src/test/groovy/CommonsHttpClientLatestDepsTest.groovy b/instrumentation/apache-httpclient/apache-httpclient-2.0/javaagent/src/test/groovy/CommonsHttpClientLatestDepsTest.groovy deleted file mode 100644 index 0a68757a00..0000000000 --- a/instrumentation/apache-httpclient/apache-httpclient-2.0/javaagent/src/test/groovy/CommonsHttpClientLatestDepsTest.groovy +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -import org.apache.commons.httpclient.HttpMethod -import spock.lang.IgnoreIf - -//this test will be ignored if not executed with -PtestLatestDeps=true -//because the latest dependency commons-httpclient v3.1 allows a call to the executeMethod -//with some null parameters like HttpClient.executeMethod(null, request, null) -//but this construct is not allowed in commons-httpclient v2 that is used for regular otel testing -@IgnoreIf({ !Boolean.getBoolean("testLatestDeps") }) -class CommonsHttpClientLatestDepsTest extends AbstractCommonsHttpClientTest { - - @Override - int sendRequest(HttpMethod request, String method, URI uri, Map headers) { - try { - getClient(uri).executeMethod(null, request, null) - return request.getStatusCode() - } finally { - request.releaseConnection() - } - } -} diff --git a/instrumentation/apache-httpclient/apache-httpclient-2.0/javaagent/src/test/groovy/CommonsHttpClientTest.groovy b/instrumentation/apache-httpclient/apache-httpclient-2.0/javaagent/src/test/groovy/CommonsHttpClientTest.groovy deleted file mode 100644 index 7502f15c16..0000000000 --- a/instrumentation/apache-httpclient/apache-httpclient-2.0/javaagent/src/test/groovy/CommonsHttpClientTest.groovy +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -import org.apache.commons.httpclient.HttpMethod - -class CommonsHttpClientTest extends AbstractCommonsHttpClientTest { - - @Override - int sendRequest(HttpMethod request, String method, URI uri, Map headers) { - try { - getClient(uri).executeMethod(request) - return request.getStatusCode() - } finally { - request.releaseConnection() - } - } -} diff --git a/instrumentation/apache-httpclient/apache-httpclient-2.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apachehttpclient/v2_0/AbstractCommonsHttpClientTest.java b/instrumentation/apache-httpclient/apache-httpclient-2.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apachehttpclient/v2_0/AbstractCommonsHttpClientTest.java new file mode 100644 index 0000000000..3575b63f4d --- /dev/null +++ b/instrumentation/apache-httpclient/apache-httpclient-2.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apachehttpclient/v2_0/AbstractCommonsHttpClientTest.java @@ -0,0 +1,96 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.apachehttpclient.v2_0; + +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest; +import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions; +import java.net.URI; +import java.util.Map; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpConnectionManager; +import org.apache.commons.httpclient.HttpMethod; +import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; +import org.apache.commons.httpclient.methods.DeleteMethod; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.methods.HeadMethod; +import org.apache.commons.httpclient.methods.OptionsMethod; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.PutMethod; +import org.apache.commons.httpclient.methods.TraceMethod; +import org.junit.jupiter.api.extension.RegisterExtension; + +abstract class AbstractCommonsHttpClientTest extends AbstractHttpClientTest { + + @RegisterExtension + static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forAgent(); + + private static final HttpConnectionManager connectionManager = + new MultiThreadedHttpConnectionManager(); + private static final HttpClient client = buildClient(false); + private static final HttpClient clientWithReadTimeout = buildClient(true); + + static HttpClient buildClient(boolean readTimeout) { + HttpClient client = new HttpClient(connectionManager); + client.setConnectionTimeout((int) CONNECTION_TIMEOUT.toMillis()); + if (readTimeout) { + client.setTimeout((int) READ_TIMEOUT.toMillis()); + } + return client; + } + + HttpClient getClient(URI uri) { + if (uri.toString().contains("/read-timeout")) { + return clientWithReadTimeout; + } + return client; + } + + @Override + public HttpMethod buildRequest(String method, URI uri, Map headers) { + HttpMethod request; + switch (method) { + case "GET": + request = new GetMethod(uri.toString()); + break; + case "PUT": + request = new PutMethod(uri.toString()); + break; + case "POST": + request = new PostMethod(uri.toString()); + break; + case "HEAD": + request = new HeadMethod(uri.toString()); + break; + case "DELETE": + request = new DeleteMethod(uri.toString()); + break; + case "OPTIONS": + request = new OptionsMethod(uri.toString()); + break; + case "TRACE": + request = new TraceMethod(uri.toString()); + break; + default: + throw new IllegalStateException("Unsupported method: " + method); + } + + for (Map.Entry entry : headers.entrySet()) { + request.setRequestHeader(entry.getKey(), entry.getValue()); + } + return request; + } + + @Override + protected void configure(HttpClientTestOptions.Builder optionsBuilder) { + optionsBuilder + .disableTestCallback() + .disableTestReusedRequest() + .disableTestNonStandardHttpMethod() + .disableTestCircularRedirects(); + } +} diff --git a/instrumentation/apache-httpclient/apache-httpclient-2.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apachehttpclient/v2_0/CommonsHttpClientLatestDepsTest.java b/instrumentation/apache-httpclient/apache-httpclient-2.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apachehttpclient/v2_0/CommonsHttpClientLatestDepsTest.java new file mode 100644 index 0000000000..a482ddeff8 --- /dev/null +++ b/instrumentation/apache-httpclient/apache-httpclient-2.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apachehttpclient/v2_0/CommonsHttpClientLatestDepsTest.java @@ -0,0 +1,29 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.apachehttpclient.v2_0; + +import java.net.URI; +import java.util.Map; +import org.apache.commons.httpclient.HttpMethod; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; + +// this test will be ignored if not executed with -PtestLatestDeps=true +// because the latest dependency commons-httpclient v3.1 allows a call to the executeMethod +// with some null parameters like HttpClient.executeMethod(null, request, null) +// but this construct is not allowed in commons-httpclient v2 that is used for regular otel testing +@EnabledIfSystemProperty(named = "testLatestDeps", matches = "true") +public class CommonsHttpClientLatestDepsTest extends AbstractCommonsHttpClientTest { + @Override + public int sendRequest(HttpMethod request, String method, URI uri, Map headers) + throws Exception { + try { + getClient(uri).executeMethod(null, request, null); + return request.getStatusCode(); + } finally { + request.releaseConnection(); + } + } +} diff --git a/instrumentation/apache-httpclient/apache-httpclient-2.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apachehttpclient/v2_0/CommonsHttpClientTest.java b/instrumentation/apache-httpclient/apache-httpclient-2.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apachehttpclient/v2_0/CommonsHttpClientTest.java new file mode 100644 index 0000000000..ddcbe7d3af --- /dev/null +++ b/instrumentation/apache-httpclient/apache-httpclient-2.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apachehttpclient/v2_0/CommonsHttpClientTest.java @@ -0,0 +1,24 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.apachehttpclient.v2_0; + +import java.net.URI; +import java.util.Map; +import org.apache.commons.httpclient.HttpMethod; + +class CommonsHttpClientTest extends AbstractCommonsHttpClientTest { + + @Override + public int sendRequest(HttpMethod request, String method, URI uri, Map headers) + throws Exception { + try { + getClient(uri).executeMethod(request); + return request.getStatusCode(); + } finally { + request.releaseConnection(); + } + } +}