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