Coverage Report - org.apache.maven.plugin.surefire.booterclient.BooterSerializer
 
Classes in this File Line Coverage Branch Coverage Complexity
BooterSerializer
97%
44/45
58%
7/12
3,333
 
 1  
 package org.apache.maven.plugin.surefire.booterclient;
 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.io.File;
 23  
 import java.io.IOException;
 24  
 import java.util.Properties;
 25  
 import org.apache.maven.surefire.booter.BooterConstants;
 26  
 import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
 27  
 import org.apache.maven.surefire.booter.KeyValueSource;
 28  
 import org.apache.maven.surefire.booter.PropertiesWrapper;
 29  
 import org.apache.maven.surefire.booter.ProviderConfiguration;
 30  
 import org.apache.maven.surefire.booter.StartupConfiguration;
 31  
 import org.apache.maven.surefire.booter.SystemPropertyManager;
 32  
 import org.apache.maven.surefire.report.ReporterConfiguration;
 33  
 import org.apache.maven.surefire.testset.DirectoryScannerParameters;
 34  
 import org.apache.maven.surefire.testset.RunOrderParameters;
 35  
 import org.apache.maven.surefire.testset.TestArtifactInfo;
 36  
 import org.apache.maven.surefire.testset.TestRequest;
 37  
 import org.apache.maven.surefire.util.RunOrder;
 38  
 
 39  
 /**
 40  
  * Knows how to serialize and deserialize the booter configuration.
 41  
  * <p/>
 42  
  * The internal serialization format is through a properties file. The long-term goal of this
 43  
  * class is not to expose this implementation information to its clients. This still leaks somewhat,
 44  
  * and there are some cases where properties are being accessed as "Properties" instead of
 45  
  * more representative domain objects.
 46  
  * <p/>
 47  
  *
 48  
  * @author Jason van Zyl
 49  
  * @author Emmanuel Venisse
 50  
  * @author Brett Porter
 51  
  * @author Dan Fabulich
 52  
  * @author Kristian Rosenvold
 53  
  */
 54  
 class BooterSerializer
 55  
 {
 56  
     private final ForkConfiguration forkConfiguration;
 57  
 
 58  
     public BooterSerializer( ForkConfiguration forkConfiguration )
 59  12
     {
 60  12
         this.forkConfiguration = forkConfiguration;
 61  12
     }
 62  
 
 63  
 
 64  
     /*
 65  
     DOes not modify sourceProperties
 66  
      */
 67  
     public File serialize( KeyValueSource sourceProperties, ProviderConfiguration booterConfiguration,
 68  
                            StartupConfiguration providerConfiguration, Object testSet, boolean readTestsFromInStream )
 69  
         throws IOException
 70  
     {
 71  
 
 72  12
         PropertiesWrapper properties = new PropertiesWrapper( new Properties() );
 73  12
         sourceProperties.copyTo( properties.getProperties() );
 74  
 
 75  12
         providerConfiguration.getClasspathConfiguration().addForkProperties( properties );
 76  
 
 77  12
         TestArtifactInfo testNg = booterConfiguration.getTestArtifact();
 78  12
         if ( testNg != null )
 79  
         {
 80  12
             properties.setProperty( BooterConstants.TESTARTIFACT_VERSION, testNg.getVersion() );
 81  12
             properties.setProperty( BooterConstants.TESTARTIFACT_CLASSIFIER, testNg.getClassifier() );
 82  
         }
 83  
 
 84  12
         properties.setProperty( BooterConstants.FORKTESTSET_PREFER_TESTS_FROM_IN_STREAM,
 85  
                                 Boolean.valueOf( readTestsFromInStream ) );
 86  12
         properties.setProperty( BooterConstants.FORKTESTSET, getTypeEncoded( testSet ) );
 87  
 
 88  12
         TestRequest testSuiteDefinition = booterConfiguration.getTestSuiteDefinition();
 89  12
         if ( testSuiteDefinition != null )
 90  
         {
 91  12
             properties.setProperty( BooterConstants.SOURCE_DIRECTORY, testSuiteDefinition.getTestSourceDirectory() );
 92  12
             properties.addList( testSuiteDefinition.getSuiteXmlFiles(), BooterConstants.TEST_SUITE_XML_FILES );
 93  12
             properties.setProperty( BooterConstants.REQUESTEDTEST, testSuiteDefinition.getRequestedTest() );
 94  12
             properties.setProperty( BooterConstants.REQUESTEDTESTMETHOD, testSuiteDefinition.getRequestedTestMethod() );
 95  
         }
 96  
 
 97  12
         DirectoryScannerParameters directoryScannerParameters = booterConfiguration.getDirScannerParams();
 98  12
         if ( directoryScannerParameters != null )
 99  
         {
 100  12
             properties.setProperty( BooterConstants.FAILIFNOTESTS,
 101  
                                     String.valueOf( directoryScannerParameters.isFailIfNoTests() ) );
 102  12
             properties.addList( directoryScannerParameters.getIncludes(), BooterConstants.INCLUDES_PROPERTY_PREFIX );
 103  12
             properties.addList( directoryScannerParameters.getExcludes(), BooterConstants.EXCLUDES_PROPERTY_PREFIX );
 104  12
             properties.addList( directoryScannerParameters.getSpecificTests(),
 105  
                                 BooterConstants.SPECIFIC_TEST_PROPERTY_PREFIX );
 106  
 
 107  12
             properties.setProperty( BooterConstants.TEST_CLASSES_DIRECTORY,
 108  
                                     directoryScannerParameters.getTestClassesDirectory() );
 109  
         }
 110  
 
 111  12
         final RunOrderParameters runOrderParameters = booterConfiguration.getRunOrderParameters();
 112  12
         if ( runOrderParameters != null )
 113  
         {
 114  12
             properties.setProperty( BooterConstants.RUN_ORDER, RunOrder.asString( runOrderParameters.getRunOrder() ) );
 115  12
             properties.setProperty( BooterConstants.RUN_STATISTICS_FILE, runOrderParameters.getRunStatisticsFile() );
 116  
         }
 117  
 
 118  12
         ReporterConfiguration reporterConfiguration = booterConfiguration.getReporterConfiguration();
 119  
 
 120  12
         Boolean rep = reporterConfiguration.isTrimStackTrace();
 121  12
         properties.setProperty( BooterConstants.ISTRIMSTACKTRACE, rep );
 122  12
         properties.setProperty( BooterConstants.REPORTSDIRECTORY, reporterConfiguration.getReportsDirectory() );
 123  12
         ClassLoaderConfiguration classLoaderConfiguration = providerConfiguration.getClassLoaderConfiguration();
 124  12
         properties.setProperty( BooterConstants.USESYSTEMCLASSLOADER,
 125  
                                 String.valueOf( classLoaderConfiguration.isUseSystemClassLoader() ) );
 126  12
         properties.setProperty( BooterConstants.USEMANIFESTONLYJAR,
 127  
                                 String.valueOf( classLoaderConfiguration.isUseManifestOnlyJar() ) );
 128  12
         properties.setProperty( BooterConstants.FAILIFNOTESTS,
 129  
                                 String.valueOf( booterConfiguration.isFailIfNoTests() ) );
 130  12
         properties.setProperty( BooterConstants.PROVIDER_CONFIGURATION, providerConfiguration.getProviderClassName() );
 131  
 
 132  12
         return SystemPropertyManager.writePropertiesFile( properties.getProperties(),
 133  
                                                           forkConfiguration.getTempDirectory(), "surefire",
 134  
                                                           forkConfiguration.isDebug() );
 135  
     }
 136  
 
 137  
 
 138  
     private String getTypeEncoded( Object value )
 139  
     {
 140  12
         if ( value == null )
 141  
         {
 142  1
             return null;
 143  
         }
 144  
         String valueToUse;
 145  11
         if ( value instanceof Class )
 146  
         {
 147  0
             valueToUse = ( (Class<?>) value ).getName();
 148  
         }
 149  
         else
 150  
         {
 151  11
             valueToUse = value.toString();
 152  
         }
 153  11
         return value.getClass().getName() + "|" + valueToUse;
 154  
     }
 155  
 
 156  
 }