1/** 2 * Copyright (C) 2008 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.google.inject.servlet; 18 19import static org.easymock.EasyMock.anyObject; 20import static org.easymock.EasyMock.createMock; 21import static org.easymock.EasyMock.expect; 22import static org.easymock.EasyMock.replay; 23import static org.easymock.EasyMock.verify; 24 25import com.google.common.collect.ImmutableMap; 26import com.google.common.collect.Sets; 27import com.google.inject.Binding; 28import com.google.inject.Injector; 29import com.google.inject.Key; 30import com.google.inject.spi.BindingScopingVisitor; 31 32import junit.framework.TestCase; 33 34import java.io.IOException; 35import java.util.Enumeration; 36import java.util.Map; 37 38import javax.servlet.ServletConfig; 39import javax.servlet.ServletContext; 40import javax.servlet.ServletException; 41import javax.servlet.http.HttpServlet; 42import javax.servlet.http.HttpServletRequest; 43import javax.servlet.http.HttpServletResponse; 44 45/** 46 * Basic unit test for lifecycle of a ServletDefinition (wrapper). 47 * 48 * @author Dhanji R. Prasanna (dhanji@gmail com) 49 */ 50public class ServletDefinitionTest extends TestCase { 51 52 public final void testServletInitAndConfig() throws ServletException { 53 Injector injector = createMock(Injector.class); 54 Binding binding = createMock(Binding.class); 55 56 expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())) 57 .andReturn(true); 58 expect(injector.getBinding(Key.get(HttpServlet.class))) 59 .andReturn(binding); 60 final HttpServlet mockServlet = new HttpServlet() { 61 }; 62 expect(injector.getInstance(Key.get(HttpServlet.class))) 63 .andReturn(mockServlet) 64 .anyTimes(); 65 66 replay(injector, binding); 67 68 //some init params 69 //noinspection SSBasedInspection 70 final Map<String, String> initParams = new ImmutableMap.Builder<String, String>() 71 .put("ahsd", "asdas24dok") 72 .put("ahssd", "asdasd124ok").build(); 73 74 String pattern = "/*"; 75 final ServletDefinition servletDefinition = new ServletDefinition(pattern, 76 Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.SERVLET, pattern), initParams, null); 77 78 ServletContext servletContext = createMock(ServletContext.class); 79 final String contextName = "thing__!@@44__SRV" + getClass(); 80 expect(servletContext.getServletContextName()) 81 .andReturn(contextName); 82 83 replay(servletContext); 84 85 servletDefinition.init(servletContext, injector, Sets.<HttpServlet>newIdentityHashSet()); 86 87 assertNotNull(mockServlet.getServletContext()); 88 assertEquals(contextName, mockServlet.getServletContext().getServletContextName()); 89 assertEquals(Key.get(HttpServlet.class).toString(), mockServlet.getServletName()); 90 91 final ServletConfig servletConfig = mockServlet.getServletConfig(); 92 final Enumeration names = servletConfig.getInitParameterNames(); 93 while (names.hasMoreElements()) { 94 String name = (String) names.nextElement(); 95 96 assertTrue(initParams.containsKey(name)); 97 assertEquals(initParams.get(name), servletConfig.getInitParameter(name)); 98 } 99 100 verify(injector, binding, servletContext); 101 } 102 103 public void testServiceWithContextPath() throws IOException, ServletException { 104 String pattern = "/*"; 105 //some init params 106 Map<String, String> initParams = new ImmutableMap.Builder<String, String>() 107 .put("ahsd", "asdas24dok") 108 .put("ahssd", "asdasd124ok") 109 .build(); 110 111 final ServletDefinition servletDefinition = new ServletDefinition(pattern, 112 Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.SERVLET, pattern), 113 initParams, null); 114 HttpServletResponse servletResponse = createMock(HttpServletResponse.class); 115 HttpServletRequest servletRequest = createMock(HttpServletRequest.class); 116 117 expect(servletRequest.getContextPath()).andReturn("/a_context_path"); 118 expect(servletRequest.getRequestURI()).andReturn("/test.html"); 119 replay(servletRequest, servletResponse); 120 servletDefinition.service(servletRequest, servletResponse); 121 verify(servletRequest, servletResponse); 122 } 123} 124