Merge "BUG-1382: eliminate QName.getPrefix()"
[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     static final int MAX_COUNTER = 4000;
28
29     @Test
30     public void testEncodeDecode() throws Exception {
31
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);
37         }
38
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());
43     }
44
45     @Test
46     public void testQNameWithPrefix() throws Exception {
47         final RandomPrefix a = new RandomPrefix();
48
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()));
55         }
56
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")));
64
65         assertEquals(1, Iterables.frequency(allGenerated, "a"));
66     }
67
68     @Test
69     public void test2QNames1Namespace() throws Exception {
70         final RandomPrefix a = new RandomPrefix();
71
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");
75
76         assertEquals(a.encodePrefix(qName.getNamespace()), a.encodePrefix(qName2.getNamespace()));
77     }
78
79     @Test
80     public void testQNameNoPrefix() throws Exception {
81         final RandomPrefix a = new RandomPrefix();
82
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()));
90
91     }
92 }