DATAKV-99 - Polishing.
Removed dedicated Entry<K,V> interface, since Map.Entry<K,V> is already sufficient. Favoured CloseableIterator over KeyValueIterator since it offers no additional functionality but less generic parameter clutter. Added missing JavaDoc.
This commit is contained in:
committed by
Thomas Darimont
parent
952a901164
commit
02759f58ea
@@ -20,7 +20,6 @@ import static org.hamcrest.core.IsNull.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
@@ -32,11 +31,13 @@ import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ForwardingIteratorUnitTests<K, V> {
|
||||
public class ForwardingCloseableIteratorUnitTests<K, V> {
|
||||
|
||||
@Mock Iterator<Map.Entry<K, V>> iteratorMock;
|
||||
@Mock Runnable closeActionMock;
|
||||
|
||||
/**
|
||||
* @see DATAKV-99
|
||||
@@ -46,7 +47,7 @@ public class ForwardingIteratorUnitTests<K, V> {
|
||||
|
||||
when(iteratorMock.hasNext()).thenReturn(true);
|
||||
|
||||
assertThat(new ForwardingKeyValueIterator<K, V>(iteratorMock).hasNext(), is(true));
|
||||
assertThat(new ForwardingCloseableIterator<K, V>(iteratorMock).hasNext(), is(true));
|
||||
|
||||
verify(iteratorMock, times(1)).hasNext();
|
||||
}
|
||||
@@ -59,7 +60,7 @@ public class ForwardingIteratorUnitTests<K, V> {
|
||||
|
||||
when(iteratorMock.next()).thenReturn((Map.Entry<K, V>) mock(Map.Entry.class));
|
||||
|
||||
assertThat(new ForwardingKeyValueIterator<K, V>(iteratorMock).next(), notNullValue());
|
||||
assertThat(new ForwardingCloseableIterator<K, V>(iteratorMock).next(), notNullValue());
|
||||
|
||||
verify(iteratorMock, times(1)).next();
|
||||
}
|
||||
@@ -72,18 +73,28 @@ public class ForwardingIteratorUnitTests<K, V> {
|
||||
|
||||
when(iteratorMock.next()).thenThrow(new NoSuchElementException());
|
||||
|
||||
new ForwardingKeyValueIterator<K, V>(iteratorMock).next();
|
||||
new ForwardingCloseableIterator<K, V>(iteratorMock).next();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAKV-99
|
||||
*/
|
||||
@Test
|
||||
public void closeShouldDoNothing() throws IOException {
|
||||
public void closeShouldDoNothingByDefault() {
|
||||
|
||||
new ForwardingKeyValueIterator<K, V>(iteratorMock).close();
|
||||
new ForwardingCloseableIterator<K, V>(iteratorMock).close();
|
||||
|
||||
verifyZeroInteractions(iteratorMock);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAKV-99
|
||||
*/
|
||||
@Test
|
||||
public void closeShouldInvokeConfiguredCloseAction() {
|
||||
|
||||
new ForwardingCloseableIterator<K, V>(iteratorMock, closeActionMock).close();
|
||||
|
||||
verify(closeActionMock, times(1)).run();
|
||||
}
|
||||
}
|
||||
@@ -15,18 +15,21 @@
|
||||
*/
|
||||
package org.springframework.data.keyvalue.test.util;
|
||||
|
||||
import java.util.AbstractMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hamcrest.CustomMatcher;
|
||||
import org.hamcrest.core.IsEqual;
|
||||
import org.springframework.data.keyvalue.core.Entry;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class IsEntry extends CustomMatcher<Entry<?, ?>> {
|
||||
public class IsEntry extends CustomMatcher<Map.Entry<?, ?>> {
|
||||
|
||||
private final Entry<?, ?> expected;
|
||||
private final Map.Entry<?, ?> expected;
|
||||
|
||||
private IsEntry(Entry<?, ?> entry) {
|
||||
private IsEntry(Map.Entry<?, ?> entry) {
|
||||
super(String.format("an entry %s=%s.", entry != null ? entry.getKey() : "null", entry != null ? entry.getValue()
|
||||
: "null"));
|
||||
this.expected = entry;
|
||||
@@ -39,11 +42,11 @@ public class IsEntry extends CustomMatcher<Entry<?, ?>> {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(item instanceof Entry)) {
|
||||
if (!(item instanceof Map.Entry)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Entry<?, ?> actual = (Entry<?, ?>) item;
|
||||
Map.Entry<?, ?> actual = (Map.Entry<?, ?>) item;
|
||||
|
||||
return new IsEqual<Object>(expected.getKey()).matches(actual.getKey())
|
||||
&& new IsEqual<Object>(expected.getValue()).matches(actual.getValue());
|
||||
@@ -53,28 +56,16 @@ public class IsEntry extends CustomMatcher<Entry<?, ?>> {
|
||||
return isEntry(new EntryImpl(key, value));
|
||||
}
|
||||
|
||||
public static IsEntry isEntry(Entry<?, ?> entry) {
|
||||
public static IsEntry isEntry(Map.Entry<?, ?> entry) {
|
||||
return new IsEntry(entry);
|
||||
}
|
||||
|
||||
private static class EntryImpl implements Entry<Object, Object> {
|
||||
private static class EntryImpl extends AbstractMap.SimpleEntry<Object, Object> {
|
||||
|
||||
private final Object key;
|
||||
private final Object value;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private EntryImpl(Object key, Object value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getValue() {
|
||||
return value;
|
||||
super(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -23,10 +23,11 @@ import static org.junit.Assert.*;
|
||||
import static org.springframework.data.keyvalue.test.util.IsEntry.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.keyvalue.core.KeyValueIterator;
|
||||
import org.springframework.data.util.CloseableIterator;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
@@ -198,7 +199,7 @@ public class MapKeyValueAdapterUnitTests {
|
||||
adapter.put("1", object1, COLLECTION_1);
|
||||
adapter.put("2", object2, COLLECTION_1);
|
||||
|
||||
KeyValueIterator<Serializable, ?> iterator = adapter.entries(COLLECTION_1);
|
||||
CloseableIterator<Map.Entry<Serializable, Object>> iterator = adapter.entries(COLLECTION_1);
|
||||
|
||||
assertThat(iterator.next(), isEntry("1", object1));
|
||||
assertThat(iterator.next(), isEntry("2", object2));
|
||||
@@ -222,7 +223,7 @@ public class MapKeyValueAdapterUnitTests {
|
||||
adapter.put("1", object1, COLLECTION_1);
|
||||
adapter.put("2", object2, COLLECTION_2);
|
||||
|
||||
KeyValueIterator<Serializable, ?> iterator = adapter.entries(COLLECTION_1);
|
||||
CloseableIterator<Map.Entry<Serializable, Object>> iterator = adapter.entries(COLLECTION_1);
|
||||
|
||||
assertThat(iterator.next(), isEntry("1", object1));
|
||||
assertThat(iterator.hasNext(), is(false));
|
||||
|
||||
Reference in New Issue
Block a user