1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.mina.io;
20
21 import java.net.SocketAddress;
22 import java.util.Iterator;
23
24 import junit.framework.Assert;
25 import junit.framework.TestCase;
26
27 import org.apache.mina.common.BaseSession;
28 import org.apache.mina.common.ByteBuffer;
29 import org.apache.mina.common.IdleStatus;
30 import org.apache.mina.common.SessionConfig;
31 import org.apache.mina.common.TransportType;
32
33 /***
34 * Tests {@link AbstractIoFilterChain}.
35 *
36 * @author The Apache Directory Project (dev@directory.apache.org)
37 * @version $Rev: 357871 $, $Date: 2005-12-20 10:56:40 +0900 (Tue, 20 Dec 2005) $
38 */
39 public class IoFilterChainTest extends TestCase
40 {
41 private IoFilterChainImpl chain;
42 private IoSession session;
43 private String result;
44
45 public void setUp()
46 {
47 chain = new IoFilterChainImpl();
48 session = new TestSession();
49 result = "";
50 }
51
52 public void tearDown()
53 {
54 }
55
56 public void testAdd() throws Exception
57 {
58 chain.addFirst( "A", new TestFilter( 'A' ) );
59 chain.addLast( "B", new TestFilter( 'B' ) );
60 chain.addFirst( "C", new TestFilter( 'C' ) );
61 chain.addLast( "D", new TestFilter( 'D' ) );
62 chain.addBefore( "B", "E", new TestFilter( 'E' ) );
63 chain.addBefore( "C", "F", new TestFilter( 'F' ) );
64 chain.addAfter( "B", "G", new TestFilter( 'G' ) );
65 chain.addAfter( "D", "H", new TestFilter( 'H' ) );
66
67 String actual = "";
68 for( Iterator i = chain.getChildren().iterator(); i.hasNext(); )
69 {
70 TestFilter f = ( TestFilter ) i.next();
71 actual += f.id;
72 }
73
74 Assert.assertEquals( "FCAEBGDH", actual );
75 }
76
77 public void testRemove() throws Exception
78 {
79 chain.addLast( "A", new TestFilter( 'A' ) );
80 chain.addLast( "B", new TestFilter( 'B' ) );
81 chain.addLast( "C", new TestFilter( 'C' ) );
82 chain.addLast( "D", new TestFilter( 'D' ) );
83 chain.addLast( "E", new TestFilter( 'E' ) );
84
85 chain.remove( "A" );
86 chain.remove( "E" );
87 chain.remove( "C" );
88 chain.remove( "B" );
89 chain.remove( "D" );
90
91 Assert.assertEquals( 0, chain.getChildren().size() );
92 }
93
94 public void testClear() throws Exception
95 {
96 chain.addLast( "A", new TestFilter( 'A' ) );
97 chain.addLast( "B", new TestFilter( 'B' ) );
98 chain.addLast( "C", new TestFilter( 'C' ) );
99 chain.addLast( "D", new TestFilter( 'D' ) );
100 chain.addLast( "E", new TestFilter( 'E' ) );
101
102 chain.clear();
103
104 Assert.assertEquals( 0, chain.getChildren().size() );
105 }
106
107 public void testDefault()
108 {
109 run( "HSO HDR HDW HSI HEC HSC" );
110 }
111
112 public void testChained()
113 {
114 chain.addLast( "A", new TestFilter( 'A' ) );
115 chain.addLast( "B", new TestFilter( 'B' ) );
116 run( "ASO BSO HSO" +
117 "ADR BDR HDR" +
118 "BFW AFW ADW BDW HDW" +
119 "ASI BSI HSI" +
120 "AEC BEC HEC" +
121 "ASC BSC HSC" );
122 }
123
124 private void run( String expectedResult )
125 {
126 chain.sessionOpened( session );
127 chain.dataRead( session, ByteBuffer.allocate( 16 ) );
128 chain.filterWrite( session, ByteBuffer.allocate( 16 ), null );
129 chain.sessionIdle( session, IdleStatus.READER_IDLE );
130 chain.exceptionCaught( session, new Exception() );
131 chain.sessionClosed( session );
132
133 result = formatResult( result );
134 expectedResult = formatResult( expectedResult );
135
136 System.out.println( "Expected: " + expectedResult );
137 System.out.println( "Actual: " + result );
138 Assert.assertEquals( expectedResult, result );
139 }
140
141 private String formatResult( String result )
142 {
143 result = result.replaceAll( "//s", "" );
144 StringBuffer buf = new StringBuffer( result.length() * 4 / 3 );
145 for( int i = 0; i < result.length(); i++ )
146 {
147 buf.append( result.charAt( i ) );
148 if( i % 3 == 2 )
149 {
150 buf.append(' ');
151 }
152 }
153
154 return buf.toString();
155 }
156
157 private class TestSession extends BaseSession implements IoSession
158 {
159 private IoHandler handler = new IoHandlerAdapter()
160 {
161 public void sessionOpened(IoSession session) {
162 result += "HSO ";
163 }
164
165 public void dataRead(IoSession session, ByteBuffer buf) {
166 result += "HDR ";
167 }
168
169 public void dataWritten(IoSession session, Object marker) {
170 result += "HDW ";
171 }
172
173 public void sessionIdle(IoSession session, IdleStatus status) {
174 result += "HSI ";
175 }
176
177 public void exceptionCaught(IoSession session, Throwable cause) {
178 result += "HEC ";
179 if( cause.getClass() != Exception.class )
180 {
181 cause.printStackTrace( System.out );
182 }
183 }
184
185 public void sessionClosed(IoSession session) {
186 result += "HSC ";
187 }
188 };
189
190 public IoHandler getHandler()
191 {
192 return handler;
193 }
194
195 public void close( boolean wait )
196 {
197 }
198
199 public void write(ByteBuffer buf, Object marker)
200 {
201 }
202
203 public int getScheduledWriteRequests()
204 {
205 return 0;
206 }
207
208 public TransportType getTransportType()
209 {
210 return null;
211 }
212
213 public boolean isConnected()
214 {
215 return false;
216 }
217
218 public SessionConfig getConfig()
219 {
220 return null;
221 }
222
223 public SocketAddress getRemoteAddress()
224 {
225 return null;
226 }
227
228 public SocketAddress getLocalAddress()
229 {
230 return null;
231 }
232
233 public IoFilterChain getFilterChain()
234 {
235 return null;
236 }
237 }
238
239 private class TestFilter implements IoFilter
240 {
241 private final char id;
242
243 private TestFilter( char id )
244 {
245 this.id = id;
246 }
247
248 public void sessionOpened(NextFilter nextFilter, IoSession session) {
249 result += id + "SO ";
250 nextFilter.sessionOpened( session );
251 }
252
253 public void sessionClosed(NextFilter nextFilter, IoSession session) {
254 result += id + "SC ";
255 nextFilter.sessionClosed( session );
256 }
257
258 public void sessionIdle(NextFilter nextFilter, IoSession session, IdleStatus status) {
259 result += id + "SI ";
260 nextFilter.sessionIdle( session, status );
261 }
262
263 public void exceptionCaught(NextFilter nextFilter, IoSession session, Throwable cause) {
264 result += id + "EC ";
265 nextFilter.exceptionCaught( session, cause );
266 }
267
268 public void dataRead(NextFilter nextFilter, IoSession session, ByteBuffer buf) {
269 result += id + "DR ";
270 nextFilter.dataRead( session, buf );
271 }
272
273 public void dataWritten(NextFilter nextFilter, IoSession session, Object marker) {
274 result += id + "DW ";
275 nextFilter.dataWritten( session, marker );
276 }
277
278 public void filterWrite(NextFilter nextFilter, IoSession session, ByteBuffer buf, Object marker) {
279 result += id + "FW ";
280 nextFilter.filterWrite( session, buf, marker );
281 }
282 }
283
284 private static class IoFilterChainImpl extends AbstractIoFilterChain
285 {
286 protected IoFilterChainImpl()
287 {
288 }
289
290 protected void doWrite(IoSession session, ByteBuffer buffer, Object marker)
291 {
292 dataWritten( session, marker );
293 }
294 }
295
296 }