1 // Copyright 2012 Google Inc. All Rights Reserved. 2 3 package com.google.inject.servlet; 4 import static org.easymock.EasyMock.createMock; 5 import static org.easymock.EasyMock.expect; 6 import static org.easymock.EasyMock.replay; 7 import static org.easymock.EasyMock.verify; 8 9 import junit.framework.TestCase; 10 11 import javax.servlet.http.HttpServletRequest; 12 13 /** 14 * Unit test for the servlet utility class. 15 * 16 * @author ntang (at) google.com (Michael Tang) 17 */ 18 public class ServletUtilsTest extends TestCase { 19 public void testGetContextRelativePath() { 20 HttpServletRequest servletRequest = createMock(HttpServletRequest.class); 21 expect(servletRequest.getContextPath()).andReturn("/a_context_path"); 22 expect(servletRequest.getRequestURI()).andReturn("/a_context_path/test.html"); 23 replay(servletRequest); 24 String path = ServletUtils.getContextRelativePath(servletRequest); 25 assertEquals("/test.html", path); 26 verify(servletRequest); 27 } 28 29 public void testGetContextRelativePathWithWrongPath() { 30 HttpServletRequest servletRequest = createMock(HttpServletRequest.class); 31 expect(servletRequest.getContextPath()).andReturn("/a_context_path"); 32 expect(servletRequest.getRequestURI()).andReturn("/test.html"); 33 replay(servletRequest); 34 String path = ServletUtils.getContextRelativePath(servletRequest); 35 assertNull(path); 36 verify(servletRequest); 37 } 38 39 public void testGetContextRelativePathWithRootPath() { 40 HttpServletRequest servletRequest = createMock(HttpServletRequest.class); 41 expect(servletRequest.getContextPath()).andReturn("/a_context_path"); 42 expect(servletRequest.getRequestURI()).andReturn("/a_context_path"); 43 replay(servletRequest); 44 String path = ServletUtils.getContextRelativePath(servletRequest); 45 assertEquals("/", path); 46 verify(servletRequest); 47 } 48 49 public void testGetContextRelativePathWithEmptyPath() { 50 HttpServletRequest servletRequest = createMock(HttpServletRequest.class); 51 expect(servletRequest.getContextPath()).andReturn(""); 52 expect(servletRequest.getRequestURI()).andReturn(""); 53 replay(servletRequest); 54 String path = ServletUtils.getContextRelativePath(servletRequest); 55 assertNull(path); 56 verify(servletRequest); 57 } 58 } 59