|
| 1 | +/* |
| 2 | + * Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> |
| 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 | +package org.ocpsoft.rewrite.servlet.impl; |
| 17 | + |
| 18 | +import static org.junit.Assert.assertEquals; |
| 19 | +import static org.junit.Assert.assertNull; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | + |
| 23 | +import javax.servlet.ServletContext; |
| 24 | +import javax.servlet.http.HttpServletRequest; |
| 25 | + |
| 26 | +import org.junit.Test; |
| 27 | +import org.ocpsoft.rewrite.servlet.DispatcherType; |
| 28 | +import org.ocpsoft.rewrite.servlet.spi.DispatcherTypeProvider; |
| 29 | + |
| 30 | +public class Servlet3DispatcherTypeProviderTest |
| 31 | +{ |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testShouldReturnCorrectDispatcherTypeForServlet3() |
| 35 | + { |
| 36 | + |
| 37 | + // GIVEN a Servlet 3.0 container |
| 38 | + ServletContext servletContext = mock(ServletContext.class); |
| 39 | + when(servletContext.getMajorVersion()).thenReturn(3); |
| 40 | + |
| 41 | + // AND a forwarded request |
| 42 | + HttpServletRequest request = mock(HttpServletRequest.class); |
| 43 | + when(request.getDispatcherType()).thenReturn(javax.servlet.DispatcherType.FORWARD); |
| 44 | + |
| 45 | + // WHEN the provider is asked for the DispatcherType |
| 46 | + DispatcherTypeProvider provider = new Servlet3DispatcherTypeProvider(); |
| 47 | + DispatcherType dispatcherType = provider.getDispatcherType(request, servletContext); |
| 48 | + |
| 49 | + // THEN it should return FORWARD |
| 50 | + assertEquals(DispatcherType.FORWARD, dispatcherType); |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testShouldReturnNullForServlet25() |
| 56 | + { |
| 57 | + |
| 58 | + // GIVEN a Servlet 2.5 container |
| 59 | + ServletContext servletContext = mock(ServletContext.class); |
| 60 | + when(servletContext.getMajorVersion()).thenReturn(2); |
| 61 | + HttpServletRequest request = mock(HttpServletRequest.class); |
| 62 | + when(request.getDispatcherType()).thenThrow(new IllegalStateException("Call not allowed")); |
| 63 | + |
| 64 | + // WHEN the provider is asked for the DispatcherType |
| 65 | + DispatcherTypeProvider provider = new Servlet3DispatcherTypeProvider(); |
| 66 | + DispatcherType dispatcherType = provider.getDispatcherType(request, servletContext); |
| 67 | + |
| 68 | + // THEN it should return null |
| 69 | + assertNull(dispatcherType); |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments