fd31f798d4a8fd0f47c0deac4ab6f450d2f88cd1
[yangtools.git] / third-party / triemap / src / test / java / org / opendaylight / yangtools / triemap / TestReadOnlyAndUpdatableIterators.java
1 /*
2  * (C) Copyright 2016 Pantheon Technologies, s.r.o. and others.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.opendaylight.yangtools.triemap;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNull;
20
21 import java.util.Iterator;
22 import java.util.Map.Entry;
23 import org.junit.Before;
24 import org.junit.Test;
25
26 /***
27  *
28  * Test that read-only iterators do not allow for any updates.
29  * Test that non read-only iterators allow for updates.
30  *
31  */
32 public class TestReadOnlyAndUpdatableIterators {
33     private static final int MAP_SIZE = 200;
34
35     private TrieMap<Integer, Integer> bt;
36
37     @Before
38     public void setUp() {
39         bt = TrieMap.create();
40         for (int j = 0; j < MAP_SIZE; j++) {
41             assertNull(bt.put(Integer.valueOf(j), Integer.valueOf(j)));
42         }
43     }
44
45     private static void trySet(final Iterator<Entry<Integer, Integer>> it) {
46         it.next().setValue(0);
47     }
48
49     private static void tryRemove(final Iterator<?> it) {
50         it.next();
51         it.remove();
52     }
53
54     @Test(expected = UnsupportedOperationException.class)
55     public void testReadOnlyIteratorSet() {
56         trySet(bt.immutableIterator());
57     }
58
59     @Test(expected = UnsupportedOperationException.class)
60     public void testReadOnlyIteratorRemove() {
61         tryRemove(bt.immutableIterator());
62     }
63
64     @Test(expected = UnsupportedOperationException.class)
65     public void testReadOnlySnapshotReadOnlyIteratorSet() {
66         trySet(bt.immutableSnapshot().immutableIterator());
67     }
68
69     @Test(expected = UnsupportedOperationException.class)
70     public void testReadOnlySnapshotReadOnlyIteratorRemove() {
71         tryRemove(bt.immutableSnapshot().immutableIterator());
72     }
73
74     @Test(expected = UnsupportedOperationException.class)
75     public void testReadOnlySnapshotIteratorSet() {
76         trySet(bt.immutableSnapshot().iterator());
77     }
78
79     @Test(expected = UnsupportedOperationException.class)
80     public void testReadOnlySnapshotIteratorRemove() {
81         tryRemove(bt.immutableSnapshot().iterator());
82     }
83
84     @Test
85     public void testIterator () {
86         Iterator<Entry<Integer, Integer>> it = bt.iterator();
87         it.next().setValue (0);
88         it.remove();
89
90         // All changes are done on the original map
91         assertEquals(MAP_SIZE - 1, bt.size());
92     }
93
94     @Test
95     public void testSnapshotIterator () {
96         TrieMap<Integer, Integer> snapshot = bt.mutableSnapshot();
97         Iterator<Entry<Integer, Integer>> it = snapshot.iterator();
98         it.next().setValue(0);
99         it.remove();
100
101         // All changes are done on the snapshot, not on the original map
102         // Map size should remain unchanged
103         assertEquals(MAP_SIZE, bt.size ());
104         // snapshot size was changed
105         assertEquals(MAP_SIZE-1, snapshot.size ());
106     }
107 }