/* * Copyright 2016 higherfrequencytrading.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package net.openhft.chronicle.engine.map; import net.openhft.chronicle.core.io.Closeable; import net.openhft.chronicle.engine.api.map.MapView; import net.openhft.chronicle.engine.api.tree.AssetTree; import net.openhft.chronicle.engine.map.MapClientTest.RemoteMapSupplier; import net.openhft.chronicle.engine.tree.VanillaAssetTree; import net.openhft.chronicle.network.TCPRegistry; import net.openhft.chronicle.wire.WireType; import net.openhft.chronicle.wire.YamlLogging; import org.jetbrains.annotations.NotNull; import org.junit.*; import org.junit.rules.TestName; import java.io.IOException; import java.util.*; import java.util.Map.Entry; import static net.openhft.chronicle.engine.Utils.methodName; import static net.openhft.chronicle.engine.Utils.yamlLoggger; import static net.openhft.chronicle.wire.YamlLogging.writeMessage; import static org.junit.Assert.*; public class RemoteChronicleMapTextWireTest extends JSR166TestCase { @NotNull private final AssetTree assetTree = new VanillaAssetTree(); @NotNull @Rule public TestName name = new TestName(); @AfterClass public static void testTearDown() { TCPRegistry.assertAllServersStopped(); } @After public void after2() { Closeable.closeQuietly(assetTree); } @Before public void before() { System.out.println("\t... test " + name.getMethodName()); methodName(name.getMethodName()); YamlLogging.setAll(false); } @NotNull private ClosableMapSupplier<Integer, String> newIntString(@NotNull String name) throws IOException { @NotNull final RemoteMapSupplier<Integer, String> remoteMapSupplier = new RemoteMapSupplier<>( "RemoteChronicleMapTextWireTest.host.port", Integer.class, String.class, WireType.TEXT, assetTree, name); return new ClosableMapSupplier<Integer, String>() { @NotNull @Override public MapView<Integer, String> get() { return remoteMapSupplier.get(); } @Override public void close() throws IOException { remoteMapSupplier.close(); assetTree.close(); } }; } @NotNull private ClosableMapSupplier<CharSequence, CharSequence> newStrStrMap() throws IOException { @NotNull final RemoteMapSupplier<CharSequence, CharSequence> remoteMapSupplier = new RemoteMapSupplier<>( "RemoteChronicleMapTextWireTest.host.port", CharSequence.class, CharSequence.class, WireType.TEXT, assetTree, "test"); return new ClosableMapSupplier<CharSequence, CharSequence>() { @NotNull @Override public MapView<CharSequence, CharSequence> get() { return remoteMapSupplier.get(); } @Override public void close() throws IOException { remoteMapSupplier.close(); assetTree.close(); } }; } /** * Returns a new map from Integers 1-5 to Strings "A"-"E". */ @NotNull private ClosableMapSupplier<Integer, String> map5() throws IOException { @NotNull ClosableMapSupplier<Integer, String> supplier = newIntString("test"); final Map<Integer, String> map = supplier.get(); assertTrue(map.isEmpty()); map.put(1, "A"); map.put(2, "B"); map.put(3, "C"); map.put(4, "D"); map.put(5, "E"); assertFalse(map.isEmpty()); assertEquals(5, map.size()); return supplier; } /** * clear removes all pairs */ @Test(timeout = 50000) public void testClear() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); yamlLoggger(map::clear); assertEquals(0, map.size()); } } /** * contains returns true for contained value */ @Test(timeout = 50000) public void testContains() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); writeMessage("when the key exists"); yamlLoggger(() -> assertTrue(map.containsValue("A"))); writeMessage("when it doesnt exist"); yamlLoggger(() -> assertFalse(map.containsValue("Z"))); } } /** * containsKey returns true for contained key */ @Test(timeout = 50000) public void testContainsKey() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); writeMessage("example of containsKey(<key>) returning true"); yamlLoggger(() -> assertTrue(map.containsKey(one))); assertFalse(map.containsKey(zero)); } } /** * containsValue returns true for held values */ @Test(timeout = 50000) public void testContainsValue() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); writeMessage("example of containsValue(<value>) returning true"); yamlLoggger(() -> 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 testGetObjectNotPresent() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); assertEquals("A", map.get(one)); try (@NotNull ClosableMapSupplier empty = newStrStrMap()) { writeMessage("example of get(<key>) returning null, when the keys is not " + "present in the map"); yamlLoggger(() -> { Object object = map.get(notPresent); assertNull(object); }); } } } /** * isEmpty is true of empty map and false for non-empty */ @Test(timeout = 50000) public void testIsEmpty() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> emptySupplier = newIntString("testEmpty")) { final Map empty = emptySupplier.get(); try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); if (!empty.isEmpty()) { System.out.print("not empty " + empty); } writeMessage("example of isEmpty() returning true, not it uses the size() method"); yamlLoggger(() -> assertTrue(empty.isEmpty())); assertFalse(map.isEmpty()); } } } /** * keySet returns a Set containing all the keys */ @Test(timeout = 50000) public void testKeySet() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); writeMessage("example of checking the size of a keyset"); yamlLoggger(() -> { @NotNull Set s = map.keySet(); assertEquals(5, s.size()); } ); @NotNull Set s = map.keySet(); assertTrue(s.contains(one)); assertTrue(s.contains(two)); assertTrue(s.contains(three)); assertTrue(s.contains(four)); assertTrue(s.contains(five)); } } /** * keySet.toArray returns contains all keys */ @Test(timeout = 50000) public void testKeySetToArray() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); @NotNull Set s = map.keySet(); @NotNull Object[] ar = s.toArray(); assertTrue(s.containsAll(Arrays.asList(ar))); assertEquals(5, ar.length); ar[0] = m10; assertFalse(s.containsAll(Arrays.asList(ar))); } } /** * Values.toArray contains all values */ @Test(timeout = 50000) public void testValuesToArray() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); @NotNull Collection v = map.values(); @NotNull Object[] ar = v.toArray(); @NotNull 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")); } } /** * entrySet.toArray contains all entries */ @Test(timeout = 50000) public void testEntrySetToArray() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); writeMessage("map.entrySet().toArray() first gets the entry set and then converts " + "it to an array"); yamlLoggger(() -> { @NotNull Set s = map.entrySet(); s.toArray(); }); @NotNull Set s = map.entrySet(); @NotNull Object[] ar = s.toArray(); assertEquals(5, ar.length); for (int i = 0; i < 5; ++i) { assertTrue(map.containsKey(((Entry) (ar[i])).getKey())); assertTrue(map.containsValue(((Entry) (ar[i])).getValue())); } } } /** * values collection contains all values */ @Test(timeout = 50000) public void testValues() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); writeMessage("example of getting the values and then calling size()"); yamlLoggger(() -> { @NotNull Collection s = map.values(); s.size(); }); @NotNull 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 { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map<Integer, String> map = supplier.get(); writeMessage("example of getting and entry set itterator"); yamlLoggger(() -> { @NotNull Set<Entry<Integer, String>> entrySet = map.entrySet(); entrySet.iterator(); }); @NotNull Set<Entry<Integer, String>> s = map.entrySet(); assertEquals(5, s.size()); for (@NotNull Entry e : s) { 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")) ); } } System.out.println("finished"); } /** * putAll adds all key-value pairs from the given map */ @Test(timeout = 50000) public void testPutAll() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> emptySupplier = newIntString("test")) { final Map<Integer, String> empty = emptySupplier.get(); try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); yamlLoggger(() -> 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)); } } } /** * putIfAbsent works when the given key is not present */ @Test(timeout = 50000) public void testPutIfAbsent() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); yamlLoggger(() -> map.putIfAbsent(six, "Z")); assertTrue(map.containsKey(six)); } } /** * putIfAbsent does not add the pair if the key is already present */ @Test(timeout = 50000) public void testPutIfAbsent2() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); yamlLoggger(() -> assertEquals("A", map.putIfAbsent(one, "Z"))); } } /** * replace fails when the given key is not present */ @Test(timeout = 50000) public void testReplace() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); writeMessage("example of replace where the value is not known"); yamlLoggger(() -> assertNull(map.replace(six, "Z"))); assertFalse(map.containsKey(six)); } } /** * replace succeeds if the key is already present */ @Test(timeout = 50000) public void testReplace2() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); writeMessage("example of replace where the value is known"); yamlLoggger(() -> assertNotNull(map.replace(one, "Z"))); assertEquals("Z", map.get(one)); } } /** * replace value fails when the given key not mapped to expected value */ @Test(timeout = 50000) public void testReplaceValue() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); assertEquals("A", map.get(one)); writeMessage("example of when then value was not replaced"); yamlLoggger(() -> assertFalse(map.replace(one, "Z", "Z"))); assertEquals("A", map.get(one)); } } /** * replace value succeeds when the given key mapped to expected value */ @Test(timeout = 50000) public void testReplaceValue2() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); assertEquals("A", map.get(one)); writeMessage("example of replace where the value is known"); yamlLoggger(() -> assertTrue(map.replace(one, "A", "Z"))); assertEquals("Z", map.get(one)); } } /** * remove removes the correct key-value pair from the map */ @Test(timeout = 50000) public void testRemove() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); yamlLoggger(() -> map.remove(five)); assertEquals(4, map.size()); assertFalse(map.containsKey(five)); } } /** * remove(key,value) removes only if pair present */ @Test(timeout = 50000) public void testRemove2 () throws IOException { /* try( ClosableMapSupplier 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)); */ } /** * size returns the correct values */ @Test(timeout = 50000) public void testSize() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); try (@NotNull ClosableMapSupplier<Integer, String> supplier0 = newIntString("testEmpty")) { final Map empty = supplier0.get(); writeMessage("size on an empty map"); yamlLoggger(() -> assertEquals(0, empty.size())); writeMessage("size on a map with entries"); yamlLoggger(() -> assertEquals(5, map.size())); } } } /** * size returns the correct values */ @Test(timeout = 150000) public void testSize2() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); try (@NotNull ClosableMapSupplier<Integer, String> supplier0 = newIntString("testEmpty")) { final Map empty = supplier0.get(); assertEquals(0, empty.size()); assertEquals(5, map.size()); } } } /** * size returns the correct values */ @Test(timeout = 50000) public void testSize3() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = map5()) { final Map map = supplier.get(); try (@NotNull ClosableMapSupplier<Integer, String> supplier0 = newIntString("testEmpty")) { final Map empty = supplier0.get(); assertEquals(0, empty.size()); assertEquals(5, map.size()); } } } /** * get(null) throws NPE */ @Test(timeout = 50000, expected = NullPointerException.class) public void testGet_NullPointerException() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = newIntString("test")) { Map<Integer, String> c = supplier.get(); writeMessage("get(null) returns a NullPointerException"); yamlLoggger(() -> c.get(null)); } } /** * containsKey(null) throws NPE */ @Test(timeout = 50000, expected = NullPointerException.class) public void testContainsKey_NullPointerException() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = newIntString("test")) { Map<Integer, String> c = supplier.get(); writeMessage("c.containsKey(null) will throw a NullPointerException"); yamlLoggger(() -> c.containsKey(null)); } } /** * put(null,x) throws NPE */ @Test(timeout = 50000, expected = NullPointerException.class) public void testPut1_NullPointerException() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = newIntString("test")) { Map<Integer, String> c = supplier.get(); writeMessage("put(null) will throw a NullPointerException"); yamlLoggger(() -> c.put(null, "whatever")); } } /** * put(x, null) throws NPE */ @Test(timeout = 50000, expected = NullPointerException.class) public void testPut2_NullPointerException() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = newIntString("test")) { Map<Integer, String> c = supplier.get(); writeMessage("put(notPresent,null) will throw a NullPointerException"); yamlLoggger(() -> c.put(notPresent, null)); } } /** * putIfAbsent(null, x) throws NPE */ @Test(timeout = 50000, expected = NullPointerException.class) public void testPutIfAbsent1_NullPointerException() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = newIntString("test")) { Map<Integer, String> c = supplier.get(); writeMessage("put(null, \"whatever\") will throw a NullPointerException"); yamlLoggger(() -> c.putIfAbsent(null, "whatever")); } } /** * replace(null, x) throws NPE */ @Test(timeout = 50000, expected = NullPointerException.class) public void testReplace_NullPointerException() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = newIntString("test")) { Map<Integer, String> c = supplier.get(); c.replace(null, "whatever"); } } /** * replace(null, x, y) throws NPE */ @Test(timeout = 50000, expected = NullPointerException.class) public void testReplaceValue_NullPointerException() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = newIntString("test")) { Map<Integer, String> c = supplier.get(); c.replace(null, "A", "whatever"); } } /** * putIfAbsent(x, null) throws NPE */ @Test(timeout = 50000, expected = NullPointerException.class) public void testPutIfAbsent2_NullPointerException() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = newIntString("test")) { Map<Integer, String> c = supplier.get(); c.putIfAbsent(notPresent, null); } } /** * replace(x, null) throws NPE */ @Test(timeout = 50000, expected = NullPointerException.class) public void testReplace2_NullPointerException() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = newIntString("test")) { Map<Integer, String> c = supplier.get(); writeMessage("replace(notPresent,null) will throw a NullPointerException"); yamlLoggger(() -> c.replace(notPresent, null)); } } /** * replace(x, null, y) throws NPE */ @Test(timeout = 50000, expected = NullPointerException.class) public void testReplaceValue2_NullPointerException() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = newIntString("test")) { Map<Integer, String> c = supplier.get(); c.replace(notPresent, null, "A"); } } /** * replace(x, y, null) throws NPE */ @Test(timeout = 50000, expected = NullPointerException.class) public void testReplaceValue3_NullPointerException() throws IOException { try (@NotNull ClosableMapSupplier<Integer, String> supplier = newIntString("test")) { Map<Integer, String> c = supplier.get(); writeMessage("replace(notPresent, \"A\", null will throw a NullPointerException"); yamlLoggger(() -> c.replace(notPresent, "A", null)); } } /** * remove(null) throws NPE */ @Test(timeout = 50000, expected = NullPointerException.class) public void testRemove1_NullPointerException() throws IOException { try (@NotNull ClosableMapSupplier<CharSequence, CharSequence> supplier = newStrStrMap()) { Map<CharSequence, CharSequence> c = supplier.get(); c.put("sadsdf", "asdads"); writeMessage("remove(null) will throw a NullPointerException"); yamlLoggger(() -> c.remove(null)); } } /** * remove(null, x) throws NPE */ @Test(timeout = 50000, expected = NullPointerException.class) public void testRemove2_NullPointerException () throws IOException { try (@NotNull ClosableMapSupplier<CharSequence, CharSequence> supplier = newStrStrMap()) { Map<CharSequence, CharSequence> c = supplier.get(); c.put("sadsdf", "asdads"); writeMessage("remove(null,whatever) will throw a NullPointerException"); yamlLoggger(() -> c.remove(null, "whatever")); } } /** * remove(x, null) returns false */ @Test(timeout = 50000, expected = NullPointerException.class) public void testRemove3() throws IOException { try (@NotNull ClosableMapSupplier<CharSequence, CharSequence> supplier = newStrStrMap()) { Map<CharSequence, CharSequence> c = supplier.get(); c.put("sadsdf", "asdads"); assertFalse(c.remove("sadsdf", null)); } } }