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