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.junitcore;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Map;
24  
25  import org.apache.maven.surefire.api.booter.ProviderParameterNames;
26  
27  /**
28   * @author Kristian Rosenvold
29   */
30  public final class JUnitCoreParameters {
31      public static final String PARALLEL_KEY = ProviderParameterNames.PARALLEL_PROP;
32  
33      public static final String PERCORETHREADCOUNT_KEY = "perCoreThreadCount";
34  
35      public static final String THREADCOUNT_KEY = ProviderParameterNames.THREADCOUNT_PROP;
36  
37      public static final String THREADCOUNTSUITES_KEY = ProviderParameterNames.THREADCOUNTSUITES_PROP;
38  
39      public static final String THREADCOUNTCLASSES_KEY = ProviderParameterNames.THREADCOUNTCLASSES_PROP;
40  
41      public static final String THREADCOUNTMETHODS_KEY = ProviderParameterNames.THREADCOUNTMETHODS_PROP;
42  
43      public static final String USEUNLIMITEDTHREADS_KEY = "useUnlimitedThreads";
44  
45      public static final String PARALLEL_TIMEOUT_KEY = ProviderParameterNames.PARALLEL_TIMEOUT_PROP;
46  
47      public static final String PARALLEL_TIMEOUTFORCED_KEY = ProviderParameterNames.PARALLEL_TIMEOUTFORCED_PROP;
48  
49      public static final String PARALLEL_OPTIMIZE_KEY = ProviderParameterNames.PARALLEL_OPTIMIZE_PROP;
50  
51      private final String parallel;
52  
53      private final boolean perCoreThreadCount;
54  
55      private final int threadCount;
56  
57      private final int threadCountSuites;
58  
59      private final int threadCountClasses;
60  
61      private final int threadCountMethods;
62  
63      private final double parallelTestsTimeoutInSeconds;
64  
65      private final double parallelTestsTimeoutForcedInSeconds;
66  
67      private final boolean useUnlimitedThreads;
68  
69      private final boolean parallelOptimization;
70  
71      public JUnitCoreParameters(Map<String, String> properties) {
72          parallel = property(properties, PARALLEL_KEY, "none").toLowerCase();
73          perCoreThreadCount = property(properties, PERCORETHREADCOUNT_KEY, true);
74          threadCount = property(properties, THREADCOUNT_KEY, 0);
75          threadCountMethods = property(properties, THREADCOUNTMETHODS_KEY, 0);
76          threadCountClasses = property(properties, THREADCOUNTCLASSES_KEY, 0);
77          threadCountSuites = property(properties, THREADCOUNTSUITES_KEY, 0);
78          useUnlimitedThreads = property(properties, USEUNLIMITEDTHREADS_KEY, false);
79          parallelTestsTimeoutInSeconds = Math.max(property(properties, PARALLEL_TIMEOUT_KEY, 0d), 0);
80          parallelTestsTimeoutForcedInSeconds = Math.max(property(properties, PARALLEL_TIMEOUTFORCED_KEY, 0d), 0);
81          parallelOptimization = property(properties, PARALLEL_OPTIMIZE_KEY, true);
82      }
83  
84      private static Collection<String> lowerCase(String... elements) {
85          ArrayList<String> lowerCase = new ArrayList<>();
86          for (String element : elements) {
87              lowerCase.add(element.toLowerCase());
88          }
89          return lowerCase;
90      }
91  
92      private boolean isAllParallel() {
93          return "all".equals(parallel);
94      }
95  
96      public boolean isParallelMethods() {
97          return isAllParallel()
98                  || lowerCase("both", "methods", "suitesAndMethods", "classesAndMethods")
99                          .contains(parallel);
100     }
101 
102     public boolean isParallelClasses() {
103         return isAllParallel()
104                 || lowerCase("both", "classes", "suitesAndClasses", "classesAndMethods")
105                         .contains(parallel);
106     }
107 
108     public boolean isParallelSuites() {
109         return isAllParallel()
110                 || lowerCase("suites", "suitesAndClasses", "suitesAndMethods").contains(parallel);
111     }
112 
113     /**
114      * @deprecated Instead use the expression isParallelMethods() &amp;&amp; isParallelClasses().
115      * @return {@code true} if classes and methods are both parallel
116      */
117     @Deprecated
118     @SuppressWarnings("unused")
119     public boolean isParallelBoth() {
120         return isParallelMethods() && isParallelClasses();
121     }
122 
123     public boolean isPerCoreThreadCount() {
124         return perCoreThreadCount;
125     }
126 
127     public int getThreadCount() {
128         return threadCount;
129     }
130 
131     public int getThreadCountMethods() {
132         return threadCountMethods;
133     }
134 
135     public int getThreadCountClasses() {
136         return threadCountClasses;
137     }
138 
139     public int getThreadCountSuites() {
140         return threadCountSuites;
141     }
142 
143     public boolean isUseUnlimitedThreads() {
144         return useUnlimitedThreads;
145     }
146 
147     public double getParallelTestsTimeoutInSeconds() {
148         return parallelTestsTimeoutInSeconds;
149     }
150 
151     public double getParallelTestsTimeoutForcedInSeconds() {
152         return parallelTestsTimeoutForcedInSeconds;
153     }
154 
155     public boolean isNoThreading() {
156         return !isParallelismSelected();
157     }
158 
159     public boolean isParallelismSelected() {
160         return isParallelSuites() || isParallelClasses() || isParallelMethods();
161     }
162 
163     public boolean isParallelOptimization() {
164         return parallelOptimization;
165     }
166 
167     @Override
168     public String toString() {
169         return "parallel='" + parallel + '\'' + ", perCoreThreadCount=" + perCoreThreadCount + ", threadCount="
170                 + threadCount + ", useUnlimitedThreads=" + useUnlimitedThreads + ", threadCountSuites="
171                 + threadCountSuites
172                 + ", threadCountClasses=" + threadCountClasses + ", threadCountMethods=" + threadCountMethods
173                 + ", parallelOptimization=" + parallelOptimization;
174     }
175 
176     private static boolean property(Map<String, String> properties, String key, boolean fallback) {
177         return properties.containsKey(key) ? Boolean.valueOf(properties.get(key)) : fallback;
178     }
179 
180     private static String property(Map<String, String> properties, String key, String fallback) {
181         return properties.containsKey(key) ? properties.get(key) : fallback;
182     }
183 
184     private static int property(Map<String, String> properties, String key, int fallback) {
185         return properties.containsKey(key) ? Integer.valueOf(properties.get(key)) : fallback;
186     }
187 
188     private static double property(Map<String, String> properties, String key, double fallback) {
189         return properties.containsKey(key) ? Double.valueOf(properties.get(key)) : fallback;
190     }
191 }