
Author: seb Date: Fri Sep 15 16:00:56 2006 New Revision: 591 Added: logback/trunk/logback-access/src/test/java/ logback/trunk/logback-access/src/test/java/ch/ logback/trunk/logback-access/src/test/java/ch/qos/ logback/trunk/logback-access/src/test/java/ch/qos/logback/ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/ConverterTest.java logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/DummyRequest.java logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/DummyResponse.java Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/PatternLayout.java Log: - added word patterns to PatternLayout - added a test class for Converters. - added two dummy implementations of HttpServletRequest and HttpServletResponse Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/PatternLayout.java ============================================================================== --- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/PatternLayout.java (original) +++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/PatternLayout.java Fri Sep 15 16:00:56 2006 @@ -64,6 +64,7 @@ defaultConverterMap.put("l", NAConverter.class.getName()); defaultConverterMap.put("m", RequestMethodConverter.class.getName()); + defaultConverterMap.put("requestMethod", RequestMethodConverter.class.getName()); defaultConverterMap.put("r", RequestURLConverter.class.getName()); defaultConverterMap.put("requestURL", RequestURLConverter.class.getName()); @@ -73,12 +74,16 @@ defaultConverterMap.put("t", DateConverter.class.getName()); + defaultConverterMap.put("date", DateConverter.class.getName()); defaultConverterMap.put("u", RemoteUserConverter.class.getName()); + defaultConverterMap.put("user", RemoteUserConverter.class.getName()); defaultConverterMap.put("U", RequestURIConverter.class.getName()); + defaultConverterMap.put("requestURI", RequestURIConverter.class.getName()); defaultConverterMap.put("v", ServerNameConverter.class.getName()); + defaultConverterMap.put("server", ServerNameConverter.class.getName()); } @@ -90,7 +95,7 @@ /** * Returns the default converter map for this instance. */ - public Map getDefaultConverterMap() { + public Map<String, String> getDefaultConverterMap() { return defaultConverterMap; } Added: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/ConverterTest.java ============================================================================== --- (empty file) +++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/ConverterTest.java Fri Sep 15 16:00:56 2006 @@ -0,0 +1,162 @@ +package ch.qos.logback.access.pattern; + +import java.util.ArrayList; +import java.util.List; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import junit.framework.TestCase; +import ch.qos.logback.access.pattern.helpers.DummyRequest; +import ch.qos.logback.access.pattern.helpers.DummyResponse; +import ch.qos.logback.access.spi.AccessEvent; + +public class ConverterTest extends TestCase { + + AccessEvent event; + HttpServletRequest request; + HttpServletResponse response; + + public void setUp() throws Exception { + super.setUp(); + request = new DummyRequest(); + response = new DummyResponse(); + event = createEvent(); + } + + public void tearDown() throws Exception { + super.tearDown(); + event = null; + request = null; + response = null; + } + + public void testContentLengthConverter() { + // TODO when AccessEvent has been modified + } + + public void testDateConverter() { + DateConverter converter = new DateConverter(); + converter.start(); + String result = converter.convert(event); + assertEquals(converter.simpleFormat.format(event.getTimeStamp()), result); + } + + public void testLineLocalPortConverter() { + LocalPortConverter converter = new LocalPortConverter(); + converter.start(); + String result = converter.convert(event); + assertEquals(Integer.toString(request.getLocalPort()), result); + } + + public void testRemoteHostConverter() { + RemoteHostConverter converter = new RemoteHostConverter(); + converter.start(); + String result = converter.convert(event); + assertEquals(request.getRemoteHost(), result); + } + + public void testRemoteIPAddressConverter() { + RemoteIPAddressConverter converter = new RemoteIPAddressConverter(); + converter.start(); + String result = converter.convert(event); + assertEquals(request.getRemoteAddr(), result); + } + + public void testRemoteUserConverter() { + RemoteUserConverter converter = new RemoteUserConverter(); + converter.start(); + String result = converter.convert(event); + assertEquals(request.getRemoteUser(), result); + } + + public void testRequestAttributeConverter() { + RequestAttributeConverter converter = new RequestAttributeConverter(); + List<String> optionList = new ArrayList<String>(); + optionList.add("testKey"); + converter.setOptionList(optionList); + converter.start(); + String result = converter.convert(event); + assertEquals(request.getAttribute("testKey"), result); + } + + public void testRequestCookieConverter() { + RequestCookieConverter converter = new RequestCookieConverter(); + List<String> optionList = new ArrayList<String>(); + optionList.add("testName"); + converter.setOptionList(optionList); + converter.start(); + String result = converter.convert(event); + Cookie cookie = request.getCookies()[0]; + assertEquals(cookie.getValue(), result); + } + + public void testRequestHeaderConverter() { + RequestHeaderConverter converter = new RequestHeaderConverter(); + List<String> optionList = new ArrayList<String>(); + optionList.add("headerName1"); + converter.setOptionList(optionList); + converter.start(); + String result = converter.convert(event); + assertEquals(request.getHeader("headerName1"), result); + } + + public void testRequestMethodConverter() { + RequestMethodConverter converter = new RequestMethodConverter(); + converter.start(); + String result = converter.convert(event); + assertEquals(request.getMethod(), result); + } + + public void testRequestProtocolConverter() { + RequestProtocolConverter converter = new RequestProtocolConverter(); + converter.start(); + String result = converter.convert(event); + assertEquals(request.getProtocol(), result); + } + + public void testRequestURIConverter() { + RequestURIConverter converter = new RequestURIConverter(); + converter.start(); + String result = converter.convert(event); + assertEquals(request.getRequestURI(), result); + } + + public void testRequestURLConverter() { + RequestURLConverter converter = new RequestURLConverter(); + converter.start(); + String result = converter.convert(event); + String expected = request.getMethod() + " " + request.getRequestURI() + " " + + request.getProtocol(); + assertEquals(expected, result); + } + + public void testResponseHeaderConverter() { + // TODO + // ResponseHeaderConverter converter = new ResponseHeaderConverter(); + // List<String> optionList = new ArrayList<String>(); + // optionList.add("headerName1"); + // converter.setOptionList(optionList); + // converter.start(); + // String result = converter.convert(event); + // assertEquals(request.getHeader("headerName1"), result); + } + + public void testServerNameConverter() { + ServerNameConverter converter = new ServerNameConverter(); + converter.start(); + String result = converter.convert(event); + assertEquals(request.getServerName(), result); + } + + public void testStatusCodeConverter() { + //TODO + } + + private AccessEvent createEvent() { + AccessEvent ae = new AccessEvent(request, response); + return ae; + } + +} Added: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/DummyRequest.java ============================================================================== --- (empty file) +++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/DummyRequest.java Fri Sep 15 16:00:56 2006 @@ -0,0 +1,246 @@ +package ch.qos.logback.access.pattern.helpers; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.security.Principal; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Locale; +import java.util.Map; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletInputStream; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + +public class DummyRequest implements HttpServletRequest { + + Hashtable<String, String> headerNames; + + public DummyRequest() { + headerNames = new Hashtable<String, String>(); + headerNames.put("headerName1", "headerValue1"); + headerNames.put("headerName2", "headerValue2"); + } + + public String getAuthType() { + return null; + } + + public String getContextPath() { + return null; + } + + public Cookie[] getCookies() { + Cookie cookie = new Cookie("testName", "testCookie"); + return new Cookie[] {cookie}; + } + + public long getDateHeader(String arg0) { + return 0; + } + + public String getHeader(String key) { + return headerNames.get(key); + } + + public Enumeration getHeaderNames() { + return headerNames.keys(); + } + + public Enumeration getHeaders(String arg0) { + return null; + } + + public int getIntHeader(String arg0) { + return 0; + } + + public String getMethod() { + return "testMethod"; + } + + public String getPathInfo() { + return null; + } + + public String getPathTranslated() { + return null; + } + + public String getQueryString() { + return null; + } + + public String getRemoteUser() { + return "testUser"; + } + + public String getRequestURI() { + return "testURI"; + } + + public StringBuffer getRequestURL() { + return null; + } + + public String getRequestedSessionId() { + return null; + } + + public String getServletPath() { + return null; + } + + public HttpSession getSession() { + return null; + } + + public HttpSession getSession(boolean arg0) { + return null; + } + + public Principal getUserPrincipal() { + return null; + } + + public boolean isRequestedSessionIdFromCookie() { + return false; + } + + public boolean isRequestedSessionIdFromURL() { + return false; + } + + public boolean isRequestedSessionIdFromUrl() { + return false; + } + + public boolean isRequestedSessionIdValid() { + return false; + } + + public boolean isUserInRole(String arg0) { + return false; + } + + public Object getAttribute(String key) { + if (key.equals("testKey")) { + return "testKey"; + } else { + return null; + } + } + + public Enumeration getAttributeNames() { + return null; + } + + public String getCharacterEncoding() { + return null; + } + + public int getContentLength() { + return 0; + } + + public String getContentType() { + return null; + } + + public ServletInputStream getInputStream() throws IOException { + return null; + } + + public String getLocalAddr() { + return null; + } + + public String getLocalName() { + return null; + } + + public int getLocalPort() { + return 11; + } + + public Locale getLocale() { + return null; + } + + public Enumeration getLocales() { + return null; + } + + public String getParameter(String arg0) { + return null; + } + + public Map getParameterMap() { + return null; + } + + public Enumeration getParameterNames() { + return null; + } + + public String[] getParameterValues(String arg0) { + return null; + } + + public String getProtocol() { + return "testProtocol"; + } + + public BufferedReader getReader() throws IOException { + return null; + } + + public String getRealPath(String arg0) { + return null; + } + + public String getRemoteAddr() { + return "testRemoteAddress"; + } + + public String getRemoteHost() { + return "testHost"; + } + + public int getRemotePort() { + return 0; + } + + public RequestDispatcher getRequestDispatcher(String arg0) { + return null; + } + + public String getScheme() { + return null; + } + + public String getServerName() { + return "testServerName"; + } + + public int getServerPort() { + return 0; + } + + public boolean isSecure() { + return false; + } + + public void removeAttribute(String arg0) { + } + + public void setAttribute(String arg0, Object arg1) { + } + + public void setCharacterEncoding(String arg0) + throws UnsupportedEncodingException { + } +} Added: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/DummyResponse.java ============================================================================== --- (empty file) +++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/DummyResponse.java Fri Sep 15 16:00:56 2006 @@ -0,0 +1,131 @@ +package ch.qos.logback.access.pattern.helpers; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Hashtable; +import java.util.Locale; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletResponse; + +public class DummyResponse implements HttpServletResponse { + + Hashtable<String, String> headerNames; + + public DummyResponse() { + headerNames = new Hashtable<String, String>(); + headerNames.put("headerName1", "headerValue1"); + headerNames.put("headerName2", "headerValue2"); + } + + public void addCookie(Cookie arg0) { + + } + + public void addDateHeader(String arg0, long arg1) { + } + + public void addHeader(String arg0, String arg1) { + } + + public void addIntHeader(String arg0, int arg1) { + } + + public boolean containsHeader(String arg0) { + return false; + } + + public String encodeRedirectURL(String arg0) { + return null; + } + + public String encodeRedirectUrl(String arg0) { + return null; + } + + public String encodeURL(String arg0) { + return null; + } + + public String encodeUrl(String arg0) { + return null; + } + + public void sendError(int arg0) throws IOException { + } + + public void sendError(int arg0, String arg1) throws IOException { + } + + public void sendRedirect(String arg0) throws IOException { + } + + public void setDateHeader(String arg0, long arg1) { + } + + public void setHeader(String arg0, String arg1) { + } + + public void setIntHeader(String arg0, int arg1) { + } + + public void setStatus(int arg0) { + } + + public void setStatus(int arg0, String arg1) { + } + + public void flushBuffer() throws IOException { + } + + public int getBufferSize() { + return 0; + } + + public String getCharacterEncoding() { + return null; + } + + public String getContentType() { + return null; + } + + public Locale getLocale() { + return null; + } + + public ServletOutputStream getOutputStream() throws IOException { + return null; + } + + public PrintWriter getWriter() throws IOException { + return null; + } + + public boolean isCommitted() { + return false; + } + + public void reset() { + } + + public void resetBuffer() { + } + + public void setBufferSize(int arg0) { + } + + public void setCharacterEncoding(String arg0) { + } + + public void setContentLength(int arg0) { + } + + public void setContentType(String arg0) { + } + + public void setLocale(Locale arg0) { + } + +}