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 <a href="http://mina.apache.org">Apache MINA Project</a>
35   */
36  public class CompressionFilterTest extends TestCase {
37      private MockControl mockSession;
38  
39      private MockControl mockNextFilter;
40  
41      private MockControl mockIoFilterChain;
42  
43      private IoSession session;
44  
45      private NextFilter nextFilter;
46  
47      private IoFilterChain ioFilterChain;
48  
49      private CompressionFilter filter;
50  
51      private Zlib deflater;
52  
53      private Zlib inflater;
54  
55      private Zlib actualDeflater;
56  
57      private Zlib actualInflater;
58  
59      // the sample data to be used for testing
60      String strCompress = "The quick brown fox jumps over the lazy dog.  "
61              + "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  
86      @Override
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         IoBuffer buf = IoBuffer.wrap(strCompress.getBytes("UTF8"));
115         IoBuffer actualOutput = actualDeflater.deflate(buf);
116         WriteRequest writeRequest = new DefaultWriteRequest(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 DefaultWriteRequest(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         IoBuffer buf = IoBuffer.wrap(strCompress.getBytes("UTF8"));
160         IoBuffer byteInput = actualDeflater.deflate(buf);
161         IoBuffer 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     class DataMatcher extends AbstractMatcher {
203         @Override
204         protected boolean argumentMatches(Object arg0, Object arg1) {
205             // we need to only verify the ByteBuffer output
206             if (arg0 instanceof WriteRequest) {
207                 WriteRequest expected = (WriteRequest) arg0;
208                 WriteRequest actual = (WriteRequest) arg1;
209                 IoBuffer bExpected = (IoBuffer) expected.getMessage();
210                 IoBuffer bActual = (IoBuffer) actual.getMessage();
211                 return bExpected.equals(bActual);
212             }
213             return true;
214         }
215     }
216 }