Revert ordering of accepted datatypes for StringItem (#1774)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
pull/1775/head
Christoph Weitkamp 2020-10-29 08:45:21 +01:00 committed by GitHub
parent ae26ce4618
commit 25683471e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 17 deletions

View File

@ -36,8 +36,10 @@ import org.openhab.core.types.UnDefType;
@NonNullByDefault
public class StringItem extends GenericItem {
private static final List<Class<? extends State>> ACCEPTED_DATA_TYPES = List.of(StringType.class,
DateTimeType.class, UnDefType.class);
// 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 Command>> ACCEPTED_COMMAND_TYPES = List.of(StringType.class,
RefreshType.class);

View File

@ -43,10 +43,8 @@ public final class TypeParser {
Class<?> stateClass = Class.forName(CORE_LIBRARY_PACKAGE + typeName);
Method valueOfMethod = stateClass.getMethod("valueOf", String.class);
return (Type) valueOfMethod.invoke(stateClass, input);
} catch (ClassNotFoundException e) {
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException
| InvocationTargetException e) {
}
return null;
}
@ -65,17 +63,15 @@ public final class TypeParser {
* @return the corresponding State instance or <code>null</code>
*/
public static State parseState(List<Class<? extends State>> types, String s) {
for (Class<? extends Type> type : types) {
for (Class<? extends State> type : types) {
try {
Method valueOf = type.getMethod("valueOf", String.class);
State state = (State) valueOf.invoke(type, s);
if (state != null) {
return state;
}
} catch (NoSuchMethodException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
} catch (NoSuchMethodException | IllegalArgumentException | IllegalAccessException
| InvocationTargetException e) {
}
}
return null;
@ -102,10 +98,8 @@ public final class TypeParser {
if (value != null) {
return value;
}
} catch (NoSuchMethodException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
} catch (NoSuchMethodException | IllegalArgumentException | IllegalAccessException
| InvocationTargetException e) {
}
}
return null;

View File

@ -0,0 +1,58 @@
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.types;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
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;
/**
* Test the {@link TypeParser}.
*
* @author Christoph Weitkamp - Initial contribution
*/
@NonNullByDefault
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));
}
@Test
void testThatANumberAsStringIsParsedDateTimeType() {
State subject = TypeParser.parseState(stringItem.getAcceptedDataTypes(), "123");
assertThat(subject instanceof DateTimeType, is(true));
}
@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));
}
}