You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jf...@apache.org on 2018/11/24 19:51:22 UTC
[incubator-plc4x] 05/06: [plc4j-driver-simulated] added tests and
fixed bugs.
This is an automated email from the ASF dual-hosted git repository.
jfeinauer pushed a commit to branch feature/plc4j-scraper
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git
commit 99d8f27a5d5840e17ea87d6c2e71b64e0f0b5196
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Sat Nov 24 19:45:22 2018 +0100
[plc4j-driver-simulated] added tests and fixed bugs.
---
.../simulated/connection/TestFieldHandler.java | 6 +-
.../java/simulated/connection/TestFieldItem.java | 2 +-
.../java/simulated/SimulatedPlcDriverTest.java | 7 ++
.../java/simulated/connection/TestDeviceTest.java | 2 +-
.../simulated/connection/TestFieldHandlerTest.java | 133 +++++++++++++++++++++
.../simulated/connection/TestFieldItemTest.java} | 20 ++--
6 files changed, 153 insertions(+), 17 deletions(-)
diff --git a/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/TestFieldHandler.java b/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/TestFieldHandler.java
index 329a28e..44d7151 100644
--- a/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/TestFieldHandler.java
+++ b/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/TestFieldHandler.java
@@ -100,7 +100,7 @@ public class TestFieldHandler implements PlcFieldHandler {
public BaseDefaultFieldItem encodeFloat(PlcField field, Object[] values) {
TestField testField = (TestField) field;
if (testField.getDataType() == Float.class) {
- return new DefaultDoubleFieldItem((Double[]) values);
+ return new DefaultFloatFieldItem((Float[]) values);
}
throw new PlcRuntimeException("Invalid encoder for type " + testField.getDataType().getName());
}
@@ -136,7 +136,7 @@ public class TestFieldHandler implements PlcFieldHandler {
public BaseDefaultFieldItem encodeTime(PlcField field, Object[] values) {
TestField testField = (TestField) field;
if (testField.getDataType() == LocalTime.class) {
- return new DefaultLocalDateTimeFieldItem((LocalDateTime[]) values);
+ return new DefaultLocalTimeFieldItem((LocalTime[]) values);
}
throw new PlcRuntimeException("Invalid encoder for type " + testField.getDataType().getName());
}
@@ -145,7 +145,7 @@ public class TestFieldHandler implements PlcFieldHandler {
public BaseDefaultFieldItem encodeDate(PlcField field, Object[] values) {
TestField testField = (TestField) field;
if (testField.getDataType() == LocalDate.class) {
- return new DefaultLocalDateTimeFieldItem((LocalDateTime[]) values);
+ return new DefaultLocalDateFieldItem((LocalDate[]) values);
}
throw new PlcRuntimeException("Invalid encoder for type " + testField.getDataType().getName());
}
diff --git a/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/TestFieldItem.java b/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/TestFieldItem.java
index 230b7b7..e65ec8d 100644
--- a/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/TestFieldItem.java
+++ b/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/TestFieldItem.java
@@ -23,7 +23,7 @@ import org.apache.plc4x.java.base.messages.items.BaseDefaultFieldItem;
public class TestFieldItem<T> extends BaseDefaultFieldItem<T> {
- public TestFieldItem(T[] values) {
+ public TestFieldItem(T... values) {
super(values);
}
diff --git a/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/SimulatedPlcDriverTest.java b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/SimulatedPlcDriverTest.java
index 61d39c9..6a5d9e8 100644
--- a/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/SimulatedPlcDriverTest.java
+++ b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/SimulatedPlcDriverTest.java
@@ -47,4 +47,11 @@ class SimulatedPlcDriverTest implements WithAssertions {
void connect_secure() {
assertThatThrownBy(() -> SUT.connect(null, null)).isInstanceOf(PlcConnectionException.class);
}
+
+ @Test
+ void wrongUrl() {
+ assertThatThrownBy(() -> SUT.connect("test:"))
+ .isInstanceOf(PlcConnectionException.class)
+ .hasMessage("Invalid URL: no device name given.");
+ }
}
\ No newline at end of file
diff --git a/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/TestDeviceTest.java b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/TestDeviceTest.java
index a4f2703..fbe3f36 100644
--- a/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/TestDeviceTest.java
+++ b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/TestDeviceTest.java
@@ -20,7 +20,7 @@ package org.apache.plc4x.java.simulated.connection;
import org.apache.plc4x.java.base.messages.items.BaseDefaultFieldItem;
import org.apache.plc4x.java.base.messages.items.DefaultLongFieldItem;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import java.util.Optional;
diff --git a/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/TestFieldHandlerTest.java b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/TestFieldHandlerTest.java
new file mode 100644
index 0000000..9fff341
--- /dev/null
+++ b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/TestFieldHandlerTest.java
@@ -0,0 +1,133 @@
+/*
+ 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.simulated.connection;
+
+import org.assertj.core.api.WithAssertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+@SuppressWarnings("unchecked")
+class TestFieldHandlerTest implements WithAssertions {
+
+ TestFieldHandler SUT = new TestFieldHandler();
+
+ @Mock
+ TestField plcField;
+
+ @Test
+ void createField() {
+ assertThat(SUT.createField("STATE/bar:INTEGER")).isNotNull();
+ }
+
+ @Test
+ void encodeBoolean() {
+ when(plcField.getDataType()).thenReturn((Class) Boolean.class);
+ assertThat(SUT.encodeBoolean(plcField, new Boolean[0])).isNotNull();
+ }
+
+ @Test
+ void encodeByte() {
+ when(plcField.getDataType()).thenReturn((Class) Byte.class);
+ assertThat(SUT.encodeByte(plcField, new Byte[0])).isNotNull();
+ }
+
+ @Test
+ void encodeShort() {
+ when(plcField.getDataType()).thenReturn((Class) Short.class);
+ assertThat(SUT.encodeShort(plcField, new Short[0])).isNotNull();
+ }
+
+ @Test
+ void encodeInteger() {
+ when(plcField.getDataType()).thenReturn((Class) Integer.class);
+ assertThat(SUT.encodeInteger(plcField, new Integer[0])).isNotNull();
+ }
+
+ @Test
+ void encodeBigInteger() {
+ when(plcField.getDataType()).thenReturn((Class) BigInteger.class);
+ assertThat(SUT.encodeBigInteger(plcField, new BigInteger[0])).isNotNull();
+ }
+
+ @Test
+ void encodeLong() {
+ when(plcField.getDataType()).thenReturn((Class) Long.class);
+ assertThat(SUT.encodeLong(plcField, new Long[0])).isNotNull();
+ }
+
+ @Test
+ void encodeFloat() {
+ when(plcField.getDataType()).thenReturn((Class) Float.class);
+ assertThat(SUT.encodeFloat(plcField, new Float[0])).isNotNull();
+ }
+
+ @Test
+ void encodeBigDecimal() {
+ when(plcField.getDataType()).thenReturn((Class) BigDecimal.class);
+ assertThat(SUT.encodeBigDecimal(plcField, new BigDecimal[0])).isNotNull();
+ }
+
+ @Test
+ void encodeDouble() {
+ when(plcField.getDataType()).thenReturn((Class) Double.class);
+ assertThat(SUT.encodeDouble(plcField, new Double[0])).isNotNull();
+ }
+
+ @Test
+ void encodeString() {
+ when(plcField.getDataType()).thenReturn((Class) String.class);
+ assertThat(SUT.encodeString(plcField, new String[0])).isNotNull();
+ }
+
+ @Test
+ void encodeTime() {
+ when(plcField.getDataType()).thenReturn((Class) LocalTime.class);
+ assertThat(SUT.encodeTime(plcField, new LocalTime[0])).isNotNull();
+ }
+
+ @Test
+ void encodeDate() {
+ when(plcField.getDataType()).thenReturn((Class) LocalDate.class);
+ assertThat(SUT.encodeDate(plcField, new LocalDate[0])).isNotNull();
+ }
+
+ @Test
+ void encodeDateTime() {
+ when(plcField.getDataType()).thenReturn((Class) LocalDateTime.class);
+ assertThat(SUT.encodeDateTime(plcField, new LocalDateTime[0])).isNotNull();
+ }
+
+ @Test
+ void encodeByteArray() {
+ when(plcField.getDataType()).thenReturn((Class) Byte[].class);
+ assertThat(SUT.encodeByteArray(plcField, new Byte[0])).isNotNull();
+ }
+}
\ No newline at end of file
diff --git a/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/TestFieldItem.java b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/TestFieldItemTest.java
similarity index 69%
copy from plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/TestFieldItem.java
copy to plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/TestFieldItemTest.java
index 230b7b7..bad6841 100644
--- a/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/TestFieldItem.java
+++ b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/TestFieldItemTest.java
@@ -7,7 +7,7 @@
"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
+ 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
@@ -19,17 +19,13 @@
package org.apache.plc4x.java.simulated.connection;
-import org.apache.plc4x.java.base.messages.items.BaseDefaultFieldItem;
+import org.assertj.core.api.WithAssertions;
+import org.junit.jupiter.api.Test;
-public class TestFieldItem<T> extends BaseDefaultFieldItem<T> {
+class TestFieldItemTest implements WithAssertions {
- public TestFieldItem(T[] values) {
- super(values);
+ @Test
+ void getObject() {
+ assertThat(new TestFieldItem<String>("").getObject(0)).isNotNull();
}
-
- @Override
- public Object getObject(int index) {
- return getValue(index);
- }
-
-}
+}
\ No newline at end of file