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 2020/05/15 14:55:12 UTC

[plc4x] branch rel/0.7 updated: - Applied a fix for some tests.

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

cdutz pushed a commit to branch rel/0.7
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/rel/0.7 by this push:
     new 3636f4d  - Applied a fix for some tests.
3636f4d is described below

commit 3636f4dc9c9ad6f2c89bdd9e19c26053ce1b0e19
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Fri May 15 16:54:57 2020 +0200

    - Applied a fix for some tests.
---
 .../apache/plc4x/java/mock/field/MockField.java    | 37 ++++++++++++++++++++--
 .../org/apache/plc4x/java/mock/field/MockType.java | 25 +++++++++++++++
 2 files changed, 59 insertions(+), 3 deletions(-)

diff --git a/plc4j/drivers/mock/src/main/java/org/apache/plc4x/java/mock/field/MockField.java b/plc4j/drivers/mock/src/main/java/org/apache/plc4x/java/mock/field/MockField.java
index 73eafdd..59f24da 100644
--- a/plc4j/drivers/mock/src/main/java/org/apache/plc4x/java/mock/field/MockField.java
+++ b/plc4j/drivers/mock/src/main/java/org/apache/plc4x/java/mock/field/MockField.java
@@ -21,14 +21,26 @@ package org.apache.plc4x.java.mock.field;
 import org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException;
 import org.apache.plc4x.java.api.model.PlcField;
 
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 public class MockField implements PlcField {
 
-    private final String address;
-    private final MockPlcValue plcValue;
+    private String address;
+    private MockPlcValue plcValue;
+    private MockType type;
 
+    private static final Pattern PATTERN =
+        Pattern.compile("%(?<name>[a-zA-Z_.0-9]+\\[?[0-9]*\\]?):?(?<type>[A-Z]*)");
 
     public static MockField of(String addressString) throws PlcInvalidFieldException {
-        return new MockField(addressString);
+        Matcher matcher = PATTERN.matcher(addressString);
+        if (matcher.matches()) {
+            String addr = matcher.group("name");
+            MockType type = MockType.valueOf(matcher.group("type"));
+            return new MockField(addr, type);
+        }
+        return null;
     }
 
     public MockField(String address) {
@@ -36,6 +48,11 @@ public class MockField implements PlcField {
         this.plcValue = null;
     }
 
+    public MockField(String address, MockType type) {
+        this.address = address;
+        this.type = type;
+    }
+
     public MockField(String address, MockPlcValue plcValue) {
         this.address = address;
         this.plcValue = plcValue;
@@ -65,4 +82,18 @@ public class MockField implements PlcField {
         return address.hashCode();
     }
 
+
+    @Override
+    public Class<?> getDefaultJavaType() {
+        switch (type) {
+            case BOOL:
+                return Boolean.class;
+            case INT:
+                return Integer.class;
+            case REAL:
+                return Double.class;
+            default:
+                return null;
+        }
+    }
 }
diff --git a/plc4j/drivers/mock/src/main/java/org/apache/plc4x/java/mock/field/MockType.java b/plc4j/drivers/mock/src/main/java/org/apache/plc4x/java/mock/field/MockType.java
new file mode 100644
index 0000000..6134d53
--- /dev/null
+++ b/plc4j/drivers/mock/src/main/java/org/apache/plc4x/java/mock/field/MockType.java
@@ -0,0 +1,25 @@
+/*
+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.mock.field;
+
+public enum MockType {
+    BOOL,
+    INT,
+    REAL;
+}