1 package org.apache.maven.shared.verifier;
2
3 import static org.hamcrest.MatcherAssert.assertThat;
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
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
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 }