]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4898 - Moved NetworkUtils to sonar-process
authorStephane Gamard <stephane.gamard@searchbox.com>
Thu, 17 Jul 2014 09:40:41 +0000 (11:40 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Fri, 18 Jul 2014 09:27:59 +0000 (11:27 +0200)
server/sonar-process/src/main/java/org/sonar/process/NetworkUtils.java [new file with mode: 0644]
server/sonar-process/src/test/java/org/sonar/process/NetworkUtilsTest.java [new file with mode: 0644]
sonar-start/src/main/java/org/sonar/start/NetworkUtils.java [deleted file]
sonar-start/src/test/java/org/sonar/start/NetworkUtilsTest.java [deleted file]

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 (file)
index 0000000..a037ce0
--- /dev/null
@@ -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 (file)
index 0000000..09f6a59
--- /dev/null
@@ -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
diff --git a/sonar-start/src/main/java/org/sonar/start/NetworkUtils.java b/sonar-start/src/main/java/org/sonar/start/NetworkUtils.java
deleted file mode 100644 (file)
index e0c1caa..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * 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.start;
-
-import java.io.IOException;
-import java.net.ServerSocket;
-
-class NetworkUtils {
-
-  static private int lastPort = -1;
-
-  static int freePort() {
-    try {
-      ServerSocket s = new ServerSocket(lastPort + 1);
-      int port = s.getLocalPort();
-      s.close();
-      return port;
-    } catch (IOException e) {
-      throw new IllegalStateException("Can not find an open network port", e);
-    }
-  }
-
-  private static boolean isValidPort(int port) {
-    return port > 1023;
-  }
-}
diff --git a/sonar-start/src/test/java/org/sonar/start/NetworkUtilsTest.java b/sonar-start/src/test/java/org/sonar/start/NetworkUtilsTest.java
deleted file mode 100644 (file)
index 11cf6ee..0000000
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * 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.start;
-
-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