View Javadoc
1   package org.apache.maven.plugins.invoker;
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.util.Arrays;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.concurrent.atomic.AtomicInteger;
28  
29  import org.apache.maven.plugins.invoker.model.BuildJob;
30  import org.junit.Test;
31  
32  import static org.assertj.core.api.Assertions.assertThat;
33  import static org.assertj.core.api.Assertions.fail;
34  
35  /**
36   * Tests for JobExecutor.
37   *
38   * @author Slawomir Jaranowski
39   */
40  public class JobExecutorTest
41  {
42  
43      @Test
44      public void emptyJobList()
45      {
46          JobExecutor jobExecutor = new JobExecutor( Collections.emptyList(), 1 );
47  
48          jobExecutor.forEach( job -> fail( "fail" ) );
49      }
50  
51      @Test
52      public void failedJob()
53      {
54          BuildJob job = aJob( "job1", 100 );
55  
56          JobExecutor jobExecutor = new JobExecutor( Collections.singletonList( job ), 1 );
57  
58          jobExecutor.forEach( j -> fail( "fail " + j.getProject() ) );
59  
60          assertThat( job.getResult() ).isEqualTo( BuildJob.Result.ERROR );
61          assertThat( job.getFailureMessage() ).isEqualTo( "java.lang.AssertionError: fail job1" );
62      }
63  
64      @Test
65      public void jobsShouldBeGroupedAndExecutedInProperOrder()
66      {
67          Map<Integer, AtomicInteger> jobsCounter = new HashMap<>();
68          jobsCounter.put( 100, new AtomicInteger( 3 ) );
69          jobsCounter.put( 10, new AtomicInteger( 2 ) );
70          jobsCounter.put( 1, new AtomicInteger( 1 ) );
71  
72          BuildJob job1 = aJob( "job1-100", 100 );
73          BuildJob job2 = aJob( "job2-100", 100 );
74          BuildJob job3 = aJob( "job3-100", 100 );
75  
76          BuildJob job4 = aJob( "job4-10", 10 );
77          BuildJob job5 = aJob( "job5-10", 10 );
78  
79          BuildJob job6 = aJob( "job6-1", 1 );
80  
81          // put jobs in wrong order
82          List<BuildJob> jobs = Arrays.asList(
83              job4, job5,
84              job1, job2,
85              job6,
86              job3
87          );
88  
89          JobExecutor jobExecutor = new JobExecutor( jobs, 10 );
90  
91          jobExecutor.forEach( job ->
92          {
93              jobsCounter.get( job.getOrdinal() ).decrementAndGet();
94  
95              switch ( job.getOrdinal() )
96              {
97                  case 100:
98                      assertThat( jobsCounter.get( 10 ).get() )
99                          .as( "Jobs-10 must not be executed before 100" )
100                         .isEqualTo( 2 );
101 
102                     assertThat( jobsCounter.get( 1 ).get() )
103                         .as( "Jobs-1 must not be executed before 100" )
104                         .isEqualTo( 1 );
105                     break;
106 
107                 case 10:
108                     assertThat( jobsCounter.get( 100 ).get() )
109                         .as( "Jobs-100 must be executed before 10" )
110                         .isZero();
111 
112                     assertThat( jobsCounter.get( 1 ).get() )
113                         .as( "Jobs-1 must not be executed before 10" )
114                         .isEqualTo( 1 );
115                     break;
116 
117                 case 1:
118                     assertThat( jobsCounter.get( 100 ).get() )
119                         .as( "Jobs-100 must be executed before 1" )
120                         .isZero();
121 
122                     assertThat( jobsCounter.get( 10 ).get() )
123                         .as( "Jobs-10 must be executed before 1" )
124                         .isZero();
125                     break;
126 
127                 default:
128                     fail( "invalid job ordinal value %d", job.getOrdinal() );
129                     break;
130             }
131 
132             job.setResult( BuildJob.Result.SUCCESS );
133             job.setDescription( Thread.currentThread().getName() );
134         } );
135 
136         // all jobs have success status
137         assertThat( jobs ).allSatisfy( job ->
138         {
139             assertThat( job.getDescription() ).isNotBlank();
140             assertThat( job.getResult() ).as( job.getFailureMessage() ).isEqualTo( BuildJob.Result.SUCCESS );
141         } );
142 
143         // jobs run on separate thread
144         assertThat( job1.getDescription() ).isNotEqualTo( job2.getDescription() );
145         assertThat( job1.getDescription() ).isNotEqualTo( job3.getDescription() );
146         assertThat( job2.getDescription() ).isNotEqualTo( job3.getDescription() );
147 
148         assertThat( job4.getDescription() ).isNotEqualTo( job5.getDescription() );
149     }
150 
151     private BuildJob aJob( String name, int ordinal )
152     {
153         BuildJob buildJob = new BuildJob( name );
154         buildJob.setOrdinal( ordinal );
155         return buildJob;
156     }
157 }