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