View Javadoc
1   package org.apache.maven.shared.verifier;
2   
3   import static org.hamcrest.MatcherAssert.assertThat;
4   
5   /*
6    * Licensed to the Apache Software Foundation (ASF) under one
7    * or more contributor license agreements.  See the NOTICE file
8    * distributed with this work for additional information
9    * regarding copyright ownership.  The ASF licenses this file
10   * to you under the Apache License, Version 2.0 (the
11   * "License"); you may not use this file except in compliance
12   * with the License.  You may obtain a copy of the License at
13   *
14   *   http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing,
17   * software distributed under the License is distributed on an
18   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   * KIND, either express or implied.  See the License for the
20   * specific language governing permissions and limitations
21   * under the License.
22   */
23  
24  import static org.hamcrest.Matchers.is;
25  import static org.junit.jupiter.api.Assertions.fail;
26  
27  import java.io.BufferedReader;
28  import java.nio.file.Files;
29  import java.io.IOException;
30  import java.nio.charset.StandardCharsets;
31  import java.nio.file.Path;
32  import java.nio.file.Paths;
33  import java.util.ArrayList;
34  import java.util.Collection;
35  import java.util.Collections;
36  import java.util.Properties;
37  
38  import org.junit.jupiter.api.Test;
39  import org.junit.jupiter.api.io.TempDir;
40  
41  
42  public class ForkedLauncherTest
43  {
44      @TempDir
45      public Path temporaryDir;
46      
47      private ForkedLauncher launcher;
48      
49      private final String workingDir = Paths.get( "src/test/resources/wrapper-project" ).toAbsolutePath().toString();
50      
51      @Test
52      public void mvnw() throws Exception
53      {
54          launcher = new ForkedLauncher( ".", Collections.emptyMap(), false, true );
55          Path logFile = temporaryDir.resolve( "build.log" );
56  
57          int exitCode = launcher.run( new String[0], new Properties(), workingDir, logFile.toFile() );
58  
59          // most likely this contains the exception in case exitCode != 0
60          expectFileLine( logFile, "Hello World" );
61  
62          assertThat( "exit code", exitCode, is ( 0 ) );
63      }
64  
65      @Test
66      public void mvnwDebug() throws Exception
67      {
68          launcher = new ForkedLauncher( ".", Collections.emptyMap(), true, true );
69          Path logFile = temporaryDir.resolve( "build.log" );
70  
71          int exitCode = launcher.run( new String[0], new Properties(), workingDir, logFile.toFile() );
72  
73          // most likely this contains the exception in case exitCode != 0
74          expectFileLine( logFile, "Hello World" );
75  
76          assertThat( "exit code", exitCode , is ( 0 ) );
77      }
78  
79      static void expectFileLine( Path file, String expectedline ) throws IOException
80      {
81          try ( BufferedReader br = Files.newBufferedReader( file, StandardCharsets.UTF_8 ) )
82          {
83              Collection<String> text = new ArrayList<>();
84              String line;
85              while ( ( line = br.readLine() ) != null )
86              {
87                  if ( expectedline.equals( line ) )
88                  {
89                      return;
90                  }
91                  text.add( line );
92              }
93  
94              String message = "%s doesn't contain '%s', was:%n%s";
95              fail( String.format( message, file.getFileName(), expectedline, text ) );
96          }
97      }
98  
99  }