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