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;
15 import com.google.common.collect.Iterables;
16 import java.util.ArrayList;
17 import java.util.HashSet;
18 import java.util.List;
20 import org.junit.jupiter.api.Test;
21 import org.opendaylight.yangtools.yang.common.XMLNamespace;
23 class NamespacePrefixesTest {
24 static final int MAX_COUNTER = 4000;
27 void testEncodeDecode() {
28 final var allGenerated = new ArrayList<>(MAX_COUNTER);
29 for (int i = 0; i < MAX_COUNTER; i++) {
30 final var encoded = NamespacePrefixes.encode(i);
31 assertEquals(decode(encoded), i);
32 allGenerated.add(encoded);
35 assertEquals(allGenerated.size(), MAX_COUNTER);
36 assertEquals("dPT", allGenerated.get(MAX_COUNTER - 1));
37 assertEquals("a", allGenerated.get(0));
38 assertEquals(allGenerated.size(), new HashSet<>(allGenerated).size());
42 void testQNameWithPrefix() {
43 final var a = new NamespacePrefixes(PreferredPrefixes.empty(), null);
45 final var allGenerated = new ArrayList<String>();
46 for (int i = 0; i < MAX_COUNTER; i++) {
47 allGenerated.add(a.encodePrefix(XMLNamespace.of("localhost:" + NamespacePrefixes.encode(i))));
50 assertEquals(MAX_COUNTER, allGenerated.size());
51 // We are generating MAX_COUNTER_VALUE + 27 prefixes total, so we should encounter a reset in prefix a start
52 // from 0 at some point. At the end, there should be only 27 values in RandomPrefix cache
53 assertEquals(MAX_COUNTER, a.emittedPrefixes().size());
54 assertThat(allGenerated, not(hasItem("xml")));
55 assertThat(allGenerated, not(hasItem("xmla")));
56 assertThat(allGenerated, not(hasItem("xmlz")));
58 assertEquals(1, Iterables.frequency(allGenerated, "a"));
62 void test2QNames1Namespace() {
63 final var a = new NamespacePrefixes(PreferredPrefixes.empty(), null);
65 final var uri = XMLNamespace.of("localhost");
67 assertEquals(a.encodePrefix(uri), a.encodePrefix(uri));
68 assertEquals(List.of(Map.entry(uri, "a")), a.emittedPrefixes());
72 void testQNameNoPrefix() {
73 final var a = new NamespacePrefixes(PreferredPrefixes.empty(), null);
75 final var uri = XMLNamespace.of("localhost");
76 final var second = XMLNamespace.of("second");
77 assertEquals("a", a.encodePrefix(uri));
78 assertEquals("a", a.encodePrefix(uri));
79 assertEquals("b", a.encodePrefix(second));
80 assertEquals(List.of(Map.entry(uri, "a"), Map.entry(second, "b")), a.emittedPrefixes());
83 private static int decode(final String str) {
85 for (char c : str.toCharArray()) {
86 int idx = NamespacePrefixes.LOOKUP.indexOf(c);
88 throw new IllegalArgumentException("Invalid string " + str);
90 ret = (ret << NamespacePrefixes.SHIFT) + idx;