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/07/26 12:22:25 UTC
[incubator-plc4x] 02/02: supported data type tests: increase
readability by introducing typed class
This is an automated email from the ASF dual-hosted git repository.
sruehl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git
commit 5d06aa7f61146d130f8a1cbe883bacfd41008bdf
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Jul 26 14:22:20 2018 +0200
supported data type tests: increase readability by introducing typed class
---
.../java/ads/protocol/Plc4x2AdsProtocolTest.java | 6 +-
.../base/protocol/Plc4XSupportedDataTypes.java | 94 +++++++++++++++-------
.../java/modbus/netty/Plc4XModbusProtocolTest.java | 22 ++---
3 files changed, 81 insertions(+), 41 deletions(-)
diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/Plc4x2AdsProtocolTest.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/Plc4x2AdsProtocolTest.java
index 4c5384e..1d97a02 100644
--- a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/Plc4x2AdsProtocolTest.java
+++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/Plc4x2AdsProtocolTest.java
@@ -105,7 +105,7 @@ public class Plc4x2AdsProtocolTest {
new PlcRequestContainer<>(
PlcWriteRequest
.builder()
- .addItem(AdsAddress.of(1, 2), pair.getLeft())
+ .addItem(AdsAddress.of(1, 2), pair.getValue())
.build(), new CompletableFuture<>()),
AdsWriteResponse.of(targetAmsNetId, targetAmsPort, sourceAmsNetId, sourceAmsPort, invokeId, Result.of(0))
),
@@ -113,9 +113,9 @@ public class Plc4x2AdsProtocolTest {
new PlcRequestContainer<>(
PlcReadRequest
.builder()
- .addItem(pair.getLeft().getClass(), AdsAddress.of(1, 2))
+ .addItem(pair.getDataTypeClass(), AdsAddress.of(1, 2))
.build(), new CompletableFuture<>()),
- AdsReadResponse.of(targetAmsNetId, targetAmsPort, sourceAmsNetId, sourceAmsPort, invokeId, Result.of(0), Data.of(pair.getRight()))
+ AdsReadResponse.of(targetAmsNetId, targetAmsPort, sourceAmsNetId, sourceAmsPort, invokeId, Result.of(0), Data.of(pair.getByteRepresentation()))
)
))
.flatMap(stream -> stream)
diff --git a/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/base/protocol/Plc4XSupportedDataTypes.java b/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/base/protocol/Plc4XSupportedDataTypes.java
index 132edf5..f0c966c 100644
--- a/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/base/protocol/Plc4XSupportedDataTypes.java
+++ b/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/base/protocol/Plc4XSupportedDataTypes.java
@@ -19,6 +19,7 @@
package org.apache.plc4x.java.base.protocol;
import org.apache.commons.lang3.ArrayUtils;
+import org.apache.commons.lang3.SerializationUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
@@ -32,32 +33,32 @@ import static org.junit.Assert.assertThat;
public class Plc4XSupportedDataTypes {
- private final static Map<Class, Pair<? extends Serializable, byte[]>> littleEndianMap;
- private final static Map<Class, Pair<? extends Serializable, byte[]>> bigEndianMap;
+ private final static Map<Class, DataTypePair<?>> littleEndianMap;
+ private final static Map<Class, DataTypePair<?>> bigEndianMap;
static {
Calendar calenderInstance = Calendar.getInstance();
calenderInstance.setTime(new Date(283686951976960L));
littleEndianMap = new HashMap<>();
- littleEndianMap.put(Boolean.class, ImmutablePair.of(Boolean.TRUE, new byte[]{0x01}));
- littleEndianMap.put(Byte.class, ImmutablePair.of(Byte.valueOf("1"), new byte[]{0x1}));
- littleEndianMap.put(Short.class, ImmutablePair.of(Short.valueOf("1"), new byte[]{0x1, 0x0}));
- littleEndianMap.put(Float.class, ImmutablePair.of(Float.valueOf("1"), new byte[]{0x0, 0x0, (byte) 0x80, 0x3F}));
- littleEndianMap.put(Integer.class, ImmutablePair.of(Integer.valueOf("1"), new byte[]{0x1, 0x0, 0x0, 0x0}));
- littleEndianMap.put(Double.class, ImmutablePair.of(Double.valueOf("1"), new byte[]{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, (byte) 0xF0, 0x3F}));
- littleEndianMap.put(BigInteger.class, ImmutablePair.of(BigInteger.valueOf(1), new byte[]{0x1, 0x0, 0x0, 0x0}));
- littleEndianMap.put(Calendar.class, ImmutablePair.of(calenderInstance, new byte[]{0x0, (byte) 0x80, 0x3E, 0x15, (byte) 0xAB, 0x47, (byte) 0xFC, 0x28}));
+ littleEndianMap.put(Boolean.class, DataTypePair.of(Boolean.TRUE, new byte[]{0x01}));
+ littleEndianMap.put(Byte.class, DataTypePair.of(Byte.valueOf("1"), new byte[]{0x1}));
+ littleEndianMap.put(Short.class, DataTypePair.of(Short.valueOf("1"), new byte[]{0x1, 0x0}));
+ littleEndianMap.put(Float.class, DataTypePair.of(Float.valueOf("1"), new byte[]{0x0, 0x0, (byte) 0x80, 0x3F}));
+ littleEndianMap.put(Integer.class, DataTypePair.of(Integer.valueOf("1"), new byte[]{0x1, 0x0, 0x0, 0x0}));
+ littleEndianMap.put(Double.class, DataTypePair.of(Double.valueOf("1"), new byte[]{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, (byte) 0xF0, 0x3F}));
+ littleEndianMap.put(BigInteger.class, DataTypePair.of(BigInteger.valueOf(1), new byte[]{0x1, 0x0, 0x0, 0x0}));
+ littleEndianMap.put(Calendar.class, DataTypePair.of(calenderInstance, new byte[]{0x0, (byte) 0x80, 0x3E, 0x15, (byte) 0xAB, 0x47, (byte) 0xFC, 0x28}));
littleEndianMap.put(GregorianCalendar.class, littleEndianMap.get(Calendar.class));
- littleEndianMap.put(String.class, ImmutablePair.of(String.valueOf("Hello World!"), new byte[]{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00}));
- littleEndianMap.put(byte[].class, ImmutablePair.of(new byte[]{0x1, 0x2, 0x3, 0x4}, new byte[]{0x1, 0x2, 0x3, 0x4}));
- littleEndianMap.put(Byte[].class, ImmutablePair.of(new Byte[]{0x1, 0x2, 0x3, 0x4}, new byte[]{0x1, 0x2, 0x3, 0x4}));
+ littleEndianMap.put(String.class, DataTypePair.of(String.valueOf("Hello World!"), new byte[]{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00}));
+ littleEndianMap.put(byte[].class, DataTypePair.of(new byte[]{0x1, 0x2, 0x3, 0x4}, new byte[]{0x1, 0x2, 0x3, 0x4}));
+ littleEndianMap.put(Byte[].class, DataTypePair.of(new Byte[]{0x1, 0x2, 0x3, 0x4}, new byte[]{0x1, 0x2, 0x3, 0x4}));
bigEndianMap = new HashMap<>();
littleEndianMap.forEach((clazz, pair) -> {
- Serializable serializable = pair.getLeft();
- byte[] littleEndianBytes = pair.getRight();
+ Serializable serializable = pair.getValue();
+ byte[] littleEndianBytes = pair.getByteRepresentation();
byte[] bigEndianBytes = ArrayUtils.clone(littleEndianBytes);
ArrayUtils.reverse(bigEndianBytes);
- bigEndianMap.put(clazz, ImmutablePair.of(serializable, bigEndianBytes));
+ bigEndianMap.put(clazz, DataTypePair.of(serializable, bigEndianBytes));
});
}
@@ -85,20 +86,20 @@ public class Plc4XSupportedDataTypes {
/**
* A {@link Stream} of instances of {@link Class}es plc4x can currently support with their according little endian byte representation.
*
- * @return a stream of {@link org.apache.commons.lang3.tuple.Pair}s of instances and their byte values.
+ * @return a stream of {@link DataTypePair}s of instances and their byte values.
* @see #streamOfPlc4XSupportedDataTypes
*/
- public static Stream<? extends Pair<? extends Serializable, byte[]>> streamOfLittleEndianDataTypePairs() {
+ public static Stream<? extends DataTypePair<?>> streamOfLittleEndianDataTypePairs() {
return streamOfLittleEndianDataTypePairs(streamOfPlc4XSupportedDataTypes());
}
/**
* A {@link Stream} of instances of {@link Class}es which are defined by {@code inputStream} can currently support with their according little endian byte representation.
*
- * @param inputStream a stream of {@link org.apache.commons.lang3.tuple.Pair}s of instances and their byte values.
+ * @param inputStream a stream of {@link DataTypePair}s of instances and their byte values.
* @see #streamOfPlc4XSupportedDataTypes
*/
- public static Stream<? extends Pair<? extends Serializable, byte[]>> streamOfLittleEndianDataTypePairs(Stream<Class<? extends Serializable>> inputStream) {
+ public static Stream<? extends DataTypePair<?>> streamOfLittleEndianDataTypePairs(Stream<Class<? extends Serializable>> inputStream) {
return inputStream
.map(littleEndianMap::get)
.peek(Objects::requireNonNull);
@@ -107,20 +108,20 @@ public class Plc4XSupportedDataTypes {
/**
* A {@link Stream} of instances of {@link Class}es plc4x can currently support with their according big endian byte representation.
*
- * @return a stream of {@link org.apache.commons.lang3.tuple.Pair}s of instances and their byte values.
+ * @return a stream of {@link DataTypePair}s of instances and their byte values.
* @see #streamOfPlc4XSupportedDataTypes
*/
- public static Stream<? extends Pair<? extends Serializable, byte[]>> streamOfBigEndianDataTypePairs() {
+ public static Stream<? extends DataTypePair<?>> streamOfBigEndianDataTypePairs() {
return streamOfBigEndianDataTypePairs(streamOfPlc4XSupportedDataTypes());
}
/**
* A {@link Stream} of instances of {@link Class}es which are defined by {@code inputStream} can currently support with their according big endian byte representation.
*
- * @param inputStream a stream of {@link org.apache.commons.lang3.tuple.Pair}s of instances and their byte values.
+ * @param inputStream a stream of {@link DataTypePair}s of instances and their byte values.
* @see #streamOfPlc4XSupportedDataTypes
*/
- public static Stream<? extends Pair<? extends Serializable, byte[]>> streamOfBigEndianDataTypePairs(Stream<Class<? extends Serializable>> inputStream) {
+ public static Stream<? extends DataTypePair<?>> streamOfBigEndianDataTypePairs(Stream<Class<? extends Serializable>> inputStream) {
return inputStream
.map(bigEndianMap::get)
.peek(Objects::requireNonNull);
@@ -135,11 +136,11 @@ public class Plc4XSupportedDataTypes {
*/
@SuppressWarnings("unchecked")
public static <T> Optional<T> getDefaultForClass(Class<T> clazz) {
- Pair<? extends Serializable, byte[]> pair = littleEndianMap.get(clazz);
+ DataTypePair<?> pair = littleEndianMap.get(clazz);
if (pair == null) {
return Optional.empty();
}
- return Optional.of((T) pair.getLeft());
+ return Optional.of((T) pair.getValue());
}
/**
@@ -148,7 +149,7 @@ public class Plc4XSupportedDataTypes {
* @param actualValue the value to check.
*/
public static void defaultAssert(Object actualValue) {
- littleEndianMap.values().forEach(pair -> assertPayloadDependentEquals(actualValue, pair.getLeft()));
+ littleEndianMap.values().forEach(pair -> assertPayloadDependentEquals(actualValue, pair.getValue()));
}
private static void assertPayloadDependentEquals(Object actual, Object expected) {
@@ -157,4 +158,43 @@ public class Plc4XSupportedDataTypes {
}
assertThat(actual, equalTo(expected));
}
+
+ /**
+ * An wrapper for {@link Pair} that make the usage a bit more readable downstream.
+ *
+ * @param <T> the type of the contained data type.
+ */
+ public static class DataTypePair<T extends Serializable> {
+ private final Pair<T, byte[]> dataTypePair;
+
+ private DataTypePair(Pair<T, byte[]> dataTypePair) {
+ this.dataTypePair = dataTypePair;
+ }
+
+ private static <T extends Serializable> DataTypePair<T> of(T value, byte[] bytes) {
+ return new DataTypePair<>(ImmutablePair.of(value, bytes));
+ }
+
+ /**
+ * @return the value of the data type.
+ */
+ public T getValue() {
+ return SerializationUtils.clone(dataTypePair.getLeft());
+ }
+
+ /**
+ * @return the {@link Class} of the data type.
+ */
+ public Class<?> getDataTypeClass() {
+ return dataTypePair.getLeft().getClass();
+ }
+
+ /**
+ * @return The byte representation of the data type according to the endianness.
+ */
+ public byte[] getByteRepresentation() {
+ return ArrayUtils.clone(dataTypePair.getRight());
+ }
+
+ }
}
diff --git a/plc4j/protocols/modbus/src/test/java/org/apache/plc4x/java/modbus/netty/Plc4XModbusProtocolTest.java b/plc4j/protocols/modbus/src/test/java/org/apache/plc4x/java/modbus/netty/Plc4XModbusProtocolTest.java
index 2cb5a06..12944e3 100644
--- a/plc4j/protocols/modbus/src/test/java/org/apache/plc4x/java/modbus/netty/Plc4XModbusProtocolTest.java
+++ b/plc4j/protocols/modbus/src/test/java/org/apache/plc4x/java/modbus/netty/Plc4XModbusProtocolTest.java
@@ -98,19 +98,19 @@ public class Plc4XModbusProtocolTest {
@Parameterized.Parameters(name = "{index} Type:{0} {3} {5}")
public static Collection<Object[]> data() {
return streamOfBigEndianDataTypePairs()
- .map(pair -> Stream.of(
- producePair(pair.getLeft().getClass(), CoilModbusAddress.of("coil:1"), new ReadCoilsResponse(Unpooled.wrappedBuffer(new byte[]{(byte) 0x1}))),
- producePair(CoilModbusAddress.of("coil:1"), new WriteSingleCoilResponse(1, 1), pair.getLeft()),
+ .map(dataTypePair -> Stream.of(
+ producePair(dataTypePair.getDataTypeClass(), CoilModbusAddress.of("coil:1"), new ReadCoilsResponse(Unpooled.wrappedBuffer(new byte[]{(byte) 0x1}))),
+ producePair(CoilModbusAddress.of("coil:1"), new WriteSingleCoilResponse(1, 1), dataTypePair.getValue()),
/* Read request no supported on maskwrite so how to handle?
- producePair(pair.getLeft().getClass(), MaskWriteRegisterModbusAddress.of("maskwrite:1/1/2"), new MaskWriteRegisterResponse(1, 1, 2)),
+ producePair(pair.getDataTypeClass(), MaskWriteRegisterModbusAddress.of("maskwrite:1/1/2"), new MaskWriteRegisterResponse(1, 1, 2)),
*/
- producePair(MaskWriteRegisterModbusAddress.of("maskwrite:1/1/2"), new MaskWriteRegisterResponse(1, 1, 2), pair.getLeft()),
- producePair(pair.getLeft().getClass(), ReadDiscreteInputsModbusAddress.of("readdiscreteinputs:1"), new ReadDiscreteInputsResponse(Unpooled.wrappedBuffer(new byte[]{(byte) 0x01}))),
- producePair(pair.getLeft().getClass(), ReadHoldingRegistersModbusAddress.of("readholdingregisters:1"), new ReadHoldingRegistersResponse(Unpooled.wrappedBuffer(cutRegister(pair.getRight())))),
- producePair(pair.getLeft().getClass(), ReadInputRegistersModbusAddress.of("readinputregisters:1"), new ReadInputRegistersResponse(Unpooled.wrappedBuffer(cutRegister(pair.getRight())))),
- producePair(CoilModbusAddress.of("coil:1"), new WriteMultipleCoilsResponse(1, 3), pair.getLeft(), pair.getLeft(), pair.getLeft()),
- producePair(RegisterModbusAddress.of("register:1"), new WriteMultipleRegistersResponse(1, 3), pair.getLeft(), pair.getLeft(), pair.getLeft()),
- producePair(RegisterModbusAddress.of("register:1"), new WriteSingleRegisterResponse(1, cutRegister(pair.getRight())[0]), pair.getLeft())
+ producePair(MaskWriteRegisterModbusAddress.of("maskwrite:1/1/2"), new MaskWriteRegisterResponse(1, 1, 2), dataTypePair.getValue()),
+ producePair(dataTypePair.getDataTypeClass(), ReadDiscreteInputsModbusAddress.of("readdiscreteinputs:1"), new ReadDiscreteInputsResponse(Unpooled.wrappedBuffer(new byte[]{(byte) 0x01}))),
+ producePair(dataTypePair.getDataTypeClass(), ReadHoldingRegistersModbusAddress.of("readholdingregisters:1"), new ReadHoldingRegistersResponse(Unpooled.wrappedBuffer(cutRegister(dataTypePair.getByteRepresentation())))),
+ producePair(dataTypePair.getDataTypeClass(), ReadInputRegistersModbusAddress.of("readinputregisters:1"), new ReadInputRegistersResponse(Unpooled.wrappedBuffer(cutRegister(dataTypePair.getByteRepresentation())))),
+ producePair(CoilModbusAddress.of("coil:1"), new WriteMultipleCoilsResponse(1, 3), dataTypePair.getValue(), dataTypePair.getValue(), dataTypePair.getValue()),
+ producePair(RegisterModbusAddress.of("register:1"), new WriteMultipleRegistersResponse(1, 3), dataTypePair.getValue(), dataTypePair.getValue(), dataTypePair.getValue()),
+ producePair(RegisterModbusAddress.of("register:1"), new WriteSingleRegisterResponse(1, cutRegister(dataTypePair.getByteRepresentation())[0]), dataTypePair.getValue())
))
.flatMap(stream -> stream)
.map(pair -> new Object[]{pair.left.getRequest().getRequestItem().orElseThrow(IllegalStateException::new).getDatatype().getSimpleName(), pair.left, pair.left.getResponseFuture(), pair.left.getRequest().getClass().getSimpleName(), pair.right, pair.right.getModbusPdu().getClass().getSimpleName()}).collect(Collectors.toList());