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("(urn:foo?revision=2013-12-24)bar", qname.toString());
27 assertEquals(qname, QName.create(qname.toString()));
31 public void testStringSerializationNoRevision() throws Exception {
33 QName qname = QName.create(NS, LOCALNAME);
34 assertEquals("(urn:foo)bar", qname.toString());
35 assertEquals(qname, QName.create(qname.toString()));
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, "&"));
49 public void testCompareTo() throws Exception {
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);
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);
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);
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);
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());
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());