dhanji | ae3de7d | 2008-12-24 06:39:43 +0000 | [diff] [blame] | 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 | |
dhanji | 387abc0 | 2009-02-14 10:28:08 +0000 | [diff] [blame] | 19 | import com.google.inject.Binding; |
dhanji | ae3de7d | 2008-12-24 06:39:43 +0000 | [diff] [blame] | 20 | import com.google.inject.Injector; |
| 21 | import com.google.inject.Key; |
limpbizkit | 30384a5 | 2009-06-24 16:11:56 +0000 | [diff] [blame] | 22 | import com.google.inject.spi.BindingScopingVisitor; |
sberlin | d9c913a | 2011-06-26 21:02:54 +0000 | [diff] [blame^] | 23 | import com.google.common.collect.Maps; |
| 24 | import com.google.common.collect.Sets; |
dhanji | ae3de7d | 2008-12-24 06:39:43 +0000 | [diff] [blame] | 25 | import java.util.Enumeration; |
| 26 | import java.util.HashMap; |
| 27 | import java.util.Map; |
| 28 | import javax.servlet.ServletConfig; |
| 29 | import javax.servlet.ServletContext; |
| 30 | import javax.servlet.ServletException; |
| 31 | import javax.servlet.http.HttpServlet; |
dhanji | 387abc0 | 2009-02-14 10:28:08 +0000 | [diff] [blame] | 32 | import junit.framework.TestCase; |
| 33 | import static org.easymock.EasyMock.createMock; |
| 34 | import static org.easymock.EasyMock.expect; |
| 35 | import static org.easymock.EasyMock.replay; |
limpbizkit | 30384a5 | 2009-06-24 16:11:56 +0000 | [diff] [blame] | 36 | import static org.easymock.EasyMock.anyObject; |
sberlin | 272ba99 | 2010-07-21 02:48:53 +0000 | [diff] [blame] | 37 | import static org.easymock.EasyMock.verify; |
dhanji | ae3de7d | 2008-12-24 06:39:43 +0000 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Basic unit test for lifecycle of a ServletDefinition (wrapper). |
| 41 | * |
| 42 | * @author Dhanji R. Prasanna (dhanji@gmail com) |
| 43 | */ |
| 44 | public class ServletDefinitionTest extends TestCase { |
| 45 | |
| 46 | public final void testServletInitAndConfig() throws ServletException { |
| 47 | Injector injector = createMock(Injector.class); |
limpbizkit | 30384a5 | 2009-06-24 16:11:56 +0000 | [diff] [blame] | 48 | Binding binding = createMock(Binding.class); |
dhanji | ae3de7d | 2008-12-24 06:39:43 +0000 | [diff] [blame] | 49 | |
limpbizkit | 30384a5 | 2009-06-24 16:11:56 +0000 | [diff] [blame] | 50 | expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())) |
| 51 | .andReturn(true); |
dhanji | 1bedfac | 2009-01-06 12:48:44 +0000 | [diff] [blame] | 52 | expect(injector.getBinding(Key.get(HttpServlet.class))) |
limpbizkit | 30384a5 | 2009-06-24 16:11:56 +0000 | [diff] [blame] | 53 | .andReturn(binding); |
dhanji | ae3de7d | 2008-12-24 06:39:43 +0000 | [diff] [blame] | 54 | final HttpServlet mockServlet = new HttpServlet() { |
| 55 | }; |
| 56 | expect(injector.getInstance(Key.get(HttpServlet.class))) |
| 57 | .andReturn(mockServlet) |
| 58 | .anyTimes(); |
| 59 | |
limpbizkit | 30384a5 | 2009-06-24 16:11:56 +0000 | [diff] [blame] | 60 | replay(injector, binding); |
dhanji | ae3de7d | 2008-12-24 06:39:43 +0000 | [diff] [blame] | 61 | |
| 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 | |
dhanji | b7ed1bb | 2008-12-30 06:32:41 +0000 | [diff] [blame] | 74 | String pattern = "/*"; |
| 75 | final ServletDefinition servletDefinition = new ServletDefinition(pattern, |
sberlin | b4b7f72 | 2010-08-22 18:48:23 +0000 | [diff] [blame] | 76 | Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.SERVLET, pattern), initParams, null); |
dhanji | ae3de7d | 2008-12-24 06:39:43 +0000 | [diff] [blame] | 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 | |
limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 85 | servletDefinition.init(servletContext, injector, |
| 86 | Sets.newSetFromMap(Maps.<HttpServlet, Boolean>newIdentityHashMap())); |
dhanji | ae3de7d | 2008-12-24 06:39:43 +0000 | [diff] [blame] | 87 | |
| 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 | } |
sberlin | 272ba99 | 2010-07-21 02:48:53 +0000 | [diff] [blame] | 100 | |
| 101 | verify(injector, binding, servletContext); |
dhanji | ae3de7d | 2008-12-24 06:39:43 +0000 | [diff] [blame] | 102 | } |
| 103 | } |