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