View Javadoc

1   package org.apache.continuum.taskqueue;
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 java.io.File;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.continuum.dao.BuildDefinitionDao;
27  import org.apache.continuum.taskqueueexecutor.ParallelBuildsThreadedTaskQueueExecutor;
28  import org.apache.maven.continuum.model.project.BuildDefinition;
29  import org.codehaus.plexus.spring.PlexusInSpringTestCase;
30  import org.codehaus.plexus.taskqueue.Task;
31  import org.codehaus.plexus.taskqueue.TaskQueue;
32  import org.jmock.Expectations;
33  import org.jmock.Mockery;
34  import org.jmock.integration.junit3.JUnit3Mockery;
35  import org.jmock.lib.legacy.ClassImposteriser;
36  
37  /**
38   * DefaultOverallBuildQueueTest
39   *
40   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
41   */
42  public class DefaultOverallBuildQueueTest
43      extends PlexusInSpringTestCase
44  {
45      private DefaultOverallBuildQueue overallQueue;
46  
47      private Mockery context;
48  
49      private BuildDefinitionDao buildDefinitionDao;
50  
51      private ParallelBuildsThreadedTaskQueueExecutor buildTaskQueueExecutor;
52  
53      private ParallelBuildsThreadedTaskQueueExecutor checkoutTaskQueueExecutor;
54  
55      @Override
56      protected void setUp()
57          throws Exception
58      {
59          super.setUp();
60  
61          overallQueue = new DefaultOverallBuildQueue();
62  
63          context = new JUnit3Mockery();
64  
65          buildDefinitionDao = context.mock( BuildDefinitionDao.class );
66          context.setImposteriser( ClassImposteriser.INSTANCE );
67  
68          buildTaskQueueExecutor = context.mock( ParallelBuildsThreadedTaskQueueExecutor.class, "build-queue-executor" );
69  
70          checkoutTaskQueueExecutor =
71              context.mock( ParallelBuildsThreadedTaskQueueExecutor.class, "checkout-queue-executor" );
72  
73          overallQueue.setBuildDefinitionDao( buildDefinitionDao );
74  
75          overallQueue.setBuildTaskQueueExecutor( buildTaskQueueExecutor );
76  
77          overallQueue.setCheckoutTaskQueueExecutor( checkoutTaskQueueExecutor );
78      }
79  
80      // checkout queue
81  
82      public void testAddToCheckoutQueue()
83          throws Exception
84      {
85          final CheckOutTask checkoutTask =
86              new CheckOutTask( 1, new File( getBasedir(), "/target/test-working-dir/1" ), "continuum-project-test-1",
87                                "dummy", "dummypass" );
88          final TaskQueue checkoutQueue = context.mock( TaskQueue.class, "checkout-queue" );
89  
90          context.checking( new Expectations()
91          {
92              {
93                  one( checkoutTaskQueueExecutor ).getQueue();
94                  will( returnValue( checkoutQueue ) );
95  
96                  one( checkoutQueue ).put( checkoutTask );
97              }} );
98  
99          overallQueue.addToCheckoutQueue( checkoutTask );
100         context.assertIsSatisfied();
101     }
102 
103     public void testGetProjectsInCheckoutQueue()
104         throws Exception
105     {
106         final TaskQueue checkoutQueue = context.mock( TaskQueue.class, "checkout-queue" );
107         final List<Task> tasks = new ArrayList<Task>();
108         tasks.add(
109             new CheckOutTask( 1, new File( getBasedir(), "/target/test-working-dir/1" ), "continuum-project-test-1",
110                               "dummy", "dummypass" ) );
111 
112         context.checking( new Expectations()
113         {
114             {
115                 one( checkoutTaskQueueExecutor ).getQueue();
116                 will( returnValue( checkoutQueue ) );
117 
118                 one( checkoutQueue ).getQueueSnapshot();
119                 will( returnValue( tasks ) );
120             }} );
121 
122         List<CheckOutTask> returnedTasks = overallQueue.getProjectsInCheckoutQueue();
123         context.assertIsSatisfied();
124 
125         assertNotNull( returnedTasks );
126         assertEquals( 1, returnedTasks.size() );
127     }
128 
129     public void testIsInCheckoutQueue()
130         throws Exception
131     {
132         final TaskQueue checkoutQueue = context.mock( TaskQueue.class, "checkout-queue" );
133         final List<Task> tasks = new ArrayList<Task>();
134         tasks.add(
135             new CheckOutTask( 1, new File( getBasedir(), "/target/test-working-dir/1" ), "continuum-project-test-1",
136                               "dummy", "dummypass" ) );
137 
138         context.checking( new Expectations()
139         {
140             {
141                 one( checkoutTaskQueueExecutor ).getQueue();
142                 will( returnValue( checkoutQueue ) );
143 
144                 one( checkoutQueue ).getQueueSnapshot();
145                 will( returnValue( tasks ) );
146             }} );
147 
148         assertTrue( overallQueue.isInCheckoutQueue( 1 ) );
149         context.assertIsSatisfied();
150     }
151 
152     public void testRemoveProjectFromCheckoutQueue()
153         throws Exception
154     {
155         final Task checkoutTask =
156             new CheckOutTask( 1, new File( getBasedir(), "/target/test-working-dir/1" ), "continuum-project-test-1",
157                               "dummy", "dummypass" );
158         final TaskQueue checkoutQueue = context.mock( TaskQueue.class, "checkout-queue" );
159         final List<Task> tasks = new ArrayList<Task>();
160         tasks.add( checkoutTask );
161 
162         context.checking( new Expectations()
163         {
164             {
165                 one( checkoutTaskQueueExecutor ).getQueue();
166                 will( returnValue( checkoutQueue ) );
167 
168                 one( checkoutQueue ).getQueueSnapshot();
169                 will( returnValue( tasks ) );
170 
171                 one( checkoutTaskQueueExecutor ).getQueue();
172                 will( returnValue( checkoutQueue ) );
173 
174                 one( checkoutQueue ).remove( checkoutTask );
175             }} );
176 
177         overallQueue.removeProjectFromCheckoutQueue( 1 );
178         context.assertIsSatisfied();
179     }
180 
181     // build queue
182 
183     public void testAddToBuildQueue()
184         throws Exception
185     {
186         final BuildProjectTask buildTask =
187             new BuildProjectTask( 2, 1, 1, "continuum-project-test-2", "BUILD_DEF", null, 2 );
188         final TaskQueue buildQueue = context.mock( TaskQueue.class, "build-queue" );
189 
190         context.checking( new Expectations()
191         {
192             {
193                 one( buildTaskQueueExecutor ).getQueue();
194                 will( returnValue( buildQueue ) );
195 
196                 one( buildQueue ).put( buildTask );
197             }} );
198 
199         overallQueue.addToBuildQueue( buildTask );
200         context.assertIsSatisfied();
201     }
202 
203     public void testGetProjectsFromBuildQueue()
204         throws Exception
205     {
206         final TaskQueue buildQueue = context.mock( TaskQueue.class, "build-queue" );
207         final List<Task> tasks = new ArrayList<Task>();
208         tasks.add( new BuildProjectTask( 2, 1, 1, "continuum-project-test-2", "BUILD_DEF", null, 2 ) );
209 
210         context.checking( new Expectations()
211         {
212             {
213                 one( buildTaskQueueExecutor ).getQueue();
214                 will( returnValue( buildQueue ) );
215 
216                 one( buildQueue ).getQueueSnapshot();
217                 will( returnValue( tasks ) );
218             }} );
219 
220         List<BuildProjectTask> returnedTasks = overallQueue.getProjectsInBuildQueue();
221         context.assertIsSatisfied();
222 
223         assertNotNull( returnedTasks );
224         assertEquals( 1, returnedTasks.size() );
225     }
226 
227     public void testIsInBuildQueue()
228         throws Exception
229     {
230         final TaskQueue buildQueue = context.mock( TaskQueue.class, "build-queue" );
231         final List<Task> tasks = new ArrayList<Task>();
232         tasks.add( new BuildProjectTask( 2, 1, 1, "continuum-project-test-2", "BUILD_DEF", null, 2 ) );
233 
234         context.checking( new Expectations()
235         {
236             {
237                 one( buildTaskQueueExecutor ).getQueue();
238                 will( returnValue( buildQueue ) );
239 
240                 one( buildQueue ).getQueueSnapshot();
241                 will( returnValue( tasks ) );
242             }} );
243 
244         assertTrue( overallQueue.isInBuildQueue( 2 ) );
245         context.assertIsSatisfied();
246     }
247 
248     public void testCancelBuildTask()
249         throws Exception
250     {
251         final Task buildTask = new BuildProjectTask( 2, 1, 1, "continuum-project-test-2", "BUILD_DEF", null, 2 );
252 
253         context.checking( new Expectations()
254         {
255             {
256                 one( buildTaskQueueExecutor ).getCurrentTask();
257                 will( returnValue( buildTask ) );
258 
259                 one( buildTaskQueueExecutor ).cancelTask( buildTask );
260             }} );
261 
262         overallQueue.cancelBuildTask( 2 );
263         context.assertIsSatisfied();
264     }
265 
266     public void testCancelCurrentBuild()
267         throws Exception
268     {
269         final Task buildTask = new BuildProjectTask( 2, 1, 1, "continuum-project-test-2", "BUILD_DEF", null, 2 );
270 
271         context.checking( new Expectations()
272         {
273             {
274                 one( buildTaskQueueExecutor ).getCurrentTask();
275                 will( returnValue( buildTask ) );
276 
277                 one( buildTaskQueueExecutor ).cancelTask( buildTask );
278             }} );
279 
280         overallQueue.cancelCurrentBuild();
281         context.assertIsSatisfied();
282     }
283 
284     public void testRemoveProjectFromBuildQueueWithGivenBuildDefinition()
285         throws Exception
286     {
287         final BuildDefinition buildDef = new BuildDefinition();
288         buildDef.setId( 1 );
289         buildDef.setDescription( "Test build definition" );
290 
291         final TaskQueue buildQueue = context.mock( TaskQueue.class, "build-queue" );
292 
293         context.checking( new Expectations()
294         {
295             {
296                 one( buildDefinitionDao ).getBuildDefinition( 1 );
297                 will( returnValue( buildDef ) );
298 
299                 one( buildTaskQueueExecutor ).getQueue();
300                 will( returnValue( buildQueue ) );
301 
302                 one( buildQueue ).remove( with( any( Task.class ) ) );
303             }} );
304 
305         overallQueue.removeProjectFromBuildQueue( 1, 1, 1, "continuum-project-test-1", 1 );
306         context.assertIsSatisfied();
307     }
308 
309     public void testRemoveProjectFromBuildQueue()
310         throws Exception
311     {
312         final Task buildTask = new BuildProjectTask( 1, 1, 1, "continuum-project-test-2", "BUILD_DEF", null, 1 );
313 
314         final TaskQueue buildQueue = context.mock( TaskQueue.class, "build-queue" );
315         final List<Task> tasks = new ArrayList<Task>();
316         tasks.add( buildTask );
317 
318         context.checking( new Expectations()
319         {
320             {
321                 one( buildTaskQueueExecutor ).getQueue();
322                 will( returnValue( buildQueue ) );
323 
324                 one( buildQueue ).getQueueSnapshot();
325                 will( returnValue( tasks ) );
326 
327                 one( buildTaskQueueExecutor ).getQueue();
328                 will( returnValue( buildQueue ) );
329 
330                 one( buildQueue ).remove( buildTask );
331             }} );
332 
333         overallQueue.removeProjectFromBuildQueue( 1 );
334         context.assertIsSatisfied();
335     }
336 }