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

View File

@ -43,10 +43,8 @@ public final class TypeParser {
Class<?> stateClass = Class.forName(CORE_LIBRARY_PACKAGE + typeName); Class<?> stateClass = Class.forName(CORE_LIBRARY_PACKAGE + typeName);
Method valueOfMethod = stateClass.getMethod("valueOf", String.class); Method valueOfMethod = stateClass.getMethod("valueOf", String.class);
return (Type) valueOfMethod.invoke(stateClass, input); return (Type) valueOfMethod.invoke(stateClass, input);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException
} catch (NoSuchMethodException e) { | InvocationTargetException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
} }
return null; return null;
} }
@ -55,7 +53,7 @@ public final class TypeParser {
* <p> * <p>
* Determines a state from a string. Possible state types are passed as a parameter. Note that the order matters * Determines a state from a string. Possible state types are passed as a parameter. Note that the order matters
* here; the first type that accepts the string as a valid value, will be used for the state. * here; the first type that accepts the string as a valid value, will be used for the state.
* *
* <p> * <p>
* Example: The type list is OnOffType.class,StringType.class. The string "ON" is now accepted by the OnOffType and * Example: The type list is OnOffType.class,StringType.class. The string "ON" is now accepted by the OnOffType and
* thus OnOffType.ON will be returned (and not a StringType with value "ON"). * thus OnOffType.ON will be returned (and not a StringType with value "ON").
@ -65,17 +63,15 @@ public final class TypeParser {
* @return the corresponding State instance or <code>null</code> * @return the corresponding State instance or <code>null</code>
*/ */
public static State parseState(List<Class<? extends State>> types, String s) { public static State parseState(List<Class<? extends State>> types, String s) {
for (Class<? extends Type> type : types) { for (Class<? extends State> type : types) {
try { try {
Method valueOf = type.getMethod("valueOf", String.class); Method valueOf = type.getMethod("valueOf", String.class);
State state = (State) valueOf.invoke(type, s); State state = (State) valueOf.invoke(type, s);
if (state != null) { if (state != null) {
return state; return state;
} }
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException | IllegalArgumentException | IllegalAccessException
} catch (IllegalArgumentException e) { | InvocationTargetException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
} }
} }
return null; return null;
@ -85,7 +81,7 @@ public final class TypeParser {
* <p> * <p>
* Determines a command from a string. Possible command types are passed as a parameter. Note that the order matters * Determines a command from a string. Possible command types are passed as a parameter. Note that the order matters
* here; the first type that accepts the string as a valid value, will be used for the command. * here; the first type that accepts the string as a valid value, will be used for the command.
* *
* <p> * <p>
* Example: The type list is OnOffType.class,StringType.class. The string "ON" is now accepted by the OnOffType and * Example: The type list is OnOffType.class,StringType.class. The string "ON" is now accepted by the OnOffType and
* thus OnOffType.ON will be returned (and not a StringType with value "ON"). * thus OnOffType.ON will be returned (and not a StringType with value "ON").
@ -102,10 +98,8 @@ public final class TypeParser {
if (value != null) { if (value != null) {
return value; return value;
} }
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException | IllegalArgumentException | IllegalAccessException
} catch (IllegalArgumentException e) { | InvocationTargetException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
} }
} }
return null; 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));
}
}