1   /*
2    *   @(#) $Id: ProtocolFilterChainTest.java 332218 2005-11-10 03:52:42Z trustin $
3    *
4    *   Copyright 2004 The Apache Software Foundation
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   *
18   */
19  package org.apache.mina.protocol;
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.IdleStatus;
28  import org.apache.mina.common.SessionConfig;
29  import org.apache.mina.common.TransportType;
30  
31  /***
32   * Tests {@link AbstractProtocolFilterChain}.
33   * 
34   * @author The Apache Directory Project (dev@directory.apache.org)
35   * @version $Rev: 332218 $, $Date: 2005-11-10 12:52:42 +0900 $ 
36   */
37  public class ProtocolFilterChainTest extends TestCase
38  {
39      private ProtocolFilterChainImpl chain;
40      private ProtocolSession session;
41      private String result;
42  
43      public void setUp()
44      {
45          chain = new ProtocolFilterChainImpl();
46          session = new TestSession();
47          result = "";
48      }
49      
50      public void tearDown()
51      {
52      }
53      
54      public void testDefault()
55      {
56          run( "HSO HMR HMS HSI HEC HSC" );
57      }
58      
59      public void testChained()
60      {
61          chain.addLast( "A", new TestFilter( 'A' ) );
62          chain.addLast( "B", new TestFilter( 'B' ) );
63          run( "ASO BSO HSO" +
64               "AMR BMR HMR" +
65               "BFW AFW AMS BMS HMS" +
66               "ASI BSI HSI" +
67               "AEC BEC HEC" +
68               "ASC BSC HSC" );
69      }
70      
71      private void run( String expectedResult )
72      {
73          chain.sessionOpened( session );
74          chain.messageReceived( session, new Object() );
75          chain.filterWrite( session, new Object() );
76          chain.sessionIdle( session, IdleStatus.READER_IDLE );
77          chain.exceptionCaught( session, new Exception() );
78          chain.sessionClosed( session );
79          
80          result = formatResult( result );
81          expectedResult = formatResult( expectedResult );
82          
83          System.out.println( "Expected: " + expectedResult );
84          System.out.println( "Actual:   " + result );
85          Assert.assertEquals( expectedResult, result );
86      }
87      
88      private String formatResult( String result )
89      {
90          result = result.replaceAll( "//s", "" );
91          StringBuffer buf = new StringBuffer( result.length() * 4 / 3 );
92          for( int i = 0; i < result.length(); i++ )
93          {
94              buf.append( result.charAt( i ) );
95              if( i % 3 == 2 )
96              {
97                  buf.append(' ');
98              }
99          }
100         
101         return buf.toString();
102     }
103 
104     private class TestSession extends BaseSession implements ProtocolSession
105     {
106         private ProtocolHandler handler = new ProtocolHandlerAdapter()
107         {
108             public void sessionOpened(ProtocolSession session) {
109                 result += "HSO";
110             }
111 
112             public void sessionClosed(ProtocolSession session) {
113                 result += "HSC";
114             }
115 
116             public void sessionIdle(ProtocolSession session, IdleStatus status) {
117                 result += "HSI";
118             }
119 
120             public void exceptionCaught(ProtocolSession session, Throwable cause) {
121                 result += "HEC";
122                 if( cause.getClass() != Exception.class )
123                 {
124                     cause.printStackTrace( System.out );
125                 }
126             }
127 
128             public void messageReceived(ProtocolSession session, Object message) {
129                 result += "HMR";
130             }
131 
132             public void messageSent(ProtocolSession session, Object message) {
133                 result += "HMS";
134             }
135         };
136 
137         public ProtocolHandler getHandler() {
138             return handler;
139         }
140 
141         public ProtocolEncoder getEncoder() {
142             return null;
143         }
144 
145         public ProtocolDecoder getDecoder() {
146             return null;
147         }
148 
149         public void close( boolean wait ) {
150         }
151 
152         public void write(Object message) {
153         }
154 
155         public int getScheduledWriteRequests()
156         {
157             return 0;
158         }
159 
160         public TransportType getTransportType() {
161             return null;
162         }
163 
164         public boolean isConnected() {
165             return false;
166         }
167 
168         public SessionConfig getConfig() {
169             return null;
170         }
171 
172         public SocketAddress getRemoteAddress() {
173             return null;
174         }
175 
176         public SocketAddress getLocalAddress() {
177             return null;
178         }
179 
180         public ProtocolFilterChain getFilterChain()
181         {
182             return null;
183         }
184     }
185 
186     private class TestFilter implements ProtocolFilter
187     {
188         private final char id;
189 
190         private TestFilter( char id )
191         {
192             this.id = id;
193         }
194         
195         public void sessionOpened(NextFilter nextFilter, ProtocolSession session) {
196             result += id + "SO";
197             nextFilter.sessionOpened( session );
198         }
199 
200         public void sessionClosed(NextFilter nextFilter, ProtocolSession session) {
201             result += id + "SC";
202             nextFilter.sessionClosed( session );
203         }
204 
205         public void sessionIdle(NextFilter nextFilter, ProtocolSession session, IdleStatus status) {
206             result += id + "SI";
207             nextFilter.sessionIdle( session, status );
208         }
209 
210         public void exceptionCaught(NextFilter nextFilter, ProtocolSession session, Throwable cause) {
211             result += id + "EC";
212             nextFilter.exceptionCaught( session, cause );
213         }
214 
215         public void filterWrite(NextFilter nextFilter, ProtocolSession session, Object message) {
216             result += id + "FW";
217             nextFilter.filterWrite( session, message );
218         }
219 
220         public void messageReceived(NextFilter nextFilter, org.apache.mina.protocol.ProtocolSession session, Object message) {
221             result += id + "MR";
222             nextFilter.messageReceived( session, message );
223         }
224 
225         public void messageSent(NextFilter nextFilter, org.apache.mina.protocol.ProtocolSession session, Object message) {
226             result += id + "MS";
227             nextFilter.messageSent( session, message );
228         }
229     }
230 
231     private static class ProtocolFilterChainImpl extends AbstractProtocolFilterChain
232     {
233         protected ProtocolFilterChainImpl()
234         {
235         }
236 
237         protected void doWrite( ProtocolSession session, Object message )
238         {
239             messageSent( session, message );
240         }
241     }
242     
243 }