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
// interpreted as a StringType
private static final List<Class<? extends State>> ACCEPTED_DATA_TYPES = List.of(UnDefType.class, DateTimeType.class,
StringType.class);
private static final List<Class<? extends State>> ACCEPTED_DATA_TYPES = List.of(UnDefType.class, StringType.class,
DateTimeType.class);
private static final List<Class<? extends Command>> ACCEPTED_COMMAND_TYPES = List.of(StringType.class,
RefreshType.class);

View File

@ -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<? extends State> expectedDataType;
public ParameterSet(String state, Class<? extends State> 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<Object[]> 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));
}
}