]> git.basschouten.com Git - openhab-addons.git/blob
ba245148e88e5d4fc47d67bebe00b2761ee3d856
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.modbus.internal;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.assertThrows;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.junit.jupiter.api.Test;
21
22 /**
23  * @author Sami Salonen - Initial contribution
24  */
25 @NonNullByDefault
26 public class AtomicStampedKeyValueTest {
27
28     @Test
29     public void testInitWithNullValue() {
30         assertThrows(NullPointerException.class, () -> new AtomicStampedValue<>(0, null));
31     }
32
33     @Test
34     public void testGetters() {
35         Object val = new Object();
36         AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
37         assertThat(keyValue.getStamp(), is(equalTo(42L)));
38         assertThat(keyValue.getValue(), is(equalTo(val)));
39     }
40
41     @Test
42     public void testUpdateWithSameStamp() {
43         Object val = new Object();
44         AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
45         keyValue.update(42L, new Object());
46         assertThat(keyValue.getStamp(), is(equalTo(42L)));
47         assertThat(keyValue.getValue(), is(not(equalTo(val))));
48     }
49
50     @Test
51     public void testUpdateWithDifferentStamp() {
52         Object val = new Object();
53         AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
54         keyValue.update(-99L, new Object());
55         assertThat(keyValue.getStamp(), is(equalTo(-99L)));
56         assertThat(keyValue.getValue(), is(not(equalTo(val))));
57     }
58
59     @Test
60     public void testCopy() {
61         Object val = new Object();
62         AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
63         AtomicStampedValue<Object> copy = keyValue.copy();
64
65         // unchanged
66         assertThat(keyValue.getStamp(), is(equalTo(42L)));
67         assertThat(keyValue.getValue(), is(equalTo(val)));
68
69         // data matches
70         assertThat(keyValue.getStamp(), is(equalTo(copy.getStamp())));
71         assertThat(keyValue.getValue(), is(equalTo(copy.getValue())));
72
73         // after update they live life of their own
74         Object val2 = new Object();
75         copy.update(-99L, val2);
76
77         assertThat(keyValue.getStamp(), is(equalTo(42L)));
78         assertThat(keyValue.getValue(), is(equalTo(val)));
79
80         assertThat(copy.getStamp(), is(equalTo(-99L)));
81         assertThat(copy.getValue(), is(equalTo(val2)));
82     }
83
84     /**
85      * instance(stamp=x).copyIfStampAfter(x)
86      */
87     @Test
88     public void testCopyIfStampAfterEqual() {
89         Object val = new Object();
90         AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
91         AtomicStampedValue<Object> copy = keyValue.copyIfStampAfter(42L);
92
93         // keyValue unchanged
94         assertThat(keyValue.getStamp(), is(equalTo(42L)));
95         assertThat(keyValue.getValue(), is(equalTo(val)));
96
97         // data matches
98         assertThat(keyValue.getStamp(), is(equalTo(copy.getStamp())));
99         assertThat(keyValue.getValue(), is(equalTo(copy.getValue())));
100
101         // after update they live life of their own
102         Object val2 = new Object();
103         copy.update(-99L, val2);
104
105         assertThat(keyValue.getStamp(), is(equalTo(42L)));
106         assertThat(keyValue.getValue(), is(equalTo(val)));
107
108         assertThat(copy.getStamp(), is(equalTo(-99L)));
109         assertThat(copy.getValue(), is(equalTo(val2)));
110     }
111
112     /**
113      * instance(stamp=x-1).copyIfStampAfter(x)
114      */
115     @Test
116     public void testCopyIfStampAfterTooOld() {
117         Object val = new Object();
118         AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
119         AtomicStampedValue<Object> copy = keyValue.copyIfStampAfter(43L);
120
121         // keyValue unchanged
122         assertThat(keyValue.getStamp(), is(equalTo(42L)));
123         assertThat(keyValue.getValue(), is(equalTo(val)));
124
125         // copy is null
126         assertThat(copy, is(nullValue()));
127     }
128
129     /**
130      * instance(stamp=x).copyIfStampAfter(x-1)
131      */
132     @Test
133     public void testCopyIfStampAfterFresh() {
134         Object val = new Object();
135         AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
136         AtomicStampedValue<Object> copy = keyValue.copyIfStampAfter(41L);
137
138         // keyValue unchanged
139         assertThat(keyValue.getStamp(), is(equalTo(42L)));
140         assertThat(keyValue.getValue(), is(equalTo(val)));
141
142         // data matches
143         assertThat(keyValue.getStamp(), is(equalTo(copy.getStamp())));
144         assertThat(keyValue.getValue(), is(equalTo(copy.getValue())));
145
146         // after update they live life of their own
147         Object val2 = new Object();
148         copy.update(-99L, val2);
149
150         assertThat(keyValue.getStamp(), is(equalTo(42L)));
151         assertThat(keyValue.getValue(), is(equalTo(val)));
152
153         assertThat(copy.getStamp(), is(equalTo(-99L)));
154         assertThat(copy.getValue(), is(equalTo(val2)));
155     }
156
157     @Test
158     public void testCompare() {
159         // equal, smaller, larger
160         assertThat(AtomicStampedValue.compare(new AtomicStampedValue<>(42L, ""), new AtomicStampedValue<>(42L, "")),
161                 is(equalTo(0)));
162         assertThat(AtomicStampedValue.compare(new AtomicStampedValue<>(41L, ""), new AtomicStampedValue<>(42L, "")),
163                 is(equalTo(-1)));
164         assertThat(AtomicStampedValue.compare(new AtomicStampedValue<>(42L, ""), new AtomicStampedValue<>(41L, "")),
165                 is(equalTo(1)));
166
167         // Nulls come first
168         assertThat(AtomicStampedValue.compare(null, new AtomicStampedValue<>(42L, "")), is(equalTo(-1)));
169         assertThat(AtomicStampedValue.compare(new AtomicStampedValue<>(42L, ""), null), is(equalTo(1)));
170     }
171 }