From 5683cc247236f9424c17dce2b21938bc27069720 Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Fri, 30 Oct 2020 15:15:03 +0100 Subject: [PATCH] Revert ordering of accepted data types for 'StringItem' (#1776) Signed-off-by: Christoph Weitkamp --- .../core/library/items/StringItem.java | 4 +- .../openhab/core/types/TypeParserTest.java | 47 ++++++++++--------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/items/StringItem.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/items/StringItem.java index e1d711311e..20ee32fb04 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/items/StringItem.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/items/StringItem.java @@ -38,8 +38,8 @@ public class StringItem extends GenericItem { // UnDefType has to come before StringType, because otherwise every UNDEF state sent as a string would be // interpreted as a StringType - private static final List> ACCEPTED_DATA_TYPES = List.of(UnDefType.class, DateTimeType.class, - StringType.class); + private static final List> ACCEPTED_DATA_TYPES = List.of(UnDefType.class, StringType.class, + DateTimeType.class); private static final List> ACCEPTED_COMMAND_TYPES = List.of(StringType.class, RefreshType.class); diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/types/TypeParserTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/types/TypeParserTest.java index 42a88ddabc..c7baf329ff 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/types/TypeParserTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/types/TypeParserTest.java @@ -12,14 +12,17 @@ */ package org.openhab.core.types; -import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.MatcherAssert.assertThat; +import java.util.Arrays; +import java.util.Collection; + import org.eclipse.jdt.annotation.NonNullByDefault; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import org.openhab.core.items.GenericItem; import org.openhab.core.library.items.StringItem; -import org.openhab.core.library.types.DateTimeType; import org.openhab.core.library.types.StringType; /** @@ -32,27 +35,29 @@ class TypeParserTest { private final GenericItem stringItem = new StringItem("Test"); - @Test - void testThatUNDEFAsStringIsParsedToUnDefType() { - State subject = TypeParser.parseState(stringItem.getAcceptedDataTypes(), "UNDEF"); - assertThat(subject instanceof UnDefType, is(true)); + public static class ParameterSet { + public final String state; + public final Class expectedDataType; + + public ParameterSet(String state, Class expectedDataType) { + this.state = state; + this.expectedDataType = expectedDataType; + } } - @Test - void testThatANumberAsStringIsParsedDateTimeType() { - State subject = TypeParser.parseState(stringItem.getAcceptedDataTypes(), "123"); - assertThat(subject instanceof DateTimeType, is(true)); + public static Collection stringItemParameters() { + return Arrays.asList(new Object[][] { // + { new ParameterSet("UNDEF", UnDefType.class) }, // + { new ParameterSet("ABC", StringType.class) }, // + { new ParameterSet("123", StringType.class) }, // + { new ParameterSet("2014-03-30T10:58:47.033+0000", StringType.class) } // + }); } - @Test - void testThatADateAsStringIsParsedDateTimeType() { - State subject = TypeParser.parseState(stringItem.getAcceptedDataTypes(), "2014-03-30T10:58:47.033+0000"); - assertThat(subject instanceof DateTimeType, is(true)); - } - - @Test - void testThatAStringIsParsedToStringType() { - State subject = TypeParser.parseState(stringItem.getAcceptedDataTypes(), "ABC"); - assertThat(subject instanceof StringType, is(true)); + @ParameterizedTest + @MethodSource("stringItemParameters") + public void testAllDataTypes(ParameterSet parameterSet) { + State subject = TypeParser.parseState(stringItem.getAcceptedDataTypes(), parameterSet.state); + assertThat(subject, instanceOf(parameterSet.expectedDataType)); } }