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 Trustin Lee (trustin@apache.org)
36 * @version $Rev: 165586 $, $Date: 2005-05-02 15:27:27 +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 TransportType getTransportType()
152 {
153 return null;
154 }
155
156 public boolean isConnected()
157 {
158 return false;
159 }
160
161 public SessionConfig getConfig()
162 {
163 return null;
164 }
165
166 public SocketAddress getRemoteAddress()
167 {
168 return null;
169 }
170
171 public SocketAddress getLocalAddress()
172 {
173 return null;
174 }
175
176 public IoFilterChain getFilterChain()
177 {
178 return null;
179 }
180 }
181
182 private class TestFilter implements IoFilter
183 {
184 private final char id;
185
186 private TestFilter( char id )
187 {
188 this.id = id;
189 }
190
191 public void sessionOpened(NextFilter nextFilter, IoSession session) {
192 result += id + "SO ";
193 nextFilter.sessionOpened( session );
194 }
195
196 public void sessionClosed(NextFilter nextFilter, IoSession session) {
197 result += id + "SC ";
198 nextFilter.sessionClosed( session );
199 }
200
201 public void sessionIdle(NextFilter nextFilter, IoSession session, IdleStatus status) {
202 result += id + "SI ";
203 nextFilter.sessionIdle( session, status );
204 }
205
206 public void exceptionCaught(NextFilter nextFilter, IoSession session, Throwable cause) {
207 result += id + "EC ";
208 nextFilter.exceptionCaught( session, cause );
209 }
210
211 public void dataRead(NextFilter nextFilter, IoSession session, ByteBuffer buf) {
212 result += id + "DR ";
213 nextFilter.dataRead( session, buf );
214 }
215
216 public void dataWritten(NextFilter nextFilter, IoSession session, Object marker) {
217 result += id + "DW ";
218 nextFilter.dataWritten( session, marker );
219 }
220
221 public void filterWrite(NextFilter nextFilter, IoSession session, ByteBuffer buf, Object marker) {
222 result += id + "FW ";
223 nextFilter.filterWrite( session, buf, marker );
224 }
225 }
226
227 private static class IoFilterChainImpl extends AbstractIoFilterChain
228 {
229 protected IoFilterChainImpl()
230 {
231 }
232
233 protected void doWrite(IoSession session, ByteBuffer buffer, Object marker)
234 {
235 dataWritten( session, marker );
236 }
237 }
238
239 }