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: */
020: package org.apache.mina.transport.socket.nio;
021:
022: import java.net.InetSocketAddress;
023:
024: import junit.framework.Assert;
025: import junit.framework.TestCase;
026:
027: import org.apache.mina.common.ConnectFuture;
028: import org.apache.mina.common.IoAcceptor;
029: import org.apache.mina.common.IoBuffer;
030: import org.apache.mina.common.IoConnector;
031: import org.apache.mina.common.IoFilter;
032: import org.apache.mina.common.IoFilterAdapter;
033: import org.apache.mina.common.IoHandler;
034: import org.apache.mina.common.IoHandlerAdapter;
035: import org.apache.mina.common.IoSession;
036: import org.apache.mina.common.WriteFuture;
037: import org.apache.mina.util.AvailablePortFinder;
038:
039: /**
040: * Tests if {@link NioDatagramAcceptor} session is configured properly.
041: *
042: * @author The Apache MINA Project (dev@mina.apache.org)
043: * @version $Rev: 600463 $, $Date: 2007-12-03 03:02:19 -0700 (Mon, 03 Dec 2007) $
044: */
045: public class DatagramConfigTest extends TestCase {
046: private IoAcceptor acceptor;
047: private IoConnector connector;
048: private String result;
049:
050: public DatagramConfigTest() {
051: }
052:
053: @Override
054: protected void setUp() throws Exception {
055: result = "";
056: acceptor = new NioDatagramAcceptor();
057: connector = new NioDatagramConnector();
058: }
059:
060: @Override
061: protected void tearDown() throws Exception {
062: acceptor.dispose();
063: connector.dispose();
064: }
065:
066: public void testAcceptorFilterChain() throws Exception {
067: int port = AvailablePortFinder.getNextAvailable(1024);
068: IoFilter mockFilter = new MockFilter();
069: IoHandler mockHandler = new MockHandler();
070:
071: acceptor.getFilterChain().addLast("mock", mockFilter);
072: acceptor.setHandler(mockHandler);
073: acceptor.bind(new InetSocketAddress(port));
074:
075: try {
076: connector.setHandler(new IoHandlerAdapter());
077: ConnectFuture future = connector
078: .connect(new InetSocketAddress("127.0.0.1", port));
079: future.awaitUninterruptibly();
080:
081: WriteFuture writeFuture = future.getSession().write(
082: IoBuffer.allocate(16).putInt(0).flip());
083: writeFuture.awaitUninterruptibly();
084: Assert.assertTrue(writeFuture.isWritten());
085:
086: future.getSession().close();
087:
088: for (int i = 0; i < 30; i++) {
089: if (result.length() == 2) {
090: break;
091: }
092: Thread.sleep(100);
093: }
094:
095: Assert.assertEquals("FH", result);
096: } finally {
097: acceptor.unbind();
098: }
099: }
100:
101: private class MockFilter extends IoFilterAdapter {
102:
103: @Override
104: public void messageReceived(NextFilter nextFilter,
105: IoSession session, Object message) throws Exception {
106: result += "F";
107: nextFilter.messageReceived(session, message);
108: }
109:
110: }
111:
112: private class MockHandler extends IoHandlerAdapter {
113: @Override
114: public void messageReceived(IoSession session, Object message)
115: throws Exception {
116: result += "H";
117: }
118: }
119: }
|