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.cling.invoker.mvn;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.nio.file.Files;
23  import java.nio.file.Path;
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.Collection;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.apache.maven.api.cli.Invoker;
32  import org.apache.maven.api.cli.Parser;
33  import org.apache.maven.api.cli.ParserRequest;
34  import org.apache.maven.jline.JLineMessageBuilderFactory;
35  import org.codehaus.plexus.classworlds.ClassWorld;
36  
37  import static org.junit.jupiter.api.Assertions.assertEquals;
38  
39  public abstract class MavenInvokerTestSupport {
40      static {
41          System.setProperty(
42                  "library.jline.path",
43                  Path.of("target/dependency/org/jline/nativ").toAbsolutePath().toString());
44      }
45  
46      public static final String POM_STRING =
47              """
48                  <?xml version="1.0" encoding="UTF-8"?>
49                  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
50                           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
51  
52                      <modelVersion>4.0.0</modelVersion>
53  
54                      <groupId>org.apache.maven.samples</groupId>
55                      <artifactId>sample</artifactId>
56                      <version>1.0.0</version>
57  
58                      <dependencyManagement>
59                        <dependencies>
60                          <dependency>
61                            <groupId>org.junit</groupId>
62                            <artifactId>junit-bom</artifactId>
63                            <version>5.11.1</version>
64                            <type>pom</type>
65                            <scope>import</scope>
66                          </dependency>
67                        </dependencies>
68                      </dependencyManagement>
69  
70                      <dependencies>
71                        <dependency>
72                          <groupId>org.junit.jupiter</groupId>
73                          <artifactId>junit-jupiter-api</artifactId>
74                          <scope>test</scope>
75                        </dependency>
76                      </dependencies>
77  
78                  </project>
79                  """;
80  
81      public static final String APP_JAVA_STRING =
82              """
83              package org.apache.maven.samples.sample;
84  
85              public class App {
86                  public static void main(String... args) {
87                      System.out.println("Hello World!");
88                  }
89              }
90              """;
91  
92      protected Map<String, String> invoke(Path cwd, Path userHome, Collection<String> goals, Collection<String> args)
93              throws Exception {
94          Files.createDirectories(cwd.resolve(".mvn"));
95          Path pom = cwd.resolve("pom.xml").toAbsolutePath();
96          Files.writeString(pom, POM_STRING);
97          Path appJava = cwd.resolve("src/main/java/org/apache/maven/samples/sample/App.java");
98          Files.createDirectories(appJava.getParent());
99          Files.writeString(appJava, APP_JAVA_STRING);
100 
101         MimirInfuser.infuse(userHome);
102 
103         HashMap<String, String> logs = new HashMap<>();
104         Parser parser = createParser();
105         try (ClassWorld classWorld = createClassWorld();
106                 Invoker invoker = createInvoker(classWorld)) {
107             for (String goal : goals) {
108                 ByteArrayOutputStream stdout = new ByteArrayOutputStream();
109                 ByteArrayOutputStream stderr = new ByteArrayOutputStream();
110                 List<String> mvnArgs = new ArrayList<>(args);
111                 mvnArgs.add(goal);
112                 int exitCode = -1;
113                 Exception exception = null;
114                 try {
115                     exitCode = invoker.invoke(
116                             parser.parseInvocation(ParserRequest.mvn(mvnArgs, new JLineMessageBuilderFactory())
117                                     .cwd(cwd)
118                                     .userHome(userHome)
119                                     .stdOut(stdout)
120                                     .stdErr(stderr)
121                                     .embedded(true)
122                                     .build()));
123                 } catch (Exception e) {
124                     exception = e;
125                 }
126 
127                 // dump things out
128                 System.out.println("===================================================");
129                 System.out.println("args: " + Arrays.toString(mvnArgs.toArray()));
130                 System.out.println("===================================================");
131                 System.out.println("stdout: " + stdout);
132                 System.out.println("===================================================");
133 
134                 System.err.println("===================================================");
135                 System.err.println("args: " + Arrays.toString(mvnArgs.toArray()));
136                 System.err.println("===================================================");
137                 System.err.println("stderr: " + stderr);
138                 System.err.println("===================================================");
139 
140                 logs.put(goal, stdout.toString());
141                 if (exception != null) {
142                     throw exception;
143                 } else {
144                     assertEquals(0, exitCode, "OUT:" + stdout + "\nERR:" + stderr);
145                 }
146             }
147         }
148         return logs;
149     }
150 
151     protected ClassWorld createClassWorld() {
152         return new ClassWorld("plexus.core", ClassLoader.getSystemClassLoader());
153     }
154 
155     protected abstract Invoker createInvoker(ClassWorld classWorld);
156 
157     protected abstract Parser createParser();
158 }