From 3923550985b9d4caaafea2b5d97f96f560292819 Mon Sep 17 00:00:00 2001 From: Marc Philipp Date: Wed, 22 Jan 2025 19:42:20 +0100 Subject: [PATCH] Use separate local Maven repos when file system type is NTFS --- .../support/tests/JavaVersionsTests.java | 2 +- .../tooling/support/tests/LocalMavenRepo.java | 27 +++++++++++++ ...obalResource.java => ManagedResource.java} | 39 ++++++++++++++++--- .../support/tests/MavenStarterTests.java | 4 +- .../MavenSurefireCompatibilityTests.java | 2 +- .../support/tests/MultiReleaseJarTests.java | 4 +- .../tests/UnalignedClasspathTests.java | 4 +- .../tests/VintageMavenIntegrationTests.java | 2 +- 8 files changed, 70 insertions(+), 14 deletions(-) rename platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/{GlobalResource.java => ManagedResource.java} (75%) diff --git a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/JavaVersionsTests.java b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/JavaVersionsTests.java index 4a7cce72c99e..417bb8119906 100644 --- a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/JavaVersionsTests.java +++ b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/JavaVersionsTests.java @@ -34,7 +34,7 @@ */ class JavaVersionsTests { - @GlobalResource + @ManagedResource LocalMavenRepo localMavenRepo; @TempDir diff --git a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/LocalMavenRepo.java b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/LocalMavenRepo.java index dc9b99e58a8d..bead9469bd08 100644 --- a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/LocalMavenRepo.java +++ b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/LocalMavenRepo.java @@ -10,14 +10,41 @@ package platform.tooling.support.tests; +import static platform.tooling.support.tests.ManagedResource.Scope.GLOBAL; +import static platform.tooling.support.tests.ManagedResource.Scope.PER_CONTEXT; + import java.io.IOException; import java.io.UncheckedIOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Comparator; +import org.junit.jupiter.api.extension.ExtensionContext; + +@ManagedResource.Scoped(LocalMavenRepo.ScopeProvider.class) public class LocalMavenRepo implements AutoCloseable { + public static class ScopeProvider implements ManagedResource.Scoped.Provider { + + @Override + public ManagedResource.Scope determineScope(ExtensionContext extensionContext) { + var tmpDir = Path.of(System.getProperty("java.io.tmpdir")); + var fileSystemType = getFileSystemType(tmpDir); + extensionContext.publishReportEntry("tempFileSystemType", fileSystemType); + // Writing to the same file from multiple Maven processes may fail the build on Windows + return "NTFS".equalsIgnoreCase(fileSystemType) ? PER_CONTEXT : GLOBAL; + } + + private static String getFileSystemType(Path path) { + try { + return Files.getFileStore(path).type(); + } + catch (IOException e) { + throw new UncheckedIOException(e); + } + } + } + private final Path tempDir; public LocalMavenRepo() { diff --git a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/GlobalResource.java b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/ManagedResource.java similarity index 75% rename from platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/GlobalResource.java rename to platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/ManagedResource.java index 8774adbda241..c278f0a8c0e8 100644 --- a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/GlobalResource.java +++ b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/ManagedResource.java @@ -32,15 +32,30 @@ @Target({ ElementType.PARAMETER, ElementType.FIELD }) @Retention(RUNTIME) -@ExtendWith(GlobalResource.Extension.class) -public @interface GlobalResource { +@ExtendWith(ManagedResource.Extension.class) +public @interface ManagedResource { + + @Target(ElementType.TYPE) + @Retention(RUNTIME) + @interface Scoped { + + Class value(); + + interface Provider { + Scope determineScope(ExtensionContext extensionContext); + } + } + + enum Scope { + GLOBAL, PER_CONTEXT + } class Extension implements ParameterResolver, TestInstancePostProcessor { @Override public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { - return parameterContext.isAnnotated(GlobalResource.class); + return parameterContext.isAnnotated(ManagedResource.class); } @Override @@ -50,9 +65,14 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte return getOrCreateResource(extensionContext, type).get(); } + @Override + public ExtensionContextScope getTestInstantiationExtensionContextScope(ExtensionContext rootContext) { + return ExtensionContextScope.TEST_METHOD; + } + @Override public void postProcessTestInstance(Object testInstance, ExtensionContext extensionContext) { - streamFields(testInstance.getClass(), field -> AnnotationSupport.isAnnotated(field, GlobalResource.class), + streamFields(testInstance.getClass(), field -> AnnotationSupport.isAnnotated(field, ManagedResource.class), HierarchyTraversalMode.BOTTOM_UP) // .forEach(field -> { try { @@ -66,7 +86,16 @@ public void postProcessTestInstance(Object testInstance, ExtensionContext extens @SuppressWarnings("unchecked") private Resource getOrCreateResource(ExtensionContext extensionContext, Class type) { - return extensionContext.getRoot().getStore(Namespace.GLOBAL) // + var scope = AnnotationSupport.findAnnotation(type, Scoped.class) // + .map(Scoped::value) // + .map(ReflectionSupport::newInstance) // + .map(provider -> provider.determineScope(extensionContext)) // + .orElse(Scope.GLOBAL); + var storingContext = switch (scope) { + case GLOBAL -> extensionContext.getRoot(); + case PER_CONTEXT -> extensionContext; + }; + return storingContext.getStore(Namespace.GLOBAL) // .getOrComputeIfAbsent(type, Resource::new, Resource.class); } } diff --git a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/MavenStarterTests.java b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/MavenStarterTests.java index 9faf699942ea..5a795087abae 100644 --- a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/MavenStarterTests.java +++ b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/MavenStarterTests.java @@ -32,10 +32,10 @@ */ class MavenStarterTests { - @GlobalResource + @ManagedResource LocalMavenRepo localMavenRepo; - @GlobalResource + @ManagedResource MavenRepoProxy mavenRepoProxy; @Test diff --git a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/MavenSurefireCompatibilityTests.java b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/MavenSurefireCompatibilityTests.java index a59d90ecbf53..3a62853d52aa 100644 --- a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/MavenSurefireCompatibilityTests.java +++ b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/MavenSurefireCompatibilityTests.java @@ -33,7 +33,7 @@ */ class MavenSurefireCompatibilityTests { - @GlobalResource + @ManagedResource LocalMavenRepo localMavenRepo; @ParameterizedTest diff --git a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/MultiReleaseJarTests.java b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/MultiReleaseJarTests.java index c07954ac5c5e..4326fa6fa7c9 100644 --- a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/MultiReleaseJarTests.java +++ b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/MultiReleaseJarTests.java @@ -33,10 +33,10 @@ */ class MultiReleaseJarTests { - @GlobalResource + @ManagedResource LocalMavenRepo localMavenRepo; - @GlobalResource + @ManagedResource MavenRepoProxy mavenRepoProxy; @Test diff --git a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/UnalignedClasspathTests.java b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/UnalignedClasspathTests.java index a6467c511564..7869a3d59186 100644 --- a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/UnalignedClasspathTests.java +++ b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/UnalignedClasspathTests.java @@ -37,10 +37,10 @@ */ class UnalignedClasspathTests { - @GlobalResource + @ManagedResource LocalMavenRepo localMavenRepo; - @GlobalResource + @ManagedResource MavenRepoProxy mavenRepoProxy; @ParameterizedTest diff --git a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/VintageMavenIntegrationTests.java b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/VintageMavenIntegrationTests.java index 7f735753f009..2b95c5b891d5 100644 --- a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/VintageMavenIntegrationTests.java +++ b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/VintageMavenIntegrationTests.java @@ -29,7 +29,7 @@ class VintageMavenIntegrationTests { - @GlobalResource + @ManagedResource LocalMavenRepo localMavenRepo; @TempDir