Skip to content

Commit 8af0a42

Browse files
Improve instrumented test agent creation (#9519)
1 parent 5fec42f commit 8af0a42

File tree

3 files changed

+23
-25
lines changed

3 files changed

+23
-25
lines changed

dd-java-agent/instrumentation-testing/src/main/java/datadog/trace/agent/test/BootstrapClasspathSetupListener.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package datadog.trace.agent.test;
22

3-
import static com.google.common.base.StandardSystemProperty.JAVA_CLASS_PATH;
4-
import static com.google.common.base.StandardSystemProperty.PATH_SEPARATOR;
3+
import static java.io.File.pathSeparator;
54

6-
import com.google.common.base.Splitter;
7-
import com.google.common.collect.ImmutableList;
85
import com.google.common.reflect.ClassPath;
96
import datadog.trace.agent.test.utils.ClasspathUtils;
107
import datadog.trace.bootstrap.BootstrapProxy;
@@ -14,9 +11,11 @@
1411
import java.net.MalformedURLException;
1512
import java.net.URL;
1613
import java.net.URLClassLoader;
14+
import java.util.ArrayList;
1715
import java.util.Arrays;
1816
import java.util.HashMap;
1917
import java.util.HashSet;
18+
import java.util.List;
2019
import java.util.Map;
2120
import java.util.Set;
2221
import java.util.TreeSet;
@@ -111,26 +110,28 @@ private static ClassPath computeTestClasspath() {
111110
*/
112111
@SuppressForbidden
113112
private static ClassLoader buildJavaClassPathClassLoader() {
114-
final ImmutableList.Builder<URL> urls = ImmutableList.builder();
115-
for (final String entry : Splitter.on(PATH_SEPARATOR.value()).split(JAVA_CLASS_PATH.value())) {
113+
List<URL> urls = new ArrayList<>();
114+
String classPath = System.getProperty("java.class.path", "");
115+
for (String entry : classPath.split(pathSeparator)) {
116116
try {
117+
File pathEntry = new File(entry);
117118
try {
118-
urls.add(new File(entry).toURI().toURL());
119-
} catch (final SecurityException e) { // File.toURI checks to see if the file is a directory
120-
urls.add(new URL("file", null, new File(entry).getAbsolutePath()));
119+
urls.add(pathEntry.toURI().toURL());
120+
} catch (final SecurityException e) {
121+
urls.add(new URL("file", null, pathEntry.getAbsolutePath()));
121122
}
122123
} catch (final MalformedURLException e) {
123124
System.err.printf(
124125
"Error injecting bootstrap jar: Malformed classpath entry: %s. %s%n", entry, e);
125126
}
126127
}
127-
return new URLClassLoader(urls.build().toArray(new URL[0]), null);
128+
return new URLClassLoader(urls.toArray(new URL[0]), null);
128129
}
129130

130131
private static void setupBootstrapClasspath() {
131132
// Ensure there weren't any bootstrap classes loaded prematurely.
132133
Set<String> prematureBootstrapClasses = new TreeSet<>();
133-
for (Class clazz : ByteBuddyAgent.getInstrumentation().getAllLoadedClasses()) {
134+
for (Class<?> clazz : ByteBuddyAgent.getInstrumentation().getAllLoadedClasses()) {
134135
if (isBootstrapClass(clazz)
135136
&& clazz.getClassLoader() != null
136137
&& !clazz.getName().equals("datadog.trace.api.DisableTestTrace")

dd-java-agent/instrumentation/play-ws-1/src/test/groovy/PlayWSClientTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class PlayJavaStreamedWSClientTest extends PlayWSClientTestBase {
5454
runInAdHocThread(callback)
5555
}).toCompletableFuture().get(5, TimeUnit.SECONDS)
5656

57-
// The status can be ready before the body so explicity call wait for body to be ready
57+
// The status can be ready before the body so explicitly call wait for body to be ready
5858
wsResponse.getBodyAsSource().runFold("", { acc, out -> "" }, materializer)
5959
.toCompletableFuture().get(5, TimeUnit.SECONDS)
6060
return wsResponse.getStatus()
@@ -119,7 +119,7 @@ class PlayScalaStreamedWSClientTest extends PlayWSClientTestBase {
119119

120120
play.api.libs.ws.StandaloneWSResponse wsResponse = Await.result(futureResponse, Duration.apply(5, TimeUnit.SECONDS))
121121

122-
// The status can be ready before the body so explicity call wait for body to be ready
122+
// The status can be ready before the body so explicitly call wait for body to be ready
123123
Await.result(
124124
wsResponse.bodyAsSource().runFold("", { acc, out -> "" }, materializer),
125125
Duration.apply(5, TimeUnit.SECONDS))

dd-java-agent/testing/src/main/groovy/datadog/trace/agent/test/utils/ClasspathUtils.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,24 @@ public static byte[] convertToByteArray(final Class<?> clazz) throws IOException
4747
* @param loader classloader used to load bytes
4848
* @param resourceNames names of resources to copy into the new jar
4949
* @return the location of the newly created jar.
50-
* @throws IOException
50+
* @throws IOException if the jar file cannot be created.
5151
*/
5252
public static URL createJarWithClasses(final ClassLoader loader, final String... resourceNames)
5353
throws IOException {
54-
final File tmpJar = File.createTempFile(UUID.randomUUID().toString() + "", ".jar");
54+
final File tmpJar = File.createTempFile(UUID.randomUUID().toString(), ".jar");
5555
tmpJar.deleteOnExit();
5656

5757
final Manifest manifest = new Manifest();
58-
final JarOutputStream target = new JarOutputStream(new FileOutputStream(tmpJar), manifest);
59-
for (final String resourceName : resourceNames) {
60-
InputStream is = null;
61-
try {
62-
is = loader.getResourceAsStream(resourceName);
63-
addToJar(resourceName, convertToByteArray(is), target);
64-
} finally {
65-
if (null != is) {
66-
is.close();
58+
try (final JarOutputStream target =
59+
new JarOutputStream(new FileOutputStream(tmpJar), manifest)) {
60+
for (final String resourceName : resourceNames) {
61+
try (InputStream is = loader.getResourceAsStream(resourceName)) {
62+
if (is != null) {
63+
addToJar(resourceName, convertToByteArray(is), target);
64+
}
6765
}
6866
}
6967
}
70-
target.close();
7168

7269
return tmpJar.toURI().toURL();
7370
}

0 commit comments

Comments
 (0)