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.impl.codec.xml;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThat;
13 import com.google.common.collect.Iterables;
14 import com.google.common.collect.Lists;
15 import com.google.common.collect.Sets;
18 import java.util.Date;
19 import java.util.List;
21 import org.hamcrest.CoreMatchers;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.QNameModule;
26 public class RandomPrefixTest {
27 static final int MAX_COUNTER = 4000;
30 public void testEncodeDecode() throws Exception {
32 final List<String> allGenerated = Lists.newArrayList();
33 for (int i = 0; i < MAX_COUNTER; i++) {
34 final String encoded = RandomPrefix.encode(i);
35 assertEquals(RandomPrefix.decode(encoded), i);
36 allGenerated.add(encoded);
39 assertEquals(allGenerated.size(), MAX_COUNTER);
40 assertEquals("dPT", allGenerated.get(MAX_COUNTER - 1));
41 assertEquals("a", allGenerated.get(0));
42 assertEquals(allGenerated.size(), Sets.newHashSet(allGenerated).size());
46 public void testQNameWithPrefix() throws Exception {
47 final RandomPrefix a = new RandomPrefix();
49 final List<String> allGenerated = Lists.newArrayList();
50 for (int i = 0; i < MAX_COUNTER; i++) {
51 final String prefix = RandomPrefix.encode(i);
52 final URI uri = new URI("localhost:" + prefix);
53 final QName qName = QName.create(QNameModule.create(uri, new Date()), "local-name");
54 allGenerated.add(a.encodePrefix(qName.getNamespace()));
57 assertEquals(MAX_COUNTER, allGenerated.size());
58 // We are generating MAX_COUNTER_VALUE + 27 prefixes total, so we should encounter a reset in prefix a start from 0 at some point
59 // At the end, there should be only 27 values in RandomPrefix cache
60 assertEquals(MAX_COUNTER, Iterables.size(a.getPrefixes()));
61 assertThat(allGenerated, CoreMatchers.not(CoreMatchers.hasItem("xml")));
62 assertThat(allGenerated, CoreMatchers.not(CoreMatchers.hasItem("xmla")));
63 assertThat(allGenerated, CoreMatchers.not(CoreMatchers.hasItem("xmlz")));
65 assertEquals(1, Iterables.frequency(allGenerated, "a"));
69 public void test2QNames1Namespace() throws Exception {
70 final RandomPrefix a = new RandomPrefix();
72 final URI uri = URI.create("localhost");
73 final QName qName = QName.create(QNameModule.create(uri, new Date()), "local-name");
74 final QName qName2 = QName.create(QNameModule.create(uri, new Date()), "local-name");
76 assertEquals(a.encodePrefix(qName.getNamespace()), a.encodePrefix(qName2.getNamespace()));
80 public void testQNameNoPrefix() throws Exception {
81 final RandomPrefix a = new RandomPrefix();
83 final URI uri = URI.create("localhost");
84 QName qName = QName.create(uri, new Date(), "local-name");
85 assertEquals("a", a.encodePrefix(qName.getNamespace()));
86 qName = QName.create(QNameModule.create(uri, new Date()), "local-name");
87 assertEquals("a", a.encodePrefix(qName.getNamespace()));
88 qName = QName.create(QNameModule.create(URI.create("second"), new Date()), "local-name");
89 assertEquals("b", a.encodePrefix(qName.getNamespace()));