Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
../../../img/srcFileCovDistChart10.png 0% of files have more coverage
28   145   16   4,67
12   69   0,57   6
6     2,67  
1    
6,1% of code in this file is excluded from these metrics.
 
  EasyMockProperties       Line # 37 28 6,1% 16 0 100% 1.0
 
  (364)
 
1    /**
2    * Copyright 2001-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
15    */
16   
17    package org.easymock.internal;
18   
19    import java.io.BufferedInputStream;
20    import java.io.IOException;
21    import java.io.InputStream;
22    import java.util.Map;
23    import java.util.Properties;
24   
25    /**
26    * Contains properties used by EasyMock to change its default behavior. The
27    * loading order is (any step being able to overload the properties of the
28    * previous step):
29    * <ul>
30    * <li>easymock.properties in classpath default package</li>
31    * <li>System properties</li>
32    * <li>explicit call to setProperty</li>
33    * </ul>
34    *
35    * @author Henri Tremblay
36    */
 
37    public final class EasyMockProperties {
38   
39    private static final String PREFIX = "easymock.";
40   
41    // volatile for double-checked locking
42    private static volatile EasyMockProperties instance;
43   
44    private final Properties properties = new Properties();
45   
 
46  2652 toggle public static EasyMockProperties getInstance() {
47  2652 if (instance == null) {
48  14 synchronized (EasyMockProperties.class) {
49    // ///CLOVER:OFF
50    if (instance == null) {
51    // ///CLOVER:ON
52  14 instance = new EasyMockProperties();
53    }
54    }
55    }
56  2650 return instance;
57    }
58   
 
59  14 toggle private EasyMockProperties() {
60    // Load the easymock.properties file
61  14 InputStream in = getClassLoader().getResourceAsStream("easymock.properties");
62  14 if (in != null) {
63  12 in = new BufferedInputStream(in);
64  12 try {
65  12 properties.load(in);
66    } catch (final IOException e) {
67  2 throw new RuntimeException("Failed to read easymock.properties file");
68    } finally {
69  12 try {
70  12 in.close();
71    } catch (final IOException e) {
72    // Doesn't matter
73    }
74    }
75    }
76    // Then overload it with system properties
77  12 for (final Map.Entry<Object, Object> entry : System.getProperties().entrySet()) {
78  730 if (entry.getKey() instanceof String && entry.getKey().toString().startsWith(PREFIX)) {
79  38 properties.put(entry.getKey(), entry.getValue());
80    }
81    }
82    }
83   
84    /**
85    * Searches for the property with the specified key. If the key is not
86    * found, return the default value.
87    *
88    * @param key
89    * key leading to the property
90    * @param defaultValue
91    * the value to be returned if the key isn't found
92    * @return the value found for the key or the default value
93    */
 
94  6 toggle public String getProperty(final String key, final String defaultValue) {
95  6 return properties.getProperty(key, defaultValue);
96    }
97   
98    /**
99    * Searches for the property with the specified key. Return null if the key
100    * is not found.
101    *
102    * @param key
103    * key leading to the property
104    * @return the value found for the key or null
105    */
 
106  2626 toggle public String getProperty(final String key) {
107  2626 return properties.getProperty(key);
108    }
109   
110    /**
111    * Add a value referenced by the provided key. A null value will remove the
112    * key
113    *
114    * @param key
115    * the key of the new property
116    * @param value
117    * the value corresponding to <tt>key</tt>.
118    * @return the property previous value
119    */
 
120  18 toggle public String setProperty(final String key, final String value) {
121  18 if (!key.startsWith(PREFIX)) {
122  2 throw new IllegalArgumentException("Invalid key (" + key
123    + "), an easymock property starts with \"" + PREFIX + "\"");
124    }
125  16 if (value == null) {
126  6 return (String) properties.remove(key);
127    }
128  10 return (String) properties.setProperty(key, value);
129    }
130   
 
131  14 toggle private ClassLoader getClassLoader() {
132  14 ClassLoader cl = null;
133  14 try {
134  14 cl = Thread.currentThread().getContextClassLoader();
135    } catch (final Throwable ex) {
136    // Cannot access thread context ClassLoader - falling back to system
137    // class loader
138    }
139  14 if (cl == null) {
140    // No thread context class loader -> use class loader of this class.
141  2 cl = getClass().getClassLoader();
142    }
143  14 return cl;
144    }
145    }