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