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 
17 package com.google.inject.servlet;
18 
19 import static org.easymock.EasyMock.anyObject;
20 import static org.easymock.EasyMock.createMock;
21 import static org.easymock.EasyMock.expect;
22 import static org.easymock.EasyMock.replay;
23 import static org.easymock.EasyMock.verify;
24 
25 import com.google.common.collect.ImmutableMap;
26 import com.google.common.collect.Sets;
27 import com.google.inject.Binding;
28 import com.google.inject.Injector;
29 import com.google.inject.Key;
30 import com.google.inject.spi.BindingScopingVisitor;
31 import java.io.IOException;
32 import java.util.Enumeration;
33 import java.util.Map;
34 import javax.servlet.ServletConfig;
35 import javax.servlet.ServletContext;
36 import javax.servlet.ServletException;
37 import javax.servlet.http.HttpServlet;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40 import junit.framework.TestCase;
41 
42 /**
43  * Basic unit test for lifecycle of a ServletDefinition (wrapper).
44  *
45  * @author Dhanji R. Prasanna (dhanji@gmail com)
46  */
47 public class ServletDefinitionTest extends TestCase {
48 
testServletInitAndConfig()49   public final void testServletInitAndConfig() throws ServletException {
50     Injector injector = createMock(Injector.class);
51     Binding binding = createMock(Binding.class);
52 
53     expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
54     expect(injector.getBinding(Key.get(HttpServlet.class))).andReturn(binding);
55     final HttpServlet mockServlet = new HttpServlet() {};
56     expect(injector.getInstance(Key.get(HttpServlet.class))).andReturn(mockServlet).anyTimes();
57 
58     replay(injector, binding);
59 
60     //some init params
61     //noinspection SSBasedInspection
62     final Map<String, String> initParams =
63         new ImmutableMap.Builder<String, String>()
64             .put("ahsd", "asdas24dok")
65             .put("ahssd", "asdasd124ok")
66             .build();
67 
68     String pattern = "/*";
69     final ServletDefinition servletDefinition =
70         new ServletDefinition(
71             Key.get(HttpServlet.class),
72             UriPatternType.get(UriPatternType.SERVLET, pattern),
73             initParams,
74             null);
75 
76     ServletContext servletContext = createMock(ServletContext.class);
77     final String contextName = "thing__!@@44__SRV" + getClass();
78     expect(servletContext.getServletContextName()).andReturn(contextName);
79 
80     replay(servletContext);
81 
82     servletDefinition.init(servletContext, injector, Sets.<HttpServlet>newIdentityHashSet());
83 
84     assertNotNull(mockServlet.getServletContext());
85     assertEquals(contextName, mockServlet.getServletContext().getServletContextName());
86     assertEquals(Key.get(HttpServlet.class).toString(), mockServlet.getServletName());
87 
88     final ServletConfig servletConfig = mockServlet.getServletConfig();
89     final Enumeration names = servletConfig.getInitParameterNames();
90     while (names.hasMoreElements()) {
91       String name = (String) names.nextElement();
92 
93       assertTrue(initParams.containsKey(name));
94       assertEquals(initParams.get(name), servletConfig.getInitParameter(name));
95     }
96 
97     verify(injector, binding, servletContext);
98   }
99 
testServiceWithContextPath()100   public void testServiceWithContextPath() throws IOException, ServletException {
101     String pattern = "/*";
102     //some init params
103     Map<String, String> initParams =
104         new ImmutableMap.Builder<String, String>()
105             .put("ahsd", "asdas24dok")
106             .put("ahssd", "asdasd124ok")
107             .build();
108 
109     final ServletDefinition servletDefinition =
110         new ServletDefinition(
111             Key.get(HttpServlet.class),
112             UriPatternType.get(UriPatternType.SERVLET, pattern),
113             initParams,
114             null);
115     HttpServletResponse servletResponse = createMock(HttpServletResponse.class);
116     HttpServletRequest servletRequest = createMock(HttpServletRequest.class);
117 
118     expect(servletRequest.getContextPath()).andReturn("/a_context_path");
119     expect(servletRequest.getRequestURI()).andReturn("/test.html");
120     replay(servletRequest, servletResponse);
121     servletDefinition.service(servletRequest, servletResponse);
122     verify(servletRequest, servletResponse);
123   }
124 }
125