1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  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,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.mina.handler.chain;
21  
22  import java.net.SocketAddress;
23  
24  import junit.framework.Assert;
25  import junit.framework.TestCase;
26  
27  import org.apache.mina.common.IoFilterChain;
28  import org.apache.mina.common.IoHandler;
29  import org.apache.mina.common.IoServiceConfig;
30  import org.apache.mina.common.IoSession;
31  import org.apache.mina.common.IoService;
32  import org.apache.mina.common.IoSessionConfig;
33  import org.apache.mina.common.TransportType;
34  import org.apache.mina.common.support.BaseIoSession;
35  
36  /**
37   * A test case for {@link ChainedIoHandler}.
38   *
39   * @author The Apache Directory Project (mina-dev@directory.apache.org)
40   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (금, 13  7월 2007) $
41   */
42  public class ChainedIoHandlerTest extends TestCase {
43      public static void main(String[] args) {
44          junit.textui.TestRunner.run(ChainedIoHandlerTest.class);
45      }
46  
47      public void testChainedCommand() throws Exception {
48          IoHandlerChain chain = new IoHandlerChain();
49          StringBuffer buf = new StringBuffer();
50          chain.addLast("A", new TestCommand(buf, 'A'));
51          chain.addLast("B", new TestCommand(buf, 'B'));
52          chain.addLast("C", new TestCommand(buf, 'C'));
53  
54          new ChainedIoHandler(chain).messageReceived(new BaseIoSession() {
55              protected void updateTrafficMask() {
56              }
57  
58              public IoService getService() {
59                  return null;
60              }
61  
62              public IoServiceConfig getServiceConfig() {
63                  return null;
64              }
65  
66              public IoHandler getHandler() {
67                  return null;
68              }
69  
70              public IoFilterChain getFilterChain() {
71                  return null;
72              }
73  
74              public TransportType getTransportType() {
75                  return null;
76              }
77  
78              public SocketAddress getRemoteAddress() {
79                  return null;
80              }
81  
82              public SocketAddress getLocalAddress() {
83                  return null;
84              }
85  
86              public int getScheduledWriteRequests() {
87                  return 0;
88              }
89  
90              public IoSessionConfig getConfig() {
91                  return null;
92              }
93  
94              public SocketAddress getServiceAddress() {
95                  return null;
96              }
97  
98              public int getScheduledWriteBytes() {
99                  return 0;
100             }
101         }, null);
102 
103         Assert.assertEquals("ABC", buf.toString());
104     }
105 
106     private class TestCommand implements IoHandlerCommand {
107         private final StringBuffer buf;
108 
109         private final char ch;
110 
111         private TestCommand(StringBuffer buf, char ch) {
112             this.buf = buf;
113             this.ch = ch;
114         }
115 
116         public void execute(NextCommand next, IoSession session, Object message)
117                 throws Exception {
118             buf.append(ch);
119             next.execute(session, message);
120         }
121     }
122 }