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.HashMap;
23  import java.util.Map;
24  
25  import org.junit.Test;
26  
27  import static org.junit.Assert.*;
28  import static org.hamcrest.core.Is.is;
29  
30  /*
31   * @author Kristian Rosenvold, kristian.rosenvold@gmail com
32   */
33  public class JUnitCoreParametersTest
34  {
35      @Test
36      public void defaultParameters()
37      {
38          assertFalse( newTestSetDefault().isParallelismSelected() );
39          assertTrue( newTestSetDefault().isPerCoreThreadCount() );
40          assertThat( newTestSetDefault().getThreadCount(), is( 0 ) );
41          assertThat( newTestSetDefault().getThreadCountMethods(), is( 0 ) );
42          assertThat( newTestSetDefault().getThreadCountClasses(), is( 0 ) );
43          assertThat( newTestSetDefault().getThreadCountSuites(), is( 0 ) );
44          assertFalse( newTestSetDefault().isUseUnlimitedThreads() );
45          assertThat( newTestSetDefault().getParallelTestsTimeoutInSeconds(), is( 0d ) );
46          assertThat( newTestSetDefault().getParallelTestsTimeoutForcedInSeconds(), is( 0d ) );
47          assertTrue( newTestSetDefault().isParallelOptimization() );
48      }
49  
50      @Test
51      public void optimizationParameter()
52      {
53          assertFalse( newTestSetOptimization( false ).isParallelOptimization() );
54      }
55  
56      @Test
57      public void timeoutParameters()
58      {
59          JUnitCoreParameters parameters = newTestSetTimeouts( 5.5d, 11.1d );
60          assertThat( parameters.getParallelTestsTimeoutInSeconds(), is( 5.5d ) );
61          assertThat( parameters.getParallelTestsTimeoutForcedInSeconds(), is( 11.1d ) );
62      }
63  
64      @Test
65      public void isParallelMethod()
66      {
67          assertFalse( newTestSetClasses().isParallelMethods() );
68          assertTrue( newTestSetMethods().isParallelMethods() );
69          assertTrue( newTestSetBoth().isParallelMethods() );
70      }
71  
72      @Test
73      public void isParallelClasses()
74      {
75          assertTrue( newTestSetClasses().isParallelClasses() );
76          assertFalse( newTestSetMethods().isParallelClasses() );
77          assertTrue( newTestSetBoth().isParallelClasses() );
78      }
79  
80      @Test
81      public void isParallelBoth()
82      {
83          assertFalse( isParallelMethodsAndClasses( newTestSetClasses() ) );
84          assertFalse( isParallelMethodsAndClasses( newTestSetMethods() ) );
85          assertTrue( isParallelMethodsAndClasses( newTestSetBoth() ) );
86      }
87  
88      @Test
89      public void isPerCoreThreadCount()
90      {
91          assertFalse( newTestSetClasses().isPerCoreThreadCount() );
92          assertFalse( newTestSetMethods().isPerCoreThreadCount() );
93          assertTrue( newTestSetBoth().isPerCoreThreadCount() );
94      }
95  
96      @Test
97      public void getThreadCount()
98      {
99          assertFalse( newTestSetClasses().isPerCoreThreadCount() );
100         assertFalse( newTestSetMethods().isPerCoreThreadCount() );
101         assertTrue( newTestSetBoth().isPerCoreThreadCount() );
102     }
103 
104     @Test
105     public void isUseUnlimitedThreads()
106     {
107         assertFalse( newTestSetClasses().isUseUnlimitedThreads() );
108         assertTrue( newTestSetMethods().isUseUnlimitedThreads() );
109         assertFalse( newTestSetBoth().isUseUnlimitedThreads() );
110     }
111 
112     @Test
113     public void isNoThreading()
114     {
115         assertFalse( newTestSetClasses().isNoThreading() );
116         assertFalse( newTestSetMethods().isNoThreading() );
117         assertFalse( newTestSetBoth().isNoThreading() );
118     }
119 
120     @Test
121     public void isAnyParallelismSelected()
122     {
123         assertTrue( newTestSetClasses().isParallelismSelected() );
124         assertTrue( newTestSetMethods().isParallelismSelected() );
125         assertTrue( newTestSetBoth().isParallelismSelected() );
126     }
127 
128     private Map<String, String> newDefaultProperties()
129     {
130         return new HashMap<>();
131     }
132 
133 
134     private Map<String, String> newPropertiesClasses()
135     {
136         Map<String, String> props = new HashMap<>();
137         props.put(JUnitCoreParameters.PARALLEL_KEY, "classes");
138         props.put(JUnitCoreParameters.PERCORETHREADCOUNT_KEY, "false");
139         props.put(JUnitCoreParameters.THREADCOUNT_KEY, "2");
140         props.put(JUnitCoreParameters.USEUNLIMITEDTHREADS_KEY, "false");
141         return props;
142     }
143 
144     private Map<String, String> newPropertiesMethods()
145     {
146         Map<String, String> props = new HashMap<>();
147         props.put(JUnitCoreParameters.PARALLEL_KEY, "methods");
148         props.put(JUnitCoreParameters.PERCORETHREADCOUNT_KEY, "false");
149         props.put(JUnitCoreParameters.THREADCOUNT_KEY, "2");
150         props.put(JUnitCoreParameters.USEUNLIMITEDTHREADS_KEY, "true");
151         return props;
152     }
153 
154     private Map<String, String> newPropertiesBoth()
155     {
156         Map<String, String> props = new HashMap<>();
157         props.put(JUnitCoreParameters.PARALLEL_KEY, "both");
158         props.put(JUnitCoreParameters.PERCORETHREADCOUNT_KEY, "true");
159         props.put(JUnitCoreParameters.THREADCOUNT_KEY, "7");
160         props.put(JUnitCoreParameters.USEUNLIMITEDTHREADS_KEY, "false");
161         return props;
162     }
163 
164     private Map<String, String> newPropertiesTimeouts( double timeout, double forcedTimeout )
165     {
166         Map<String, String> props = new HashMap<>();
167         props.put(JUnitCoreParameters.PARALLEL_TIMEOUT_KEY, Double.toString(timeout));
168         props.put(JUnitCoreParameters.PARALLEL_TIMEOUTFORCED_KEY, Double.toString(forcedTimeout));
169         return props;
170     }
171 
172     private Map<String, String> newPropertiesOptimization( boolean optimize )
173     {
174         Map<String, String> props = new HashMap<>();
175         props.put( JUnitCoreParameters.PARALLEL_OPTIMIZE_KEY, Boolean.toString( optimize ) );
176         return props;
177     }
178 
179     private JUnitCoreParameters newTestSetDefault()
180     {
181         return new JUnitCoreParameters( newDefaultProperties() );
182     }
183 
184     private JUnitCoreParameters newTestSetBoth()
185     {
186         return new JUnitCoreParameters( newPropertiesBoth() );
187     }
188 
189     private JUnitCoreParameters newTestSetClasses()
190     {
191         return new JUnitCoreParameters( newPropertiesClasses() );
192     }
193 
194     private JUnitCoreParameters newTestSetMethods()
195     {
196         return new JUnitCoreParameters( newPropertiesMethods() );
197     }
198 
199     private JUnitCoreParameters newTestSetOptimization( boolean optimize )
200     {
201         return new JUnitCoreParameters( newPropertiesOptimization( optimize ) );
202     }
203 
204     private JUnitCoreParameters newTestSetTimeouts( double timeout, double forcedTimeout )
205     {
206         return new JUnitCoreParameters( newPropertiesTimeouts( timeout, forcedTimeout ) );
207     }
208 
209     private boolean isParallelMethodsAndClasses( JUnitCoreParameters jUnitCoreParameters )
210     {
211         return jUnitCoreParameters.isParallelMethods() && jUnitCoreParameters.isParallelClasses();
212     }
213 }