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  package org.apache.mina.integration.xbean;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertTrue;
25  
26  import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;
27  import org.apache.xbean.spring.context.FileSystemXmlApplicationContext;
28  import org.junit.Test;
29  import org.springframework.context.ApplicationContext;
30  
31  import java.io.File;
32  import java.net.InetSocketAddress;
33  import java.net.URL;
34  
35  /**
36   * TODO : Add documentation
37   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
38   */
39  public class SpringXBeanTest {
40      /**
41       * Checks to see we can easily configure a NIO based DatagramAcceptor 
42       * using XBean-Spring.  Tests various configuration settings for the 
43       * NIO based DatagramAcceptor.
44       * 
45       * @throws Exception if e got some error
46       */
47      @Test
48      public void testNioDatagramAcceptor() throws Exception {
49          ClassLoader classLoader = this.getClass().getClassLoader();
50          URL configURL = classLoader.getResource("org/apache/mina/integration/xbean/datagramAcceptor.xml");
51  
52          File configF = new File(configURL.toURI());
53          ApplicationContext factory = new FileSystemXmlApplicationContext(configF.toURI().toURL().toString());
54  
55          // test default without any properties
56          NioDatagramAcceptor acceptor0 = (NioDatagramAcceptor) factory.getBean("datagramAcceptor0");
57          assertNotNull("acceptor0 should not be null", acceptor0);
58          assertTrue(
59                  "Default constructor for NioDatagramAcceptor should have true value for closeOnDeactivation property",
60                  acceptor0.isCloseOnDeactivation());
61  
62          // test setting the port and IP for the acceptor
63          NioDatagramAcceptor acceptor1 = (NioDatagramAcceptor) factory.getBean("datagramAcceptor1");
64          assertNotNull("acceptor1 should not be null", acceptor1);
65          assertEquals("192.168.0.1", acceptor1.getDefaultLocalAddress().getAddress().getHostAddress());
66          assertEquals(110, acceptor1.getDefaultLocalAddress().getPort());
67  
68          // test creating with executor and some primitive properties
69          NioDatagramAcceptor acceptor2 = (NioDatagramAcceptor) factory.getBean("datagramAcceptor2");
70          assertNotNull(acceptor2);
71          assertFalse(acceptor2.isCloseOnDeactivation());
72          assertFalse("NioDatagramAcceptor should have false value for closeOnDeactivation property",
73                  acceptor2.isCloseOnDeactivation());
74  
75          // test creating with multiple addresses
76          NioDatagramAcceptor acceptor3 = (NioDatagramAcceptor) factory.getBean("datagramAcceptor3");
77          assertNotNull(acceptor3);
78          assertEquals(3, acceptor3.getDefaultLocalAddresses().size());
79  
80          InetSocketAddress address1 = (InetSocketAddress) acceptor3.getDefaultLocalAddresses().get(0);
81          assertEquals("192.168.0.1", address1.getAddress().getHostAddress());
82          assertEquals(10001, address1.getPort());
83  
84          InetSocketAddress address2 = (InetSocketAddress) acceptor3.getDefaultLocalAddresses().get(1);
85          assertEquals("192.168.0.2", address2.getAddress().getHostAddress());
86          assertEquals(10002, address2.getPort());
87  
88          InetSocketAddress address3 = (InetSocketAddress) acceptor3.getDefaultLocalAddresses().get(2);
89          assertEquals("192.168.0.3", address3.getAddress().getHostAddress());
90          assertEquals(10003, address3.getPort());
91  
92          // test with multiple default addresses 
93          //        NioDatagramAcceptor acceptor3 = ( NioDatagramAcceptor ) factory.getBean( "datagramAcceptor3" );
94          //        assertNotNull( acceptor3 );
95          //        assertEquals( 3, acceptor3.getDefaultLocalAddresses().size() );
96          //
97          //        SocketAddress address0 = acceptor3.getDefaultLocalAddresses().get( 0 );
98          //        assertNotNull( address0 );
99          //        
100         //        SocketAddress address1 = acceptor3.getDefaultLocalAddresses().get( 1 );
101         //        assertNotNull( address1 );
102         //
103         //        SocketAddress address2 = acceptor3.getDefaultLocalAddresses().get( 2 );
104         //        assertNotNull( address2 );
105     }
106 }