1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
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
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
63 List<BuildJob> jobs = invokerMojo.getBuildJobs();
64
65
66 assertThat(jobs).map(BuildJob::getProject).containsExactlyInAnyOrder(DUMMY_PROJECT);
67 }
68
69 @Test
70 void testMultiInvokerTest() throws Exception {
71
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
83 List<BuildJob> jobs = invokerMojo.getBuildJobs();
84
85
86 assertThat(jobs).map(BuildJob::getProject).containsExactlyInAnyOrder(DUMMY_PROJECT, INTERPOLATION_PROJECT);
87 }
88
89 @Test
90 void testFullPatternInvokerTest() throws Exception {
91
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
103 List<BuildJob> jobs = invokerMojo.getBuildJobs();
104
105
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
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
126 List<BuildJob> jobs = invokerMojo.getBuildJobs();
127
128
129
130
131 assertThat(jobs)
132 .map(BuildJob::getProject)
133 .containsExactlyInAnyOrder(DUMMY_PROJECT, WITH_POM_DIR_PROJECT, INTERPOLATION_PROJECT);
134
135
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
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
157 List<BuildJob> jobs = invokerMojo.getBuildJobs();
158
159
160
161
162 assertThat(jobs).map(BuildJob::getProject).containsExactlyInAnyOrder(WITH_POM_DIR_PROJECT, WITHOUT_POM_PROJECT);
163
164
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 }