    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20  package org.apache.mina.filter;
    21
    22  import org.apache.mina.common.ByteBuffer;
    23  import org.apache.mina.common.IoFilterChain;
    24  import org.apache.mina.common.IoSession;
    25  import org.apache.mina.common.IoFilter.NextFilter;
    26  import org.apache.mina.filter.support.Zlib;
    27  import org.apache.mina.common.IoFilter.WriteRequest;
    28  import org.easymock.MockControl;
    29  import org.easymock.AbstractMatcher;
    30
    31  import junit.framework.TestCase;
    32
    33  /**
    34   * @author The Apache Directory Project (mina-dev@directory.apache.org)
    35   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $
    36   */
    37  public class CompressionFilterTest extends TestCase {
    38      private MockControl mockSession;
    39
    40      private MockControl mockNextFilter;
    41
    42      private MockControl mockIoFilterChain;
    43
    44      private IoSession session;
    45
    46      private NextFilter nextFilter;
    47
    48      private IoFilterChain ioFilterChain;
    49
    50      private CompressionFilter filter;
    51
    52      private Zlib deflater;
    53
    54      private Zlib inflater;
    55
    56      private Zlib actualDeflater;
    57
    58      private Zlib actualInflater;
    59
    60      // the sample data to be used for testing
    61      String strCompress = "The quick brown fox jumps over the lazy dog.  "
    62              + "The quick brown fox jumps over the lazy dog.  "
    63              + "The quick brown fox jumps over the lazy dog.  "
    64              + "The quick brown fox jumps over the lazy dog.  "
    65              + "The quick brown fox jumps over the lazy dog.  "
    66              + "The quick brown fox jumps over the lazy dog.  "
    67              + "The quick brown fox jumps over the lazy dog.  "
    68              + "The quick brown fox jumps over the lazy dog.  "
    69              + "The quick brown fox jumps over the lazy dog.  "
    70              + "The quick brown fox jumps over the lazy dog.  "
    71              + "The quick brown fox jumps over the lazy dog.  "
    72              + "The quick brown fox jumps over the lazy dog.  "
    73              + "The quick brown fox jumps over the lazy dog.  "
    74              + "The quick brown fox jumps over the lazy dog.  "
    75              + "The quick brown fox jumps over the lazy dog.  "
    76              + "The quick brown fox jumps over the lazy dog.  "
    77              + "The quick brown fox jumps over the lazy dog.  "
    78              + "The quick brown fox jumps over the lazy dog.  "
    79              + "The quick brown fox jumps over the lazy dog.  "
    80              + "The quick brown fox jumps over the lazy dog.  "
    81              + "The quick brown fox jumps over the lazy dog.  "
    82              + "The quick brown fox jumps over the lazy dog.  "
    83              + "The quick brown fox jumps over the lazy dog.  "
    84              + "The quick brown fox jumps over the lazy dog.  "
    85              + "The quick brown fox jumps over the lazy dog.  ";
    86
    87      protected void setUp() {
    88          // create the necessary mock controls.
    89          mockSession = MockControl.createControl(IoSession.class);
    90          mockNextFilter = MockControl.createControl(NextFilter.class);
    91          mockIoFilterChain = MockControl.createControl(IoFilterChain.class);
    92
    93          // set the default matcher
    94          mockNextFilter.setDefaultMatcher(new DataMatcher());
    95
    96          session = (IoSession) mockSession.getMock();
    97          nextFilter = (NextFilter) mockNextFilter.getMock();
    98          ioFilterChain = (IoFilterChain) mockIoFilterChain.getMock();
    99
    100         // create an instance of the filter
    101         filter = new CompressionFilter(CompressionFilter.COMPRESSION_MAX);
    102
    103         // deflater and inflater that will be used by the filter
    104         deflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_DEFLATER);
    105         inflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_INFLATER);
    106
    107         // create instances of the deflater and inflater to help test the output
    108         actualDeflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_DEFLATER);
    109         actualInflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_INFLATER);
    110     }
    111
    112     public void testCompression() throws Exception {
    113         // prepare the input data
    114         ByteBuffer buf = ByteBuffer.wrap(strCompress.getBytes("UTF8"));
    115         ByteBuffer actualOutput = actualDeflater.deflate(buf);
    116         WriteRequest writeRequest = new WriteRequest(buf);
    117
    118         // record all the mock calls
    119         ioFilterChain.contains(CompressionFilter.class);
    120         mockIoFilterChain.setReturnValue(false);
    121
    122         ioFilterChain.getSession();
    123         mockIoFilterChain.setReturnValue(session);
    124
    125         session.setAttribute(CompressionFilter.class.getName() + ".Deflater",
    126                 deflater);
    127         mockSession.setDefaultMatcher(new DataMatcher());
    128         mockSession.setReturnValue(null, MockControl.ONE);
    129
    130         session.setAttribute(CompressionFilter.class.getName() + ".Inflater",
    131                 inflater);
    132         mockSession.setReturnValue(null, MockControl.ONE);
    133
    134         session.containsAttribute(CompressionFilter.DISABLE_COMPRESSION_ONCE);
    135         mockSession.setReturnValue(false);
    136
    137         session.getAttribute(CompressionFilter.class.getName() + ".Deflater");
    138         mockSession.setReturnValue(deflater);
    139
    140         nextFilter.filterWrite(session, new WriteRequest(actualOutput));
    141
    142         // switch to playback mode
    143         mockSession.replay();
    144         mockIoFilterChain.replay();
    145         mockNextFilter.replay();
    146
    147         // make the actual calls on the filter
    148         filter.onPreAdd(ioFilterChain, "CompressionFilter", nextFilter);
    149         filter.filterWrite(nextFilter, session, writeRequest);
    150
    151         // verify that all the calls happened as recorded
    152         mockNextFilter.verify();
    153
    154         assertTrue(true);
    155     }
    156
    157     public void testDecompression() throws Exception {
    158         // prepare the input data
    159         ByteBuffer buf = ByteBuffer.wrap(strCompress.getBytes("UTF8"));
    160         ByteBuffer byteInput = actualDeflater.deflate(buf);
    161         ByteBuffer actualOutput = actualInflater.inflate(byteInput);
    162
    163         // record all the mock calls
    164         ioFilterChain.contains(CompressionFilter.class);
    165         mockIoFilterChain.setReturnValue(false);
    166
    167         ioFilterChain.getSession();
    168         mockIoFilterChain.setReturnValue(session);
    169
    170         session.setAttribute(CompressionFilter.class.getName() + ".Deflater",
    171                 deflater);
    172         mockSession.setDefaultMatcher(new DataMatcher());
    173         mockSession.setReturnValue(null, MockControl.ONE);
    174
    175         session.setAttribute(CompressionFilter.class.getName() + ".Inflater",
    176                 inflater);
    177         mockSession.setReturnValue(null, MockControl.ONE);
    178
    179         session.getAttribute(CompressionFilter.class.getName() + ".Inflater");
    180         mockSession.setReturnValue(inflater);
    181
    182         nextFilter.messageReceived(session, actualOutput);
    183
    184         // switch to playback mode
    185         mockSession.replay();
    186         mockIoFilterChain.replay();
    187         mockNextFilter.replay();
    188
    189         // make the actual calls on the filter
    190         filter.onPreAdd(ioFilterChain, "CompressionFilter", nextFilter);
    191         filter.messageReceived(nextFilter, session, byteInput);
    192
    193         // verify that all the calls happened as recorded
    194         mockNextFilter.verify();
    195
    196         assertTrue(true);
    197     }
    198
    199     /**
    200      * A matcher used to check if the actual and expected outputs matched
    201      *
    202      * @author The Apache Directory MINA subproject (mina-dev@directory.apache.org)
    203      */
    204     class DataMatcher extends AbstractMatcher {
    205         protected boolean argumentMatches(Object arg0, Object arg1) {
    206             // we need to only verify the ByteBuffer output
    207             if (arg0 instanceof WriteRequest) {
    208                 WriteRequest expected = (WriteRequest) arg0;
    209                 WriteRequest actual = (WriteRequest) arg1;
    210                 ByteBuffer bExpected = (ByteBuffer) expected.getMessage();
    211                 ByteBuffer bActual = (ByteBuffer) actual.getMessage();
    212                 return bExpected.equals(bActual);
    213             }
    214             return true;
    215         }
    216     }
    217 }