Use RevisionUnion in QNameModule
[yangtools.git] / common / yang-common / src / test / java / org / opendaylight / yangtools / yang / common / QNameTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.assertThrows;
12 import static org.junit.jupiter.api.Assertions.assertTrue;
13
14 import org.junit.jupiter.api.Test;
15
16 public class QNameTest {
17     private static final String NAMESPACE = "urn:foo";
18     private static final String REVISION = "2013-12-24";
19     private static final String LOCALNAME = "bar";
20     private static final XMLNamespace NS = XMLNamespace.of(NAMESPACE);
21
22     @Test
23     public void testStringSerialization() throws Exception {
24         var qname = QName.create(NAMESPACE, REVISION, LOCALNAME);
25         assertEquals("(urn:foo?revision=2013-12-24)bar", qname.toString());
26         assertEquals(qname, QName.create(qname.toString()));
27     }
28
29     @Test
30     public void testStringSerializationNoRevision() throws Exception {
31         // no revision
32         var qname = QName.create(NS, LOCALNAME);
33         assertEquals("(urn:foo)bar", qname.toString());
34         assertEquals(qname, QName.create(qname.toString()));
35     }
36
37     @Test
38     public void testIllegalLocalNames() {
39         assertThrows(NullPointerException.class, () -> QName.create(NS, null));
40         assertThrows(IllegalArgumentException.class, () -> QName.create(NS, ""));
41         assertThrows(IllegalArgumentException.class, () -> QName.create(NS, "("));
42         assertThrows(IllegalArgumentException.class, () -> QName.create(NS, ")"));
43         assertThrows(IllegalArgumentException.class, () -> QName.create(NS, "?"));
44         assertThrows(IllegalArgumentException.class, () -> QName.create(NS, "&"));
45     }
46
47     @Test
48     public void testCompareTo() throws Exception {
49         final String A = "a";
50         final String B = "b";
51
52         // compare with namespace
53         var qa = QName.create(A, REVISION, A);
54         var qb = QName.create(B, REVISION, A);
55         assertTrue(qa.compareTo(qb) < 0);
56         assertTrue(qb.compareTo(qa) > 0);
57
58         // compare with revision
59         qa = QName.create(A, "2013-12-24", A);
60         qb = QName.create(A, "2013-12-25", A);
61         assertTrue(qa.compareTo(qb) < 0);
62         assertTrue(qb.compareTo(qa) > 0);
63
64         // compare with 1 null revision
65         qa = QName.create(XMLNamespace.of(A), A);
66         qb = QName.create(XMLNamespace.of(A), Revision.of(REVISION), A);
67         assertTrue(qa.compareTo(qb) < 0);
68         assertTrue(qb.compareTo(qa) > 0);
69
70         // compare with both null revision
71         qb = QName.create(XMLNamespace.of(A), A);
72         assertTrue(qa.compareTo(qb) == 0);
73         assertTrue(qb.compareTo(qa) == 0);
74     }
75
76     @Test
77     public void testQName() {
78         final var qname = QName.create(NAMESPACE, REVISION, LOCALNAME);
79         final var qname1 = QName.create(NAMESPACE, LOCALNAME);
80         final var qname2 = QName.create(qname1, LOCALNAME);
81         assertEquals(qname1, qname.withoutRevision());
82         assertEquals(qname1, qname2);
83         assertTrue(qname.isEqualWithoutRevision(qname1));
84         assertEquals("2000-01-01", QName.formattedRevision(Revision.ofNullable("2000-01-01")));
85         assertEquals(qname, qname.intern());
86     }
87
88     @Test
89     public void testQNameModule() throws Exception {
90         final var qnameModule = QNameModule.of(NS, Revision.of("2000-01-01"));
91         assertEquals("QNameModule{ns=urn:foo, rev=2000-01-01}", qnameModule.toString());
92     }
93 }