Coverage Report - org.apache.maven.surefire.booter.PropertiesWrapper
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertiesWrapper
50%
29/57
50%
13/26
2,286
 
 1  
 package org.apache.maven.surefire.booter;
 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.util.ArrayList;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 import java.util.Properties;
 28  
 import org.apache.maven.surefire.util.internal.StringUtils;
 29  
 
 30  
 /**
 31  
  * Makes java.util.Properties behave like it's 2013
 32  
  *
 33  
  * @author Kristian Rosenvold
 34  
  */
 35  
 public class PropertiesWrapper
 36  
     implements KeyValueSource
 37  
 {
 38  
     private final Properties properties;
 39  
 
 40  
     public PropertiesWrapper( Properties properties )
 41  6
     {
 42  6
         if ( properties == null )
 43  
         {
 44  0
             throw new IllegalStateException( "Properties cannot be null" );
 45  
         }
 46  6
         this.properties = properties;
 47  6
     }
 48  
 
 49  
     public Properties getProperties()
 50  
     {
 51  0
         return properties;
 52  
     }
 53  
 
 54  
     public void setAsSystemProperties()
 55  
     {
 56  0
         for ( Object o : properties.keySet() )
 57  
         {
 58  0
             String key = (String) o;
 59  
 
 60  0
             System.setProperty( key, properties.getProperty( key ) );
 61  0
         }
 62  0
     }
 63  
 
 64  
     public String getProperty( String key )
 65  
     {
 66  15
         return properties.getProperty( key );
 67  
     }
 68  
 
 69  
     public boolean getBooleanProperty( String propertyName )
 70  
     {
 71  0
         return Boolean.valueOf( properties.getProperty( propertyName ) );
 72  
     }
 73  
 
 74  
     public Boolean getBooleanObjectProperty( String propertyName )
 75  
     {
 76  0
         return Boolean.valueOf( properties.getProperty( propertyName ) );
 77  
     }
 78  
 
 79  
     public File getFileProperty( String key )
 80  
     {
 81  0
         final String property = getProperty( key );
 82  0
         if ( property == null )
 83  
         {
 84  0
             return null;
 85  
         }
 86  0
         TypeEncodedValue typeEncodedValue = new TypeEncodedValue( File.class.getName(), property );
 87  0
         return (File) typeEncodedValue.getDecodedValue();
 88  
     }
 89  
 
 90  
     public List<String> getStringList( String propertyPrefix )
 91  
     {
 92  
         String value;
 93  4
         List<String> result = new ArrayList<String>();
 94  
         // Whoa, C !!
 95  13
         for ( int i = 0; ( value = getProperty( propertyPrefix + i ) ) != null; i++ )
 96  
         {
 97  9
             result.add( value );
 98  
         }
 99  4
         return result;
 100  
     }
 101  
 
 102  
     /**
 103  
      * Retrieves as single object that is persisted with type encoding
 104  
      *
 105  
      * @param key The key for the propery
 106  
      * @return The object, of a supported type
 107  
      */
 108  
     public TypeEncodedValue getTypeEncodedValue( String key )
 109  
     {
 110  0
         String typeEncoded = getProperty( key );
 111  0
         if ( typeEncoded == null )
 112  
         {
 113  0
             return null;
 114  
         }
 115  0
         int typeSep = typeEncoded.indexOf( "|" );
 116  0
         String type = typeEncoded.substring( 0, typeSep );
 117  0
         String value = typeEncoded.substring( typeSep + 1 );
 118  0
         return new TypeEncodedValue( type, value );
 119  
     }
 120  
 
 121  
 
 122  
     Classpath getClasspath( String prefix )
 123  
     {
 124  3
         List<String> elements = getStringList( prefix );
 125  3
         return new Classpath( elements );
 126  
     }
 127  
 
 128  
     public void setClasspath( String prefix, Classpath classpath )
 129  
     {
 130  2
         List classpathElements = classpath.getClassPath();
 131  6
         for ( int i = 0; i < classpathElements.size(); ++i )
 132  
         {
 133  4
             String element = (String) classpathElements.get( i );
 134  4
             setProperty( prefix + i, element );
 135  
         }
 136  2
     }
 137  
 
 138  
 
 139  
     public void setProperty( String key, String value )
 140  
     {
 141  4
         if ( value != null )
 142  
         {
 143  4
             properties.setProperty( key, value );
 144  
         }
 145  4
     }
 146  
 
 147  
     public void addList( List items, String propertyPrefix )
 148  
     {
 149  1
         if ( items == null || items.size() == 0 )
 150  
         {
 151  0
             return;
 152  
         }
 153  1
         int i = 0;
 154  1
         for ( Object item : items )
 155  
         {
 156  4
             if ( item == null )
 157  
             {
 158  0
                 throw new NullPointerException( propertyPrefix + i + " has null value" );
 159  
             }
 160  
 
 161  4
             String[] stringArray = StringUtils.split( item.toString(), "," );
 162  
 
 163  9
             for ( String aStringArray : stringArray )
 164  
             {
 165  5
                 properties.setProperty( propertyPrefix + i, aStringArray );
 166  5
                 i++;
 167  
             }
 168  
 
 169  4
         }
 170  1
     }
 171  
 
 172  
     public void copyTo( Map target )
 173  
     {
 174  0
         Iterator iter = properties.keySet().iterator();
 175  
         Object key;
 176  0
         while ( iter.hasNext() )
 177  
         {
 178  0
             key = iter.next();
 179  
             //noinspection unchecked
 180  0
             target.put( key, properties.get( key ) );
 181  
         }
 182  0
     }
 183  
 }