2 * Copyright (c) 2014 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.data.codec.xml;
10 import static org.hamcrest.CoreMatchers.hasItem;
11 import static org.hamcrest.CoreMatchers.not;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.jupiter.api.Assertions.assertEquals;
14 import static org.junit.jupiter.api.Assertions.assertNotEquals;
16 import com.google.common.collect.Iterables;
17 import java.util.ArrayList;
18 import java.util.HashSet;
19 import java.util.List;
21 import org.junit.jupiter.api.Test;
22 import org.opendaylight.yangtools.yang.common.XMLNamespace;
24 class NamespacePrefixesTest {
25 static final int MAX_COUNTER = 4000;
28 void testEncodeDecode() {
29 final var allGenerated = new ArrayList<>(MAX_COUNTER);
30 for (int i = 0; i < MAX_COUNTER; i++) {
31 final var encoded = NamespacePrefixes.encode(i);
32 assertDecodesTo(i, encoded);
33 allGenerated.add(encoded);
36 assertEquals(MAX_COUNTER, allGenerated.size());
37 assertEquals("dPT", allGenerated.get(MAX_COUNTER - 1));
38 assertEquals("a", allGenerated.get(0));
39 assertEquals(allGenerated.size(), new HashSet<>(allGenerated).size());
43 void testQNameWithPrefix() {
44 final var a = new NamespacePrefixes(null, null);
46 final var allGenerated = new ArrayList<String>();
47 for (int i = 0; i < MAX_COUNTER; i++) {
48 allGenerated.add(a.encodePrefix(XMLNamespace.of("localhost:" + NamespacePrefixes.encode(i))));
51 assertEquals(MAX_COUNTER, allGenerated.size());
52 // We are generating MAX_COUNTER_VALUE + 27 prefixes total, so we should encounter a reset in prefix a start
53 // from 0 at some point. At the end, there should be only 27 values in RandomPrefix cache
54 assertEquals(MAX_COUNTER, a.emittedPrefixes().size());
55 assertThat(allGenerated, not(hasItem("xml")));
56 assertThat(allGenerated, not(hasItem("xmla")));
57 assertThat(allGenerated, not(hasItem("xmlz")));
59 assertEquals(1, Iterables.frequency(allGenerated, "a"));
63 void test2QNames1Namespace() {
64 final var a = new NamespacePrefixes(null, null);
66 final var uri = XMLNamespace.of("localhost");
68 assertEquals(a.encodePrefix(uri), a.encodePrefix(uri));
69 assertEquals(List.of(Map.entry(uri, "a")), a.emittedPrefixes());
73 void testQNameNoPrefix() {
74 final var a = new NamespacePrefixes(null, new PreferredPrefixes.Precomputed(Map.of()));
76 final var uri = XMLNamespace.of("localhost");
77 final var second = XMLNamespace.of("second");
78 assertEquals("a", a.encodePrefix(uri));
79 assertEquals("a", a.encodePrefix(uri));
80 assertEquals("b", a.encodePrefix(second));
81 assertEquals(List.of(Map.entry(uri, "a"), Map.entry(second, "b")), a.emittedPrefixes());
84 private static void assertDecodesTo(final int expected, final String str) {
86 for (char c : str.toCharArray()) {
87 int idx = NamespacePrefixes.LOOKUP.indexOf(c);
88 assertNotEquals(-1, idx, () -> "Invalid string " + str);
89 actual = (actual << NamespacePrefixes.SHIFT) + idx;
91 assertEquals(expected, actual);