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.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   * <strong>NOTE</strong> Eclipse users must disable the <code>Build automatically</code> option,
41   * otherwise it'll continually rebuild the project, causing compilations or tests to fail.
42   *
43   * @author Robert Scholte
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 }