### Question:
FluentCursor extends CrossProcessCursorWrapper { public <T> T toOnlyElement(Function<? super Cursor, T> singleRowTransform) { try { switch (getCount()) { case 0: throw new NoSuchElementException(); case 1: moveToFirst(); return singleRowTransform.apply(this); default: throw new IllegalArgumentException("expected one element but was: " + getCount()); } } finally { close(); } } FluentCursor(Cursor cursor); FluentIterable<T> toFluentIterable(Function<? super Cursor, T> singleRowTransform); LazyCursorList<T> toLazyCursorList(Function<? super Cursor, T> singleRowTransform); LinkedHashMultimap<TKey, TValue> toMultimap(Function<? super Cursor, TKey> keyTransform, Function<? super Cursor, TValue> valueTransform); LinkedHashMap<TKey, TValue> toMap(Function<? super Cursor, TKey> keyTransform, Function<? super Cursor, TValue> valueTransform); T toOnlyElement(Function<? super Cursor, T> singleRowTransform); T toOnlyElement(Function<? super Cursor, T> singleRowTransform, T defaultValue); int toRowCount(); FluentCursor withNotificationUri(ContentResolver resolver, Uri uri);  }### Answer:
@Test public void shouldCloseCursorAfterTransformingToOnlyElement() throws Exception { MatrixCursor cursor = buildMatrixCursor(1); new FluentCursor(cursor).toOnlyElement(Functions.constant(null)); assertThat(cursor).isClosed(); }
@Test public void shouldCloseCursorAfterTransformingToOnlyElementWithDefaultValue() throws Exception { MatrixCursor cursor = buildMatrixCursor(0); new FluentCursor(cursor).toOnlyElement(Functions.constant(null), null); assertThat(cursor).isClosed(); }
@Test(expected = NoSuchElementException.class) public void shouldThrowAnExceptionWhenTransformingEmptyCursorToOnlyElement() throws Exception { new FluentCursor(buildMatrixCursor(0)).toOnlyElement(Functions.constant(null)); }
@Test public void shouldReturnDefaultValueWhenTransformingEmptyCursorToOnlyElementWithDefaultValue() throws Exception { Integer shouldBeDefault = new FluentCursor(buildMatrixCursor(0)).toOnlyElement(Functions.constant(1500), 2900); assertThat(shouldBeDefault).isEqualTo(2900); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowAnExceptionWhenTransformingCursorWithMultipleToOnlyElement() throws Exception { new FluentCursor(buildMatrixCursor(10)).toOnlyElement(Functions.constant(null)); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowAnExceptionWhenTransformingCursorWithMultipleToOnlyElementWithDefaultValue() throws Exception { new FluentCursor(buildMatrixCursor(10)).toOnlyElement(Functions.constant(null), null); }
@Test public void shouldApplyGivenFunctionWhenTransformingCursorToOnlyElement() throws Exception { MatrixCursor cursor = buildMatrixCursor(1); Integer onlyElement = new FluentCursor(cursor).toOnlyElement(Functions.constant(1500)); assertThat(onlyElement).isEqualTo(1500); }
@Test public void shouldApplyGivenFunctionWhenTransformingCursorToOnlyElementWithDefaultValue() throws Exception { MatrixCursor cursor = buildMatrixCursor(1); Integer onlyElement = new FluentCursor(cursor).toOnlyElement(Functions.constant(1500), 2900); assertThat(onlyElement).isEqualTo(1500); }
@Test public void shouldNotIterateOverCursorWhenTransformingCursorToOnlyElement() throws Exception { Cursor cursor = spy(buildMatrixCursor(1)); new FluentCursor(cursor).toOnlyElement(Functions.constant(null)); verify(cursor, never()).moveToNext(); verify(cursor, never()).moveToLast(); verify(cursor, never()).moveToPrevious(); verify(cursor, never()).moveToPosition(intThat(not(equalTo(0)))); }
@Test public void shouldNotIterateOverCursorWhenTransformingCursorToOnlyElementWithDefaultValue() throws Exception { Cursor cursor = spy(buildMatrixCursor(0)); new FluentCursor(cursor).toOnlyElement(Functions.constant(null), null); verify(cursor, never()).moveToNext(); verify(cursor, never()).moveToLast(); verify(cursor, never()).moveToPrevious(); verify(cursor, never()).moveToPosition(intThat(not(equalTo(0)))); }