View Javadoc

1   package org.apache.maven.continuum;
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.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   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
57   * @version $Id: DefaultContinuumTest.java 1391353 2012-09-28 07:50:49Z brett $
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             // validate project in project group
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 /* test failing intermittently, possibly due to the dodgy for loop
142     // handle flat multi-module projects
143     public void testAddMavenTwoProjectSetInSingleDirectory()
144         throws Exception
145     {   
146         Continuum continuum = (Continuum) lookup( Continuum.ROLE );
147         
148         String url = getTestFile( "src/test-projects/flat-multi-module/parent-project/pom.xml" ).toURL().toExternalForm();
149 
150         ContinuumProjectBuildingResult result = continuum.addMavenTwoProject( url, -1, true, false, true, -1, true );
151  
152         assertNotNull( result );
153 
154         List<Project> projects = result.getProjects();
155 
156         assertEquals( 4, projects.size() );     
157         
158         Project rootProject = result.getRootProject();
159         
160         assertNotNull( rootProject );
161         
162         Map<String, Project> projectsMap = new HashMap<String, Project>();
163 
164         int projectGroupId = 0;
165 
166         for ( Project project : getProjectDao().getAllProjectsByName() )
167         {
168             projectsMap.put( project.getName(), project );
169 
170             ProjectGroup projectGroup = getProjectGroupDao().getProjectGroupByProjectId( project.getId() );
171             projectGroupId = projectGroup.getId();
172 
173             // validate project in project group
174             assertTrue( "project not in project group", projectGroup != null );
175         }
176 
177         // sometimes projects don't get added to checkout queue
178         continuum.buildProjectGroup( projectGroupId, new org.apache.continuum.utils.build.BuildTrigger( 1, "user" ) );
179 
180         assertTrue( "no module-a", projectsMap.containsKey( "module-a" ) );
181         
182         assertTrue( "no module-b", projectsMap.containsKey( "module-b" ) );
183 
184         assertTrue( "no module-d", projectsMap.containsKey( "module-d" ) );
185 
186         // check if the modules were checked out in the same directory as the parent
187         ConfigurationService configurationService = ( ConfigurationService ) lookup( "configurationService" );
188         
189         File workingDir = configurationService.getWorkingDirectory();
190         
191         Project parentProject = getProjectDao().getProjectByName( "parent-project" );
192         
193         File checkoutDir = new File( workingDir, String.valueOf( parentProject.getId() ) );
194 
195         for( long delay = 0; delay <= 999999999; delay++ )
196         {
197             // wait while the project has been checked out/build
198         }
199         
200         assertTrue( "checkout directory of project 'parent-project' does not exist." , new File( checkoutDir, "parent-project" ).exists() );
201         
202         assertFalse( "module-a should not have been checked out as a separate project.",
203                     new File( workingDir, String.valueOf( getProjectDao().getProjectByName( "module-a" ).getId() ) ).exists() );
204         
205         assertFalse( "module-b should not have been checked out as a separate project.",
206                     new File( workingDir, String.valueOf( getProjectDao().getProjectByName( "module-b" ).getId() ) ).exists() );
207 
208         assertFalse( "module-d should not have been checked out as a separate project.",
209                      new File( workingDir, String.valueOf( getProjectDao().getProjectByName( "module-d" ).getId() ) ).exists() );
210 
211         assertTrue( "module-a was not checked out in the same directory as it's parent.", new File( checkoutDir, "module-a" ).exists() );
212         
213         assertTrue( "module-b was not checked out in the same directory as it's parent.", new File( checkoutDir, "module-b" ).exists() );
214 
215         assertTrue( "module-d was not checked out in the same directory as it's parent.", new File( checkoutDir, "module-c/module-d" ).exists() );
216 
217         // assert project state
218         // commented out this test case as it sometimes fails because the actual checkout hasn't finished yet so
219         //    the state hasn't been updated yet
220         //assertEquals( "state of 'parent-project' should have been updated.", ContinuumProjectState.CHECKEDOUT, parentProject.getState() );
221         //
222         //assertEquals( "state of 'module-a' should have been updated.", ContinuumProjectState.CHECKEDOUT,
223         //            getProjectDao().getProjectByName( "module-a" ).getState() );
224         //
225         //assertEquals( "state of 'module-b' should have been updated.", ContinuumProjectState.CHECKEDOUT,
226         //            getProjectDao().getProjectByName( "module-b" ).getState() );
227     }
228 */
229 
230     public void testUpdateMavenTwoProject()
231         throws Exception
232     {
233         Continuum continuum = (Continuum) lookup( Continuum.ROLE );
234 
235         // ----------------------------------------------------------------------
236         // Test projects with duplicate names
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         // reattach
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         // delete project
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         // reattach
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      * todo add another project group to test
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      * test the logic for notifiers
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             //expected, check for twice wrapped exception
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     /*public void testCreationOfProjectScmRootDuringInitialization()
524     throws Exception
525 {
526     DefaultContinuum continuum = (DefaultContinuum) getContinuum();
527 
528     ProjectGroup defaultProjectGroup =
529         continuum.getProjectGroupByGroupId( ContinuumInitializer.DEFAULT_PROJECT_GROUP_GROUP_ID );
530 
531     ProjectScmRoot scmRoot = new ProjectScmRoot();
532     scmRoot.setProjectGroup( defaultProjectGroup );
533     scmRoot.setScmRootAddress( "http://temp.company.com/svn/trunk" );
534     getProjectScmRootDao().addProjectScmRoot( scmRoot );
535 
536     defaultProjectGroup = continuum.getProjectGroupWithProjects( defaultProjectGroup.getId() );
537     assertEquals( 0, defaultProjectGroup.getProjects().size() );
538 
539     Project project = new Project();
540     project.setGroupId( "project1" );
541     project.setArtifactId( "project1" );
542     project.setVersion( "1.0-SNAPSHOT" );
543     project.setScmUrl( "http://temp.company.com/svn/trunk/project1" );
544     defaultProjectGroup.addProject( project );
545 
546     project = new Project();
547     project.setGroupId( "project2" );
548     project.setArtifactId( "project2" );
549     project.setVersion( "1.0-SNAPSHOT" );
550     project.setScmUrl( "http://temp.company.com/svn/trunk/project2" );
551     defaultProjectGroup.addProject( project );
552 
553     project = new Project();
554     project.setGroupId( "project3" );
555     project.setArtifactId( "project3" );
556     project.setVersion( "1.0-SNAPSHOT" );
557     project.setScmUrl( "http://temp.company.com/svn/trunk/project3" );
558     defaultProjectGroup.addProject( project );
559 
560     getProjectGroupDao().updateProjectGroup( defaultProjectGroup );
561 
562     continuum.initialize();
563 
564     List<ProjectScmRoot> scmRoots = continuum.getProjectScmRootByProjectGroup( defaultProjectGroup.getId() );
565     assertEquals( "#scmRoots in the group", 1, scmRoots.size() );
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             // should fail. do nothing.
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 }