Skip to content

Commit 66eda9a

Browse files
authored
Migrate cross-project test source sharing to java-test-fixtures for Gradle 9.5 compatibility (#11367)
build: use java-test-fixtures to share JFR test resources profiling-controller-openjdk used files(project(':...:profiling-controller-jfr').sourceSets.test.output) In Gradle 8 this worked: inside the `dependencies` closure, `project(':foo')` resolved via `Project.project(String)` -- which is a Groovy DSL lookup fallback, since `DependencyHandler` had no `project(String)` method -- returning the real `Project` -- which has the `sourceSets` property. Gradle 9.5 added `DependencyHandler.project(String)` that returns a `ProjectDependency`, which Groovy now resolves first. The instance is a `DefaultProjectDependency`, which has no `sourceSets` property, and as such fails with: ``` > Could not get unknown property 'sourceSets' for project ':dd-java-agent:agent-profiling:profiling-controller-jfr' of type org.gradle.api.internal.artifacts.dependencies.DefaultProjectDependency. ``` This commit instead rely on the java-test-fixtures plugin applied to profiling-controller-jfr to share test resources, consumer projects now declare : testFixtures(project(':...:profiling-controller-jfr')). build(mongo): remove broken sourceSets.test.output dependency lines Gradle 9.5 added `DependencyHandler.project(String)` returning a `ProjectDependency`, shadowing `Project.project(String)` in `dependencies` closures. Since `ProjectDependency` implementation do not has a `sourceSets` property it fails the build. The goal of using `files(project(':foo').sourceSets.test.output)` is to share test code in other projects which is supported by the test java-test-fixtures plugin that is already applied to the `:mongo-common` project. Also the nine mongo driver/test consumers already declared the correct `testFixtures(project(':...:mongo-common'))` so this commit drops the now-broken duplicate `sourceSets.test.output` dependency declarations. chore: Avoid jfp file extraction at class init Co-authored-by: brice.dutheil <brice.dutheil@datadoghq.com>
1 parent 0eb8894 commit 66eda9a

19 files changed

Lines changed: 72 additions & 24 deletions

File tree

dd-java-agent/agent-profiling/profiling-controller-jfr/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
plugins {
2+
id 'java-test-fixtures'
3+
}
4+
15
apply from: "$rootDir/gradle/java.gradle"
26
apply plugin: 'idea'
37

dd-java-agent/agent-profiling/profiling-controller-jfr/src/test/java/com/datadog/profiling/controller/jfr/JfpUtilsTest.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ public class JfpUtilsTest {
1616
private static final String CONFIG_ENTRY = "jdk.ThreadAllocationStatistics#enabled";
1717
private static final String CONFIG_OVERRIDE_ENTRY = "test.continuous.override#value";
1818

19-
public static final String OVERRIDES =
20-
JfpUtilsTest.class.getClassLoader().getResource("overrides.jfp").getFile();
21-
public static final String OVERRIDES_OLD_OBJECT_SAMPLE =
22-
JfpUtilsTest.class.getClassLoader().getResource("overrides-oldobjectsample.jfp").getFile();
23-
public static final String OVERRIDES_OBJECT_ALLOCATION =
24-
JfpUtilsTest.class.getClassLoader().getResource("overrides-objectallocation.jfp").getFile();
25-
public static final String OVERRIDES_NATIVE_METHOD_SAMPLE =
26-
JfpUtilsTest.class.getClassLoader().getResource("overrides-nativemethodsample.jfp").getFile();
27-
2819
@Test
2920
public void testLoadingInvalidOverride() throws IOException {
3021
final String INVALID_OVERRIDE = "really_non_existent_file.jfp";
@@ -42,7 +33,8 @@ public void testLoadingContinuousConfig() throws IOException {
4233

4334
@Test
4435
public void testLoadingContinuousConfigWithOverride() throws IOException {
45-
final Map<String, String> config = JfpUtils.readJfpResources(JfpUtils.DEFAULT_JFP, OVERRIDES);
36+
final Map<String, String> config =
37+
JfpUtils.readJfpResources(JfpUtils.DEFAULT_JFP, JfpTestResources.overrides());
4638
assertEquals("true", config.get(CONFIG_ENTRY));
4739
assertEquals("200", config.get(CONFIG_OVERRIDE_ENTRY));
4840
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.datadog.profiling.controller.jfr;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
import java.nio.file.StandardCopyOption;
8+
import java.util.concurrent.atomic.AtomicReference;
9+
10+
public final class JfpTestResources {
11+
private static final AtomicReference<String> overrides = new AtomicReference<>();
12+
private static final AtomicReference<String> overridesOldObjectSample = new AtomicReference<>();
13+
private static final AtomicReference<String> overridesObjectAllocation = new AtomicReference<>();
14+
private static final AtomicReference<String> overridesNativeMethodSample =
15+
new AtomicReference<>();
16+
17+
private JfpTestResources() {}
18+
19+
public static String overrides() {
20+
return get(overrides, "overrides.jfp");
21+
}
22+
23+
public static String overridesOldObjectSample() {
24+
return get(overridesOldObjectSample, "overrides-oldobjectsample.jfp");
25+
}
26+
27+
public static String overridesObjectAllocation() {
28+
return get(overridesObjectAllocation, "overrides-objectallocation.jfp");
29+
}
30+
31+
public static String overridesNativeMethodSample() {
32+
return get(overridesNativeMethodSample, "overrides-nativemethodsample.jfp");
33+
}
34+
35+
private static String get(AtomicReference<String> ref, String resourceName) {
36+
String v = ref.get();
37+
if (v != null) {
38+
return v;
39+
}
40+
String computed = extract(resourceName);
41+
return ref.compareAndSet(null, computed) ? computed : ref.get();
42+
}
43+
44+
// Resources packaged in the testFixtures jar are not directly accessible as filesystem paths.
45+
// Extract to a temp file so callers can use the path with java.io.File.
46+
private static String extract(String resourceName) {
47+
try {
48+
Path temp = Files.createTempFile(resourceName.replace('.', '_'), ".jfp");
49+
temp.toFile().deleteOnExit();
50+
try (InputStream is =
51+
JfpTestResources.class.getClassLoader().getResourceAsStream(resourceName)) {
52+
if (is == null) {
53+
throw new IllegalStateException("Test resource not found on classpath: " + resourceName);
54+
}
55+
Files.copy(is, temp, StandardCopyOption.REPLACE_EXISTING);
56+
}
57+
return temp.toAbsolutePath().toString();
58+
} catch (IOException e) {
59+
throw new RuntimeException("Failed to extract test resource: " + resourceName, e);
60+
}
61+
}
62+
}

dd-java-agent/agent-profiling/profiling-controller-jfr/src/test/resources/overrides-nativemethodsample.jfp renamed to dd-java-agent/agent-profiling/profiling-controller-jfr/src/testFixtures/resources/overrides-nativemethodsample.jfp

File renamed without changes.

dd-java-agent/agent-profiling/profiling-controller-jfr/src/test/resources/overrides-objectallocation.jfp renamed to dd-java-agent/agent-profiling/profiling-controller-jfr/src/testFixtures/resources/overrides-objectallocation.jfp

File renamed without changes.

dd-java-agent/agent-profiling/profiling-controller-jfr/src/test/resources/overrides-oldobjectsample.jfp renamed to dd-java-agent/agent-profiling/profiling-controller-jfr/src/testFixtures/resources/overrides-oldobjectsample.jfp

File renamed without changes.

dd-java-agent/agent-profiling/profiling-controller-jfr/src/test/resources/overrides.jfp renamed to dd-java-agent/agent-profiling/profiling-controller-jfr/src/testFixtures/resources/overrides.jfp

File renamed without changes.

dd-java-agent/agent-profiling/profiling-controller-openjdk/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ dependencies {
2828

2929
testImplementation libs.bundles.junit5
3030
testImplementation libs.bundles.mockito
31-
testImplementation files(project(':dd-java-agent:agent-profiling:profiling-controller-jfr').sourceSets.test.output)
31+
testImplementation(testFixtures(project(':dd-java-agent:agent-profiling:profiling-controller-jfr')))
3232
testImplementation project(':dd-java-agent:agent-profiling')
3333
}
3434

dd-java-agent/agent-profiling/profiling-controller-openjdk/src/test/java/com/datadog/profiling/controller/openjdk/OpenJdkControllerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import static org.junit.jupiter.api.Assumptions.assumeFalse;
1616

1717
import com.datadog.profiling.controller.ControllerContext;
18-
import com.datadog.profiling.controller.jfr.JfpUtilsTest;
18+
import com.datadog.profiling.controller.jfr.JfpTestResources;
1919
import com.datadog.profiling.utils.ProfilingMode;
2020
import datadog.environment.JavaVirtualMachine;
2121
import datadog.trace.api.profiling.RecordingData;
@@ -76,7 +76,7 @@ public void testHeapProfilerIsDisabledOnUnsupportedVersion() throws Exception {
7676
@Test
7777
public void testHeapProfilerIsStillOverriddenOnUnsupportedVersion() throws Exception {
7878
Properties props = getConfigProperties();
79-
props.put(PROFILING_TEMPLATE_OVERRIDE_FILE, JfpUtilsTest.OVERRIDES_OLD_OBJECT_SAMPLE);
79+
props.put(PROFILING_TEMPLATE_OVERRIDE_FILE, JfpTestResources.overridesOldObjectSample());
8080

8181
ConfigProvider configProvider = ConfigProvider.withPropertiesOverride(props);
8282

@@ -158,7 +158,7 @@ public void testAllocationProfilerIsDisabledOnUnsupportedVersion() throws Except
158158
@Test
159159
public void testAllocationProfilerIsStillOverriddenOnUnsupportedVersion() throws Exception {
160160
Properties props = getConfigProperties();
161-
props.put(PROFILING_TEMPLATE_OVERRIDE_FILE, JfpUtilsTest.OVERRIDES_OBJECT_ALLOCATION);
161+
props.put(PROFILING_TEMPLATE_OVERRIDE_FILE, JfpTestResources.overridesObjectAllocation());
162162
ConfigProvider configProvider = ConfigProvider.withPropertiesOverride(props);
163163

164164
OpenJdkController controller = new OpenJdkController(configProvider);

dd-java-agent/agent-profiling/profiling-controller-oracle/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ dependencies {
2121

2222
testImplementation libs.bundles.junit5
2323
testImplementation libs.bundles.mockito
24-
testImplementation files(project(':dd-java-agent:agent-profiling:profiling-controller-jfr').sourceSets.test.output)
2524
testImplementation project(':dd-java-agent:agent-profiling')
2625
}
2726

0 commit comments

Comments
 (0)