You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2018/02/02 12:57:51 UTC

[incubator-plc4x] branch feature/Beckhoff_ADS_protocol updated: added possibility to use a random port

This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch feature/Beckhoff_ADS_protocol
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git


The following commit(s) were added to refs/heads/feature/Beckhoff_ADS_protocol by this push:
     new fe4f3d9  added possibility to use a random port
fe4f3d9 is described below

commit fe4f3d9b0be44a9d591f2a54cc2228582adf88f4
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri Feb 2 13:57:46 2018 +0100

    added possibility to use a random port
---
 .../apache/plc4x/java/ads/ADSPlcDriverTest.java    |  8 +++-
 .../java/ads/util/ExtendWithTcpHexDumper.java      |  2 +-
 .../apache/plc4x/java/ads/util/TcpHexDumper.java   | 47 +++++++++++++++++++---
 3 files changed, 49 insertions(+), 8 deletions(-)

diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/ADSPlcDriverTest.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/ADSPlcDriverTest.java
index d15f55a..021de10 100644
--- a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/ADSPlcDriverTest.java
+++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/ADSPlcDriverTest.java
@@ -30,11 +30,17 @@ import org.junit.jupiter.api.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@ExtendWithTcpHexDumper(value = ADSPlcConnection.TCP_PORT, shutdownTimeout = 3)
+@ExtendWithTcpHexDumper(port = ADSPlcConnection.TCP_PORT, shutdownTimeout = 3)
 public class ADSPlcDriverTest {
 
     private static final Logger logger = LoggerFactory.getLogger(ADSPlcDriverTest.class);
 
+    int usedPort;
+
+    ADSPlcDriverTest(int port) {
+        usedPort = port;
+    }
+
     @Test
     @Tag("fast")
     void getConnection() throws Exception {
diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/util/ExtendWithTcpHexDumper.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/util/ExtendWithTcpHexDumper.java
index 32666cf..e91a020 100644
--- a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/util/ExtendWithTcpHexDumper.java
+++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/util/ExtendWithTcpHexDumper.java
@@ -30,7 +30,7 @@ import static java.lang.annotation.ElementType.*;
 @Retention(RetentionPolicy.RUNTIME)
 @ExtendWith(TcpHexDumper.class)
 public @interface ExtendWithTcpHexDumper {
-    int value();
+    int port() default 0;
 
     int shutdownTimeout() default 10;
 }
diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/util/TcpHexDumper.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/util/TcpHexDumper.java
index 2f1f420..92aa16e 100644
--- a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/util/TcpHexDumper.java
+++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/util/TcpHexDumper.java
@@ -20,9 +20,7 @@ package org.apache.plc4x.java.ads.util;
 
 import org.apache.commons.io.HexDump;
 import org.apache.commons.io.IOUtils;
-import org.junit.jupiter.api.extension.AfterEachCallback;
-import org.junit.jupiter.api.extension.BeforeEachCallback;
-import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -35,12 +33,14 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
 
-public class TcpHexDumper implements BeforeEachCallback, AfterEachCallback {
+public class TcpHexDumper implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
 
     private static final Logger logger = LoggerFactory.getLogger(TcpHexDumper.class);
 
     private ExecutorService pool = Executors.newCachedThreadPool();
 
+    private Integer portToUse;
+
     private ServerSocket serverSocket;
 
     int shutdownTimeout = 10;
@@ -96,8 +96,43 @@ public class TcpHexDumper implements BeforeEachCallback, AfterEachCallback {
 
     @Override
     public void beforeEach(ExtensionContext context) throws Exception {
+        init(initPortToUse(context));
+        shutdownTimeout = initShutdownTimeout(context);
+    }
+
+    @Override
+    public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
+        return parameterContext.getParameter().getType()
+            .equals(int.class);
+    }
+
+    @Override
+    public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
+        try {
+            return initPortToUse(extensionContext);
+        } catch (IOException e) {
+            throw new ParameterResolutionException("Could not find a free port", e);
+        }
+    }
+
+    private int initShutdownTimeout(ExtensionContext context) {
         ExtendWithTcpHexDumper annotation = context.getRequiredTestClass().getAnnotation(ExtendWithTcpHexDumper.class);
-        init(annotation.value());
-        shutdownTimeout = annotation.shutdownTimeout();
+        return annotation.shutdownTimeout();
+    }
+
+    private int initPortToUse(ExtensionContext context) throws IOException {
+        if (portToUse == null) {
+            ExtendWithTcpHexDumper annotation = context.getRequiredTestClass().getAnnotation(ExtendWithTcpHexDumper.class);
+            int port = annotation.port();
+            portToUse = port != 0 ? port : findFreePort();
+        }
+        return portToUse;
+    }
+
+    private static int findFreePort() throws IOException {
+        try (ServerSocket socket = new ServerSocket(0)) {
+            socket.setReuseAddress(true);
+            return socket.getLocalPort();
+        }
     }
 }

-- 
To stop receiving notification emails like this one, please contact
sruehl@apache.org.