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         buf.flip();
108         WriteRequest writeRequest = new DefaultWriteRequest(buf);
109 
110         // record all the mock calls
111         ioFilterChain.contains(CompressionFilter.class);
112         mockIoFilterChain.setReturnValue(false);
113 
114         ioFilterChain.getSession();
115         mockIoFilterChain.setReturnValue(session);
116 
117         session.setAttribute(CompressionFilter.class.getName() + ".Deflater", deflater);
118         mockSession.setDefaultMatcher(new DataMatcher());
119         mockSession.setReturnValue(null, MockControl.ONE);
120 
121         session.setAttribute(CompressionFilter.class.getName() + ".Inflater", inflater);
122         mockSession.setReturnValue(null, MockControl.ONE);
123 
124         session.containsAttribute(CompressionFilter.DISABLE_COMPRESSION_ONCE);
125         mockSession.setReturnValue(false);
126 
127         session.getAttribute(CompressionFilter.class.getName() + ".Deflater");
128         mockSession.setReturnValue(deflater);
129 
130         nextFilter.filterWrite(session, new DefaultWriteRequest(actualOutput));
131 
132         // switch to playback mode
133         mockSession.replay();
134         mockIoFilterChain.replay();
135         mockNextFilter.replay();
136 
137         // make the actual calls on the filter
138         filter.onPreAdd(ioFilterChain, "CompressionFilter", nextFilter);
139         filter.filterWrite(nextFilter, session, writeRequest);
140 
141         // verify that all the calls happened as recorded
142         mockNextFilter.verify();
143 
144         assertTrue(true);
145     }
146 
147     @Test
148     public void testDecompression() throws Exception {
149         // prepare the input data
150         IoBuffer buf = IoBuffer.wrap(strCompress.getBytes("UTF8"));
151         IoBuffer byteInput = actualDeflater.deflate(buf);
152         IoBuffer actualOutput = actualInflater.inflate(byteInput);
153 
154         // record all the mock calls
155         ioFilterChain.contains(CompressionFilter.class);
156         mockIoFilterChain.setReturnValue(false);
157 
158         ioFilterChain.getSession();
159         mockIoFilterChain.setReturnValue(session);
160 
161         session.setAttribute(CompressionFilter.class.getName() + ".Deflater", deflater);
162         mockSession.setDefaultMatcher(new DataMatcher());
163         mockSession.setReturnValue(null, MockControl.ONE);
164 
165         session.setAttribute(CompressionFilter.class.getName() + ".Inflater", inflater);
166         mockSession.setReturnValue(null, MockControl.ONE);
167 
168         session.getAttribute(CompressionFilter.class.getName() + ".Inflater");
169         mockSession.setReturnValue(inflater);
170 
171         nextFilter.messageReceived(session, actualOutput);
172 
173         // switch to playback mode
174         mockSession.replay();
175         mockIoFilterChain.replay();
176         mockNextFilter.replay();
177 
178         // make the actual calls on the filter
179         filter.onPreAdd(ioFilterChain, "CompressionFilter", nextFilter);
180         filter.messageReceived(nextFilter, session, byteInput);
181 
182         // verify that all the calls happened as recorded
183         mockNextFilter.verify();
184 
185         assertTrue(true);
186     }
187 
188     /**
189      * A matcher used to check if the actual and expected outputs matched
190      */
191     class DataMatcher extends AbstractMatcher {
192         @Override
193         protected boolean argumentMatches(Object arg0, Object arg1) {
194             // we need to only verify the ByteBuffer output
195             if (arg0 instanceof WriteRequest) {
196                 WriteRequest expected = (WriteRequest) arg0;
197                 WriteRequest actual = (WriteRequest) arg1;
198                 IoBuffer bExpected = (IoBuffer) expected.getMessage();
199                 IoBuffer bActual = (IoBuffer) actual.getMessage();
200                 return bExpected.equals(bActual);
201             }
202             return true;
203         }
204     }
205 }