X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=third-party%2Ftriemap%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Ftriemap%2FTestReadOnlyAndUpdatableIterators.java;h=b3f7bb12a1c20e50eeab2baa6aaf41ea00ef4fe3;hb=5d673fd89ed04fb3a9cdb1539758c499312917f2;hp=214a2f6f44b0614f61e3047ff42da5967789866b;hpb=cea1776eb4b541427c1c77a67cb95fa39595c0ac;p=yangtools.git diff --git a/third-party/triemap/src/test/java/org/opendaylight/yangtools/triemap/TestReadOnlyAndUpdatableIterators.java b/third-party/triemap/src/test/java/org/opendaylight/yangtools/triemap/TestReadOnlyAndUpdatableIterators.java index 214a2f6f44..b3f7bb12a1 100644 --- a/third-party/triemap/src/test/java/org/opendaylight/yangtools/triemap/TestReadOnlyAndUpdatableIterators.java +++ b/third-party/triemap/src/test/java/org/opendaylight/yangtools/triemap/TestReadOnlyAndUpdatableIterators.java @@ -15,6 +15,9 @@ */ package org.opendaylight.yangtools.triemap; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + import java.util.Iterator; import java.util.Map.Entry; import org.junit.Before; @@ -27,118 +30,78 @@ import org.junit.Test; * */ public class TestReadOnlyAndUpdatableIterators { - TrieMap bt; private static final int MAP_SIZE = 200; + private TrieMap bt; + @Before public void setUp() { - bt = new TrieMap <> (); + bt = new TrieMap<>(); for (int j = 0; j < MAP_SIZE; j++) { - TestHelper.assertEquals (null, bt.put (Integer.valueOf (j), Integer.valueOf (j))); + assertNull(bt.put(Integer.valueOf(j), Integer.valueOf(j))); } } - @Test - public void testReadOnlyIterator () { - Iterator> it = bt.readOnlyIterator (); - try { - it.next().setValue (0); - // It should have generated an exception, because it is a read-only iterator - TestHelper.assertFalse (true); - } catch (Exception e) { - - } - try { - it.remove (); - // It should have generated an exception, because it is a read-only iterator - TestHelper.assertFalse (true); - } catch (Exception e) { + private static void trySet(final Iterator> it) { + it.next().setValue(0); + } - } + private static void tryRemove(final Iterator it) { + it.next(); + it.remove(); } - @Test - public void testReadOnlySnapshotReadOnlyIterator () { - TrieMap roSnapshot = bt.readOnlySnapshot (); - Iterator> it = roSnapshot.readOnlyIterator (); - try { - it.next().setValue (0); - // It should have generated an exception, because it is a read-only iterator - TestHelper.assertFalse (true); - } catch (Exception e) { + @Test(expected = UnsupportedOperationException.class) + public void testReadOnlyIteratorSet() { + trySet(bt.readOnlyIterator()); + } - } - try { - it.remove (); - // It should have generated an exception, because it is a read-only iterator - TestHelper.assertFalse (true); - } catch (Exception e) { + @Test(expected = UnsupportedOperationException.class) + public void testReadOnlyIteratorRemove() { + tryRemove(bt.readOnlyIterator()); + } - } + @Test(expected = UnsupportedOperationException.class) + public void testReadOnlySnapshotReadOnlyIteratorSet() { + trySet(bt.readOnlySnapshot().readOnlyIterator()); } - @Test - public void testReadOnlySnapshotIterator () { - TrieMap roSnapshot = bt.readOnlySnapshot (); - Iterator> it = roSnapshot.iterator (); - try { - it.next().setValue (0); - // It should have generated an exception, because it is a read-only iterator - TestHelper.assertFalse (true); - } catch (Exception e) { + @Test(expected = UnsupportedOperationException.class) + public void testReadOnlySnapshotReadOnlyIteratorRemove() { + tryRemove(bt.readOnlySnapshot().readOnlyIterator()); + } - } - try { - it.remove (); - // It should have generated an exception, because it is a read-only iterator - TestHelper.assertFalse (true); - } catch (Exception e) { + @Test(expected = UnsupportedOperationException.class) + public void testReadOnlySnapshotIteratorSet() { + trySet(bt.readOnlySnapshot().iterator()); + } - } + @Test(expected = UnsupportedOperationException.class) + public void testReadOnlySnapshotIteratorRemove() { + tryRemove(bt.readOnlySnapshot().iterator()); } @Test public void testIterator () { - Iterator> it = bt.iterator (); - try { - it.next().setValue (0); - } catch (Exception e) { - // It should not have generated an exception, because it is a non read-only iterator - TestHelper.assertFalse (true); - } - - try { - it.remove (); - } catch (Exception e) { - // It should not have generated an exception, because it is a non read-only iterator - TestHelper.assertFalse (true); - } + Iterator> it = bt.iterator(); + it.next().setValue (0); + it.remove(); // All changes are done on the original map - TestHelper.assertEquals (MAP_SIZE - 1, bt.size ()); + assertEquals(MAP_SIZE - 1, bt.size()); } @Test public void testSnapshotIterator () { - TrieMap snapshot = bt.snapshot (); - Iterator> it = snapshot.iterator (); - try { - it.next().setValue (0); - } catch (Exception e) { - // It should not have generated an exception, because it is a non read-only iterator - TestHelper.assertFalse (true); - } - try { - it.remove (); - } catch (Exception e) { - // It should not have generated an exception, because it is a non read-only iterator - TestHelper.assertFalse (true); - } + TrieMap snapshot = bt.snapshot(); + Iterator> it = snapshot.iterator(); + it.next().setValue(0); + it.remove(); // All changes are done on the snapshot, not on the original map // Map size should remain unchanged - TestHelper.assertEquals (MAP_SIZE, bt.size ()); + assertEquals(MAP_SIZE, bt.size ()); // snapshot size was changed - TestHelper.assertEquals (MAP_SIZE-1, snapshot.size ()); + assertEquals(MAP_SIZE-1, snapshot.size ()); } }