2 * Copyright (c) 2019 Pantheon Technologies, s.r.o. 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.assertFalse;
12 import static org.junit.Assert.assertTrue;
14 import com.google.common.collect.ImmutableBiMap;
15 import java.io.ByteArrayInputStream;
16 import java.io.ByteArrayOutputStream;
17 import java.io.DataInputStream;
18 import java.io.DataOutputStream;
19 import java.io.IOException;
20 import java.util.Optional;
21 import org.junit.Test;
23 public class BiMapYangNamespaceContextTest {
24 private static final QNameModule FOO = QNameModule.create(XMLNamespace.of("foo"));
25 private static final QNameModule BAR = QNameModule.create(XMLNamespace.of("bar"));
26 private static final QNameModule BAZ = QNameModule.create(XMLNamespace.of("baz"));
28 private final BiMapYangNamespaceContext context = new BiMapYangNamespaceContext(
29 ImmutableBiMap.of("foo", FOO, "bar", BAR));
32 public void testEquals() {
33 assertTrue(context.equals(context));
34 assertTrue(context.equals(new BiMapYangNamespaceContext(ImmutableBiMap.of("foo", FOO, "bar", BAR))));
35 assertFalse(context.equals(null));
36 assertFalse(context.equals(new BiMapYangNamespaceContext(ImmutableBiMap.of("foo", FOO))));
37 assertFalse(context.equals(new BiMapYangNamespaceContext(ImmutableBiMap.of("bar", BAR))));
41 public void testPrefixForNamespace() {
42 assertEquals(Optional.of("foo"), context.findPrefixForNamespace(FOO));
43 assertEquals(Optional.of("bar"), context.findPrefixForNamespace(BAR));
44 assertEquals(Optional.empty(), context.findPrefixForNamespace(BAZ));
48 public void testNamespaceForPrefix() {
49 assertEquals(Optional.of(FOO), context.findNamespaceForPrefix("foo"));
50 assertEquals(Optional.of(BAR), context.findNamespaceForPrefix("bar"));
51 assertEquals(Optional.empty(), context.findNamespaceForPrefix("baz"));
55 public void testReadWrite() throws IOException {
57 try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
58 try (DataOutputStream dos = new DataOutputStream(bos)) {
61 bytes = bos.toByteArray();
64 final BiMapYangNamespaceContext other;
65 try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes))) {
66 other = BiMapYangNamespaceContext.readFrom(dis);
69 assertEquals(context, other);
73 public void testCreateQName() {
74 assertEquals(QName.create(FOO, "some"), context.createQName("foo", "some"));