@Test public void testKeySetSize() throws Exception { ConcurrentWeakIdentityHashMap<String, String> map = new ConcurrentWeakIdentityHashMap<>(); Set<String> keys = map.keySet(); String key1 = "key1"; String key2 = "key2"; String key3 = "key3"; map.put(key1, "value1"); map.put(key2, "value2"); map.put(key3, "value3"); assertThat(keys.size(), is(3)); }
@Test public void testBasicOperations() { final ConcurrentWeakIdentityHashMap<Integer, String> map = new ConcurrentWeakIdentityHashMap<>(); final Integer key = 1; final String firstValue = "foo"; final String otherValue = "bar"; assertThat(map.containsKey(key), is(false)); assertThat(map.get(key), nullValue()); assertThat(map.put(key, firstValue), nullValue()); assertThat(map.containsKey(key), is(true)); assertThat(map.putIfAbsent(key, otherValue), is(firstValue)); assertThat(map.replace(key, otherValue, firstValue), is(false)); assertThat(map.get(key), is(firstValue)); assertThat(map.replace(key, firstValue, otherValue), is(true)); assertThat(map.get(key), is(otherValue)); assertThat(map.remove(key, firstValue), is(false)); assertThat(map.get(key), is(otherValue)); assertThat(map.containsKey(key), is(true)); assertThat(map.remove(key, otherValue), is(true)); assertThat(map.containsKey(key), is(false)); assertThat(map.get(key), nullValue()); assertThat(map.putIfAbsent(key, otherValue), nullValue()); assertThat(map.get(key), is(otherValue)); assertThat(map.remove(key), is(otherValue)); assertThat(map.containsKey(key), is(false)); assertThat(map.get(key), nullValue()); }
@Test public void testEntrySetSize() throws Exception { ConcurrentWeakIdentityHashMap<String, String> map = new ConcurrentWeakIdentityHashMap<>(); Set<Map.Entry<String, String>> entries = map.entrySet(); String key1 = "key1"; String key2 = "key2"; String key3 = "key3"; map.put(key1, "value1"); map.put(key2, "value2"); map.put(key3, "value3"); assertThat(entries.size(), is(3)); }
@Test(expected = IOException.class) public void testReleaseCloseableInstanceThrows() throws Exception { ClassInstanceProvider<String, ClassInstanceConfiguration<Closeable>, Closeable> classInstanceProvider = new ClassInstanceProvider<>(null, null); Closeable closeable = mock(Closeable.class); doThrow(IOException.class).when(closeable).close(); classInstanceProvider.providedVsCount.put(closeable, new AtomicInteger(1)); classInstanceProvider.instantiated.add(closeable); classInstanceProvider.releaseInstance(closeable); }
@Test public void testReleaseCloseableInstance() throws Exception { ClassInstanceProvider<String, ClassInstanceConfiguration<Closeable>, Closeable> classInstanceProvider = new ClassInstanceProvider<>(null, null); Closeable closeable = mock(Closeable.class); classInstanceProvider.providedVsCount.put(closeable, new AtomicInteger(1)); classInstanceProvider.instantiated.add(closeable); classInstanceProvider.releaseInstance(closeable); verify(closeable).close(); }
@Test public void testEntrySetContainsReflectsMapChanges() throws Exception { ConcurrentWeakIdentityHashMap<String, String> map = new ConcurrentWeakIdentityHashMap<>(); Set<Map.Entry<String, String>> entries = map.entrySet(); String key1 = "key1"; String key2 = "key2"; String key3 = "key3"; map.put(key1, "value1"); map.put(key2, "value2"); map.put(key3, "value3"); assertThat(entries.contains(new AbstractMap.SimpleEntry<>("key1", "value1")), is(true)); assertThat(entries.contains(new AbstractMap.SimpleEntry<>("key1", "value1")), is(true)); assertThat(entries.contains(new AbstractMap.SimpleEntry<>("key1", "value1")), is(true)); }
@Test public void testKeySetContainsReflectsMapChanges() throws Exception { ConcurrentWeakIdentityHashMap<String, String> map = new ConcurrentWeakIdentityHashMap<>(); Set<String> keys = map.keySet(); String key1 = "key1"; String key2 = "key2"; String key3 = "key3"; map.put(key1, "value1"); map.put(key2, "value2"); map.put(key3, "value3"); assertThat(keys.contains(key1), is(true)); assertThat(keys.contains(key2), is(true)); assertThat(keys.contains(key3), is(true)); }
@Test public void testReleaseSerializerWithProvidedCloseableSerializerDoesNotClose() throws Exception { DefaultSerializationProvider provider = new DefaultSerializationProvider(null); CloseableSerializer<?> closeableSerializer = new CloseableSerializer<>(); provider.providedVsCount.put(closeableSerializer, new AtomicInteger(1)); provider.releaseSerializer(closeableSerializer); assertThat(closeableSerializer.closed, is(false)); }
@Test public void testEntrySetIteratorReflectsMapChanges() { ConcurrentWeakIdentityHashMap<String, String> map = new ConcurrentWeakIdentityHashMap<>(); Set<Map.Entry<String, String>> entries = map.entrySet(); String key1 = "key1"; String key2 = "key2"; String key3 = "key3"; map.put(key1, "value1"); map.put(key2, "value2"); map.put(key3, "value3"); Iterator<Map.Entry<String, String>> iterator = entries.iterator(); assertThat(iterator.hasNext(), is(true)); assertThat(iterator.next().getKey(), startsWith("key")); assertThat(iterator.hasNext(), is(true)); assertThat(iterator.next().getKey(), startsWith("key")); assertThat(iterator.hasNext(), is(true)); assertThat(iterator.next().getKey(), startsWith("key")); assertThat(iterator.hasNext(), is(false)); }
@Test public void testSizeAccountsForGCedKeys() { final ConcurrentWeakIdentityHashMap<Object, String> map = new ConcurrentWeakIdentityHashMap<>(); final String v = "present"; addToMap(map, "gone"); map.put(1, v); while(map.size() > 1) { System.gc(); } assertThat(map.values().size(), is(1)); assertThat(map.values().iterator().next(), is(v)); }
@Test public void testRemoveAccountsForReference() { final ConcurrentWeakIdentityHashMap<Object, String> map = new ConcurrentWeakIdentityHashMap<>(); final Integer key = 1; final String v = "present"; map.put(key, v); assertThat(map.remove(key), is(v)); }
@Test(expected = IllegalArgumentException.class) public void testReleaseSameInstanceMultipleTimesThrows() throws Exception { DefaultSerializationProvider provider = new DefaultSerializationProvider(null); Serializer<?> serializer = mock(Serializer.class); provider.providedVsCount.put(serializer, new AtomicInteger(1)); provider.releaseSerializer(serializer); assertThat(provider.providedVsCount.get("foo"), Matchers.nullValue()); provider.releaseSerializer(serializer); }
@Test public void testKeySetIteratorReflectsMapChanges() { ConcurrentWeakIdentityHashMap<String, String> map = new ConcurrentWeakIdentityHashMap<>(); Set<String> keys = map.keySet(); String key1 = "key1"; String key2 = "key2"; String key3 = "key3"; map.put(key1, "value1"); map.put(key2, "value2"); map.put(key3, "value3"); Iterator<String> iterator = keys.iterator(); assertThat(iterator.hasNext(), is(true)); assertThat(iterator.next(), startsWith("key")); assertThat(iterator.hasNext(), is(true)); assertThat(iterator.next(), startsWith("key")); assertThat(iterator.hasNext(), is(true)); assertThat(iterator.next(), startsWith("key")); assertThat(iterator.hasNext(), is(false)); }
@Test(expected = IllegalArgumentException.class) public void testReleaseSameInstanceMultipleTimesThrows() throws Exception { ClassInstanceProvider<String, ClassInstanceConfiguration<String>, String> classInstanceProvider = new ClassInstanceProvider<>(null, null); classInstanceProvider.providedVsCount.put("foo", new AtomicInteger(1)); classInstanceProvider.releaseInstance("foo"); classInstanceProvider.releaseInstance("foo"); }
@Test public void testIteration() throws InterruptedException { final ConcurrentWeakIdentityHashMap<Object, Integer> map = new ConcurrentWeakIdentityHashMap<>(); int i = 0; while(i < 10240) { if (i % 1024 == 0) { map.put(i++, i); } else { addToMap(map, i++); } } System.gc(); System.gc(); Thread.sleep(500); System.gc(); int size = 0; // This relies on the entrySet keeping a hard ref to all keys in the map at invocation time for (Map.Entry<Object, Integer> entry : map.entrySet()) { i--; size = map.size(); assertThat(entry.getKey(), notNullValue()); } assertThat(i, not(is(0))); assertThat(size, not(is(0))); }