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;
15 import java.net.URISyntaxException;
16 import org.junit.Test;
18 public class QNameTest {
19 private static final String NAMESPACE = "urn:foo";
20 private static final String REVISION = "2013-12-24";
21 private static final String LOCALNAME = "bar";
22 private static final URI NS = URI.create(NAMESPACE);
25 public void testStringSerialization() throws Exception {
26 QName qname = QName.create(NAMESPACE, REVISION, LOCALNAME);
27 assertEquals(QName.QNAME_LEFT_PARENTHESIS + NAMESPACE + QName.QNAME_REVISION_DELIMITER + REVISION
28 + QName.QNAME_RIGHT_PARENTHESIS + LOCALNAME, qname.toString());
29 assertEquals(qname, QName.create(qname.toString()));
33 public void testStringSerializationNoRevision() throws Exception {
35 QName qname = QName.create(NS, LOCALNAME);
36 assertEquals(QName.QNAME_LEFT_PARENTHESIS + NAMESPACE + QName.QNAME_RIGHT_PARENTHESIS + LOCALNAME,
38 assertEquals(qname, QName.create(qname.toString()));
42 public void testIllegalLocalNames() {
43 assertThrows(NullPointerException.class, () -> QName.create(NS, null));
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 assertThrows(IllegalArgumentException.class, () -> QName.create(NS, "&"));
52 public void testCompareTo() throws Exception {
56 // compare with namespace
57 QName qa = QName.create(A, REVISION, A);
58 QName qb = QName.create(B, REVISION, A);
59 assertTrue(qa.compareTo(qb) < 0);
60 assertTrue(qb.compareTo(qa) > 0);
62 // compare with revision
63 qa = QName.create(A, "2013-12-24", A);
64 qb = QName.create(A, "2013-12-25", A);
65 assertTrue(qa.compareTo(qb) < 0);
66 assertTrue(qb.compareTo(qa) > 0);
68 // compare with 1 null revision
69 qa = QName.create(URI.create(A), A);
70 qb = QName.create(URI.create(A), Revision.of(REVISION), A);
71 assertTrue(qa.compareTo(qb) < 0);
72 assertTrue(qb.compareTo(qa) > 0);
74 // compare with both null revision
75 qb = QName.create(URI.create(A), A);
76 assertTrue(qa.compareTo(qb) == 0);
77 assertTrue(qb.compareTo(qa) == 0);
81 public void testQName() {
82 final QName qname = QName.create(NAMESPACE, REVISION, LOCALNAME);
83 final QName qname1 = QName.create(NAMESPACE, LOCALNAME);
84 final QName qname2 = QName.create(qname1, LOCALNAME);
85 assertEquals(qname1, qname.withoutRevision());
86 assertEquals(qname1, qname2);
87 assertTrue(qname.isEqualWithoutRevision(qname1));
88 assertEquals("2000-01-01", QName.formattedRevision(Revision.ofNullable("2000-01-01")));
89 assertEquals(qname, qname.intern());
93 public void testQNameModule() throws URISyntaxException {
94 final QNameModule qnameModule = QNameModule.create(NS, Revision.of("2000-01-01"));
95 assertEquals("QNameModule{ns=urn:foo, rev=2000-01-01}", qnameModule.toString());