Coverage Report - org.apache.maven.surefire.junitcore.JUnitCoreTestSet
 
Classes in this File Line Coverage Branch Coverage Complexity
JUnitCoreTestSet
0%
0/57
0%
0/10
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.maven.surefire.junitcore;
 20  
 
 21  
 
 22  
 import org.apache.maven.surefire.report.ReporterManagerFactory;
 23  
 import org.apache.maven.surefire.testset.TestSetFailedException;
 24  
 import org.junit.experimental.ParallelComputer;
 25  
 import org.junit.runner.Computer;
 26  
 import org.junit.runner.JUnitCore;
 27  
 import org.junit.runner.notification.RunListener;
 28  
 
 29  
 import java.lang.reflect.Constructor;
 30  
 import java.lang.reflect.InvocationTargetException;
 31  
 import java.lang.reflect.Method;
 32  
 
 33  
 
 34  
 /**
 35  
  * Writes out a specific {@link org.junit.runner.notification.Failure} for
 36  
  * surefire as a stacktrace.
 37  
  *
 38  
  * @author Karl M. Davis
 39  
  * @author Kristian Rosenvold (junit core adaption)
 40  
  */
 41  
 
 42  
 class JUnitCoreTestSet
 43  
 {
 44  
     private final Class testClass;
 45  
 
 46  
     private static final String className = "org.jdogma.junit.ConfigurableParallelComputer";
 47  
 
 48  
     public String getName()
 49  
     {
 50  0
         return testClass.getName();
 51  
     }
 52  
 
 53  
     Class getTestClass()
 54  
     {
 55  0
         return testClass;
 56  
     }
 57  
 
 58  
     /**
 59  
      * Constructor.
 60  
      *
 61  
      * @param testClasses the classes to be run as a test
 62  
      */
 63  
     protected JUnitCoreTestSet( Class testClasses )
 64  0
     {
 65  0
         this.testClass = testClasses;
 66  0
     }
 67  
 
 68  
     /**
 69  
      * Actually runs the test and adds the tests results to the <code>reportManager</code>.
 70  
      *
 71  
      * @param reportManager       The report manager
 72  
      * @param JUnitCoreParameters The parameters for this test
 73  
      * @throws TestSetFailedException If something fails
 74  
      * @see org.apache.maven.surefire.testset.SurefireTestSet#execute(org.apache.maven.surefire.report.ReporterManager,java.lang.ClassLoader)
 75  
      */
 76  
     public void execute( ReporterManagerFactory reportManager, JUnitCoreParameters JUnitCoreParameters )
 77  
         throws TestSetFailedException
 78  
     {
 79  
 
 80  0
         Class[] classes = new Class[1];
 81  0
         classes[0] = testClass;
 82  0
         execute( classes, reportManager, JUnitCoreParameters );
 83  0
     }
 84  
 
 85  
     public static void execute( Class[] classes, ReporterManagerFactory reporterManagerFactory,
 86  
                                 JUnitCoreParameters jUnitCoreParameters )
 87  
         throws TestSetFailedException
 88  
     {
 89  0
         final ConcurrentReportingRunListener listener =
 90  
             ConcurrentReportingRunListener.createInstance( reporterManagerFactory, jUnitCoreParameters.isParallelClasses(),
 91  
                                                       jUnitCoreParameters.isParallelBoth() );
 92  0
         Computer computer = getComputer( jUnitCoreParameters );
 93  
         try
 94  
         {
 95  0
             runJunitCore( classes, computer, listener );
 96  
         }
 97  
         finally
 98  
         {
 99  0
             closeIfConfigurable( computer );
 100  0
         }
 101  0
     }
 102  
 
 103  
     private static void closeIfConfigurable( Computer computer )
 104  
         throws TestSetFailedException
 105  
     {
 106  0
         if ( computer.getClass().getName().startsWith( className ) )
 107  
         {
 108  
             try
 109  
             {
 110  0
                 Class<?> cpcClass = Class.forName( className );
 111  0
                 Method method = cpcClass.getMethod( "close" );
 112  0
                 method.invoke( computer );
 113  
             }
 114  0
             catch ( ClassNotFoundException e )
 115  
             {
 116  0
                 throw new TestSetFailedException( e );
 117  
             }
 118  0
             catch ( NoSuchMethodException e )
 119  
             {
 120  0
                 throw new TestSetFailedException( e );
 121  
             }
 122  0
             catch ( InvocationTargetException e )
 123  
             {
 124  0
                 throw new TestSetFailedException( e );
 125  
             }
 126  0
             catch ( IllegalAccessException e )
 127  
             {
 128  0
                 throw new TestSetFailedException( e );
 129  0
             }
 130  
         }
 131  0
     }
 132  
 
 133  
     private static Computer getComputer( JUnitCoreParameters jUnitCoreParameters )
 134  
         throws TestSetFailedException
 135  
     {
 136  0
         if ( jUnitCoreParameters.isNoThreading() )
 137  
         {
 138  0
             return new Computer();
 139  
         }
 140  0
         return jUnitCoreParameters.isConfigurableParallelComputerPresent() ? getConfigurableParallelComputer(
 141  
             jUnitCoreParameters ) : getParallelComputer( jUnitCoreParameters );
 142  
     }
 143  
 
 144  
     private static Computer getParallelComputer( JUnitCoreParameters JUnitCoreParameters )
 145  
     {
 146  0
         if ( JUnitCoreParameters.isUseUnlimitedThreads() )
 147  
         {
 148  0
             return new ParallelComputer( true, true );
 149  
         }
 150  
         else
 151  
         {
 152  0
             return new ParallelComputer( JUnitCoreParameters.isParallelClasses(),
 153  
                                          JUnitCoreParameters.isParallelMethod() );
 154  
         }
 155  
     }
 156  
 
 157  
     private static Computer getConfigurableParallelComputer( JUnitCoreParameters JUnitCoreParameters )
 158  
         throws TestSetFailedException
 159  
     {
 160  
 
 161  
         try
 162  
         {
 163  0
             Class<?> cpcClass = Class.forName( className );
 164  0
             if ( JUnitCoreParameters.isUseUnlimitedThreads() )
 165  
             {
 166  0
                 Constructor<?> constructor = cpcClass.getConstructor();
 167  0
                 return (Computer) constructor.newInstance();
 168  
             }
 169  
             else
 170  
             {
 171  0
                 Constructor<?> constructor =
 172  
                     cpcClass.getConstructor( boolean.class, boolean.class, Integer.class, boolean.class );
 173  0
                 return (Computer) constructor.newInstance( JUnitCoreParameters.isParallelClasses(),
 174  
                                                            JUnitCoreParameters.isParallelMethod(),
 175  
                                                            JUnitCoreParameters.getThreadCount(),
 176  
                                                            JUnitCoreParameters.isPerCoreThreadCount() );
 177  
             }
 178  
         }
 179  0
         catch ( ClassNotFoundException e )
 180  
         {
 181  0
             throw new TestSetFailedException( e );
 182  
         }
 183  0
         catch ( NoSuchMethodException e )
 184  
         {
 185  0
             throw new TestSetFailedException( e );
 186  
         }
 187  0
         catch ( InvocationTargetException e )
 188  
         {
 189  0
             throw new TestSetFailedException( e );
 190  
         }
 191  0
         catch ( InstantiationException e )
 192  
         {
 193  0
             throw new TestSetFailedException( e );
 194  
         }
 195  0
         catch ( IllegalAccessException e )
 196  
         {
 197  0
             throw new TestSetFailedException( e );
 198  
         }
 199  
     }
 200  
 
 201  
     private static void runJunitCore( Class[] classes, Computer computer, RunListener real )
 202  
         throws TestSetFailedException
 203  
     {
 204  0
         JUnitCore junitCore = new JUnitCore();
 205  0
         junitCore.addListener( real );
 206  
         try
 207  
         {
 208  0
             junitCore.run( computer, classes );
 209  
         }
 210  
         finally
 211  
         {
 212  0
             junitCore.removeListener( real );
 213  0
         }
 214  0
     }
 215  
 
 216  
 
 217  
 }