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 10:24:44 UTC
[incubator-plc4x] 01/02: fixed ReadLength implementation
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 d0e34452450a8b76bf033aa85e8cd30746d4aad1
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri Feb 2 11:21:46 2018 +0100
fixed ReadLength implementation
---
.../java/ads/api/commands/types/ReadLength.java | 19 +++++++-
.../ads/api/commands/types/ReadLengthTest.java | 55 ++++++++++++++++++++++
2 files changed, 72 insertions(+), 2 deletions(-)
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/ReadLength.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/ReadLength.java
index 0917ebf..9f48a2b 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/ReadLength.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/ReadLength.java
@@ -31,11 +31,26 @@ public class ReadLength extends ByteValue {
assertLength(NUM_BYTES);
}
- public static ReadLength of(int length) {
- return new ReadLength(ByteBuffer.allocate(NUM_BYTES).putInt(length).array());
+ public static ReadLength of(long length) {
+ checkUnsignedBounds(length, NUM_BYTES);
+ return new ReadLength(ByteBuffer.allocate(NUM_BYTES)
+ .put((byte) (length >> 24 & 0xff))
+ .put((byte) (length >> 16 & 0xff))
+ .put((byte) (length >> 8 & 0xff))
+ .put((byte) (length & 0xff))
+ .array());
+ }
+
+ public static ReadLength of(String length) {
+ return of(Long.parseLong(length));
}
public static ReadLength of(byte... values) {
return new ReadLength(values);
}
+
+ @Override
+ public String toString() {
+ return "" + (getBytes()[0] << 24 | getBytes()[1] << 16 | getBytes()[2] << 8 | getBytes()[3]);
+ }
}
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
new file mode 100644
index 0000000..1c93d05
--- /dev/null
+++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/ReadLengthTest.java
@@ -0,0 +1,55 @@
+/*
+ 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.commands.types;
+
+import org.apache.commons.codec.binary.Hex;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+class ReadReadLengthTest {
+ 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));
+ }
+
+ @Test
+ void ofLong() {
+ assertByte(ReadLength.of(1), "0x00000001");
+ assertByte(ReadLength.of(65535), "0x0000ffff");
+ Assertions.assertThrows(IllegalArgumentException.class, () -> ReadLength.of(-1));
+ Assertions.assertThrows(IllegalArgumentException.class, () -> ReadLength.of(Long.valueOf("4294967296")));
+ }
+
+ @Test
+ void ofString() {
+ assertByte(ReadLength.of("1"), "0x00000001");
+ }
+
+ @Test
+ void testToString() {
+ Assertions.assertEquals(ReadLength.of("1").toString(), "1");
+ }
+
+ void assertByte(ReadLength 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.