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 org.apache.continuum.dao.BuildDefinitionDao;
23  import org.apache.continuum.taskqueueexecutor.ParallelBuildsThreadedTaskQueueExecutor;
24  import org.apache.continuum.utils.build.BuildTrigger;
25  import org.apache.maven.continuum.model.project.BuildDefinition;
26  import org.codehaus.plexus.spring.PlexusInSpringTestCase;
27  import org.codehaus.plexus.taskqueue.Task;
28  import org.codehaus.plexus.taskqueue.TaskQueue;
29  import org.jmock.Expectations;
30  import org.jmock.Mockery;
31  import org.jmock.integration.junit3.JUnit3Mockery;
32  import org.jmock.lib.legacy.ClassImposteriser;
33  
34  import java.io.File;
35  import java.util.ArrayList;
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  
40  /**
41   * DefaultOverallBuildQueueTest
42   *
43   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
44   */
45  public class DefaultOverallBuildQueueTest
46      extends PlexusInSpringTestCase
47  {
48      private DefaultOverallBuildQueue overallQueue;
49  
50      private Mockery context;
51  
52      private BuildDefinitionDao buildDefinitionDao;
53  
54      private ParallelBuildsThreadedTaskQueueExecutor buildTaskQueueExecutor;
55  
56      private ParallelBuildsThreadedTaskQueueExecutor checkoutTaskQueueExecutor;
57  
58      private ParallelBuildsThreadedTaskQueueExecutor prepareBuildTaskQueueExecutor;
59  
60      @Override
61      protected void setUp()
62          throws Exception
63      {
64          super.setUp();
65  
66          overallQueue = new DefaultOverallBuildQueue();
67  
68          context = new JUnit3Mockery();
69  
70          buildDefinitionDao = context.mock( BuildDefinitionDao.class );
71          context.setImposteriser( ClassImposteriser.INSTANCE );
72  
73          buildTaskQueueExecutor = context.mock( ParallelBuildsThreadedTaskQueueExecutor.class, "build-queue-executor" );
74  
75          checkoutTaskQueueExecutor = context.mock( ParallelBuildsThreadedTaskQueueExecutor.class,
76                                                    "checkout-queue-executor" );
77  
78          prepareBuildTaskQueueExecutor = context.mock( ParallelBuildsThreadedTaskQueueExecutor.class,
79                                                        "prepare-build-queue-executor" );
80  
81          overallQueue.setBuildDefinitionDao( buildDefinitionDao );
82  
83          overallQueue.setBuildTaskQueueExecutor( buildTaskQueueExecutor );
84  
85          overallQueue.setCheckoutTaskQueueExecutor( checkoutTaskQueueExecutor );
86  
87          overallQueue.setPrepareBuildTaskQueueExecutor( prepareBuildTaskQueueExecutor );
88      }
89  
90      // checkout queue
91  
92      public void testAddToCheckoutQueue()
93          throws Exception
94      {
95          final CheckOutTask checkoutTask = new CheckOutTask( 1, new File( getBasedir(), "/target/test-working-dir/1" ),
96                                                              "continuum-project-test-1", "dummy", "dummypass", null,
97                                                              null );
98          final TaskQueue checkoutQueue = context.mock( TaskQueue.class, "checkout-queue" );
99  
100         context.checking( new Expectations()
101         {
102             {
103                 one( checkoutTaskQueueExecutor ).getQueue();
104                 will( returnValue( checkoutQueue ) );
105 
106                 one( checkoutQueue ).put( checkoutTask );
107             }
108         } );
109 
110         overallQueue.addToCheckoutQueue( checkoutTask );
111         context.assertIsSatisfied();
112     }
113 
114     public void testGetProjectsInCheckoutQueue()
115         throws Exception
116     {
117         final TaskQueue checkoutQueue = context.mock( TaskQueue.class, "checkout-queue" );
118         final List<Task> tasks = new ArrayList<Task>();
119         tasks.add( new CheckOutTask( 1, new File( getBasedir(), "/target/test-working-dir/1" ),
120                                      "continuum-project-test-1", "dummy", "dummypass", null, null ) );
121 
122         context.checking( new Expectations()
123         {
124             {
125                 one( checkoutTaskQueueExecutor ).getQueue();
126                 will( returnValue( checkoutQueue ) );
127 
128                 one( checkoutQueue ).getQueueSnapshot();
129                 will( returnValue( tasks ) );
130             }
131         } );
132 
133         List<CheckOutTask> returnedTasks = overallQueue.getProjectsInCheckoutQueue();
134         context.assertIsSatisfied();
135 
136         assertNotNull( returnedTasks );
137         assertEquals( 1, returnedTasks.size() );
138     }
139 
140     public void testIsInCheckoutQueue()
141         throws Exception
142     {
143         final TaskQueue checkoutQueue = context.mock( TaskQueue.class, "checkout-queue" );
144         final List<Task> tasks = new ArrayList<Task>();
145         tasks.add( new CheckOutTask( 1, new File( getBasedir(), "/target/test-working-dir/1" ),
146                                      "continuum-project-test-1", "dummy", "dummypass", null, null ) );
147 
148         context.checking( new Expectations()
149         {
150             {
151                 one( checkoutTaskQueueExecutor ).getQueue();
152                 will( returnValue( checkoutQueue ) );
153 
154                 one( checkoutQueue ).getQueueSnapshot();
155                 will( returnValue( tasks ) );
156             }
157         } );
158 
159         assertTrue( overallQueue.isInCheckoutQueue( 1 ) );
160         context.assertIsSatisfied();
161     }
162 
163     public void testRemoveProjectFromCheckoutQueue()
164         throws Exception
165     {
166         final Task checkoutTask = new CheckOutTask( 1, new File( getBasedir(), "/target/test-working-dir/1" ),
167                                                     "continuum-project-test-1", "dummy", "dummypass", null, null );
168         final TaskQueue checkoutQueue = context.mock( TaskQueue.class, "checkout-queue" );
169         final List<Task> tasks = new ArrayList<Task>();
170         tasks.add( checkoutTask );
171 
172         context.checking( new Expectations()
173         {
174             {
175                 one( checkoutTaskQueueExecutor ).getQueue();
176                 will( returnValue( checkoutQueue ) );
177 
178                 one( checkoutQueue ).getQueueSnapshot();
179                 will( returnValue( tasks ) );
180 
181                 one( checkoutTaskQueueExecutor ).getQueue();
182                 will( returnValue( checkoutQueue ) );
183 
184                 one( checkoutQueue ).remove( checkoutTask );
185             }
186         } );
187 
188         overallQueue.removeProjectFromCheckoutQueue( 1 );
189         context.assertIsSatisfied();
190     }
191 
192     // build queue
193 
194     public void testAddToBuildQueue()
195         throws Exception
196     {
197         final BuildProjectTask buildTask = new BuildProjectTask( 2, 1, new BuildTrigger( 1, "test-user" ),
198                                                                  "continuum-project-test-2", "BUILD_DEF", null, 2 );
199         final TaskQueue buildQueue = context.mock( TaskQueue.class, "build-queue" );
200 
201         context.checking( new Expectations()
202         {
203             {
204                 one( buildTaskQueueExecutor ).getQueue();
205                 will( returnValue( buildQueue ) );
206 
207                 one( buildQueue ).put( buildTask );
208             }
209         } );
210 
211         overallQueue.addToBuildQueue( buildTask );
212         context.assertIsSatisfied();
213     }
214 
215     public void testGetProjectsFromBuildQueue()
216         throws Exception
217     {
218         final TaskQueue buildQueue = context.mock( TaskQueue.class, "build-queue" );
219         final List<Task> tasks = new ArrayList<Task>();
220         tasks.add( new BuildProjectTask( 2, 1, new BuildTrigger( 1, "test-user" ), "continuum-project-test-2",
221                                          "BUILD_DEF", null, 2 ) );
222 
223         context.checking( new Expectations()
224         {
225             {
226                 one( buildTaskQueueExecutor ).getQueue();
227                 will( returnValue( buildQueue ) );
228 
229                 one( buildQueue ).getQueueSnapshot();
230                 will( returnValue( tasks ) );
231             }
232         } );
233 
234         List<BuildProjectTask> returnedTasks = overallQueue.getProjectsInBuildQueue();
235         context.assertIsSatisfied();
236 
237         assertNotNull( returnedTasks );
238         assertEquals( 1, returnedTasks.size() );
239     }
240 
241     public void testIsInBuildQueue()
242         throws Exception
243     {
244         final TaskQueue buildQueue = context.mock( TaskQueue.class, "build-queue" );
245         final List<Task> tasks = new ArrayList<Task>();
246         tasks.add( new BuildProjectTask( 2, 1, new BuildTrigger( 1, "test-user" ), "continuum-project-test-2",
247                                          "BUILD_DEF", null, 2 ) );
248 
249         context.checking( new Expectations()
250         {
251             {
252                 one( buildTaskQueueExecutor ).getQueue();
253                 will( returnValue( buildQueue ) );
254 
255                 one( buildQueue ).getQueueSnapshot();
256                 will( returnValue( tasks ) );
257             }
258         } );
259 
260         assertTrue( overallQueue.isInBuildQueue( 2 ) );
261         context.assertIsSatisfied();
262     }
263 
264     public void testCancelBuildTask()
265         throws Exception
266     {
267         final Task buildTask = new BuildProjectTask( 2, 1, new BuildTrigger( 1, "test-user" ),
268                                                      "continuum-project-test-2", "BUILD_DEF", null, 2 );
269 
270         context.checking( new Expectations()
271         {
272             {
273                 one( buildTaskQueueExecutor ).getCurrentTask();
274                 will( returnValue( buildTask ) );
275 
276                 one( buildTaskQueueExecutor ).cancelTask( buildTask );
277             }
278         } );
279 
280         overallQueue.cancelBuildTask( 2 );
281         context.assertIsSatisfied();
282     }
283 
284     public void testCancelCurrentBuild()
285         throws Exception
286     {
287         final Task buildTask = new BuildProjectTask( 2, 1, new BuildTrigger( 1, "test-user" ),
288                                                      "continuum-project-test-2", "BUILD_DEF", null, 2 );
289 
290         context.checking( new Expectations()
291         {
292             {
293                 one( buildTaskQueueExecutor ).getCurrentTask();
294                 will( returnValue( buildTask ) );
295 
296                 one( buildTaskQueueExecutor ).cancelTask( buildTask );
297             }
298         } );
299 
300         overallQueue.cancelCurrentBuild();
301         context.assertIsSatisfied();
302     }
303 
304     public void testRemoveProjectFromBuildQueueWithGivenBuildDefinition()
305         throws Exception
306     {
307         final BuildDefinition buildDef = new BuildDefinition();
308         buildDef.setId( 1 );
309         buildDef.setDescription( "Test build definition" );
310 
311         final TaskQueue buildQueue = context.mock( TaskQueue.class, "build-queue" );
312 
313         context.checking( new Expectations()
314         {
315             {
316                 one( buildDefinitionDao ).getBuildDefinition( 1 );
317                 will( returnValue( buildDef ) );
318 
319                 one( buildTaskQueueExecutor ).getQueue();
320                 will( returnValue( buildQueue ) );
321 
322                 one( buildQueue ).remove( with( any( Task.class ) ) );
323             }
324         } );
325 
326         overallQueue.removeProjectFromBuildQueue( 1, 1, new BuildTrigger( 1, "test-user" ), "continuum-project-test-1",
327                                                   1 );
328         context.assertIsSatisfied();
329     }
330 
331     public void testRemoveProjectFromBuildQueue()
332         throws Exception
333     {
334         final Task buildTask = new BuildProjectTask( 1, 1, new BuildTrigger( 1, "test-user" ),
335                                                      "continuum-project-test-2", "BUILD_DEF", null, 1 );
336 
337         final TaskQueue buildQueue = context.mock( TaskQueue.class, "build-queue" );
338         final List<Task> tasks = new ArrayList<Task>();
339         tasks.add( buildTask );
340 
341         context.checking( new Expectations()
342         {
343             {
344                 one( buildTaskQueueExecutor ).getQueue();
345                 will( returnValue( buildQueue ) );
346 
347                 one( buildQueue ).getQueueSnapshot();
348                 will( returnValue( tasks ) );
349 
350                 one( buildTaskQueueExecutor ).getQueue();
351                 will( returnValue( buildQueue ) );
352 
353                 one( buildQueue ).remove( buildTask );
354             }
355         } );
356 
357         overallQueue.removeProjectFromBuildQueue( 1 );
358         context.assertIsSatisfied();
359     }
360 
361     // prepare build queue
362 
363     public void testAddToPrepareBuildQueue()
364         throws Exception
365     {
366         final PrepareBuildProjectsTask prepareBuildTask = new PrepareBuildProjectsTask( new HashMap<Integer, Integer>(),
367                                                                                         new BuildTrigger( 1,
368                                                                                                           "test-user" ),
369                                                                                         1, "Project Group A",
370                                                                                         "http://scmRootAddress", 1 );
371         final TaskQueue prepareBuildQueue = context.mock( TaskQueue.class, "prepare-build-queue" );
372 
373         context.checking( new Expectations()
374         {
375             {
376                 one( prepareBuildTaskQueueExecutor ).getQueue();
377                 will( returnValue( prepareBuildQueue ) );
378 
379                 one( prepareBuildQueue ).put( prepareBuildTask );
380             }
381         } );
382 
383         overallQueue.addToPrepareBuildQueue( prepareBuildTask );
384         context.assertIsSatisfied();
385     }
386 
387     public void testCancelCurrentPrepareBuild()
388         throws Exception
389     {
390         final Task prepareBuildTask = new PrepareBuildProjectsTask( new HashMap<Integer, Integer>(), new BuildTrigger(
391             1, "test-user" ), 1, "Project Group A", "http://scm.root.address", 1 );
392 
393         context.checking( new Expectations()
394         {
395             {
396                 one( prepareBuildTaskQueueExecutor ).getCurrentTask();
397                 will( returnValue( prepareBuildTask ) );
398 
399                 one( prepareBuildTaskQueueExecutor ).cancelTask( prepareBuildTask );
400             }
401         } );
402 
403         overallQueue.cancelCurrentPrepareBuild();
404         context.assertIsSatisfied();
405     }
406 
407     public void testCancelPrepareBuildTaskByProject()
408         throws Exception
409     {
410         Map<Integer, Integer> map = new HashMap<Integer, Integer>();
411         map.put( 1, 1 );
412 
413         final Task prepareBuildTask = new PrepareBuildProjectsTask( map, new BuildTrigger( 1, "test-user" ), 1,
414                                                                     "Project Group A", "http://scm.root.address", 1 );
415 
416         context.checking( new Expectations()
417         {
418             {
419                 one( prepareBuildTaskQueueExecutor ).getCurrentTask();
420                 will( returnValue( prepareBuildTask ) );
421 
422                 one( prepareBuildTaskQueueExecutor ).cancelTask( prepareBuildTask );
423             }
424         } );
425 
426         overallQueue.cancelPrepareBuildTask( 1 );
427         context.assertIsSatisfied();
428     }
429 
430     public void testCancelPrepareBuildTaskByProjectGroup()
431         throws Exception
432     {
433         Map<Integer, Integer> map = new HashMap<Integer, Integer>();
434         map.put( 1, 1 );
435 
436         final Task prepareBuildTask = new PrepareBuildProjectsTask( map, new BuildTrigger( 1, "test-user" ), 1,
437                                                                     "Project Group A", "http://scm.root.address", 2 );
438 
439         context.checking( new Expectations()
440         {
441             {
442                 one( prepareBuildTaskQueueExecutor ).getCurrentTask();
443                 will( returnValue( prepareBuildTask ) );
444 
445                 one( prepareBuildTaskQueueExecutor ).cancelTask( prepareBuildTask );
446             }
447         } );
448 
449         overallQueue.cancelPrepareBuildTask( 1, 2 );
450         context.assertIsSatisfied();
451     }
452 
453     public void testGetProjectsFromPrepareBuildQueue()
454         throws Exception
455     {
456         final TaskQueue prepareBuildQueue = context.mock( TaskQueue.class, "prepare-build-queue" );
457         final List<Task> tasks = new ArrayList<Task>();
458         tasks.add( new PrepareBuildProjectsTask( new HashMap<Integer, Integer>(), new BuildTrigger( 1, "test-user" ), 2,
459                                                  "Project Group A", "http://scm.root.address", 2 ) );
460 
461         context.checking( new Expectations()
462         {
463             {
464                 one( prepareBuildTaskQueueExecutor ).getQueue();
465                 will( returnValue( prepareBuildQueue ) );
466 
467                 one( prepareBuildQueue ).getQueueSnapshot();
468                 will( returnValue( tasks ) );
469             }
470         } );
471 
472         List<PrepareBuildProjectsTask> returnedTasks = overallQueue.getProjectsInPrepareBuildQueue();
473         context.assertIsSatisfied();
474 
475         assertNotNull( returnedTasks );
476         assertEquals( 1, returnedTasks.size() );
477     }
478 
479     public void testIsInPrepareBuildQueueByProject()
480         throws Exception
481     {
482         final TaskQueue prepareBuildQueue = context.mock( TaskQueue.class, "prepare-build-queue" );
483 
484         final Map<Integer, Integer> map = new HashMap<Integer, Integer>();
485         map.put( 2, 1 );
486 
487         final List<Task> tasks = new ArrayList<Task>();
488         tasks.add( new PrepareBuildProjectsTask( map, new BuildTrigger( 1, "test-user" ), 1, "Project Group A",
489                                                  "http://scm.root.address", 2 ) );
490 
491         context.checking( new Expectations()
492         {
493             {
494                 one( prepareBuildTaskQueueExecutor ).getQueue();
495                 will( returnValue( prepareBuildQueue ) );
496 
497                 one( prepareBuildQueue ).getQueueSnapshot();
498                 will( returnValue( tasks ) );
499             }
500         } );
501 
502         assertTrue( overallQueue.isInPrepareBuildQueue( 2 ) );
503         context.assertIsSatisfied();
504     }
505 
506     public void testIsInPrepareBuildQueueByProjectGroupAndScmRootId()
507         throws Exception
508     {
509         final TaskQueue prepareBuildQueue = context.mock( TaskQueue.class, "prepare-build-queue" );
510 
511         final Map<Integer, Integer> map = new HashMap<Integer, Integer>();
512         map.put( 2, 1 );
513 
514         final List<Task> tasks = new ArrayList<Task>();
515         tasks.add( new PrepareBuildProjectsTask( map, new BuildTrigger( 1, "test-user" ), 1, "Project Group A",
516                                                  "http://scm.root.address", 2 ) );
517 
518         context.checking( new Expectations()
519         {
520             {
521                 one( prepareBuildTaskQueueExecutor ).getQueue();
522                 will( returnValue( prepareBuildQueue ) );
523 
524                 one( prepareBuildQueue ).getQueueSnapshot();
525                 will( returnValue( tasks ) );
526             }
527         } );
528 
529         assertTrue( overallQueue.isInPrepareBuildQueue( 1, 2 ) );
530         context.assertIsSatisfied();
531     }
532 
533     public void testIsInPrepareBuildQueueByProjectGroupAndScmRootAddress()
534         throws Exception
535     {
536         final TaskQueue prepareBuildQueue = context.mock( TaskQueue.class, "prepare-build-queue" );
537 
538         final Map<Integer, Integer> map = new HashMap<Integer, Integer>();
539         map.put( 2, 1 );
540 
541         final List<Task> tasks = new ArrayList<Task>();
542         tasks.add( new PrepareBuildProjectsTask( map, new BuildTrigger( 1, "test-user" ), 1, "Project Group A",
543                                                  "http://scm.root.address", 2 ) );
544 
545         context.checking( new Expectations()
546         {
547             {
548                 one( prepareBuildTaskQueueExecutor ).getQueue();
549                 will( returnValue( prepareBuildQueue ) );
550 
551                 one( prepareBuildQueue ).getQueueSnapshot();
552                 will( returnValue( tasks ) );
553             }
554         } );
555 
556         assertTrue( overallQueue.isInPrepareBuildQueue( 1, "http://scm.root.address" ) );
557         context.assertIsSatisfied();
558     }
559 
560     public void testRemoveProjectsFromPrepareBuildQueueByProjectGroupAndScmRootId()
561         throws Exception
562     {
563         final Task prepareBuildTask = new PrepareBuildProjectsTask( new HashMap<Integer, Integer>(), new BuildTrigger(
564             1, "test-user" ), 1, "Project Group A", "http://scm.root.address", 1 );
565 
566         final TaskQueue prepareBuildQueue = context.mock( TaskQueue.class, "prepare-build-queue" );
567         final List<Task> tasks = new ArrayList<Task>();
568         tasks.add( prepareBuildTask );
569 
570         context.checking( new Expectations()
571         {
572             {
573                 one( prepareBuildTaskQueueExecutor ).getQueue();
574                 will( returnValue( prepareBuildQueue ) );
575 
576                 one( prepareBuildQueue ).getQueueSnapshot();
577                 will( returnValue( tasks ) );
578 
579                 one( prepareBuildTaskQueueExecutor ).getQueue();
580                 will( returnValue( prepareBuildQueue ) );
581 
582                 one( prepareBuildQueue ).remove( prepareBuildTask );
583             }
584         } );
585 
586         overallQueue.removeProjectFromPrepareBuildQueue( 1, 1 );
587         context.assertIsSatisfied();
588     }
589 
590     public void testRemoveProjectsFromPrepareBuildQueueByProjectGroupAndScmRootAddress()
591         throws Exception
592     {
593         final Task prepareBuildTask = new PrepareBuildProjectsTask( new HashMap<Integer, Integer>(), new BuildTrigger(
594             1, "test-user" ), 1, "Project Group A", "http://scm.root.address", 1 );
595 
596         final TaskQueue prepareBuildQueue = context.mock( TaskQueue.class, "prepare-build-queue" );
597         final List<Task> tasks = new ArrayList<Task>();
598         tasks.add( prepareBuildTask );
599 
600         context.checking( new Expectations()
601         {
602             {
603                 one( prepareBuildTaskQueueExecutor ).getQueue();
604                 will( returnValue( prepareBuildQueue ) );
605 
606                 one( prepareBuildQueue ).getQueueSnapshot();
607                 will( returnValue( tasks ) );
608 
609                 one( prepareBuildTaskQueueExecutor ).getQueue();
610                 will( returnValue( prepareBuildQueue ) );
611 
612                 one( prepareBuildQueue ).remove( prepareBuildTask );
613             }
614         } );
615 
616         overallQueue.removeProjectFromPrepareBuildQueue( 1, "http://scm.root.address" );
617         context.assertIsSatisfied();
618     }
619 }