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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThat;
13 import com.google.common.collect.Iterables;
15 import java.util.ArrayList;
16 import java.util.HashSet;
17 import java.util.List;
18 import org.hamcrest.CoreMatchers;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.common.Revision;
24 public class RandomPrefixTest {
25 static final int MAX_COUNTER = 4000;
28 public void testEncodeDecode() throws Exception {
30 final List<String> allGenerated = new ArrayList<>(MAX_COUNTER);
31 for (int i = 0; i < MAX_COUNTER; i++) {
32 final String encoded = RandomPrefix.encode(i);
33 assertEquals(RandomPrefix.decode(encoded), i);
34 allGenerated.add(encoded);
37 assertEquals(allGenerated.size(), MAX_COUNTER);
38 assertEquals("dPT", allGenerated.get(MAX_COUNTER - 1));
39 assertEquals("a", allGenerated.get(0));
40 assertEquals(allGenerated.size(), new HashSet<>(allGenerated).size());
44 public void testQNameWithPrefix() throws Exception {
45 final RandomPrefix a = new RandomPrefix(null);
47 final List<String> allGenerated = new ArrayList<>();
48 for (int i = 0; i < MAX_COUNTER; i++) {
49 final String prefix = RandomPrefix.encode(i);
50 final URI uri = new URI("localhost:" + prefix);
51 final QName qname = QName.create(QNameModule.create(uri, Revision.valueOf("2000-01-01")), "local-name");
52 allGenerated.add(a.encodePrefix(qname.getNamespace()));
55 assertEquals(MAX_COUNTER, allGenerated.size());
56 // We are generating MAX_COUNTER_VALUE + 27 prefixes total, so we should encounter a reset in prefix a start
57 // from 0 at some point. At the end, there should be only 27 values in RandomPrefix cache
58 assertEquals(MAX_COUNTER, Iterables.size(a.getPrefixes()));
59 assertThat(allGenerated, CoreMatchers.not(CoreMatchers.hasItem("xml")));
60 assertThat(allGenerated, CoreMatchers.not(CoreMatchers.hasItem("xmla")));
61 assertThat(allGenerated, CoreMatchers.not(CoreMatchers.hasItem("xmlz")));
63 assertEquals(1, Iterables.frequency(allGenerated, "a"));
67 public void test2QNames1Namespace() throws Exception {
68 final RandomPrefix a = new RandomPrefix(null);
70 final URI uri = URI.create("localhost");
71 final QName qname = QName.create(QNameModule.create(uri, Revision.valueOf("2000-01-01")), "local-name");
72 final QName qname2 = QName.create(QNameModule.create(uri, Revision.valueOf("2000-01-01")), "local-name");
74 assertEquals(a.encodePrefix(qname.getNamespace()), a.encodePrefix(qname2.getNamespace()));
78 public void testQNameNoPrefix() throws Exception {
79 final RandomPrefix a = new RandomPrefix(null);
81 final URI uri = URI.create("localhost");
82 QName qname = QName.create(uri, Revision.valueOf("2000-01-01"), "local-name");
83 assertEquals("a", a.encodePrefix(qname.getNamespace()));
84 qname = QName.create(QNameModule.create(uri, Revision.valueOf("2000-01-01")), "local-name");
85 assertEquals("a", a.encodePrefix(qname.getNamespace()));
86 qname = QName.create(QNameModule.create(URI.create("second"), Revision.valueOf("2000-01-01")), "local-name");
87 assertEquals("b", a.encodePrefix(qname.getNamespace()));