View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.jar;
20  
21  import java.nio.file.Files;
22  import java.nio.file.Path;
23  import java.nio.file.Paths;
24  import java.util.Locale;
25  import java.util.Optional;
26  
27  import org.apache.maven.toolchain.Toolchain;
28  import org.junit.jupiter.api.Assertions;
29  import org.junit.jupiter.api.Assumptions;
30  import org.junit.jupiter.api.Test;
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.mockito.Mockito.when;
36  
37  @ExtendWith(MockitoExtension.class)
38  class ToolchainsJdkVersionTest {
39  
40      @Mock
41      private Toolchain toolchain;
42  
43      private final ToolchainsJdkSpecification toolchainsJdkSpecification = new ToolchainsJdkSpecification();
44  
45      @Test
46      void shouldReturnCorrectSpec() {
47  
48          Path javacPath = getJavacPath();
49          Assumptions.assumeTrue(Files.isExecutable(javacPath));
50  
51          when(toolchain.findTool("javac")).thenReturn(javacPath.toString());
52  
53          Optional<String> jdkVersion = toolchainsJdkSpecification.getJDKSpecification(toolchain);
54          Assertions.assertTrue(jdkVersion.isPresent());
55          Assertions.assertEquals(System.getProperty("java.specification.version"), jdkVersion.get());
56      }
57  
58      @Test
59      void shouldReturnEmptySpec() {
60  
61          when(toolchain.findTool("javac")).thenReturn(null);
62  
63          Optional<String> jdkVersion = toolchainsJdkSpecification.getJDKSpecification(toolchain);
64          Assertions.assertFalse(jdkVersion.isPresent());
65      }
66  
67      private String getJavaCName() {
68          if (System.getProperty("os.name", "").toLowerCase(Locale.ROOT).startsWith("win")) {
69              return "javac.exe";
70          } else {
71              return "javac";
72          }
73      }
74  
75      private Path getJavacPath() {
76          String javaCName = getJavaCName();
77  
78          String javaHome = System.getProperty("java.home");
79  
80          Path javacPath = Paths.get(javaHome, "bin", javaCName);
81          if (Files.isExecutable(javacPath)) {
82              return javacPath;
83          }
84  
85          // try with jre
86          return Paths.get(javaHome, "../bin", javaCName);
87      }
88  }