View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.invoker;
20  
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.concurrent.atomic.AtomicInteger;
27  
28  import org.apache.maven.plugins.invoker.model.BuildJob;
29  import org.junit.Test;
30  
31  import static org.assertj.core.api.Assertions.assertThat;
32  import static org.assertj.core.api.Assertions.fail;
33  
34  /**
35   * Tests for JobExecutor.
36   *
37   * @author Slawomir Jaranowski
38   */
39  public class JobExecutorTest {
40  
41      @Test
42      public void emptyJobList() {
43          JobExecutor jobExecutor = new JobExecutor(Collections.emptyList(), 1);
44  
45          jobExecutor.forEach(job -> fail("fail"));
46      }
47  
48      @Test
49      public void failedJob() {
50          BuildJob job = aJob("job1", 100);
51  
52          JobExecutor jobExecutor = new JobExecutor(Collections.singletonList(job), 1);
53  
54          jobExecutor.forEach(j -> fail("fail " + j.getProject()));
55  
56          assertThat(job.getResult()).isEqualTo(BuildJob.Result.ERROR);
57          assertThat(job.getFailureMessage()).isEqualTo("java.lang.AssertionError: fail job1");
58      }
59  
60      @Test
61      public void jobsShouldBeGroupedAndExecutedInProperOrder() {
62          Map<Integer, AtomicInteger> jobsCounter = new HashMap<>();
63          jobsCounter.put(100, new AtomicInteger(3));
64          jobsCounter.put(10, new AtomicInteger(2));
65          jobsCounter.put(1, new AtomicInteger(1));
66  
67          BuildJob job1 = aJob("job1-100", 100);
68          BuildJob job2 = aJob("job2-100", 100);
69          BuildJob job3 = aJob("job3-100", 100);
70  
71          BuildJob job4 = aJob("job4-10", 10);
72          BuildJob job5 = aJob("job5-10", 10);
73  
74          BuildJob job6 = aJob("job6-1", 1);
75  
76          // put jobs in wrong order
77          List<BuildJob> jobs = Arrays.asList(job4, job5, job1, job2, job6, job3);
78  
79          JobExecutor jobExecutor = new JobExecutor(jobs, 10);
80  
81          jobExecutor.forEach(job -> {
82              jobsCounter.get(job.getOrdinal()).decrementAndGet();
83  
84              switch (job.getOrdinal()) {
85                  case 100:
86                      assertThat(jobsCounter.get(10).get())
87                              .as("Jobs-10 must not be executed before 100")
88                              .isEqualTo(2);
89  
90                      assertThat(jobsCounter.get(1).get())
91                              .as("Jobs-1 must not be executed before 100")
92                              .isEqualTo(1);
93                      break;
94  
95                  case 10:
96                      assertThat(jobsCounter.get(100).get())
97                              .as("Jobs-100 must be executed before 10")
98                              .isZero();
99  
100                     assertThat(jobsCounter.get(1).get())
101                             .as("Jobs-1 must not be executed before 10")
102                             .isEqualTo(1);
103                     break;
104 
105                 case 1:
106                     assertThat(jobsCounter.get(100).get())
107                             .as("Jobs-100 must be executed before 1")
108                             .isZero();
109 
110                     assertThat(jobsCounter.get(10).get())
111                             .as("Jobs-10 must be executed before 1")
112                             .isZero();
113                     break;
114 
115                 default:
116                     fail("invalid job ordinal value %d", job.getOrdinal());
117                     break;
118             }
119 
120             job.setResult(BuildJob.Result.SUCCESS);
121             job.setDescription(Thread.currentThread().getName());
122         });
123 
124         // all jobs have success status
125         assertThat(jobs).allSatisfy(job -> {
126             assertThat(job.getDescription()).isNotBlank();
127             assertThat(job.getResult()).as(job.getFailureMessage()).isEqualTo(BuildJob.Result.SUCCESS);
128         });
129 
130         // jobs run on separate thread
131         assertThat(job1.getDescription()).isNotEqualTo(job2.getDescription());
132         assertThat(job1.getDescription()).isNotEqualTo(job3.getDescription());
133         assertThat(job2.getDescription()).isNotEqualTo(job3.getDescription());
134 
135         assertThat(job4.getDescription()).isNotEqualTo(job5.getDescription());
136     }
137 
138     private BuildJob aJob(String name, int ordinal) {
139         BuildJob buildJob = new BuildJob(name);
140         buildJob.setOrdinal(ordinal);
141         return buildJob;
142     }
143 }