1 package org.codehaus.plexus.languages.java.jpms;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.nio.file.Path;
23 import java.nio.file.Paths;
24 import java.util.Collections;
25
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.junit.jupiter.api.condition.DisabledOnJre;
29 import org.junit.jupiter.api.condition.JRE;
30 import org.junit.jupiter.api.extension.ExtendWith;
31 import org.mockito.Mock;
32 import org.mockito.junit.jupiter.MockitoExtension;
33
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.junit.jupiter.api.Assertions.assertThrows;
36 import static org.mockito.ArgumentMatchers.any;
37 import static org.mockito.Mockito.when;
38
39
40
41
42
43
44
45 @DisabledOnJre(value = JRE.JAVA_8, disabledReason = "Requires Java 9+ Module System")
46 @ExtendWith(MockitoExtension.class)
47 class LocationManagerIT {
48 @Mock
49 private BinaryModuleInfoParser asmParser;
50
51 @Mock
52 private SourceModuleInfoParser qdoxParser;
53
54 private LocationManager locationManager;
55
56 final Path mockModuleInfoJava = Paths.get("src/test/test-data/mock/module-info.java");
57
58 @BeforeEach
59 void onSetup() {
60 locationManager = new LocationManager(qdoxParser) {
61 @Override
62 ModuleInfoParser getBinaryModuleInfoParser(Path jdkHome) {
63 return asmParser;
64 }
65 };
66 }
67
68 @Test
69 void testManifestWithoutReflectRequires() throws Exception {
70 Path abc = Paths.get("src/test/test-data/manifest.without/out");
71 JavaModuleDescriptor descriptor =
72 JavaModuleDescriptor.newModule("base").requires("any").build();
73 when(qdoxParser.fromSourcePath(any(Path.class))).thenReturn(descriptor);
74 ResolvePathsRequest<Path> request =
75 ResolvePathsRequest.ofPaths(Collections.singletonList(abc)).setMainModuleDescriptor(mockModuleInfoJava);
76
77 ResolvePathsResult<Path> result = locationManager.resolvePaths(request);
78
79 assertThat(result.getPathExceptions()).isEmpty();
80 assertThat(result.getMainModuleDescriptor()).isEqualTo(descriptor);
81 assertThat(result.getPathElements()).hasSize(1);
82 assertThat(result.getModulepathElements()).isEmpty();
83 assertThat(result.getClasspathElements()).hasSize(1);
84 }
85
86 @Test
87 void testEmptyWithReflectRequires() throws Exception {
88 Path abc = Paths.get("src/test/test-data/empty/out");
89 JavaModuleDescriptor descriptor =
90 JavaModuleDescriptor.newModule("base").requires("a.b.c").build();
91 when(qdoxParser.fromSourcePath(any(Path.class))).thenReturn(descriptor);
92 ResolvePathsRequest<Path> request =
93 ResolvePathsRequest.ofPaths(Collections.singletonList(abc)).setMainModuleDescriptor(mockModuleInfoJava);
94
95 ResolvePathsResult<Path> result = locationManager.resolvePaths(request);
96
97 assertThat(result.getPathExceptions()).hasSize(0);
98 assertThat(result.getMainModuleDescriptor()).isEqualTo(descriptor);
99 assertThat(result.getPathElements()).hasSize(1);
100 assertThat(result.getModulepathElements()).hasSize(0);
101 assertThat(result.getClasspathElements()).hasSize(1);
102 }
103
104 @Test
105 void testResolvePathWithException() {
106 assertThrows(RuntimeException.class, () -> {
107 Path p = Paths.get("src/test/test-data/jar.empty.invalid.name/101-1.0.0-SNAPSHOT.jar");
108 ResolvePathRequest<Path> request = ResolvePathRequest.ofPath(p);
109
110 locationManager.resolvePath(request);
111 });
112 }
113
114 @Test
115 void testClassicJarNameStartsWithNumber() throws Exception {
116 Path p = Paths.get("src/test/test-data/jar.empty.invalid.name/101-1.0.0-SNAPSHOT.jar");
117 ResolvePathsRequest<Path> request =
118 ResolvePathsRequest.ofPaths(Collections.singletonList(p)).setMainModuleDescriptor(mockModuleInfoJava);
119
120 ResolvePathsResult<Path> result = locationManager.resolvePaths(request);
121
122 assertThat(result.getPathExceptions()).hasSize(1);
123 assertThat(result.getClasspathElements()).hasSize(1);
124 }
125 }