]> git.basschouten.com Git - openhab-addons.git/blob
fd7658a05a00b984443d0b0001cde27bf63470eb
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.junit.jupiter.api.Test;
20
21 /**
22  * @author Sami Salonen - Initial contribution
23  */
24 public class AtomicStampedKeyValueTest {
25
26     @Test
27     public void testInitWithNullValue() {
28         assertThrows(NullPointerException.class, () -> new AtomicStampedValue<>(0, null));
29     }
30
31     @Test
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)));
37     }
38
39     @Test
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))));
46     }
47
48     @Test
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))));
55     }
56
57     @Test
58     public void testCopy() {
59         Object val = new Object();
60         AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
61         AtomicStampedValue<Object> copy = keyValue.copy();
62
63         // unchanged
64         assertThat(keyValue.getStamp(), is(equalTo(42L)));
65         assertThat(keyValue.getValue(), is(equalTo(val)));
66
67         // data matches
68         assertThat(keyValue.getStamp(), is(equalTo(copy.getStamp())));
69         assertThat(keyValue.getValue(), is(equalTo(copy.getValue())));
70
71         // after update they live life of their own
72         Object val2 = new Object();
73         copy.update(-99L, val2);
74
75         assertThat(keyValue.getStamp(), is(equalTo(42L)));
76         assertThat(keyValue.getValue(), is(equalTo(val)));
77
78         assertThat(copy.getStamp(), is(equalTo(-99L)));
79         assertThat(copy.getValue(), is(equalTo(val2)));
80     }
81
82     /**
83      * instance(stamp=x).copyIfStampAfter(x)
84      */
85     @Test
86     public void testCopyIfStampAfterEqual() {
87         Object val = new Object();
88         AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
89         AtomicStampedValue<Object> copy = keyValue.copyIfStampAfter(42L);
90
91         // keyValue unchanged
92         assertThat(keyValue.getStamp(), is(equalTo(42L)));
93         assertThat(keyValue.getValue(), is(equalTo(val)));
94
95         // data matches
96         assertThat(keyValue.getStamp(), is(equalTo(copy.getStamp())));
97         assertThat(keyValue.getValue(), is(equalTo(copy.getValue())));
98
99         // after update they live life of their own
100         Object val2 = new Object();
101         copy.update(-99L, val2);
102
103         assertThat(keyValue.getStamp(), is(equalTo(42L)));
104         assertThat(keyValue.getValue(), is(equalTo(val)));
105
106         assertThat(copy.getStamp(), is(equalTo(-99L)));
107         assertThat(copy.getValue(), is(equalTo(val2)));
108     }
109
110     /**
111      * instance(stamp=x-1).copyIfStampAfter(x)
112      */
113     @Test
114     public void testCopyIfStampAfterTooOld() {
115         Object val = new Object();
116         AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
117         AtomicStampedValue<Object> copy = keyValue.copyIfStampAfter(43L);
118
119         // keyValue unchanged
120         assertThat(keyValue.getStamp(), is(equalTo(42L)));
121         assertThat(keyValue.getValue(), is(equalTo(val)));
122
123         // copy is null
124         assertThat(copy, is(nullValue()));
125     }
126
127     /**
128      * instance(stamp=x).copyIfStampAfter(x-1)
129      */
130     @Test
131     public void testCopyIfStampAfterFresh() {
132         Object val = new Object();
133         AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
134         AtomicStampedValue<Object> copy = keyValue.copyIfStampAfter(41L);
135
136         // keyValue unchanged
137         assertThat(keyValue.getStamp(), is(equalTo(42L)));
138         assertThat(keyValue.getValue(), is(equalTo(val)));
139
140         // data matches
141         assertThat(keyValue.getStamp(), is(equalTo(copy.getStamp())));
142         assertThat(keyValue.getValue(), is(equalTo(copy.getValue())));
143
144         // after update they live life of their own
145         Object val2 = new Object();
146         copy.update(-99L, val2);
147
148         assertThat(keyValue.getStamp(), is(equalTo(42L)));
149         assertThat(keyValue.getValue(), is(equalTo(val)));
150
151         assertThat(copy.getStamp(), is(equalTo(-99L)));
152         assertThat(copy.getValue(), is(equalTo(val2)));
153     }
154
155     @Test
156     public void testCompare() {
157         // equal, smaller, larger
158         assertThat(AtomicStampedValue.compare(new AtomicStampedValue<>(42L, ""), new AtomicStampedValue<>(42L, "")),
159                 is(equalTo(0)));
160         assertThat(AtomicStampedValue.compare(new AtomicStampedValue<>(41L, ""), new AtomicStampedValue<>(42L, "")),
161                 is(equalTo(-1)));
162         assertThat(AtomicStampedValue.compare(new AtomicStampedValue<>(42L, ""), new AtomicStampedValue<>(41L, "")),
163                 is(equalTo(1)));
164
165         // Nulls come first
166         assertThat(AtomicStampedValue.compare(null, new AtomicStampedValue<>(42L, "")), is(equalTo(-1)));
167         assertThat(AtomicStampedValue.compare(new AtomicStampedValue<>(42L, ""), null), is(equalTo(1)));
168     }
169 }