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