BUG-4688: switch revisions from Date to Revision
[yangtools.git] / yang / yang-common / src / test / java / org / opendaylight / yangtools / yang / common / QNameTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.common;
9
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;
14
15 import java.net.URI;
16 import java.net.URISyntaxException;
17 import java.util.Optional;
18 import org.junit.Test;
19
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);
25
26     @Test
27     public void testStringSerialization() throws Exception {
28         {
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);
34         }
35         // no revision
36         {
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);
42         }
43     }
44
45     @Test
46     public void testIllegalLocalNames() {
47         assertLocalNameFails(null);
48         assertLocalNameFails("");
49         assertLocalNameFails("(");
50         assertLocalNameFails(")");
51         assertLocalNameFails("?");
52         assertLocalNameFails("&");
53     }
54
55     @Test
56     public void testCompareTo() throws Exception {
57         final String A = "a";
58         final String B = "b";
59
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);
65
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);
71
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);
77
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);
82     }
83
84     @Test
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());
95     }
96
97     @Test
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());
102     }
103
104     private static void assertLocalNameFails(final String localName) {
105         try {
106             QName.create(NS, localName);
107             fail("Local name should fail:" + localName);
108         } catch (IllegalArgumentException e) {
109             // Expected
110         }
111     }
112 }