View Javadoc

1   package org.apache.maven.continuum.scm.queue;
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 org.apache.continuum.buildmanager.BuildsManager;
23  import org.apache.continuum.buildmanager.ParallelBuildsManager;
24  import org.apache.continuum.dao.ProjectScmRootDao;
25  import org.apache.continuum.model.project.ProjectScmRoot;
26  import org.apache.continuum.taskqueue.BuildProjectTask;
27  import org.apache.continuum.taskqueue.PrepareBuildProjectsTask;
28  import org.apache.maven.continuum.AbstractContinuumTest;
29  import org.apache.maven.continuum.configuration.ConfigurationService;
30  import org.apache.maven.continuum.core.action.AbstractContinuumAction;
31  import org.apache.maven.continuum.model.project.BuildDefinition;
32  import org.apache.maven.continuum.model.project.Project;
33  import org.apache.maven.continuum.model.project.ProjectGroup;
34  import org.apache.maven.continuum.project.ContinuumProjectState;
35  import org.apache.maven.continuum.project.builder.ContinuumProjectBuilder;
36  import org.apache.maven.continuum.project.builder.ContinuumProjectBuilderException;
37  import org.apache.maven.continuum.project.builder.ContinuumProjectBuildingResult;
38  import org.apache.maven.continuum.project.builder.maven.MavenTwoContinuumProjectBuilder;
39  import org.codehaus.plexus.action.ActionManager;
40  import org.codehaus.plexus.util.StringUtils;
41  
42  import java.io.File;
43  import java.io.IOException;
44  import java.util.HashMap;
45  import java.util.List;
46  import java.util.Map;
47  
48  public class PrepareBuildProjectsTaskExecutorTest
49      extends AbstractContinuumTest
50  {
51      private ContinuumProjectBuilder projectBuilder;
52  
53      private ActionManager actionManager;
54  
55      private ProjectScmRootDao projectScmRootDao;
56  
57      private ConfigurationService configurationService;
58  
59      private BuildsManager buildsManager;
60  
61      @Override
62      protected void setUp()
63          throws Exception
64      {
65          super.setUp();
66  
67          projectBuilder = (ContinuumProjectBuilder) lookup( ContinuumProjectBuilder.ROLE,
68                                                             MavenTwoContinuumProjectBuilder.ID );
69  
70          projectScmRootDao = (ProjectScmRootDao) lookup( ProjectScmRootDao.class.getName() );
71  
72          actionManager = (ActionManager) lookup( ActionManager.ROLE );
73  
74          configurationService = (ConfigurationService) lookup( "configurationService" );
75  
76          buildsManager = (ParallelBuildsManager) lookup( BuildsManager.class, "parallel" );
77      }
78  
79      public void testCheckoutPrepareBuildMultiModuleProject()
80          throws Exception
81      {
82          PrepareBuildProjectsTask task = createTask( "src/test-projects/multi-module/pom.xml", false, false );
83  
84          List<Project> projects = getProjectDao().getProjectsInGroup( task.getProjectGroupId() );
85  
86          assertEquals( "failed to add all projects", 4, projects.size() );
87  
88          Project rootProject = getProjectDao().getProjectByName( "multi-module-parent" );
89          Project moduleA = getProjectDao().getProjectByName( "module-A" );
90          Project moduleB = getProjectDao().getProjectByName( "module-B" );
91          Project moduleD = getProjectDao().getProjectByName( "module-D" );
92  
93          buildsManager.prepareBuildProjects( task.getProjectsBuildDefinitionsMap(), task.getBuildTrigger(),
94                                              task.getProjectGroupId(), task.getProjectGroupName(),
95                                              task.getScmRootAddress(), task.getProjectScmRootId() );
96  
97          // wait while task finishes prepare build
98          waitForPrepareBuildToFinish( task.getProjectGroupId(), task.getProjectScmRootId() );
99  
100         ProjectScmRoot scmRoot = projectScmRootDao.getProjectScmRoot( task.getProjectScmRootId() );
101         assertEquals( "Failed to update multi-module project", ContinuumProjectState.UPDATED, scmRoot.getState() );
102 
103         File workingDir = configurationService.getWorkingDirectory();
104 
105         assertTrue( "checkout directory of project 'multi-module-parent' does not exist.", new File( workingDir,
106                                                                                                      Integer.toString(
107                                                                                                          rootProject.getId() ) ).exists() );
108 
109         assertTrue( "checkout directory of project 'module-A' does not exist.", new File( workingDir, Integer.toString(
110             moduleA.getId() ) ).exists() );
111 
112         assertTrue( "checkout directory of project 'module-B' does not exist.", new File( workingDir, Integer.toString(
113             moduleB.getId() ) ).exists() );
114 
115         assertTrue( "checkout directory of project 'module-D' does not exist.", new File( workingDir, Integer.toString(
116             moduleD.getId() ) ).exists() );
117 
118         assertTrue( "failed to checkout project 'multi-module-parent'", new File( workingDir, Integer.toString(
119             rootProject.getId() ) ).list().length > 0 );
120 
121         assertTrue( "failed to checkout project 'module-A'", new File( workingDir, Integer.toString(
122             moduleA.getId() ) ).list().length > 0 );
123 
124         assertTrue( "failed to checkout project 'module-B'", new File( workingDir, Integer.toString(
125             moduleB.getId() ) ).list().length > 0 );
126 
127         assertTrue( "failed to checkout project 'module-D'", new File( workingDir, Integer.toString(
128             moduleD.getId() ) ).list().length > 0 );
129 
130         // wait while task finished build
131         waitForBuildToFinish();
132     }
133 
134     public void testCheckoutPrepareBuildMultiModuleProjectFreshBuild()
135         throws Exception
136     {
137         PrepareBuildProjectsTask task = createTask( "src/test-projects/multi-module/pom.xml", false, true );
138 
139         List<Project> projects = getProjectDao().getProjectsInGroup( task.getProjectGroupId() );
140 
141         assertEquals( "failed to add all projects", 4, projects.size() );
142 
143         Project rootProject = getProjectDao().getProjectByName( "multi-module-parent" );
144         Project moduleA = getProjectDao().getProjectByName( "module-A" );
145         Project moduleB = getProjectDao().getProjectByName( "module-B" );
146         Project moduleD = getProjectDao().getProjectByName( "module-D" );
147 
148         buildsManager.prepareBuildProjects( task.getProjectsBuildDefinitionsMap(), task.getBuildTrigger(),
149                                             task.getProjectGroupId(), task.getProjectGroupName(),
150                                             task.getScmRootAddress(), task.getProjectScmRootId() );
151 
152         // wait while task finishes prepare build
153         waitForPrepareBuildToFinish( task.getProjectGroupId(), task.getProjectScmRootId() );
154 
155         ProjectScmRoot scmRoot = projectScmRootDao.getProjectScmRoot( task.getProjectScmRootId() );
156         assertEquals( "Failed to update multi-module project", ContinuumProjectState.UPDATED, scmRoot.getState() );
157 
158         File workingDir = configurationService.getWorkingDirectory();
159 
160         assertTrue( "checkout directory of project 'multi-module-parent' does not exist.", new File( workingDir,
161                                                                                                      Integer.toString(
162                                                                                                          rootProject.getId() ) ).exists() );
163 
164         assertTrue( "checkout directory of project 'module-A' does not exist.", new File( workingDir, Integer.toString(
165             moduleA.getId() ) ).exists() );
166 
167         assertTrue( "checkout directory of project 'module-B' does not exist.", new File( workingDir, Integer.toString(
168             moduleB.getId() ) ).exists() );
169 
170         assertTrue( "checkout directory of project 'module-D' does not exist.", new File( workingDir, Integer.toString(
171             moduleD.getId() ) ).exists() );
172 
173         assertTrue( "failed to checkout project 'multi-module-parent'", new File( workingDir, Integer.toString(
174             rootProject.getId() ) ).list().length > 0 );
175 
176         assertTrue( "failed to checkout project 'module-A'", new File( workingDir, Integer.toString(
177             moduleA.getId() ) ).list().length > 0 );
178 
179         assertTrue( "failed to checkout project 'module-B'", new File( workingDir, Integer.toString(
180             moduleB.getId() ) ).list().length > 0 );
181 
182         assertTrue( "failed to checkout project 'module-D'", new File( workingDir, Integer.toString(
183             moduleD.getId() ) ).list().length > 0 );
184 
185         // wait while task finished build
186         waitForBuildToFinish();
187     }
188 
189     public void testCheckoutPrepareBuildSingleCheckedoutMultiModuleProject()
190         throws Exception
191     {
192         PrepareBuildProjectsTask task = createTask( "src/test-projects/multi-module/pom.xml", true, false );
193 
194         List<Project> projects = getProjectDao().getProjectsInGroup( task.getProjectGroupId() );
195 
196         assertEquals( "failed to add all projects", 4, projects.size() );
197 
198         Project rootProject = getProjectDao().getProjectByName( "multi-module-parent" );
199 
200         buildsManager.prepareBuildProjects( task.getProjectsBuildDefinitionsMap(), task.getBuildTrigger(),
201                                             task.getProjectGroupId(), task.getProjectGroupName(),
202                                             task.getScmRootAddress(), task.getProjectScmRootId() );
203 
204         // wait while task finishes prepare build
205         waitForPrepareBuildToFinish( task.getProjectGroupId(), task.getProjectScmRootId() );
206 
207         ProjectScmRoot scmRoot = projectScmRootDao.getProjectScmRoot( task.getProjectScmRootId() );
208         assertEquals( "Failed to update multi-module project", ContinuumProjectState.UPDATED, scmRoot.getState() );
209 
210         File checkedOutDir = new File( configurationService.getWorkingDirectory(), Integer.toString(
211             rootProject.getId() ) );
212 
213         assertTrue( "checkout directory of project 'multi-module-parent' does not exist.", checkedOutDir.exists() );
214 
215         assertTrue( "module-A was not checked out in the same directory as it's parent.", new File( checkedOutDir,
216                                                                                                     "module-A" ).exists() );
217 
218         assertTrue( "module-B was not checked out in the same directory as it's parent.", new File( checkedOutDir,
219                                                                                                     "module-B" ).exists() );
220 
221         assertTrue( "module-D was not checked out in the same directory as it's parent.", new File( checkedOutDir,
222                                                                                                     "module-C/module-D" ).exists() );
223 
224         assertTrue( "failed to checkout project 'multi-module-parent'", checkedOutDir.list().length > 0 );
225 
226         assertTrue( "failed to checkout project 'module-A'", new File( checkedOutDir, "module-A" ).list().length > 0 );
227 
228         assertTrue( "failed to checkout project 'module-B'", new File( checkedOutDir, "module-B" ).list().length > 0 );
229 
230         assertTrue( "failed to checkout project 'module-D'", new File( checkedOutDir,
231                                                                        "module-C/module-D" ).list().length > 0 );
232 
233         // wait while task finishes build
234         waitForBuildToFinish();
235     }
236 
237     public void testCheckoutPrepareBuildSingleCheckedoutMultiModuleProjectFreshBuild()
238         throws Exception
239     {
240         PrepareBuildProjectsTask task = createTask( "src/test-projects/multi-module/pom.xml", true, true );
241 
242         List<Project> projects = getProjectDao().getProjectsInGroup( task.getProjectGroupId() );
243 
244         assertEquals( "failed to add all projects", 4, projects.size() );
245 
246         Project rootProject = getProjectDao().getProjectByName( "multi-module-parent" );
247 
248         buildsManager.prepareBuildProjects( task.getProjectsBuildDefinitionsMap(), task.getBuildTrigger(),
249                                             task.getProjectGroupId(), task.getProjectGroupName(),
250                                             task.getScmRootAddress(), task.getProjectScmRootId() );
251 
252         // wait while task finishes prepare build
253         waitForPrepareBuildToFinish( task.getProjectGroupId(), task.getProjectScmRootId() );
254 
255         ProjectScmRoot scmRoot = projectScmRootDao.getProjectScmRoot( task.getProjectScmRootId() );
256         assertEquals( "Failed to update multi-module project", ContinuumProjectState.UPDATED, scmRoot.getState() );
257 
258         File checkedOutDir = new File( configurationService.getWorkingDirectory(), Integer.toString(
259             rootProject.getId() ) );
260 
261         assertTrue( "checkout directory of project 'multi-module-parent' does not exist.", checkedOutDir.exists() );
262 
263         assertTrue( "module-A was not checked out in the same directory as it's parent.", new File( checkedOutDir,
264                                                                                                     "module-A" ).exists() );
265 
266         assertTrue( "module-B was not checked out in the same directory as it's parent.", new File( checkedOutDir,
267                                                                                                     "module-B" ).exists() );
268 
269         assertTrue( "module-D was not checked out in the same directory as it's parent.", new File( checkedOutDir,
270                                                                                                     "module-C/module-D" ).exists() );
271 
272         assertTrue( "failed to checkout project 'multi-module-parent'", checkedOutDir.list().length > 0 );
273 
274         assertTrue( "failed to checkout project 'module-A'", new File( checkedOutDir, "module-A" ).list().length > 0 );
275 
276         assertTrue( "failed to checkout project 'module-B'", new File( checkedOutDir, "module-B" ).list().length > 0 );
277 
278         assertTrue( "failed to checkout project 'module-D'", new File( checkedOutDir,
279                                                                        "module-C/module-D" ).list().length > 0 );
280 
281         // wait while task finishes build
282         waitForBuildToFinish();
283     }
284 
285     public void testCheckoutPrepareBuildSingleCheckoutFlatMultiModuleProject()
286         throws Exception
287     {
288         PrepareBuildProjectsTask task = createTask( "src/test-projects/flat-multi-module/parent-project/pom.xml", true,
289                                                     false );
290 
291         List<Project> projects = getProjectDao().getProjectsInGroup( task.getProjectGroupId() );
292 
293         assertEquals( "failed to add all projects", 4, projects.size() );
294 
295         Project rootProject = getProjectDao().getProjectByName( "parent-project" );
296 
297         buildsManager.prepareBuildProjects( task.getProjectsBuildDefinitionsMap(), task.getBuildTrigger(),
298                                             task.getProjectGroupId(), task.getProjectGroupName(),
299                                             task.getScmRootAddress(), task.getProjectScmRootId() );
300 
301         // wait while task finishes prepare build
302         waitForPrepareBuildToFinish( task.getProjectGroupId(), task.getProjectScmRootId() );
303 
304         ProjectScmRoot scmRoot = projectScmRootDao.getProjectScmRoot( task.getProjectScmRootId() );
305         assertEquals( "Failed to update multi-module project", ContinuumProjectState.UPDATED, scmRoot.getState() );
306 
307         File checkedOutDir = new File( configurationService.getWorkingDirectory(), Integer.toString(
308             rootProject.getId() ) );
309 
310         assertTrue( "checkout directory of project 'parent-project' does not exist.", new File( checkedOutDir,
311                                                                                                 "parent-project" ).exists() );
312 
313         assertTrue( "module-a was not checked out in the same directory as it's parent.", new File( checkedOutDir,
314                                                                                                     "module-a" ).exists() );
315 
316         assertTrue( "module-b was not checked out in the same directory as it's parent.", new File( checkedOutDir,
317                                                                                                     "module-b" ).exists() );
318 
319         assertTrue( "module-d was not checked out in the same directory as it's parent.", new File( checkedOutDir,
320                                                                                                     "module-c/module-d" ).exists() );
321 
322         assertTrue( "failed to checkout parent-project", new File( checkedOutDir, "parent-project" ).list().length >
323             0 );
324 
325         assertTrue( "failed to checkout module-a", new File( checkedOutDir, "module-a" ).list().length > 0 );
326 
327         assertTrue( "failed to checkout module-b", new File( checkedOutDir, "module-b" ).list().length > 0 );
328 
329         assertTrue( "failed to checkout module-d", new File( checkedOutDir, "module-c/module-d" ).list().length > 0 );
330 
331         // wait while task finishes build
332         waitForPrepareBuildToFinish( task.getProjectGroupId(), task.getProjectScmRootId() );
333     }
334 
335     public void testCheckoutPrepareBuildSingleCheckoutFlatMultiModuleProjectBuildFresh()
336         throws Exception
337     {
338         PrepareBuildProjectsTask task = createTask( "src/test-projects/flat-multi-module/parent-project/pom.xml", true,
339                                                     true );
340 
341         List<Project> projects = getProjectDao().getProjectsInGroup( task.getProjectGroupId() );
342 
343         assertEquals( "failed to add all projects", 4, projects.size() );
344 
345         Project rootProject = getProjectDao().getProjectByName( "parent-project" );
346 
347         buildsManager.prepareBuildProjects( task.getProjectsBuildDefinitionsMap(), task.getBuildTrigger(),
348                                             task.getProjectGroupId(), task.getProjectGroupName(),
349                                             task.getScmRootAddress(), task.getProjectScmRootId() );
350 
351         // wait while task finishes prepare build
352         waitForPrepareBuildToFinish( task.getProjectGroupId(), task.getProjectScmRootId() );
353 
354         ProjectScmRoot scmRoot = projectScmRootDao.getProjectScmRoot( task.getProjectScmRootId() );
355         assertEquals( "Failed to update multi-module project", ContinuumProjectState.UPDATED, scmRoot.getState() );
356 
357         File checkedOutDir = new File( configurationService.getWorkingDirectory(), Integer.toString(
358             rootProject.getId() ) );
359 
360         assertTrue( "checkout directory of project 'parent-project' does not exist.", new File( checkedOutDir,
361                                                                                                 "parent-project" ).exists() );
362 
363         assertTrue( "module-a was not checked out in the same directory as it's parent.", new File( checkedOutDir,
364                                                                                                     "module-a" ).exists() );
365 
366         assertTrue( "module-b was not checked out in the same directory as it's parent.", new File( checkedOutDir,
367                                                                                                     "module-b" ).exists() );
368 
369         assertTrue( "module-d was not checked out in the same directory as it's parent.", new File( checkedOutDir,
370                                                                                                     "module-c/module-d" ).exists() );
371 
372         assertTrue( "failed to checkout parent-project", new File( checkedOutDir, "parent-project" ).list().length >
373             0 );
374 
375         assertTrue( "failed to checkout module-a", new File( checkedOutDir, "module-a" ).list().length > 0 );
376 
377         assertTrue( "failed to checkout module-b", new File( checkedOutDir, "module-b" ).list().length > 0 );
378 
379         assertTrue( "failed to checkout module-d", new File( checkedOutDir, "module-c/module-d" ).list().length > 0 );
380 
381         // wait while task finishes build
382         waitForBuildToFinish();
383     }
384 
385     public void testCheckoutPrepareBuildSingleCheckoutFlatMultiModuleProjectBuildFreshAfterRemovingWorkingCopy()
386         throws Exception
387     {
388         PrepareBuildProjectsTask task = createTask( "src/test-projects/flat-multi-module/parent-project/pom.xml", true,
389                                                     true );
390 
391         List<Project> projects = getProjectDao().getProjectsInGroup( task.getProjectGroupId() );
392 
393         assertEquals( "failed to add all projects", 4, projects.size() );
394 
395         Project rootProject = getProjectDao().getProjectByName( "parent-project" );
396 
397         File rootProjectDir = new File( configurationService.getWorkingDirectory(), Integer.toString(
398             rootProject.getId() ) );
399         rootProjectDir = new File( rootProjectDir, "parent-project" );
400 
401         rootProject.setWorkingDirectory( rootProjectDir.getAbsolutePath() );
402 
403         getProjectDao().updateProject( rootProject );
404 
405         buildsManager.prepareBuildProjects( task.getProjectsBuildDefinitionsMap(), task.getBuildTrigger(),
406                                             task.getProjectGroupId(), task.getProjectGroupName(),
407                                             task.getScmRootAddress(), task.getProjectScmRootId() );
408 
409         // wait while task finishes prepare build
410         waitForPrepareBuildToFinish( task.getProjectGroupId(), task.getProjectScmRootId() );
411 
412         ProjectScmRoot scmRoot = projectScmRootDao.getProjectScmRoot( task.getProjectScmRootId() );
413         assertEquals( "Failed to update multi-module project", ContinuumProjectState.UPDATED, scmRoot.getState() );
414 
415         File checkedOutDir = new File( configurationService.getWorkingDirectory(), Integer.toString(
416             rootProject.getId() ) );
417 
418         assertTrue( "checkout directory of project 'parent-project' does not exist.", new File( checkedOutDir,
419                                                                                                 "parent-project" ).exists() );
420 
421         assertTrue( "module-a was not checked out in the same directory as it's parent.", new File( checkedOutDir,
422                                                                                                     "module-a" ).exists() );
423 
424         assertTrue( "module-b was not checked out in the same directory as it's parent.", new File( checkedOutDir,
425                                                                                                     "module-b" ).exists() );
426 
427         assertTrue( "module-d was not checked out in the same directory as it's parent.", new File( checkedOutDir,
428                                                                                                     "module-c/module-d" ).exists() );
429 
430         assertTrue( "failed to checkout parent-project", new File( checkedOutDir, "parent-project" ).list().length >
431             0 );
432 
433         assertTrue( "failed to checkout module-a", new File( checkedOutDir, "module-a" ).list().length > 0 );
434 
435         assertTrue( "failed to checkout module-b", new File( checkedOutDir, "module-b" ).list().length > 0 );
436 
437         assertTrue( "failed to checkout module-d", new File( checkedOutDir, "module-c/module-d" ).list().length > 0 );
438 
439         // wait while task finishes build
440         waitForBuildToFinish();
441     }
442 
443     private PrepareBuildProjectsTask createTask( String pomResource, boolean singleCheckout, boolean buildFresh )
444         throws Exception
445     {
446         ProjectGroup projectGroup = getProjectGroup( pomResource, singleCheckout );
447 
448         BuildDefinition buildDefinition = new BuildDefinition();
449         buildDefinition.setId( 0 );
450         buildDefinition.setGoals( "clean" );
451         buildDefinition.setBuildFresh( buildFresh );
452 
453         projectGroup.addBuildDefinition( buildDefinition );
454 
455         Map<String, Object> pgContext = new HashMap<String, Object>();
456 
457         AbstractContinuumAction.setWorkingDirectory( pgContext,
458                                                      configurationService.getWorkingDirectory().getAbsolutePath() );
459 
460         AbstractContinuumAction.setUnvalidatedProjectGroup( pgContext, projectGroup );
461 
462         actionManager.lookup( "validate-project-group" ).execute( pgContext );
463 
464         actionManager.lookup( "store-project-group" ).execute( pgContext );
465 
466         int projectGroupId = AbstractContinuumAction.getProjectGroupId( pgContext );
467 
468         projectGroup = getProjectGroupDao().getProjectGroupWithBuildDetailsByProjectGroupId( projectGroupId );
469 
470         String scmRootUrl = getScmRootUrl( projectGroup );
471 
472         assertNotNull( scmRootUrl );
473 
474         ProjectScmRoot scmRoot = getProjectScmRoot( projectGroup, scmRootUrl );
475 
476         assertNotNull( scmRoot );
477 
478         buildDefinition = (BuildDefinition) projectGroup.getBuildDefinitions().get( 0 );
479 
480         Map<Integer, Integer> map = new HashMap<Integer, Integer>();
481 
482         Project rootProject = null;
483 
484         List<Project> projects = (List<Project>) projectGroup.getProjects();
485 
486         for ( Project project : projects )
487         {
488             assertFalse( project.getId() == 0 );
489 
490             map.put( project.getId(), buildDefinition.getId() );
491 
492             if ( rootProject == null || rootProject.getId() > project.getId() )
493             {
494                 rootProject = project;
495             }
496         }
497 
498         assertEquals( 4, map.size() );
499         PrepareBuildProjectsTask task = new PrepareBuildProjectsTask( map,
500                                                                       new org.apache.continuum.utils.build.BuildTrigger(
501                                                                           1, "user" ), projectGroupId,
502                                                                       projectGroup.getName(),
503                                                                       scmRoot.getScmRootAddress(), scmRoot.getId() );
504 
505         return task;
506     }
507 
508     private ProjectGroup getProjectGroup( String pomResource, boolean singleCheckout )
509         throws ContinuumProjectBuilderException, IOException
510     {
511         File pom = getTestFile( pomResource );
512 
513         assertNotNull( "Can't find project " + pomResource, pom );
514 
515         //ContinuumProjectBuildingResult result = projectBuilder.buildProjectsFromMetadata( pom.toURL(), null, null, true );
516         ContinuumProjectBuildingResult result = projectBuilder.buildProjectsFromMetadata( pom.toURL(), null, null, true,
517                                                                                           singleCheckout );
518 
519         // some assertions to make sure our expectations match. This is NOT
520         // meant as a unit test for the projectbuilder!
521 
522         assertNotNull( "Project list not null", result.getProjects() );
523 
524         assertEquals( "#Projectgroups", 1, result.getProjectGroups().size() );
525 
526         ProjectGroup pg = result.getProjectGroups().get( 0 );
527 
528         if ( !result.getProjects().isEmpty() )
529         {
530             for ( Project p : result.getProjects() )
531             {
532                 pg.addProject( p );
533             }
534         }
535 
536         return pg;
537     }
538 
539     private ProjectScmRoot getProjectScmRoot( ProjectGroup pg, String url )
540         throws Exception
541     {
542         if ( StringUtils.isEmpty( url ) )
543         {
544             return null;
545         }
546 
547         ProjectScmRoot scmRoot = projectScmRootDao.getProjectScmRootByProjectGroupAndScmRootAddress( pg.getId(), url );
548 
549         if ( scmRoot != null )
550         {
551             return scmRoot;
552         }
553 
554         ProjectScmRoot projectScmRoot = new ProjectScmRoot();
555 
556         projectScmRoot.setProjectGroup( pg );
557 
558         projectScmRoot.setScmRootAddress( url );
559 
560         //projectScmRoot.setState( ContinuumProjectState.ERROR );
561 
562         return projectScmRootDao.addProjectScmRoot( projectScmRoot );
563     }
564 
565     private String getScmRootUrl( ProjectGroup pg )
566     {
567         String scmRootUrl = null;
568 
569         for ( Project project : (List<Project>) pg.getProjects() )
570         {
571             String scmUrl = project.getScmUrl();
572 
573             scmRootUrl = getCommonPath( scmUrl, scmRootUrl );
574         }
575 
576         return scmRootUrl;
577     }
578 
579     private String getCommonPath( String path1, String path2 )
580     {
581         if ( path2 == null || path2.equals( "" ) )
582         {
583             return path1;
584         }
585         else
586         {
587             int indexDiff = StringUtils.differenceAt( path1, path2 );
588             return path1.substring( 0, indexDiff );
589         }
590     }
591 
592     private void waitForPrepareBuildToFinish( int projectGroupId, int scmRootId )
593         throws Exception
594     {
595         while ( buildsManager.isInPrepareBuildQueue( projectGroupId, scmRootId ) ||
596             buildsManager.isProjectGroupCurrentlyPreparingBuild( projectGroupId, scmRootId ) )
597         {
598             Thread.sleep( 10 );
599         }
600     }
601 
602     private void waitForBuildToFinish()
603         throws Exception
604     {
605         while ( buildsManager.isBuildInProgress() || isAnyProjectInBuildQueue() )
606         {
607             Thread.sleep( 10 );
608         }
609     }
610 
611     private boolean isAnyProjectInBuildQueue()
612         throws Exception
613     {
614         Map<String, List<BuildProjectTask>> buildTasks = buildsManager.getProjectsInBuildQueues();
615 
616         for ( String queue : buildTasks.keySet() )
617         {
618             if ( !buildTasks.get( queue ).isEmpty() )
619             {
620                 return true;
621             }
622         }
623 
624         return false;
625     }
626 }