blob: e201f87aadfd6e9205efd8b53535a3c0158a807b [file] [log] [blame]
dhanjiae3de7d2008-12-24 06:39:43 +00001/**
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
dhanji387abc02009-02-14 10:28:08 +000019import com.google.inject.Binding;
dhanjiae3de7d2008-12-24 06:39:43 +000020import com.google.inject.Injector;
21import com.google.inject.Key;
limpbizkit30384a52009-06-24 16:11:56 +000022import com.google.inject.spi.BindingScopingVisitor;
sberlind9c913a2011-06-26 21:02:54 +000023import com.google.common.collect.Maps;
24import com.google.common.collect.Sets;
dhanjiae3de7d2008-12-24 06:39:43 +000025import java.util.Enumeration;
26import java.util.HashMap;
27import java.util.Map;
28import javax.servlet.ServletConfig;
29import javax.servlet.ServletContext;
30import javax.servlet.ServletException;
31import javax.servlet.http.HttpServlet;
dhanji387abc02009-02-14 10:28:08 +000032import junit.framework.TestCase;
33import static org.easymock.EasyMock.createMock;
34import static org.easymock.EasyMock.expect;
35import static org.easymock.EasyMock.replay;
limpbizkit30384a52009-06-24 16:11:56 +000036import static org.easymock.EasyMock.anyObject;
sberlin272ba992010-07-21 02:48:53 +000037import static org.easymock.EasyMock.verify;
dhanjiae3de7d2008-12-24 06:39:43 +000038
39/**
40 * Basic unit test for lifecycle of a ServletDefinition (wrapper).
41 *
42 * @author Dhanji R. Prasanna (dhanji@gmail com)
43 */
44public class ServletDefinitionTest extends TestCase {
45
46 public final void testServletInitAndConfig() throws ServletException {
47 Injector injector = createMock(Injector.class);
limpbizkit30384a52009-06-24 16:11:56 +000048 Binding binding = createMock(Binding.class);
dhanjiae3de7d2008-12-24 06:39:43 +000049
limpbizkit30384a52009-06-24 16:11:56 +000050 expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject()))
51 .andReturn(true);
dhanji1bedfac2009-01-06 12:48:44 +000052 expect(injector.getBinding(Key.get(HttpServlet.class)))
limpbizkit30384a52009-06-24 16:11:56 +000053 .andReturn(binding);
dhanjiae3de7d2008-12-24 06:39:43 +000054 final HttpServlet mockServlet = new HttpServlet() {
55 };
56 expect(injector.getInstance(Key.get(HttpServlet.class)))
57 .andReturn(mockServlet)
58 .anyTimes();
59
limpbizkit30384a52009-06-24 16:11:56 +000060 replay(injector, binding);
dhanjiae3de7d2008-12-24 06:39:43 +000061
62 //some init params
63 //noinspection SSBasedInspection
64 final Map<String, String> initParams = new HashMap<String, String>() {
65 {
66 put("ahsd", "asdas24dok");
67 put("ahssd", "asdasd124ok");
68 put("ahfsasd", "asda124sdok");
69 put("ahsasgd", "a124sdasdok");
70 put("ahsd124124", "as124124124dasdok");
71 }
72 };
73
dhanjib7ed1bb2008-12-30 06:32:41 +000074 String pattern = "/*";
75 final ServletDefinition servletDefinition = new ServletDefinition(pattern,
sberlinb4b7f722010-08-22 18:48:23 +000076 Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.SERVLET, pattern), initParams, null);
dhanjiae3de7d2008-12-24 06:39:43 +000077
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
limpbizkit53664a72009-02-21 00:25:27 +000085 servletDefinition.init(servletContext, injector,
86 Sets.newSetFromMap(Maps.<HttpServlet, Boolean>newIdentityHashMap()));
dhanjiae3de7d2008-12-24 06:39:43 +000087
88 assertNotNull(mockServlet.getServletContext());
89 assertEquals(contextName, mockServlet.getServletContext().getServletContextName());
90 assertEquals(Key.get(HttpServlet.class).toString(), mockServlet.getServletName());
91
92 final ServletConfig servletConfig = mockServlet.getServletConfig();
93 final Enumeration names = servletConfig.getInitParameterNames();
94 while (names.hasMoreElements()) {
95 String name = (String) names.nextElement();
96
97 assertTrue(initParams.containsKey(name));
98 assertEquals(initParams.get(name), servletConfig.getInitParameter(name));
99 }
sberlin272ba992010-07-21 02:48:53 +0000100
101 verify(injector, binding, servletContext);
dhanjiae3de7d2008-12-24 06:39:43 +0000102 }
103}