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