Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
65   226   18   4,33
4   132   0,28   15
15     1,2  
1    
 
  EasyMockPropertiesTest       Line # 33 65 0% 18 9 89,3% 0.89285713
 
  (12)
 
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.tests2;
18   
19    import static org.easymock.EasyMock.*;
20    import static org.junit.Assert.*;
21   
22    import java.io.IOException;
23    import java.io.InputStream;
24    import java.lang.reflect.Field;
25   
26    import org.easymock.internal.EasyMockProperties;
27    import org.junit.BeforeClass;
28    import org.junit.Test;
29   
30    /**
31    * @author Henri Tremblay
32    */
 
33    public class EasyMockPropertiesTest {
34   
 
35  2 toggle @BeforeClass
36    public static void setup() throws Exception {
37    // Make sure to reset to prevent getting an already initialized
38    // EasyMockProperties
39  2 resetInstance();
40   
41    // Overload before initializing easymock.properties
42  2 System.setProperty("easymock.b", "3");
43   
44    // Set a value only in System properties
45  2 System.setProperty("easymock.f", "5");
46   
47    // Set a value not starting by "easymock."
48  2 System.setProperty("xxx.yyy", "6");
49   
50    // Be wicked, set an object
51  2 System.getProperties().put(System.class, System.class);
52  2 System.getProperties().put("easymock.g", System.class);
53   
54    // Set manually a new one
55  2 setEasyMockProperty("easymock.e", "7");
56   
57    // Set manually an old one
58  2 setEasyMockProperty("easymock.c", "8");
59   
60    // Overload after (will be ignored)
61  2 System.setProperty("easymock.h", "4");
62    }
63   
 
64  2 toggle @Test
65    public void testGetInstance() {
66  2 assertExpectedValue("1", "easymock.a");
67  2 assertExpectedValue("3", "easymock.b");
68  2 assertExpectedValue("8", "easymock.c");
69  2 assertExpectedValue("7", "easymock.e");
70  2 assertExpectedValue("5", "easymock.f");
71  2 assertExpectedValue(null, "easymock.g");
72  2 assertExpectedValue(null, "easymock.h");
73  2 assertExpectedValue(null, "xxx.yyy");
74    }
75   
 
76  2 toggle @Test
77    public void testGetProperty() {
78  2 final EasyMockProperties instance = EasyMockProperties.getInstance();
79   
80    // use the default
81  2 assertEquals("1", instance.getProperty("easymock.a", "10"));
82    // don't use the default
83  2 assertEquals("10", instance.getProperty("easymock.z", "10"));
84    // null default
85  2 assertNull(instance.getProperty("easymock.z", null));
86    }
87   
 
88  2 toggle @Test(expected = IllegalArgumentException.class)
89    public void testSetProperty() {
90  2 final EasyMockProperties instance = EasyMockProperties.getInstance();
91   
92  2 instance.setProperty("tralala.a", null);
93    }
94   
 
95  2 toggle @Test
96    public void testNoThreadContextClassLoader() throws Exception {
97  2 final ClassLoader old = Thread.currentThread().getContextClassLoader();
98  2 try {
99  2 resetInstance();
100   
101    // Remove the context class loader
102  2 Thread.currentThread().setContextClassLoader(null);
103   
104    // This instance will load easymock.properties from the
105    // EasyMockProperties class loader
106  2 EasyMockProperties.getInstance();
107   
108    // And so "easymock.a" should be there
109  2 assertExpectedValue("1", "easymock.a");
110   
111    } finally {
112    // Whatever happens, set the initial class loader back or it'll get
113    // messy
114  2 Thread.currentThread().setContextClassLoader(old);
115    }
116    }
117   
 
118  2 toggle @Test
119    public void testBadPropertiesFile() throws Exception {
120   
121  2 final Boolean[] close = new Boolean[1];
122   
123    // A ClassLoader that returns no easymock.properties
124  2 final ClassLoader cl = new ClassLoader(getClass().getClassLoader()) {
125   
 
126  2 toggle @Override
127    public InputStream getResourceAsStream(final String name) {
128  2 if ("easymock.properties".equals(name)) {
129  2 return new InputStream() {
 
130  2 toggle @Override
131    public void close() throws IOException {
132  2 close[0] = Boolean.TRUE;
133    }
134   
 
135  2 toggle @Override
136    public int read(final byte[] b, final int off, final int len) throws IOException {
137  2 throw new IOException("Failed!");
138    }
139   
 
140  0 toggle @Override
141    public int read(final byte[] b) throws IOException {
142  0 throw new IOException("Failed!");
143    }
144   
 
145  0 toggle @Override
146    public int read() throws IOException {
147  0 throw new IOException("Failed!");
148    }
149    };
150    }
151  0 return super.getResourceAsStream(name);
152    }
153   
154    };
155  2 final ClassLoader old = Thread.currentThread().getContextClassLoader();
156  2 try {
157  2 resetInstance();
158   
159    // Remove the context class loader
160  2 Thread.currentThread().setContextClassLoader(cl);
161   
162  2 try {
163  2 EasyMockProperties.getInstance();
164  0 fail("Should have an issue loading the easymock.properties file");
165    } catch (final RuntimeException e) {
166  2 assertEquals("Failed to read easymock.properties file", e.getMessage());
167    // Make sure the thread was closed
168  2 assertSame(Boolean.TRUE, close[0]);
169    }
170   
171    } finally {
172    // Whatever happens, set the initial class loader back or it'll get
173    // messy
174  2 Thread.currentThread().setContextClassLoader(old);
175    }
176    }
177   
 
178  2 toggle @Test
179    public void testNoEasymockPropertiesFile() throws Exception {
180    // A ClassLoader that returns no easymock.properties
181  2 final ClassLoader cl = new ClassLoader(getClass().getClassLoader()) {
182   
 
183  2 toggle @Override
184    public InputStream getResourceAsStream(final String name) {
185  2 if ("easymock.properties".equals(name)) {
186  2 return null;
187    }
188  0 return super.getResourceAsStream(name);
189    }
190   
191    };
192  2 final ClassLoader old = Thread.currentThread().getContextClassLoader();
193  2 try {
194  2 resetInstance();
195   
196    // Set our class loader
197  2 Thread.currentThread().setContextClassLoader(cl);
198   
199    // This instance will try to load easymock.properties with our
200    // custom
201    // class loader and so won't find it
202  2 EasyMockProperties.getInstance();
203   
204    // And so it shouldn't find "easymock.a"
205  2 assertExpectedValue(null, "easymock.a");
206    // But "easymock.b" is still there
207  2 assertExpectedValue("3", "easymock.b");
208   
209    } finally {
210    // Whatever happens, set the initial class loader back or it'll get
211    // messy
212  2 Thread.currentThread().setContextClassLoader(old);
213    }
214    }
215   
 
216  8 toggle private static void resetInstance() throws NoSuchFieldException, IllegalAccessException {
217    // Cheat and make the singleton uninitialized
218  8 final Field field = EasyMockProperties.class.getDeclaredField("instance");
219  8 field.setAccessible(true);
220  8 field.set(null, null);
221    }
222   
 
223  22 toggle private static void assertExpectedValue(final String expected, final String key) {
224  22 assertEquals(expected, getEasyMockProperty(key));
225    }
226    }