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