crazyboblee | 3a09e29 | 2007-02-08 22:36:21 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2006 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 com.google.inject.Container; |
| 20 | import com.google.inject.ContainerBuilder; |
crazyboblee | 5746d5d | 2007-02-18 21:52:24 +0000 | [diff] [blame^] | 21 | import com.google.inject.CreationException; |
crazyboblee | 3a09e29 | 2007-02-08 22:36:21 +0000 | [diff] [blame] | 22 | import com.google.inject.Key; |
| 23 | |
| 24 | import junit.framework.TestCase; |
| 25 | |
kevinb9n | 6a565c7 | 2007-02-11 01:58:33 +0000 | [diff] [blame] | 26 | import static org.easymock.EasyMock.createMock; |
| 27 | import static org.easymock.EasyMock.eq; |
| 28 | import static org.easymock.EasyMock.expect; |
| 29 | import static org.easymock.EasyMock.isA; |
| 30 | import static org.easymock.EasyMock.replay; |
| 31 | import static org.easymock.EasyMock.verify; |
crazyboblee | 3a09e29 | 2007-02-08 22:36:21 +0000 | [diff] [blame] | 32 | |
| 33 | import java.io.IOException; |
| 34 | |
| 35 | import javax.servlet.FilterChain; |
| 36 | import javax.servlet.ServletException; |
| 37 | import javax.servlet.ServletRequest; |
| 38 | import javax.servlet.ServletResponse; |
| 39 | import javax.servlet.http.HttpServletRequest; |
crazyboblee | 4c68d34 | 2007-02-09 00:59:58 +0000 | [diff] [blame] | 40 | import javax.servlet.http.HttpSession; |
crazyboblee | 3a09e29 | 2007-02-08 22:36:21 +0000 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * @author crazybob@google.com (Bob Lee) |
| 44 | */ |
| 45 | public class ServletTest extends TestCase { |
| 46 | |
| 47 | public void testNewRequestObject() |
crazyboblee | 5746d5d | 2007-02-18 21:52:24 +0000 | [diff] [blame^] | 48 | throws CreationException, IOException, ServletException { |
crazyboblee | 4c68d34 | 2007-02-09 00:59:58 +0000 | [diff] [blame] | 49 | final Container container = createContainer(); |
crazyboblee | 3a09e29 | 2007-02-08 22:36:21 +0000 | [diff] [blame] | 50 | |
| 51 | GuiceFilter filter = new GuiceFilter(); |
| 52 | |
| 53 | final HttpServletRequest request = createMock(HttpServletRequest.class); |
| 54 | |
crazyboblee | 4c68d34 | 2007-02-09 00:59:58 +0000 | [diff] [blame] | 55 | String name = Key.get(InRequest.class).toString(); |
| 56 | expect(request.getAttribute(name)).andReturn(null); |
| 57 | request.setAttribute(eq(name), isA(InRequest.class)); |
crazyboblee | 3a09e29 | 2007-02-08 22:36:21 +0000 | [diff] [blame] | 58 | |
| 59 | final boolean[] invoked = new boolean[1]; |
| 60 | FilterChain filterChain = new FilterChain() { |
| 61 | public void doFilter(ServletRequest servletRequest, |
| 62 | ServletResponse servletResponse) { |
| 63 | invoked[0] = true; |
| 64 | assertSame(request, servletRequest); |
| 65 | assertTrue(container.getInstance(InRequest.class) instanceof InRequest); |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | replay(request); |
| 70 | |
| 71 | filter.doFilter(request, null, filterChain); |
| 72 | |
| 73 | verify(request); |
| 74 | assertTrue(invoked[0]); |
| 75 | } |
| 76 | |
crazyboblee | 4c68d34 | 2007-02-09 00:59:58 +0000 | [diff] [blame] | 77 | public void testExistingRequestObject() |
crazyboblee | 5746d5d | 2007-02-18 21:52:24 +0000 | [diff] [blame^] | 78 | throws CreationException, IOException, ServletException { |
crazyboblee | 4c68d34 | 2007-02-09 00:59:58 +0000 | [diff] [blame] | 79 | final Container container = createContainer(); |
| 80 | |
| 81 | GuiceFilter filter = new GuiceFilter(); |
| 82 | |
| 83 | final HttpServletRequest request = createMock(HttpServletRequest.class); |
| 84 | |
| 85 | final InRequest inRequest = new InRequest(); |
| 86 | String name = Key.get(InRequest.class).toString(); |
| 87 | expect(request.getAttribute(name)).andReturn(inRequest).times(2); |
| 88 | |
| 89 | final boolean[] invoked = new boolean[1]; |
| 90 | FilterChain filterChain = new FilterChain() { |
| 91 | public void doFilter(ServletRequest servletRequest, |
| 92 | ServletResponse servletResponse) { |
| 93 | invoked[0] = true; |
| 94 | assertSame(request, servletRequest); |
| 95 | assertSame(inRequest, container.getInstance(InRequest.class)); |
| 96 | assertSame(inRequest, container.getInstance(InRequest.class)); |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | replay(request); |
| 101 | |
| 102 | filter.doFilter(request, null, filterChain); |
| 103 | |
| 104 | verify(request); |
| 105 | assertTrue(invoked[0]); |
| 106 | } |
| 107 | |
| 108 | public void testNewSessionObject() |
crazyboblee | 5746d5d | 2007-02-18 21:52:24 +0000 | [diff] [blame^] | 109 | throws CreationException, IOException, ServletException { |
crazyboblee | 4c68d34 | 2007-02-09 00:59:58 +0000 | [diff] [blame] | 110 | final Container container = createContainer(); |
| 111 | |
| 112 | GuiceFilter filter = new GuiceFilter(); |
| 113 | |
| 114 | final HttpServletRequest request = createMock(HttpServletRequest.class); |
| 115 | final HttpSession session = createMock(HttpSession.class); |
| 116 | |
| 117 | String name = Key.get(InSession.class).toString(); |
| 118 | |
| 119 | expect(request.getSession()).andReturn(session); |
| 120 | expect(session.getAttribute(name)).andReturn(null); |
| 121 | session.setAttribute(eq(name), isA(InSession.class)); |
| 122 | |
| 123 | final boolean[] invoked = new boolean[1]; |
| 124 | FilterChain filterChain = new FilterChain() { |
| 125 | public void doFilter(ServletRequest servletRequest, |
| 126 | ServletResponse servletResponse) { |
| 127 | invoked[0] = true; |
| 128 | assertSame(request, servletRequest); |
| 129 | assertTrue(container.getInstance(InSession.class) instanceof InSession); |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | replay(request, session); |
| 134 | |
| 135 | filter.doFilter(request, null, filterChain); |
| 136 | |
| 137 | verify(request, session); |
| 138 | assertTrue(invoked[0]); |
| 139 | } |
| 140 | |
| 141 | public void testExistingSessionObject() |
crazyboblee | 5746d5d | 2007-02-18 21:52:24 +0000 | [diff] [blame^] | 142 | throws CreationException, IOException, ServletException { |
crazyboblee | 4c68d34 | 2007-02-09 00:59:58 +0000 | [diff] [blame] | 143 | final Container container = createContainer(); |
| 144 | |
| 145 | GuiceFilter filter = new GuiceFilter(); |
| 146 | |
| 147 | final HttpServletRequest request = createMock(HttpServletRequest.class); |
| 148 | final HttpSession session = createMock(HttpSession.class); |
| 149 | |
| 150 | String name = Key.get(InSession.class).toString(); |
| 151 | |
| 152 | final InSession inSession = new InSession(); |
| 153 | expect(request.getSession()).andReturn(session).times(2); |
| 154 | expect(session.getAttribute(name)).andReturn(inSession).times(2); |
| 155 | |
| 156 | final boolean[] invoked = new boolean[1]; |
| 157 | FilterChain filterChain = new FilterChain() { |
| 158 | public void doFilter(ServletRequest servletRequest, |
| 159 | ServletResponse servletResponse) { |
| 160 | invoked[0] = true; |
| 161 | assertSame(request, servletRequest); |
| 162 | |
| 163 | assertSame(inSession, container.getInstance(InSession.class)); |
| 164 | assertSame(inSession, container.getInstance(InSession.class)); |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | replay(request, session); |
| 169 | |
| 170 | filter.doFilter(request, null, filterChain); |
| 171 | |
| 172 | verify(request, session); |
| 173 | assertTrue(invoked[0]); |
| 174 | } |
| 175 | |
crazyboblee | 5746d5d | 2007-02-18 21:52:24 +0000 | [diff] [blame^] | 176 | private Container createContainer() throws CreationException { |
crazyboblee | 4c68d34 | 2007-02-09 00:59:58 +0000 | [diff] [blame] | 177 | ContainerBuilder builder = new ContainerBuilder(); |
| 178 | builder.install(new ServletModule()); |
| 179 | builder.bind(InSession.class); |
| 180 | builder.bind(InRequest.class); |
crazyboblee | 278ee4d | 2007-02-15 19:23:13 +0000 | [diff] [blame] | 181 | return builder.create(); |
crazyboblee | 4c68d34 | 2007-02-09 00:59:58 +0000 | [diff] [blame] | 182 | } |
| 183 | |
crazyboblee | 3a09e29 | 2007-02-08 22:36:21 +0000 | [diff] [blame] | 184 | @SessionScoped |
| 185 | static class InSession {} |
| 186 | |
| 187 | @RequestScoped |
| 188 | static class InRequest {} |
| 189 | } |