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
23 import junit.framework.Assert;
24 import junit.framework.TestCase;
25
26 import org.apache.mina.common.BaseSession;
27 import org.apache.mina.common.ByteBuffer;
28 import org.apache.mina.common.IdleStatus;
29 import org.apache.mina.common.SessionConfig;
30 import org.apache.mina.common.TransportType;
31
32 /***
33 * Tests {@link AbstractIoFilterChain}.
34 *
35 * @author The Apache Directory Project (dev@directory.apache.org)
36 * @version $Rev: 332218 $, $Date: 2005-11-10 12:52:42 +0900 $
37 */
38 public class IoFilterChainTest extends TestCase
39 {
40 private IoFilterChainImpl chain;
41 private IoSession session;
42 private String result;
43
44 public void setUp()
45 {
46 chain = new IoFilterChainImpl();
47 session = new TestSession();
48 result = "";
49 }
50
51 public void tearDown()
52 {
53 }
54
55 public void testDefault()
56 {
57 run( "HSO HDR HDW HSI HEC HSC" );
58 }
59
60 public void testChained()
61 {
62 chain.addLast( "A", new TestFilter( 'A' ) );
63 chain.addLast( "B", new TestFilter( 'B' ) );
64 run( "ASO BSO HSO" +
65 "ADR BDR HDR" +
66 "BFW AFW ADW BDW HDW" +
67 "ASI BSI HSI" +
68 "AEC BEC HEC" +
69 "ASC BSC HSC" );
70 }
71
72 private void run( String expectedResult )
73 {
74 chain.sessionOpened( session );
75 chain.dataRead( session, ByteBuffer.allocate( 16 ) );
76 chain.filterWrite( session, ByteBuffer.allocate( 16 ), null );
77 chain.sessionIdle( session, IdleStatus.READER_IDLE );
78 chain.exceptionCaught( session, new Exception() );
79 chain.sessionClosed( session );
80
81 result = formatResult( result );
82 expectedResult = formatResult( expectedResult );
83
84 System.out.println( "Expected: " + expectedResult );
85 System.out.println( "Actual: " + result );
86 Assert.assertEquals( expectedResult, result );
87 }
88
89 private String formatResult( String result )
90 {
91 result = result.replaceAll( "//s", "" );
92 StringBuffer buf = new StringBuffer( result.length() * 4 / 3 );
93 for( int i = 0; i < result.length(); i++ )
94 {
95 buf.append( result.charAt( i ) );
96 if( i % 3 == 2 )
97 {
98 buf.append(' ');
99 }
100 }
101
102 return buf.toString();
103 }
104
105 private class TestSession extends BaseSession implements IoSession
106 {
107 private IoHandler handler = new IoHandlerAdapter()
108 {
109 public void sessionOpened(IoSession session) {
110 result += "HSO ";
111 }
112
113 public void dataRead(IoSession session, ByteBuffer buf) {
114 result += "HDR ";
115 }
116
117 public void dataWritten(IoSession session, Object marker) {
118 result += "HDW ";
119 }
120
121 public void sessionIdle(IoSession session, IdleStatus status) {
122 result += "HSI ";
123 }
124
125 public void exceptionCaught(IoSession session, Throwable cause) {
126 result += "HEC ";
127 if( cause.getClass() != Exception.class )
128 {
129 cause.printStackTrace( System.out );
130 }
131 }
132
133 public void sessionClosed(IoSession session) {
134 result += "HSC ";
135 }
136 };
137
138 public IoHandler getHandler()
139 {
140 return handler;
141 }
142
143 public void close( boolean wait )
144 {
145 }
146
147 public void write(ByteBuffer buf, Object marker)
148 {
149 }
150
151 public int getScheduledWriteRequests()
152 {
153 return 0;
154 }
155
156 public TransportType getTransportType()
157 {
158 return null;
159 }
160
161 public boolean isConnected()
162 {
163 return false;
164 }
165
166 public SessionConfig getConfig()
167 {
168 return null;
169 }
170
171 public SocketAddress getRemoteAddress()
172 {
173 return null;
174 }
175
176 public SocketAddress getLocalAddress()
177 {
178 return null;
179 }
180
181 public IoFilterChain getFilterChain()
182 {
183 return null;
184 }
185 }
186
187 private class TestFilter implements IoFilter
188 {
189 private final char id;
190
191 private TestFilter( char id )
192 {
193 this.id = id;
194 }
195
196 public void sessionOpened(NextFilter nextFilter, IoSession session) {
197 result += id + "SO ";
198 nextFilter.sessionOpened( session );
199 }
200
201 public void sessionClosed(NextFilter nextFilter, IoSession session) {
202 result += id + "SC ";
203 nextFilter.sessionClosed( session );
204 }
205
206 public void sessionIdle(NextFilter nextFilter, IoSession session, IdleStatus status) {
207 result += id + "SI ";
208 nextFilter.sessionIdle( session, status );
209 }
210
211 public void exceptionCaught(NextFilter nextFilter, IoSession session, Throwable cause) {
212 result += id + "EC ";
213 nextFilter.exceptionCaught( session, cause );
214 }
215
216 public void dataRead(NextFilter nextFilter, IoSession session, ByteBuffer buf) {
217 result += id + "DR ";
218 nextFilter.dataRead( session, buf );
219 }
220
221 public void dataWritten(NextFilter nextFilter, IoSession session, Object marker) {
222 result += id + "DW ";
223 nextFilter.dataWritten( session, marker );
224 }
225
226 public void filterWrite(NextFilter nextFilter, IoSession session, ByteBuffer buf, Object marker) {
227 result += id + "FW ";
228 nextFilter.filterWrite( session, buf, marker );
229 }
230 }
231
232 private static class IoFilterChainImpl extends AbstractIoFilterChain
233 {
234 protected IoFilterChainImpl()
235 {
236 }
237
238 protected void doWrite(IoSession session, ByteBuffer buffer, Object marker)
239 {
240 dataWritten( session, marker );
241 }
242 }
243
244 }