Skip to content

Commit

Permalink
Adds missing date valuetype (#214)
Browse files Browse the repository at this point in the history
* Adds missing date valuetype

Signed-off-by: Frank Schnicke <frank.schnicke@iese.fraunhofer.de>
  • Loading branch information
FrankSchnicke committed Jan 30, 2023
1 parent 700a0b1 commit cf9eccf
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit cf9eccf

Please sign in to comment.