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