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 junit.framework.TestCase;
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  
33  /**
34   * @author The Apache MINA Project (dev@mina.apache.org)
35   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (Thu, 26 Jun 2008) $
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      @Override
88      protected void setUp() {
89          // create the necessary mock controls.
90          mockSession = MockControl.createControl(IoSession.class);
91          mockNextFilter = MockControl.createControl(NextFilter.class);
92          mockIoFilterChain = MockControl.createControl(IoFilterChain.class);
93  
94          // set the default matcher
95          mockNextFilter.setDefaultMatcher(new DataMatcher());
96  
97          session = (IoSession) mockSession.getMock();
98          nextFilter = (NextFilter) mockNextFilter.getMock();
99          ioFilterChain = (IoFilterChain) mockIoFilterChain.getMock();
100 
101         // create an instance of the filter
102         filter = new CompressionFilter(CompressionFilter.COMPRESSION_MAX);
103 
104         // deflater and inflater that will be used by the filter
105         deflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_DEFLATER);
106         inflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_INFLATER);
107 
108         // create instances of the deflater and inflater to help test the output
109         actualDeflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_DEFLATER);
110         actualInflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_INFLATER);
111     }
112 
113     public void testCompression() throws Exception {
114         // prepare the input data
115         IoBuffer buf = IoBuffer.wrap(strCompress.getBytes("UTF8"));
116         IoBuffer actualOutput = actualDeflater.deflate(buf);
117         WriteRequest writeRequest = new DefaultWriteRequest(buf);
118 
119         // record all the mock calls
120         ioFilterChain.contains(CompressionFilter.class);
121         mockIoFilterChain.setReturnValue(false);
122 
123         ioFilterChain.getSession();
124         mockIoFilterChain.setReturnValue(session);
125 
126         session.setAttribute(CompressionFilter.class.getName() + ".Deflater",
127                 deflater);
128         mockSession.setDefaultMatcher(new DataMatcher());
129         mockSession.setReturnValue(null, MockControl.ONE);
130 
131         session.setAttribute(CompressionFilter.class.getName() + ".Inflater",
132                 inflater);
133         mockSession.setReturnValue(null, MockControl.ONE);
134 
135         session.containsAttribute(CompressionFilter.DISABLE_COMPRESSION_ONCE);
136         mockSession.setReturnValue(false);
137 
138         session.getAttribute(CompressionFilter.class.getName() + ".Deflater");
139         mockSession.setReturnValue(deflater);
140 
141         nextFilter.filterWrite(session, new DefaultWriteRequest(actualOutput));
142 
143         // switch to playback mode
144         mockSession.replay();
145         mockIoFilterChain.replay();
146         mockNextFilter.replay();
147 
148         // make the actual calls on the filter
149         filter.onPreAdd(ioFilterChain, "CompressionFilter", nextFilter);
150         filter.filterWrite(nextFilter, session, writeRequest);
151 
152         // verify that all the calls happened as recorded
153         mockNextFilter.verify();
154 
155         assertTrue(true);
156     }
157 
158     public void testDecompression() throws Exception {
159         // prepare the input data
160         IoBuffer buf = IoBuffer.wrap(strCompress.getBytes("UTF8"));
161         IoBuffer byteInput = actualDeflater.deflate(buf);
162         IoBuffer actualOutput = actualInflater.inflate(byteInput);
163 
164         // record all the mock calls
165         ioFilterChain.contains(CompressionFilter.class);
166         mockIoFilterChain.setReturnValue(false);
167 
168         ioFilterChain.getSession();
169         mockIoFilterChain.setReturnValue(session);
170 
171         session.setAttribute(CompressionFilter.class.getName() + ".Deflater",
172                 deflater);
173         mockSession.setDefaultMatcher(new DataMatcher());
174         mockSession.setReturnValue(null, MockControl.ONE);
175 
176         session.setAttribute(CompressionFilter.class.getName() + ".Inflater",
177                 inflater);
178         mockSession.setReturnValue(null, MockControl.ONE);
179 
180         session.getAttribute(CompressionFilter.class.getName() + ".Inflater");
181         mockSession.setReturnValue(inflater);
182 
183         nextFilter.messageReceived(session, actualOutput);
184 
185         // switch to playback mode
186         mockSession.replay();
187         mockIoFilterChain.replay();
188         mockNextFilter.replay();
189 
190         // make the actual calls on the filter
191         filter.onPreAdd(ioFilterChain, "CompressionFilter", nextFilter);
192         filter.messageReceived(nextFilter, session, byteInput);
193 
194         // verify that all the calls happened as recorded
195         mockNextFilter.verify();
196 
197         assertTrue(true);
198     }
199 
200     /**
201      * A matcher used to check if the actual and expected outputs matched
202      */
203     class DataMatcher extends AbstractMatcher {
204         @Override
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                 IoBuffer bExpected = (IoBuffer) expected.getMessage();
211                 IoBuffer bActual = (IoBuffer) actual.getMessage();
212                 return bExpected.equals(bActual);
213             }
214             return true;
215         }
216     }
217 }