View Javadoc
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 <a href="http://mina.apache.org">Apache MINA Project</a>
31   */
32  public class ChainedIoHandlerTest {
33      @Test
34      public void testChainedCommand() throws Exception {
35          IoHandlerChain chain = new IoHandlerChain();
36          StringBuilder buf = new StringBuilder();
37          chain.addLast("A", new TestCommand(buf, 'A'));
38          chain.addLast("B", new TestCommand(buf, 'B'));
39          chain.addLast("C", new TestCommand(buf, 'C'));
40  
41          new ChainedIoHandler(chain).messageReceived(new DummySession(), null);
42  
43          assertEquals("ABC", buf.toString());
44      }
45  
46      private class TestCommand implements IoHandlerCommand {
47          private final StringBuilder buf;
48  
49          private final char ch;
50  
51          public TestCommand(StringBuilder buf, char ch) {
52              this.buf = buf;
53              this.ch = ch;
54          }
55  
56          public void execute(NextCommand next, IoSession session, Object message) throws Exception {
57              buf.append(ch);
58              next.execute(session, message);
59          }
60      }
61  }