This is an automated email from the ASF dual-hosted git repository.
orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 3bfdb9e (chores) camel-cxf: fix test failure in Camel1145RouteTest
due to fixed port usage
3bfdb9e is described below
commit 3bfdb9e6a324e7590f5ef79558d6de65f3b2ac3e
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Fri Oct 15 15:32:27 2021 +0200
(chores) camel-cxf: fix test failure in Camel1145RouteTest due to fixed
port usage
---
.../camel/component/cxf/Camel1145RouteTest.java | 16 +++++++++++++-
.../org/apache/camel/test/AvailablePortFinder.java | 25 ++++++++++++++++++++++
2 files changed, 40 insertions(+), 1 deletion(-)
diff --git
a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/Camel1145RouteTest.java
b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/Camel1145RouteTest.java
index 553b572..0e49e89 100644
---
a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/Camel1145RouteTest.java
+++
b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/Camel1145RouteTest.java
@@ -24,20 +24,34 @@ import org.apache.camel.non_wrapper.Person;
import org.apache.camel.non_wrapper.PersonService;
import org.apache.camel.non_wrapper.types.GetPerson;
import org.apache.camel.non_wrapper.types.GetPersonResponse;
+import org.apache.camel.test.AvailablePortFinder;
+import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
@ContextConfiguration(locations = {
"/org/apache/camel/component/cxf/context-camel-1145.xml" })
@ExtendWith(SpringExtension.class)
public class Camel1145RouteTest {
+ private static int port;
+
+ public static int portNotAvailableHandler(boolean allocated) {
+ assumeTrue(allocated);
+ return -1;
+ }
+
+ @BeforeAll
+ public static void getPort() {
+ port = AvailablePortFinder.getSpecificPort(9000, false,
Camel1145RouteTest::portNotAvailableHandler);
+ }
@Test
public void testCamel1145Route() throws Exception {
- URL wsdlURL = new URL("http://localhost:9000/PersonService/?wsdl");
+ URL wsdlURL = new
URL(String.format("http://localhost:%d/PersonService/?wsdl", port));
PersonService ss = new PersonService(wsdlURL, new
QName("http://camel.apache.org/non-wrapper", "PersonService"));
Person client = ss.getSoap();
GetPerson request = new GetPerson();
diff --git
a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/AvailablePortFinder.java
b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/AvailablePortFinder.java
index 8f03e57..fe11bc1 100644
---
a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/AvailablePortFinder.java
+++
b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/AvailablePortFinder.java
@@ -22,6 +22,7 @@ import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Function;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
@@ -147,6 +148,30 @@ public final class AvailablePortFinder {
}
/**
+ * Gets the next available port in the given range.
+ *
+ * @param portNumber port number start range.
+ * @param failurePayload handover data in case port allocation
fails (i.e.: a default one to use)
+ * @param failureHandler a handler in case the requested port is
not available
+ *
+ * @throws IllegalStateException if there are no ports available
+ * @return the available port
+ */
+ public static <T> int getSpecificPort(int portNumber, T failurePayload,
Function<T, Integer> failureHandler) {
+ try (Port port = INSTANCE.findPort(portNumber, portNumber)) {
+ return port.getPort();
+ } catch (IllegalStateException e) {
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Unable to obtain the requested TCP port {}: {}",
portNumber, e.getMessage(), e);
+ } else {
+ LOG.warn("Unable to obtain the requested TCP port {}: {}",
portNumber, e.getMessage());
+ }
+
+ return failureHandler.apply(failurePayload);
+ }
+ }
+
+ /**
* Probe a port to see if it is free
*
* @param port an integer port number to be tested. If
port is 0, then the next available port is