001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.access.pattern;
015
016import static org.junit.Assert.assertEquals;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import javax.servlet.http.Cookie;
022
023import ch.qos.logback.access.spi.IAccessEvent;
024import org.junit.After;
025import org.junit.Before;
026import org.junit.Test;
027
028import ch.qos.logback.access.dummy.DummyRequest;
029import ch.qos.logback.access.dummy.DummyResponse;
030import ch.qos.logback.access.dummy.DummyServerAdapter;
031import ch.qos.logback.access.spi.AccessEvent;
032
033public class ConverterTest {
034
035    IAccessEvent event;
036    DummyRequest request;
037    DummyResponse response;
038
039    @Before
040    public void setUp() throws Exception {
041        request = new DummyRequest();
042        response = new DummyResponse();
043        event = createEvent();
044    }
045
046    @After
047    public void tearDown() throws Exception {
048        event = null;
049        request = null;
050        response = null;
051    }
052
053    @Test
054    public void testContentLengthConverter() {
055        ContentLengthConverter converter = new ContentLengthConverter();
056        converter.start();
057        String result = converter.convert(event);
058        assertEquals(Long.toString(event.getServerAdapter().getContentLength()), result);
059    }
060
061    @Test
062    public void testDateConverter() {
063        DateConverter converter = new DateConverter();
064        converter.start();
065        String result = converter.convert(event);
066        assertEquals(converter.cachingDateFormatter.format(event.getTimeStamp()), result);
067    }
068
069    public void testLineLocalPortConverter() {
070        LocalPortConverter converter = new LocalPortConverter();
071        converter.start();
072        String result = converter.convert(event);
073        assertEquals(Integer.toString(request.getLocalPort()), result);
074    }
075
076    @Test
077    public void testRemoteHostConverter() {
078        RemoteHostConverter converter = new RemoteHostConverter();
079        converter.start();
080        String result = converter.convert(event);
081        assertEquals(request.getRemoteHost(), result);
082    }
083
084    @Test
085    public void testRemoteIPAddressConverter() {
086        RemoteIPAddressConverter converter = new RemoteIPAddressConverter();
087        converter.start();
088        String result = converter.convert(event);
089        assertEquals(request.getRemoteAddr(), result);
090    }
091
092    @Test
093    public void testRemoteUserConverter() {
094        RemoteUserConverter converter = new RemoteUserConverter();
095        converter.start();
096        String result = converter.convert(event);
097        assertEquals(request.getRemoteUser(), result);
098    }
099
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}