Merge "BUG-1258: fix TreeNode amortization"
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / codec / xml / RandomPrefixTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThat;
12
13 import com.google.common.collect.Iterables;
14 import com.google.common.collect.Lists;
15 import com.google.common.collect.Sets;
16
17 import java.net.URI;
18 import java.util.Date;
19 import java.util.List;
20
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;
25
26 public class RandomPrefixTest {
27
28     @Test
29     public void testEncodeDecode() throws Exception {
30         final List<String> allGenerated = Lists.newArrayList();
31         for (int i = 0; i < RandomPrefix.MAX_COUNTER_VALUE; i++) {
32             final String encoded = RandomPrefix.encode(i);
33             assertEquals(RandomPrefix.decode(encoded), i);
34             allGenerated.add(encoded);
35         }
36
37         assertEquals(allGenerated.size(), RandomPrefix.MAX_COUNTER_VALUE);
38         assertEquals("zzzz", allGenerated.get(RandomPrefix.MAX_COUNTER_VALUE - 1));
39         assertEquals("a", allGenerated.get(0));
40         assertEquals("xml", allGenerated.get(RandomPrefix.decode("xml")));
41         assertEquals(allGenerated.size(), Sets.newHashSet(allGenerated).size());
42     }
43
44     @Test
45     public void testQNameWithPrefix() throws Exception {
46         final RandomPrefix a = new RandomPrefix();
47
48         final List<String> allGenerated = Lists.newArrayList();
49         for (int i = 0; i < RandomPrefix.MAX_COUNTER_VALUE; i++) {
50             final String prefix = RandomPrefix.encode(i);
51             final URI uri = new URI("localhost:" + prefix);
52             final QName qName = QName.create(QNameModule.create(uri, new Date()), prefix, "local-name");
53             allGenerated.add(a.encodePrefix(qName.getNamespace()));
54         }
55
56         assertEquals(RandomPrefix.MAX_COUNTER_VALUE, allGenerated.size());
57         // We are generating MAX_COUNTER_VALUE + 27 prefixes total, so we should encounter a reset in prefix a start from 0 at some point
58         // At the end, there should be only 27 values in RandomPrefix cache
59         assertEquals(27, Iterables.size(a.getPrefixes()));
60         assertThat(allGenerated, CoreMatchers.not(CoreMatchers.hasItem("xml")));
61         assertThat(allGenerated, CoreMatchers.not(CoreMatchers.hasItem("xmla")));
62         assertThat(allGenerated, CoreMatchers.not(CoreMatchers.hasItem("xmlz")));
63
64         assertEquals(2, Iterables.frequency(allGenerated, "a"));
65     }
66
67     @Test
68     public void test2QNames1Namespace() throws Exception {
69         final RandomPrefix a = new RandomPrefix();
70
71         final URI uri = URI.create("localhost");
72         final QName qName = QName.create(QNameModule.create(uri, new Date()), "p1", "local-name");
73         final QName qName2 = QName.create(QNameModule.create(uri, new Date()), "p2", "local-name");
74
75         assertEquals(a.encodePrefix(qName.getNamespace()), a.encodePrefix(qName2.getNamespace()));
76     }
77
78     @Test
79     public void testQNameNoPrefix() throws Exception {
80         final RandomPrefix a = new RandomPrefix();
81
82         final URI uri = URI.create("localhost");
83         QName qName = QName.create(uri, new Date(), "local-name");
84         assertEquals("a", a.encodePrefix(qName.getNamespace()));
85         qName = QName.create(QNameModule.create(uri, new Date()), "", "local-name");
86         assertEquals("a", a.encodePrefix(qName.getNamespace()));
87         qName = QName.create(QNameModule.create(URI.create("second"), new Date()), "", "local-name");
88         assertEquals("b", a.encodePrefix(qName.getNamespace()));
89
90     }
91 }