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