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