|
19 | 19 | */ |
20 | 20 | package org.apache.mina.filter.compression; |
21 | 21 |
|
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; |
23 | 28 |
|
24 | 29 | import java.nio.charset.StandardCharsets; |
| 30 | +import java.util.HashMap; |
| 31 | +import java.util.Map; |
25 | 32 |
|
26 | 33 | import org.apache.mina.core.buffer.IoBuffer; |
27 | | -import org.apache.mina.core.filterchain.IoFilterChain; |
28 | 34 | import org.apache.mina.core.filterchain.IoFilter.NextFilter; |
| 35 | +import org.apache.mina.core.filterchain.IoFilterChain; |
| 36 | +import org.apache.mina.core.session.AttributeKey; |
29 | 37 | import org.apache.mina.core.session.IoSession; |
30 | 38 | import org.apache.mina.core.write.DefaultWriteRequest; |
31 | 39 | import org.apache.mina.core.write.WriteRequest; |
32 | | -import org.easymock.AbstractMatcher; |
33 | | -import org.easymock.MockControl; |
34 | 40 | import org.junit.Before; |
35 | 41 | import org.junit.Test; |
| 42 | +import org.mockito.ArgumentCaptor; |
36 | 43 |
|
37 | 44 | /** |
38 | 45 | * @author <a href="http://mina.apache.org">Apache MINA Project</a> |
39 | 46 | */ |
40 | 47 | 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); |
52 | 50 |
|
53 | 51 | private CompressionFilter filter; |
54 | 52 |
|
55 | | - private Zlib deflater; |
56 | | - |
57 | | - private Zlib inflater; |
| 53 | + private IoSession session; |
58 | 54 |
|
59 | | - private Zlib actualDeflater; |
| 55 | + private IoFilterChain filterChain; |
60 | 56 |
|
61 | | - private Zlib actualInflater; |
| 57 | + private NextFilter nextFilter; |
62 | 58 |
|
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 | + } |
77 | 66 |
|
78 | 67 | @Before |
79 | 68 | 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 | 69 | 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); |
146 | 86 | } |
147 | 87 |
|
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)); |
158 | 94 |
|
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)); |
205 | 98 | } |
206 | 99 | } |
0 commit comments