2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.modbus.internal;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.assertThrows;
19 import org.junit.jupiter.api.Test;
22 * @author Sami Salonen - Initial contribution
24 public class AtomicStampedKeyValueTest {
27 public void testInitWithNullValue() {
28 assertThrows(NullPointerException.class, () -> new AtomicStampedValue<>(0, null));
32 public void testGetters() {
33 Object val = new Object();
34 AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
35 assertThat(keyValue.getStamp(), is(equalTo(42L)));
36 assertThat(keyValue.getValue(), is(equalTo(val)));
40 public void testUpdateWithSameStamp() {
41 Object val = new Object();
42 AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
43 keyValue.update(42L, new Object());
44 assertThat(keyValue.getStamp(), is(equalTo(42L)));
45 assertThat(keyValue.getValue(), is(not(equalTo(val))));
49 public void testUpdateWithDifferentStamp() {
50 Object val = new Object();
51 AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
52 keyValue.update(-99L, new Object());
53 assertThat(keyValue.getStamp(), is(equalTo(-99L)));
54 assertThat(keyValue.getValue(), is(not(equalTo(val))));
58 public void testCopy() {
59 Object val = new Object();
60 AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
61 AtomicStampedValue<Object> copy = keyValue.copy();
64 assertThat(keyValue.getStamp(), is(equalTo(42L)));
65 assertThat(keyValue.getValue(), is(equalTo(val)));
68 assertThat(keyValue.getStamp(), is(equalTo(copy.getStamp())));
69 assertThat(keyValue.getValue(), is(equalTo(copy.getValue())));
71 // after update they live life of their own
72 Object val2 = new Object();
73 copy.update(-99L, val2);
75 assertThat(keyValue.getStamp(), is(equalTo(42L)));
76 assertThat(keyValue.getValue(), is(equalTo(val)));
78 assertThat(copy.getStamp(), is(equalTo(-99L)));
79 assertThat(copy.getValue(), is(equalTo(val2)));
83 * instance(stamp=x).copyIfStampAfter(x)
86 public void testCopyIfStampAfterEqual() {
87 Object key = new Object();
88 Object val = new Object();
89 AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
90 AtomicStampedValue<Object> copy = keyValue.copyIfStampAfter(42L);
93 assertThat(keyValue.getStamp(), is(equalTo(42L)));
94 assertThat(keyValue.getValue(), is(equalTo(val)));
97 assertThat(keyValue.getStamp(), is(equalTo(copy.getStamp())));
98 assertThat(keyValue.getValue(), is(equalTo(copy.getValue())));
100 // after update they live life of their own
101 Object key2 = new Object();
102 Object val2 = new Object();
103 copy.update(-99L, val2);
105 assertThat(keyValue.getStamp(), is(equalTo(42L)));
106 assertThat(keyValue.getValue(), is(equalTo(val)));
108 assertThat(copy.getStamp(), is(equalTo(-99L)));
109 assertThat(copy.getValue(), is(equalTo(val2)));
113 * instance(stamp=x-1).copyIfStampAfter(x)
116 public void testCopyIfStampAfterTooOld() {
117 Object val = new Object();
118 AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
119 AtomicStampedValue<Object> copy = keyValue.copyIfStampAfter(43L);
121 // keyValue unchanged
122 assertThat(keyValue.getStamp(), is(equalTo(42L)));
123 assertThat(keyValue.getValue(), is(equalTo(val)));
126 assertThat(copy, is(nullValue()));
130 * instance(stamp=x).copyIfStampAfter(x-1)
133 public void testCopyIfStampAfterFresh() {
134 Object val = new Object();
135 AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
136 AtomicStampedValue<Object> copy = keyValue.copyIfStampAfter(41L);
138 // keyValue unchanged
139 assertThat(keyValue.getStamp(), is(equalTo(42L)));
140 assertThat(keyValue.getValue(), is(equalTo(val)));
143 assertThat(keyValue.getStamp(), is(equalTo(copy.getStamp())));
144 assertThat(keyValue.getValue(), is(equalTo(copy.getValue())));
146 // after update they live life of their own
147 Object key2 = new Object();
148 Object val2 = new Object();
149 copy.update(-99L, val2);
151 assertThat(keyValue.getStamp(), is(equalTo(42L)));
152 assertThat(keyValue.getValue(), is(equalTo(val)));
154 assertThat(copy.getStamp(), is(equalTo(-99L)));
155 assertThat(copy.getValue(), is(equalTo(val2)));
159 public void testCompare() {
160 // equal, smaller, larger
161 assertThat(AtomicStampedValue.compare(new AtomicStampedValue<>(42L, ""), new AtomicStampedValue<>(42L, "")),
163 assertThat(AtomicStampedValue.compare(new AtomicStampedValue<>(41L, ""), new AtomicStampedValue<>(42L, "")),
165 assertThat(AtomicStampedValue.compare(new AtomicStampedValue<>(42L, ""), new AtomicStampedValue<>(41L, "")),
169 assertThat(AtomicStampedValue.compare(null, new AtomicStampedValue<>(42L, "")), is(equalTo(-1)));
170 assertThat(AtomicStampedValue.compare(new AtomicStampedValue<>(42L, ""), null), is(equalTo(1)));