From c83030bc40181d9f32829ff65c379418fb1887ec Mon Sep 17 00:00:00 2001 From: Rob Austin Date: Tue, 21 Oct 2014 20:47:50 +0100 Subject: [PATCH] HCOLL-167 improved the StatelessChronicleMapTest --- .../chronicle/map/StatelessChronicleMap.java | 5 +- .../jrs166/map/StatelessChronicleMapTest.java | 606 +++++++++++------- 2 files changed, 362 insertions(+), 249 deletions(-) diff --git a/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java b/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java index 9b4b7f97c..560a99e11 100644 --- a/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java @@ -1,6 +1,5 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.common.ClosableHolder; import net.openhft.chronicle.common.StatelessBuilder; import net.openhft.chronicle.common.exceptions.IORuntimeException; import net.openhft.chronicle.common.exceptions.TimeoutRuntimeException; @@ -23,7 +22,7 @@ import static net.openhft.chronicle.map.StatelessChronicleMap.EventId.*; /** * @author Rob Austin. */ -class StatelessChronicleMap implements ChronicleMap, Closeable, ClosableHolder { +class StatelessChronicleMap implements ChronicleMap, Closeable { private static final Logger LOG = LoggerFactory.getLogger(StatelessChronicleMap.class); @@ -145,7 +144,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clos } - public void addCloseable(final Closeable closeable) { + private void addCloseable(final Closeable closeable) { try { closeables.add(closeable); } catch (IllegalStateException e) { diff --git a/src/test/java/net/openhft/chronicle/map/jrs166/map/StatelessChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/jrs166/map/StatelessChronicleMapTest.java index da37a6588..68ad75bc6 100644 --- a/src/test/java/net/openhft/chronicle/map/jrs166/map/StatelessChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/jrs166/map/StatelessChronicleMapTest.java @@ -16,13 +16,15 @@ package net.openhft.chronicle.map.jrs166.map; -import net.openhft.chronicle.common.ClosableHolder; import net.openhft.chronicle.map.ChronicleMap; import net.openhft.chronicle.map.ChronicleMapBuilder; import net.openhft.chronicle.map.TcpReplicationConfig; import net.openhft.chronicle.map.jrs166.JSR166TestCase; +import org.jetbrains.annotations.NotNull; import org.junit.Test; +import java.io.Closeable; +import java.io.File; import java.io.IOException; import java.net.InetSocketAddress; import java.util.*; @@ -41,6 +43,131 @@ import static org.junit.Assert.*; public class StatelessChronicleMapTest extends JSR166TestCase { + static class SingleCloseMap implements ChronicleMap { + private final Closeable[] closeables; + private final ChronicleMap d; + + @Override + public String toString() { + return d.toString(); + } + + public SingleCloseMap(ChronicleMap statelessMap, Closeable... closeables) { + this.closeables = closeables; + this.d = statelessMap; + } + + + @Override + public File file() { + return null; + } + + @Override + public void close() throws IOException { + for (Closeable c : closeables) { + c.close(); + } + } + + @Override + public long longSize() { + return d.longSize(); + } + + @Override + public int size() { + return d.size(); + } + + @Override + public boolean isEmpty() { + return d.isEmpty(); + } + + @Override + public boolean containsKey(Object key) { + return d.containsKey(key); + } + + @Override + public boolean containsValue(Object value) { + return d.containsValue(value); + } + + @Override + public V get(Object key) { + return d.get(key); + } + + @Override + public V put(K key, V value) { + return d.put(key, value); + } + + @Override + public V remove(Object key) { + return d.remove(key); + } + + @Override + public void putAll(java.util.Map m) { + d.putAll(m); + } + + @Override + public void clear() { + d.clear(); + } + + @NotNull + @Override + public Set keySet() { + return d.keySet(); + } + + @NotNull + @Override + public Collection values() { + return d.values(); + } + + @NotNull + @Override + public Set> entrySet() { + return d.entrySet(); + } + + @Override + public V getUsing(K key, V value) { + return d.getUsing(key, value); + } + + @Override + public V acquireUsing(K key, V value) { + return d.acquireUsing(key, value); + } + + @Override + public V putIfAbsent(K key, V value) { + return d.putIfAbsent(key, value); + } + + @Override + public boolean remove(Object key, Object value) { + return d.remove(key, value); + } + + @Override + public boolean replace(K key, V oldValue, V newValue) { + return d.replace(key, oldValue, newValue); + } + + @Override + public V replace(K key, V value) { + return d.replace(key, value); + } + } static ChronicleMap newShmIntString(int port) throws IOException { @@ -51,14 +178,13 @@ public class StatelessChronicleMapTest extends JSR166TestCase { .class, String.class) .stateless(remoteAddress(new InetSocketAddress("localhost", port))).create(); - ((ClosableHolder) statelessMap).addCloseable(serverMap); - - return statelessMap; + return new SingleCloseMap(statelessMap, statelessMap, serverMap); } - static ChronicleMap newShmStringString(int port) throws IOException { + static ChronicleMap newStrStrMap(int port) throws + IOException { final ChronicleMap serverMap = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) .replicators((byte) 2, TcpReplicationConfig.of(port)).create(); @@ -68,9 +194,7 @@ public class StatelessChronicleMapTest extends JSR166TestCase { .class, CharSequence.class) .stateless(remoteAddress(new InetSocketAddress("localhost", port))).create(); - ((ClosableHolder) statelessMap).addCloseable(serverMap); - - return statelessMap; + return new SingleCloseMap(statelessMap, statelessMap, serverMap); } @@ -79,9 +203,8 @@ public class StatelessChronicleMapTest extends JSR166TestCase { * * @param port */ - private static ChronicleMap map5(final int port) throws IOException { + private ChronicleMap map5(final int port) throws IOException { ChronicleMap map = newShmIntString(port); - assertTrue(map.isEmpty()); map.put(one, "A"); map.put(two, "B"); @@ -91,6 +214,7 @@ public class StatelessChronicleMapTest extends JSR166TestCase { assertFalse(map.isEmpty()); assertEquals(5, map.size()); return map; + } /** @@ -98,22 +222,25 @@ public class StatelessChronicleMapTest extends JSR166TestCase { */ @Test public void testClear() throws IOException { - ChronicleMap map = map5(8076); - map.clear(); - assertEquals(0, map.size()); - map.close(); - } + try (ChronicleMap map = map5(8076)) { + map.clear(); + assertEquals(0, map.size()); + + } + } /** * contains returns true for contained value */ @Test public void testContains() throws IOException { - ChronicleMap map = map5(8076); - assertTrue(map.containsValue("A")); - assertFalse(map.containsValue("Z")); - map.close(); + try (ChronicleMap map = map5(8076)) { + assertTrue(map.containsValue("A")); + assertFalse(map.containsValue("Z")); + + } + } /** @@ -121,10 +248,11 @@ public class StatelessChronicleMapTest extends JSR166TestCase { */ @Test public void testContainsKey() throws IOException { - ChronicleMap map = map5(8076); - assertTrue(map.containsKey(one)); - assertFalse(map.containsKey(zero)); - map.close(); + try (ChronicleMap map = map5(8076)) { + assertTrue(map.containsKey(one)); + assertFalse(map.containsKey(zero)); + + } } /** @@ -132,25 +260,26 @@ public class StatelessChronicleMapTest extends JSR166TestCase { */ @Test public void testContainsValue() throws IOException { - ChronicleMap map = map5(8076); - assertTrue(map.containsValue("A")); - assertFalse(map.containsValue("Z")); - map.close(); + try (ChronicleMap map = map5(8076)) { + assertTrue(map.containsValue("A")); + assertFalse(map.containsValue("Z")); + + } } - - /** * get returns the correct element at the given key, or null if not present */ @Test public void testGet() throws IOException { - ChronicleMap map = map5(8076); - assertEquals("A", (String) map.get(one)); - ChronicleMap empty = newShmIntString(8077); - assertNull(map.get(notPresent)); - map.close(); - empty.close(); + try (ChronicleMap map = map5(8076)) { + assertEquals("A", (String) map.get(one)); + try (ChronicleMap empty = newStrStrMap(8078)) { + assertNull(map.get(notPresent)); + + empty.close(); + } + } } /** @@ -158,16 +287,18 @@ public class StatelessChronicleMapTest extends JSR166TestCase { */ @Test public void testIsEmpty() throws IOException { - ChronicleMap empty = newShmIntString(8078); - ChronicleMap map = map5(8079); + try (ChronicleMap empty = newShmIntString(8078)) { + try (ChronicleMap map = map5(8079)) { - if (!empty.isEmpty()) { - System.out.print("not empty " + empty); + if (!empty.isEmpty()) { + System.out.print("not empty " + empty); + } + assertTrue(empty.isEmpty()); + assertFalse(map.isEmpty()); + + empty.close(); + } } - assertTrue(empty.isEmpty()); - assertFalse(map.isEmpty()); - map.close(); - empty.close(); } /** @@ -175,15 +306,16 @@ public class StatelessChronicleMapTest extends JSR166TestCase { */ @Test public void testKeySet() throws IOException { - ChronicleMap map = map5(8076); - Set s = map.keySet(); - assertEquals(5, s.size()); - assertTrue(s.contains(one)); - assertTrue(s.contains(two)); - assertTrue(s.contains(three)); - assertTrue(s.contains(four)); - assertTrue(s.contains(five)); - map.close(); + try (ChronicleMap map = map5(8076)) { + Set s = map.keySet(); + assertEquals(5, s.size()); + assertTrue(s.contains(one)); + assertTrue(s.contains(two)); + assertTrue(s.contains(three)); + assertTrue(s.contains(four)); + assertTrue(s.contains(five)); + + } } /** @@ -191,14 +323,15 @@ public class StatelessChronicleMapTest extends JSR166TestCase { */ @Test public void testKeySetToArray() throws IOException { - ChronicleMap map = map5(8076); - Set s = map.keySet(); - Object[] ar = s.toArray(); - assertTrue(s.containsAll(Arrays.asList(ar))); - assertEquals(5, ar.length); - ar[0] = m10; - assertFalse(s.containsAll(Arrays.asList(ar))); - map.close(); + try (ChronicleMap map = map5(8076)) { + Set s = map.keySet(); + Object[] ar = s.toArray(); + assertTrue(s.containsAll(Arrays.asList(ar))); + assertEquals(5, ar.length); + ar[0] = m10; + assertFalse(s.containsAll(Arrays.asList(ar))); + + } } /** @@ -206,17 +339,18 @@ public class StatelessChronicleMapTest extends JSR166TestCase { */ @Test public void testValuesToArray() throws IOException { - ChronicleMap map = map5(8076); - Collection v = map.values(); - Object[] ar = v.toArray(); - ArrayList s = new ArrayList(Arrays.asList(ar)); - assertEquals(5, ar.length); - assertTrue(s.contains("A")); - assertTrue(s.contains("B")); - assertTrue(s.contains("C")); - assertTrue(s.contains("D")); - assertTrue(s.contains("E")); - map.close(); + try (ChronicleMap map = map5(8076)) { + Collection v = map.values(); + Object[] ar = v.toArray(); + ArrayList s = new ArrayList(Arrays.asList(ar)); + assertEquals(5, ar.length); + assertTrue(s.contains("A")); + assertTrue(s.contains("B")); + assertTrue(s.contains("C")); + assertTrue(s.contains("D")); + assertTrue(s.contains("E")); + + } } /** @@ -224,15 +358,16 @@ public class StatelessChronicleMapTest extends JSR166TestCase { */ @Test public void testEntrySetToArray() throws IOException { - ChronicleMap map = map5(8076); - Set s = map.entrySet(); - Object[] ar = s.toArray(); - assertEquals(5, ar.length); - for (int i = 0; i < 5; ++i) { - assertTrue(map.containsKey(((Map.Entry) (ar[i])).getKey())); - assertTrue(map.containsValue(((Map.Entry) (ar[i])).getValue())); + try (ChronicleMap map = map5(8076)) { + Set s = map.entrySet(); + Object[] ar = s.toArray(); + assertEquals(5, ar.length); + for (int i = 0; i < 5; ++i) { + assertTrue(map.containsKey(((java.util.Map.Entry) (ar[i])).getKey())); + assertTrue(map.containsValue(((java.util.Map.Entry) (ar[i])).getValue())); + } + } - map.close(); } /** @@ -240,38 +375,40 @@ public class StatelessChronicleMapTest extends JSR166TestCase { */ @Test public void testValues() throws IOException { - ChronicleMap map = map5(8076); - Collection s = map.values(); - assertEquals(5, s.size()); - assertTrue(s.contains("A")); - assertTrue(s.contains("B")); - assertTrue(s.contains("C")); - assertTrue(s.contains("D")); - assertTrue(s.contains("E")); - map.close(); - } + try (ChronicleMap map = map5(8076)) { + Collection s = map.values(); + assertEquals(5, s.size()); + assertTrue(s.contains("A")); + assertTrue(s.contains("B")); + assertTrue(s.contains("C")); + assertTrue(s.contains("D")); + assertTrue(s.contains("E")); + } + + } /** * entrySet contains all pairs */ @Test public void testEntrySet() throws IOException { - ChronicleMap map = map5(8076); - Set s = map.entrySet(); - assertEquals(5, s.size()); - Iterator it = s.iterator(); - while (it.hasNext()) { - Map.Entry e = (Map.Entry) it.next(); - assertTrue( - (e.getKey().equals(one) && e.getValue().equals("A")) || - (e.getKey().equals(two) && e.getValue().equals("B")) || - (e.getKey().equals(three) && e.getValue().equals("C")) || - (e.getKey().equals(four) && e.getValue().equals("D")) || - (e.getKey().equals(five) && e.getValue().equals("E")) - ); + try (ChronicleMap map = map5(8076)) { + Set s = map.entrySet(); + assertEquals(5, s.size()); + Iterator it = s.iterator(); + while (it.hasNext()) { + java.util.Map.Entry e = (java.util.Map.Entry) it.next(); + assertTrue( + (e.getKey().equals(one) && e.getValue().equals("A")) || + (e.getKey().equals(two) && e.getValue().equals("B")) || + (e.getKey().equals(three) && e.getValue().equals("C")) || + (e.getKey().equals(four) && e.getValue().equals("D")) || + (e.getKey().equals(five) && e.getValue().equals("E")) + ); + } + } - map.close(); } /** @@ -279,17 +416,20 @@ public class StatelessChronicleMapTest extends JSR166TestCase { */ @Test public void testPutAll() throws IOException { - ChronicleMap empty = newShmIntString(8076); - ChronicleMap map = map5(8076); - empty.putAll(map); - assertEquals(5, empty.size()); - assertTrue(empty.containsKey(one)); - assertTrue(empty.containsKey(two)); - assertTrue(empty.containsKey(three)); - assertTrue(empty.containsKey(four)); - assertTrue(empty.containsKey(five)); - map.close(); - empty.close(); + + try (ChronicleMap empty = newShmIntString(8076)) { + try (ChronicleMap map = map5(8076)) { + empty.putAll(map); + assertEquals(5, empty.size()); + assertTrue(empty.containsKey(one)); + assertTrue(empty.containsKey(two)); + assertTrue(empty.containsKey(three)); + assertTrue(empty.containsKey(four)); + assertTrue(empty.containsKey(five)); + + empty.close(); + } + } } /** @@ -297,20 +437,23 @@ public class StatelessChronicleMapTest extends JSR166TestCase { */ @Test public void testPutIfAbsent() throws IOException { - ChronicleMap map = map5(8076); - map.putIfAbsent(six, "Z"); - assertTrue(map.containsKey(six)); - map.close(); + try (ChronicleMap map = map5(8076)) { + map.putIfAbsent(six, "Z"); + assertTrue(map.containsKey(six)); + + } } /** * putIfAbsent does not add the pair if the key is already present */ @Test - public void testPutIfAbsent2() throws IOException { - ChronicleMap map = map5(8076); - assertEquals("A", map.putIfAbsent(one, "Z")); - map.close(); + public void testPutIfAbsent2() throws + IOException { + try (ChronicleMap map = map5(8076)) { + assertEquals("A", map.putIfAbsent(one, "Z")); + + } } /** @@ -318,72 +461,82 @@ public class StatelessChronicleMapTest extends JSR166TestCase { */ @Test public void testReplace() throws IOException { - ChronicleMap map = map5(8076); - assertNull(map.replace(six, "Z")); - assertFalse(map.containsKey(six)); - map.close(); + try (ChronicleMap map = map5(8076)) { + assertNull(map.replace(six, "Z")); + assertFalse(map.containsKey(six)); + + } } /** * replace succeeds if the key is already present */ @Test - public void testReplace2() throws IOException { - ChronicleMap map = map5(8076); - assertNotNull(map.replace(one, "Z")); - assertEquals("Z", map.get(one)); - map.close(); + public void testReplace2() throws + IOException { + try (ChronicleMap map = map5(8076)) { + assertNotNull(map.replace(one, "Z")); + assertEquals("Z", map.get(one)); + + } } /** * replace value fails when the given key not mapped to expected value */ @Test - public void testReplaceValue() throws IOException { - ChronicleMap map = map5(8076); - assertEquals("A", map.get(one)); - assertFalse(map.replace(one, "Z", "Z")); - assertEquals("A", map.get(one)); - map.close(); + public void testReplaceValue() throws + IOException { + try (ChronicleMap map = map5(8076)) { + assertEquals("A", map.get(one)); + assertFalse(map.replace(one, "Z", "Z")); + assertEquals("A", map.get(one)); + + } } /** * replace value succeeds when the given key mapped to expected value */ @Test - public void testReplaceValue2() throws IOException { - ChronicleMap map = map5(8076); - assertEquals("A", map.get(one)); - assertTrue(map.replace(one, "A", "Z")); - assertEquals("Z", map.get(one)); - map.close(); + public void testReplaceValue2 + () throws IOException { + try (ChronicleMap map = map5(8076)) { + assertEquals("A", map.get(one)); + assertTrue(map.replace(one, "A", "Z")); + assertEquals("Z", map.get(one)); + + } } /** * remove removes the correct key-value pair from the map */ @Test - public void testRemove() throws IOException { - ChronicleMap map = map5(8076); - map.remove(five); - assertEquals(4, map.size()); - assertFalse(map.containsKey(five)); - map.close(); + public void testRemove() throws + IOException { + try (ChronicleMap map = map5(8076)) { + map.remove(five); + assertEquals(4, map.size()); + assertFalse(map.containsKey(five)); + + } } /** * remove(key,value) removes only if pair present */ @Test - public void testRemove2() throws IOException { - /* ChronicleMap map = map5(8076); + public void testRemove2 + () throws IOException { + /* try( ChronicleMap map = map5(8076)) { map.remove(five, "E"); assertEquals(4, map.size()); assertFalse(map.containsKey(five)); map.remove(four, "A"); assertEquals(4, map.size()); assertTrue(map.containsKey(four)); - map.close();*/ + */ } /** @@ -391,12 +544,16 @@ public class StatelessChronicleMapTest extends JSR166TestCase { */ @Test public void testSize() throws IOException { - ChronicleMap map = map5(8076); - ChronicleMap empty = newShmIntString(8077); - assertEquals(0, empty.size()); - assertEquals(5, map.size()); - map.close(); - empty.close(); + try (ChronicleMap map = map5(8076)) { + + try (ChronicleMap empty = newShmIntString(8078)) { + assertEquals(0, empty.size()) + ; + assertEquals(5, map.size()); + + empty.close(); + } + } } /** @@ -404,12 +561,13 @@ public class StatelessChronicleMapTest extends JSR166TestCase { */ @Test public void testToString() throws IOException { - ChronicleMap map = map5(8076); - String s = map.toString(); - for (int i = 1; i <= 5; ++i) { - assertTrue(s.contains(String.valueOf(i))); + try (ChronicleMap map = map5(8076)) { + String s = map.toString(); + for (int i = 1; i <= 5; ++i) { + assertTrue(s.contains(String.valueOf(i))); + } + } - map.close(); } /** @@ -418,14 +576,10 @@ public class StatelessChronicleMapTest extends JSR166TestCase { @Test public void testGet_NullPointerException() throws IOException { - ChronicleMap c = null; - try { - c = newShmIntString(8076); + try (ChronicleMap c = newShmIntString(8076)) { c.get(null); shouldThrow(); } catch (NullPointerException success) { - } finally { - c.close(); } } @@ -435,14 +589,10 @@ public class StatelessChronicleMapTest extends JSR166TestCase { */ @Test public void testContainsKey_NullPointerException() throws IOException { - ChronicleMap c = null; - try { - c = newShmIntString(8076); + try (ChronicleMap c = newShmIntString(8076)) { c.containsKey(null); shouldThrow(); } catch (NullPointerException success) { - } finally { - c.close(); } } @@ -450,15 +600,12 @@ public class StatelessChronicleMapTest extends JSR166TestCase { * put(null,x) throws NPE */ @Test - public void testPut1_NullPointerException() throws IOException { - ChronicleMap c = null; - try { - c = newShmIntString(8076); + public void testPut1_NullPointerException + () throws IOException { + try (ChronicleMap c = newShmIntString(8076)) { c.put(null, "whatever"); shouldThrow(); } catch (NullPointerException success) { - } finally { - c.close(); } } @@ -466,15 +613,12 @@ public class StatelessChronicleMapTest extends JSR166TestCase { * put(x, null) throws NPE */ @Test - public void testPut2_NullPointerException() throws IOException { - ChronicleMap c = null; - try { - c = newShmIntString(8076); + public void testPut2_NullPointerException + () throws IOException { + try (ChronicleMap c = newShmIntString(8076)) { c.put(notPresent, null); shouldThrow(); } catch (NullPointerException success) { - } finally { - c.close(); } } @@ -482,33 +626,26 @@ public class StatelessChronicleMapTest extends JSR166TestCase { * putIfAbsent(null, x) throws NPE */ @Test - public void testPutIfAbsent1_NullPointerException() throws IOException { - ChronicleMap c = null; - try { - c = newShmIntString(8076); + public void testPutIfAbsent1_NullPointerException + () throws IOException { + try (ChronicleMap c = newShmIntString(8076)) { c.putIfAbsent(null, "whatever"); shouldThrow(); } catch (NullPointerException success) { - } finally { - c.close(); } } - /** * replace(null, x) throws NPE */ @Test - public void testReplace_NullPointerException() throws IOException { - ChronicleMap c = null; - try { - c = newShmIntString(8076); + public void testReplace_NullPointerException + () throws IOException { + try (ChronicleMap c = newShmIntString(8076)) { c.replace(null, "whatever"); shouldThrow(); } catch (NullPointerException success) { - } finally { - c.close(); } } @@ -516,33 +653,26 @@ public class StatelessChronicleMapTest extends JSR166TestCase { * replace(null, x, y) throws NPE */ @Test - public void testReplaceValue_NullPointerException() throws IOException { - ChronicleMap c = null; - try { - c = newShmIntString(8076); + public void testReplaceValue_NullPointerException + () throws IOException { + try (ChronicleMap c = newShmIntString(8076)) { c.replace(null, "A", "whatever"); shouldThrow(); } catch (NullPointerException success) { - } finally { - c.close(); } } - /** * putIfAbsent(x, null) throws NPE */ @Test - public void testPutIfAbsent2_NullPointerException() throws IOException { - ChronicleMap c = null; - try { - c = newShmIntString(8076); + public void testPutIfAbsent2_NullPointerException + () throws IOException { + try (ChronicleMap c = newShmIntString(8076)) { c.putIfAbsent(notPresent, null); shouldThrow(); } catch (NullPointerException success) { - } finally { - c.close(); } } @@ -550,15 +680,12 @@ public class StatelessChronicleMapTest extends JSR166TestCase { * replace(x, null) throws NPE */ @Test - public void testReplace2_NullPointerException() throws IOException { - ChronicleMap c = null; - try { - c = newShmIntString(8076); + public void testReplace2_NullPointerException + () throws IOException { + try (ChronicleMap c = newShmIntString(8076)) { c.replace(notPresent, null); shouldThrow(); } catch (NullPointerException success) { - } finally { - c.close(); } } @@ -566,15 +693,12 @@ public class StatelessChronicleMapTest extends JSR166TestCase { * replace(x, null, y) throws NPE */ @Test - public void testReplaceValue2_NullPointerException() throws IOException { - ChronicleMap c = null; - try { - c = newShmIntString(8076); + public void testReplaceValue2_NullPointerException + () throws IOException { + try (ChronicleMap c = newShmIntString(8076)) { c.replace(notPresent, null, "A"); shouldThrow(); } catch (NullPointerException success) { - } finally { - c.close(); } } @@ -582,15 +706,12 @@ public class StatelessChronicleMapTest extends JSR166TestCase { * replace(x, y, null) throws NPE */ @Test - public void testReplaceValue3_NullPointerException() throws IOException { - ChronicleMap c = null; - try { - c = newShmIntString(8076); + public void testReplaceValue3_NullPointerException + () throws IOException { + try (ChronicleMap c = newShmIntString(8076)) { c.replace(notPresent, "A", null); shouldThrow(); } catch (NullPointerException success) { - } finally { - c.close(); } } @@ -599,16 +720,11 @@ public class StatelessChronicleMapTest extends JSR166TestCase { */ @Test public void testRemove1_NullPointerException() throws IOException { - ChronicleMap c = null; - try { - c = newShmStringString(8077); + try (ChronicleMap c = newStrStrMap(8076)) { c.put("sadsdf", "asdads"); c.remove(null); shouldThrow(); } catch (NullPointerException success) { - } finally { - if (c != null) - c.close(); } } @@ -616,16 +732,13 @@ public class StatelessChronicleMapTest extends JSR166TestCase { * remove(null, x) throws NPE */ @Test - public void testRemove2_NullPointerException() throws IOException { - ChronicleMap c = null; - try { - c = newShmStringString(8077); + public void testRemove2_NullPointerException + () throws IOException { + try (ChronicleMap c = newStrStrMap(8076)) { c.put("sadsdf", "asdads"); c.remove(null, "whatever"); shouldThrow(); } catch (NullPointerException success) { - } finally { - c.close(); } } @@ -633,10 +746,12 @@ public class StatelessChronicleMapTest extends JSR166TestCase { * remove(x, null) returns false */ @Test - public void testRemove3() throws IOException { - ChronicleMap c = newShmStringString(8077); - c.put("sadsdf", "asdads"); - assertFalse(c.remove("sadsdf", null)); + public void testRemove3 () throws IOException { + + try (ChronicleMap c = newStrStrMap(8076)) { + c.put("sadsdf", "asdads"); + assertFalse(c.remove("sadsdf", null)); + } } // classes for testing Comparable fallbacks @@ -723,6 +838,5 @@ public class StatelessChronicleMapTest extends JSR166TestCase { } - }