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.surefire.api.runorder;
20  
21  import java.util.List;
22  
23  import junit.framework.TestCase;
24  import org.apache.maven.surefire.api.util.internal.ClassMethod;
25  
26  /**
27   * @author Kristian Rosenvold
28   */
29  public class ThreadedExecutionSchedulerTest extends TestCase {
30  
31      private final RunEntryStatistics a1 = fromValues(200, 2, A.class, "at1");
32  
33      private final RunEntryStatistics a2 = fromValues(300, 2, A.class, "at2");
34  
35      private final RunEntryStatistics b1 = fromValues(400, 2, B.class, "bt1");
36  
37      private final RunEntryStatistics b2 = fromValues(300, 2, B.class, "bt2");
38  
39      private final RunEntryStatistics c1 = fromValues(400, 2, C.class, "ct1");
40  
41      private final RunEntryStatistics c2 = fromValues(200, 2, C.class, "ct2");
42  
43      private final RunEntryStatistics d1 = fromValues(401, 2, D.class, "ct2");
44  
45      private final RunEntryStatistics e1 = fromValues(200, 2, E.class, "ct2");
46  
47      public void testAddTest() {
48          ThreadedExecutionScheduler threadedExecutionScheduler = new ThreadedExecutionScheduler(2);
49          addPrioritizedTests(threadedExecutionScheduler);
50          final List<Class<?>> result = threadedExecutionScheduler.getResult();
51          assertEquals(5, result.size());
52          assertEquals(B.class, result.get(0));
53          assertEquals(C.class, result.get(1));
54          assertEquals(D.class, result.get(2));
55          assertEquals(A.class, result.get(3));
56          assertEquals(E.class, result.get(4));
57      }
58  
59      public void testAddTestJaggedResult() {
60          ThreadedExecutionScheduler threadedExecutionScheduler = new ThreadedExecutionScheduler(4);
61          addPrioritizedTests(threadedExecutionScheduler);
62          final List<Class<?>> result = threadedExecutionScheduler.getResult();
63          assertEquals(5, result.size());
64      }
65  
66      private void addPrioritizedTests(ThreadedExecutionScheduler threadedExecutionScheduler) {
67          threadedExecutionScheduler.addTest(new PrioritizedTest(B.class, createPriority(b1, b2)));
68          threadedExecutionScheduler.addTest(new PrioritizedTest(C.class, createPriority(c1, c2)));
69          threadedExecutionScheduler.addTest(new PrioritizedTest(A.class, createPriority(a1, a2)));
70          threadedExecutionScheduler.addTest(new PrioritizedTest(D.class, createPriority(d1)));
71          threadedExecutionScheduler.addTest(new PrioritizedTest(E.class, createPriority(e1)));
72      }
73  
74      private Priority createPriority(RunEntryStatistics runEntryStatistics) {
75          final Priority priority = new Priority(A.class.getName());
76          priority.addItem(runEntryStatistics);
77          return priority;
78      }
79  
80      private Priority createPriority(RunEntryStatistics runEntryStatistics, RunEntryStatistics runEntryStatistics2) {
81          final Priority priority = new Priority(A.class.getName());
82          priority.addItem(runEntryStatistics);
83          priority.addItem(runEntryStatistics2);
84          return priority;
85      }
86  
87      private static RunEntryStatistics fromValues(int runTime, int successfulBuilds, Class clazz, String testName) {
88          ClassMethod classMethod = new ClassMethod(clazz.getName(), testName);
89          return new RunEntryStatistics(runTime, successfulBuilds, classMethod);
90      }
91  
92      class A {} // 500 total
93  
94      class B {} // 700 total
95  
96      class C {} // 600  total
97  
98      class D {} // 400 total
99  
100     class E {} // 200 total
101 }