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      @Test
46      public void testNioDatagramAcceptor() throws Exception {
47          ClassLoader classLoader = this.getClass().getClassLoader();
48          URL configURL = classLoader.getResource("org/apache/mina/integration/xbean/datagramAcceptor.xml");
49  
50          File configF = new File(configURL.toURI());
51          ApplicationContext factory = new FileSystemXmlApplicationContext(configF.toURI().toURL().toString());
52  
53          // test default without any properties
54          NioDatagramAcceptor acceptor0 = (NioDatagramAcceptor) factory.getBean("datagramAcceptor0");
55          assertNotNull("acceptor0 should not be null", acceptor0);
56          assertTrue(
57                  "Default constructor for NioDatagramAcceptor should have true value for closeOnDeactivation property",
58                  acceptor0.isCloseOnDeactivation());
59  
60          // test setting the port and IP for the acceptor
61          NioDatagramAcceptor acceptor1 = (NioDatagramAcceptor) factory.getBean("datagramAcceptor1");
62          assertNotNull("acceptor1 should not be null", acceptor1);
63          assertEquals("192.168.0.1", acceptor1.getDefaultLocalAddress().getAddress().getHostAddress());
64          assertEquals(110, acceptor1.getDefaultLocalAddress().getPort());
65  
66          // test creating with executor and some primitive properties
67          NioDatagramAcceptor acceptor2 = (NioDatagramAcceptor) factory.getBean("datagramAcceptor2");
68          assertNotNull(acceptor2);
69          assertFalse(acceptor2.isCloseOnDeactivation());
70          assertFalse("NioDatagramAcceptor should have false value for closeOnDeactivation property",
71                  acceptor2.isCloseOnDeactivation());
72  
73          // test creating with multiple addresses
74          NioDatagramAcceptor acceptor3 = (NioDatagramAcceptor) factory.getBean("datagramAcceptor3");
75          assertNotNull(acceptor3);
76          assertEquals(3, acceptor3.getDefaultLocalAddresses().size());
77  
78          InetSocketAddress address1 = (InetSocketAddress) acceptor3.getDefaultLocalAddresses().get(0);
79          assertEquals("192.168.0.1", address1.getAddress().getHostAddress());
80          assertEquals(10001, address1.getPort());
81  
82          InetSocketAddress address2 = (InetSocketAddress) acceptor3.getDefaultLocalAddresses().get(1);
83          assertEquals("192.168.0.2", address2.getAddress().getHostAddress());
84          assertEquals(10002, address2.getPort());
85  
86          InetSocketAddress address3 = (InetSocketAddress) acceptor3.getDefaultLocalAddresses().get(2);
87          assertEquals("192.168.0.3", address3.getAddress().getHostAddress());
88          assertEquals(10003, address3.getPort());
89  
90          // test with multiple default addresses 
91          //        NioDatagramAcceptor acceptor3 = ( NioDatagramAcceptor ) factory.getBean( "datagramAcceptor3" );
92          //        assertNotNull( acceptor3 );
93          //        assertEquals( 3, acceptor3.getDefaultLocalAddresses().size() );
94          //
95          //        SocketAddress address0 = acceptor3.getDefaultLocalAddresses().get( 0 );
96          //        assertNotNull( address0 );
97          //        
98          //        SocketAddress address1 = acceptor3.getDefaultLocalAddresses().get( 1 );
99          //        assertNotNull( address1 );
100         //
101         //        SocketAddress address2 = acceptor3.getDefaultLocalAddresses().get( 2 );
102         //        assertNotNull( address2 );
103     }
104 }