diff --git a/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/internal/types/StateDescriptionFragmentImpl.java b/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/internal/types/StateDescriptionFragmentImpl.java index e6f4a27617c..5fa1e9c5104 100644 --- a/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/internal/types/StateDescriptionFragmentImpl.java +++ b/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/internal/types/StateDescriptionFragmentImpl.java @@ -187,7 +187,7 @@ public StateDescriptionFragment merge(StateDescriptionFragment fragment) { if (this.readOnly == null) { this.readOnly = fragment.isReadOnly(); } - if (this.options == null) { + if (this.options == null || this.options.isEmpty()) { this.options = fragment.getOptions(); } diff --git a/itests/org.openhab.core.tests/src/main/java/org/eclipse/smarthome/core/internal/types/StateDescriptionFragmentImplTest.java b/bundles/org.openhab.core/src/test/java/org/eclipse/smarthome/core/internal/types/StateDescriptionFragmentImplTest.java similarity index 72% rename from itests/org.openhab.core.tests/src/main/java/org/eclipse/smarthome/core/internal/types/StateDescriptionFragmentImplTest.java rename to bundles/org.openhab.core/src/test/java/org/eclipse/smarthome/core/internal/types/StateDescriptionFragmentImplTest.java index 508eaa160c4..9d8d9133b4d 100644 --- a/itests/org.openhab.core.tests/src/main/java/org/eclipse/smarthome/core/internal/types/StateDescriptionFragmentImplTest.java +++ b/bundles/org.openhab.core/src/test/java/org/eclipse/smarthome/core/internal/types/StateDescriptionFragmentImplTest.java @@ -12,14 +12,15 @@ */ package org.eclipse.smarthome.core.internal.types; -import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.assertThat; import java.math.BigDecimal; -import java.util.ArrayList; +import java.util.Collections; import org.eclipse.smarthome.core.types.StateDescription; import org.eclipse.smarthome.core.types.StateDescriptionFragment; +import org.eclipse.smarthome.core.types.StateOption; import org.junit.Before; import org.junit.Test; @@ -27,7 +28,6 @@ * Test the {@link StateDescriptionFragmentImpl}. * * @author Henning Treu - Initial contribution - * */ public class StateDescriptionFragmentImplTest { @@ -41,7 +41,7 @@ public void setup() { source.setStep(BigDecimal.ONE); source.setPattern("pattern"); source.setReadOnly(Boolean.TRUE); - source.setOptions(new ArrayList<>(0)); + source.setOptions(Collections.emptyList()); } @Test @@ -54,6 +54,21 @@ public void mergeFragment() { assertThat(fragment.getPattern(), is(source.getPattern())); assertThat(fragment.isReadOnly(), is(source.isReadOnly())); assertThat(fragment.getOptions(), is(source.getOptions())); + + // fragment with empty options should inherit new options + StateDescriptionFragmentImpl sourceWithOptions = new StateDescriptionFragmentImpl(); + sourceWithOptions.setOptions(Collections.singletonList(new StateOption("value1", "label1"))); + + fragment = source.merge(sourceWithOptions); + + assertThat(fragment.getOptions(), is(sourceWithOptions.getOptions())); + + // fragment with options should NOT inherit new options + sourceWithOptions.setOptions(Collections.singletonList(new StateOption("value2", "label2"))); + + fragment = source.merge(sourceWithOptions); + + assertThat(fragment.getOptions(), is(not(sourceWithOptions.getOptions()))); } @Test diff --git a/bundles/org.openhab.core/src/test/java/org/eclipse/smarthome/core/types/StateDescriptionFragmentBuilderTest.java b/bundles/org.openhab.core/src/test/java/org/eclipse/smarthome/core/types/StateDescriptionFragmentBuilderTest.java index 2e44f241260..1360f48ed5e 100644 --- a/bundles/org.openhab.core/src/test/java/org/eclipse/smarthome/core/types/StateDescriptionFragmentBuilderTest.java +++ b/bundles/org.openhab.core/src/test/java/org/eclipse/smarthome/core/types/StateDescriptionFragmentBuilderTest.java @@ -16,11 +16,9 @@ import static org.junit.Assert.assertThat; import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.Arrays; +import java.util.Collections; import java.util.List; -import org.eclipse.jdt.annotation.NonNull; import org.junit.Before; import org.junit.Test; @@ -64,15 +62,21 @@ public void builderWithReadOnly() { } @Test - public void builderWithIOptions() { - List<@NonNull StateOption> options = new ArrayList<>(0); + public void builderWithEmptyOptions() { + List options = Collections.emptyList(); + assertThat(builder.withOptions(options).build().getOptions(), is(options)); + } + + @Test + public void builderWithOptions() { + List options = Collections.singletonList(new StateOption("value", "label")); assertThat(builder.withOptions(options).build().getOptions(), is(options)); } @Test public void builderWithStateDescription() { StateDescription source = new StateDescription(BigDecimal.ZERO, BigDecimal.TEN, BigDecimal.ONE, "pattern", true, - Arrays.asList(new StateOption[] { new StateOption("value", "label") })); + Collections.singletonList(new StateOption("value", "label"))); StateDescriptionFragment fragment = StateDescriptionFragmentBuilder.create(source).build(); assertThat(fragment.getMinimum(), is(source.getMinimum())); @@ -86,13 +90,18 @@ public void builderWithStateDescription() { @Test public void subsequentBuildsCreateIndependentFragments() { StateDescriptionFragment fragment1 = builder.withMinimum(BigDecimal.ZERO).withMaximum(BigDecimal.TEN) - .withPattern("pattern").build(); + .withStep(BigDecimal.ONE).withPattern("pattern").withReadOnly(Boolean.FALSE) + .withOptions(Collections.singletonList(new StateOption("value", "label"))).build(); StateDescriptionFragment fragment2 = builder.withMinimum(BigDecimal.ONE).withMaximum(BigDecimal.ONE) - .withPattern("pattern_new").build(); + .withStep(BigDecimal.ZERO).withPattern("pattern_new").withReadOnly(Boolean.TRUE) + .withOptions(Collections.emptyList()).build(); assertThat(fragment1.getMinimum(), is(not(fragment2.getMinimum()))); assertThat(fragment1.getMaximum(), is(not(fragment2.getMaximum()))); + assertThat(fragment1.getStep(), is(not(fragment2.getStep()))); assertThat(fragment1.getPattern(), is(not(fragment2.getPattern()))); + assertThat(fragment1.isReadOnly(), is(not(fragment2.isReadOnly()))); + assertThat(fragment1.getOptions().size(), is(not(fragment2.getOptions().size()))); } }