From 94b8c2a5405ea3fe277b60ddd5a65eb86742c5fd Mon Sep 17 00:00:00 2001 From: J-N-K Date: Sun, 11 Dec 2022 11:00:50 +0100 Subject: [PATCH] Fix DataAmount dimension units (#3208) Signed-off-by: Jan N. Klug --- bom/compile/pom.xml | 2 + .../org/openhab/core/library/unit/Units.java | 14 ++++- .../library/dimension/DataAmountTest.java | 55 +++++++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 bundles/org.openhab.core/src/test/java/org/openhab/core/library/dimension/DataAmountTest.java diff --git a/bom/compile/pom.xml b/bom/compile/pom.xml index 713aa1b80..5cf902839 100644 --- a/bom/compile/pom.xml +++ b/bom/compile/pom.xml @@ -166,6 +166,8 @@ + javax.measure unit-api diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/Units.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/Units.java index 0419aca16..387360881 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/Units.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/Units.java @@ -64,8 +64,10 @@ import tech.units.indriya.function.ExpConverter; import tech.units.indriya.function.LogConverter; import tech.units.indriya.function.MultiplyConverter; import tech.units.indriya.unit.AlternateUnit; +import tech.units.indriya.unit.BaseUnit; import tech.units.indriya.unit.ProductUnit; import tech.units.indriya.unit.TransformedUnit; +import tech.units.indriya.unit.UnitDimension; /** * Delegate common units to {@link Units} to hide this dependency from the rest of openHAB. @@ -188,7 +190,7 @@ public final class Units extends CustomUnits { tech.units.indriya.unit.Units.CUBIC_METRE.divide(tech.units.indriya.unit.Units.HOUR))); public static final Unit CUBICMETRE_PER_DAY = addUnit(new ProductUnit( tech.units.indriya.unit.Units.CUBIC_METRE.divide(tech.units.indriya.unit.Units.DAY))); - public static final Unit BIT = addUnit(new AlternateUnit<>(ONE, "bit")); + public static final Unit BIT = addUnit(new BaseUnit<>("bit", UnitDimension.parse('X'))); public static final Unit KILOBIT = addUnit(MetricPrefix.KILO(BIT)); public static final Unit MEGABIT = addUnit(MetricPrefix.MEGA(BIT)); public static final Unit GIGABIT = addUnit(MetricPrefix.GIGA(BIT)); @@ -245,8 +247,14 @@ public final class Units extends CustomUnits { SimpleUnitFormat.getInstance().label(GIGABIT, "Gbit"); SimpleUnitFormat.getInstance().label(GIGABIT_PER_SECOND, "Gbit/s"); SimpleUnitFormat.getInstance().label(IRRADIANCE, "W/m²"); - SimpleUnitFormat.getInstance().label(KILOBYTE, "KB"); - SimpleUnitFormat.getInstance().label(KIBIBYTE, "KiB"); + SimpleUnitFormat.getInstance().label(KILOBYTE, "kB"); + // "KB" is wrong, but we keep it for backward compatibility + SimpleUnitFormat.getInstance().alias(KILOBYTE, "KB"); + SimpleUnitFormat.getInstance().label(KIBIBYTE, "kiB"); + // "KiB" is wrong, but we keep it for backward compatibility + SimpleUnitFormat.getInstance().alias(KIBIBYTE, "KiB"); + SimpleUnitFormat.getInstance().alias(KIBIBYTE, "kio"); + // "Kio" is wrong, but we keep it for backward compatibility SimpleUnitFormat.getInstance().alias(KIBIBYTE, "Kio"); SimpleUnitFormat.getInstance().label(KILOBIT, "kbit"); SimpleUnitFormat.getInstance().label(KILOBIT_PER_SECOND, "kbit/s"); diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/dimension/DataAmountTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/dimension/DataAmountTest.java new file mode 100644 index 000000000..0be2943b4 --- /dev/null +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/dimension/DataAmountTest.java @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2010-2022 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.library.dimension; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.junit.jupiter.api.Test; +import org.openhab.core.library.types.QuantityType; +import org.openhab.core.library.unit.Units; + +/** + * The {@link DataAmountTest} holds tests for {@link DataAmount} + * + * @author Jan N. Klug - Initial contribution + */ +@NonNullByDefault +public class DataAmountTest { + + @Test + public void testBToMB() { + QuantityType quantityType = new QuantityType("1000 B"); + QuantityType converted = quantityType.toUnit("MB"); + + assertThat(converted.toString(), is(equalTo("0.001 MB"))); + } + + @Test + public void testKBToMB() { + QuantityType quantityType = new QuantityType("1000 kB"); + QuantityType converted = quantityType.toUnit("MB"); + + assertThat(converted.toString(), is(equalTo("1 MB"))); + } + + @Test + public void testInvertibleUnit() { + QuantityType quantityType = new QuantityType("1000 KB"); + QuantityType inverted = quantityType.toInvertibleUnit(Units.MEGABYTE); + + assertThat(quantityType, is(equalTo(inverted))); + } +}