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