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      public static final String ENABLE_OUT_ERR_ELEMENTS_KEY = ProviderParameterNames.ENABLE_OUT_ERR_ELEMENTS_PROP;
52  
53      public static final String ENABLE_PROPERTIES_ELEMENT_KEY = ProviderParameterNames.ENABLE_PROPERTIES_ELEMENT_PROP;
54  
55      private final String parallel;
56  
57      private final boolean perCoreThreadCount;
58  
59      private final int threadCount;
60  
61      private final int threadCountSuites;
62  
63      private final int threadCountClasses;
64  
65      private final int threadCountMethods;
66  
67      private final double parallelTestsTimeoutInSeconds;
68  
69      private final double parallelTestsTimeoutForcedInSeconds;
70  
71      private final boolean useUnlimitedThreads;
72  
73      private final boolean parallelOptimization;
74  
75      private final boolean enableOutErrElements;
76  
77      private final boolean enablePropertiesElement;
78  
79      public JUnitCoreParameters(Map<String, String> properties) {
80          parallel = property(properties, PARALLEL_KEY, "none").toLowerCase();
81          perCoreThreadCount = property(properties, PERCORETHREADCOUNT_KEY, true);
82          threadCount = property(properties, THREADCOUNT_KEY, 0);
83          threadCountMethods = property(properties, THREADCOUNTMETHODS_KEY, 0);
84          threadCountClasses = property(properties, THREADCOUNTCLASSES_KEY, 0);
85          threadCountSuites = property(properties, THREADCOUNTSUITES_KEY, 0);
86          useUnlimitedThreads = property(properties, USEUNLIMITEDTHREADS_KEY, false);
87          parallelTestsTimeoutInSeconds = Math.max(property(properties, PARALLEL_TIMEOUT_KEY, 0d), 0);
88          parallelTestsTimeoutForcedInSeconds = Math.max(property(properties, PARALLEL_TIMEOUTFORCED_KEY, 0d), 0);
89          parallelOptimization = property(properties, PARALLEL_OPTIMIZE_KEY, true);
90          enableOutErrElements = property(properties, ENABLE_OUT_ERR_ELEMENTS_KEY, true);
91          enablePropertiesElement = property(properties, ENABLE_PROPERTIES_ELEMENT_KEY, true);
92      }
93  
94      private static Collection<String> lowerCase(String... elements) {
95          ArrayList<String> lowerCase = new ArrayList<>();
96          for (String element : elements) {
97              lowerCase.add(element.toLowerCase());
98          }
99          return lowerCase;
100     }
101 
102     private boolean isAllParallel() {
103         return "all".equals(parallel);
104     }
105 
106     public boolean isParallelMethods() {
107         return isAllParallel()
108                 || lowerCase("both", "methods", "suitesAndMethods", "classesAndMethods")
109                         .contains(parallel);
110     }
111 
112     public boolean isParallelClasses() {
113         return isAllParallel()
114                 || lowerCase("both", "classes", "suitesAndClasses", "classesAndMethods")
115                         .contains(parallel);
116     }
117 
118     public boolean isParallelSuites() {
119         return isAllParallel()
120                 || lowerCase("suites", "suitesAndClasses", "suitesAndMethods").contains(parallel);
121     }
122 
123     /**
124      * @deprecated Instead use the expression isParallelMethods() &amp;&amp; isParallelClasses().
125      * @return {@code true} if classes and methods are both parallel
126      */
127     @Deprecated
128     @SuppressWarnings("unused")
129     public boolean isParallelBoth() {
130         return isParallelMethods() && isParallelClasses();
131     }
132 
133     public boolean isPerCoreThreadCount() {
134         return perCoreThreadCount;
135     }
136 
137     public int getThreadCount() {
138         return threadCount;
139     }
140 
141     public int getThreadCountMethods() {
142         return threadCountMethods;
143     }
144 
145     public int getThreadCountClasses() {
146         return threadCountClasses;
147     }
148 
149     public int getThreadCountSuites() {
150         return threadCountSuites;
151     }
152 
153     public boolean isUseUnlimitedThreads() {
154         return useUnlimitedThreads;
155     }
156 
157     public double getParallelTestsTimeoutInSeconds() {
158         return parallelTestsTimeoutInSeconds;
159     }
160 
161     public double getParallelTestsTimeoutForcedInSeconds() {
162         return parallelTestsTimeoutForcedInSeconds;
163     }
164 
165     public boolean isNoThreading() {
166         return !isParallelismSelected();
167     }
168 
169     public boolean isParallelismSelected() {
170         return isParallelSuites() || isParallelClasses() || isParallelMethods();
171     }
172 
173     public boolean isParallelOptimization() {
174         return parallelOptimization;
175     }
176 
177     public boolean isEnableOutErrElements() {
178         return enableOutErrElements;
179     }
180 
181     public boolean isEnablePropertiesElement() {
182         return enablePropertiesElement;
183     }
184 
185     @Override
186     public String toString() {
187         return "parallel='" + parallel + '\'' + ", perCoreThreadCount=" + perCoreThreadCount + ", threadCount="
188                 + threadCount + ", useUnlimitedThreads=" + useUnlimitedThreads + ", threadCountSuites="
189                 + threadCountSuites
190                 + ", threadCountClasses=" + threadCountClasses + ", threadCountMethods=" + threadCountMethods
191                 + ", parallelOptimization=" + parallelOptimization
192                 + ", enableOutErrElements=" + enableOutErrElements
193                 + ", enablePropertiesElement=" + enablePropertiesElement;
194     }
195 
196     private static boolean property(Map<String, String> properties, String key, boolean fallback) {
197         return properties.containsKey(key) ? Boolean.valueOf(properties.get(key)) : fallback;
198     }
199 
200     private static String property(Map<String, String> properties, String key, String fallback) {
201         return properties.containsKey(key) ? properties.get(key) : fallback;
202     }
203 
204     private static int property(Map<String, String> properties, String key, int fallback) {
205         return properties.containsKey(key) ? Integer.valueOf(properties.get(key)) : fallback;
206     }
207 
208     private static double property(Map<String, String> properties, String key, double fallback) {
209         return properties.containsKey(key) ? Double.valueOf(properties.get(key)) : fallback;
210     }
211 }