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.assertSame;
11 import static org.junit.jupiter.api.Assertions.assertEquals;
12 import static org.junit.jupiter.api.Assertions.assertFalse;
13 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
14 import static org.junit.jupiter.api.Assertions.assertThrows;
15 import static org.junit.jupiter.api.Assertions.assertTrue;
17 import java.io.ByteArrayInputStream;
18 import java.io.ByteArrayOutputStream;
19 import java.io.ObjectInputStream;
20 import java.io.ObjectOutputStream;
21 import java.time.format.DateTimeParseException;
22 import java.util.Optional;
23 import org.junit.jupiter.api.Test;
25 public class RevisionTest {
28 assertEquals("2017-12-25", Revision.of("2017-12-25").toString());
33 assertThrows(NullPointerException.class, () -> Revision.of(null));
38 assertThrowsParse("", "Text '' could not be parsed at index 0");
42 void testOfInvalid() {
43 assertThrowsParse("invalid", "Text 'invalid' could not be parsed at index 0");
47 void testOfInvalidDate1() {
48 assertThrowsParse("2017-13-01",
49 "Text '2017-13-01' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 13");
53 void testOfInvalidDate2() {
54 assertThrowsParse("2017-12-00",
55 "Text '2017-12-00' could not be parsed: Invalid value for DayOfMonth (valid values 1 - 28/31): 0");
59 void testOfInvalidDate3() {
60 assertThrowsParse("2017-12-32",
61 "Text '2017-12-32' could not be parsed: Invalid value for DayOfMonth (valid values 1 - 28/31): 32");
64 private static void assertThrowsParse(final String input, final String expectedMessage) {
65 final var ex = assertThrows(DateTimeParseException.class, () -> Revision.of(input));
66 assertEquals(expectedMessage, ex.getMessage());
71 final var rev1 = Revision.of("2017-12-25");
72 final var rev1dup = Revision.of("2017-12-25");
73 final var rev2 = Revision.of("2017-12-26");
75 assertFalse(rev1.equals(null));
76 assertTrue(rev1.equals(rev1));
77 assertTrue(rev1.equals(rev1dup));
78 assertTrue(rev1dup.equals(rev1));
79 assertFalse(rev1.equals(rev2));
80 assertFalse(rev2.equals(rev1));
84 void testOfNullable() {
85 assertEquals(Optional.empty(), Revision.ofNullable(null));
86 assertEquals(Optional.of(Revision.of("2017-12-25")), Revision.ofNullable("2017-12-25"));
90 void testCompareOptional() {
91 assertEquals(0, Revision.compare(Optional.empty(), Optional.empty()));
92 assertEquals(0, Revision.compare(Revision.ofNullable("2017-12-25"), Revision.ofNullable("2017-12-25")));
93 assertEquals(-1, Revision.compare(Optional.empty(), Revision.ofNullable("2017-12-25")));
94 assertEquals(1, Revision.compare(Revision.ofNullable("2017-12-25"), Optional.empty()));
98 void testSerializationRevision() throws Exception {
99 final var revision = Revision.of("2017-12-25");
100 assertEquals(revision, testSerialization(revision, 78));
104 void testSerializationNotRevision() throws Exception {
105 assertSame(NotRevision.of(), testSerialization(NotRevision.of(), 68));
108 private static RevisionUnion testSerialization(final RevisionUnion obj, final int expectedLength) throws Exception {
109 final var bos = new ByteArrayOutputStream();
110 try (var oos = new ObjectOutputStream(bos)) {
111 oos.writeObject(obj);
114 final var bytes = bos.toByteArray();
115 assertEquals(expectedLength, bytes.length);
117 try (var ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
118 return assertInstanceOf(obj.getClass(), ois.readObject());