View Javadoc
1   package org.apache.maven.plugin.surefire;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  
22  import java.lang.reflect.Field;
23  import org.apache.maven.toolchain.Toolchain;
24  
25  import junit.framework.TestCase;
26  
27  public class SurefirePluginTest
28      extends TestCase
29  {
30  
31      public void testForkMode()
32          throws NoSuchFieldException, IllegalAccessException
33      {
34          SurefirePlugin surefirePlugin = new SurefirePlugin();
35          setFieldValue( surefirePlugin, "toolchain", new MyToolChain() );
36          setFieldValue( surefirePlugin, "forkMode", "never" );
37          assertEquals( "once", surefirePlugin.getEffectiveForkMode() );
38      }
39  
40      public void testForkCountComputation()
41      {
42          SurefirePlugin surefirePlugin = new SurefirePlugin();
43          assertConversionFails( surefirePlugin, "nothing" );
44  
45          assertConversionFails( surefirePlugin, "5,0" );
46          assertConversionFails( surefirePlugin, "5.0" );
47          assertConversionFails( surefirePlugin, "5,0C" );
48          assertConversionFails( surefirePlugin, "5.0CC" );
49  
50          assertForkCount( surefirePlugin, 5, "5" );
51  
52          int availableProcessors = Runtime.getRuntime().availableProcessors();
53          assertForkCount( surefirePlugin, 3*availableProcessors, "3C" );
54          assertForkCount( surefirePlugin, (int) ( 2.5*availableProcessors ), "2.5C" );
55          assertForkCount( surefirePlugin, availableProcessors, "1.0001 C" );
56          assertForkCount( surefirePlugin, 1, 1d / ( (double) availableProcessors + 1 ) + "C" );
57          assertForkCount( surefirePlugin, 0, "0 C" );
58      }
59  
60      private void assertForkCount( SurefirePlugin surefirePlugin, int expected, String value )
61      {
62          assertEquals( expected, surefirePlugin.convertWithCoreCount( value ));
63      }
64  
65      private void assertConversionFails( SurefirePlugin surefirePlugin, String value )
66      {
67          try {
68              surefirePlugin.convertWithCoreCount( value );
69          } catch (NumberFormatException nfe)
70          {
71              return;
72          }
73          fail( "Expected NumberFormatException when converting " + value );
74      }
75  
76      private void setFieldValue( SurefirePlugin plugin, String fieldName, Object value )
77          throws NoSuchFieldException, IllegalAccessException
78      {
79          Field field = findField( plugin.getClass(), fieldName );
80          field.setAccessible( true );
81          field.set( plugin, value );
82  
83      }
84  
85      private Field findField( Class clazz, String fieldName )
86      {
87          while ( clazz != null )
88          {
89              try
90              {
91                  return clazz.getDeclaredField( fieldName );
92              }
93              catch ( NoSuchFieldException e )
94              {
95                  clazz = clazz.getSuperclass();
96              }
97          }
98          throw new IllegalArgumentException( "Field not found" );
99      }
100 
101     private class MyToolChain
102         implements Toolchain
103     {
104         @Override
105         public String getType()
106         {
107             return null;
108         }
109 
110         @Override
111         public String findTool( String s )
112         {
113             return null;
114         }
115     }
116 }