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
commit 6635fa77b43a10c44a9fa3c5eb50684a2c52aca3 Author: Sebastian Rühl <sru...@apache.org> AuthorDate: Thu Feb 15 17:46:25 2018 +0100 post merge junit 4 migration --- .../java/ads/connection/ADSPlcConnection.java | 8 +++ .../apache/plc4x/java/ads/ADSPlcDriverTest.java | 38 +++++-------- .../java/ads/api/commands/types/LengthTest.java | 28 ++++----- .../ads/api/commands/types/ReadLengthTest.java | 30 +++++----- .../ads/api/commands/types/SampleSizeTest.java | 29 +++++----- .../java/ads/api/commands/types/SamplesTest.java | 28 ++++----- .../ads/api/commands/types/WriteLengthTest.java | 29 +++++----- .../java/ads/api/generic/types/AMSPortTest.java | 29 +++++----- .../java/ads/api/generic/types/DataLengthTest.java | 28 ++++----- .../java/ads/api/generic/types/LengthTest.java | 29 +++++----- .../java/ads/connection/ADSPlcConnectionTests.java | 28 ++++----- ...ndWithTcpHexDumper.java => Junit5Backport.java} | 33 +++++------ .../apache/plc4x/java/ads/util/TcpHexDumper.java | 66 +++++++--------------- 13 files changed, 195 insertions(+), 208 deletions(-) diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/connection/ADSPlcConnection.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/connection/ADSPlcConnection.java index ef584c0..3e6c9ce 100644 --- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/connection/ADSPlcConnection.java +++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/connection/ADSPlcConnection.java @@ -57,6 +57,7 @@ public class ADSPlcConnection extends AbstractPlcConnection implements PlcReader private EventLoopGroup workerGroup; private Channel channel; + private boolean connected; public ADSPlcConnection(String hostName, AMSNetId targetAmsNetId, AMSPort targetAmsPort) { this(hostName, targetAmsNetId, targetAmsPort, generateAMSNetId(), generateAMSPort()); @@ -78,6 +79,7 @@ public class ADSPlcConnection extends AbstractPlcConnection implements PlcReader this.targetAmsPort = targetAmsPort; this.sourceAmsNetId = sourceAmsNetId; this.sourceAmsPort = sourceAmsPort; + connected = false; } public String getHostName() { @@ -126,6 +128,7 @@ public class ADSPlcConnection extends AbstractPlcConnection implements PlcReader f.awaitUninterruptibly(); // Wait till the session is finished initializing. channel = f.channel(); + connected = true; } catch (UnknownHostException e) { throw new PlcConnectionException("Unknown Host " + hostName, e); } catch (InterruptedException e) { @@ -135,6 +138,11 @@ public class ADSPlcConnection extends AbstractPlcConnection implements PlcReader } @Override + public boolean isConnected() { + return connected; + } + + @Override public void close() throws Exception { workerGroup.shutdownGracefully(); } 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 ae19173..f62376a 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 @@ -21,34 +21,27 @@ package org.apache.plc4x.java.ads; import org.apache.plc4x.java.PlcDriverManager; import org.apache.plc4x.java.ads.connection.ADSPlcConnection; -import org.apache.plc4x.java.ads.util.ExtendWithTcpHexDumper; +import org.apache.plc4x.java.ads.util.TcpHexDumper; import org.apache.plc4x.java.api.exceptions.PlcConnectionException; import org.apache.plc4x.java.api.exceptions.PlcException; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Tag; -import org.junit.jupiter.api.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.junit.Rule; +import org.junit.Test; -@ExtendWithTcpHexDumper(shutdownTimeout = 3) -public class ADSPlcDriverTest { - - private static final Logger logger = LoggerFactory.getLogger(ADSPlcDriverTest.class); +import static org.apache.plc4x.java.ads.util.Junit5Backport.assertThrows; +import static org.junit.Assert.assertEquals; - int usedPort; +public class ADSPlcDriverTest { - ADSPlcDriverTest(int port) { - usedPort = port; - } + @Rule + public TcpHexDumper tcpHexDumper = new TcpHexDumper(0); @Test - @Tag("fast") - void getConnection() throws Exception { + public void getConnection() throws Exception { ADSPlcConnection adsConnection = (ADSPlcConnection) - new PlcDriverManager().getConnection("ads://localhost:" + usedPort + "/0.0.0.0.0.0:13"); - Assertions.assertEquals(adsConnection.getHostName(), "localhost"); - Assertions.assertEquals(adsConnection.getTargetAmsNetId().toString(), "0.0.0.0.0.0"); - Assertions.assertEquals(adsConnection.getTargetAmsPort().toString(), "13"); + new PlcDriverManager().getConnection("ads://localhost:" + tcpHexDumper.getPort() + "/0.0.0.0.0.0:13"); + assertEquals(adsConnection.getHostName(), "localhost"); + assertEquals(adsConnection.getTargetAmsNetId().toString(), "0.0.0.0.0.0"); + assertEquals(adsConnection.getTargetAmsPort().toString(), "13"); adsConnection.close(); } @@ -58,9 +51,8 @@ public class ADSPlcDriverTest { * @throws PlcException something went wrong */ @Test - @Tag("fast") - void getConnectionInvalidUrl() throws PlcException { - Assertions.assertThrows(PlcConnectionException.class, + public void getConnectionInvalidUrl() throws PlcException { + assertThrows(PlcConnectionException.class, () -> new PlcDriverManager().getConnection("ads://localhost/hurz/2")); } diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/LengthTest.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/LengthTest.java index c44cad4..bb25c22 100644 --- a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/LengthTest.java +++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/LengthTest.java @@ -19,38 +19,40 @@ package org.apache.plc4x.java.ads.api.commands.types; import org.apache.commons.codec.binary.Hex; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; -class LengthTest { +import static org.apache.plc4x.java.ads.util.Junit5Backport.assertThrows; +import static org.junit.Assert.assertEquals; + +public class LengthTest { byte NULL_BYTE = 0x0; @Test - void ofBytes() { - Assertions.assertEquals("0", Length.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE).toString()); - Assertions.assertThrows(IllegalArgumentException.class, () -> Length.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE)); + public void ofBytes() { + assertEquals("0", Length.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE).toString()); + assertThrows(IllegalArgumentException.class, () -> Length.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE)); } @Test - void ofLong() { + public void ofLong() { assertByte(Length.of(1), "0x01000000"); assertByte(Length.of(65535), "0xffff0000"); - Assertions.assertThrows(IllegalArgumentException.class, () -> Length.of(-1)); - Assertions.assertThrows(IllegalArgumentException.class, () -> Length.of(4294967296L)); + assertThrows(IllegalArgumentException.class, () -> Length.of(-1)); + assertThrows(IllegalArgumentException.class, () -> Length.of(4294967296L)); } @Test - void ofString() { + public void ofString() { assertByte(Length.of("1"), "0x01000000"); } @Test - void testToString() { - Assertions.assertEquals(Length.of("1").toString(), "1"); + public void testToString() { + assertEquals(Length.of("1").toString(), "1"); } void assertByte(Length actual, String expected) { - Assertions.assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes())); + assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes())); } } \ No newline at end of file diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/ReadLengthTest.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/ReadLengthTest.java index a1a2442..1858713 100644 --- a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/ReadLengthTest.java +++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/ReadLengthTest.java @@ -19,37 +19,41 @@ package org.apache.plc4x.java.ads.api.commands.types; import org.apache.commons.codec.binary.Hex; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; + +import static org.apache.plc4x.java.ads.util.Junit5Backport.assertThrows; +import static org.junit.Assert.assertEquals; + + +public class ReadLengthTest { -class ReadLengthTest { byte NULL_BYTE = 0x0; @Test - void ofBytes() { - Assertions.assertEquals("0", ReadLength.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE).toString()); - Assertions.assertThrows(IllegalArgumentException.class, () -> ReadLength.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE)); + public void ofBytes() { + assertEquals("0", ReadLength.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE).toString()); + assertThrows(IllegalArgumentException.class, () -> ReadLength.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE)); } @Test - void ofLong() { + public void ofLong() { assertByte(ReadLength.of(1), "0x01000000"); assertByte(ReadLength.of(65535), "0xffff0000"); - Assertions.assertThrows(IllegalArgumentException.class, () -> ReadLength.of(-1)); - Assertions.assertThrows(IllegalArgumentException.class, () -> ReadLength.of(4294967296L)); + assertThrows(IllegalArgumentException.class, () -> ReadLength.of(-1)); + assertThrows(IllegalArgumentException.class, () -> ReadLength.of(4294967296L)); } @Test - void ofString() { + public void ofString() { assertByte(ReadLength.of("1"), "0x01000000"); } @Test - void testToString() { - Assertions.assertEquals(ReadLength.of("1").toString(), "1"); + public void testToString() { + assertEquals(ReadLength.of("1").toString(), "1"); } void assertByte(ReadLength actual, String expected) { - Assertions.assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes())); + assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes())); } } \ No newline at end of file diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/SampleSizeTest.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/SampleSizeTest.java index a80a28a..22a128f 100644 --- a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/SampleSizeTest.java +++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/SampleSizeTest.java @@ -19,40 +19,41 @@ package org.apache.plc4x.java.ads.api.commands.types; import org.apache.commons.codec.binary.Hex; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; -class SampleSizeTest { +import static org.apache.plc4x.java.ads.util.Junit5Backport.assertThrows; +import static org.junit.Assert.assertEquals; + +public class SampleSizeTest { byte NULL_BYTE = 0x0; @Test - void ofBytes() { - Assertions.assertEquals("0", SampleSize.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE).toString()); - Assertions.assertThrows(IllegalArgumentException.class, () -> SampleSize.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE)); + public void ofBytes() { + assertEquals("0", SampleSize.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE).toString()); + assertThrows(IllegalArgumentException.class, () -> SampleSize.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE)); } @Test - void ofLong() { + public void ofLong() { assertByte(SampleSize.of(1), "0x01000000"); assertByte(SampleSize.of(65535), "0xffff0000"); - Assertions.assertThrows(IllegalArgumentException.class, () -> SampleSize.of(-1)); - Assertions.assertThrows(IllegalArgumentException.class, () -> SampleSize.of(4294967296L)); + assertThrows(IllegalArgumentException.class, () -> SampleSize.of(-1)); + assertThrows(IllegalArgumentException.class, () -> SampleSize.of(4294967296L)); } @Test - void ofString() { + public void ofString() { assertByte(SampleSize.of("1"), "0x01000000"); } @Test - void testToString() { - Assertions.assertEquals(SampleSize.of("1").toString(), "1"); + public void testToString() { + assertEquals(SampleSize.of("1").toString(), "1"); } void assertByte(SampleSize actual, String expected) { - Assertions.assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes())); + assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes())); } - } \ No newline at end of file diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/SamplesTest.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/SamplesTest.java index 49ef8d2..1434ec7 100644 --- a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/SamplesTest.java +++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/SamplesTest.java @@ -19,39 +19,41 @@ package org.apache.plc4x.java.ads.api.commands.types; import org.apache.commons.codec.binary.Hex; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; -class SamplesTest { +import static org.apache.plc4x.java.ads.util.Junit5Backport.assertThrows; +import static org.junit.Assert.assertEquals; + +public class SamplesTest { byte NULL_BYTE = 0x0; @Test - void ofBytes() { - Assertions.assertEquals("0", Samples.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE).toString()); - Assertions.assertThrows(IllegalArgumentException.class, () -> Samples.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE)); + public void ofBytes() { + assertEquals("0", Samples.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE).toString()); + assertThrows(IllegalArgumentException.class, () -> Samples.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE)); } @Test - void ofLong() { + public void ofLong() { assertByte(Samples.of(1), "0x01000000"); assertByte(Samples.of(65535), "0xffff0000"); - Assertions.assertThrows(IllegalArgumentException.class, () -> Samples.of(-1)); - Assertions.assertThrows(IllegalArgumentException.class, () -> Samples.of(4294967296L)); + assertThrows(IllegalArgumentException.class, () -> Samples.of(-1)); + assertThrows(IllegalArgumentException.class, () -> Samples.of(4294967296L)); } @Test - void ofString() { + public void ofString() { assertByte(Samples.of("1"), "0x01000000"); } @Test - void testToString() { - Assertions.assertEquals(Samples.of("1").toString(), "1"); + public void testToString() { + assertEquals(Samples.of("1").toString(), "1"); } void assertByte(Samples actual, String expected) { - Assertions.assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes())); + assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes())); } } \ No newline at end of file diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/WriteLengthTest.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/WriteLengthTest.java index d6780e2..087dc92 100644 --- a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/WriteLengthTest.java +++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/WriteLengthTest.java @@ -19,39 +19,42 @@ package org.apache.plc4x.java.ads.api.commands.types; import org.apache.commons.codec.binary.Hex; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; -class WriteLengthTest { +import static org.apache.plc4x.java.ads.util.Junit5Backport.assertThrows; +import static org.junit.Assert.assertEquals; + + +public class WriteLengthTest { byte NULL_BYTE = 0x0; @Test - void ofBytes() { - Assertions.assertEquals("0", WriteLength.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE).toString()); - Assertions.assertThrows(IllegalArgumentException.class, () -> WriteLength.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE)); + public void ofBytes() { + assertEquals("0", WriteLength.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE).toString()); + assertThrows(IllegalArgumentException.class, () -> WriteLength.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE)); } @Test - void ofLong() { + public void ofLong() { assertByte(WriteLength.of(1), "0x01000000"); assertByte(WriteLength.of(65535), "0xffff0000"); - Assertions.assertThrows(IllegalArgumentException.class, () -> WriteLength.of(-1)); - Assertions.assertThrows(IllegalArgumentException.class, () -> WriteLength.of(4294967296L)); + assertThrows(IllegalArgumentException.class, () -> WriteLength.of(-1)); + assertThrows(IllegalArgumentException.class, () -> WriteLength.of(4294967296L)); } @Test - void ofString() { + public void ofString() { assertByte(WriteLength.of("1"), "0x01000000"); } @Test - void testToString() { - Assertions.assertEquals(WriteLength.of("1").toString(), "1"); + public void testToString() { + assertEquals(WriteLength.of("1").toString(), "1"); } void assertByte(WriteLength actual, String expected) { - Assertions.assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes())); + assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes())); } diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/generic/types/AMSPortTest.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/generic/types/AMSPortTest.java index 1845349..12ee66b 100644 --- a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/generic/types/AMSPortTest.java +++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/generic/types/AMSPortTest.java @@ -19,38 +19,41 @@ package org.apache.plc4x.java.ads.api.generic.types; import org.apache.commons.codec.binary.Hex; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; -class AMSPortTest { +import static org.apache.plc4x.java.ads.util.Junit5Backport.assertThrows; +import static org.junit.Assert.assertEquals; + + +public class AMSPortTest { byte NULL_BYTE = 0x0; @Test - void ofBytes() { - Assertions.assertEquals("0", AMSPort.of(NULL_BYTE, NULL_BYTE).toString()); - Assertions.assertThrows(IllegalArgumentException.class, () -> AMSPort.of(NULL_BYTE, NULL_BYTE, NULL_BYTE)); + public void ofBytes() { + assertEquals("0", AMSPort.of(NULL_BYTE, NULL_BYTE).toString()); + assertThrows(IllegalArgumentException.class, () -> AMSPort.of(NULL_BYTE, NULL_BYTE, NULL_BYTE)); } @Test - void ofInt() { + public void ofInt() { assertByte(AMSPort.of(1), "0x0100"); assertByte(AMSPort.of(65535), "0xffff"); - Assertions.assertThrows(IllegalArgumentException.class, () -> AMSPort.of(-1)); - Assertions.assertThrows(IllegalArgumentException.class, () -> AMSPort.of(65536)); + assertThrows(IllegalArgumentException.class, () -> AMSPort.of(-1)); + assertThrows(IllegalArgumentException.class, () -> AMSPort.of(65536)); } @Test - void ofString() { + public void ofString() { assertByte(AMSPort.of("1"), "0x0100"); } @Test - void testToString() { - Assertions.assertEquals(AMSPort.of("1").toString(), "1"); + public void testToString() { + assertEquals(AMSPort.of("1").toString(), "1"); } void assertByte(AMSPort actual, String expected) { - Assertions.assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes())); + assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes())); } } \ No newline at end of file diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/generic/types/DataLengthTest.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/generic/types/DataLengthTest.java index aafab4a..fd5e6d0 100644 --- a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/generic/types/DataLengthTest.java +++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/generic/types/DataLengthTest.java @@ -19,40 +19,42 @@ package org.apache.plc4x.java.ads.api.generic.types; import org.apache.commons.codec.binary.Hex; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; +import static org.apache.plc4x.java.ads.util.Junit5Backport.assertThrows; +import static org.junit.Assert.assertEquals; -class DataLengthTest { + +public class DataLengthTest { byte NULL_BYTE = 0x0; @Test - void ofBytes() { - Assertions.assertEquals("0", DataLength.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE).toString()); - Assertions.assertThrows(IllegalArgumentException.class, () -> DataLength.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE)); + public void ofBytes() { + assertEquals("0", DataLength.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE).toString()); + assertThrows(IllegalArgumentException.class, () -> DataLength.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE)); } @Test - void ofLong() { + public void ofLong() { assertByte(DataLength.of(1), "0x01000000"); assertByte(DataLength.of(65535), "0xffff0000"); - Assertions.assertThrows(IllegalArgumentException.class, () -> DataLength.of(-1)); - Assertions.assertThrows(IllegalArgumentException.class, () -> DataLength.of(4294967296L)); + assertThrows(IllegalArgumentException.class, () -> DataLength.of(-1)); + assertThrows(IllegalArgumentException.class, () -> DataLength.of(4294967296L)); } @Test - void ofString() { + public void ofString() { assertByte(DataLength.of("1"), "0x01000000"); } @Test - void testToString() { - Assertions.assertEquals(DataLength.of("1").toString(), "1"); + public void testToString() { + assertEquals(DataLength.of("1").toString(), "1"); } void assertByte(DataLength actual, String expected) { - Assertions.assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes())); + assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes())); } diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/generic/types/LengthTest.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/generic/types/LengthTest.java index 97dab3f..5648e18 100644 --- a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/generic/types/LengthTest.java +++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/generic/types/LengthTest.java @@ -20,38 +20,41 @@ package org.apache.plc4x.java.ads.api.generic.types; import org.apache.commons.codec.binary.Hex; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; -class LengthTest { +import static org.apache.plc4x.java.ads.util.Junit5Backport.assertThrows; +import static org.junit.Assert.assertEquals; + + +public class LengthTest { byte NULL_BYTE = 0x0; @Test - void ofBytes() { - Assertions.assertEquals("0", Length.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE).toString()); - Assertions.assertThrows(IllegalArgumentException.class, () -> Length.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE)); + public void ofBytes() { + assertEquals("0", Length.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE).toString()); + assertThrows(IllegalArgumentException.class, () -> Length.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE)); } @Test - void ofLong() { + public void ofLong() { assertByte(Length.of(1), "0x01000000"); assertByte(Length.of(65535), "0xffff0000"); - Assertions.assertThrows(IllegalArgumentException.class, () -> Length.of(-1)); - Assertions.assertThrows(IllegalArgumentException.class, () -> Length.of(4294967296L)); + assertThrows(IllegalArgumentException.class, () -> Length.of(-1)); + assertThrows(IllegalArgumentException.class, () -> Length.of(4294967296L)); } @Test - void ofString() { + public void ofString() { assertByte(Length.of("1"), "0x01000000"); } @Test - void testToString() { - Assertions.assertEquals(Length.of("1").toString(), "1"); + public void testToString() { + assertEquals(Length.of("1").toString(), "1"); } void assertByte(Length actual, String expected) { - Assertions.assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes())); + assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes())); } } \ No newline at end of file diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/connection/ADSPlcConnectionTests.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/connection/ADSPlcConnectionTests.java index 78d2813..5045643 100644 --- a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/connection/ADSPlcConnectionTests.java +++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/connection/ADSPlcConnectionTests.java @@ -22,44 +22,44 @@ package org.apache.plc4x.java.ads.connection; import org.apache.plc4x.java.ads.api.generic.types.AMSNetId; import org.apache.plc4x.java.ads.api.generic.types.AMSPort; import org.apache.plc4x.java.ads.model.ADSAddress; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.Assert.*; -class ADSPlcConnectionTests { +public class ADSPlcConnectionTests { private ADSPlcConnection adsPlcConnection; - @BeforeEach - void setUp() { + @Before + public void setUp() { adsPlcConnection = new ADSPlcConnection("localhost", AMSNetId.of("0.0.0.0.0.0"), AMSPort.of(13)); } - @AfterEach - void tearDown() { + @After + public void tearDown() { adsPlcConnection = null; } @Test - void initialState() { - assertTrue(adsPlcConnection.getHostName().equalsIgnoreCase("localhost"), "Hostname is incorrect"); + public void initialState() { + assertTrue("Hostname is incorrect", adsPlcConnection.getHostName().equalsIgnoreCase("localhost")); assertEquals(adsPlcConnection.getTargetAmsNetId().toString(), "0.0.0.0.0.0"); assertEquals(adsPlcConnection.getTargetAmsPort().toString(), "13"); } @Test - void emptyParseAddress() throws Exception { + public void emptyParseAddress() throws Exception { try { adsPlcConnection.parseAddress(""); } catch (IllegalArgumentException exception) { - assertTrue(exception.getMessage().startsWith("address doesn't match "), "Unexpected exception"); + assertTrue("Unexpected exception", exception.getMessage().startsWith("address doesn't match ")); } } @Test - void parseAddress() throws Exception { + public void parseAddress() throws Exception { try { ADSAddress address = (ADSAddress) adsPlcConnection.parseAddress("1/1"); assertEquals(address.getIndexGroup(), 1); 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/Junit5Backport.java similarity index 55% rename from plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/util/ExtendWithTcpHexDumper.java rename to plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/util/Junit5Backport.java index 9be3a85..34d8783 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/Junit5Backport.java @@ -18,25 +18,20 @@ */ package org.apache.plc4x.java.ads.util; -import org.junit.jupiter.api.extension.ExtendWith; +public class Junit5Backport { -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; + public static void assertThrows(Class<? extends Exception> exception, Acceptor acceptor) { + try { + acceptor.accept(); + } catch (Exception e) { + if (!exception.isAssignableFrom(e.getClass())) { + throw new RuntimeException(e); + } + } + } -import static java.lang.annotation.ElementType.*; - -@Target({TYPE, METHOD, ANNOTATION_TYPE}) -@Retention(RetentionPolicy.RUNTIME) -@ExtendWith(TcpHexDumper.class) -public @interface ExtendWithTcpHexDumper { - /** - * Can be used to set a fixed port for the dumper, otherwise he will use a random port. - */ - int port() default 0; - - /** - * The timeout for the pool shutdown. After this no dumps will be printed anymore. - */ - int shutdownTimeout() default 10; + @FunctionalInterface + public interface Acceptor { + void accept() throws Exception; + } } 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 c0b0449..ca6a684 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,7 +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.*; +import org.junit.rules.ExternalResource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,17 +34,23 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; -public class TcpHexDumper implements BeforeEachCallback, AfterEachCallback, ParameterResolver, Closeable { +public class TcpHexDumper extends ExternalResource implements Closeable { private static final Logger logger = LoggerFactory.getLogger(TcpHexDumper.class); - private ExecutorService pool = Executors.newCachedThreadPool(); + private ExecutorService pool; - private Integer portToUse; + private final Integer portToUse; private ServerSocket serverSocket; - int shutdownTimeout = 10; + int shutdownTimeout; + + public TcpHexDumper(Integer portToUse) { + pool = Executors.newCachedThreadPool(); + this.portToUse = portToUse; + this.shutdownTimeout = 10; + } public void init(int port) throws IOException, InterruptedException { if (serverSocket != null) { @@ -99,33 +105,20 @@ public class TcpHexDumper implements BeforeEachCallback, AfterEachCallback, Para } @Override - public void afterEach(ExtensionContext context) throws Exception { - stop(true); - } - - @Override - public void beforeEach(ExtensionContext context) throws Exception { - init(initPortToUse(context)); - shutdownTimeout = initShutdownTimeout(context); + public void after() { + try { + stop(true); + } catch (IOException | InterruptedException ignore) { + } } @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); - } + public void before() throws Throwable { + init(portToUse); } public static TcpHexDumper runOn(int port) throws IOException, InterruptedException { - TcpHexDumper tcpHexDumper = new TcpHexDumper(); + TcpHexDumper tcpHexDumper = new TcpHexDumper(port); tcpHexDumper.init(port); return tcpHexDumper; } @@ -134,27 +127,6 @@ public class TcpHexDumper implements BeforeEachCallback, AfterEachCallback, Para return serverSocket.getLocalPort(); } - private int initShutdownTimeout(ExtensionContext context) { - ExtendWithTcpHexDumper annotation = context.getRequiredTestClass().getAnnotation(ExtendWithTcpHexDumper.class); - 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(); - } - } - @Override public void close() throws IOException { try { -- To stop receiving notification emails like this one, please contact sru...@apache.org.