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.Map; |
27 | |
import java.util.Properties; |
28 | |
import org.apache.maven.surefire.util.internal.StringUtils; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
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 | |
|
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 | |
|
104 | |
|
105 | |
|
106 | |
|
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 | |
|
180 | 0 | target.put( key, properties.get( key ) ); |
181 | |
} |
182 | 0 | } |
183 | |
} |