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.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
16 import java.net.URISyntaxException;
17 import java.util.Optional;
18 import org.junit.Test;
20 public class QNameTest {
21 private static final String NAMESPACE = "urn:foo";
22 private static final String REVISION = "2013-12-24";
23 private static final String LOCALNAME = "bar";
24 private static final URI NS = URI.create(NAMESPACE);
27 public void testStringSerialization() throws Exception {
29 QName qname = QName.create(NAMESPACE, REVISION, LOCALNAME);
30 assertEquals(QName.QNAME_LEFT_PARENTHESIS + NAMESPACE + QName.QNAME_REVISION_DELIMITER
31 + REVISION + QName.QNAME_RIGHT_PARENTHESIS + LOCALNAME, qname.toString());
32 QName copied = QName.create(qname.toString());
33 assertEquals(qname, copied);
37 QName qname = QName.create(NS, LOCALNAME);
38 assertEquals(QName.QNAME_LEFT_PARENTHESIS + NAMESPACE + QName.QNAME_RIGHT_PARENTHESIS
39 + LOCALNAME, qname.toString());
40 QName copied = QName.create(qname.toString());
41 assertEquals(qname, copied);
46 public void testIllegalLocalNames() {
47 assertLocalNameFails(null);
48 assertLocalNameFails("");
49 assertLocalNameFails("(");
50 assertLocalNameFails(")");
51 assertLocalNameFails("?");
52 assertLocalNameFails("&");
56 public void testCompareTo() throws Exception {
60 // compare with namespace
61 QName qa = QName.create(A, REVISION, A);
62 QName qb = QName.create(B, REVISION, A);
63 assertTrue(qa.compareTo(qb) < 0);
64 assertTrue(qb.compareTo(qa) > 0);
66 // compare with revision
67 qa = QName.create(A, "2013-12-24", A);
68 qb = QName.create(A, "2013-12-25", A);
69 assertTrue(qa.compareTo(qb) < 0);
70 assertTrue(qb.compareTo(qa) > 0);
72 // compare with 1 null revision
73 qa = QName.create(URI.create(A), A);
74 qb = QName.create(URI.create(A), Revision.valueOf(REVISION), A);
75 assertTrue(qa.compareTo(qb) < 0);
76 assertTrue(qb.compareTo(qa) > 0);
78 // compare with both null revision
79 qb = QName.create(URI.create(A), A);
80 assertTrue(qa.compareTo(qb) == 0);
81 assertTrue(qb.compareTo(qa) == 0);
85 public void testQName() {
86 final QName qname = QName.create(NAMESPACE, REVISION, LOCALNAME);
87 final QName qname1 = QName.create(NAMESPACE, LOCALNAME);
88 final QName qname2 = QName.create(qname1, LOCALNAME);
89 assertEquals(qname1, qname.withoutRevision());
90 assertEquals(qname1, qname2);
91 assertTrue(qname.isEqualWithoutRevision(qname1));
92 assertNotNull(QName.formattedRevision(Optional.of(Revision.valueOf("2000-01-01"))));
93 assertNotNull(qname.hashCode());
94 assertEquals(qname, qname.intern());
98 public void testQNameModule() throws URISyntaxException {
99 final QNameModule qnameModule = QNameModule.create(NS, Revision.valueOf("2000-01-01"));
100 assertNotNull(qnameModule.toString());
101 assertNotNull(qnameModule.getRevisionNamespace());
104 private static void assertLocalNameFails(final String localName) {
106 QName.create(NS, localName);
107 fail("Local name should fail:" + localName);
108 } catch (IllegalArgumentException e) {