BUG-7464: Apply copyright headers
[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 java.util.Iterator;
19 import java.util.Map.Entry;
20 import org.junit.Before;
21 import org.junit.Test;
22
23 /***
24  *
25  * Test that read-only iterators do not allow for any updates.
26  * Test that non read-only iterators allow for updates.
27  *
28  */
29 public class TestReadOnlyAndUpdatableIterators {
30     TrieMap<Integer, Integer> bt;
31     private static final int MAP_SIZE = 200;
32
33     @Before
34     public void setUp() {
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)));
38         }
39     }
40
41     @Test
42     public void testReadOnlyIterator () {
43         Iterator<Entry<Integer, Integer>> it = bt.readOnlyIterator ();
44         try {
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) {
49
50         }
51         try {
52             it.remove ();
53             // It should have generated an exception, because it is a read-only iterator
54             TestHelper.assertFalse (true);
55         } catch (Exception e) {
56
57         }
58     }
59
60     @Test
61     public void testReadOnlySnapshotReadOnlyIterator () {
62         TrieMap<Integer, Integer> roSnapshot = bt.readOnlySnapshot ();
63         Iterator<Entry<Integer, Integer>> it = roSnapshot.readOnlyIterator ();
64         try {
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) {
69
70         }
71         try {
72             it.remove ();
73             // It should have generated an exception, because it is a read-only iterator
74             TestHelper.assertFalse (true);
75         } catch (Exception e) {
76
77         }
78     }
79
80     @Test
81     public void testReadOnlySnapshotIterator () {
82         TrieMap<Integer, Integer> roSnapshot = bt.readOnlySnapshot ();
83         Iterator<Entry<Integer, Integer>> it = roSnapshot.iterator ();
84         try {
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) {
89
90         }
91         try {
92             it.remove ();
93             // It should have generated an exception, because it is a read-only iterator
94             TestHelper.assertFalse (true);
95         } catch (Exception e) {
96
97         }
98     }
99
100     @Test
101     public void testIterator () {
102         Iterator<Entry<Integer, Integer>> it = bt.iterator ();
103         try {
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);
108         }
109
110         try {
111             it.remove ();
112         } catch (Exception e) {
113             // It should not have generated an exception, because it is a non read-only iterator
114             TestHelper.assertFalse (true);
115         }
116
117         // All changes are done on the original map
118         TestHelper.assertEquals (MAP_SIZE - 1, bt.size ());
119     }
120
121     @Test
122     public void testSnapshotIterator () {
123         TrieMap<Integer, Integer> snapshot = bt.snapshot ();
124         Iterator<Entry<Integer, Integer>> it = snapshot.iterator ();
125         try {
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);
130         }
131         try {
132             it.remove ();
133         } catch (Exception e) {
134             // It should not have generated an exception, because it is a non read-only iterator
135             TestHelper.assertFalse (true);
136         }
137
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 ());
143     }
144 }