diff --git a/spring-cloud-function-rsocket/src/test/java/org/springframework/cloud/function/rsocket/RoutingBrokerTests.java b/spring-cloud-function-rsocket/src/test/java/org/springframework/cloud/function/rsocket/RoutingBrokerTests.java index e44919ee4..77bbd565a 100644 --- a/spring-cloud-function-rsocket/src/test/java/org/springframework/cloud/function/rsocket/RoutingBrokerTests.java +++ b/spring-cloud-function-rsocket/src/test/java/org/springframework/cloud/function/rsocket/RoutingBrokerTests.java @@ -33,6 +33,7 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.messaging.rsocket.RSocketRequester; +import org.springframework.test.util.TestSocketUtils; /** * @author Spencer Gibb diff --git a/spring-cloud-function-rsocket/src/test/java/org/springframework/cloud/function/rsocket/TestSocketUtils.java b/spring-cloud-function-rsocket/src/test/java/org/springframework/cloud/function/rsocket/TestSocketUtils.java deleted file mode 100644 index 27722015e..000000000 --- a/spring-cloud-function-rsocket/src/test/java/org/springframework/cloud/function/rsocket/TestSocketUtils.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright 2022-2022 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.function.rsocket; - -import java.net.InetAddress; -import java.net.ServerSocket; -import java.util.Random; - -import javax.net.ServerSocketFactory; - -/** - * Simple test utility to find a random available TCP port. - *

Inspired by the now removed {@code org.springframework.util.SocketUtils} and is only used in a testing capacity. - * - * @author Chris Bono - * @deprecated will soon be removed or consolidated - do not use further - */ -@Deprecated -public final class TestSocketUtils { - - private static final Random random = new Random(System.nanoTime()); - - private TestSocketUtils() { - } - - /** - * Find an available TCP port randomly selected from the range {@code 1024-65535}. - * @return an available TCP port number - * @throws IllegalStateException if no available port could be found - */ - public static int findAvailableTcpPort() { - int minPort = 1024; - int maxPort = 65535; - int portRange = maxPort - minPort; - int candidatePort; - int searchCounter = 0; - do { - if (searchCounter > portRange) { - throw new IllegalStateException(String.format( - "Could not find an available TCP port after %d attempts", searchCounter)); - } - candidatePort = minPort + random.nextInt(portRange + 1); - searchCounter++; - } - while (!isPortAvailable(candidatePort)); - - return candidatePort; - } - - private static boolean isPortAvailable(int port) { - try { - ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket( - port, 1, InetAddress.getByName("localhost")); - serverSocket.close(); - return true; - } - catch (Exception ex) { - return false; - } - } -} diff --git a/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/test/FunctionalExporterTests.java b/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/test/FunctionalExporterTests.java index 0f0826e83..15ecb84c7 100644 --- a/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/test/FunctionalExporterTests.java +++ b/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/test/FunctionalExporterTests.java @@ -35,13 +35,13 @@ import org.springframework.cloud.function.context.FunctionRegistration; import org.springframework.cloud.function.context.catalog.FunctionTypeUtils; import org.springframework.cloud.function.context.test.FunctionalSpringBootTest; import org.springframework.cloud.function.test.FunctionalExporterTests.ApplicationConfiguration; -import org.springframework.cloud.function.web.TestSocketUtils; import org.springframework.cloud.function.web.source.SupplierExporter; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.GenericApplicationContext; import org.springframework.messaging.Message; import org.springframework.messaging.support.MessageBuilder; +import org.springframework.test.util.TestSocketUtils; import org.springframework.util.ReflectionUtils; import static org.assertj.core.api.Assertions.assertThat; diff --git a/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/TestSocketUtils.java b/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/TestSocketUtils.java deleted file mode 100644 index 5c35613de..000000000 --- a/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/TestSocketUtils.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright 2022-2022 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.function.web; - -import java.net.InetAddress; -import java.net.ServerSocket; -import java.util.Random; - -import javax.net.ServerSocketFactory; - -/** - * Simple test utility to find a random available TCP port. - *

Inspired by the now removed {@code org.springframework.util.SocketUtils} and is only used in a testing capacity. - * - * @author Chris Bono - * @deprecated will soon be removed or consolidated - do not use further - */ -@Deprecated -public final class TestSocketUtils { - - private static final Random random = new Random(System.nanoTime()); - - private TestSocketUtils() { - } - - /** - * Find an available TCP port randomly selected from the range {@code 1024-65535}. - * @return an available TCP port number - * @throws IllegalStateException if no available port could be found - */ - public static int findAvailableTcpPort() { - int minPort = 1024; - int maxPort = 65535; - int portRange = maxPort - minPort; - int candidatePort; - int searchCounter = 0; - do { - if (searchCounter > portRange) { - throw new IllegalStateException(String.format( - "Could not find an available TCP port after %d attempts", searchCounter)); - } - candidatePort = minPort + random.nextInt(portRange + 1); - searchCounter++; - } - while (!isPortAvailable(candidatePort)); - - return candidatePort; - } - - private static boolean isPortAvailable(int port) { - try { - ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket( - port, 1, InetAddress.getByName("localhost")); - serverSocket.close(); - return true; - } - catch (Exception ex) { - return false; - } - } -} diff --git a/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/source/FunctionAutoConfigurationIntegrationTests.java b/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/source/FunctionAutoConfigurationIntegrationTests.java index f2cfe400d..73f768b4d 100644 --- a/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/source/FunctionAutoConfigurationIntegrationTests.java +++ b/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/source/FunctionAutoConfigurationIntegrationTests.java @@ -34,11 +34,11 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.TestConfiguration; -import org.springframework.cloud.function.web.TestSocketUtils; import org.springframework.cloud.function.web.source.FunctionAutoConfigurationIntegrationTests.ApplicationConfiguration; import org.springframework.cloud.function.web.source.FunctionAutoConfigurationIntegrationTests.RestConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.http.ResponseEntity; +import org.springframework.test.util.TestSocketUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; diff --git a/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/source/FunctionAutoConfigurationWithRetriesIntegrationTests.java b/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/source/FunctionAutoConfigurationWithRetriesIntegrationTests.java index 4ded82a1c..e7957e32b 100644 --- a/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/source/FunctionAutoConfigurationWithRetriesIntegrationTests.java +++ b/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/source/FunctionAutoConfigurationWithRetriesIntegrationTests.java @@ -32,11 +32,11 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.TestConfiguration; -import org.springframework.cloud.function.web.TestSocketUtils; import org.springframework.cloud.function.web.source.FunctionAutoConfigurationWithRetriesIntegrationTests.ApplicationConfiguration; import org.springframework.cloud.function.web.source.FunctionAutoConfigurationWithRetriesIntegrationTests.RestConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.http.ResponseEntity; +import org.springframework.test.util.TestSocketUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; diff --git a/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/source/WebAppIntegrationTests.java b/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/source/WebAppIntegrationTests.java index a768fc537..261ca50bf 100644 --- a/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/source/WebAppIntegrationTests.java +++ b/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/source/WebAppIntegrationTests.java @@ -35,9 +35,9 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.cloud.function.web.RestApplication; -import org.springframework.cloud.function.web.TestSocketUtils; import org.springframework.cloud.function.web.source.WebAppIntegrationTests.ApplicationConfiguration; import org.springframework.context.annotation.Bean; +import org.springframework.test.util.TestSocketUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController;