View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.access.spi;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertNotNull;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.ByteArrayOutputStream;
21  import java.io.IOException;
22  import java.io.ObjectOutputStream;
23  
24  import org.junit.Test;
25  
26  import ch.qos.logback.access.dummy.DummyAccessEventBuilder;
27  import ch.qos.logback.access.dummy.DummyRequest;
28  import ch.qos.logback.access.dummy.DummyResponse;
29  import ch.qos.logback.access.dummy.DummyServerAdapter;
30  import ch.qos.logback.access.net.HardenedAccessEventInputStream;
31  
32  public class AccessEventSerializationTest {
33  
34      private Object buildSerializedAccessEvent() throws IOException, ClassNotFoundException {
35          ByteArrayOutputStream baos = new ByteArrayOutputStream();
36          ObjectOutputStream oos = new ObjectOutputStream(baos);
37          IAccessEvent ae = DummyAccessEventBuilder.buildNewAccessEvent();
38          // average time for the next method: 5000 nanos
39          ae.prepareForDeferredProcessing();
40          oos.writeObject(ae);
41          oos.flush();
42  
43          ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
44          HardenedAccessEventInputStream hardenedOIS = new HardenedAccessEventInputStream(bais);
45  
46          Object sae = hardenedOIS.readObject();
47          hardenedOIS.close();
48          return sae;
49      }
50  
51      @Test
52      public void testSerialization() throws IOException, ClassNotFoundException {
53          Object o = buildSerializedAccessEvent();
54          assertNotNull(o);
55          IAccessEvent aeBack = (IAccessEvent) o;
56  
57          assertEquals(DummyResponse.DUMMY_DEFAULT_HDEADER_MAP, aeBack.getResponseHeaderMap());
58          assertEquals(DummyResponse.DUMMY_DEFAULT_HDEADER_MAP.get("x"), aeBack.getResponseHeader("x"));
59          assertEquals(DummyResponse.DUMMY_DEFAULT_HDEADER_MAP.get("headerName1"), aeBack.getResponseHeader("headerName1"));
60          assertEquals(DummyResponse.DUMMY_DEFAULT_HDEADER_MAP.size(), aeBack.getResponseHeaderNameList().size());
61          assertEquals(DummyResponse.DUMMY_DEFAULT_CONTENT_COUNT, aeBack.getContentLength());
62          assertEquals(DummyResponse.DUMMY_DEFAULT_STATUS, aeBack.getStatusCode());
63  
64          assertEquals(DummyRequest.DUMMY_CONTENT_STRING, aeBack.getRequestContent());
65  
66          assertEquals(DummyRequest.DUMMY_RESPONSE_CONTENT_STRING, aeBack.getResponseContent());
67  
68          assertEquals(DummyRequest.DUMMY_DEFAULT_ATTR_MAP.get("testKey"), aeBack.getAttribute("testKey"));
69      }
70  
71      // Web containers may (and will) recycle requests objects. So we must make sure that after
72      // we prepared an event for deferred processing it won't be using data from the original
73      // HttpRequest object which may at that time represent another request
74      @Test
75      public void testAttributesAreNotTakenFromRecycledRequestWhenProcessingDeferred() {
76  
77          DummyRequest request = new DummyRequest();
78          DummyResponse response = new DummyResponse();
79          DummyServerAdapter adapter = new DummyServerAdapter(request, response);
80  
81          IAccessEvent event = new AccessEvent(request, response, adapter);
82  
83          request.setAttribute("testKey", "ORIGINAL");
84  
85          event.prepareForDeferredProcessing();
86  
87          request.setAttribute("testKey", "NEW");
88  
89          // Event should capture the original value
90          assertEquals("ORIGINAL", event.getAttribute("testKey"));
91      }
92  }