diff options
author | Stephane Gamard <stephane.gamard@searchbox.com> | 2014-07-17 11:40:41 +0200 |
---|---|---|
committer | Stephane Gamard <stephane.gamard@searchbox.com> | 2014-07-18 11:27:59 +0200 |
commit | b4746359150e51cc9e8bfa2281739ae0fd614f93 (patch) | |
tree | 445ff32f3aa250357e69fa0de313fe97ac49ebec /server | |
parent | 85a1d650d2f671c8ff6397f36d47bca9d831305b (diff) | |
download | sonarqube-b4746359150e51cc9e8bfa2281739ae0fd614f93.tar.gz sonarqube-b4746359150e51cc9e8bfa2281739ae0fd614f93.zip |
SONAR-4898 - Moved NetworkUtils to sonar-process
Diffstat (limited to 'server')
-rw-r--r-- | server/sonar-process/src/main/java/org/sonar/process/NetworkUtils.java | 37 | ||||
-rw-r--r-- | server/sonar-process/src/test/java/org/sonar/process/NetworkUtilsTest.java | 61 |
2 files changed, 98 insertions, 0 deletions
diff --git a/server/sonar-process/src/main/java/org/sonar/process/NetworkUtils.java b/server/sonar-process/src/main/java/org/sonar/process/NetworkUtils.java new file mode 100644 index 00000000000..a037ce0113a --- /dev/null +++ b/server/sonar-process/src/main/java/org/sonar/process/NetworkUtils.java @@ -0,0 +1,37 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.process; + +import java.io.IOException; +import java.net.ServerSocket; + +public class NetworkUtils { + + public static int freePort() { + try { + ServerSocket s = new ServerSocket(0); + int port = s.getLocalPort(); + s.close(); + return port; + } catch (IOException e) { + throw new IllegalStateException("Can not find an open network port", e); + } + } +} diff --git a/server/sonar-process/src/test/java/org/sonar/process/NetworkUtilsTest.java b/server/sonar-process/src/test/java/org/sonar/process/NetworkUtilsTest.java new file mode 100644 index 00000000000..09f6a597209 --- /dev/null +++ b/server/sonar-process/src/test/java/org/sonar/process/NetworkUtilsTest.java @@ -0,0 +1,61 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.process; + +import org.junit.Test; + +import java.net.ServerSocket; + +import static org.fest.assertions.Assertions.assertThat; + +public class NetworkUtilsTest { + + + @Test + public void find_free_port() throws Exception { + int port = NetworkUtils.freePort(); + assertThat(port).isGreaterThan(1024); + } + + @Test + public void find_multiple_free_port() throws Exception { + int port1 = NetworkUtils.freePort(); + int port2 = NetworkUtils.freePort(); + + assertThat(port1).isGreaterThan(1024); + assertThat(port2).isGreaterThan(1024); + + assertThat(port1).isNotSameAs(port2); + } + + @Test + public void find_multiple_free_non_adjacent_port() throws Exception { + int port1 = NetworkUtils.freePort(); + + ServerSocket socket = new ServerSocket(port1 + 1); + + int port2 = NetworkUtils.freePort(); + + assertThat(port1).isGreaterThan(1024); + assertThat(port2).isGreaterThan(1024); + + assertThat(port1).isNotSameAs(port2); + } +}
\ No newline at end of file |