001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.handler.chain;
021
022import org.apache.mina.core.session.DummySession;
023import org.apache.mina.core.session.IoSession;
024import org.junit.Test;
025import static org.junit.Assert.assertEquals;
026
027/**
028 * A test case for {@link ChainedIoHandler}.
029 *
030 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
031 */
032public class ChainedIoHandlerTest {
033    @Test
034    public void testChainedCommand() throws Exception {
035        IoHandlerChain chain = new IoHandlerChain();
036        StringBuilder buf = new StringBuilder();
037        chain.addLast("A", new TestCommand(buf, 'A'));
038        chain.addLast("B", new TestCommand(buf, 'B'));
039        chain.addLast("C", new TestCommand(buf, 'C'));
040
041        new ChainedIoHandler(chain).messageReceived(new DummySession(), null);
042
043        assertEquals("ABC", buf.toString());
044    }
045
046    private class TestCommand implements IoHandlerCommand {
047        private final StringBuilder buf;
048
049        private final char ch;
050
051        public TestCommand(StringBuilder buf, char ch) {
052            this.buf = buf;
053            this.ch = ch;
054        }
055
056        public void execute(NextCommand next, IoSession session, Object message) throws Exception {
057            buf.append(ch);
058            next.execute(session, message);
059        }
060    }
061}