Clover icon

Magnolia Module Cache 5.5.9

  1. Project Clover database Mon Nov 25 2019 16:46:50 CET
  2. Package info.magnolia.module.cache.filter

File CacheResponseWrapperTest.java

 

Code metrics

0
74
10
1
250
142
13
0.18
7.4
10
1.3

Classes

Class Line # Actions
CacheResponseWrapperTest 70 74 0% 13 3
0.9642857396.4%
 

Contributing tests

This file is covered by 8 tests. .

Source view

1    /**
2    * This file Copyright (c) 2008-2018 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
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    * Test for {@link CacheResponseWrapper}.
69    */
 
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   
 
76  8 toggle @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   
 
88  8 toggle @After
89    public void tearDown() throws IOException {
90  8 SystemProperty.clear();
91  8 MgnlContext.setInstance(null);
92    }
93   
 
94  1 toggle @Test
95    public void testSetHeader() {
96    // set one value - one will be in...
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    // set another value - only this should be in then
103  1 wrapper.setHeader(testName, "anotherValue");
104  1 names = (Collection) wrapper.getHeaders().get(testName);
105  1 assertEquals(1, names.size());
106    }
107   
 
108  1 toggle @Test
109    public void cacheNegotiationHeadersAreNotSwallowedWhenSettingHeader() {
110    //GIVEN
111  1 wrapper.setResponseExpirationDetectionEnabled();
112   
113    //WHEN
114  1 wrapper.setHeader(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_NO_CACHE);
115   
116    //THEN
117  1 assertThat((Collection<String>) wrapper.getHeaders().get(CacheConstants.HEADER_CACHE_CONTROL), hasItem(CacheConstants.HEADER_VALUE_NO_CACHE));
118    }
119   
 
120  1 toggle @Test
121    public void cacheNegotiationHeadersAreNotSwallowedWhenAddingHeader() {
122    //GIVEN
123  1 wrapper.setResponseExpirationDetectionEnabled();
124   
125    //WHEN
126  1 wrapper.addHeader(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_NO_CACHE);
127   
128    //THEN
129  1 assertThat((Collection<String>) wrapper.getHeaders().get(CacheConstants.HEADER_CACHE_CONTROL), hasItem(CacheConstants.HEADER_VALUE_NO_CACHE));
130    }
131   
 
132  1 toggle @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    // force adding second LastModified-value
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    // add long Last-Modified
157  1 wrapper.setDateHeader(lastModified, 1291356841000l);
158  1 assertEquals(1291356841000l, wrapper.getLastModified());
159   
160    // add invalid int-type for Last-Modified
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   
 
170  1 toggle @Test
171    public void testTresholdReachedReturnFileOutputStreamWhenServeIfThresholdReachedIsSetToFalse() throws IOException, InstantiationException, IllegalAccessException {
172    // GIVEN
173  1 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
174   
175    // WHEN
176  1 OutputStream out = wrapper.thresholdReached(byteArrayOutputStream);
177   
178    // THEN
179  1 assertTrue(out instanceof FileOutputStream);
180  1 assertFalse(out instanceof ByteArrayOutputStream);
181    }
182   
 
183  1 toggle @Test
184    public void testTresholdCanBeSpecifiedForTimeConsumingResources() throws Exception {
185    // GIVEN
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    // WHEN
191  1 out.write("1234567890".getBytes()); //10 bytes to the default threshold
192    // THEN
193  1 assertThat(out.isThresholdExceeded(), equalTo(false));
194   
195    // WHEN
196  1 out.write('c'); //one byte beyond the default threshold
197    // THEN
198  1 assertThat(out.isThresholdExceeded(), equalTo(false));
199   
200    // WHEN
201  1 out.write("123456789".getBytes()); //another 9 bytes to the threshold * 2
202    // THEN
203  1 assertThat(out.isThresholdExceeded(), equalTo(false));
204   
205    // WHEN
206  1 out.write('c'); //one byte beyond the threshold * 2
207    // THEN
208  1 assertThat(out.isThresholdExceeded(), equalTo(true));
209    }
210   
 
211  1 toggle @Test
212    public void testTempFileSizeIsSameAsOriginalSize() throws IOException, InstantiationException, IllegalAccessException {
213    // GIVEN
214  1 File originalFile = new File(this.getClass().getClassLoader().getResource("styles.css").getFile());
215  1 final InputStream is = new FileInputStream(originalFile);
216   
217    // WHEN
218  1 IOUtils.copyLarge(is, wrapper.getOutputStream());
219   
220    // THEN
221  1 assertEquals(wrapper.getContentFile().length(), originalFile.length());
222    }
223   
 
224  1 toggle @Test
225    public void replayContentAboveThresholdRespectsServeFlag() throws IOException {
226    // GIVEN
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    // WHEN & THEN
239  1 serveWrapper.getOutputStream().write(payload);
240  1 serveWrapper.replayContent(target, true);
241    // TODO: We should check targetStream's content here, but due to a bug, this would be empty (MGNLCACHE-191)
242    // assertThat(targetStream.toByteArray(), is(payload));
243   
244  1 noServeWrapper.getOutputStream().write(payload);
245  1 noServeWrapper.replayContent(target, true);
246  1 assertThat(targetStream.toByteArray(), is(payload));
247   
248    // (implicit test: No exception should have been thrown, e.g. when looking for a non-existing temp file)
249    }
250    }