1 package org.apache.maven.continuum;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.continuum.buildmanager.BuildsManager;
23 import org.apache.continuum.dao.BuildResultDao;
24 import org.apache.continuum.dao.ProjectDao;
25 import org.apache.continuum.model.release.ContinuumReleaseResult;
26 import org.apache.continuum.model.repository.LocalRepository;
27 import org.apache.continuum.release.config.ContinuumReleaseDescriptor;
28 import org.apache.continuum.repository.RepositoryService;
29 import org.apache.continuum.taskqueue.manager.TaskQueueManager;
30 import org.apache.continuum.utils.build.BuildTrigger;
31 import org.apache.maven.continuum.builddefinition.BuildDefinitionService;
32 import org.apache.maven.continuum.configuration.ConfigurationService;
33 import org.apache.maven.continuum.execution.ContinuumBuildExecutorConstants;
34 import org.apache.maven.continuum.initialization.ContinuumInitializer;
35 import org.apache.maven.continuum.model.project.BuildDefinition;
36 import org.apache.maven.continuum.model.project.BuildResult;
37 import org.apache.maven.continuum.model.project.Project;
38 import org.apache.maven.continuum.model.project.ProjectGroup;
39 import org.apache.maven.continuum.model.project.ProjectNotifier;
40 import org.apache.maven.continuum.project.builder.ContinuumProjectBuildingResult;
41 import org.apache.maven.shared.release.ReleaseResult;
42 import org.jmock.Expectations;
43 import org.jmock.Mockery;
44 import org.jmock.integration.junit3.JUnit3Mockery;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 import java.io.File;
49 import java.util.ArrayList;
50 import java.util.Collection;
51 import java.util.HashMap;
52 import java.util.List;
53 import java.util.Map;
54
55
56
57
58
59 public class DefaultContinuumTest
60 extends AbstractContinuumTest
61 {
62 private static final Logger log = LoggerFactory.getLogger( DefaultContinuumTest.class );
63
64 private Mockery context;
65
66 private TaskQueueManager taskQueueManager;
67
68 private ProjectDao projectDao;
69
70 private BuildResultDao buildResultDao;
71
72 @Override
73 protected void setUp()
74 throws Exception
75 {
76 super.setUp();
77
78 context = new JUnit3Mockery();
79
80 taskQueueManager = context.mock( TaskQueueManager.class );
81
82 projectDao = context.mock( ProjectDao.class );
83 }
84
85 public void testContinuumConfiguration()
86 throws Exception
87 {
88 lookup( Continuum.ROLE );
89 }
90
91 public void testAddMavenTwoProjectSet()
92 throws Exception
93 {
94 Continuum continuum = (Continuum) lookup( Continuum.ROLE );
95
96 int projectCount = getProjectDao().getAllProjectsByName().size();
97
98 int projectGroupCount = getProjectGroupDao().getAllProjectGroupsWithProjects().size();
99
100 File rootPom = getTestFile( "src/test/resources/projects/continuum/continuum-notifiers/pom.xml" );
101
102 assertTrue( rootPom.exists() );
103
104 ContinuumProjectBuildingResult result = continuum.addMavenTwoProject( rootPom.toURI().toURL().toExternalForm(),
105 -1, true, false, true, -1, false );
106
107 assertNotNull( result );
108
109 assertEquals( "result.warnings.size" + result.getErrors(), 0, result.getErrors().size() );
110
111 assertEquals( "result.projects.size", 3, result.getProjects().size() );
112
113 assertEquals( "result.projectGroups.size", 1, result.getProjectGroups().size() );
114
115 log.info( "number of projects: " + getProjectDao().getAllProjectsByName().size() );
116
117 log.info( "number of project groups: " + getProjectGroupDao().getAllProjectGroupsWithProjects().size() );
118
119 assertEquals( "Total project count", projectCount + 3, getProjectDao().getAllProjectsByName().size() );
120
121 assertEquals( "Total project group count.", projectGroupCount + 1,
122 getProjectGroupDao().getAllProjectGroupsWithProjects().size() );
123
124 Map<String, Project> projects = new HashMap<String, Project>();
125
126 for ( Project project : getProjectDao().getAllProjectsByName() )
127 {
128 projects.put( project.getName(), project );
129
130
131 assertTrue( "project not in project group", getProjectGroupDao().getProjectGroupByProjectId(
132 project.getId() ) != null );
133 }
134
135 assertTrue( "no irc notifier", projects.containsKey( "Continuum IRC Notifier" ) );
136
137 assertTrue( "no jabber notifier", projects.containsKey( "Continuum Jabber Notifier" ) );
138
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230 public void testUpdateMavenTwoProject()
231 throws Exception
232 {
233 Continuum continuum = (Continuum) lookup( Continuum.ROLE );
234
235
236
237
238
239 String url = getTestFile( "src/test-projects/project1/pom.xml" ).toURL().toExternalForm();
240
241 ContinuumProjectBuildingResult result = continuum.addMavenTwoProject( url );
242
243 assertNotNull( result );
244
245 List<Project> projects = result.getProjects();
246
247 assertEquals( 1, projects.size() );
248
249 assertEquals( Project.class, projects.get( 0 ).getClass() );
250
251 Project project = projects.get( 0 );
252
253
254 project = continuum.getProject( project.getId() );
255
256 project.setName( project.getName() + " 2" );
257
258 continuum.updateProject( project );
259
260 project = continuum.getProject( project.getId() );
261 }
262
263 public void testRemoveMavenTwoProject()
264 throws Exception
265 {
266 Continuum continuum = (Continuum) lookup( Continuum.ROLE );
267
268 Project project = makeStubProject( "test-project" );
269
270 ProjectGroup defaultGroup = getDefaultProjectGroup();
271
272 defaultGroup.addProject( project );
273
274 getProjectGroupDao().updateProjectGroup( defaultGroup );
275
276 project = getProjectDao().getProjectByName( "test-project" );
277
278 assertNotNull( project );
279
280 BuildResult buildResult = new BuildResult();
281
282 getBuildResultDao().addBuildResult( project, buildResult );
283
284 Collection<BuildResult> brs = continuum.getBuildResultsForProject( project.getId() );
285
286 assertEquals( "Build result of project was not added", 1, brs.size() );
287
288
289 continuum.removeProject( project.getId() );
290
291 try
292 {
293 continuum.getProject( project.getId() );
294
295 fail( "Project was not removed" );
296 }
297 catch ( ContinuumException expected )
298 {
299 brs = continuum.getBuildResultsForProject( project.getId() );
300
301 assertEquals( "Build result of project was not removed", 0, brs.size() );
302 }
303 }
304
305 public void testBuildDefinitions()
306 throws Exception
307 {
308 Continuum continuum = (Continuum) lookup( Continuum.ROLE );
309
310 String url = getTestFile( "src/test-projects/project1/pom.xml" ).toURL().toExternalForm();
311
312 ContinuumProjectBuildingResult result = continuum.addMavenTwoProject( url );
313
314 assertNotNull( result );
315
316 List<Project> projects = result.getProjects();
317
318 assertEquals( 1, projects.size() );
319
320 assertEquals( Project.class, projects.get( 0 ).getClass() );
321
322 Project project = projects.get( 0 );
323
324
325 project = continuum.getProject( project.getId() );
326
327 ProjectGroup projectGroup = getProjectGroupDao().getProjectGroupByProjectId( project.getId() );
328
329 projectGroup = getProjectGroupDao().getProjectGroupWithBuildDetailsByProjectGroupId( projectGroup.getId() );
330
331 List<BuildDefinition> buildDefs = projectGroup.getBuildDefinitions();
332
333 assertTrue( "missing project group build definition", !buildDefs.isEmpty() );
334
335 assertTrue( "more then one project group build definition on add project", buildDefs.size() == 1 );
336
337 BuildDefinition pgbd = buildDefs.get( 0 );
338
339 pgbd.setGoals( "foo" );
340
341 continuum.updateBuildDefinitionForProjectGroup( projectGroup.getId(), pgbd );
342
343 pgbd = continuum.getBuildDefinition( pgbd.getId() );
344
345 assertTrue( "update failed for project group build definition", "foo".equals( pgbd.getGoals() ) );
346
347 assertTrue( "project group build definition is not default", pgbd.isDefaultForProject() );
348
349 BuildDefinition nbd = new BuildDefinition();
350 nbd.setGoals( "clean" );
351 nbd.setArguments( "" );
352 nbd.setDefaultForProject( true );
353 nbd.setSchedule( getScheduleDao().getScheduleByName( ConfigurationService.DEFAULT_SCHEDULE_NAME ) );
354
355 continuum.addBuildDefinitionToProject( project.getId(), nbd );
356
357 assertTrue( "project lvl build definition not default for project", continuum.getDefaultBuildDefinition(
358 project.getId() ).getId() == nbd.getId() );
359
360 continuum.removeBuildDefinitionFromProject( project.getId(), nbd.getId() );
361
362 assertTrue( "default build definition didn't toggle back to project group level",
363 continuum.getDefaultBuildDefinition( project.getId() ).getId() == pgbd.getId() );
364
365 try
366 {
367 continuum.removeBuildDefinitionFromProjectGroup( projectGroup.getId(), pgbd.getId() );
368 fail( "we were able to remove the default build definition, and that is bad" );
369 }
370 catch ( ContinuumException expected )
371 {
372
373 }
374 }
375
376
377
378
379 public void testProjectGroups()
380 throws Exception
381 {
382 Continuum continuum = (Continuum) lookup( Continuum.ROLE );
383
384 Collection projectGroupList = continuum.getAllProjectGroups();
385
386 int projectGroupsBefore = projectGroupList.size();
387
388 assertEquals( 1, projectGroupsBefore );
389
390 String url = getTestFile( "src/test-projects/project1/pom.xml" ).toURL().toExternalForm();
391
392 ContinuumProjectBuildingResult result = continuum.addMavenTwoProject( url );
393
394 assertNotNull( result );
395
396 assertEquals( 1, result.getProjectGroups().size() );
397
398 ProjectGroup projectGroup = result.getProjectGroups().get( 0 );
399
400 assertEquals( "plexus", projectGroup.getGroupId() );
401
402 projectGroupList = continuum.getAllProjectGroups();
403
404 assertEquals( "Project group missing, should have " + ( projectGroupsBefore + 1 ) + " project groups",
405 projectGroupsBefore + 1, projectGroupList.size() );
406
407 projectGroup = (ProjectGroup) projectGroupList.iterator().next();
408
409 assertNotNull( projectGroup );
410
411 BuildsManager buildsManager = continuum.getBuildsManager();
412
413 List<Project> projects = continuum.getProjectGroupWithProjects( projectGroup.getId() ).getProjects();
414 int[] projectIds = new int[projects.size()];
415
416 int idx = 0;
417 for ( Project project : projects )
418 {
419 projectIds[idx] = project.getId();
420 idx++;
421 }
422
423 while ( buildsManager.isAnyProjectCurrentlyBeingCheckedOut( projectIds ) )
424 {
425 }
426
427 continuum.removeProjectGroup( projectGroup.getId() );
428
429 projectGroupList = continuum.getAllProjectGroups();
430
431 assertEquals( "Remove project group failed", projectGroupsBefore, projectGroupList.size() );
432 }
433
434
435
436
437 public void testProjectAndGroupNotifiers()
438 throws Exception
439 {
440 Continuum continuum = (Continuum) lookup( Continuum.ROLE );
441
442 Collection projectGroupList = continuum.getAllProjectGroups();
443
444 int projectGroupsBefore = projectGroupList.size();
445
446 assertEquals( 1, projectGroupsBefore );
447
448 String url = getTestFile( "src/test-projects/project1/pom.xml" ).toURL().toExternalForm();
449
450 ContinuumProjectBuildingResult result = continuum.addMavenTwoProject( url );
451
452 assertNotNull( result );
453
454 assertEquals( 1, result.getProjectGroups().size() );
455
456 ProjectGroup projectGroup = result.getProjectGroups().get( 0 );
457
458 continuum.addGroupNotifier( projectGroup.getId(), new ProjectNotifier() );
459
460 for ( Project p : (List<Project>) projectGroup.getProjects() )
461 {
462 continuum.addNotifier( p.getId(), new ProjectNotifier() );
463 }
464
465 projectGroup = continuum.getProjectGroupWithBuildDetails( projectGroup.getId() );
466
467 assertEquals( 1, projectGroup.getNotifiers().size() );
468
469 for ( Project p : (List<Project>) projectGroup.getProjects() )
470 {
471 assertEquals( 2, p.getNotifiers().size() );
472 }
473 }
474
475 public void testExecuteAction()
476 throws Exception
477 {
478 DefaultContinuum continuum = (DefaultContinuum) lookup( Continuum.ROLE );
479
480 String exceptionName = ContinuumException.class.getName();
481 try
482 {
483 continuum.executeAction( "testAction", new HashMap() );
484 }
485 catch ( ContinuumException e )
486 {
487
488 if ( e.getCause() != null )
489 {
490 assertFalse( exceptionName + " is wrapped in " + exceptionName, e.getCause().getClass().equals(
491 ContinuumException.class ) );
492 }
493 }
494 }
495
496 public void testRemoveProjectFromCheckoutQueue()
497 throws Exception
498 {
499 Continuum continuum = (Continuum) lookup( Continuum.ROLE );
500
501 BuildsManager parallelBuildsManager = continuum.getBuildsManager();
502
503 String url = getTestFile( "src/test-projects/project1/pom.xml" ).toURL().toExternalForm();
504
505 ContinuumProjectBuildingResult result = continuum.addMavenTwoProject( url );
506
507 assertNotNull( result );
508
509 List<Project> projects = result.getProjects();
510
511 assertEquals( 1, projects.size() );
512
513 assertEquals( Project.class, projects.get( 0 ).getClass() );
514
515 Project project = projects.get( 0 );
516
517 parallelBuildsManager.removeProjectFromCheckoutQueue( project.getId() );
518
519 assertFalse( "project still exist on the checkout queue", parallelBuildsManager.isInAnyCheckoutQueue(
520 project.getId() ) );
521 }
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568 public void testAddAntProjectWithdefaultBuildDef()
569 throws Exception
570 {
571 Continuum continuum = getContinuum();
572
573 Project project = new Project();
574 project.setScmUrl( "scmUrl" );
575 ProjectGroup defaultProjectGroup = continuum.getProjectGroupByGroupId(
576 ContinuumInitializer.DEFAULT_PROJECT_GROUP_GROUP_ID );
577 int projectId = continuum.addProject( project, ContinuumBuildExecutorConstants.ANT_BUILD_EXECUTOR,
578 defaultProjectGroup.getId() );
579 assertEquals( 1, continuum.getProjectGroupWithProjects( defaultProjectGroup.getId() ).getProjects().size() );
580 project = continuum.getProjectWithAllDetails( projectId );
581 assertNotNull( project );
582
583 BuildDefinitionService service = (BuildDefinitionService) lookup( BuildDefinitionService.class );
584 assertEquals( 4, service.getAllBuildDefinitionTemplate().size() );
585 assertEquals( 5, service.getAllBuildDefinitions().size() );
586
587 BuildDefinition buildDef =
588 (BuildDefinition) service.getDefaultAntBuildDefinitionTemplate().getBuildDefinitions().get( 0 );
589 buildDef = service.cloneBuildDefinition( buildDef );
590 buildDef.setTemplate( false );
591 continuum.addBuildDefinitionToProject( project.getId(), buildDef );
592 project = continuum.getProjectWithAllDetails( project.getId() );
593 assertEquals( 2, project.getBuildDefinitions().size() );
594 assertEquals( 4, service.getAllBuildDefinitionTemplate().size() );
595 assertEquals( 6, service.getAllBuildDefinitions().size() );
596 }
597
598 public void testRemoveProjectGroupWithRepository()
599 throws Exception
600 {
601 Continuum continuum = getContinuum();
602 RepositoryService service = (RepositoryService) lookup( RepositoryService.ROLE );
603
604 LocalRepository repository = new LocalRepository();
605 repository.setName( "defaultRepo" );
606 repository.setLocation( getTestFile( "target/default-repository" ).getAbsolutePath() );
607 repository = service.addLocalRepository( repository );
608
609 ProjectGroup group = new ProjectGroup();
610 group.setGroupId( "testGroup" );
611 group.setName( "testGroup" );
612 group.setLocalRepository( repository );
613 continuum.addProjectGroup( group );
614
615 ProjectGroup retrievedDefaultProjectGroup = continuum.getProjectGroupByGroupId( "testGroup" );
616 assertNotNull( retrievedDefaultProjectGroup.getLocalRepository() );
617
618 continuum.removeProjectGroup( retrievedDefaultProjectGroup.getId() );
619
620 try
621 {
622 continuum.getProjectGroupByGroupId( "testGroup" );
623 fail( "project group was not deleted" );
624 }
625 catch ( Exception e )
626 {
627
628 }
629
630 LocalRepository retrievedRepository = service.getLocalRepository( repository.getId() );
631 assertNotNull( retrievedRepository );
632 assertEquals( repository, retrievedRepository );
633 }
634
635 public void testContinuumReleaseResult()
636 throws Exception
637 {
638 Continuum continuum = getContinuum();
639
640 Project project = makeStubProject( "test-project" );
641 ProjectGroup defaultGroup = getDefaultProjectGroup();
642 defaultGroup.addProject( project );
643 getProjectGroupDao().updateProjectGroup( defaultGroup );
644 project = getProjectDao().getProjectByName( "test-project" );
645 assertNotNull( project );
646 assertEquals( 0, continuum.getAllContinuumReleaseResults().size() );
647
648 ReleaseResult result = new ReleaseResult();
649 result.setStartTime( System.currentTimeMillis() );
650 result.setEndTime( System.currentTimeMillis() );
651 result.setResultCode( 200 );
652 result.appendOutput( "Error in release" );
653
654 ContinuumReleaseDescriptor descriptor = new ContinuumReleaseDescriptor();
655 descriptor.setPreparationGoals( "clean" );
656 descriptor.setReleaseBy( "admin" );
657
658 continuum.getReleaseManager().getReleaseResults().put( "test-release-id", result );
659 continuum.getReleaseManager().getPreparedReleases().put( "test-release-id", descriptor );
660
661 ContinuumReleaseResult releaseResult = continuum.addContinuumReleaseResult( project.getId(), "test-release-id",
662 "prepare" );
663
664 releaseResult = continuum.addContinuumReleaseResult( releaseResult );
665
666 List<ContinuumReleaseResult> releaseResults = continuum.getContinuumReleaseResultsByProjectGroup(
667 defaultGroup.getId() );
668 assertEquals( 1, releaseResults.size() );
669 assertEquals( releaseResult, releaseResults.get( 0 ) );
670
671 continuum.removeContinuumReleaseResult( releaseResult.getId() );
672 assertEquals( 0, continuum.getAllContinuumReleaseResults().size() );
673 assertEquals( defaultGroup, continuum.getProjectGroupByGroupId(
674 ContinuumInitializer.DEFAULT_PROJECT_GROUP_GROUP_ID ) );
675 }
676
677 public void testBuildProjectWhileProjectIsInReleaseStage()
678 throws Exception
679 {
680 DefaultContinuum continuum = (DefaultContinuum) getContinuum();
681
682 continuum.setTaskQueueManager( taskQueueManager );
683
684 continuum.setProjectDao( projectDao );
685
686 final Project project = new Project();
687 project.setId( 1 );
688 project.setName( "Continuum Core" );
689 project.setGroupId( "org.apache.continuum" );
690 project.setArtifactId( "continuum-core" );
691
692 context.checking( new Expectations()
693 {
694 {
695 one( projectDao ).getProject( 1 );
696 will( returnValue( project ) );
697
698 one( taskQueueManager ).isProjectInReleaseStage( "org.apache.continuum:continuum-core" );
699 will( returnValue( true ) );
700 }
701 } );
702
703 try
704 {
705 continuum.buildProject( 1, "test-user" );
706 fail( "An exception should have been thrown." );
707 }
708 catch ( ContinuumException e )
709 {
710 assertEquals( "Project (id=1) is currently in release stage.", e.getMessage() );
711 }
712 }
713
714 public void testBuildProjectGroupWhileAtLeastOneProjectIsInReleaseStage()
715 throws Exception
716 {
717 DefaultContinuum continuum = (DefaultContinuum) getContinuum();
718
719 continuum.setTaskQueueManager( taskQueueManager );
720
721 continuum.setProjectDao( projectDao );
722
723 final List<Project> projects = new ArrayList<Project>();
724
725 Project project = new Project();
726 project.setId( 1 );
727 project.setName( "Continuum Core" );
728 project.setGroupId( "org.apache.continuum" );
729 project.setArtifactId( "continuum-core" );
730 projects.add( project );
731
732 project = new Project();
733 project.setId( 2 );
734 project.setName( "Continuum API" );
735 project.setGroupId( "org.apache.continuum" );
736 project.setArtifactId( "continuum-api" );
737 projects.add( project );
738
739 context.checking( new Expectations()
740 {
741 {
742 one( projectDao ).getProjectsInGroup( 1 );
743 will( returnValue( projects ) );
744
745 one( taskQueueManager ).isProjectInReleaseStage( "org.apache.continuum:continuum-core" );
746 will( returnValue( true ) );
747 }
748 } );
749
750 try
751 {
752 continuum.buildProjectGroup( 1, new BuildTrigger( 1, "test-user" ) );
753 fail( "An exception should have been thrown." );
754 }
755 catch ( ContinuumException e )
756 {
757 assertEquals( "Cannot build project group. Project (id=1) in group is currently in release stage.",
758 e.getMessage() );
759 }
760 }
761
762 private Continuum getContinuum()
763 throws Exception
764 {
765 return (Continuum) lookup( Continuum.ROLE );
766 }
767
768 private BuildResultDao getBuildResultDao()
769 {
770 return (BuildResultDao) lookup( BuildResultDao.class.getName() );
771 }
772 }