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 java.util.Iterator;
19 import java.util.Map.Entry;
20 import org.junit.Before;
21 import org.junit.Test;
25 * Test that read-only iterators do not allow for any updates.
26 * Test that non read-only iterators allow for updates.
29 public class TestReadOnlyAndUpdatableIterators {
30 TrieMap<Integer, Integer> bt;
31 private static final int MAP_SIZE = 200;
35 bt = new TrieMap <> ();
36 for (int j = 0; j < MAP_SIZE; j++) {
37 TestHelper.assertEquals (null, bt.put (Integer.valueOf (j), Integer.valueOf (j)));
42 public void testReadOnlyIterator () {
43 Iterator<Entry<Integer, Integer>> it = bt.readOnlyIterator ();
45 it.next().setValue (0);
46 // It should have generated an exception, because it is a read-only iterator
47 TestHelper.assertFalse (true);
48 } catch (Exception e) {
53 // It should have generated an exception, because it is a read-only iterator
54 TestHelper.assertFalse (true);
55 } catch (Exception e) {
61 public void testReadOnlySnapshotReadOnlyIterator () {
62 TrieMap<Integer, Integer> roSnapshot = bt.readOnlySnapshot ();
63 Iterator<Entry<Integer, Integer>> it = roSnapshot.readOnlyIterator ();
65 it.next().setValue (0);
66 // It should have generated an exception, because it is a read-only iterator
67 TestHelper.assertFalse (true);
68 } catch (Exception e) {
73 // It should have generated an exception, because it is a read-only iterator
74 TestHelper.assertFalse (true);
75 } catch (Exception e) {
81 public void testReadOnlySnapshotIterator () {
82 TrieMap<Integer, Integer> roSnapshot = bt.readOnlySnapshot ();
83 Iterator<Entry<Integer, Integer>> it = roSnapshot.iterator ();
85 it.next().setValue (0);
86 // It should have generated an exception, because it is a read-only iterator
87 TestHelper.assertFalse (true);
88 } catch (Exception e) {
93 // It should have generated an exception, because it is a read-only iterator
94 TestHelper.assertFalse (true);
95 } catch (Exception e) {
101 public void testIterator () {
102 Iterator<Entry<Integer, Integer>> it = bt.iterator ();
104 it.next().setValue (0);
105 } catch (Exception e) {
106 // It should not have generated an exception, because it is a non read-only iterator
107 TestHelper.assertFalse (true);
112 } catch (Exception e) {
113 // It should not have generated an exception, because it is a non read-only iterator
114 TestHelper.assertFalse (true);
117 // All changes are done on the original map
118 TestHelper.assertEquals (MAP_SIZE - 1, bt.size ());
122 public void testSnapshotIterator () {
123 TrieMap<Integer, Integer> snapshot = bt.snapshot ();
124 Iterator<Entry<Integer, Integer>> it = snapshot.iterator ();
126 it.next().setValue (0);
127 } catch (Exception e) {
128 // It should not have generated an exception, because it is a non read-only iterator
129 TestHelper.assertFalse (true);
133 } catch (Exception e) {
134 // It should not have generated an exception, because it is a non read-only iterator
135 TestHelper.assertFalse (true);
138 // All changes are done on the snapshot, not on the original map
139 // Map size should remain unchanged
140 TestHelper.assertEquals (MAP_SIZE, bt.size ());
141 // snapshot size was changed
142 TestHelper.assertEquals (MAP_SIZE-1, snapshot.size ());