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 234c1211e54ebdbf3dda83847b0146ead42da209
Author: Sebastian Rühl <sru...@apache.org>
AuthorDate: Thu Jul 19 15:23:15 2018 +0200

    refactored common used test code into driver-bases-test
---
 .../java/ads/protocol/Plc4x2AdsProtocolTest.java   |  37 +------
 .../base/protocol/Plc4XSupportedDataTypes.java     | 118 +++++++++++++++++++++
 .../java/modbus/netty/Plc4XModbusProtocolTest.java |  63 +++--------
 3 files changed, 140 insertions(+), 78 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 4d456bd..6a921c4 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
@@ -51,6 +51,7 @@ import java.util.concurrent.atomic.AtomicLong;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
+import static 
org.apache.plc4x.java.base.protocol.Plc4XSupportedDataTypes.streamOfLittleEndianDataTypePairs;
 import static org.hamcrest.Matchers.*;
 import static org.junit.Assert.assertThat;
 
@@ -87,41 +88,13 @@ public class Plc4x2AdsProtocolTest {
         AmsNetId sourceAmsNetId = AmsNetId.of("8.9.10.11.12.13");
         AmsPort sourceAmsPort = AmsPort.of(14);
         Invoke invokeId = Invoke.of(2);
-        return Stream.of(Boolean.class,
-            Byte.class,
-            Short.class,
-            Calendar.class,
-            Float.class,
-            Double.class,
-            Integer.class,
-            String.class)
-            .map(clazz -> {
-                if (clazz == Boolean.class) {
-                    return ImmutablePair.of(Boolean.TRUE, new byte[]{0x01});
-                } else if (clazz == Byte.class) {
-                    return ImmutablePair.of(Byte.valueOf("1"), new 
byte[]{0x1});
-                } else if (clazz == Short.class) {
-                    return ImmutablePair.of(Short.valueOf("1"), new 
byte[]{0x1, 0x0});
-                } else if (clazz == Calendar.class) {
-                    return ImmutablePair.of(calenderInstance, new byte[]{0x0, 
0x0, 0x0, 0x0, 0x4, 0x3, 0x2, 0x1});
-                } else if (clazz == Float.class) {
-                    return ImmutablePair.of(Float.valueOf("1"), new 
byte[]{0x0, 0x0, (byte) 0x80, 0x3F});
-                } else if (clazz == Double.class) {
-                    return ImmutablePair.of(Double.valueOf("1"), new 
byte[]{0x0, 0x0,0x0, 0x0, 0x0, 0x0, (byte) 0xF0, 0x3F});
-                } else if (clazz == Integer.class) {
-                    return ImmutablePair.of(Integer.valueOf("1"), new 
byte[]{0x1, 0x0, 0x0, 0x0});
-                } else if (clazz == String.class) {
-                    return ImmutablePair.of(String.valueOf("Hello World!"), 
new byte[]{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 
0x21, 0x00});
-                } else {
-                    throw new IllegalArgumentException("Unmapped type " + 
clazz);
-                }
-            })
+        return streamOfLittleEndianDataTypePairs()
             .map(pair -> Stream.of(
                 ImmutablePair.of(
                     new PlcRequestContainer<>(
                         PlcWriteRequest
                             .builder()
-                            .addItem(AdsAddress.of(1, 2), pair.left)
+                            .addItem(AdsAddress.of(1, 2), pair.getLeft())
                             .build(), new CompletableFuture<>()),
                     AdsWriteResponse.of(targetAmsNetId, targetAmsPort, 
sourceAmsNetId, sourceAmsPort, invokeId, Result.of(0))
                 ),
@@ -129,9 +102,9 @@ public class Plc4x2AdsProtocolTest {
                     new PlcRequestContainer<>(
                         PlcReadRequest
                             .builder()
-                            .addItem(pair.left.getClass(), AdsAddress.of(1, 2))
+                            .addItem(pair.getLeft().getClass(), 
AdsAddress.of(1, 2))
                             .build(), new CompletableFuture<>()),
-                    AdsReadResponse.of(targetAmsNetId, targetAmsPort, 
sourceAmsNetId, sourceAmsPort, invokeId, Result.of(0), Data.of(pair.right))
+                    AdsReadResponse.of(targetAmsNetId, targetAmsPort, 
sourceAmsNetId, sourceAmsPort, invokeId, Result.of(0), Data.of(pair.getRight()))
                 )
             ))
             .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
new file mode 100644
index 0000000..b7be8cf
--- /dev/null
+++ 
b/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/base/protocol/Plc4XSupportedDataTypes.java
@@ -0,0 +1,118 @@
+/*
+ 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.base.protocol;
+
+import org.apache.commons.lang3.ArrayUtils;
+import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.commons.lang3.tuple.Pair;
+
+import java.io.Serializable;
+import java.util.*;
+import java.util.stream.Stream;
+
+@SuppressWarnings("unused")
+public class Plc4XSupportedDataTypes {
+
+    private final static Map<Class, Pair<? extends Serializable, byte[]>> 
littleEndianMap;
+    private final static Map<Class, Pair<? extends Serializable, byte[]>> 
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(Calendar.class, ImmutablePair.of(calenderInstance, 
new byte[]{0x0, 0x0, 0x0, 0x0, 0x4, 0x3, 0x2, 0x1}));
+        littleEndianMap.put(Float.class, ImmutablePair.of(Float.valueOf("1"), 
new byte[]{0x0, 0x0, (byte) 0x80, 0x3F}));
+        littleEndianMap.put(Double.class, 
ImmutablePair.of(Double.valueOf("1"), new byte[]{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
(byte) 0xF0, 0x3F}));
+        littleEndianMap.put(Integer.class, 
ImmutablePair.of(Integer.valueOf("1"), new byte[]{0x1, 0x0, 0x0, 0x0}));
+        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}));
+        bigEndianMap = new HashMap<>();
+        littleEndianMap.forEach((clazz, pair) -> {
+            Serializable serializable = pair.getLeft();
+            byte[] littleEndianBytes = pair.getRight();
+            byte[] bigEndianBytes = ArrayUtils.clone(littleEndianBytes);
+            ArrayUtils.reverse(bigEndianBytes);
+            bigEndianMap.put(clazz, ImmutablePair.of(serializable, 
bigEndianBytes));
+        });
+    }
+
+    /**
+     * A {@link Stream} of {@link Class}es plc4x can currently support.
+     *
+     * @return a stream of supported data types.
+     */
+    public static Stream<Class<? extends Serializable>> 
streamOfPlc4XSupportedDataTypes() {
+        return Stream.of(
+            Boolean.class,
+            Byte.class,
+            Short.class,
+            Calendar.class,
+            Float.class,
+            Double.class,
+            Integer.class,
+            String.class
+        );
+    }
+
+    /**
+     * A {@link Stream} of instances of {@link Class}es plc4x can currently 
support with their according byte representation.
+     *
+     * @return a stream of {@link org.apache.commons.lang3.tuple.Pair}s of 
instances and their byte values.
+     * @see #streamOfPlc4XSupportedDataTypes
+     */
+    public static Stream<? extends Pair<? extends Serializable, byte[]>> 
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 byte 
representation.
+     *
+     * @param inputStream a stream of {@link 
org.apache.commons.lang3.tuple.Pair}s of instances and their byte values.
+     * @see #streamOfPlc4XSupportedDataTypes
+     */
+    public static Stream<? extends Pair<? extends Serializable, byte[]>> 
streamOfLittleEndianDataTypePairs(Stream<Class<? extends Serializable>> 
inputStream) {
+        return inputStream
+            .map(littleEndianMap::get)
+            .peek(Objects::requireNonNull);
+    }
+
+    /**
+     * 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.
+     * @see #streamOfPlc4XSupportedDataTypes
+     */
+    public static Stream<? extends Pair<? extends Serializable, byte[]>> 
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.
+     * @see #streamOfPlc4XSupportedDataTypes
+     */
+    public static Stream<? extends Pair<? extends Serializable, byte[]>> 
streamOfBigEndianDataTypePairs(Stream<Class<? extends Serializable>> 
inputStream) {
+        return inputStream
+            .map(bigEndianMap::get)
+            .peek(Objects::requireNonNull);
+    }
+}
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 6aa5863..544baa5 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
@@ -49,6 +49,7 @@ import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
+import static 
org.apache.plc4x.java.base.protocol.Plc4XSupportedDataTypes.streamOfLittleEndianDataTypePairs;
 import static org.apache.plc4x.java.base.util.Assert.assertByteEquals;
 import static org.hamcrest.Matchers.*;
 import static org.junit.Assert.assertEquals;
@@ -94,53 +95,23 @@ public class Plc4XModbusProtocolTest {
 
     @Parameterized.Parameters(name = "{index} Type:{0} {3} {5}")
     public static Collection<Object[]> data() {
-        return Stream.of(
-            Boolean.class,
-            Byte.class,
-            Short.class,
-            Calendar.class,
-            Float.class,
-            Double.class,
-            Integer.class,
-            String.class
-        )
-            .map(clazz -> {
-                if (clazz == Boolean.class) {
-                    return ImmutablePair.of(Boolean.TRUE, new byte[]{0x01});
-                } else if (clazz == Byte.class) {
-                    return ImmutablePair.of(Byte.valueOf("1"), new 
byte[]{0x1});
-                } else if (clazz == Short.class) {
-                    return ImmutablePair.of(Short.valueOf("1"), new 
byte[]{0x1, 0x0});
-                } else if (clazz == Calendar.class) {
-                    return ImmutablePair.of(calenderInstance, new byte[]{0x0, 
0x0, 0x0, 0x0, 0x4, 0x3, 0x2, 0x1});
-                } else if (clazz == Float.class) {
-                    return ImmutablePair.of(Float.valueOf("1"), new 
byte[]{0x0, 0x0, (byte) 0x80, 0x3F});
-                } else if (clazz == Double.class) {
-                    return ImmutablePair.of(Double.valueOf("1"), new 
byte[]{0x0, 0x0, 0x0, 0x0, (byte) 0xF0, 0x3F});
-                } else if (clazz == Integer.class) {
-                    return ImmutablePair.of(Integer.valueOf("1"), new 
byte[]{0x1, 0x0, 0x0, 0x0});
-                } else if (clazz == String.class) {
-                    return ImmutablePair.of(String.valueOf("Hello World!"), 
new byte[]{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 
0x21, 0x00});
-                } else {
-                    throw new IllegalArgumentException("Unmapped type " + 
clazz);
-                }
-            })
+        return streamOfLittleEndianDataTypePairs()
             .map(pair -> Stream.of(
                 ImmutablePair.of(
                     new PlcRequestContainer<>(
                         PlcReadRequest
                             .builder()
-                            .addItem(pair.left.getClass(), 
CoilModbusAddress.of("coil:1"))
+                            .addItem(pair.getLeft().getClass(), 
CoilModbusAddress.of("coil:1"))
                             .build(), new CompletableFuture<>()),
-                    new ModbusTcpPayload((short) 0, (short) 0, new 
ReadCoilsResponse(Unpooled.wrappedBuffer(pair.right)))
+                    new ModbusTcpPayload((short) 0, (short) 0, new 
ReadCoilsResponse(Unpooled.wrappedBuffer(pair.getRight())))
                 ),
                 ImmutablePair.of(
                     new PlcRequestContainer<>(
                         PlcWriteRequest
                             .builder()
-                            .addItem(CoilModbusAddress.of("coil:1"), pair.left)
+                            .addItem(CoilModbusAddress.of("coil:1"), 
pair.getLeft())
                             .build(), new CompletableFuture<>()),
-                    new ModbusTcpPayload((short) 0, (short) 0, new 
WriteSingleCoilResponse(1, pair.right[0]))
+                    new ModbusTcpPayload((short) 0, (short) 0, new 
WriteSingleCoilResponse(1, pair.getRight()[0]))
                 ),
                 /* Read request no supported on maskwrite so how to handle?
                 ImmutablePair.of(
@@ -155,7 +126,7 @@ public class Plc4XModbusProtocolTest {
                     new PlcRequestContainer<>(
                         PlcWriteRequest
                             .builder()
-                            
.addItem(MaskWriteRegisterModbusAddress.of("maskwrite:1/1/2"), pair.left)
+                            
.addItem(MaskWriteRegisterModbusAddress.of("maskwrite:1/1/2"), pair.getLeft())
                             .build(), new CompletableFuture<>()),
                     new ModbusTcpPayload((short) 0, (short) 0, new 
MaskWriteRegisterResponse(1, 1, 2))
                 ),
@@ -163,31 +134,31 @@ public class Plc4XModbusProtocolTest {
                     new PlcRequestContainer<>(
                         PlcReadRequest
                             .builder()
-                            .addItem(pair.left.getClass(), 
ReadDiscreteInputsModbusAddress.of("readdiscreteinputs:1"))
+                            .addItem(pair.getLeft().getClass(), 
ReadDiscreteInputsModbusAddress.of("readdiscreteinputs:1"))
                             .build(), new CompletableFuture<>()),
-                    new ModbusTcpPayload((short) 0, (short) 0, new 
ReadDiscreteInputsResponse(Unpooled.wrappedBuffer(pair.right)))
+                    new ModbusTcpPayload((short) 0, (short) 0, new 
ReadDiscreteInputsResponse(Unpooled.wrappedBuffer(pair.getRight())))
                 ),
                 ImmutablePair.of(
                     new PlcRequestContainer<>(
                         PlcReadRequest
                             .builder()
-                            .addItem(pair.left.getClass(), 
ReadHoldingRegistersModbusAddress.of("readholdingregisters:1"))
+                            .addItem(pair.getLeft().getClass(), 
ReadHoldingRegistersModbusAddress.of("readholdingregisters:1"))
                             .build(), new CompletableFuture<>()),
-                    new ModbusTcpPayload((short) 0, (short) 0, new 
ReadHoldingRegistersResponse(Unpooled.wrappedBuffer(evenUp(pair.right))))
+                    new ModbusTcpPayload((short) 0, (short) 0, new 
ReadHoldingRegistersResponse(Unpooled.wrappedBuffer(evenUp(pair.getRight()))))
                 ),
                 ImmutablePair.of(
                     new PlcRequestContainer<>(
                         PlcReadRequest
                             .builder()
-                            .addItem(pair.left.getClass(), 
ReadInputRegistersModbusAddress.of("readinputregisters:1"))
+                            .addItem(pair.getLeft().getClass(), 
ReadInputRegistersModbusAddress.of("readinputregisters:1"))
                             .build(), new CompletableFuture<>()),
-                    new ModbusTcpPayload((short) 0, (short) 0, new 
ReadInputRegistersResponse(Unpooled.wrappedBuffer(evenUp(pair.right))))
+                    new ModbusTcpPayload((short) 0, (short) 0, new 
ReadInputRegistersResponse(Unpooled.wrappedBuffer(evenUp(pair.getRight()))))
                 ),
                 ImmutablePair.of(
                     new PlcRequestContainer<>(
                         PlcWriteRequest
                             .builder()
-                            .addItem((Class) pair.left.getClass(), 
CoilModbusAddress.of("coil:1"), pair.left, pair.left, pair.left)
+                            .addItem((Class) pair.getLeft().getClass(), 
CoilModbusAddress.of("coil:1"), pair.getLeft(), pair.getLeft(), pair.getLeft())
                             .build(), new CompletableFuture<>()),
                     new ModbusTcpPayload((short) 0, (short) 0, new 
WriteMultipleCoilsResponse(1, 3))
                 ),
@@ -195,7 +166,7 @@ public class Plc4XModbusProtocolTest {
                     new PlcRequestContainer<>(
                         PlcWriteRequest
                             .builder()
-                            .addItem((Class) pair.left.getClass(), 
RegisterModbusAddress.of("register:1"), pair.left, pair.left, pair.left)
+                            .addItem((Class) pair.getLeft().getClass(), 
RegisterModbusAddress.of("register:1"), pair.getLeft(), pair.getLeft(), 
pair.getLeft())
                             .build(), new CompletableFuture<>()),
                     new ModbusTcpPayload((short) 0, (short) 0, new 
WriteMultipleCoilsResponse(1, 3))
                 ),
@@ -203,9 +174,9 @@ public class Plc4XModbusProtocolTest {
                     new PlcRequestContainer<>(
                         PlcWriteRequest
                             .builder()
-                            .addItem(RegisterModbusAddress.of("register:1"), 
pair.left)
+                            .addItem(RegisterModbusAddress.of("register:1"), 
pair.getLeft())
                             .build(), new CompletableFuture<>()),
-                    new ModbusTcpPayload((short) 0, (short) 0, new 
WriteSingleCoilResponse(1, pair.right[0]))
+                    new ModbusTcpPayload((short) 0, (short) 0, new 
WriteSingleCoilResponse(1, pair.getRight()[0]))
                 )
             ))
             .flatMap(stream -> stream)

Reply via email to