2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.common;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
15 import java.io.ByteArrayInputStream;
16 import java.io.ByteArrayOutputStream;
17 import java.io.IOException;
18 import java.io.ObjectInputStream;
19 import java.io.ObjectOutputStream;
20 import java.time.format.DateTimeParseException;
21 import java.util.Optional;
22 import org.junit.Test;
24 public class RevisionTest {
26 public void testOf() {
27 assertEquals("2017-12-25", Revision.of("2017-12-25").toString());
31 public void testOfNull() {
32 assertThrows(NullPointerException.class, () -> Revision.of(null));
36 public void testOfEmpty() {
37 assertThrows(DateTimeParseException.class, () -> Revision.of(""));
41 public void testOfInvalid() {
42 assertThrows(DateTimeParseException.class, () -> Revision.of("invalid"));
46 public void testOfInvalidDate1() {
47 assertThrows(DateTimeParseException.class, () -> Revision.of("2017-13-01"));
51 public void testOfInvalidDate2() {
52 assertThrows(DateTimeParseException.class, () -> Revision.of("2017-12-00"));
56 public void testOfInvalidDate3() {
57 assertThrows(DateTimeParseException.class, () -> Revision.of("2017-12-32"));
61 public void testEquals() {
62 final Revision rev1 = Revision.of("2017-12-25");
63 final Revision rev1dup = Revision.of("2017-12-25");
64 final Revision rev2 = Revision.of("2017-12-26");
66 assertFalse(rev1.equals(null));
67 assertTrue(rev1.equals(rev1));
68 assertTrue(rev1.equals(rev1dup));
69 assertTrue(rev1dup.equals(rev1));
70 assertFalse(rev1.equals(rev2));
71 assertFalse(rev2.equals(rev1));
75 public void testOfNullable() {
76 assertEquals(Optional.empty(), Revision.ofNullable(null));
78 final Optional<Revision> opt = Revision.ofNullable("2017-12-25");
79 assertTrue(opt.isPresent());
80 assertEquals("2017-12-25", opt.get().toString());
84 public void testCompareOptional() {
85 assertEquals(0, Revision.compare(Optional.empty(), Optional.empty()));
86 assertEquals(0, Revision.compare(Revision.ofNullable("2017-12-25"), Revision.ofNullable("2017-12-25")));
87 assertEquals(-1, Revision.compare(Optional.empty(), Revision.ofNullable("2017-12-25")));
88 assertEquals(1, Revision.compare(Revision.ofNullable("2017-12-25"), Optional.empty()));
92 public void testSerialization() throws IOException, ClassNotFoundException {
93 final Revision source = Revision.of("2017-12-25");
94 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
95 try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
96 oos.writeObject(source);
100 try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
101 read = ois.readObject();
104 assertEquals(source, read);