]> git.basschouten.com Git - openhab-addons.git/commitdiff
[jdbc] Fixed ClassCastException for ColorItem / HSBType (#12225)
authorChristoph Weitkamp <github@christophweitkamp.de>
Sun, 6 Feb 2022 14:45:03 +0000 (15:45 +0100)
committerGitHub <noreply@github.com>
Sun, 6 Feb 2022 14:45:03 +0000 (15:45 +0100)
* Fixed ClassCastException for ColorItem / HSBType

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcBaseDAO.java
bundles/org.openhab.persistence.jdbc/src/test/java/org/openhab/persistence/jdbc/db/JdbcBaseDAOTest.java

index 7679ac4cca148a578212b6baaee95fddc9f0f8b5..1ce2bdf93506ad75824423dd5c1c19038484bf0d 100644 (file)
@@ -30,6 +30,7 @@ import org.eclipse.jdt.annotation.Nullable;
 import org.knowm.yank.Yank;
 import org.openhab.core.items.GroupItem;
 import org.openhab.core.items.Item;
+import org.openhab.core.library.items.ColorItem;
 import org.openhab.core.library.items.ContactItem;
 import org.openhab.core.library.items.DateTimeItem;
 import org.openhab.core.library.items.DimmerItem;
@@ -40,6 +41,7 @@ import org.openhab.core.library.items.RollershutterItem;
 import org.openhab.core.library.items.SwitchItem;
 import org.openhab.core.library.types.DateTimeType;
 import org.openhab.core.library.types.DecimalType;
+import org.openhab.core.library.types.HSBType;
 import org.openhab.core.library.types.PercentType;
 import org.openhab.core.library.types.QuantityType;
 import org.openhab.core.library.types.RawType;
@@ -541,14 +543,15 @@ public class JdbcBaseDAO {
                 return unit == null ? new DecimalType((BigDecimal) v)
                         : QuantityType.valueOf(((BigDecimal) v).doubleValue(), unit);
             } else if (it.toUpperCase().contains("INT")) {
-                return unit == null ? new DecimalType(((Integer) v).intValue())
+                return unit == null ? new DecimalType(objectAsInteger(v))
                         : QuantityType.valueOf(((Integer) v).doubleValue(), unit);
             }
-            return unit == null ? DecimalType.valueOf(((String) v).toString())
-                    : QuantityType.valueOf(((String) v).toString());
+            return unit == null ? DecimalType.valueOf(objectAsString(v)) : QuantityType.valueOf(objectAsString(v));
         } else if (item instanceof DateTimeItem) {
             return new DateTimeType(
                     ZonedDateTime.ofInstant(Instant.ofEpochMilli(objectAsLong(v)), ZoneId.systemDefault()));
+        } else if (item instanceof ColorItem) {
+            return HSBType.valueOf(objectAsString(v));
         } else if (item instanceof DimmerItem || item instanceof RollershutterItem) {
             return new PercentType(objectAsInteger(v));
         } else if (item instanceof ImageItem) {
index cc0fe9c4cdd969e94876a4c6f43f8a5f5c28fcb6..019b1271cad7d3b292dd4958c1c2bdfeff40edfb 100644 (file)
@@ -14,6 +14,7 @@ package org.openhab.persistence.jdbc.db;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
 
 import java.time.LocalDateTime;
 import java.time.ZoneId;
@@ -23,8 +24,35 @@ import java.time.format.DateTimeFormatter;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.openhab.core.library.items.CallItem;
+import org.openhab.core.library.items.ColorItem;
+import org.openhab.core.library.items.ContactItem;
+import org.openhab.core.library.items.DateTimeItem;
+import org.openhab.core.library.items.DimmerItem;
+import org.openhab.core.library.items.ImageItem;
+import org.openhab.core.library.items.LocationItem;
+import org.openhab.core.library.items.NumberItem;
+import org.openhab.core.library.items.PlayerItem;
+import org.openhab.core.library.items.RollershutterItem;
+import org.openhab.core.library.items.StringItem;
+import org.openhab.core.library.items.SwitchItem;
+import org.openhab.core.library.types.DateTimeType;
+import org.openhab.core.library.types.DecimalType;
+import org.openhab.core.library.types.HSBType;
+import org.openhab.core.library.types.OnOffType;
+import org.openhab.core.library.types.OpenClosedType;
+import org.openhab.core.library.types.PercentType;
+import org.openhab.core.library.types.PlayPauseType;
+import org.openhab.core.library.types.PointType;
+import org.openhab.core.library.types.QuantityType;
+import org.openhab.core.library.types.RawType;
+import org.openhab.core.library.types.RewindFastforwardType;
+import org.openhab.core.library.types.StringListType;
+import org.openhab.core.library.types.StringType;
+import org.openhab.core.library.unit.SIUnits;
 import org.openhab.core.persistence.FilterCriteria;
 import org.openhab.core.persistence.FilterCriteria.Ordering;
+import org.openhab.core.types.State;
 
 /**
  * Tests the {@link JdbcBaseDAO}.
@@ -47,6 +75,65 @@ public class JdbcBaseDAOTest {
         filter = new FilterCriteria();
     }
 
+    @Test
+    public void testObjectAsStateReturnsValidState() {
+        State decimalType = jdbcBaseDAO.objectAsState(new NumberItem("testNumberItem"), null, 7.3);
+        assertInstanceOf(DecimalType.class, decimalType);
+        assertThat(decimalType, is(DecimalType.valueOf("7.3")));
+        State quantityType = jdbcBaseDAO.objectAsState(new NumberItem("testNumberItem"), SIUnits.CELSIUS, 7.3);
+        assertInstanceOf(QuantityType.class, quantityType);
+        assertThat(quantityType, is(QuantityType.valueOf("7.3 °C")));
+
+        State dateTimeType = jdbcBaseDAO.objectAsState(new DateTimeItem("testDateTimeItem"), null,
+                java.sql.Timestamp.valueOf("2021-02-01 23:30:02.049"));
+        assertInstanceOf(DateTimeType.class, dateTimeType);
+        assertThat(dateTimeType, is(DateTimeType.valueOf("2021-02-01T23:30:02.049")));
+
+        State hsbType = jdbcBaseDAO.objectAsState(new ColorItem("testColorItem"), null, "184,100,52");
+        assertInstanceOf(HSBType.class, hsbType);
+        assertThat(hsbType, is(HSBType.valueOf("184,100,52")));
+
+        State percentType = jdbcBaseDAO.objectAsState(new DimmerItem("testDimmerItem"), null, 52);
+        assertInstanceOf(PercentType.class, percentType);
+        assertThat(percentType, is(PercentType.valueOf("52")));
+
+        percentType = jdbcBaseDAO.objectAsState(new RollershutterItem("testRollershutterItem"), null, 39);
+        assertInstanceOf(PercentType.class, percentType);
+        assertThat(percentType, is(PercentType.valueOf("39")));
+
+        State openClosedType = jdbcBaseDAO.objectAsState(new ContactItem("testContactItem"), null, "OPEN");
+        assertInstanceOf(OpenClosedType.class, openClosedType);
+        assertThat(openClosedType, is(OpenClosedType.OPEN));
+
+        State playPauseType = jdbcBaseDAO.objectAsState(new PlayerItem("testPlayerItem"), null, "PLAY");
+        assertInstanceOf(PlayPauseType.class, playPauseType);
+        assertThat(playPauseType, is(PlayPauseType.PLAY));
+        State rewindFastforwardType = jdbcBaseDAO.objectAsState(new PlayerItem("testPlayerItem"), null, "REWIND");
+        assertInstanceOf(RewindFastforwardType.class, rewindFastforwardType);
+        assertThat(rewindFastforwardType, is(RewindFastforwardType.REWIND));
+
+        State onOffType = jdbcBaseDAO.objectAsState(new SwitchItem("testSwitchItem"), null, "ON");
+        assertInstanceOf(OnOffType.class, onOffType);
+        assertThat(onOffType, is(OnOffType.ON));
+
+        State stringListType = jdbcBaseDAO.objectAsState(new CallItem("testCallItem"), null, "0699222222,0179999998");
+        assertInstanceOf(StringListType.class, stringListType);
+        assertThat(stringListType, is(StringListType.valueOf("0699222222,0179999998")));
+
+        State expectedRawType = new RawType(new byte[0], "application/octet-stream");
+        State rawType = jdbcBaseDAO.objectAsState(new ImageItem("testImageItem"), null, expectedRawType.toFullString());
+        assertInstanceOf(RawType.class, rawType);
+        assertThat(rawType, is(expectedRawType));
+
+        State pointType = jdbcBaseDAO.objectAsState(new LocationItem("testLocationItem"), null, "1,2,3");
+        assertInstanceOf(PointType.class, pointType);
+        assertThat(pointType, is(PointType.valueOf("1,2,3")));
+
+        State stringType = jdbcBaseDAO.objectAsState(new StringItem("testStringItem"), null, "String");
+        assertInstanceOf(StringType.class, stringType);
+        assertThat(stringType, is(StringType.valueOf("String")));
+    }
+
     @Test
     public void testHistItemFilterQueryProviderReturnsSelectQueryWithoutWhereClauseDescendingOrder() {
         String sql = jdbcBaseDAO.histItemFilterQueryProvider(filter, 0, DB_TABLE_NAME, "TEST", UTC_ZONE_ID);