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.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertThrows;
12 import static org.junit.jupiter.api.Assertions.assertTrue;
14 import org.junit.jupiter.api.Test;
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);
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()));
30 public void testStringSerializationNoRevision() throws Exception {
32 var qname = QName.create(NS, LOCALNAME);
33 assertEquals("(urn:foo)bar", qname.toString());
34 assertEquals(qname, QName.create(qname.toString()));
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, "&"));
48 public void testCompareTo() throws Exception {
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);
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);
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);
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);
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());
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());