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 09:45:12 UTC

[incubator-plc4x] branch feature/Beckhoff_ADS_protocol updated: fixed wrong implementation of AMSPort

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 5d3ecb8  fixed wrong implementation of AMSPort
5d3ecb8 is described below

commit 5d3ecb85fa0afe392e335cdd10ffd393dfd58619
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri Feb 2 10:45:08 2018 +0100

    fixed wrong implementation of AMSPort
---
 plc4j/protocols/ads/pom.xml                        |  6 +++
 .../plc4x/java/ads/api/generic/types/AMSPort.java  | 14 +++---
 .../java/ads/api/generic/types/AMSPortTest.java    | 56 ++++++++++++++++++++++
 3 files changed, 70 insertions(+), 6 deletions(-)

diff --git a/plc4j/protocols/ads/pom.xml b/plc4j/protocols/ads/pom.xml
index 2eb013e..18e37df 100644
--- a/plc4j/protocols/ads/pom.xml
+++ b/plc4j/protocols/ads/pom.xml
@@ -78,6 +78,12 @@
       <artifactId>commons-io</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>commons-codec</groupId>
+      <artifactId>commons-codec</artifactId>
+      <version>1.11</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
 </project>
\ No newline at end of file
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/AMSPort.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/AMSPort.java
index 5d30f96..64b30d5 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/AMSPort.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/AMSPort.java
@@ -38,12 +38,14 @@ public class AMSPort extends ByteValue {
         return new AMSPort(values);
     }
 
-    public static AMSPort of(short port) {
-        return new AMSPort(ByteBuffer.allocate(NUM_BYTES).putShort(port).array());
-    }
-
     public static AMSPort of(int port) {
-        return new AMSPort(ByteBuffer.allocate(NUM_BYTES).put((byte) (port & 0xffff)).array());
+        if (port < 0 || port > 65535) {
+            throw new IllegalArgumentException("Value must between 0 and 65535");
+        }
+        return new AMSPort(ByteBuffer.allocate(NUM_BYTES)
+            .put((byte) (port >> 8 & 0xff))
+            .put((byte) (port & 0xff))
+            .array());
     }
 
     public static AMSPort of(String port) {
@@ -55,6 +57,6 @@ public class AMSPort extends ByteValue {
 
     @Override
     public String toString() {
-        return "" + getBytes()[0];
+        return "" + (getBytes()[0] << 8 | getBytes()[1]);
     }
 }
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
new file mode 100644
index 0000000..7cf79b8
--- /dev/null
+++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/generic/types/AMSPortTest.java
@@ -0,0 +1,56 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you 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
+
+   http://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.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;
+
+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));
+    }
+
+    @Test
+    void ofInt() {
+        assertByte(AMSPort.of(1), "0x0001");
+        assertByte(AMSPort.of(65535), "0xffff");
+        Assertions.assertThrows(IllegalArgumentException.class, () -> AMSPort.of(-1));
+        Assertions.assertThrows(IllegalArgumentException.class, () -> AMSPort.of(65536));
+    }
+
+    @Test
+    void ofString() {
+        assertByte(AMSPort.of("1"), "0x0001");
+    }
+
+    @Test
+    void testToString() {
+        Assertions.assertEquals(AMSPort.of("1").toString(), "1");
+    }
+
+    void assertByte(AMSPort actual, String expected) {
+        Assertions.assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes()));
+    }
+}
\ No newline at end of file

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