Migrate yang-common to JUnit5
[yangtools.git] / common / yang-common / src / test / java / org / opendaylight / yangtools / yang / common / RevisionTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.common;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertFalse;
12 import static org.junit.jupiter.api.Assertions.assertThrows;
13 import static org.junit.jupiter.api.Assertions.assertTrue;
14
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.jupiter.api.Test;
23
24 public class RevisionTest {
25     @Test
26     public void testOf() {
27         assertEquals("2017-12-25", Revision.of("2017-12-25").toString());
28     }
29
30     @Test
31     public void testOfNull() {
32         assertThrows(NullPointerException.class, () -> Revision.of(null));
33     }
34
35     @Test
36     public void testOfEmpty() {
37         assertThrows(DateTimeParseException.class, () -> Revision.of(""));
38     }
39
40     @Test
41     public void testOfInvalid() {
42         assertThrows(DateTimeParseException.class, () -> Revision.of("invalid"));
43     }
44
45     @Test
46     public void testOfInvalidDate1() {
47         assertThrows(DateTimeParseException.class, () -> Revision.of("2017-13-01"));
48     }
49
50     @Test
51     public void testOfInvalidDate2() {
52         assertThrows(DateTimeParseException.class, () -> Revision.of("2017-12-00"));
53     }
54
55     @Test
56     public void testOfInvalidDate3() {
57         assertThrows(DateTimeParseException.class, () -> Revision.of("2017-12-32"));
58     }
59
60     @Test
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");
65
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));
72     }
73
74     @Test
75     public void testOfNullable() {
76         assertEquals(Optional.empty(), Revision.ofNullable(null));
77
78         final Optional<Revision> opt = Revision.ofNullable("2017-12-25");
79         assertTrue(opt.isPresent());
80         assertEquals("2017-12-25", opt.get().toString());
81     }
82
83     @Test
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()));
89     }
90
91     @Test
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);
97         }
98
99         final Object read;
100         try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
101             read = ois.readObject();
102         }
103
104         assertEquals(source, read);
105     }
106 }