1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.module.cache.filter; |
35 |
|
|
36 |
|
import static org.hamcrest.Matchers.hasItem; |
37 |
|
import static org.hamcrest.core.Is.is; |
38 |
|
import static org.hamcrest.core.IsEqual.equalTo; |
39 |
|
import static org.junit.Assert.*; |
40 |
|
import static org.mockito.Mockito.*; |
41 |
|
|
42 |
|
import info.magnolia.cms.cache.CacheConstants; |
43 |
|
import info.magnolia.cms.core.SystemProperty; |
44 |
|
import info.magnolia.context.MgnlContext; |
45 |
|
import info.magnolia.context.WebContext; |
46 |
|
import info.magnolia.test.ComponentsTestUtil; |
47 |
|
import info.magnolia.test.TestMagnoliaConfigurationProperties; |
48 |
|
|
49 |
|
import java.io.ByteArrayOutputStream; |
50 |
|
import java.io.File; |
51 |
|
import java.io.FileInputStream; |
52 |
|
import java.io.FileOutputStream; |
53 |
|
import java.io.IOException; |
54 |
|
import java.io.InputStream; |
55 |
|
import java.io.OutputStream; |
56 |
|
import java.util.Collection; |
57 |
|
|
58 |
|
import javax.servlet.http.HttpServletRequest; |
59 |
|
import javax.servlet.http.HttpServletResponse; |
60 |
|
|
61 |
|
import org.apache.commons.io.IOUtils; |
62 |
|
import org.apache.commons.io.output.ThresholdingOutputStream; |
63 |
|
import org.junit.After; |
64 |
|
import org.junit.Before; |
65 |
|
import org.junit.Test; |
66 |
|
|
67 |
|
|
68 |
|
@link |
69 |
|
|
|
|
| 96.4% |
Uncovered Elements: 3 (84) |
Complexity: 13 |
Complexity Density: 0.18 |
|
70 |
|
public class CacheResponseWrapperTest { |
71 |
|
|
72 |
|
private HttpServletRequest request; |
73 |
|
private HttpServletResponse response = mock(HttpServletResponse.class); |
74 |
|
private CacheResponseWrapper wrapper = new CacheResponseWrapper(response, CacheResponseWrapper.DEFAULT_THRESHOLD, false); |
75 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
76 |
8 |
@Before... |
77 |
|
public void setUp() throws IOException { |
78 |
8 |
final InputStream in = ComponentsTestUtil.class.getResourceAsStream("/test-magnolia.properties"); |
79 |
8 |
final TestMagnoliaConfigurationProperties cfg = new TestMagnoliaConfigurationProperties(in); |
80 |
8 |
SystemProperty.setMagnoliaConfigurationProperties(cfg); |
81 |
|
|
82 |
8 |
request = mock(HttpServletRequest.class); |
83 |
8 |
WebContext ctx = mock(WebContext.class); |
84 |
8 |
MgnlContext.setInstance(ctx); |
85 |
8 |
when(ctx.getRequest()).thenReturn(request); |
86 |
|
} |
87 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
88 |
8 |
@After... |
89 |
|
public void tearDown() throws IOException { |
90 |
8 |
SystemProperty.clear(); |
91 |
8 |
MgnlContext.setInstance(null); |
92 |
|
} |
93 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
94 |
1 |
@Test... |
95 |
|
public void testSetHeader() { |
96 |
|
|
97 |
1 |
String testName = "name"; |
98 |
1 |
wrapper.setHeader(testName, "value"); |
99 |
1 |
Collection names = (Collection) wrapper.getHeaders().get(testName); |
100 |
1 |
assertEquals(1, names.size()); |
101 |
|
|
102 |
|
|
103 |
1 |
wrapper.setHeader(testName, "anotherValue"); |
104 |
1 |
names = (Collection) wrapper.getHeaders().get(testName); |
105 |
1 |
assertEquals(1, names.size()); |
106 |
|
} |
107 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
108 |
1 |
@Test... |
109 |
|
public void cacheNegotiationHeadersAreNotSwallowedWhenSettingHeader() { |
110 |
|
|
111 |
1 |
wrapper.setResponseExpirationDetectionEnabled(); |
112 |
|
|
113 |
|
|
114 |
1 |
wrapper.setHeader(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_NO_CACHE); |
115 |
|
|
116 |
|
|
117 |
1 |
assertThat((Collection<String>) wrapper.getHeaders().get(CacheConstants.HEADER_CACHE_CONTROL), hasItem(CacheConstants.HEADER_VALUE_NO_CACHE)); |
118 |
|
} |
119 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
120 |
1 |
@Test... |
121 |
|
public void cacheNegotiationHeadersAreNotSwallowedWhenAddingHeader() { |
122 |
|
|
123 |
1 |
wrapper.setResponseExpirationDetectionEnabled(); |
124 |
|
|
125 |
|
|
126 |
1 |
wrapper.addHeader(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_NO_CACHE); |
127 |
|
|
128 |
|
|
129 |
1 |
assertThat((Collection<String>) wrapper.getHeaders().get(CacheConstants.HEADER_CACHE_CONTROL), hasItem(CacheConstants.HEADER_VALUE_NO_CACHE)); |
130 |
|
} |
131 |
|
|
|
|
| 85% |
Uncovered Elements: 3 (20) |
Complexity: 4 |
Complexity Density: 0.2 |
1PASS
|
|
132 |
1 |
@Test... |
133 |
|
public void testGetLastModified() { |
134 |
1 |
try { |
135 |
1 |
wrapper.getLastModified(); |
136 |
0 |
fail("No Last-Modified was set yet - expected Exception here!"); |
137 |
|
} catch (IllegalStateException e) { |
138 |
1 |
assertTrue(true); |
139 |
|
} |
140 |
|
|
141 |
1 |
String timestampString = "Fri, 03 Dec 2010 07:14:01 CET"; |
142 |
1 |
String lastModified = "Last-Modified"; |
143 |
1 |
wrapper.setHeader(lastModified, timestampString); |
144 |
1 |
assertEquals(1291356841000l, wrapper.getLastModified()); |
145 |
|
|
146 |
|
|
147 |
1 |
wrapper.addHeader(lastModified, timestampString); |
148 |
|
|
149 |
1 |
try { |
150 |
1 |
wrapper.getLastModified(); |
151 |
0 |
fail("Two Last-Modified were set - expected Exception here!"); |
152 |
|
} catch (IllegalStateException e) { |
153 |
1 |
assertTrue(true); |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
1 |
wrapper.setDateHeader(lastModified, 1291356841000l); |
158 |
1 |
assertEquals(1291356841000l, wrapper.getLastModified()); |
159 |
|
|
160 |
|
|
161 |
1 |
wrapper.setIntHeader(lastModified, 42); |
162 |
1 |
try { |
163 |
1 |
wrapper.getLastModified(); |
164 |
0 |
fail("Last-Modified was set as int - expected Exception here!"); |
165 |
|
} catch (IllegalStateException e) { |
166 |
1 |
assertTrue(true); |
167 |
|
} |
168 |
|
} |
169 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
170 |
1 |
@Test... |
171 |
|
public void testTresholdReachedReturnFileOutputStreamWhenServeIfThresholdReachedIsSetToFalse() throws IOException, InstantiationException, IllegalAccessException { |
172 |
|
|
173 |
1 |
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
174 |
|
|
175 |
|
|
176 |
1 |
OutputStream out = wrapper.thresholdReached(byteArrayOutputStream); |
177 |
|
|
178 |
|
|
179 |
1 |
assertTrue(out instanceof FileOutputStream); |
180 |
1 |
assertFalse(out instanceof ByteArrayOutputStream); |
181 |
|
} |
182 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
183 |
1 |
@Test... |
184 |
|
public void testTresholdCanBeSpecifiedForTimeConsumingResources() throws Exception { |
185 |
|
|
186 |
1 |
when(request.getAttribute(CacheResponseWrapper.ATTRIBUTE_IN_MEMORY_THRESHOLD)).thenReturn(20); |
187 |
1 |
CacheResponseWrapper responseWrapper = new CacheResponseWrapper(response, 10, false); |
188 |
1 |
ThresholdingOutputStream out = responseWrapper.getThresholdingOutputStream(); |
189 |
|
|
190 |
|
|
191 |
1 |
out.write("1234567890".getBytes()); |
192 |
|
|
193 |
1 |
assertThat(out.isThresholdExceeded(), equalTo(false)); |
194 |
|
|
195 |
|
|
196 |
1 |
out.write('c'); |
197 |
|
|
198 |
1 |
assertThat(out.isThresholdExceeded(), equalTo(false)); |
199 |
|
|
200 |
|
|
201 |
1 |
out.write("123456789".getBytes()); |
202 |
|
|
203 |
1 |
assertThat(out.isThresholdExceeded(), equalTo(false)); |
204 |
|
|
205 |
|
|
206 |
1 |
out.write('c'); |
207 |
|
|
208 |
1 |
assertThat(out.isThresholdExceeded(), equalTo(true)); |
209 |
|
} |
210 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
211 |
1 |
@Test... |
212 |
|
public void testTempFileSizeIsSameAsOriginalSize() throws IOException, InstantiationException, IllegalAccessException { |
213 |
|
|
214 |
1 |
File originalFile = new File(this.getClass().getClassLoader().getResource("styles.css").getFile()); |
215 |
1 |
final InputStream is = new FileInputStream(originalFile); |
216 |
|
|
217 |
|
|
218 |
1 |
IOUtils.copyLarge(is, wrapper.getOutputStream()); |
219 |
|
|
220 |
|
|
221 |
1 |
assertEquals(wrapper.getContentFile().length(), originalFile.length()); |
222 |
|
} |
223 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
224 |
1 |
@Test... |
225 |
|
public void replayContentAboveThresholdRespectsServeFlag() throws IOException { |
226 |
|
|
227 |
1 |
final byte[] payload = "Media hurting and left behind, I say: it looked like a million people. It's imploding as we sit with my steak.".getBytes(); |
228 |
|
|
229 |
1 |
final HttpServletResponse originalResponse = mock(HttpServletResponse.class); |
230 |
1 |
when(originalResponse.getOutputStream()).thenReturn(new SimpleServletOutputStream(new ByteArrayOutputStream())); |
231 |
1 |
final HttpServletResponse target = mock(HttpServletResponse.class); |
232 |
1 |
final ByteArrayOutputStream targetStream = new ByteArrayOutputStream(); |
233 |
1 |
when(target.getOutputStream()).thenReturn(new SimpleServletOutputStream(targetStream)); |
234 |
|
|
235 |
1 |
final CacheResponseWrapper serveWrapper = new CacheResponseWrapper(originalResponse, payload.length - 1, true); |
236 |
1 |
final CacheResponseWrapper noServeWrapper = new CacheResponseWrapper(originalResponse, payload.length - 1, false); |
237 |
|
|
238 |
|
|
239 |
1 |
serveWrapper.getOutputStream().write(payload); |
240 |
1 |
serveWrapper.replayContent(target, true); |
241 |
|
|
242 |
|
|
243 |
|
|
244 |
1 |
noServeWrapper.getOutputStream().write(payload); |
245 |
1 |
noServeWrapper.replayContent(target, true); |
246 |
1 |
assertThat(targetStream.toByteArray(), is(payload)); |
247 |
|
|
248 |
|
|
249 |
|
} |
250 |
|
} |