    1   /***
    2    * JFoxSOAF, Service-Oriented Application Framework
    3    *
    4    * Copyright (C) www.huihoo.org
    5    *
    6    * Distributable under GNU LGPL For more information, please visit:
    7    * http://www.huihoo.org/jfox/jfoxsoaf
    8    */
    9
    10  package org.huihoo.jfox.soaf.util.filter;
    11
    12  import java.io.IOException;
    13
    14  import javax.servlet.FilterChain;
    15  import javax.servlet.FilterConfig;
    16  import javax.servlet.ServletException;
    17  import javax.servlet.ServletRequest;
    18  import javax.servlet.ServletResponse;
    19
    20  import junit.framework.TestCase;
    21
    22  /***
    23   * JUnit test case for the
    24   * {@link org.huihoo.jfox.soaf.util.filter.EncodingFilter).
    25   *
    26   * @author <a href="mailto:founder_chen@yahoo.com">Peter Cheng </a>
    27   * @version $Revision: 1.1 $ $Date: 2004/10/25 11:25:48 $
    28   * @version Revision: 1.0
    29   */
    30
    31  public class EncodingFilterTest extends TestCase {
    32
    33      private EncodingFilter encodingFilter;
    34
    35      /***
    36       * @see TestCase#setUp()
    37       */
    38      protected void setUp() throws Exception {
    39          super.setUp();
    40      }
    41
    42      /***
    43       * @see TestCase#tearDown()
    44       */
    45      protected void tearDown() throws Exception {
    46          super.tearDown();
    47      }
    48
    49      /***
    50       * Test for method: destroy()
    51       *
    52       * @see EncodingFilter#destroy()
    53       */
    54      public void testDestroy() {
    55          EncodingFilter encodingFilter = new EncodingFilter();
    56          encodingFilter.destroy();
    57          super.assertNull(encodingFilter.getEncoding());
    58      }
    59
    60      /***
    61       * Test for method:
    62       * doFilter(javax.servlet.ServletRequest,javax.servlet.ServletResponse,javax.servlet.FilterChain)
    63       *
    64       * @see CharacterEncodingFilter#doFilter(javax.servlet.ServletRequest,javax.servlet.ServletResponse,javax.servlet.FilterChain)
    65       */
    66      public void testDoFilter() {
    67          EncodingFilter encodingFilter = new EncodingFilter();
    68          try {
    69              encodingFilter.doFilter((ServletRequest) null,
    70                      (ServletResponse) null, (FilterChain) null);
    71              // NullPointerException thrown
    72          } catch (NullPointerException e) {
    73          } catch (IOException e) {
    74              e.printStackTrace();
    75          } catch (ServletException e) {
    76              e.printStackTrace();
    77          }
    78      }
    79
    80      /***
    81       * Test for method:
    82       * doFilter(javax.servlet.ServletRequest,javax.servlet.ServletResponse,javax.servlet.FilterChain)
    83       *
    84       * @see CharacterEncodingFilter#doFilter(javax.servlet.ServletRequest,javax.servlet.ServletResponse,javax.servlet.FilterChain)
    85       */
    86      public void testDoFilter1() {
    87          EncodingFilter encodingFilter = new EncodingFilter();
    88          MockServletRequest mockServletRequest = new MockServletRequest();
    89          MockServletResponse mockServletResponse = new MockServletResponse();
    90          MockFilterChain mockServletChain = new MockFilterChain();
    91          try {
    92              encodingFilter.doFilter(mockServletRequest, mockServletResponse,
    93                      mockServletChain);
    94          } catch (IOException e) {
    95              e.printStackTrace();
    96          } catch (ServletException e) {
    97              e.printStackTrace();
    98          }
    99      }
    100
    101     /***
    102      * Test for method: init(javax.servlet.FilterConfig)
    103      *
    104      * @see EncodingFilter#init(javax.servlet.FilterConfig)
    105      */
    106     public void testInit() throws Throwable {
    107         EncodingFilter encodingFilter = new EncodingFilter();
    108         try {
    109             encodingFilter.init((FilterConfig) null);
    110             // NullPointerException thrown
    111         } catch (NullPointerException e) {
    112         }
    113     }
    114
    115     /***
    116      * Test for method: init(javax.servlet.FilterConfig)
    117      *
    118      * @see EncodingFilter#init(javax.servlet.FilterConfig)
    119      */
    120     public void testInit1() {
    121         EncodingFilter encodingFilter = new EncodingFilter();
    122         MockFilterConfig mockFilterConfig = new MockFilterConfig();
    123         mockFilterConfig.setEncoding("GB2312");
    124         mockFilterConfig.setIgnore("true");
    125         try {
    126             encodingFilter.init(mockFilterConfig);
    127         } catch (ServletException e) {
    128             e.printStackTrace();
    129         }
    130         assertEquals("GB2312", encodingFilter.getEncoding());
    131         assertTrue(encodingFilter.isIgnore());
    132     }
    133
    134     /***
    135      * Test for method: init(javax.servlet.FilterConfig)
    136      *
    137      * @see EncodingFilter#init(javax.servlet.FilterConfig)
    138      */
    139     public void testInit2() {
    140         EncodingFilter encodingFilter = new EncodingFilter();
    141         MockFilterConfig mockFilterConfig = new MockFilterConfig();
    142         mockFilterConfig.setIgnore("error");
    143         try {
    144             encodingFilter.init(mockFilterConfig);
    145         } catch (ServletException e) {
    146             e.printStackTrace();
    147         }
    148         assertFalse(encodingFilter.isIgnore());
    149     }
    150
    151     /***
    152      * Test for method: init(javax.servlet.FilterConfig)
    153      *
    154      * @see EncodingFilter#init(javax.servlet.FilterConfig)
    155      */
    156     public void testInit3() {
    157         EncodingFilter encodingFilter = new EncodingFilter();
    158         MockFilterConfig mockFilterConfig = new MockFilterConfig();
    159         try {
    160             encodingFilter.init(mockFilterConfig);
    161         } catch (ServletException e) {
    162             e.printStackTrace();
    163         }
    164         assertTrue(encodingFilter.isIgnore());
    165     }
    166
    167     /***
    168      * Test for method: selectEncoding(ServletRequest request)
    169      *
    170      * @see EncodingFilter#(ServletRequest request)
    171      */
    172     public void testSelectEncoding() {
    173         EncodingFilter encodingFilter = new EncodingFilter();
    174         MockServletRequest mockServletRequest = new MockServletRequest();
    175         assertNull(encodingFilter.selectEncoding(mockServletRequest));
    176     }
    177
    178     /***
    179      * Test for method: selectEncoding(javax.servlet.ServletRequest)
    180      *
    181      * @see EncodingFilter#selectEncoding(javax.servlet.ServletRequest)
    182      */
    183     public void testSelectEncoding1() throws Throwable {
    184         EncodingFilter encodingFilter = new EncodingFilter();
    185         String RETVAL = encodingFilter.selectEncoding((ServletRequest) null);
    186         assertEquals(null, RETVAL);
    187     }
    188
    189     /***
    190      * Test for method: getEncoding()
    191      *
    192      * @see EncodingFilter#getEncoding()
    193      */
    194     public void testGetEncoding() {
    195         EncodingFilter encodingFilter = new EncodingFilter();
    196         assertNull(encodingFilter.getEncoding());
    197     }
    198
    199     /***
    200      * Test for method: getEncoding()
    201      *
    202      * @see EncodingFilter#getEncoding()
    203      */
    204     public void testIsIgnore() {
    205         EncodingFilter encodingFilter = new EncodingFilter();
    206         assertTrue(encodingFilter.isIgnore());
    207     }
    208
    209 }