1 | |
package org.apache.maven.plugin.surefire; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
import org.apache.maven.surefire.booter.Classpath; |
22 | |
import org.apache.maven.surefire.booter.KeyValueSource; |
23 | |
import org.apache.maven.surefire.util.internal.StringUtils; |
24 | |
|
25 | |
import java.io.File; |
26 | |
import java.io.FileInputStream; |
27 | |
import java.io.IOException; |
28 | |
import java.io.InputStream; |
29 | |
import java.util.Collections; |
30 | |
import java.util.Enumeration; |
31 | |
import java.util.HashSet; |
32 | |
import java.util.Iterator; |
33 | |
import java.util.LinkedHashSet; |
34 | |
import java.util.List; |
35 | |
import java.util.Map; |
36 | |
import java.util.Properties; |
37 | |
import java.util.Set; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public class SurefireProperties |
43 | |
extends Properties |
44 | |
implements KeyValueSource |
45 | |
{ |
46 | 15 | private final LinkedHashSet<Object> items = new LinkedHashSet<Object>(); |
47 | |
|
48 | |
public SurefireProperties() |
49 | 0 | { |
50 | 0 | } |
51 | |
|
52 | |
public SurefireProperties( Properties source ) |
53 | 1 | { |
54 | 1 | if ( source != null ) |
55 | |
{ |
56 | 1 | this.putAll( source ); |
57 | |
} |
58 | 1 | } |
59 | |
|
60 | |
public SurefireProperties( KeyValueSource source ) |
61 | 14 | { |
62 | 14 | if ( source != null ) |
63 | |
{ |
64 | 12 | source.copyTo( this ); |
65 | |
} |
66 | 14 | } |
67 | |
|
68 | |
@Override |
69 | |
public synchronized Object put( Object key, Object value ) |
70 | |
{ |
71 | 297 | items.add( key ); |
72 | 297 | return super.put( key, value ); |
73 | |
} |
74 | |
|
75 | |
@Override |
76 | |
public synchronized Object remove( Object key ) |
77 | |
{ |
78 | 1 | items.remove( key ); |
79 | 1 | return super.remove( key ); |
80 | |
} |
81 | |
|
82 | |
@Override |
83 | |
public synchronized void clear() |
84 | |
{ |
85 | 0 | items.clear(); |
86 | 0 | super.clear(); |
87 | 0 | } |
88 | |
|
89 | |
public synchronized Enumeration<Object> keys() |
90 | |
{ |
91 | 14 | return Collections.enumeration( items ); |
92 | |
} |
93 | |
|
94 | |
public void copyPropertiesFrom( Properties source ) |
95 | |
{ |
96 | 0 | if ( source != null ) |
97 | |
{ |
98 | |
|
99 | 0 | for ( Object key : source.keySet() ) |
100 | |
{ |
101 | 0 | Object value = source.get( key ); |
102 | 0 | put( key, value ); |
103 | 0 | } |
104 | |
} |
105 | 0 | } |
106 | |
|
107 | |
public Iterable<Object> getStringKeySet() |
108 | |
{ |
109 | |
|
110 | |
|
111 | 0 | return keySet(); |
112 | |
} |
113 | |
|
114 | 1 | private static final Set<String> keysThatCannotBeUsedAsSystemProperties = new HashSet<String>() |
115 | 1 | {{ |
116 | 1 | add( "java.library.path" ); |
117 | 1 | add( "file.encoding" ); |
118 | 1 | add( "jdk.map.althashing.threshold" ); |
119 | 1 | }}; |
120 | |
|
121 | |
public Set<Object> propertiesThatCannotBeSetASystemProperties() |
122 | |
{ |
123 | 0 | Set<Object> result = new HashSet<Object>(); |
124 | 0 | for ( Object key : getStringKeySet() ) |
125 | |
{ |
126 | 0 | if ( keysThatCannotBeUsedAsSystemProperties.contains( key ) ) |
127 | |
{ |
128 | 0 | result.add( key ); |
129 | |
} |
130 | |
} |
131 | 0 | return result; |
132 | |
} |
133 | |
|
134 | |
|
135 | |
public void copyToSystemProperties() |
136 | |
{ |
137 | |
|
138 | |
|
139 | 0 | for ( Object o : items ) |
140 | |
{ |
141 | 0 | String key = (String) o; |
142 | 0 | String value = getProperty( key ); |
143 | |
|
144 | 0 | System.setProperty( key, value ); |
145 | 0 | } |
146 | 0 | } |
147 | |
|
148 | |
static SurefireProperties calculateEffectiveProperties( Properties systemProperties, |
149 | |
Map<String, String> systemPropertyVariables, |
150 | |
Properties userProperties, SurefireProperties props ) |
151 | |
{ |
152 | 0 | SurefireProperties result = new SurefireProperties(); |
153 | 0 | result.copyPropertiesFrom( systemProperties ); |
154 | |
|
155 | 0 | result.copyPropertiesFrom( props ); |
156 | |
|
157 | 0 | copyProperties( result, systemPropertyVariables ); |
158 | 0 | copyProperties( result, systemPropertyVariables ); |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | 0 | result.copyPropertiesFrom( userProperties ); |
166 | 0 | return result; |
167 | |
} |
168 | |
|
169 | |
public static void copyProperties( Properties target, Map<String, String> source ) |
170 | |
{ |
171 | 0 | if ( source != null ) |
172 | |
{ |
173 | 0 | for ( String key : source.keySet() ) |
174 | |
{ |
175 | 0 | String value = source.get( key ); |
176 | |
|
177 | 0 | if ( value != null ) |
178 | |
{ |
179 | 0 | target.setProperty( key, value ); |
180 | |
} |
181 | 0 | } |
182 | |
} |
183 | 0 | } |
184 | |
|
185 | |
public void copyTo( Map target ) |
186 | |
{ |
187 | 0 | Iterator iter = keySet().iterator(); |
188 | |
Object key; |
189 | 0 | while ( iter.hasNext() ) |
190 | |
{ |
191 | 0 | key = iter.next(); |
192 | |
|
193 | 0 | target.put( key, get( key ) ); |
194 | |
} |
195 | 0 | } |
196 | |
|
197 | |
public void setProperty( String key, File file ) |
198 | |
{ |
199 | 48 | if ( file != null ) |
200 | |
{ |
201 | 36 | setProperty( key, file.toString() ); |
202 | |
} |
203 | 48 | } |
204 | |
|
205 | |
public void setProperty( String key, Boolean aBoolean ) |
206 | |
{ |
207 | 24 | if ( aBoolean != null ) |
208 | |
{ |
209 | 24 | setProperty( key, aBoolean.toString() ); |
210 | |
} |
211 | 24 | } |
212 | |
|
213 | |
public void addList( List items, String propertyPrefix ) |
214 | |
{ |
215 | 48 | if ( items == null || items.size() == 0 ) |
216 | |
{ |
217 | 20 | return; |
218 | |
} |
219 | 28 | int i = 0; |
220 | 28 | for ( Object item : items ) |
221 | |
{ |
222 | 56 | if ( item == null ) |
223 | |
{ |
224 | 0 | throw new NullPointerException( propertyPrefix + i + " has null value" ); |
225 | |
} |
226 | |
|
227 | 56 | String[] stringArray = StringUtils.split( item.toString(), "," ); |
228 | |
|
229 | 112 | for ( String aStringArray : stringArray ) |
230 | |
{ |
231 | 56 | setProperty( propertyPrefix + i, aStringArray ); |
232 | 56 | i++; |
233 | |
} |
234 | |
|
235 | 56 | } |
236 | 28 | } |
237 | |
|
238 | |
public void setClasspath( String prefix, Classpath classpath ) |
239 | |
{ |
240 | 24 | List classpathElements = classpath.getClassPath(); |
241 | 40 | for ( int i = 0; i < classpathElements.size(); ++i ) |
242 | |
{ |
243 | 16 | String element = (String) classpathElements.get( i ); |
244 | 16 | setProperty( prefix + i, element ); |
245 | |
} |
246 | 24 | } |
247 | |
|
248 | |
private static SurefireProperties loadProperties( InputStream inStream ) |
249 | |
throws IOException |
250 | |
{ |
251 | 0 | Properties p = new Properties(); |
252 | |
|
253 | |
try |
254 | |
{ |
255 | 0 | p.load( inStream ); |
256 | |
} |
257 | |
finally |
258 | |
{ |
259 | 0 | close( inStream ); |
260 | 0 | } |
261 | |
|
262 | 0 | return new SurefireProperties( p ); |
263 | |
} |
264 | |
|
265 | |
public static SurefireProperties loadProperties( File file ) |
266 | |
throws IOException |
267 | |
{ |
268 | 0 | if ( file != null ) |
269 | |
{ |
270 | 0 | return loadProperties( new FileInputStream( file ) ); |
271 | |
} |
272 | |
|
273 | 0 | return new SurefireProperties(); |
274 | |
} |
275 | |
|
276 | |
private static void close( InputStream inputStream ) |
277 | |
{ |
278 | 0 | if ( inputStream == null ) |
279 | |
{ |
280 | 0 | return; |
281 | |
} |
282 | |
|
283 | |
try |
284 | |
{ |
285 | 0 | inputStream.close(); |
286 | |
} |
287 | 0 | catch ( IOException ex ) |
288 | |
{ |
289 | |
|
290 | 0 | } |
291 | 0 | } |
292 | |
|
293 | |
public void setNullableProperty( String key, String value ) |
294 | |
{ |
295 | 48 | if ( value != null ) |
296 | |
{ |
297 | 47 | super.setProperty( key, value ); |
298 | |
} |
299 | |
|
300 | 48 | } |
301 | |
|
302 | |
|
303 | |
} |