1 | |
package org.apache.maven.surefire.booter; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
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 | |
|
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 | |
|
101 | |
|
102 | |
|
103 | |
|
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 | |
} |