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 junit.framework.Assert;
23  import junit.framework.TestCase;
24  
25  import org.apache.mina.core.session.DummySession;
26  import org.apache.mina.core.session.IoSession;
27  
28  /**
29   * A test case for {@link ChainedIoHandler}.
30   *
31   * @author The Apache MINA Project (dev@mina.apache.org)
32   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
33   */
34  public class ChainedIoHandlerTest extends TestCase {
35      public static void main(String[] args) {
36          junit.textui.TestRunner.run(ChainedIoHandlerTest.class);
37      }
38  
39      public void testChainedCommand() throws Exception {
40          IoHandlerChain chain = new IoHandlerChain();
41          StringBuffer buf = new StringBuffer();
42          chain.addLast("A", new TestCommand(buf, 'A'));
43          chain.addLast("B", new TestCommand(buf, 'B'));
44          chain.addLast("C", new TestCommand(buf, 'C'));
45  
46          new ChainedIoHandler(chain).messageReceived(new DummySession(), null);
47  
48          Assert.assertEquals("ABC", buf.toString());
49      }
50  
51      private class TestCommand implements IoHandlerCommand {
52          private final StringBuffer buf;
53  
54          private final char ch;
55  
56          private TestCommand(StringBuffer buf, char ch) {
57              this.buf = buf;
58              this.ch = ch;
59          }
60  
61          public void execute(NextCommand next, IoSession session, Object message)
62                  throws Exception {
63              buf.append(ch);
64              next.execute(session, message);
65          }
66      }
67  }