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.plugin.surefire.booterclient.ForkConfiguration;
24  import org.apache.maven.toolchain.Toolchain;
25  
26  import junit.framework.TestCase;
27  
28  public class SurefirePluginTest
29      extends TestCase
30  {
31  
32      public void testForkMode()
33          throws NoSuchFieldException, IllegalAccessException
34      {
35          SurefirePlugin surefirePlugin = new SurefirePlugin();
36          setFieldValue( surefirePlugin, "toolchain", new MyToolChain() );
37          setFieldValue( surefirePlugin, "forkMode", "never" );
38          assertEquals( ForkConfiguration.FORK_ONCE, surefirePlugin.getEffectiveForkMode() );
39      }
40  
41      public void testForkCountComputation()
42      {
43          SurefirePlugin surefirePlugin = new SurefirePlugin();
44          assertConversionFails( surefirePlugin, "nothing" );
45  
46          assertConversionFails( surefirePlugin, "5,0" );
47          assertConversionFails( surefirePlugin, "5.0" );
48          assertConversionFails( surefirePlugin, "5,0C" );
49          assertConversionFails( surefirePlugin, "5.0CC" );
50  
51          assertForkCount( surefirePlugin, 5, "5" );
52  
53          int availableProcessors = Runtime.getRuntime().availableProcessors();
54          assertForkCount( surefirePlugin, 3*availableProcessors, "3C" );
55          assertForkCount( surefirePlugin, (int) ( 2.5*availableProcessors ), "2.5C" );
56          assertForkCount( surefirePlugin, availableProcessors, "1.0001 C" );
57          assertForkCount( surefirePlugin, 1, 1d / ( (double) availableProcessors + 1 ) + "C" );
58          assertForkCount( surefirePlugin, 0, "0 C" );
59      }
60  
61      private void assertForkCount( SurefirePlugin surefirePlugin, int expected, String value )
62      {
63          assertEquals( expected, surefirePlugin.convertWithCoreCount( value ));
64      }
65  
66      private void assertConversionFails( SurefirePlugin surefirePlugin, String value )
67      {
68          try {
69              surefirePlugin.convertWithCoreCount( value );
70          } catch (NumberFormatException nfe)
71          {
72              return;
73          }
74          fail( "Expected NumberFormatException when converting " + value );
75      }
76  
77      private void setFieldValue( SurefirePlugin plugin, String fieldName, Object value )
78          throws NoSuchFieldException, IllegalAccessException
79      {
80          Field field = findField( plugin.getClass(), fieldName );
81          field.setAccessible( true );
82          field.set( plugin, value );
83  
84      }
85  
86      private Field findField( Class clazz, String fieldName )
87      {
88          while ( clazz != null )
89          {
90              try
91              {
92                  return clazz.getDeclaredField( fieldName );
93              }
94              catch ( NoSuchFieldException e )
95              {
96                  clazz = clazz.getSuperclass();
97              }
98          }
99          throw new IllegalArgumentException( "Field not found" );
100     }
101 
102     private class MyToolChain
103         implements Toolchain
104     {
105         public String getType()
106         {
107             return null;
108         }
109 
110         public String findTool( String s )
111         {
112             return null;
113         }
114     }
115 }