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.plugins.invoker;
20  
21  import java.io.File;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.apache.maven.plugins.invoker.model.BuildJob;
26  import org.apache.maven.project.MavenProject;
27  import org.apache.maven.settings.Settings;
28  import org.junit.jupiter.api.Test;
29  
30  import static org.assertj.core.api.Assertions.assertThat;
31  
32  /**
33   * @author Olivier Lamy
34   * @since 18 nov. 07
35   */
36  class InvokerMojoTest extends AbstractTestUtil {
37  
38      private static final String DUMMY_PROJECT = "dummy" + File.separator + "pom.xml";
39      private static final String WITH_POM_DIR_PROJECT = "with-pom-project-dir" + File.separator + "pom.xml";
40      private static final String INTERPOLATION_PROJECT = "interpolation" + File.separator + "pom.xml";
41      private static final String WITHOUT_POM_PROJECT = "without-pom-project-dir";
42  
43      private MavenProject getMavenProject() {
44          MavenProject mavenProject = new MavenProject();
45          mavenProject.setFile(new File("target/foo.txt"));
46          return mavenProject;
47      }
48  
49      @Test
50      void testSingleInvokerTest() throws Exception {
51          // given
52          MavenProject mavenProject = getMavenProject();
53          InvokerMojo invokerMojo = new InvokerMojo();
54          String dirPath = getBasedir() + "/src/test/resources/unit";
55          setVariableValueToObject(invokerMojo, "projectsDirectory", new File(dirPath));
56          setVariableValueToObject(invokerMojo, "invokerPropertiesFile", "invoker.properties");
57          setVariableValueToObject(invokerMojo, "project", mavenProject);
58          setVariableValueToObject(invokerMojo, "interpolatorUtils", new InterpolatorUtils(mavenProject));
59          setVariableValueToObject(invokerMojo, "invokerTest", "*dummy*");
60          setVariableValueToObject(invokerMojo, "settings", new Settings());
61  
62          // when
63          List<BuildJob> jobs = invokerMojo.getBuildJobs();
64  
65          // then
66          assertThat(jobs).map(BuildJob::getProject).containsExactlyInAnyOrder(DUMMY_PROJECT);
67      }
68  
69      @Test
70      void testMultiInvokerTest() throws Exception {
71          // given
72          MavenProject mavenProject = getMavenProject();
73          InvokerMojo invokerMojo = new InvokerMojo();
74          String dirPath = getBasedir() + "/src/test/resources/unit";
75          setVariableValueToObject(invokerMojo, "projectsDirectory", new File(dirPath));
76          setVariableValueToObject(invokerMojo, "invokerPropertiesFile", "invoker.properties");
77          setVariableValueToObject(invokerMojo, "project", mavenProject);
78          setVariableValueToObject(invokerMojo, "interpolatorUtils", new InterpolatorUtils(mavenProject));
79          setVariableValueToObject(invokerMojo, "invokerTest", "*dummy*,*terpolatio*");
80          setVariableValueToObject(invokerMojo, "settings", new Settings());
81  
82          // when
83          List<BuildJob> jobs = invokerMojo.getBuildJobs();
84  
85          // then
86          assertThat(jobs).map(BuildJob::getProject).containsExactlyInAnyOrder(DUMMY_PROJECT, INTERPOLATION_PROJECT);
87      }
88  
89      @Test
90      void testFullPatternInvokerTest() throws Exception {
91          // given
92          MavenProject mavenProject = getMavenProject();
93          InvokerMojo invokerMojo = new InvokerMojo();
94          String dirPath = getBasedir() + "/src/test/resources/unit";
95          setVariableValueToObject(invokerMojo, "projectsDirectory", new File(dirPath));
96          setVariableValueToObject(invokerMojo, "invokerPropertiesFile", "invoker.properties");
97          setVariableValueToObject(invokerMojo, "project", mavenProject);
98          setVariableValueToObject(invokerMojo, "interpolatorUtils", new InterpolatorUtils(mavenProject));
99          setVariableValueToObject(invokerMojo, "invokerTest", "*");
100         setVariableValueToObject(invokerMojo, "settings", new Settings());
101 
102         // when
103         List<BuildJob> jobs = invokerMojo.getBuildJobs();
104 
105         // then
106         assertThat(jobs)
107                 .map(BuildJob::getProject)
108                 .containsExactlyInAnyOrder(
109                         DUMMY_PROJECT, WITH_POM_DIR_PROJECT, WITHOUT_POM_PROJECT, INTERPOLATION_PROJECT);
110     }
111 
112     @Test
113     void testSetupInProjectList() throws Exception {
114         // given
115         MavenProject mavenProject = getMavenProject();
116         InvokerMojo invokerMojo = new InvokerMojo();
117         String dirPath = getBasedir() + "/src/test/resources/unit";
118         setVariableValueToObject(invokerMojo, "projectsDirectory", new File(dirPath));
119         setVariableValueToObject(invokerMojo, "invokerPropertiesFile", "invoker.properties");
120         setVariableValueToObject(invokerMojo, "project", mavenProject);
121         setVariableValueToObject(invokerMojo, "interpolatorUtils", new InterpolatorUtils(mavenProject));
122         setVariableValueToObject(invokerMojo, "settings", new Settings());
123         setVariableValueToObject(invokerMojo, "setupIncludes", Collections.singletonList("dum*/pom.xml"));
124 
125         // when
126         List<BuildJob> jobs = invokerMojo.getBuildJobs();
127 
128         // then
129 
130         // we have all projects with pom.xml
131         assertThat(jobs)
132                 .map(BuildJob::getProject)
133                 .containsExactlyInAnyOrder(DUMMY_PROJECT, WITH_POM_DIR_PROJECT, INTERPOLATION_PROJECT);
134 
135         // and we have one setup project
136         assertThat(jobs)
137                 .filteredOn(job -> BuildJob.Type.SETUP.equals(job.getType()))
138                 .map(BuildJob::getProject)
139                 .containsExactlyInAnyOrder(DUMMY_PROJECT);
140     }
141 
142     @Test
143     void testSetupProjectIsFiltered() throws Exception {
144         // given
145         MavenProject mavenProject = getMavenProject();
146         InvokerMojo invokerMojo = new InvokerMojo();
147         String dirPath = getBasedir() + "/src/test/resources/unit";
148         setVariableValueToObject(invokerMojo, "projectsDirectory", new File(dirPath));
149         setVariableValueToObject(invokerMojo, "invokerPropertiesFile", "invoker.properties");
150         setVariableValueToObject(invokerMojo, "project", mavenProject);
151         setVariableValueToObject(invokerMojo, "interpolatorUtils", new InterpolatorUtils(mavenProject));
152         setVariableValueToObject(invokerMojo, "settings", new Settings());
153         setVariableValueToObject(invokerMojo, "setupIncludes", Collections.singletonList("dum*/pom.xml"));
154         setVariableValueToObject(invokerMojo, "invokerTest", "*project-dir*");
155 
156         // when
157         List<BuildJob> jobs = invokerMojo.getBuildJobs();
158 
159         // then
160 
161         // we have filtered projects
162         assertThat(jobs).map(BuildJob::getProject).containsExactlyInAnyOrder(WITH_POM_DIR_PROJECT, WITHOUT_POM_PROJECT);
163 
164         // and we don't have a setup project
165         assertThat(jobs)
166                 .filteredOn(job -> BuildJob.Type.SETUP.equals(job.getType()))
167                 .isEmpty();
168     }
169 
170     @Test
171     void testAlreadyCloned() {
172         assertThat(AbstractInvokerMojo.alreadyCloned("dir", Collections.emptyList()))
173                 .isFalse();
174         assertThat(AbstractInvokerMojo.alreadyCloned("dir", Collections.singletonList("dir")))
175                 .isTrue();
176         assertThat(AbstractInvokerMojo.alreadyCloned("dir" + File.separator + "sub", Collections.singletonList("dir")))
177                 .isTrue();
178         assertThat(AbstractInvokerMojo.alreadyCloned("dirs", Collections.singletonList("dir")))
179                 .isFalse();
180     }
181 
182     @Test
183     void testParallelThreadsSettings() throws Exception {
184         Object[][] testValues = {
185             {"4", 4},
186             {"1C", Runtime.getRuntime().availableProcessors()},
187             {"2.5C", (int) (Double.parseDouble("2.5") * Runtime.getRuntime().availableProcessors())}
188         };
189 
190         InvokerMojo invokerMojo = new InvokerMojo();
191 
192         for (Object[] testValue : testValues) {
193             String parallelThreads = (String) testValue[0];
194             int expectedParallelThreads = (Integer) testValue[1];
195 
196             setVariableValueToObject(invokerMojo, "parallelThreads", parallelThreads);
197 
198             assertThat(expectedParallelThreads).isEqualTo(invokerMojo.getParallelThreadsCount());
199         }
200     }
201 }