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