Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.impl; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 17 | |
| 18 | import static org.junit.Assert.*; |
| 19 | |
| 20 | import java.nio.ByteBuffer; |
| 21 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 22 | import org.junit.Test; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.store.Timestamp; |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 24 | import org.onlab.util.KryoNamespace; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 25 | |
| 26 | import com.google.common.testing.EqualsTester; |
| 27 | |
Yuta HIGUCHI | 273dcc8 | 2014-10-03 00:32:12 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Test of {@link Timestamped}. |
| 30 | */ |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 31 | public class TimestampedTest { |
| 32 | |
Yuta HIGUCHI | 273dcc8 | 2014-10-03 00:32:12 -0700 | [diff] [blame] | 33 | 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 HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 36 | |
| 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 HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 95 | final KryoNamespace kryos = KryoNamespace.newBuilder() |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 96 | .register(Timestamped.class, |
Yuta HIGUCHI | 273dcc8 | 2014-10-03 00:32:12 -0700 | [diff] [blame] | 97 | MastershipBasedTimestamp.class) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 98 | .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 | } |