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