View Javadoc

    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.compression;
    21
    22  import static org.junit.Assert.assertTrue;
    23
    24  import org.apache.mina.core.buffer.IoBuffer;
    25  import org.apache.mina.core.filterchain.IoFilterChain;
    26  import org.apache.mina.core.filterchain.IoFilter.NextFilter;
    27  import org.apache.mina.core.session.IoSession;
    28  import org.apache.mina.core.write.DefaultWriteRequest;
    29  import org.apache.mina.core.write.WriteRequest;
    30  import org.easymock.AbstractMatcher;
    31  import org.easymock.MockControl;
    32  import org.junit.Before;
    33  import org.junit.Test;
    34
    35  /**
    36   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    37   */
    38  public class CompressionFilterTest {
    39      private MockControl mockSession;
    40
    41      private MockControl mockNextFilter;
    42
    43      private MockControl mockIoFilterChain;
    44
    45      private IoSession session;
    46
    47      private NextFilter nextFilter;
    48
    49      private IoFilterChain ioFilterChain;
    50
    51      private CompressionFilter filter;
    52
    53      private Zlib deflater;
    54
    55      private Zlib inflater;
    56
    57      private Zlib actualDeflater;
    58
    59      private Zlib actualInflater;
    60
    61      // the sample data to be used for testing
    62      String strCompress = "The quick brown fox jumps over the lazy dog.  "
    63              + "The quick brown fox jumps over the lazy dog.  " + "The quick brown fox jumps over the lazy dog.  "
    64              + "The quick brown fox jumps over the lazy dog.  " + "The quick brown fox jumps over the lazy dog.  "
    65              + "The quick brown fox jumps over the lazy dog.  " + "The quick brown fox jumps over the lazy dog.  "
    66              + "The quick brown fox jumps over the lazy dog.  " + "The quick brown fox jumps over the lazy dog.  "
    67              + "The quick brown fox jumps over the lazy dog.  " + "The quick brown fox jumps over the lazy dog.  "
    68              + "The quick brown fox jumps over the lazy dog.  " + "The quick brown fox jumps over the lazy dog.  "
    69              + "The quick brown fox jumps over the lazy dog.  " + "The quick brown fox jumps over the lazy dog.  "
    70              + "The quick brown fox jumps over the lazy dog.  " + "The quick brown fox jumps over the lazy dog.  "
    71              + "The quick brown fox jumps over the lazy dog.  " + "The quick brown fox jumps over the lazy dog.  "
    72              + "The quick brown fox jumps over the lazy dog.  " + "The quick brown fox jumps over the lazy dog.  "
    73              + "The quick brown fox jumps over the lazy dog.  " + "The quick brown fox jumps over the lazy dog.  "
    74              + "The quick brown fox jumps over the lazy dog.  " + "The quick brown fox jumps over the lazy dog.  ";
    75
    76      @Before
    77      public void setUp() {
    78          // create the necessary mock controls.
    79          mockSession = MockControl.createControl(IoSession.class);
    80          mockNextFilter = MockControl.createControl(NextFilter.class);
    81          mockIoFilterChain = MockControl.createControl(IoFilterChain.class);
    82
    83          // set the default matcher
    84          mockNextFilter.setDefaultMatcher(new DataMatcher());
    85
    86          session = (IoSession) mockSession.getMock();
    87          nextFilter = (NextFilter) mockNextFilter.getMock();
    88          ioFilterChain = (IoFilterChain) mockIoFilterChain.getMock();
    89
    90          // create an instance of the filter
    91          filter = new CompressionFilter(CompressionFilter.COMPRESSION_MAX);
    92
    93          // deflater and inflater that will be used by the filter
    94          deflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_DEFLATER);
    95          inflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_INFLATER);
    96
    97          // create instances of the deflater and inflater to help test the output
    98          actualDeflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_DEFLATER);
    99          actualInflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_INFLATER);
    100     }
    101
    102     @Test
    103     public void testCompression() throws Exception {
    104         // prepare the input data
    105         IoBuffer buf = IoBuffer.wrap(strCompress.getBytes("UTF8"));
    106         IoBuffer actualOutput = actualDeflater.deflate(buf);
    107         WriteRequest writeRequest = new DefaultWriteRequest(buf);
    108
    109         // record all the mock calls
    110         ioFilterChain.contains(CompressionFilter.class);
    111         mockIoFilterChain.setReturnValue(false);
    112
    113         ioFilterChain.getSession();
    114         mockIoFilterChain.setReturnValue(session);
    115
    116         session.setAttribute(CompressionFilter.class.getName() + ".Deflater", deflater);
    117         mockSession.setDefaultMatcher(new DataMatcher());
    118         mockSession.setReturnValue(null, MockControl.ONE);
    119
    120         session.setAttribute(CompressionFilter.class.getName() + ".Inflater", inflater);
    121         mockSession.setReturnValue(null, MockControl.ONE);
    122
    123         session.containsAttribute(CompressionFilter.DISABLE_COMPRESSION_ONCE);
    124         mockSession.setReturnValue(false);
    125
    126         session.getAttribute(CompressionFilter.class.getName() + ".Deflater");
    127         mockSession.setReturnValue(deflater);
    128
    129         nextFilter.filterWrite(session, new DefaultWriteRequest(actualOutput));
    130
    131         // switch to playback mode
    132         mockSession.replay();
    133         mockIoFilterChain.replay();
    134         mockNextFilter.replay();
    135
    136         // make the actual calls on the filter
    137         filter.onPreAdd(ioFilterChain, "CompressionFilter", nextFilter);
    138         filter.filterWrite(nextFilter, session, writeRequest);
    139
    140         // verify that all the calls happened as recorded
    141         mockNextFilter.verify();
    142
    143         assertTrue(true);
    144     }
    145
    146     @Test
    147     public void testDecompression() throws Exception {
    148         // prepare the input data
    149         IoBuffer buf = IoBuffer.wrap(strCompress.getBytes("UTF8"));
    150         IoBuffer byteInput = actualDeflater.deflate(buf);
    151         IoBuffer actualOutput = actualInflater.inflate(byteInput);
    152
    153         // record all the mock calls
    154         ioFilterChain.contains(CompressionFilter.class);
    155         mockIoFilterChain.setReturnValue(false);
    156
    157         ioFilterChain.getSession();
    158         mockIoFilterChain.setReturnValue(session);
    159
    160         session.setAttribute(CompressionFilter.class.getName() + ".Deflater", deflater);
    161         mockSession.setDefaultMatcher(new DataMatcher());
    162         mockSession.setReturnValue(null, MockControl.ONE);
    163
    164         session.setAttribute(CompressionFilter.class.getName() + ".Inflater", inflater);
    165         mockSession.setReturnValue(null, MockControl.ONE);
    166
    167         session.getAttribute(CompressionFilter.class.getName() + ".Inflater");
    168         mockSession.setReturnValue(inflater);
    169
    170         nextFilter.messageReceived(session, actualOutput);
    171
    172         // switch to playback mode
    173         mockSession.replay();
    174         mockIoFilterChain.replay();
    175         mockNextFilter.replay();
    176
    177         // make the actual calls on the filter
    178         filter.onPreAdd(ioFilterChain, "CompressionFilter", nextFilter);
    179         filter.messageReceived(nextFilter, session, byteInput);
    180
    181         // verify that all the calls happened as recorded
    182         mockNextFilter.verify();
    183
    184         assertTrue(true);
    185     }
    186
    187     /**
    188      * A matcher used to check if the actual and expected outputs matched
    189      */
    190     class DataMatcher extends AbstractMatcher {
    191         @Override
    192         protected boolean argumentMatches(Object arg0, Object arg1) {
    193             // we need to only verify the ByteBuffer output
    194             if (arg0 instanceof WriteRequest) {
    195                 WriteRequest expected = (WriteRequest) arg0;
    196                 WriteRequest actual = (WriteRequest) arg1;
    197                 IoBuffer bExpected = (IoBuffer) expected.getMessage();
    198                 IoBuffer bActual = (IoBuffer) actual.getMessage();
    199                 return bExpected.equals(bActual);
    200             }
    201             return true;
    202         }
    203     }
    204 }