blob: 6303be8004b56d100417eb10dded7d3df92348bb [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Yuta HIGUCHIeecee552014-10-16 14:09:01 -070016package org.onlab.onos.store.impl;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070017
18import static org.junit.Assert.*;
19
20import java.nio.ByteBuffer;
21
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070022import org.junit.Test;
23import org.onlab.onos.store.Timestamp;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070024import org.onlab.util.KryoNamespace;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070025
26import com.google.common.testing.EqualsTester;
27
Yuta HIGUCHI273dcc82014-10-03 00:32:12 -070028/**
29 * Test of {@link Timestamped}.
30 */
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070031public class TimestampedTest {
32
Yuta HIGUCHI273dcc82014-10-03 00:32:12 -070033 private static final Timestamp TS_1_1 = new MastershipBasedTimestamp(1, 1);
34 private static final Timestamp TS_1_2 = new MastershipBasedTimestamp(1, 2);
35 private static final Timestamp TS_2_1 = new MastershipBasedTimestamp(2, 1);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070036
37 @Test
38 public final void testHashCode() {
39 Timestamped<String> a = new Timestamped<>("a", TS_1_1);
40 Timestamped<String> b = new Timestamped<>("b", TS_1_1);
41 assertTrue("value does not impact hashCode",
42 a.hashCode() == b.hashCode());
43 }
44
45 @Test
46 public final void testEquals() {
47 Timestamped<String> a = new Timestamped<>("a", TS_1_1);
48 Timestamped<String> b = new Timestamped<>("b", TS_1_1);
49 assertTrue("value does not impact equality",
50 a.equals(b));
51
52 new EqualsTester()
53 .addEqualityGroup(new Timestamped<>("a", TS_1_1),
54 new Timestamped<>("b", TS_1_1),
55 new Timestamped<>("c", TS_1_1))
56 .addEqualityGroup(new Timestamped<>("a", TS_1_2),
57 new Timestamped<>("b", TS_1_2),
58 new Timestamped<>("c", TS_1_2))
59 .addEqualityGroup(new Timestamped<>("a", TS_2_1),
60 new Timestamped<>("b", TS_2_1),
61 new Timestamped<>("c", TS_2_1))
62 .testEquals();
63
64 }
65
66 @Test
67 public final void testValue() {
68 final Integer n = Integer.valueOf(42);
69 Timestamped<Integer> tsv = new Timestamped<>(n, TS_1_1);
70 assertSame(n, tsv.value());
71
72 }
73
74 @Test(expected = NullPointerException.class)
75 public final void testValueNonNull() {
76 new Timestamped<>(null, TS_1_1);
77 }
78
79 @Test(expected = NullPointerException.class)
80 public final void testTimestampNonNull() {
81 new Timestamped<>("Foo", null);
82 }
83
84 @Test
85 public final void testIsNewer() {
86 Timestamped<String> a = new Timestamped<>("a", TS_1_2);
87 Timestamped<String> b = new Timestamped<>("b", TS_1_1);
88 assertTrue(a.isNewer(b));
89 assertFalse(b.isNewer(a));
90 }
91
92 @Test
93 public final void testKryoSerializable() {
94 final ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070095 final KryoNamespace kryos = KryoNamespace.newBuilder()
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070096 .register(Timestamped.class,
Yuta HIGUCHI273dcc82014-10-03 00:32:12 -070097 MastershipBasedTimestamp.class)
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070098 .build();
99
100 Timestamped<String> original = new Timestamped<>("foobar", TS_1_1);
101 kryos.serialize(original, buffer);
102 buffer.flip();
103 Timestamped<String> copy = kryos.deserialize(buffer);
104
105 new EqualsTester()
106 .addEqualityGroup(original, copy)
107 .testEquals();
108 }
109}