kbuntrock commented on code in PR #147:
URL: 
https://github.com/apache/maven-build-cache-extension/pull/147#discussion_r1580192169


##########
src/test/java/org/apache/maven/buildcache/xml/CacheConfigImplTest.java:
##########
@@ -122,11 +126,30 @@ private static void deepMockConfigFile(File mockFile, 
boolean exists) throws IOE
         when(mockPath.getFileSystem()).thenReturn(mockFileSystem);
         FileSystemProvider mockProvider = mock(FileSystemProvider.class);
         when(mockFileSystem.provider()).thenReturn(mockProvider);
+
+        // Mock for java < 20.
         if (!exists) {
             doThrow(new IOException("mock IOException"))
                     .when(mockProvider)
                     .checkAccess(ArgumentMatchers.eq(mockPath), 
ArgumentMatchers.any(AccessMode.class));
         }
+
+        // Mock for java >= 20. Since the FileSystemProvider.exists method 
does not exist before v20, we use reflection
+        // to create the mock
+        Optional<Method> methodToMock = 
Arrays.stream(FileSystemProvider.class.getDeclaredMethods())
+                .filter(method -> "exists".equals(method.getName()))
+                .findAny();
+        if (methodToMock.isPresent()) {
+            Class<?>[] paramTypes = methodToMock.get().getParameterTypes();
+            Object[] params = Arrays.stream(paramTypes)
+                    .map(paramType -> Mockito.any(paramType))
+                    .toArray();
+            try {
+                Mockito.when(methodToMock.get().invoke(mockProvider, 
params)).thenReturn(exists);

Review Comment:
   Yes, it is used internally by the static `java.nio.file.Files#exists` method.
   
   Implementation differs from jdk versions.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@maven.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to