Skip to content

Commit

Permalink
Added some more coverage tests for cursor adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
lyubomyr-shaydariv committed Sep 19, 2024
1 parent 6041b62 commit 335c4dd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.provider.Arguments;
import org.mockito.Mockito;

public final class IteratorTypeAdapterTest
extends AbstractElementCursorTypeAdapterTest<Iterator<?>> {
Expand Down Expand Up @@ -45,20 +46,43 @@ protected List<Arguments> makeTestCases() {
public void testLaziness()
throws IOException {
final TypeAdapter<Iterator<? extends Integer>> unit = IteratorTypeAdapter.getInstance(TypeAdapters.intTypeAdapter);
final JsonReader in = new JsonReader(new StringReader("[1,2,4,8]"));
final Iterator<? extends Integer> iterator = unit.read(in);
Assertions.assertEquals("$", in.getPath());
in.beginArray();
final JsonReader inSpy = Mockito.spy(new JsonReader(new StringReader("[1,2,4,8]")));
final Iterator<? extends Integer> iterator = unit.read(inSpy);
Assertions.assertEquals("$", inSpy.getPath());
inSpy.beginArray();
Assertions.assertEquals(1, iterator.next());
Assertions.assertEquals("$[1]", in.getPath());
Assertions.assertEquals("$[1]", inSpy.getPath());
Assertions.assertEquals(2, iterator.next());
Assertions.assertEquals("$[2]", in.getPath());
Assertions.assertEquals("$[2]", inSpy.getPath());
Assertions.assertEquals(4, iterator.next());
Assertions.assertEquals("$[3]", in.getPath());
Assertions.assertEquals("$[3]", inSpy.getPath());
Assertions.assertEquals(8, iterator.next());
Assertions.assertEquals("$[4]", in.getPath());
in.endArray();
Assertions.assertEquals("$", in.getPath());
Assertions.assertEquals("$[4]", inSpy.getPath());
inSpy.endArray();
Assertions.assertEquals("$", inSpy.getPath());
Mockito.verify(inSpy, Mockito.never())
.close();
}

@Test
public void testLazinessClosingTheInputFails()
throws IOException {
final TypeAdapter<Iterator<? extends Integer>> unit = IteratorTypeAdapter.getInstance(TypeAdapters.intTypeAdapter);
final IOException expectedEx = new IOException();
final JsonReader inSpy = Mockito.spy(new JsonReader(new StringReader("[1,2,4,8]")) {
@Override
public void close()
throws IOException {
throw expectedEx;
}
});
final Iterator<? extends Integer> iterator = unit.read(inSpy);
Assertions.assertEquals("$", inSpy.getPath());
inSpy.beginArray();
Assertions.assertEquals(1, iterator.next());
Assertions.assertEquals("$[1]", inSpy.getPath());
final IOException ex = Assertions.assertThrows(IOException.class, inSpy::close);
Assertions.assertSame(expectedEx, ex);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,21 @@ public void testLazinessClosingTheStream()
Assertions.assertEquals("JsonReader is closed", ex.getMessage());
}

@Test
public void testLazinessClosingTheStreamFails()
throws IOException {
final TypeAdapter<Stream<? extends Integer>> unit = StreamTypeAdapter.getInstance(TypeAdapters.intTypeAdapter);
final IOException expectedEx = new IOException();
final JsonReader inSpy = Mockito.spy(new JsonReader(new StringReader("[1,2,4,8]")) {
@Override
public void close()
throws IOException {
throw expectedEx;
}
});
final Stream<? extends Integer> stream = unit.read(inSpy);
final RuntimeException ex = Assertions.assertThrows(RuntimeException.class, stream::close);
Assertions.assertSame(expectedEx, ex.getCause());
}

}

0 comments on commit 335c4dd

Please sign in to comment.