1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
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 |
32 |
|
|
|
|
| 89,3% |
Uncovered Elements: 9 (84) |
Complexity: 18 |
Complexity Density: 0,28 |
|
33 |
|
public class EasyMockPropertiesTest { |
34 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0,11 |
|
35 |
2
|
@BeforeClass... |
36 |
|
public static void setup() throws Exception { |
37 |
|
|
38 |
|
|
39 |
2
|
resetInstance(); |
40 |
|
|
41 |
|
|
42 |
2
|
System.setProperty("easymock.b", "3"); |
43 |
|
|
44 |
|
|
45 |
2
|
System.setProperty("easymock.f", "5"); |
46 |
|
|
47 |
|
|
48 |
2
|
System.setProperty("xxx.yyy", "6"); |
49 |
|
|
50 |
|
|
51 |
2
|
System.getProperties().put(System.class, System.class); |
52 |
2
|
System.getProperties().put("easymock.g", System.class); |
53 |
|
|
54 |
|
|
55 |
2
|
setEasyMockProperty("easymock.e", "7"); |
56 |
|
|
57 |
|
|
58 |
2
|
setEasyMockProperty("easymock.c", "8"); |
59 |
|
|
60 |
|
|
61 |
2
|
System.setProperty("easymock.h", "4"); |
62 |
|
} |
63 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0,12 |
1
PASS
|
|
64 |
2
|
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
1
PASS
|
|
76 |
2
|
@Test... |
77 |
|
public void testGetProperty() { |
78 |
2
|
final EasyMockProperties instance = EasyMockProperties.getInstance(); |
79 |
|
|
80 |
|
|
81 |
2
|
assertEquals("1", instance.getProperty("easymock.a", "10")); |
82 |
|
|
83 |
2
|
assertEquals("10", instance.getProperty("easymock.z", "10")); |
84 |
|
|
85 |
2
|
assertNull(instance.getProperty("easymock.z", null)); |
86 |
|
} |
87 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
1
PASS
|
|
88 |
2
|
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0,14 |
1
PASS
|
|
95 |
2
|
@Test... |
96 |
|
public void testNoThreadContextClassLoader() throws Exception { |
97 |
2
|
final ClassLoader old = Thread.currentThread().getContextClassLoader(); |
98 |
2
|
try { |
99 |
2
|
resetInstance(); |
100 |
|
|
101 |
|
|
102 |
2
|
Thread.currentThread().setContextClassLoader(null); |
103 |
|
|
104 |
|
|
105 |
|
|
106 |
2
|
EasyMockProperties.getInstance(); |
107 |
|
|
108 |
|
|
109 |
2
|
assertExpectedValue("1", "easymock.a"); |
110 |
|
|
111 |
|
} finally { |
112 |
|
|
113 |
|
|
114 |
2
|
Thread.currentThread().setContextClassLoader(old); |
115 |
|
} |
116 |
|
} |
117 |
|
|
|
|
| 91,7% |
Uncovered Elements: 1 (12) |
Complexity: 2 |
Complexity Density: 0,17 |
1
PASS
|
|
118 |
2
|
@Test... |
119 |
|
public void testBadPropertiesFile() throws Exception { |
120 |
|
|
121 |
2
|
final Boolean[] close = new Boolean[1]; |
122 |
|
|
123 |
|
|
124 |
2
|
final ClassLoader cl = new ClassLoader(getClass().getClassLoader()) { |
125 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
126 |
2
|
@Override... |
127 |
|
public InputStream getResourceAsStream(final String name) { |
128 |
2
|
if ("easymock.properties".equals(name)) { |
129 |
2
|
return new InputStream() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
130 |
2
|
@Override... |
131 |
|
public void close() throws IOException { |
132 |
2
|
close[0] = Boolean.TRUE; |
133 |
|
} |
134 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
135 |
2
|
@Override... |
136 |
|
public int read(final byte[] b, final int off, final int len) throws IOException { |
137 |
2
|
throw new IOException("Failed!"); |
138 |
|
} |
139 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
140 |
0
|
@Override... |
141 |
|
public int read(final byte[] b) throws IOException { |
142 |
0
|
throw new IOException("Failed!"); |
143 |
|
} |
144 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
145 |
0
|
@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 |
|
|
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 |
|
|
168 |
2
|
assertSame(Boolean.TRUE, close[0]); |
169 |
|
} |
170 |
|
|
171 |
|
} finally { |
172 |
|
|
173 |
|
|
174 |
2
|
Thread.currentThread().setContextClassLoader(old); |
175 |
|
} |
176 |
|
} |
177 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0,11 |
1
PASS
|
|
178 |
2
|
@Test... |
179 |
|
public void testNoEasymockPropertiesFile() throws Exception { |
180 |
|
|
181 |
2
|
final ClassLoader cl = new ClassLoader(getClass().getClassLoader()) { |
182 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
183 |
2
|
@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 |
|
|
197 |
2
|
Thread.currentThread().setContextClassLoader(cl); |
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
2
|
EasyMockProperties.getInstance(); |
203 |
|
|
204 |
|
|
205 |
2
|
assertExpectedValue(null, "easymock.a"); |
206 |
|
|
207 |
2
|
assertExpectedValue("3", "easymock.b"); |
208 |
|
|
209 |
|
} finally { |
210 |
|
|
211 |
|
|
212 |
2
|
Thread.currentThread().setContextClassLoader(old); |
213 |
|
} |
214 |
|
} |
215 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
216 |
8
|
private static void resetInstance() throws NoSuchFieldException, IllegalAccessException {... |
217 |
|
|
218 |
8
|
final Field field = EasyMockProperties.class.getDeclaredField("instance"); |
219 |
8
|
field.setAccessible(true); |
220 |
8
|
field.set(null, null); |
221 |
|
} |
222 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
223 |
22
|
private static void assertExpectedValue(final String expected, final String key) {... |
224 |
22
|
assertEquals(expected, getEasyMockProperty(key)); |
225 |
|
} |
226 |
|
} |