001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.filter.compression;
021:
022: import junit.framework.TestCase;
023:
024: import org.apache.mina.common.IoBuffer;
025: import org.apache.mina.common.DefaultWriteRequest;
026: import org.apache.mina.common.IoFilterChain;
027: import org.apache.mina.common.IoSession;
028: import org.apache.mina.common.WriteRequest;
029: import org.apache.mina.common.IoFilter.NextFilter;
030: import org.easymock.AbstractMatcher;
031: import org.easymock.MockControl;
032:
033: /**
034: * @author The Apache MINA Project (dev@mina.apache.org)
035: * @version $Rev: 589932 $, $Date: 2007-10-29 19:50:39 -0600 (Mon, 29 Oct 2007) $
036: */
037: public class CompressionFilterTest extends TestCase {
038: private MockControl mockSession;
039:
040: private MockControl mockNextFilter;
041:
042: private MockControl mockIoFilterChain;
043:
044: private IoSession session;
045:
046: private NextFilter nextFilter;
047:
048: private IoFilterChain ioFilterChain;
049:
050: private CompressionFilter filter;
051:
052: private Zlib deflater;
053:
054: private Zlib inflater;
055:
056: private Zlib actualDeflater;
057:
058: private Zlib actualInflater;
059:
060: // the sample data to be used for testing
061: String strCompress = "The quick brown fox jumps over the lazy dog. "
062: + "The quick brown fox jumps over the lazy dog. "
063: + "The quick brown fox jumps over the lazy dog. "
064: + "The quick brown fox jumps over the lazy dog. "
065: + "The quick brown fox jumps over the lazy dog. "
066: + "The quick brown fox jumps over the lazy dog. "
067: + "The quick brown fox jumps over the lazy dog. "
068: + "The quick brown fox jumps over the lazy dog. "
069: + "The quick brown fox jumps over the lazy dog. "
070: + "The quick brown fox jumps over the lazy dog. "
071: + "The quick brown fox jumps over the lazy dog. "
072: + "The quick brown fox jumps over the lazy dog. "
073: + "The quick brown fox jumps over the lazy dog. "
074: + "The quick brown fox jumps over the lazy dog. "
075: + "The quick brown fox jumps over the lazy dog. "
076: + "The quick brown fox jumps over the lazy dog. "
077: + "The quick brown fox jumps over the lazy dog. "
078: + "The quick brown fox jumps over the lazy dog. "
079: + "The quick brown fox jumps over the lazy dog. "
080: + "The quick brown fox jumps over the lazy dog. "
081: + "The quick brown fox jumps over the lazy dog. "
082: + "The quick brown fox jumps over the lazy dog. "
083: + "The quick brown fox jumps over the lazy dog. "
084: + "The quick brown fox jumps over the lazy dog. "
085: + "The quick brown fox jumps over the lazy dog. ";
086:
087: @Override
088: protected void setUp() {
089: // create the necessary mock controls.
090: mockSession = MockControl.createControl(IoSession.class);
091: mockNextFilter = MockControl.createControl(NextFilter.class);
092: mockIoFilterChain = MockControl
093: .createControl(IoFilterChain.class);
094:
095: // set the default matcher
096: mockNextFilter.setDefaultMatcher(new DataMatcher());
097:
098: session = (IoSession) mockSession.getMock();
099: nextFilter = (NextFilter) mockNextFilter.getMock();
100: ioFilterChain = (IoFilterChain) mockIoFilterChain.getMock();
101:
102: // create an instance of the filter
103: filter = new CompressionFilter(
104: CompressionFilter.COMPRESSION_MAX);
105:
106: // deflater and inflater that will be used by the filter
107: deflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_DEFLATER);
108: inflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_INFLATER);
109:
110: // create instances of the deflater and inflater to help test the output
111: actualDeflater = new Zlib(Zlib.COMPRESSION_MAX,
112: Zlib.MODE_DEFLATER);
113: actualInflater = new Zlib(Zlib.COMPRESSION_MAX,
114: Zlib.MODE_INFLATER);
115: }
116:
117: public void testCompression() throws Exception {
118: // prepare the input data
119: IoBuffer buf = IoBuffer.wrap(strCompress.getBytes("UTF8"));
120: IoBuffer actualOutput = actualDeflater.deflate(buf);
121: WriteRequest writeRequest = new DefaultWriteRequest(buf);
122:
123: // record all the mock calls
124: ioFilterChain.contains(CompressionFilter.class);
125: mockIoFilterChain.setReturnValue(false);
126:
127: ioFilterChain.getSession();
128: mockIoFilterChain.setReturnValue(session);
129:
130: session.setAttribute(CompressionFilter.class.getName()
131: + ".Deflater", deflater);
132: mockSession.setDefaultMatcher(new DataMatcher());
133: mockSession.setReturnValue(null, MockControl.ONE);
134:
135: session.setAttribute(CompressionFilter.class.getName()
136: + ".Inflater", inflater);
137: mockSession.setReturnValue(null, MockControl.ONE);
138:
139: session
140: .containsAttribute(CompressionFilter.DISABLE_COMPRESSION_ONCE);
141: mockSession.setReturnValue(false);
142:
143: session.getAttribute(CompressionFilter.class.getName()
144: + ".Deflater");
145: mockSession.setReturnValue(deflater);
146:
147: nextFilter.filterWrite(session, new DefaultWriteRequest(
148: actualOutput));
149:
150: // switch to playback mode
151: mockSession.replay();
152: mockIoFilterChain.replay();
153: mockNextFilter.replay();
154:
155: // make the actual calls on the filter
156: filter.onPreAdd(ioFilterChain, "CompressionFilter", nextFilter);
157: filter.filterWrite(nextFilter, session, writeRequest);
158:
159: // verify that all the calls happened as recorded
160: mockNextFilter.verify();
161:
162: assertTrue(true);
163: }
164:
165: public void testDecompression() throws Exception {
166: // prepare the input data
167: IoBuffer buf = IoBuffer.wrap(strCompress.getBytes("UTF8"));
168: IoBuffer byteInput = actualDeflater.deflate(buf);
169: IoBuffer actualOutput = actualInflater.inflate(byteInput);
170:
171: // record all the mock calls
172: ioFilterChain.contains(CompressionFilter.class);
173: mockIoFilterChain.setReturnValue(false);
174:
175: ioFilterChain.getSession();
176: mockIoFilterChain.setReturnValue(session);
177:
178: session.setAttribute(CompressionFilter.class.getName()
179: + ".Deflater", deflater);
180: mockSession.setDefaultMatcher(new DataMatcher());
181: mockSession.setReturnValue(null, MockControl.ONE);
182:
183: session.setAttribute(CompressionFilter.class.getName()
184: + ".Inflater", inflater);
185: mockSession.setReturnValue(null, MockControl.ONE);
186:
187: session.getAttribute(CompressionFilter.class.getName()
188: + ".Inflater");
189: mockSession.setReturnValue(inflater);
190:
191: nextFilter.messageReceived(session, actualOutput);
192:
193: // switch to playback mode
194: mockSession.replay();
195: mockIoFilterChain.replay();
196: mockNextFilter.replay();
197:
198: // make the actual calls on the filter
199: filter.onPreAdd(ioFilterChain, "CompressionFilter", nextFilter);
200: filter.messageReceived(nextFilter, session, byteInput);
201:
202: // verify that all the calls happened as recorded
203: mockNextFilter.verify();
204:
205: assertTrue(true);
206: }
207:
208: /**
209: * A matcher used to check if the actual and expected outputs matched
210: */
211: class DataMatcher extends AbstractMatcher {
212: @Override
213: protected boolean argumentMatches(Object arg0, Object arg1) {
214: // we need to only verify the ByteBuffer output
215: if (arg0 instanceof WriteRequest) {
216: WriteRequest expected = (WriteRequest) arg0;
217: WriteRequest actual = (WriteRequest) arg1;
218: IoBuffer bExpected = (IoBuffer) expected.getMessage();
219: IoBuffer bActual = (IoBuffer) actual.getMessage();
220: return bExpected.equals(bActual);
221: }
222: return true;
223: }
224: }
225: }
|