View Javadoc

1   package org.apache.maven.continuum.buildqueue;
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.taskqueue.BuildProjectTask;
23  import org.apache.continuum.utils.build.BuildTrigger;
24  import org.apache.maven.continuum.AbstractContinuumTest;
25  import org.apache.maven.continuum.model.project.Project;
26  import org.apache.maven.continuum.model.project.ProjectGroup;
27  import org.apache.maven.continuum.project.ContinuumProjectState;
28  import org.codehaus.plexus.taskqueue.Task;
29  import org.codehaus.plexus.taskqueue.TaskQueue;
30  
31  /**
32   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
33   * @version $Id: BuildQueueTest.java 1372260 2012-08-13 04:29:09Z brett $
34   */
35  public class BuildQueueTest
36      extends AbstractContinuumTest
37  {
38      private TaskQueue buildQueue;
39  
40      public void setUp()
41          throws Exception
42      {
43          super.setUp();
44  
45          buildQueue = (TaskQueue) lookup( TaskQueue.ROLE, "build-project" );
46      }
47  
48      public void testTestTheQueueWithASingleProject()
49          throws Exception
50      {
51          Project project = addProject( "Build Queue Project 1" );
52  
53          int projectId = project.getId();
54  
55          buildProject( projectId, ContinuumProjectState.TRIGGER_SCHEDULED );
56  
57          assertNextBuildIs( projectId );
58  
59          assertNextBuildIsNull();
60  
61          buildProject( projectId, ContinuumProjectState.TRIGGER_SCHEDULED );
62  
63          buildProject( projectId, ContinuumProjectState.TRIGGER_SCHEDULED );
64          buildProject( projectId, ContinuumProjectState.TRIGGER_SCHEDULED );
65          buildProject( projectId, ContinuumProjectState.TRIGGER_SCHEDULED );
66          buildProject( projectId, ContinuumProjectState.TRIGGER_SCHEDULED );
67  
68          assertNextBuildIs( projectId );
69  
70          assertNextBuildIsNull();
71      }
72  
73      public void testTheQueueWithMultipleProjects()
74          throws Exception
75      {
76          int projectId1 = addProject( "Build Queue Project 2" ).getId();
77  
78          int projectId2 = addProject( "Build Queue Project 3" ).getId();
79  
80          buildProject( projectId1, ContinuumProjectState.TRIGGER_SCHEDULED );
81  
82          buildProject( projectId2, ContinuumProjectState.TRIGGER_SCHEDULED );
83  
84          assertNextBuildIs( projectId1 );
85  
86          assertNextBuildIs( projectId2 );
87  
88          assertNextBuildIsNull();
89  
90          for ( int i = 0; i < 5; i++ )
91          {
92              buildProject( projectId1, ContinuumProjectState.TRIGGER_SCHEDULED );
93              buildProject( projectId2, ContinuumProjectState.TRIGGER_SCHEDULED );
94          }
95  
96          assertNextBuildIs( projectId1 );
97          assertNextBuildIs( projectId2 );
98  
99          assertNextBuildIsNull();
100     }
101 
102     public void testTestTheQueueWithASingleProjectAndForcedBuilds()
103         throws Exception
104     {
105         String name = "Build Queue Project 4";
106 
107         int projectId = addProject( name ).getId();
108 
109         buildProject( projectId, ContinuumProjectState.TRIGGER_FORCED );
110 
111         assertNextBuildIs( projectId );
112 
113         assertNextBuildIsNull();
114 
115         for ( int i = 0; i < 5; i++ )
116         {
117             buildProject( projectId, ContinuumProjectState.TRIGGER_FORCED );
118         }
119 
120         assertNextBuildIs( projectId );
121         assertNextBuildIs( projectId );
122         assertNextBuildIs( projectId );
123         assertNextBuildIs( projectId );
124         assertNextBuildIs( projectId );
125 
126         assertNextBuildIsNull();
127     }
128 
129     // ----------------------------------------------------------------------
130     //
131     // ----------------------------------------------------------------------
132 
133     private void buildProject( int projectId, int trigger )
134         throws Exception
135     {
136         ProjectGroup group = getDefaultProjectGroup();
137         buildQueue.put( new BuildProjectTask( projectId, 0, new BuildTrigger( trigger, "" ), null, null, null,
138                                               group.getId() ) );
139     }
140 
141     private void assertNextBuildIs( int expectedProjectId )
142         throws Exception
143     {
144         Task task = buildQueue.take();
145 
146         assertEquals( BuildProjectTask.class.getName(), task.getClass().getName() );
147 
148         BuildProjectTask buildProjectTask = (BuildProjectTask) task;
149 
150         assertEquals( "Didn't get the expected project id.", expectedProjectId, buildProjectTask.getProjectId() );
151     }
152 
153     private void assertNextBuildIsNull()
154         throws Exception
155     {
156         Task task = buildQueue.take();
157 
158         if ( task != null )
159         {
160             fail( "Got a non-null build task returned. Project id: " + ( (BuildProjectTask) task ).getProjectId() );
161         }
162     }
163 }