View Javadoc
1   package org.codehaus.plexus.languages.java.jpms;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.nio.file.Path;
23  import java.nio.file.Paths;
24  import java.util.Arrays;
25  import java.util.Collections;
26  
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  import org.junit.jupiter.api.condition.DisabledOnJre;
30  import org.junit.jupiter.api.condition.JRE;
31  import org.junit.jupiter.api.extension.ExtendWith;
32  import org.mockito.Mock;
33  import org.mockito.junit.jupiter.MockitoExtension;
34  
35  import static org.assertj.core.api.Assertions.assertThat;
36  import static org.junit.jupiter.api.Assertions.assertThrows;
37  import static org.mockito.ArgumentMatchers.any;
38  import static org.mockito.Mockito.when;
39  
40  /**
41   * <strong>NOTE</strong> Eclipse users must disable the <code>Build automatically</code> option,
42   * otherwise it'll continually rebuild the project, causing compilations or tests to fail.
43   *
44   * @author Robert Scholte
45   */
46  @DisabledOnJre(value = JRE.JAVA_8, disabledReason = "Requires Java 9+ Module System")
47  @ExtendWith(MockitoExtension.class)
48  class LocationManagerIT {
49      @Mock
50      private BinaryModuleInfoParser asmParser;
51  
52      @Mock
53      private SourceModuleInfoParser qdoxParser;
54  
55      private LocationManager locationManager;
56  
57      final Path mockModuleInfoJava = Paths.get("src/test/resources/mock/module-info.java");
58  
59      @BeforeEach
60      void onSetup() {
61          locationManager = new LocationManager(qdoxParser) {
62              @Override
63              ModuleInfoParser getBinaryModuleInfoParser(Path jdkHome) {
64                  return asmParser;
65              }
66          };
67      }
68  
69      @Test
70      void testManifestWithoutReflectRequires() throws Exception {
71          Path abc = Paths.get("src/test/resources/manifest.without/out");
72          JavaModuleDescriptor descriptor =
73                  JavaModuleDescriptor.newModule("base").requires("any").build();
74          when(qdoxParser.fromSourcePath(any(Path.class))).thenReturn(descriptor);
75          ResolvePathsRequest<Path> request =
76                  ResolvePathsRequest.ofPaths(Collections.singletonList(abc)).setMainModuleDescriptor(mockModuleInfoJava);
77  
78          ResolvePathsResult<Path> result = locationManager.resolvePaths(request);
79  
80          assertThat(result.getPathExceptions()).hasSize(0);
81          assertThat(result.getMainModuleDescriptor()).isEqualTo(descriptor);
82          assertThat(result.getPathElements()).hasSize(1);
83          assertThat(result.getModulepathElements()).hasSize(0);
84          assertThat(result.getClasspathElements()).hasSize(1);
85      }
86  
87      @Test
88      void testEmptyWithReflectRequires() throws Exception {
89          Path abc = Paths.get("src/test/resources/empty/out");
90          JavaModuleDescriptor descriptor =
91                  JavaModuleDescriptor.newModule("base").requires("a.b.c").build();
92          when(qdoxParser.fromSourcePath(any(Path.class))).thenReturn(descriptor);
93          ResolvePathsRequest<Path> request =
94                  ResolvePathsRequest.ofPaths(Collections.singletonList(abc)).setMainModuleDescriptor(mockModuleInfoJava);
95  
96          ResolvePathsResult<Path> result = locationManager.resolvePaths(request);
97  
98          assertThat(result.getPathExceptions()).hasSize(0);
99          assertThat(result.getMainModuleDescriptor()).isEqualTo(descriptor);
100         assertThat(result.getPathElements()).hasSize(1);
101         assertThat(result.getModulepathElements()).hasSize(0);
102         assertThat(result.getClasspathElements()).hasSize(1);
103     }
104 
105     @Test
106     void testResolvePathWithException() {
107         assertThrows(RuntimeException.class, () -> {
108             Path p = Paths.get("src/test/resources/jar.empty.invalid.name/101-1.0.0-SNAPSHOT.jar");
109             ResolvePathRequest<Path> request = ResolvePathRequest.ofPath(p);
110 
111             locationManager.resolvePath(request);
112         });
113     }
114 
115     @Test
116     void testClassicJarNameStartsWithNumber() throws Exception {
117         Path p = Paths.get("src/test/resources/jar.empty.invalid.name/101-1.0.0-SNAPSHOT.jar");
118         ResolvePathsRequest<Path> request =
119                 ResolvePathsRequest.ofPaths(Arrays.asList(p)).setMainModuleDescriptor(mockModuleInfoJava);
120 
121         ResolvePathsResult<Path> result = locationManager.resolvePaths(request);
122 
123         assertThat(result.getPathExceptions()).hasSize(1);
124         assertThat(result.getClasspathElements()).hasSize(1);
125     }
126 }