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