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;
27 * Test that read-only iterators do not allow for any updates.
28 * Test that non read-only iterators allow for updates.
30 public class TestReadOnlyAndUpdatableIterators {
31 private static final int MAP_SIZE = 200;
33 private TrieMap<Integer, Integer> bt;
37 bt = TrieMap.create();
38 for (int j = 0; j < MAP_SIZE; j++) {
39 assertNull(bt.put(Integer.valueOf(j), Integer.valueOf(j)));
43 private static void trySet(final Iterator<Entry<Integer, Integer>> it) {
44 it.next().setValue(0);
47 private static void tryRemove(final Iterator<?> it) {
52 @Test(expected = UnsupportedOperationException.class)
53 public void testReadOnlyIteratorSet() {
54 trySet(bt.immutableIterator());
57 @Test(expected = UnsupportedOperationException.class)
58 public void testReadOnlyIteratorRemove() {
59 tryRemove(bt.immutableIterator());
62 @Test(expected = UnsupportedOperationException.class)
63 public void testReadOnlySnapshotReadOnlyIteratorSet() {
64 trySet(bt.immutableSnapshot().immutableIterator());
67 @Test(expected = UnsupportedOperationException.class)
68 public void testReadOnlySnapshotReadOnlyIteratorRemove() {
69 tryRemove(bt.immutableSnapshot().immutableIterator());
72 @Test(expected = UnsupportedOperationException.class)
73 public void testReadOnlySnapshotIteratorSet() {
74 trySet(bt.immutableSnapshot().iterator());
77 @Test(expected = UnsupportedOperationException.class)
78 public void testReadOnlySnapshotIteratorRemove() {
79 tryRemove(bt.immutableSnapshot().iterator());
83 public void testIterator() {
84 Iterator<Entry<Integer, Integer>> it = bt.iterator();
85 it.next().setValue(0);
88 // All changes are done on the original map
89 assertEquals(MAP_SIZE - 1, bt.size());
93 public void testSnapshotIterator() {
94 TrieMap<Integer, Integer> snapshot = bt.mutableSnapshot();
95 Iterator<Entry<Integer, Integer>> it = snapshot.iterator();
96 it.next().setValue(0);
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());