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 */
019package org.apache.mina.integration.xbean;
020
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertFalse;
023import static org.junit.Assert.assertNotNull;
024import static org.junit.Assert.assertTrue;
025
026import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;
027import org.apache.xbean.spring.context.FileSystemXmlApplicationContext;
028import org.junit.Test;
029import org.springframework.context.ApplicationContext;
030
031import java.io.File;
032import java.net.InetSocketAddress;
033import java.net.URL;
034
035/**
036 * TODO : Add documentation
037 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
038 */
039public class SpringXBeanTest {
040    /**
041     * Checks to see we can easily configure a NIO based DatagramAcceptor 
042     * using XBean-Spring.  Tests various configuration settings for the 
043     * NIO based DatagramAcceptor.
044     */
045    @Test
046    public void testNioDatagramAcceptor() throws Exception {
047        ClassLoader classLoader = this.getClass().getClassLoader();
048        URL configURL = classLoader.getResource("org/apache/mina/integration/xbean/datagramAcceptor.xml");
049
050        File configF = new File(configURL.toURI());
051        ApplicationContext factory = new FileSystemXmlApplicationContext(configF.toURI().toURL().toString());
052
053        // test default without any properties
054        NioDatagramAcceptor acceptor0 = (NioDatagramAcceptor) factory.getBean("datagramAcceptor0");
055        assertNotNull("acceptor0 should not be null", acceptor0);
056        assertTrue(
057                "Default constructor for NioDatagramAcceptor should have true value for closeOnDeactivation property",
058                acceptor0.isCloseOnDeactivation());
059
060        // test setting the port and IP for the acceptor
061        NioDatagramAcceptor acceptor1 = (NioDatagramAcceptor) factory.getBean("datagramAcceptor1");
062        assertNotNull("acceptor1 should not be null", acceptor1);
063        assertEquals("192.168.0.1", acceptor1.getDefaultLocalAddress().getAddress().getHostAddress());
064        assertEquals(110, acceptor1.getDefaultLocalAddress().getPort());
065
066        // test creating with executor and some primitive properties
067        NioDatagramAcceptor acceptor2 = (NioDatagramAcceptor) factory.getBean("datagramAcceptor2");
068        assertNotNull(acceptor2);
069        assertFalse(acceptor2.isCloseOnDeactivation());
070        assertFalse("NioDatagramAcceptor should have false value for closeOnDeactivation property",
071                acceptor2.isCloseOnDeactivation());
072
073        // test creating with multiple addresses
074        NioDatagramAcceptor acceptor3 = (NioDatagramAcceptor) factory.getBean("datagramAcceptor3");
075        assertNotNull(acceptor3);
076        assertEquals(3, acceptor3.getDefaultLocalAddresses().size());
077
078        InetSocketAddress address1 = (InetSocketAddress) acceptor3.getDefaultLocalAddresses().get(0);
079        assertEquals("192.168.0.1", address1.getAddress().getHostAddress());
080        assertEquals(10001, address1.getPort());
081
082        InetSocketAddress address2 = (InetSocketAddress) acceptor3.getDefaultLocalAddresses().get(1);
083        assertEquals("192.168.0.2", address2.getAddress().getHostAddress());
084        assertEquals(10002, address2.getPort());
085
086        InetSocketAddress address3 = (InetSocketAddress) acceptor3.getDefaultLocalAddresses().get(2);
087        assertEquals("192.168.0.3", address3.getAddress().getHostAddress());
088        assertEquals(10003, address3.getPort());
089
090        // test with multiple default addresses 
091        //        NioDatagramAcceptor acceptor3 = ( NioDatagramAcceptor ) factory.getBean( "datagramAcceptor3" );
092        //        assertNotNull( acceptor3 );
093        //        assertEquals( 3, acceptor3.getDefaultLocalAddresses().size() );
094        //
095        //        SocketAddress address0 = acceptor3.getDefaultLocalAddresses().get( 0 );
096        //        assertNotNull( address0 );
097        //        
098        //        SocketAddress address1 = acceptor3.getDefaultLocalAddresses().get( 1 );
099        //        assertNotNull( address1 );
100        //
101        //        SocketAddress address2 = acceptor3.getDefaultLocalAddresses().get( 2 );
102        //        assertNotNull( address2 );
103    }
104}