Skip to content

Commit 82053c7

Browse files
committed
Fixed the compression-filter
1 parent 3c4bc70 commit 82053c7

4 files changed

Lines changed: 60 additions & 159 deletions

File tree

mina-filter-compression/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
</dependency>
4646

4747
<dependency>
48-
<groupId>org.easymock</groupId>
49-
<artifactId>easymock</artifactId>
48+
<groupId>org.mockito</groupId>
49+
<artifactId>mockito-core</artifactId>
5050
</dependency>
5151
</dependencies>
5252

mina-filter-compression/src/main/java/org/apache/mina/filter/compression/CompressionFilter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,7 @@ public void onPreAdd(IoFilterChain parent, String name, NextFilter nextFilter) t
261261
throw new IllegalStateException("Only one " + CompressionFilter.class + " is permitted.");
262262
}
263263

264-
Zlib deflater = new Zlib(compressionLevel, Zlib.MODE_INFLATER, maxDecompressedSize,
265-
maxDecompressRatio, decompressRatioMinSize);
264+
Zlib deflater = new Zlib(compressionLevel, Zlib.MODE_DEFLATER);
266265
Zlib inflater = new Zlib(compressionLevel, Zlib.MODE_INFLATER, maxDecompressedSize,
267266
maxDecompressRatio, decompressRatioMinSize);
268267

mina-filter-compression/src/test/java/org/apache/mina/filter/compression/CompressionFilterTest.java

Lines changed: 48 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -19,188 +19,81 @@
1919
*/
2020
package org.apache.mina.filter.compression;
2121

22-
import static org.junit.Assert.assertTrue;
22+
import static org.junit.Assert.*;
23+
import static org.mockito.ArgumentMatchers.any;
24+
import static org.mockito.ArgumentMatchers.eq;
25+
import static org.mockito.Mockito.mock;
26+
import static org.mockito.Mockito.verify;
27+
import static org.mockito.Mockito.when;
2328

2429
import java.nio.charset.StandardCharsets;
30+
import java.util.HashMap;
31+
import java.util.Map;
2532

2633
import org.apache.mina.core.buffer.IoBuffer;
27-
import org.apache.mina.core.filterchain.IoFilterChain;
2834
import org.apache.mina.core.filterchain.IoFilter.NextFilter;
35+
import org.apache.mina.core.filterchain.IoFilterChain;
36+
import org.apache.mina.core.session.AttributeKey;
2937
import org.apache.mina.core.session.IoSession;
3038
import org.apache.mina.core.write.DefaultWriteRequest;
3139
import org.apache.mina.core.write.WriteRequest;
32-
import org.easymock.AbstractMatcher;
33-
import org.easymock.MockControl;
3440
import org.junit.Before;
3541
import org.junit.Test;
42+
import org.mockito.ArgumentCaptor;
3643

3744
/**
3845
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
3946
*/
4047
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;
48+
// the sample data to be used for testing
49+
private static final String STR_COMPRESS = repeat("The quick brown fox jumps over the lazy dog. ", 25);
5250

5351
private CompressionFilter filter;
5452

55-
private Zlib deflater;
56-
57-
private Zlib inflater;
53+
private IoSession session;
5854

59-
private Zlib actualDeflater;
55+
private IoFilterChain filterChain;
6056

61-
private Zlib actualInflater;
57+
private NextFilter nextFilter;
6258

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. ";
59+
private static String repeat(String value, int count) {
60+
StringBuilder builder = new StringBuilder(value.length() * count);
61+
for (int i = 0; i < count; i++) {
62+
builder.append(value);
63+
}
64+
return builder.toString();
65+
}
7766

7867
@Before
7968
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
9369
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);
70+
71+
// a mock session whose attributes are stored in a real map, so that the deflater and inflater
72+
// created by onPreAdd() are actually retrieved by filterWrite() and messageReceived().
73+
session = mock(IoSession.class);
74+
final Map<Object, Object> attributes = new HashMap<>();
75+
when(session.setAttribute(any(), any()))
76+
.thenAnswer(invocation -> attributes.put(invocation.getArgument(0), invocation.getArgument(1)));
77+
when(session.getAttribute(any())).thenAnswer(invocation -> attributes.get(invocation.getArgument(0)));
78+
when(session.containsAttribute(any())).thenAnswer(invocation -> attributes.containsKey(invocation.getArgument(0)));
79+
when(session.removeAttribute(any())).thenAnswer(invocation -> attributes.remove(invocation.getArgument(0)));
80+
81+
filterChain = mock(IoFilterChain.class);
82+
when(filterChain.contains(CompressionFilter.class)).thenReturn(false);
83+
when(filterChain.getSession()).thenReturn(session);
84+
85+
nextFilter = mock(NextFilter.class);
14686
}
14787

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);
88+
public void testDeflaterAndInflaterNotSwapped() throws Exception {
89+
filter.onPreAdd(filterChain, "CompressionFilter", nextFilter);
90+
IoBuffer input = IoBuffer.wrap(STR_COMPRESS.getBytes(StandardCharsets.UTF_8));
91+
Zlib deflater = (Zlib) session.getAttribute(new AttributeKey(CompressionFilter.class, "deflater"));
92+
assertNotNull(deflater);
93+
assertThrows(IllegalStateException.class, () -> deflater.inflate(input));
15894

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-
}
95+
Zlib inflater = (Zlib) session.getAttribute(new AttributeKey(CompressionFilter.class, "inflater"));
96+
assertNotNull(inflater);
97+
assertThrows(IllegalStateException.class, () -> inflater.deflate(input));
20598
}
20699
}

pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
<!-- Jars -->
150150
<!-- This version of easymock can't be upgraded -->
151151
<version.easymock>2.5.2</version.easymock>
152+
<version.mockito>4.11.0</version.mockito>
152153
<version.jboss.javassist>3.8.0.GA</version.jboss.javassist>
153154
<version.jdom>1.0</version.jdom>
154155
<version.jmock>1.2.0</version.jmock>
@@ -317,6 +318,14 @@
317318
<scope>test</scope>
318319
</dependency>
319320

321+
<dependency>
322+
<groupId>org.mockito</groupId>
323+
<artifactId>mockito-core</artifactId>
324+
<version>${version.mockito}</version>
325+
<optional>true</optional>
326+
<scope>test</scope>
327+
</dependency>
328+
320329
<dependency>
321330
<groupId>net.sourceforge.pmd</groupId>
322331
<artifactId>pmd-core</artifactId>

0 commit comments

Comments
 (0)