You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2018/11/24 23:22:50 UTC

[incubator-plc4x] branch develop updated: - Improved the test coverage

This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 6cfccbd  - Improved the test coverage
6cfccbd is described below

commit 6cfccbd926ce9f9f1dda30aa7ff23d7f7eb0f9ec
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Sun Nov 25 00:22:39 2018 +0100

    - Improved the test coverage
---
 .../plc4x/java/s7/connection/S7PlcConnection.java  |  4 ++
 .../java/s7/connection/S7PlcConnectionIT.java      |  3 +-
 .../java/s7/connection/S7PlcConnectionTests.java   | 12 +++-
 .../java/s7/connection/S7PlcTestConnection.java    |  8 +--
 plc4j/drivers/simulated/pom.xml                    |  7 ++
 .../plc4x/java/simulated/connection/TestField.java |  5 +-
 .../java/simulated/connection/TestFieldTest.java   | 61 ++++++++++++++++
 .../base/connection/AbstractPlcConnection.java     |  3 +-
 .../java/base/connection/NettyPlcConnection.java   |  1 -
 ...rnalPlcRequest.java => InternalPlcMessage.java} |  7 +-
 .../java/base/messages/InternalPlcRequest.java     |  2 +-
 .../base/model/InternalPlcSubscriptionHandle.java  |  3 +-
 .../base/connection/AbstractPlcConnectionTest.java | 16 +++++
 .../base/connection/NettyPlcConnectionTest.java    | 82 ++++++++++++++++++++++
 14 files changed, 194 insertions(+), 20 deletions(-)

diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/connection/S7PlcConnection.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/connection/S7PlcConnection.java
index 0dbc2a3..da65b9a 100644
--- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/connection/S7PlcConnection.java
+++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/connection/S7PlcConnection.java
@@ -220,6 +220,10 @@ public class S7PlcConnection extends NettyPlcConnection implements PlcReader, Pl
         return paramMaxAmqCallee;
     }
 
+    public S7ControllerType getParamControllerType() {
+        return paramControllerType;
+    }
+
     @Override
     public void close() throws PlcConnectionException {
         if ((channel != null) && channel.isOpen()) {
diff --git a/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcConnectionIT.java b/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcConnectionIT.java
index 76c2270..00d16bd 100644
--- a/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcConnectionIT.java
+++ b/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcConnectionIT.java
@@ -24,7 +24,6 @@ import org.apache.plc4x.java.api.messages.PlcReadRequest;
 import org.apache.plc4x.java.api.messages.PlcReadResponse;
 import org.apache.plc4x.java.api.messages.PlcWriteRequest;
 import org.apache.plc4x.java.api.messages.PlcWriteResponse;
-import org.apache.plc4x.java.s7.types.S7ControllerType;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
@@ -48,7 +47,7 @@ public class S7PlcConnectionIT {
     @Before
     public void setUp() {
         SUT = new S7PlcTestConnection(1, 2,
-            "pdu-size=128&max-amq-caller=2&max-amq-callee=3&unknown=parameter&unknown-flag", S7ControllerType.S7_1500);
+            "pdu-size=128&max-amq-caller=2&max-amq-callee=3&unknown=parameter&unknown-flag");
     }
 
     @After
diff --git a/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcConnectionTests.java b/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcConnectionTests.java
index 9e3bcd4..65d37ba 100644
--- a/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcConnectionTests.java
+++ b/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcConnectionTests.java
@@ -20,7 +20,6 @@
 package org.apache.plc4x.java.s7.connection;
 
 import org.apache.plc4x.java.api.exceptions.PlcUnsupportedOperationException;
-import org.apache.plc4x.java.s7.types.S7ControllerType;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -37,7 +36,7 @@ public class S7PlcConnectionTests {
     @Before
     public void setUp() {
         SUT = new S7PlcTestConnection(1, 2,
-            "pdu-size=128&max-amq-caller=2&max-amq-callee=3&unknown=parameter&unknown-flag", S7ControllerType.S7_1200);
+            "pdu-size=128&max-amq-caller=2&max-amq-callee=3&unknown=parameter&unknown-flag");
     }
 
     @After
@@ -54,6 +53,15 @@ public class S7PlcConnectionTests {
         assertThat("Max AMQ Callee size is incorrect", SUT.getParamMaxAmqCallee(), equalTo(3) );
     }
 
+    /**
+     * When configuring a connection to a LOGO device, then the pdu size has to be set to a different value.
+     */
+    @Test
+    public void initialStateLogo() {
+        SUT = new S7PlcTestConnection(1, 2, "controller-type=LOGO");
+        assertThat("Pdu size is incorrect", SUT.getParamPduSize(), equalTo((short) 480));
+    }
+
     @Test
     public void capabilities() {
         assertThat(SUT.canRead(), equalTo(true));
diff --git a/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcTestConnection.java b/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcTestConnection.java
index d46ec90..15d5770 100644
--- a/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcTestConnection.java
+++ b/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcTestConnection.java
@@ -24,7 +24,6 @@ import io.netty.channel.embedded.EmbeddedChannel;
 import org.apache.commons.io.IOUtils;
 import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
 import org.apache.plc4x.java.base.connection.TestChannelFactory;
-import org.apache.plc4x.java.s7.types.S7ControllerType;
 import org.pcap4j.core.NotOpenException;
 import org.pcap4j.core.PcapHandle;
 import org.pcap4j.core.PcapNativeException;
@@ -45,11 +44,8 @@ import static org.junit.Assert.fail;
 
 public class S7PlcTestConnection extends S7PlcConnection {
 
-    private S7ControllerType controllerType;
-
-    public S7PlcTestConnection(int rack, int slot, String params, S7ControllerType controllerType) {
+    public S7PlcTestConnection(int rack, int slot, String params) {
         super(new TestChannelFactory(), rack, slot, params);
-        this.controllerType = controllerType;
     }
 
     /*
@@ -117,7 +113,7 @@ public class S7PlcTestConnection extends S7PlcConnection {
         byte[] cpuFunctionsResponse = readPcapFile(
             "org/apache/plc4x/java/s7/connection/s7-cpu-functions-response.pcap");
         // Override the type of reported S7 device.
-        switch (controllerType) {
+        switch (getParamControllerType()) {
             case S7_1200:
                 cpuFunctionsResponse[48] = '2';
                 break;
diff --git a/plc4j/drivers/simulated/pom.xml b/plc4j/drivers/simulated/pom.xml
index 5716099..4e777b9 100644
--- a/plc4j/drivers/simulated/pom.xml
+++ b/plc4j/drivers/simulated/pom.xml
@@ -51,6 +51,13 @@
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-lang3</artifactId>
     </dependency>
+
+    <dependency>
+      <groupId>nl.jqno.equalsverifier</groupId>
+      <artifactId>equalsverifier</artifactId>
+      <version>3.0.2</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
 </project>
\ No newline at end of file
diff --git a/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/TestField.java b/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/TestField.java
index a6d90de..3c14c0a 100644
--- a/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/TestField.java
+++ b/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/TestField.java
@@ -40,7 +40,7 @@ public class TestField implements PlcField {
      * - {@code RANDOM/foo:INTEGER}
      * - {@code STDOUT/foo:STRING}
      */
-    private static final Pattern ADDRESS_PATTERN = Pattern.compile("^(?<type>\\w+)/(?<name>\\w+):(?<dataType>.+)(\\[(?<numElements>\\d)])?$");
+    private static final Pattern ADDRESS_PATTERN = Pattern.compile("^(?<type>\\w+)/(?<name>\\w+):(?<dataType>[a-zA-Z]++)(\\[(?<numElements>\\d+)])?$");
 
     private final TestType type;
     private final String name;
@@ -100,7 +100,7 @@ public class TestField implements PlcField {
         if (this == o) {
             return true;
         }
-        if (!(o instanceof TestField)) {
+        if (o == null || getClass() != o.getClass()) {
             return false;
         }
         TestField testField = (TestField) o;
@@ -124,4 +124,5 @@ public class TestField implements PlcField {
             ", numElements=" + numElements +
             '}';
     }
+
 }
diff --git a/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/TestFieldTest.java b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/TestFieldTest.java
new file mode 100644
index 0000000..f2e5c53
--- /dev/null
+++ b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/TestFieldTest.java
@@ -0,0 +1,61 @@
+/*
+ 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 nl.jqno.equalsverifier.EqualsVerifier;
+import org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException;
+import org.junit.jupiter.api.Test;
+
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+public class TestFieldTest {
+
+    @Test
+    public void constructor() {
+        assertThat(TestField.matches("RANDOM/test:Integer[2]"), equalTo(true));
+        TestField field = TestField.of("RANDOM/test:Integer[2]");
+        assertThat(field.getType(), equalTo(TestType.RANDOM));
+        assertThat(field.getName(), equalTo("test"));
+        assertThat(field.getDataType(), equalTo(Integer.class));
+        assertThat(field.getNumElements(), equalTo(2));
+        assertThat(field.toString(),
+            equalTo("TestField{type=RANDOM, name='test', dataType=class java.lang.Integer, numElements=2}"));
+    }
+
+    @Test
+    public void invalidType() {
+        assertThat(TestField.matches("RANDOM/test:Foo"), equalTo(true));
+        assertThrows(PlcInvalidFieldException.class, () -> TestField.of("RANDOM/test:Foo"));
+    }
+
+    @Test
+    public void invalidAddress() {
+        assertThat(TestField.matches("Foo"), equalTo(false));
+        assertThrows(PlcInvalidFieldException.class, () -> TestField.of("Foo"));
+    }
+
+    @Test
+    public void equalsTest() {
+        EqualsVerifier.forClass(TestField.class).usingGetClass().verify();
+    }
+
+}
diff --git a/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/connection/AbstractPlcConnection.java b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/connection/AbstractPlcConnection.java
index e991512..cef1786 100644
--- a/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/connection/AbstractPlcConnection.java
+++ b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/connection/AbstractPlcConnection.java
@@ -25,6 +25,7 @@ import org.apache.plc4x.java.api.messages.PlcSubscriptionRequest;
 import org.apache.plc4x.java.api.messages.PlcUnsubscriptionRequest;
 import org.apache.plc4x.java.api.messages.PlcWriteRequest;
 import org.apache.plc4x.java.api.metadata.PlcConnectionMetadata;
+import org.apache.plc4x.java.base.messages.InternalPlcMessage;
 
 import java.util.Objects;
 
@@ -84,7 +85,7 @@ public abstract class AbstractPlcConnection implements PlcConnection, PlcConnect
      * @param <T>   the type of the expected {@code clazz}.
      * @return the cast type of {@code clazz}.
      */
-    protected <T> T checkInternal(Object o, Class<T> clazz) {
+    protected <T extends InternalPlcMessage> T checkInternal(Object o, Class<T> clazz) {
         Objects.requireNonNull(o);
         Objects.requireNonNull(clazz);
         if (!clazz.isInstance(o)) {
diff --git a/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/connection/NettyPlcConnection.java b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/connection/NettyPlcConnection.java
index fd1ac78..cd4c1ca 100644
--- a/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/connection/NettyPlcConnection.java
+++ b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/connection/NettyPlcConnection.java
@@ -25,7 +25,6 @@ import io.netty.util.Timer;
 import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
 import org.apache.plc4x.java.api.exceptions.PlcIoException;
 
-import java.util.Objects;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 
diff --git a/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/messages/InternalPlcRequest.java b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/messages/InternalPlcMessage.java
similarity index 83%
copy from plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/messages/InternalPlcRequest.java
copy to plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/messages/InternalPlcMessage.java
index b264d7b..ac26a30 100644
--- a/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/messages/InternalPlcRequest.java
+++ b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/messages/InternalPlcMessage.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
@@ -16,9 +16,8 @@
  specific language governing permissions and limitations
  under the License.
  */
-package org.apache.plc4x.java.base.messages;
 
-import org.apache.plc4x.java.api.messages.PlcRequest;
+package org.apache.plc4x.java.base.messages;
 
-public interface InternalPlcRequest extends PlcRequest {
+public interface InternalPlcMessage {
 }
diff --git a/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/messages/InternalPlcRequest.java b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/messages/InternalPlcRequest.java
index b264d7b..b031ee3 100644
--- a/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/messages/InternalPlcRequest.java
+++ b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/messages/InternalPlcRequest.java
@@ -20,5 +20,5 @@ package org.apache.plc4x.java.base.messages;
 
 import org.apache.plc4x.java.api.messages.PlcRequest;
 
-public interface InternalPlcRequest extends PlcRequest {
+public interface InternalPlcRequest extends PlcRequest, InternalPlcMessage {
 }
diff --git a/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/model/InternalPlcSubscriptionHandle.java b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/model/InternalPlcSubscriptionHandle.java
index 32e9000..e8fbc37 100644
--- a/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/model/InternalPlcSubscriptionHandle.java
+++ b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/model/InternalPlcSubscriptionHandle.java
@@ -19,6 +19,7 @@
 package org.apache.plc4x.java.base.model;
 
 import org.apache.plc4x.java.api.model.PlcSubscriptionHandle;
+import org.apache.plc4x.java.base.messages.InternalPlcMessage;
 
-public interface InternalPlcSubscriptionHandle extends PlcSubscriptionHandle {
+public interface InternalPlcSubscriptionHandle extends PlcSubscriptionHandle, InternalPlcMessage {
 }
diff --git a/plc4j/protocols/driver-bases/base/src/test/java/org/apache/plc4x/java/base/connection/AbstractPlcConnectionTest.java b/plc4j/protocols/driver-bases/base/src/test/java/org/apache/plc4x/java/base/connection/AbstractPlcConnectionTest.java
index b5a3dc3..2064709 100644
--- a/plc4j/protocols/driver-bases/base/src/test/java/org/apache/plc4x/java/base/connection/AbstractPlcConnectionTest.java
+++ b/plc4j/protocols/driver-bases/base/src/test/java/org/apache/plc4x/java/base/connection/AbstractPlcConnectionTest.java
@@ -21,9 +21,14 @@ package org.apache.plc4x.java.base.connection;
 
 import org.apache.commons.lang3.NotImplementedException;
 import org.apache.plc4x.java.api.exceptions.PlcUnsupportedOperationException;
+import org.apache.plc4x.java.base.messages.DefaultPlcReadRequest;
+import org.apache.plc4x.java.base.messages.PlcReader;
 import org.assertj.core.api.WithAssertions;
 import org.junit.jupiter.api.Test;
 
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+
 class AbstractPlcConnectionTest implements WithAssertions {
 
     AbstractPlcConnection SUT = new AbstractPlcConnection() {
@@ -82,4 +87,15 @@ class AbstractPlcConnectionTest implements WithAssertions {
     void unsubscriptionRequestBuilder() {
         assertThatThrownBy(() -> SUT.unsubscriptionRequestBuilder()).isInstanceOf(PlcUnsupportedOperationException.class);
     }
+
+    @Test
+    void checkInternalTest() {
+        assertThrows(IllegalArgumentException.class, () -> SUT.checkInternal("Test", DefaultPlcReadRequest.class));
+        DefaultPlcReadRequest readRequest = SUT.checkInternal(
+            new DefaultPlcReadRequest.Builder(mock(PlcReader.class), mock(PlcFieldHandler.class)).build(),
+            DefaultPlcReadRequest.class);
+        assertThat(readRequest).isNotNull();
+    }
+
+
 }
\ No newline at end of file
diff --git a/plc4j/protocols/driver-bases/base/src/test/java/org/apache/plc4x/java/base/connection/NettyPlcConnectionTest.java b/plc4j/protocols/driver-bases/base/src/test/java/org/apache/plc4x/java/base/connection/NettyPlcConnectionTest.java
new file mode 100644
index 0000000..ad6da73
--- /dev/null
+++ b/plc4j/protocols/driver-bases/base/src/test/java/org/apache/plc4x/java/base/connection/NettyPlcConnectionTest.java
@@ -0,0 +1,82 @@
+/*
+ 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.connection;
+
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelHandler;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.embedded.EmbeddedChannel;
+import org.apache.plc4x.java.base.events.ConnectEvent;
+import org.assertj.core.api.WithAssertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.CompletableFuture;
+
+
+public class NettyPlcConnectionTest implements WithAssertions {
+
+    NettyPlcConnection SUT = new NettyPlcConnection(EmbeddedChannel::new, true) {
+        @Override
+        protected ChannelHandler getChannelHandler(CompletableFuture<Void> sessionSetupCompleteFuture) {
+            sessionSetupCompleteFuture.complete(null);
+
+            return new ChannelHandler() {
+                @Override
+                public void handlerAdded(ChannelHandlerContext ctx) {
+                }
+
+                @Override
+                public void handlerRemoved(ChannelHandlerContext ctx) {
+                }
+
+                @Override
+                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
+                }
+            };
+        }
+
+        @Override
+        protected void sendChannelCreatedEvent() {
+            channel.pipeline().fireUserEventTriggered(new ConnectEvent());
+        }
+    };
+
+    @Test
+    public void connect() throws Exception {
+        SUT.connect();
+    }
+
+    @Test
+    public void close() throws Exception {
+        SUT.close();
+    }
+
+    @Test
+    public void isConnected() {
+        SUT.isConnected();
+    }
+
+    @Test
+    public void getChannel() {
+        Channel channel = SUT.getChannel();
+        assertThat(channel).isNotNull();
+    }
+
+}