View Javadoc
1   package org.apache.maven.plugins.invoker;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.assertj.core.api.Assertions.assertThat;
23  
24  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
25  import org.apache.maven.plugins.invoker.model.BuildJob;
26  import org.apache.maven.project.MavenProject;
27  import org.apache.maven.settings.Settings;
28  
29  import java.io.File;
30  import java.util.Collections;
31  import java.util.List;
32  
33  /**
34   * @author Olivier Lamy
35   * @since 18 nov. 07
36   */
37  public class InvokerMojoTest extends AbstractMojoTestCase
38  {
39  
40      private static final String DUMMY_PROJECT = "dummy" + File.separator + "pom.xml";
41      private static final String WITH_POM_DIR_PROJECT = "with-pom-project-dir" + File.separator + "pom.xml";
42      private static final String INTERPOLATION_PROJECT = "interpolation" + File.separator + "pom.xml";
43      private static final String WITHOUT_POM_PROJECT = "without-pom-project-dir";
44  
45      private MavenProject getMavenProject()
46      {
47          MavenProject mavenProject = new MavenProject();
48          mavenProject.setFile( new File( "target/foo.txt" ) );
49          return mavenProject;
50      }
51  
52      public void testSingleInvokerTest() throws Exception
53      {
54          // given
55          InvokerMojo invokerMojo = new InvokerMojo();
56          String dirPath = getBasedir() + "/src/test/resources/unit";
57          setVariableValueToObject( invokerMojo, "projectsDirectory", new File( dirPath ) );
58          setVariableValueToObject( invokerMojo, "invokerPropertiesFile", "invoker.properties" );
59          setVariableValueToObject( invokerMojo, "project", getMavenProject() );
60          setVariableValueToObject( invokerMojo, "invokerTest", "*dummy*" );
61          setVariableValueToObject( invokerMojo, "settings", new Settings() );
62  
63          // when
64          List<BuildJob> jobs = invokerMojo.getBuildJobs();
65  
66          // then
67          assertThat( jobs )
68                  .map( BuildJob::getProject )
69                  .containsExactlyInAnyOrder( DUMMY_PROJECT );
70      }
71  
72      public void testMultiInvokerTest() throws Exception
73      {
74          // given
75          InvokerMojo invokerMojo = new InvokerMojo();
76          String dirPath = getBasedir() + "/src/test/resources/unit";
77          setVariableValueToObject( invokerMojo, "projectsDirectory", new File( dirPath ) );
78          setVariableValueToObject( invokerMojo, "invokerPropertiesFile", "invoker.properties" );
79          setVariableValueToObject( invokerMojo, "project", getMavenProject() );
80          setVariableValueToObject( invokerMojo, "invokerTest", "*dummy*,*terpolatio*" );
81          setVariableValueToObject( invokerMojo, "settings", new Settings() );
82  
83          // when
84          List<BuildJob> jobs = invokerMojo.getBuildJobs();
85  
86          // then
87          assertThat( jobs )
88                  .map( BuildJob::getProject )
89                  .containsExactlyInAnyOrder( DUMMY_PROJECT, INTERPOLATION_PROJECT );
90      }
91  
92      public void testFullPatternInvokerTest() throws Exception
93      {
94          // given
95          InvokerMojo invokerMojo = new InvokerMojo();
96          String dirPath = getBasedir() + "/src/test/resources/unit";
97          setVariableValueToObject( invokerMojo, "projectsDirectory", new File( dirPath ) );
98          setVariableValueToObject( invokerMojo, "invokerPropertiesFile", "invoker.properties" );
99          setVariableValueToObject( invokerMojo, "project", getMavenProject() );
100         setVariableValueToObject( invokerMojo, "invokerTest", "*" );
101         setVariableValueToObject( invokerMojo, "settings", new Settings() );
102 
103         // when
104         List<BuildJob> jobs = invokerMojo.getBuildJobs();
105 
106         // then
107         assertThat( jobs )
108                 .map( BuildJob::getProject )
109                 .containsExactlyInAnyOrder(
110                         DUMMY_PROJECT, WITH_POM_DIR_PROJECT, WITHOUT_POM_PROJECT,
111                         INTERPOLATION_PROJECT );
112     }
113 
114     public void testSetupInProjectList() throws Exception
115     {
116         // given
117         InvokerMojo invokerMojo = new InvokerMojo();
118         String dirPath = getBasedir() + "/src/test/resources/unit";
119         setVariableValueToObject( invokerMojo, "projectsDirectory", new File( dirPath ) );
120         setVariableValueToObject( invokerMojo, "invokerPropertiesFile", "invoker.properties" );
121         setVariableValueToObject( invokerMojo, "project", getMavenProject() );
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(
134                         DUMMY_PROJECT, WITH_POM_DIR_PROJECT, INTERPOLATION_PROJECT );
135 
136         // and we have one setup project
137         assertThat( jobs )
138                 .filteredOn( job -> BuildJob.Type.SETUP.equals( job.getType() ) )
139                 .map( BuildJob::getProject )
140                 .containsExactlyInAnyOrder(  DUMMY_PROJECT );
141     }
142 
143     public void testSetupProjectIsFiltered() throws Exception
144     {
145         // given
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", getMavenProject() );
151         setVariableValueToObject( invokerMojo, "settings", new Settings() );
152         setVariableValueToObject( invokerMojo, "setupIncludes", Collections.singletonList( "dum*/pom.xml" ) );
153         setVariableValueToObject( invokerMojo, "invokerTest", "*project-dir*" );
154 
155 
156         // when
157         List<BuildJob> jobs = invokerMojo.getBuildJobs();
158 
159         // then
160 
161         // we have filtered projects
162         assertThat( jobs )
163                 .map( BuildJob::getProject )
164                 .containsExactlyInAnyOrder(
165                         WITH_POM_DIR_PROJECT, WITHOUT_POM_PROJECT );
166 
167         // and we don't have a setup project
168         assertThat( jobs )
169                 .filteredOn( job -> BuildJob.Type.SETUP.equals( job.getType() ) )
170                 .isEmpty();
171     }
172 
173     public void testAlreadyCloned()
174     {
175         assertFalse( AbstractInvokerMojo.alreadyCloned( "dir", Collections.emptyList() ) );
176         assertTrue( AbstractInvokerMojo.alreadyCloned( "dir", Collections.singletonList( "dir" ) ) );
177         assertTrue( AbstractInvokerMojo.alreadyCloned( "dir" + File.separator + "sub",
178                 Collections.singletonList( "dir" ) ) );
179         assertFalse( AbstractInvokerMojo.alreadyCloned( "dirs", Collections.singletonList( "dir" ) ) );
180     }
181 
182     public void testParallelThreadsSettings() throws IllegalAccessException
183     {
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         {
194             String parallelThreads = (String) testValue[0];
195             int expectedParallelThreads = (Integer) testValue[1];
196 
197             setVariableValueToObject( invokerMojo, "parallelThreads", parallelThreads );
198 
199             assertEquals( expectedParallelThreads, invokerMojo.getParallelThreadsCount() );
200         }
201     }
202 }