1 package com.romix.scala.collection.concurrent;
\r
3 import java.util.Iterator;
\r
4 import java.util.Map.Entry;
\r
6 import org.junit.Before;
\r
7 import org.junit.Test;
\r
11 * Test that read-only iterators do not allow for any updates.
\r
12 * Test that non read-only iterators allow for updates.
\r
15 public class TestReadOnlyAndUpdatableIterators {
\r
16 TrieMap<Integer, Integer> bt;
\r
17 private static final int MAP_SIZE = 200;
\r
20 public void setUp() {
\r
21 bt = new TrieMap <Integer, Integer> ();
\r
22 for (int j = 0; j < MAP_SIZE; j++) {
\r
23 TestHelper.assertEquals (null, bt.put (Integer.valueOf (j), Integer.valueOf (j)));
\r
28 public void testReadOnlyIterator () {
\r
29 Iterator<Entry<Integer, Integer>> it = bt.readOnlyIterator ();
\r
31 it.next().setValue (0);
\r
32 // It should have generated an exception, because it is a read-only iterator
\r
33 TestHelper.assertFalse (true);
\r
34 } catch (Exception e) {
\r
39 // It should have generated an exception, because it is a read-only iterator
\r
40 TestHelper.assertFalse (true);
\r
41 } catch (Exception e) {
\r
47 public void testReadOnlySnapshotReadOnlyIterator () {
\r
48 TrieMap<Integer, Integer> roSnapshot = bt.readOnlySnapshot ();
\r
49 Iterator<Entry<Integer, Integer>> it = roSnapshot.readOnlyIterator ();
\r
51 it.next().setValue (0);
\r
52 // It should have generated an exception, because it is a read-only iterator
\r
53 TestHelper.assertFalse (true);
\r
54 } catch (Exception e) {
\r
59 // It should have generated an exception, because it is a read-only iterator
\r
60 TestHelper.assertFalse (true);
\r
61 } catch (Exception e) {
\r
67 public void testReadOnlySnapshotIterator () {
\r
68 TrieMap<Integer, Integer> roSnapshot = bt.readOnlySnapshot ();
\r
69 Iterator<Entry<Integer, Integer>> it = roSnapshot.iterator ();
\r
71 it.next().setValue (0);
\r
72 // It should have generated an exception, because it is a read-only iterator
\r
73 TestHelper.assertFalse (true);
\r
74 } catch (Exception e) {
\r
79 // It should have generated an exception, because it is a read-only iterator
\r
80 TestHelper.assertFalse (true);
\r
81 } catch (Exception e) {
\r
87 public void testIterator () {
\r
88 Iterator<Entry<Integer, Integer>> it = bt.iterator ();
\r
90 it.next().setValue (0);
\r
91 } catch (Exception e) {
\r
92 // It should not have generated an exception, because it is a non read-only iterator
\r
93 TestHelper.assertFalse (true);
\r
98 } catch (Exception e) {
\r
99 // It should not have generated an exception, because it is a non read-only iterator
\r
100 TestHelper.assertFalse (true);
\r
103 // All changes are done on the original map
\r
104 TestHelper.assertEquals (MAP_SIZE - 1, bt.size ());
\r
108 public void testSnapshotIterator () {
\r
109 TrieMap<Integer, Integer> snapshot = bt.snapshot ();
\r
110 Iterator<Entry<Integer, Integer>> it = snapshot.iterator ();
\r
112 it.next().setValue (0);
\r
113 } catch (Exception e) {
\r
114 // It should not have generated an exception, because it is a non read-only iterator
\r
115 TestHelper.assertFalse (true);
\r
119 } catch (Exception e) {
\r
120 // It should not have generated an exception, because it is a non read-only iterator
\r
121 TestHelper.assertFalse (true);
\r
124 // All changes are done on the snapshot, not on the original map
\r
125 // Map size should remain unchanged
\r
126 TestHelper.assertEquals (MAP_SIZE, bt.size ());
\r
127 // snapshot size was changed
\r
128 TestHelper.assertEquals (MAP_SIZE-1, snapshot.size ());
\r