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.invoker;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import org.apache.maven.plugins.invoker.AbstractInvokerMojo.ToolchainPrivateManager;
29  import org.apache.maven.toolchain.ToolchainPrivate;
30  import org.junit.jupiter.api.Test;
31  
32  import static org.assertj.core.api.Assertions.assertThat;
33  import static org.assertj.core.api.Assertions.assertThatCode;
34  import static org.mockito.ArgumentMatchers.anyMap;
35  import static org.mockito.Mockito.mock;
36  import static org.mockito.Mockito.when;
37  
38  /**
39   * Tests {@link SelectorUtils}.
40   *
41   * @author Benjamin Bentmann
42   */
43  class SelectorUtilsTest {
44  
45      @Test
46      void testParseList() {
47          List<String> includes = new ArrayList<>();
48          List<String> excludes = new ArrayList<>();
49  
50          SelectorUtils.parseList(null, includes, excludes);
51  
52          SelectorUtils.parseList(" 1.5, !1.4, 1.6+ ", includes, excludes);
53          assertThat(includes).containsExactly("1.5", "1.6+");
54          assertThat(excludes).containsExactly("1.4");
55      }
56  
57      @Test
58      void testParseVersion() {
59          assertThat(SelectorUtils.parseVersion("1.6.0_12")).containsExactly(1, 6, 0, 12);
60          assertThat(SelectorUtils.parseVersion("1.6.0_12+")).containsExactly(1, 6, 0, 12);
61          assertThat(SelectorUtils.parseVersion("1.6.0_12-")).containsExactly(1, 6, 0, 12);
62      }
63  
64      @Test
65      void testCompareVersions() {
66          assertThat(SelectorUtils.compareVersions(Arrays.asList(1, 6), Arrays.asList(1, 6)))
67                  .isZero();
68  
69          assertThat(SelectorUtils.compareVersions(Arrays.asList(1, 5), Arrays.asList(1, 6)))
70                  .isNegative();
71          assertThat(SelectorUtils.compareVersions(Arrays.asList(1, 6), Arrays.asList(1, 5)))
72                  .isPositive();
73  
74          assertThat(SelectorUtils.compareVersions(Collections.singletonList(1), Arrays.asList(1, 6)))
75                  .isNegative();
76          assertThat(SelectorUtils.compareVersions(Arrays.asList(1, 6), Collections.singletonList(1)))
77                  .isPositive();
78      }
79  
80      @Test
81      void testIsMatchingJre() {
82  
83          assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 4, 2, 8), "1.5")).isFalse();
84          assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 5), "1.5")).isTrue();
85          assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 5, 9), "1.5")).isTrue();
86          assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 6), "1.5")).isFalse();
87  
88          assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 4, 2, 8), "1.5+"))
89                  .isFalse();
90          assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 5), "1.5+")).isTrue();
91          assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 5, 9), "1.5+")).isTrue();
92          assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 6), "1.5+")).isTrue();
93  
94          assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 4, 2, 8), "1.5-"))
95                  .isTrue();
96          assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 5), "1.5-")).isFalse();
97          assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 5, 9), "1.5-")).isFalse();
98          assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 6), "1.5-")).isFalse();
99  
100         assertThat(SelectorUtils.isJreVersion((String) null, "1.5")).isTrue();
101         assertThat(SelectorUtils.isJreVersion("", "1.5")).isTrue();
102     }
103 
104     @Test
105     void testIsMatchingToolchain() throws Exception {
106         InvokerToolchain openJdk9 = new InvokerToolchain("jdk");
107         openJdk9.addProvides("version", "9");
108         openJdk9.addProvides("vendor", "openJDK");
109 
110         InvokerToolchain maven360 = new InvokerToolchain("maven");
111         openJdk9.addProvides("version", "3.6.0");
112 
113         ToolchainPrivateManager toolchainPrivateManager = mock(ToolchainPrivateManager.class);
114         ToolchainPrivate jdkMatching = mock(ToolchainPrivate.class);
115         when(jdkMatching.matchesRequirements(anyMap())).thenReturn(true);
116         when(jdkMatching.getType()).thenReturn("jdk");
117 
118         ToolchainPrivate jdkMismatch = mock(ToolchainPrivate.class);
119         when(jdkMismatch.getType()).thenReturn("jdk");
120 
121         when(toolchainPrivateManager.getToolchainPrivates("jdk")).thenReturn(new ToolchainPrivate[] {jdkMatching});
122         assertThat(SelectorUtils.isToolchain(toolchainPrivateManager, Collections.singleton(openJdk9)))
123                 .isTrue();
124 
125         when(toolchainPrivateManager.getToolchainPrivates("jdk")).thenReturn(new ToolchainPrivate[] {jdkMismatch});
126         assertThat(SelectorUtils.isToolchain(toolchainPrivateManager, Collections.singleton(openJdk9)))
127                 .isFalse();
128 
129         when(toolchainPrivateManager.getToolchainPrivates("jdk"))
130                 .thenReturn(new ToolchainPrivate[] {jdkMatching, jdkMismatch, jdkMatching});
131         assertThat(SelectorUtils.isToolchain(toolchainPrivateManager, Collections.singleton(openJdk9)))
132                 .isTrue();
133 
134         when(toolchainPrivateManager.getToolchainPrivates("jdk")).thenReturn(new ToolchainPrivate[0]);
135         assertThat(SelectorUtils.isToolchain(toolchainPrivateManager, Collections.singleton(openJdk9)))
136                 .isFalse();
137 
138         when(toolchainPrivateManager.getToolchainPrivates("jdk")).thenReturn(new ToolchainPrivate[] {jdkMatching});
139         when(toolchainPrivateManager.getToolchainPrivates("maven")).thenReturn(new ToolchainPrivate[0]);
140         assertThat(SelectorUtils.isToolchain(toolchainPrivateManager, Arrays.asList(openJdk9, maven360)))
141                 .isFalse();
142     }
143 
144     @Test
145     void mavenVersionForNotExistingMavenHomeThrowException() {
146         File mavenHome = new File("not-existing-path");
147 
148         assertThatCode(() -> SelectorUtils.getMavenVersion(mavenHome))
149                 .isExactlyInstanceOf(IllegalArgumentException.class)
150                 .hasMessage("Invalid Maven home installation directory: not-existing-path");
151     }
152 
153     @Test
154     void mavenVersionFromMavenHome() throws IOException {
155         File mavenHome = new File(System.getProperty("maven.home"));
156 
157         String mavenVersion = SelectorUtils.getMavenVersion(mavenHome);
158 
159         assertThat(mavenVersion).isNotBlank();
160     }
161 }