Coverage Report - org.apache.maven.surefire.booter.PropertiesWrapper
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertiesWrapper
0%
0/106
0%
0/78
3.2
 
 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 org.apache.maven.surefire.util.NestedRuntimeException;
 23  
 import org.apache.maven.surefire.util.ReflectionUtils;
 24  
 import org.apache.maven.surefire.util.internal.StringUtils;
 25  
 
 26  
 import java.io.ByteArrayInputStream;
 27  
 import java.io.File;
 28  
 import java.util.ArrayList;
 29  
 import java.util.Iterator;
 30  
 import java.util.List;
 31  
 import java.util.Properties;
 32  
 
 33  
 /**
 34  
  * @author Kristian Rosenvold
 35  
  */
 36  
 public class PropertiesWrapper
 37  
 {
 38  
     private final Properties properties;
 39  
 
 40  
     public PropertiesWrapper( Properties properties )
 41  0
     {
 42  0
         if ( properties == null )
 43  
         {
 44  0
             throw new IllegalStateException( "Properties cannot be null" );
 45  
         }
 46  0
         this.properties = properties;
 47  0
     }
 48  
 
 49  
     public Properties getProperties()
 50  
     {
 51  0
         return properties;
 52  
     }
 53  
 
 54  
     public void setAsSystemProperties()
 55  
     {
 56  0
         for ( Iterator i = properties.keySet().iterator(); i.hasNext(); )
 57  
         {
 58  0
             String key = (String) i.next();
 59  
 
 60  0
             System.setProperty( key, properties.getProperty( key ) );
 61  0
         }
 62  0
     }
 63  
 
 64  
     public String getProperty( String key )
 65  
     {
 66  0
         return properties.getProperty( key );
 67  
     }
 68  
 
 69  
     public boolean getBooleanProperty( String propertyName )
 70  
     {
 71  0
         final Boolean aBoolean = Boolean.valueOf( properties.getProperty( propertyName ) );
 72  0
         return aBoolean.booleanValue();
 73  
     }
 74  
 
 75  
     public Boolean getBooleanObjectProperty( String propertyName )
 76  
     {
 77  0
         return Boolean.valueOf( properties.getProperty( propertyName ) );
 78  
     }
 79  
 
 80  
     public Integer getIntegerObjectProperty( String propertyName )
 81  
     {
 82  0
         String property = properties.getProperty( propertyName );
 83  0
         return property != null ? Integer.valueOf( property ) : null;
 84  
     }
 85  
 
 86  
     public File getFileProperty( String key )
 87  
     {
 88  0
         final String property = getProperty( key );
 89  0
         if ( property == null )
 90  
         {
 91  0
             return null;
 92  
         }
 93  0
         return (File) getParamValue( property, File.class.getName() );
 94  
     }
 95  
 
 96  
     public List getListOfTypedObjects( String propertyPrefix )
 97  
     {
 98  
         String type;
 99  
         String value;
 100  0
         List result = new ArrayList();
 101  0
         for ( int i = 0; ( type = getProperty( propertyPrefix + i + BooterConstants.TYPES_SUFIX ) ) != null; i++ )
 102  
         {
 103  0
             value = getProperty( propertyPrefix + i + BooterConstants.PARAMS_SUFIX );
 104  0
             result.add( getParamValue( value, type ) );
 105  
         }
 106  0
         return result;
 107  
     }
 108  
 
 109  
     public List getStringList( String propertyPrefix )
 110  
     {
 111  
         String value;
 112  0
         List result = new ArrayList();
 113  
         // Whoa, C !!
 114  0
         for ( int i = 0; ( value = getProperty( propertyPrefix + i ) ) != null; i++ )
 115  
         {
 116  0
             result.add( value );
 117  
         }
 118  0
         return result;
 119  
     }
 120  
 
 121  
     /**
 122  
      * Retrieves as single object that is persisted with type encoding
 123  
      *
 124  
      * @param key The key for the propery
 125  
      * @return The object, of a supported type
 126  
      */
 127  
     public Object getTypeDecoded( String key )
 128  
     {
 129  0
         String typeEncoded = getProperty( key );
 130  0
         if ( typeEncoded == null )
 131  
         {
 132  0
             return null;
 133  
         }
 134  0
         int typeSep = typeEncoded.indexOf( "|" );
 135  0
         String type = typeEncoded.substring( 0, typeSep );
 136  0
         String value = typeEncoded.substring( typeSep + 1 );
 137  0
         return getParamValue( value, type );
 138  
     }
 139  
 
 140  
     private Object getParamValue( String param, String typeName )
 141  
     {
 142  0
         if ( typeName.trim().length() == 0 )
 143  
         {
 144  0
             return null;
 145  
         }
 146  0
         else if ( typeName.equals( String.class.getName() ) )
 147  
         {
 148  0
             return param;
 149  
         }
 150  0
         else if ( typeName.equals( Class.class.getName() ) )
 151  
         {
 152  0
             return ReflectionUtils.loadClass( Thread.currentThread().getContextClassLoader(), param );
 153  
         }
 154  0
         else if ( typeName.equals( File.class.getName() ) )
 155  
         {
 156  0
             return new File( param );
 157  
         }
 158  0
         else if ( typeName.equals( File[].class.getName() ) )
 159  
         {
 160  0
             List stringList = processStringList( param );
 161  0
             File[] fileList = new File[stringList.size()];
 162  0
             for ( int j = 0; j < stringList.size(); j++ )
 163  
             {
 164  0
                 fileList[j] = new File( (String) stringList.get( j ) );
 165  
             }
 166  0
             return fileList;
 167  
         }
 168  0
         else if ( typeName.equals( ArrayList.class.getName() ) )
 169  
         {
 170  0
             return processStringList( param );
 171  
         }
 172  0
         else if ( typeName.equals( Boolean.class.getName() ) )
 173  
         {
 174  0
             return Boolean.valueOf( param );
 175  
         }
 176  0
         else if ( typeName.equals( Integer.class.getName() ) )
 177  
         {
 178  0
             return Integer.valueOf( param );
 179  
         }
 180  0
         else if ( typeName.equals( Properties.class.getName() ) )
 181  
         {
 182  0
             final Properties result = new Properties();
 183  
             try
 184  
             {
 185  0
                 ByteArrayInputStream bais = new ByteArrayInputStream( param.getBytes( "8859_1" ) );
 186  0
                 result.load( bais );
 187  
             }
 188  0
             catch ( Exception e )
 189  
             {
 190  0
                 throw new NestedRuntimeException( "bug in property conversion", e );
 191  0
             }
 192  0
             return result;
 193  
         }
 194  
         else
 195  
         {
 196  
             // TODO: could attempt to construct with a String constructor if needed
 197  0
             throw new IllegalArgumentException( "Unknown parameter type: " + typeName );
 198  
         }
 199  
     }
 200  
 
 201  
     private static List processStringList( String stringList )
 202  
     {
 203  0
         String sl = stringList;
 204  
 
 205  0
         if ( sl.startsWith( "[" ) && sl.endsWith( "]" ) )
 206  
         {
 207  0
             sl = sl.substring( 1, sl.length() - 1 );
 208  
         }
 209  
 
 210  0
         List list = new ArrayList();
 211  
 
 212  0
         String[] stringArray = StringUtils.split( sl, "," );
 213  
 
 214  0
         for ( int i = 0; i < stringArray.length; i++ )
 215  
         {
 216  0
             list.add( stringArray[i].trim() );
 217  
         }
 218  0
         return list;
 219  
     }
 220  
 
 221  
     public void setProperty( String key, File file )
 222  
     {
 223  0
         if ( file != null )
 224  
         {
 225  0
             setProperty( key, file.toString() );
 226  
         }
 227  0
     }
 228  
 
 229  
     public void setProperty( String key, Boolean aBoolean )
 230  
     {
 231  0
         if ( aBoolean != null )
 232  
         {
 233  0
             setProperty( key, aBoolean.toString() );
 234  
         }
 235  0
     }
 236  
 
 237  
     Classpath getClasspath( String prefix  )
 238  
     {
 239  0
         List elements = getStringList( prefix );
 240  0
         return new Classpath( elements );
 241  
     }
 242  
 
 243  
     public void setClasspath( String prefix, Classpath classpath )
 244  
     {
 245  0
         List classpathElements = classpath.getClassPath();
 246  0
         for ( int i = 0; i < classpathElements.size(); ++i )
 247  
         {
 248  0
             String element = (String) classpathElements.get( i );
 249  0
             setProperty( prefix + i, element );
 250  
         }
 251  0
     }
 252  
 
 253  
     public void setProperty( String key, Integer integer )
 254  
     {
 255  0
         if ( integer != null )
 256  
         {
 257  0
             setProperty( key, integer.toString() );
 258  
         }
 259  0
     }
 260  
 
 261  
 
 262  
     public void setProperty( String key, String value )
 263  
     {
 264  0
         if ( value != null )
 265  
         {
 266  0
             properties.setProperty( key, value );
 267  
         }
 268  0
     }
 269  
 
 270  
     public void addList( List items, String propertyPrefix )
 271  
     {
 272  0
         if ( items == null || items.size() == 0 )
 273  
         {
 274  0
             return;
 275  
         }
 276  0
         int i = 0;
 277  0
         for (Iterator iterator = items.iterator(); iterator.hasNext();)
 278  
         {
 279  0
             Object item = iterator.next();
 280  0
             if ( item == null )
 281  
             {
 282  0
                 throw new NullPointerException( propertyPrefix + i + " has null value" );
 283  
             }
 284  
 
 285  0
             String[] stringArray = StringUtils.split(item.toString(), ",");
 286  
 
 287  0
             for ( int j = 0; j < stringArray.length; j++ )
 288  
             {
 289  0
                 properties.setProperty( propertyPrefix + i, stringArray[j] );
 290  0
                 i++;
 291  
             }
 292  
 
 293  0
         }
 294  0
     }
 295  
 
 296  
 }