View Javadoc
1   package org.apache.maven.shared.verifier;
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.FileNotFoundException;
24  import java.io.IOException;
25  import java.nio.file.Path;
26  import java.nio.file.Paths;
27  import java.util.Arrays;
28  import java.util.Map;
29  import java.util.Properties;
30  import java.util.stream.Stream;
31  
32  import org.junit.jupiter.api.Test;
33  import org.junit.jupiter.api.io.TempDir;
34  import org.junit.jupiter.params.ParameterizedTest;
35  import org.junit.jupiter.params.provider.Arguments;
36  import org.junit.jupiter.params.provider.MethodSource;
37  
38  import static org.hamcrest.MatcherAssert.assertThat;
39  import static org.hamcrest.Matchers.allOf;
40  import static org.hamcrest.Matchers.arrayContaining;
41  import static org.hamcrest.Matchers.hasItemInArray;
42  import static org.junit.jupiter.api.Assertions.assertEquals;
43  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
44  import static org.junit.jupiter.api.Assertions.assertThrows;
45  import static org.junit.jupiter.api.Assertions.assertTrue;
46  import static org.junit.jupiter.params.provider.Arguments.arguments;
47  
48  public class VerifierTest
49  {
50      @TempDir
51      public Path temporaryDir;
52  
53      private void check( String expected, String... lines )
54      {
55          assertEquals( expected, ForkedLauncher.extractMavenVersion( Arrays.asList( lines ) ) );
56      }
57  
58      @Test
59      public void testSunBug9009028ForJdk()
60      {
61          Properties oldProperties = System.getProperties();
62          try
63          {
64              final String version = System.getProperty( "java.version" );
65              System.setProperties( null );
66              assertEquals( version, System.getProperty( "java.version" ) );
67          }
68          finally
69          {
70              System.setProperties( oldProperties );
71          }
72      }
73  
74      @Test
75      public void testExtractMavenVersion()
76      {
77          check( "2.0.6", "Maven version: 2.0.6" );
78  
79          check( "2.0.10", "Maven version: 2.0.10", "Java version: 1.5.0_22",
80                 "OS name: \"windows 7\" version: \"6.1\" arch: \"x86\" Family: \"windows\"" );
81  
82          check( "3.0", "Apache Maven 3.0 (r1004208; 2010-10-04 13:50:56+0200)", "Java version: 1.5.0_22",
83                 "OS name: \"windows 7\" version: \"6.1\" arch: \"x86\" Family: \"windows\"" );
84  
85          check( "3.0.5", "Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 14:51:28+0100)",
86                 "Java version: 1.7.0_25",
87                 "OS name: \"linux\" version: \"3.11.0-13-generic\" arch: \"amd64\" Family: \"unix\"" );
88  
89          check( "3.2-SNAPSHOT",
90                 "Apache Maven with Log4j 2 3.2-SNAPSHOT (bc5e99f9f0aaf7d5b2431ff5d602ca3bb38559d5; 2013-11-25 21:17:33+0100)",
91                 "Java version: 1.7.0_25",
92                 "OS name: \"linux\" version: \"3.11.0-13-generic\" arch: \"amd64\" Family: \"unix\"" );
93      }
94  
95      @Test
96      public void testFileInJarPresent()
97          throws VerificationException
98      {
99          //File file = new File( "src/test/resources/mshared104.jar!fud.xml" );
100         Verifier verifier = new Verifier( "src/test/resources" );
101         verifier.verifyFilePresent( "mshared104.jar!/pom.xml" );
102         verifier.verifyFileNotPresent( "mshared104.jar!/fud.xml" );
103         verifier.resetStreams();
104     }
105 
106     @Test
107     public void testStripAnsi()
108     {
109         assertEquals( "--- plugin:version:goal (id) @ artifactId ---",
110                       Verifier.stripAnsi( "\u001B[1m--- \u001B[0;32mplugin:version:goal\u001B[0;1m (id)\u001B[m @ "
111                                               + "\u001B[36martifactId\u001B[0;1m ---\u001B[m" ) );
112     }
113 
114     @Test
115     public void testLoadPropertiesFNFE()
116     {
117         VerificationException exception = assertThrows( VerificationException.class, () -> {
118             Verifier verifier = new Verifier( "src/test/resources" );
119             try
120             {
121                 verifier.loadProperties( "unknown.properties" );
122             }
123             finally
124             {
125                 verifier.resetStreams();
126             }
127         } );
128         assertInstanceOf( FileNotFoundException.class, exception.getCause() );
129     }
130 
131     @Test
132     public void testDedicatedMavenHome() throws VerificationException, IOException
133     {
134         String mavenHome = Paths.get( "src/test/resources/maven-home" ).toAbsolutePath().toString();
135         Verifier verifier = new Verifier( temporaryDir.toString(), null, false, mavenHome );
136         verifier.executeGoal( "some-goal" );
137         verifier.resetStreams();
138         Path logFile = Paths.get( verifier.getBasedir(), verifier.getLogFileName() );
139         ForkedLauncherTest.expectFileLine( logFile, "Hello World from Maven Home" );
140     }
141 
142     @Test
143     void testDefaultFilterMap() throws VerificationException
144     {
145         Verifier verifier = new Verifier( "src/test/resources" );
146         Map<String, String> filterMap = verifier.newDefaultFilterMap();
147         verifier.resetStreams();
148 
149         assertEquals( 2, filterMap.size() );
150         assertTrue( filterMap.containsKey( "@basedir@" ) );
151         assertTrue( filterMap.containsKey( "@baseurl@" ) );
152     }
153 
154     @Test
155     void testDefaultMavenArgument() throws VerificationException
156     {
157         TestVerifier verifier = new TestVerifier( "src/test/resources" );
158 
159         verifier.executeGoal( "test" );
160         verifier.resetStreams();
161 
162         assertThat( verifier.launcher.cliArgs, arrayContaining(
163             "-e", "--batch-mode", "-Dmaven.repo.local=test-local-repo",
164             "org.apache.maven.plugins:maven-clean-plugin:clean", "test" ) );
165     }
166 
167     @Test
168     void testDefaultMavenArgumentWithExecuteMethod() throws VerificationException
169     {
170         TestVerifier verifier = new TestVerifier( "src/test/resources" );
171 
172         verifier.addCliArgument( "test" );
173         verifier.execute();
174         verifier.resetStreams();
175 
176         assertThat( verifier.launcher.cliArgs, arrayContaining(
177             "-e", "--batch-mode", "-Dmaven.repo.local=test-local-repo",
178             "org.apache.maven.plugins:maven-clean-plugin:clean", "test" ) );
179     }
180 
181     public static Stream<Arguments> argumentsForTest()
182     {
183         return Stream.of(
184             arguments( "test-argument", "test-argument" ),
185             arguments( "test1/${basedir}/test2/${basedir}", "test1/src/test/resources/test2/src/test/resources" ),
186             arguments( "argument with space", "argument with space" )
187         );
188     }
189 
190     @ParameterizedTest
191     @MethodSource( "argumentsForTest" )
192     void argumentShouldBePassedAsIs( String inputArgument, String expectedArgument ) throws VerificationException
193     {
194         TestVerifier verifier = new TestVerifier( "src/test/resources" );
195 
196         verifier.addCliArgument( inputArgument );
197         verifier.executeGoal( "test" );
198         verifier.resetStreams();
199 
200         assertThat( verifier.launcher.cliArgs, hasItemInArray( expectedArgument ) );
201     }
202 
203     @Test
204     void addCliArgsShouldAddSeparateArguments() throws VerificationException
205     {
206         TestVerifier verifier = new TestVerifier( "src/test/resources" );
207 
208         verifier.addCliArguments( "cliArg1", "cliArg2" );
209         verifier.executeGoal( "test" );
210         verifier.resetStreams();
211 
212         assertThat( verifier.launcher.cliArgs, allOf(
213             hasItemInArray( "cliArg1" ), hasItemInArray( "cliArg2" ) ) );
214 
215     }
216 
217     private static class TestMavenLauncher implements MavenLauncher
218     {
219         String[] cliArgs;
220 
221         @Override
222         public int run( String[] cliArgs, Properties systemProperties, String workingDirectory, File logFile )
223             throws IOException, LauncherException
224         {
225             this.cliArgs = cliArgs;
226             return 0;
227         }
228 
229         @Override
230         public String getMavenVersion() throws IOException, LauncherException
231         {
232             return null;
233         }
234     }
235 
236     private static class TestVerifier extends Verifier
237     {
238         TestMavenLauncher launcher;
239 
240         public TestVerifier( String basedir ) throws VerificationException
241         {
242             super( basedir );
243             setLocalRepo( "test-local-repo" );
244             launcher = new TestMavenLauncher();
245         }
246 
247         @Override
248         protected MavenLauncher getMavenLauncher( Map<String, String> envVars ) throws LauncherException
249         {
250             return launcher;
251         }
252     }
253 }