Revert ordering of accepted data types for 'StringItem' (#1776)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
pull/1789/head
Christoph Weitkamp 2020-10-30 15:15:03 +01:00 committed by GitHub
parent 25683471e8
commit 5683cc2472
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 23 deletions

View File

@ -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 // UnDefType has to come before StringType, because otherwise every UNDEF state sent as a string would be
// interpreted as a StringType // interpreted as a StringType
private static final List<Class<? extends State>> ACCEPTED_DATA_TYPES = List.of(UnDefType.class, DateTimeType.class, private static final List<Class<? extends State>> ACCEPTED_DATA_TYPES = List.of(UnDefType.class, StringType.class,
StringType.class); DateTimeType.class);
private static final List<Class<? extends Command>> ACCEPTED_COMMAND_TYPES = List.of(StringType.class, private static final List<Class<? extends Command>> ACCEPTED_COMMAND_TYPES = List.of(StringType.class,
RefreshType.class); RefreshType.class);

View File

@ -12,14 +12,17 @@
*/ */
package org.openhab.core.types; package org.openhab.core.types;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import java.util.Arrays;
import java.util.Collection;
import org.eclipse.jdt.annotation.NonNullByDefault; 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.items.GenericItem;
import org.openhab.core.library.items.StringItem; import org.openhab.core.library.items.StringItem;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.StringType; import org.openhab.core.library.types.StringType;
/** /**
@ -32,27 +35,29 @@ class TypeParserTest {
private final GenericItem stringItem = new StringItem("Test"); private final GenericItem stringItem = new StringItem("Test");
@Test public static class ParameterSet {
void testThatUNDEFAsStringIsParsedToUnDefType() { public final String state;
State subject = TypeParser.parseState(stringItem.getAcceptedDataTypes(), "UNDEF"); public final Class<? extends State> expectedDataType;
assertThat(subject instanceof UnDefType, is(true));
public ParameterSet(String state, Class<? extends State> expectedDataType) {
this.state = state;
this.expectedDataType = expectedDataType;
}
} }
@Test public static Collection<Object[]> stringItemParameters() {
void testThatANumberAsStringIsParsedDateTimeType() { return Arrays.asList(new Object[][] { //
State subject = TypeParser.parseState(stringItem.getAcceptedDataTypes(), "123"); { new ParameterSet("UNDEF", UnDefType.class) }, //
assertThat(subject instanceof DateTimeType, is(true)); { new ParameterSet("ABC", StringType.class) }, //
{ new ParameterSet("123", StringType.class) }, //
{ new ParameterSet("2014-03-30T10:58:47.033+0000", StringType.class) } //
});
} }
@Test @ParameterizedTest
void testThatADateAsStringIsParsedDateTimeType() { @MethodSource("stringItemParameters")
State subject = TypeParser.parseState(stringItem.getAcceptedDataTypes(), "2014-03-30T10:58:47.033+0000"); public void testAllDataTypes(ParameterSet parameterSet) {
assertThat(subject instanceof DateTimeType, is(true)); State subject = TypeParser.parseState(stringItem.getAcceptedDataTypes(), parameterSet.state);
} assertThat(subject, instanceOf(parameterSet.expectedDataType));
@Test
void testThatAStringIsParsedToStringType() {
State subject = TypeParser.parseState(stringItem.getAcceptedDataTypes(), "ABC");
assertThat(subject instanceof StringType, is(true));
} }
} }