2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.common;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
12 import static org.junit.Assert.assertTrue;
14 import java.net.URISyntaxException;
15 import org.junit.Test;
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);
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()));
32 public void testStringSerializationNoRevision() throws Exception {
34 QName qname = QName.create(NS, LOCALNAME);
35 assertEquals(QName.QNAME_LEFT_PARENTHESIS + NAMESPACE + QName.QNAME_RIGHT_PARENTHESIS + LOCALNAME,
37 assertEquals(qname, QName.create(qname.toString()));
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, "&"));
51 public void testCompareTo() throws Exception {
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);
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);
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);
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);
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());
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());