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