    1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
    2    *
    3    * Licensed under the Apache License, Version 2.0 (the "License");
    4    * you may not use this file except in compliance with the License.
    5    * You may obtain a copy of the License at
    6    *
    7    *     http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15
    16  package org.acegisecurity.intercept.web;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.acegisecurity.MockFilterChain;
    21
    22  import java.io.BufferedReader;
    23  import java.io.IOException;
    24  import java.io.PrintWriter;
    25  import java.io.UnsupportedEncodingException;
    26
    27  import java.util.Enumeration;
    28  import java.util.Locale;
    29  import java.util.Map;
    30
    31  import javax.servlet.RequestDispatcher;
    32  import javax.servlet.ServletInputStream;
    33  import javax.servlet.ServletOutputStream;
    34  import javax.servlet.ServletRequest;
    35  import javax.servlet.ServletResponse;
    36
    37  import org.springframework.mock.web.MockHttpServletRequest;
    38  import org.springframework.mock.web.MockHttpServletResponse;
    39
    40
    41  /***
    42   * Tests {@link FilterInvocation}.
    43   *
    44   * @author Ben Alex
    45   * @author colin sampaleanu
    46   * @version $Id: FilterInvocationTests.java,v 1.7 2005/11/17 00:55:50 benalex Exp $
    47   */
    48  public class FilterInvocationTests extends TestCase {
    49      //~ Constructors ===========================================================
    50
    51      public FilterInvocationTests() {
    52          super();
    53      }
    54
    55      public FilterInvocationTests(String arg0) {
    56          super(arg0);
    57      }
    58
    59      //~ Methods ================================================================
    60
    61      public final void setUp() throws Exception {
    62          super.setUp();
    63      }
    64
    65      public static void main(String[] args) {
    66          junit.textui.TestRunner.run(FilterInvocationTests.class);
    67      }
    68
    69      public void testGettersAndStringMethods() {
    70          MockHttpServletRequest request = new MockHttpServletRequest(null, null);
    71          request.setServletPath("/HelloWorld");
    72          request.setPathInfo("/some/more/segments.html");
    73          request.setServerName("www.example.com");
    74          request.setScheme("http");
    75          request.setServerPort(80);
    76          request.setContextPath("/mycontext");
    77          request.setRequestURI("/mycontext/HelloWorld/some/more/segments.html");
    78
    79          MockHttpServletResponse response = new MockHttpServletResponse();
    80          MockFilterChain chain = new MockFilterChain();
    81          FilterInvocation fi = new FilterInvocation(request, response, chain);
    82          assertEquals(request, fi.getRequest());
    83          assertEquals(request, fi.getHttpRequest());
    84          assertEquals(response, fi.getResponse());
    85          assertEquals(response, fi.getHttpResponse());
    86          assertEquals(chain, fi.getChain());
    87          assertEquals("/HelloWorld/some/more/segments.html", fi.getRequestUrl());
    88          assertEquals("FilterInvocation: URL: /HelloWorld/some/more/segments.html",
    89              fi.toString());
    90          assertEquals("http://www.example.com:80/mycontext/HelloWorld/some/more/segments.html",
    91              fi.getFullRequestUrl());
    92      }
    93
    94      public void testNoArgsConstructor() {
    95          try {
    96              new FilterInvocation();
    97              fail("Should have thrown IllegalArgumentException");
    98          } catch (IllegalArgumentException expected) {
    99              assertTrue(true);
    100         }
    101     }
    102
    103     public void testRejectsNullFilterChain() {
    104         MockHttpServletRequest request = new MockHttpServletRequest(null, null);
    105         MockHttpServletResponse response = new MockHttpServletResponse();
    106
    107         try {
    108             new FilterInvocation(request, response, null);
    109             fail("Should have thrown IllegalArgumentException");
    110         } catch (IllegalArgumentException expected) {
    111             assertTrue(true);
    112         }
    113     }
    114
    115     public void testRejectsNullServletRequest() {
    116         MockHttpServletResponse response = new MockHttpServletResponse();
    117         MockFilterChain chain = new MockFilterChain();
    118
    119         try {
    120             new FilterInvocation(null, response, chain);
    121             fail("Should have thrown IllegalArgumentException");
    122         } catch (IllegalArgumentException expected) {
    123             assertTrue(true);
    124         }
    125     }
    126
    127     public void testRejectsNullServletResponse() {
    128         MockHttpServletRequest request = new MockHttpServletRequest(null, null);
    129         MockFilterChain chain = new MockFilterChain();
    130
    131         try {
    132             new FilterInvocation(request, null, chain);
    133             fail("Should have thrown IllegalArgumentException");
    134         } catch (IllegalArgumentException expected) {
    135             assertTrue(true);
    136         }
    137     }
    138
    139     public void testRejectsServletRequestWhichIsNotHttpServletRequest() {
    140         MockServletRequest request = new MockServletRequest();
    141         MockHttpServletResponse response = new MockHttpServletResponse();
    142         MockFilterChain chain = new MockFilterChain();
    143
    144         try {
    145             new FilterInvocation(request, response, chain);
    146             fail("Should have thrown IllegalArgumentException");
    147         } catch (IllegalArgumentException expected) {
    148             assertEquals("Can only process HttpServletRequest",
    149                 expected.getMessage());
    150         }
    151     }
    152
    153     public void testRejectsServletResponseWhichIsNotHttpServletResponse() {
    154         MockHttpServletRequest request = new MockHttpServletRequest(null, null);
    155         MockServletResponse response = new MockServletResponse();
    156         MockFilterChain chain = new MockFilterChain();
    157
    158         try {
    159             new FilterInvocation(request, response, chain);
    160             fail("Should have thrown IllegalArgumentException");
    161         } catch (IllegalArgumentException expected) {
    162             assertEquals("Can only process HttpServletResponse",
    163                 expected.getMessage());
    164         }
    165     }
    166
    167     public void testStringMethodsWithAQueryString() {
    168         MockHttpServletRequest request = new MockHttpServletRequest();
    169         request.setQueryString("foo=bar");
    170         request.setServletPath("/HelloWorld");
    171         request.setServerName("www.example.com");
    172         request.setScheme("http");
    173         request.setServerPort(80);
    174         request.setContextPath("/mycontext");
    175         request.setRequestURI("/mycontext/HelloWorld");
    176
    177         MockHttpServletResponse response = new MockHttpServletResponse();
    178         MockFilterChain chain = new MockFilterChain();
    179         FilterInvocation fi = new FilterInvocation(request, response, chain);
    180         assertEquals("/HelloWorld?foo=bar", fi.getRequestUrl());
    181         assertEquals("FilterInvocation: URL: /HelloWorld?foo=bar", fi.toString());
    182         assertEquals("http://www.example.com:80/mycontext/HelloWorld?foo=bar",
    183             fi.getFullRequestUrl());
    184     }
    185
    186     public void testStringMethodsWithoutAnyQueryString() {
    187         MockHttpServletRequest request = new MockHttpServletRequest(null, null);
    188         request.setServletPath("/HelloWorld");
    189         request.setServerName("www.example.com");
    190         request.setScheme("http");
    191         request.setServerPort(80);
    192         request.setContextPath("/mycontext");
    193         request.setRequestURI("/mycontext/HelloWorld");
    194
    195         MockHttpServletResponse response = new MockHttpServletResponse();
    196         MockFilterChain chain = new MockFilterChain();
    197         FilterInvocation fi = new FilterInvocation(request, response, chain);
    198         assertEquals("/HelloWorld", fi.getRequestUrl());
    199         assertEquals("FilterInvocation: URL: /HelloWorld", fi.toString());
    200         assertEquals("http://www.example.com:80/mycontext/HelloWorld",
    201             fi.getFullRequestUrl());
    202     }
    203
    204     //~ Inner Classes ==========================================================
    205
    206     private class MockServletRequest implements ServletRequest {
    207         public void setAttribute(String arg0, Object arg1) {
    208             throw new UnsupportedOperationException(
    209                 "mock method not implemented");
    210         }
    211
    212         public Object getAttribute(String arg0) {
    213             throw new UnsupportedOperationException(
    214                 "mock method not implemented");
    215         }
    216
    217         public Enumeration getAttributeNames() {
    218             throw new UnsupportedOperationException(
    219                 "mock method not implemented");
    220         }
    221
    222         public void setCharacterEncoding(String arg0)
    223             throws UnsupportedEncodingException {
    224             throw new UnsupportedOperationException(
    225                 "mock method not implemented");
    226         }
    227
    228         public String getCharacterEncoding() {
    229             throw new UnsupportedOperationException(
    230                 "mock method not implemented");
    231         }
    232
    233         public int getContentLength() {
    234             throw new UnsupportedOperationException(
    235                 "mock method not implemented");
    236         }
    237
    238         public String getContentType() {
    239             throw new UnsupportedOperationException(
    240                 "mock method not implemented");
    241         }
    242
    243         public ServletInputStream getInputStream() throws IOException {
    244             throw new UnsupportedOperationException(
    245                 "mock method not implemented");
    246         }
    247
    248         public String getLocalAddr() {
    249             throw new UnsupportedOperationException(
    250                 "mock method not implemented");
    251         }
    252
    253         public String getLocalName() {
    254             throw new UnsupportedOperationException(
    255                 "mock method not implemented");
    256         }
    257
    258         public int getLocalPort() {
    259             throw new UnsupportedOperationException(
    260                 "mock method not implemented");
    261         }
    262
    263         public Locale getLocale() {
    264             throw new UnsupportedOperationException(
    265                 "mock method not implemented");
    266         }
    267
    268         public Enumeration getLocales() {
    269             throw new UnsupportedOperationException(
    270                 "mock method not implemented");
    271         }
    272
    273         public String getParameter(String arg0) {
    274             throw new UnsupportedOperationException(
    275                 "mock method not implemented");
    276         }
    277
    278         public Map getParameterMap() {
    279             throw new UnsupportedOperationException(
    280                 "mock method not implemented");
    281         }
    282
    283         public Enumeration getParameterNames() {
    284             throw new UnsupportedOperationException(
    285                 "mock method not implemented");
    286         }
    287
    288         public String[] getParameterValues(String arg0) {
    289             throw new UnsupportedOperationException(
    290                 "mock method not implemented");
    291         }
    292
    293         public String getProtocol() {
    294             throw new UnsupportedOperationException(
    295                 "mock method not implemented");
    296         }
    297
    298         public BufferedReader getReader() throws IOException {
    299             throw new UnsupportedOperationException(
    300                 "mock method not implemented");
    301         }
    302
    303         public String getRealPath(String arg0) {
    304             throw new UnsupportedOperationException(
    305                 "mock method not implemented");
    306         }
    307
    308         public String getRemoteAddr() {
    309             throw new UnsupportedOperationException(
    310                 "mock method not implemented");
    311         }
    312
    313         public String getRemoteHost() {
    314             throw new UnsupportedOperationException(
    315                 "mock method not implemented");
    316         }
    317
    318         public int getRemotePort() {
    319             throw new UnsupportedOperationException(
    320                 "mock method not implemented");
    321         }
    322
    323         public RequestDispatcher getRequestDispatcher(String arg0) {
    324             throw new UnsupportedOperationException(
    325                 "mock method not implemented");
    326         }
    327
    328         public String getScheme() {
    329             throw new UnsupportedOperationException(
    330                 "mock method not implemented");
    331         }
    332
    333         public boolean isSecure() {
    334             throw new UnsupportedOperationException(
    335                 "mock method not implemented");
    336         }
    337
    338         public String getServerName() {
    339             throw new UnsupportedOperationException(
    340                 "mock method not implemented");
    341         }
    342
    343         public int getServerPort() {
    344             throw new UnsupportedOperationException(
    345                 "mock method not implemented");
    346         }
    347
    348         public void removeAttribute(String arg0) {
    349             throw new UnsupportedOperationException(
    350                 "mock method not implemented");
    351         }
    352     }
    353
    354     private class MockServletResponse implements ServletResponse {
    355         public void setBufferSize(int arg0) {
    356             throw new UnsupportedOperationException(
    357                 "mock method not implemented");
    358         }
    359
    360         public int getBufferSize() {
    361             throw new UnsupportedOperationException(
    362                 "mock method not implemented");
    363         }
    364
    365         public void setCharacterEncoding(String arg0) {
    366             throw new UnsupportedOperationException(
    367                 "mock method not implemented");
    368         }
    369
    370         public String getCharacterEncoding() {
    371             throw new UnsupportedOperationException(
    372                 "mock method not implemented");
    373         }
    374
    375         public boolean isCommitted() {
    376             throw new UnsupportedOperationException(
    377                 "mock method not implemented");
    378         }
    379
    380         public void setContentLength(int arg0) {
    381             throw new UnsupportedOperationException(
    382                 "mock method not implemented");
    383         }
    384
    385         public void setContentType(String arg0) {
    386             throw new UnsupportedOperationException(
    387                 "mock method not implemented");
    388         }
    389
    390         public String getContentType() {
    391             throw new UnsupportedOperationException(
    392                 "mock method not implemented");
    393         }
    394
    395         public void setLocale(Locale arg0) {
    396             throw new UnsupportedOperationException(
    397                 "mock method not implemented");
    398         }
    399
    400         public Locale getLocale() {
    401             throw new UnsupportedOperationException(
    402                 "mock method not implemented");
    403         }
    404
    405         public ServletOutputStream getOutputStream() throws IOException {
    406             throw new UnsupportedOperationException(
    407                 "mock method not implemented");
    408         }
    409
    410         public PrintWriter getWriter() throws IOException {
    411             throw new UnsupportedOperationException(
    412                 "mock method not implemented");
    413         }
    414
    415         public void flushBuffer() throws IOException {
    416             throw new UnsupportedOperationException(
    417                 "mock method not implemented");
    418         }
    419
    420         public void reset() {
    421             throw new UnsupportedOperationException(
    422                 "mock method not implemented");
    423         }
    424
    425         public void resetBuffer() {
    426             throw new UnsupportedOperationException(
    427                 "mock method not implemented");
    428         }
    429     }
    430 }