2 * (C) Copyright 2016 Pantheon Technologies, s.r.o. and others.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.opendaylight.yangtools.triemap;
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNull;
21 import java.util.Iterator;
22 import java.util.Map.Entry;
23 import org.junit.Before;
24 import org.junit.Test;
28 * Test that read-only iterators do not allow for any updates.
29 * Test that non read-only iterators allow for updates.
32 public class TestReadOnlyAndUpdatableIterators {
33 private static final int MAP_SIZE = 200;
35 private TrieMap<Integer, Integer> bt;
39 bt = TrieMap.create();
40 for (int j = 0; j < MAP_SIZE; j++) {
41 assertNull(bt.put(Integer.valueOf(j), Integer.valueOf(j)));
45 private static void trySet(final Iterator<Entry<Integer, Integer>> it) {
46 it.next().setValue(0);
49 private static void tryRemove(final Iterator<?> it) {
54 @Test(expected = UnsupportedOperationException.class)
55 public void testReadOnlyIteratorSet() {
56 trySet(bt.readOnlyIterator());
59 @Test(expected = UnsupportedOperationException.class)
60 public void testReadOnlyIteratorRemove() {
61 tryRemove(bt.readOnlyIterator());
64 @Test(expected = UnsupportedOperationException.class)
65 public void testReadOnlySnapshotReadOnlyIteratorSet() {
66 trySet(bt.immutableSnapshot().readOnlyIterator());
69 @Test(expected = UnsupportedOperationException.class)
70 public void testReadOnlySnapshotReadOnlyIteratorRemove() {
71 tryRemove(bt.immutableSnapshot().readOnlyIterator());
74 @Test(expected = UnsupportedOperationException.class)
75 public void testReadOnlySnapshotIteratorSet() {
76 trySet(bt.immutableSnapshot().iterator());
79 @Test(expected = UnsupportedOperationException.class)
80 public void testReadOnlySnapshotIteratorRemove() {
81 tryRemove(bt.immutableSnapshot().iterator());
85 public void testIterator () {
86 Iterator<Entry<Integer, Integer>> it = bt.iterator();
87 it.next().setValue (0);
90 // All changes are done on the original map
91 assertEquals(MAP_SIZE - 1, bt.size());
95 public void testSnapshotIterator () {
96 TrieMap<Integer, Integer> snapshot = bt.mutableSnapshot();
97 Iterator<Entry<Integer, Integer>> it = snapshot.iterator();
98 it.next().setValue(0);
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 ());