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.pattern;
15  
16  import static org.junit.Assert.assertEquals;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.servlet.http.Cookie;
22  
23  import ch.qos.logback.access.spi.IAccessEvent;
24  import org.junit.After;
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  import ch.qos.logback.access.dummy.DummyRequest;
29  import ch.qos.logback.access.dummy.DummyResponse;
30  import ch.qos.logback.access.dummy.DummyServerAdapter;
31  import ch.qos.logback.access.spi.AccessEvent;
32  
33  public class ConverterTest {
34  
35      IAccessEvent event;
36      DummyRequest request;
37      DummyResponse response;
38  
39      @Before
40      public void setUp() throws Exception {
41          request = new DummyRequest();
42          response = new DummyResponse();
43          event = createEvent();
44      }
45  
46      @After
47      public void tearDown() throws Exception {
48          event = null;
49          request = null;
50          response = null;
51      }
52  
53      @Test
54      public void testContentLengthConverter() {
55          ContentLengthConverter converter = new ContentLengthConverter();
56          converter.start();
57          String result = converter.convert(event);
58          assertEquals(Long.toString(event.getServerAdapter().getContentLength()), result);
59      }
60  
61      @Test
62      public void testDateConverter() {
63          DateConverter converter = new DateConverter();
64          converter.start();
65          String result = converter.convert(event);
66          assertEquals(converter.cachingDateFormatter.format(event.getTimeStamp()), result);
67      }
68  
69      public void testLineLocalPortConverter() {
70          LocalPortConverter converter = new LocalPortConverter();
71          converter.start();
72          String result = converter.convert(event);
73          assertEquals(Integer.toString(request.getLocalPort()), result);
74      }
75  
76      @Test
77      public void testRemoteHostConverter() {
78          RemoteHostConverter converter = new RemoteHostConverter();
79          converter.start();
80          String result = converter.convert(event);
81          assertEquals(request.getRemoteHost(), result);
82      }
83  
84      @Test
85      public void testRemoteIPAddressConverter() {
86          RemoteIPAddressConverter converter = new RemoteIPAddressConverter();
87          converter.start();
88          String result = converter.convert(event);
89          assertEquals(request.getRemoteAddr(), result);
90      }
91  
92      @Test
93      public void testRemoteUserConverter() {
94          RemoteUserConverter converter = new RemoteUserConverter();
95          converter.start();
96          String result = converter.convert(event);
97          assertEquals(request.getRemoteUser(), result);
98      }
99  
100     @Test
101     public void testRequestAttributeConverter() {
102         RequestAttributeConverter converter = new RequestAttributeConverter();
103         List<String> optionList = new ArrayList<String>();
104         optionList.add("testKey");
105         converter.setOptionList(optionList);
106         converter.start();
107         String result = converter.convert(event);
108         assertEquals(request.getAttribute("testKey"), result);
109     }
110 
111     @Test
112     public void testRequestCookieConverter() {
113         RequestCookieConverter converter = new RequestCookieConverter();
114         List<String> optionList = new ArrayList<String>();
115         optionList.add("testName");
116         converter.setOptionList(optionList);
117         converter.start();
118         String result = converter.convert(event);
119         Cookie cookie = request.getCookies()[0];
120         assertEquals(cookie.getValue(), result);
121     }
122 
123     @Test
124     public void testRequestHeaderConverter() {
125         RequestHeaderConverter converter = new RequestHeaderConverter();
126         List<String> optionList = new ArrayList<String>();
127         optionList.add("headerName1");
128         converter.setOptionList(optionList);
129         converter.start();
130         String result = converter.convert(event);
131         assertEquals(request.getHeader("headerName1"), result);
132     }
133 
134     @Test
135     public void testRequestMethodConverter() {
136         RequestMethodConverter converter = new RequestMethodConverter();
137         converter.start();
138         String result = converter.convert(event);
139         assertEquals(request.getMethod(), result);
140     }
141 
142     @Test
143     public void testRequestProtocolConverter() {
144         RequestProtocolConverter converter = new RequestProtocolConverter();
145         converter.start();
146         String result = converter.convert(event);
147         assertEquals(request.getProtocol(), result);
148     }
149 
150     @Test
151     public void testRequestURIConverter() {
152         RequestURIConverter converter = new RequestURIConverter();
153         converter.start();
154         String result = converter.convert(event);
155         assertEquals(request.getRequestURI(), result);
156     }
157 
158     @Test
159     public void testRequestURLConverter() {
160         RequestURLConverter converter = new RequestURLConverter();
161         converter.start();
162         String result = converter.convert(event);
163         String expected = request.getMethod() + " " + request.getRequestURI() + " " + request.getProtocol();
164         assertEquals(expected, result);
165     }
166 
167     @Test
168     public void testResponseHeaderConverter() {
169         ResponseHeaderConverter converter = new ResponseHeaderConverter();
170         List<String> optionList = new ArrayList<String>();
171         optionList.add("headerName1");
172         converter.setOptionList(optionList);
173         converter.start();
174         String result = converter.convert(event);
175         assertEquals(request.getHeader("headerName1"), result);
176     }
177 
178     @Test
179     public void testServerNameConverter() {
180         ServerNameConverter converter = new ServerNameConverter();
181         converter.start();
182         String result = converter.convert(event);
183         assertEquals(request.getServerName(), result);
184     }
185 
186     @Test
187     public void testStatusCodeConverter() {
188         StatusCodeConverter converter = new StatusCodeConverter();
189         converter.start();
190         String result = converter.convert(event);
191         assertEquals(Integer.toString(event.getServerAdapter().getStatusCode()), result);
192     }
193 
194     private IAccessEvent createEvent() {
195         DummyServerAdapter dummyAdapter = new DummyServerAdapter(request, response);
196         return new AccessEvent(request, response, dummyAdapter);
197     }
198 
199 }