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