diff --git a/src/main/java/org/eclipse/basyx/submodel/metamodel/map/submodelelement/dataelement/property/valuetype/ValueType.java b/src/main/java/org/eclipse/basyx/submodel/metamodel/map/submodelelement/dataelement/property/valuetype/ValueType.java index a35e012f..570cfbd2 100644 --- a/src/main/java/org/eclipse/basyx/submodel/metamodel/map/submodelelement/dataelement/property/valuetype/ValueType.java +++ b/src/main/java/org/eclipse/basyx/submodel/metamodel/map/submodelelement/dataelement/property/valuetype/ValueType.java @@ -38,8 +38,8 @@ public enum ValueType implements StandardizedLiteralEnum { Int8("byte"), Int16("short"), Int32("int"), Int64("long"), UInt8("unsignedByte"), UInt16("unsignedShort"), UInt32("unsignedInt"), UInt64("unsignedLong"), String("string"), LangString("langString"), AnyURI("anyURI"), Base64Binary( "base64Binary"), HexBinary("hexBinary"), NOTATION("notation"), ENTITY("entity"), ID("id"), IDREF("idref"), Integer("integer"), NonPositiveInteger("nonPositiveInteger"), NonNegativeInteger("nonNegativeInteger"), PositiveInteger( - "positiveInteger"), NegativeInteger("negativeInteger"), Double("double"), Float("float"), Boolean("boolean"), Duration("duration"), DayTimeDuration("dayTimeDuration"), YearMonthDuration("yearMonthDuration"), DateTime( - "dateTime"), DateTimeStamp( + "positiveInteger"), NegativeInteger("negativeInteger"), Double("double"), Float("float"), Boolean("boolean"), Duration("duration"), DayTimeDuration("dayTimeDuration"), YearMonthDuration("yearMonthDuration"), Date( + "date"), DateTime("dateTime"), DateTimeStamp( "dateTimeStamp"), GDay("gDay"), GMonth("gMonth"), GMonthDay("gMonthDay"), GYear("gYear"), GYearMonth("gYearMonth"), QName("qName"), None("none"), AnyType("anyType"), AnySimpleType("anySimpleType"); private String standardizedLiteral; diff --git a/src/main/java/org/eclipse/basyx/submodel/metamodel/map/submodelelement/dataelement/property/valuetype/ValueTypeHelper.java b/src/main/java/org/eclipse/basyx/submodel/metamodel/map/submodelelement/dataelement/property/valuetype/ValueTypeHelper.java index 5932fd84..155ab7f2 100644 --- a/src/main/java/org/eclipse/basyx/submodel/metamodel/map/submodelelement/dataelement/property/valuetype/ValueTypeHelper.java +++ b/src/main/java/org/eclipse/basyx/submodel/metamodel/map/submodelelement/dataelement/property/valuetype/ValueTypeHelper.java @@ -96,21 +96,12 @@ public static ValueType getType(Object obj) { } else if (c == long.class || c == Long.class) { objectType = ValueType.Int64; } else if (c == BigInteger.class) { - BigInteger tmp = (BigInteger) obj; - if (tmp.compareTo(new BigInteger("0")) > 0) { - objectType = ValueType.PositiveInteger; - } else if (tmp.compareTo(new BigInteger("0")) < 0) { - objectType = ValueType.NegativeInteger; - } else { - objectType = ValueType.Integer; - } - + objectType = handleBigInteger((BigInteger) obj); } else if (c == void.class || c == Void.class) { objectType = ValueType.None; } else if (c == boolean.class || c == Boolean.class) { objectType = ValueType.Boolean; } else if (c == float.class || c == Float.class) { - // TODO C# deprecated due to new serialization objectType = ValueType.Float; } else if (c == double.class || c == Double.class) { objectType = ValueType.Double; @@ -131,6 +122,16 @@ public static ValueType getType(Object obj) { return objectType; } + private static ValueType handleBigInteger(BigInteger integer) { + if (integer.compareTo(new BigInteger("0")) > 0) { + return ValueType.PositiveInteger; + } else if (integer.compareTo(new BigInteger("0")) < 0) { + return ValueType.NegativeInteger; + } else { + return ValueType.Integer; + } + } + /** * Map the PropertyValueType to Java type * @@ -228,6 +229,7 @@ public static Object getJavaObject(Object value, ValueType objType) { case YearMonthDuration: target = Period.parse((String) value); break; + case Date: case DateTime: case DateTimeStamp: case GDay: diff --git a/src/test/java/org/eclipse/basyx/testsuite/regression/submodel/metamodel/map/submodelelement/dataelement/property/TestProperty.java b/src/test/java/org/eclipse/basyx/testsuite/regression/submodel/metamodel/map/submodelelement/dataelement/property/TestProperty.java index 58d5921a..6f6602ec 100644 --- a/src/test/java/org/eclipse/basyx/testsuite/regression/submodel/metamodel/map/submodelelement/dataelement/property/TestProperty.java +++ b/src/test/java/org/eclipse/basyx/testsuite/regression/submodel/metamodel/map/submodelelement/dataelement/property/TestProperty.java @@ -83,6 +83,18 @@ public void testConstructor1() { assertEquals(STRING_TYPE, property.getValueType()); } + @Test + public void dateProperty() throws DatatypeConfigurationException { + String date = "2002-09-24"; + XMLGregorianCalendar expected = DatatypeFactory.newInstance().newXMLGregorianCalendar(date); + + Property prop = new Property("idShort", ValueType.Date); + prop.setValue(expected); + + assertEquals(date, prop.get(Property.VALUE)); + assertEquals(expected, prop.getValue()); + } + @Test public void testConstructor2() { Referable referable = new Referable("testIdShort", "testCategory", new LangStrings("DE", "test")); diff --git a/src/test/java/org/eclipse/basyx/testsuite/regression/submodel/metamodel/map/submodelelement/dataelement/property/valuetype/TestValueTypeHelper.java b/src/test/java/org/eclipse/basyx/testsuite/regression/submodel/metamodel/map/submodelelement/dataelement/property/valuetype/TestValueTypeHelper.java new file mode 100644 index 00000000..790394ce --- /dev/null +++ b/src/test/java/org/eclipse/basyx/testsuite/regression/submodel/metamodel/map/submodelelement/dataelement/property/valuetype/TestValueTypeHelper.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (C) 2023 the Eclipse BaSyx Authors + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * SPDX-License-Identifier: MIT + ******************************************************************************/ + + +package org.eclipse.basyx.testsuite.regression.submodel.metamodel.map.submodelelement.dataelement.property.valuetype; + +import static org.junit.Assert.assertEquals; + +import javax.xml.datatype.DatatypeConfigurationException; +import javax.xml.datatype.DatatypeFactory; +import javax.xml.datatype.XMLGregorianCalendar; + +import org.eclipse.basyx.submodel.metamodel.map.submodelelement.dataelement.property.valuetype.ValueType; +import org.eclipse.basyx.submodel.metamodel.map.submodelelement.dataelement.property.valuetype.ValueTypeHelper; +import org.junit.Test; + +/** + * Tests the ValueTypeHelper class + * + * @author schnicke + * + */ +public class TestValueTypeHelper { + + @Test + public void dateFromString() throws DatatypeConfigurationException { + String date = "2002-09-24"; + Object javaObject = ValueTypeHelper.getJavaObject(date, ValueType.Date); + + XMLGregorianCalendar expected = DatatypeFactory.newInstance().newXMLGregorianCalendar(date); + assertEquals(expected, javaObject); + } +}