TcpRosServerTest.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.internal.transport.tcp;
00018 
00019 import static org.junit.Assert.assertEquals;
00020 import static org.junit.Assert.assertTrue;
00021 import static org.junit.Assert.fail;
00022 
00023 import org.junit.After;
00024 import org.junit.Before;
00025 import org.junit.Test;
00026 import org.ros.address.AdvertiseAddress;
00027 import org.ros.address.BindAddress;
00028 import org.ros.address.InetAddressFactory;
00029 
00030 import java.net.InetSocketAddress;
00031 import java.net.UnknownHostException;
00032 import java.util.concurrent.Executors;
00033 import java.util.concurrent.ScheduledExecutorService;
00034 
00039 public class TcpRosServerTest {
00040   private ScheduledExecutorService executorService;
00041 
00042   @Before
00043   public void setup() {
00044     executorService = Executors.newScheduledThreadPool(10);
00045   }
00046 
00047   @After
00048   public void tearDown() {
00049     executorService.shutdown();
00050   }
00051 
00052   @Test
00053   public void testGetAddressFailsIfServerNotRunning() throws UnknownHostException {
00054     TcpRosServer tcpRosServer =
00055         new TcpRosServer(BindAddress.newPublic(), AdvertiseAddress.newPublic(), null, null,
00056             executorService);
00057 
00058     try {
00059       tcpRosServer.getAddress();
00060       fail();
00061     } catch (RuntimeException e) {
00062       // getAddress() must fail when the server is not running.
00063     }
00064 
00065     tcpRosServer.start();
00066     InetSocketAddress address = tcpRosServer.getAddress();
00067     assertTrue(address.getPort() > 0);
00068     assertEquals(InetAddressFactory.newNonLoopback().getCanonicalHostName(), address.getAddress()
00069         .getHostName());
00070     tcpRosServer.shutdown();
00071 
00072     try {
00073       tcpRosServer.getAddress();
00074       fail();
00075     } catch (RuntimeException e) {
00076       // getAddress() must fail when the server is not running.
00077     }
00078   }
00079 
00080   @Test
00081   public void testFailIfPortTaken() {
00082     TcpRosServer firstServer =
00083         new TcpRosServer(BindAddress.newPublic(), AdvertiseAddress.newPublic(), null, null,
00084             executorService);
00085     firstServer.start();
00086     try {
00087       TcpRosServer secondServer =
00088           new TcpRosServer(BindAddress.newPublic(firstServer.getAddress().getPort()),
00089               AdvertiseAddress.newPublic(), null, null, executorService);
00090       secondServer.start();
00091       fail();
00092     } catch (RuntimeException e) {
00093       // Starting a server on an already used port must fail.
00094     }
00095     firstServer.shutdown();
00096   }
00097 
00098   @Test
00099   public void testFailIfStartedWhileRunning() {
00100     TcpRosServer tcpRosServer =
00101         new TcpRosServer(BindAddress.newPublic(), AdvertiseAddress.newPublic(), null, null,
00102             executorService);
00103     tcpRosServer.start();
00104     try {
00105       tcpRosServer.start();
00106       fail();
00107     } catch (RuntimeException e) {
00108       // Starting the server twice must fail.
00109     }
00110     tcpRosServer.shutdown();
00111   }
00112 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49