Skip to content

Commit 2a72502

Browse files
committed
Move SampleClasses test utility to dedicated 'testing' sub-project
1 parent dc1326b commit 2a72502

File tree

10 files changed

+56
-31
lines changed

10 files changed

+56
-31
lines changed

buildSrc/src/main/kotlin/java-common.gradle.kts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ plugins {
44
java
55
jacoco
66

7+
id("me.champeau.jmh")
78
id("com.diffplug.spotless")
89
id("com.github.spotbugs")
910
id("pl.allegro.tech.build.axion-release")
@@ -15,6 +16,14 @@ repositories {
1516

1617
version = rootProject.version
1718

19+
dependencies {
20+
compileOnly(libs.spotbugs.annotations)
21+
testCompileOnly(libs.spotbugs.annotations)
22+
testImplementation(libs.junit.jupiter)
23+
testImplementation(libs.assertj.core)
24+
testRuntimeOnly(libs.junit.launcher)
25+
}
26+
1827
java {
1928
toolchain {
2029
languageVersion = JavaLanguageVersion.of(17)
@@ -32,14 +41,6 @@ tasks.javadoc {
3241
}
3342
}
3443

35-
dependencies {
36-
compileOnly(libs.spotbugs.annotations)
37-
testCompileOnly(libs.spotbugs.annotations)
38-
testImplementation(libs.junit.jupiter)
39-
testImplementation(libs.assertj.core)
40-
testRuntimeOnly(libs.junit.launcher)
41-
}
42-
4344
tasks.named<Test>("test") {
4445
useJUnitPlatform()
4546
}
@@ -70,7 +71,6 @@ tasks.check { finalizedBy(tasks.jacocoTestReport) }
7071
spotless {
7172
java {
7273
target("src/**/*.java")
73-
7474
importOrder()
7575
removeUnusedImports()
7676
googleJavaFormat()
@@ -79,6 +79,21 @@ spotless {
7979

8080
spotbugs {
8181
useJavaToolchains = true
82-
8382
omitVisitors = listOf("FindReturnRef")
8483
}
84+
85+
// dependency configuration to help pull sample bytecode in for testing
86+
val sampleBytecode by configurations.creating {
87+
isTransitive = false
88+
}
89+
90+
// copy sample bytecode jars to a known location for testing/benchmarking
91+
val sampleBytecodeDir = layout.buildDirectory.dir("sampleBytecode")
92+
val copySampleBytecode = tasks.register<Copy>("sampleBytecode") {
93+
val versionJarSuffix = "-[0-9.]*\\.jar$".toRegex()
94+
rename { name -> name.replace(versionJarSuffix, ".jar") }
95+
into(sampleBytecodeDir)
96+
from(sampleBytecode)
97+
}
98+
tasks.test { dependsOn(copySampleBytecode) }
99+
tasks.jmh { dependsOn(copySampleBytecode) }

class-match/build.gradle.kts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,12 @@
11
plugins {
22
id("java-common")
3-
id("me.champeau.jmh")
4-
}
5-
6-
val sampleBytecode by configurations.creating {
7-
isTransitive = false
83
}
94

105
dependencies {
116
implementation(project(":utils"))
127

138
sampleBytecode("org.ow2.asm:asm-test:9.8")
149
sampleBytecode("org.springframework:spring-web:6.2.8")
15-
10+
jmh(project(":testing"))
1611
jmh(libs.asm)
1712
}
18-
19-
// copy sample bytecode jars to a known location for testing/benchmarking
20-
val sampleBytecodeDir = layout.buildDirectory.dir("sampleBytecode")
21-
val copySampleBytecode = tasks.register<Copy>("sampleBytecode") {
22-
val versionJarSuffix = "-[0-9.]*\\.jar$".toRegex()
23-
rename { name -> name.replace(versionJarSuffix, ".jar") }
24-
into(sampleBytecodeDir)
25-
from(sampleBytecode)
26-
}
27-
tasks.test { dependsOn(copySampleBytecode) }
28-
tasks.jmh { dependsOn(copySampleBytecode) }

class-match/src/jmh/java/datadog/instrument/classmatch/ClassFileBenchmark.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static org.objectweb.asm.Opcodes.ASM9;
99
import static org.openjdk.jmh.annotations.Mode.AverageTime;
1010

11+
import datadog.instrument.testing.SampleClasses;
1112
import java.util.ArrayList;
1213
import java.util.List;
1314
import org.objectweb.asm.AnnotationVisitor;

class-match/src/jmh/java/datadog/instrument/classmatch/ClassInfoCacheBenchmark.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static java.util.concurrent.TimeUnit.MICROSECONDS;
44
import static org.openjdk.jmh.annotations.Mode.AverageTime;
55

6+
import datadog.instrument.testing.SampleClasses;
67
import java.net.URL;
78
import java.net.URLClassLoader;
89
import java.util.List;

class-match/src/jmh/java/datadog/instrument/classmatch/ClassNameFilterBenchmark.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static java.util.concurrent.TimeUnit.MICROSECONDS;
44
import static org.openjdk.jmh.annotations.Mode.AverageTime;
55

6+
import datadog.instrument.testing.SampleClasses;
67
import java.util.List;
78
import java.util.stream.Collectors;
89
import org.openjdk.jmh.annotations.Benchmark;

class-match/src/jmh/java/datadog/instrument/classmatch/ClassNameTrieBenchmark.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static java.util.concurrent.TimeUnit.MICROSECONDS;
44
import static org.openjdk.jmh.annotations.Mode.AverageTime;
55

6+
import datadog.instrument.testing.SampleClasses;
67
import java.util.Arrays;
78
import java.util.HashSet;
89
import java.util.List;

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
rootProject.name = "dd-instrument-java"
22

3+
include(":testing")
34
include(":utils")
45
include(":class-inject")
56
include(":class-match")

testing/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
plugins {
2+
id("java-common")
3+
}

class-match/src/jmh/java/datadog/instrument/classmatch/SampleClasses.java renamed to testing/src/main/java/datadog/instrument/testing/SampleClasses.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
package datadog.instrument.classmatch;
1+
/*
2+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
3+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
* Copyright 2025-Present Datadog, Inc.
5+
*/
6+
7+
package datadog.instrument.testing;
28

39
import java.io.ByteArrayOutputStream;
410
import java.io.File;
@@ -10,9 +16,22 @@
1016
import java.util.jar.JarFile;
1117
import java.util.stream.Collectors;
1218

13-
final class SampleClasses {
19+
/**
20+
* Provides access to external bytecode declared using the {@code sampleBytecode} gradle dependency.
21+
*/
22+
public final class SampleClasses {
23+
24+
/** The directory containing the sample bytecode jars. */
25+
public static final String SAMPLE_BYTECODE_DIRECTORY = "build/sampleBytecode/";
26+
27+
/**
28+
* Loads all class bytecode from the named sample bytecode jar.
29+
*
30+
* @param sampleJar the name of the sample jar (without version)
31+
* @return list of bytecode contained in the sample jar
32+
*/
1433
public static List<byte[]> load(String sampleJar) {
15-
File sampleJarFile = new File("build/sampleBytecode/" + sampleJar);
34+
File sampleJarFile = new File(SAMPLE_BYTECODE_DIRECTORY + sampleJar);
1635
byte[] buf = new byte[16384];
1736
ByteArrayOutputStream out = new ByteArrayOutputStream();
1837
try (JarFile sample = new JarFile(sampleJarFile)) {

utils/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
plugins {
22
id("java-multiversion")
3-
id("me.champeau.jmh")
43
}
54

65
tasks.withType<Test>().configureEach {

0 commit comments

Comments
 (0)