View Javadoc

1   package org.apache.maven.plugin.surefire;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
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   * A properties implementation that preserves insertion order.
41   */
42  public class SurefireProperties
43      extends Properties
44      implements KeyValueSource
45  {
46      private final LinkedHashSet<Object> items = new LinkedHashSet<Object>();
47  
48      public SurefireProperties()
49      {
50      }
51  
52      public SurefireProperties( Properties source )
53      {
54          if ( source != null )
55          {
56              this.putAll( source );
57          }
58      }
59  
60      public SurefireProperties( KeyValueSource source )
61      {
62          if ( source != null )
63          {
64              source.copyTo( this );
65          }
66      }
67  
68      @Override
69      public synchronized Object put( Object key, Object value )
70      {
71          items.add( key );
72          return super.put( key, value );
73      }
74  
75      @Override
76      public synchronized Object remove( Object key )
77      {
78          items.remove( key );
79          return super.remove( key );
80      }
81  
82      @Override
83      public synchronized void clear()
84      {
85          items.clear();
86          super.clear();
87      }
88  
89      public synchronized Enumeration<Object> keys()
90      {
91          return Collections.enumeration( items );
92      }
93  
94      public void copyPropertiesFrom( Properties source )
95      {
96          if ( source != null )
97          {
98              //noinspection unchecked
99              for ( Object key : source.keySet() )
100             {
101                 Object value = source.get( key );
102                 put( key, value );
103             }
104         }
105     }
106 
107     public Iterable<Object> getStringKeySet()
108     {
109 
110         //noinspection unchecked
111         return keySet();
112     }
113 
114     private static final Set<String> keysThatCannotBeUsedAsSystemProperties = new HashSet<String>()
115     {{
116             add( "java.library.path" );
117             add( "file.encoding" );
118             add( "jdk.map.althashing.threshold" );
119         }};
120 
121     public Set<Object> propertiesThatCannotBeSetASystemProperties()
122     {
123         Set<Object> result = new HashSet<Object>();
124         for ( Object key : getStringKeySet() )
125         {
126             if ( keysThatCannotBeUsedAsSystemProperties.contains( key ) )
127             {
128                 result.add( key );
129             }
130         }
131         return result;
132     }
133 
134 
135     public void copyToSystemProperties()
136     {
137 
138         //noinspection unchecked
139         for ( Object o : items )
140         {
141             String key = (String) o;
142             String value = getProperty( key );
143 
144             System.setProperty( key, value );
145         }
146     }
147 
148     static SurefireProperties calculateEffectiveProperties( Properties systemProperties,
149                                                             Map<String, String> systemPropertyVariables,
150                                                             Properties userProperties, SurefireProperties props )
151     {
152         SurefireProperties result = new SurefireProperties();
153         result.copyPropertiesFrom( systemProperties );
154 
155         result.copyPropertiesFrom( props );
156 
157         copyProperties( result, systemPropertyVariables );
158         copyProperties( result, systemPropertyVariables );
159 
160         // We used to take all of our system properties and dump them in with the
161         // user specified properties for SUREFIRE-121, causing SUREFIRE-491.
162         // Not gonna do THAT any more... instead, we only propagate those system properties
163         // that have been explicitly specified by the user via -Dkey=value on the CLI
164 
165         result.copyPropertiesFrom( userProperties );
166         return result;
167     }
168 
169     public static void copyProperties( Properties target, Map<String, String> source )
170     {
171         if ( source != null )
172         {
173             for ( String key : source.keySet() )
174             {
175                 String value = source.get( key );
176                 //java Properties does not accept null value
177                 if ( value != null )
178                 {
179                     target.setProperty( key, value );
180                 }
181             }
182         }
183     }
184 
185     public void copyTo( Map target )
186     {
187         Iterator iter = keySet().iterator();
188         Object key;
189         while ( iter.hasNext() )
190         {
191             key = iter.next();
192             //noinspection unchecked
193             target.put( key, get( key ) );
194         }
195     }
196 
197     public void setProperty( String key, File file )
198     {
199         if ( file != null )
200         {
201             setProperty( key, file.toString() );
202         }
203     }
204 
205     public void setProperty( String key, Boolean aBoolean )
206     {
207         if ( aBoolean != null )
208         {
209             setProperty( key, aBoolean.toString() );
210         }
211     }
212 
213     public void addList( List items, String propertyPrefix )
214     {
215         if ( items == null || items.size() == 0 )
216         {
217             return;
218         }
219         int i = 0;
220         for ( Object item : items )
221         {
222             if ( item == null )
223             {
224                 throw new NullPointerException( propertyPrefix + i + " has null value" );
225             }
226 
227             String[] stringArray = StringUtils.split( item.toString(), "," );
228 
229             for ( String aStringArray : stringArray )
230             {
231                 setProperty( propertyPrefix + i, aStringArray );
232                 i++;
233             }
234 
235         }
236     }
237 
238     public void setClasspath( String prefix, Classpath classpath )
239     {
240         List classpathElements = classpath.getClassPath();
241         for ( int i = 0; i < classpathElements.size(); ++i )
242         {
243             String element = (String) classpathElements.get( i );
244             setProperty( prefix + i, element );
245         }
246     }
247 
248     private static SurefireProperties loadProperties( InputStream inStream )
249         throws IOException
250     {
251         Properties p = new Properties();
252 
253         try
254         {
255             p.load( inStream );
256         }
257         finally
258         {
259             close( inStream );
260         }
261 
262         return new SurefireProperties( p );
263     }
264 
265     public static SurefireProperties loadProperties( File file )
266         throws IOException
267     {
268         if ( file != null )
269         {
270             return loadProperties( new FileInputStream( file ) );
271         }
272 
273         return new SurefireProperties();
274     }
275 
276     private static void close( InputStream inputStream )
277     {
278         if ( inputStream == null )
279         {
280             return;
281         }
282 
283         try
284         {
285             inputStream.close();
286         }
287         catch ( IOException ex )
288         {
289             // ignore
290         }
291     }
292 
293     public void setNullableProperty( String key, String value )
294     {
295         if ( value != null )
296         {
297             super.setProperty( key, value );
298         }
299 
300     }
301 
302 
303 }