4ac015a829ddb4be8282da353b9c4eb8dbee9f86
[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  * Test that read-only iterators do not allow for any updates.
28  * Test that non read-only iterators allow for updates.
29  */
30 public class TestReadOnlyAndUpdatableIterators {
31     private static final int MAP_SIZE = 200;
32
33     private TrieMap<Integer, Integer> bt;
34
35     @Before
36     public void setUp() {
37         bt = TrieMap.create();
38         for (int j = 0; j < MAP_SIZE; j++) {
39             assertNull(bt.put(Integer.valueOf(j), Integer.valueOf(j)));
40         }
41     }
42
43     private static void trySet(final Iterator<Entry<Integer, Integer>> it) {
44         it.next().setValue(0);
45     }
46
47     private static void tryRemove(final Iterator<?> it) {
48         it.next();
49         it.remove();
50     }
51
52     @Test(expected = UnsupportedOperationException.class)
53     public void testReadOnlyIteratorSet() {
54         trySet(bt.immutableIterator());
55     }
56
57     @Test(expected = UnsupportedOperationException.class)
58     public void testReadOnlyIteratorRemove() {
59         tryRemove(bt.immutableIterator());
60     }
61
62     @Test(expected = UnsupportedOperationException.class)
63     public void testReadOnlySnapshotReadOnlyIteratorSet() {
64         trySet(bt.immutableSnapshot().immutableIterator());
65     }
66
67     @Test(expected = UnsupportedOperationException.class)
68     public void testReadOnlySnapshotReadOnlyIteratorRemove() {
69         tryRemove(bt.immutableSnapshot().immutableIterator());
70     }
71
72     @Test(expected = UnsupportedOperationException.class)
73     public void testReadOnlySnapshotIteratorSet() {
74         trySet(bt.immutableSnapshot().iterator());
75     }
76
77     @Test(expected = UnsupportedOperationException.class)
78     public void testReadOnlySnapshotIteratorRemove() {
79         tryRemove(bt.immutableSnapshot().iterator());
80     }
81
82     @Test
83     public void testIterator() {
84         Iterator<Entry<Integer, Integer>> it = bt.iterator();
85         it.next().setValue(0);
86         it.remove();
87
88         // All changes are done on the original map
89         assertEquals(MAP_SIZE - 1, bt.size());
90     }
91
92     @Test
93     public void testSnapshotIterator() {
94         TrieMap<Integer, Integer> snapshot = bt.mutableSnapshot();
95         Iterator<Entry<Integer, Integer>> it = snapshot.iterator();
96         it.next().setValue(0);
97         it.remove();
98
99         // All changes are done on the snapshot, not on the original map
100         // Map size should remain unchanged
101         assertEquals(MAP_SIZE, bt.size());
102         // snapshot size was changed
103         assertEquals(MAP_SIZE - 1, snapshot.size());
104     }
105 }