Skip to content

Commit

Permalink
[MBUILDCACHE-88] Fix tests execution on jdk > 20 (#147)
Browse files Browse the repository at this point in the history
* [MBUILDCACHE-88] Fix tests execution on jdk > 20

* Try tests on jdk21
  • Loading branch information
kbuntrock authored Apr 28, 2024
1 parent 2f4c1b1 commit b6dc4a2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maven-verify-3.9.x.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
with:
maven-args: '-D"maven.test.redirectTestOutputToFile=false -Pmaven3"'
maven-matrix: '[ "3.9.5", "4.0.0-alpha-8" ]'
jdk-matrix: '[ "11", "17" ]'
jdk-matrix: '[ "11", "17", "21" ]'
ff-goal: '-P run-its verify javadoc:jar'
verify-goal: '-P run-its verify javadoc:jar'

2 changes: 1 addition & 1 deletion .github/workflows/maven-verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ jobs:
with:
maven-args: '-D"maven.test.redirectTestOutputToFile=false"'
maven-matrix: '[ "3.9.5", "4.0.0-alpha-8" ]'
jdk-matrix: '[ "11", "17" ]'
jdk-matrix: '[ "11", "17", "21" ]'
ff-goal: '-P run-its verify javadoc:jar'
verify-goal: '-P run-its verify javadoc:jar'
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
* under the License.
*/

asfMavenTlpStdBuild( 'jdks' : [ "11", "17" ] )
asfMavenTlpStdBuild( 'jdks' : [ "11", "17", "21" ] )
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.AccessMode;
import java.nio.file.FileSystem;
import java.nio.file.Path;
Expand All @@ -28,6 +30,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Collectors;

Expand All @@ -47,6 +50,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
Expand Down Expand Up @@ -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);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("Error while mocking the 'exists' method from FileSystemProvider", e);
}
}
}

private void assertDefaults() {
Expand Down

0 comments on commit b6dc4a2

Please sign in to comment.