|
| 1 | +/* |
| 2 | + * Copyright 2015 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.keyvalue.core; |
| 17 | + |
| 18 | +import static org.hamcrest.core.Is.*; |
| 19 | +import static org.hamcrest.core.IsNull.*; |
| 20 | +import static org.junit.Assert.*; |
| 21 | +import static org.mockito.Mockito.*; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.Iterator; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.NoSuchElementException; |
| 27 | + |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.runner.RunWith; |
| 30 | +import org.mockito.Mock; |
| 31 | +import org.mockito.runners.MockitoJUnitRunner; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Christoph Strobl |
| 35 | + */ |
| 36 | +@RunWith(MockitoJUnitRunner.class) |
| 37 | +public class ForwardingIteratorUnitTests<K, V> { |
| 38 | + |
| 39 | + @Mock Iterator<Map.Entry<K, V>> iteratorMock; |
| 40 | + |
| 41 | + /** |
| 42 | + * @see DATAKV-99 |
| 43 | + */ |
| 44 | + @Test |
| 45 | + public void hasNextShoudDelegateToWrappedIterator() { |
| 46 | + |
| 47 | + when(iteratorMock.hasNext()).thenReturn(true); |
| 48 | + |
| 49 | + assertThat(new ForwardingKeyValueIterator<K, V>(iteratorMock).hasNext(), is(true)); |
| 50 | + |
| 51 | + verify(iteratorMock, times(1)).hasNext(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @see DATAKV-99 |
| 56 | + */ |
| 57 | + @Test |
| 58 | + public void nextShoudDelegateToWrappedIterator() { |
| 59 | + |
| 60 | + when(iteratorMock.next()).thenReturn((Map.Entry<K, V>) mock(Map.Entry.class)); |
| 61 | + |
| 62 | + assertThat(new ForwardingKeyValueIterator<K, V>(iteratorMock).next(), notNullValue()); |
| 63 | + |
| 64 | + verify(iteratorMock, times(1)).next(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @see DATAKV-99 |
| 69 | + */ |
| 70 | + @Test(expected = NoSuchElementException.class) |
| 71 | + public void nextShoudThrowErrorWhenWrappedIteratorHasNoMoreElements() { |
| 72 | + |
| 73 | + when(iteratorMock.next()).thenThrow(new NoSuchElementException()); |
| 74 | + |
| 75 | + new ForwardingKeyValueIterator<K, V>(iteratorMock).next(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @see DATAKV-99 |
| 80 | + */ |
| 81 | + @Test |
| 82 | + public void closeShouldDoNothing() throws IOException { |
| 83 | + |
| 84 | + new ForwardingKeyValueIterator<K, V>(iteratorMock).close(); |
| 85 | + |
| 86 | + verifyZeroInteractions(iteratorMock); |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments