BdioReader implements Closeable { @Nullable public Object nextEntry() throws IOException { try (InputStream input = nextStream()) { return input != null ? JsonUtils.fromInputStream(input) : null; } } BdioReader(InputStream in); @Nullable Object nextEntry(); @Override synchronized void close();  }
@SuppressWarnings("resource") @Test(expected = JsonParseException.class) public void emptyStream() throws IOException { new BdioReader(new HeapInputStream(new byte[0])).nextEntry(); }
@Test public void objectSingleNameValueJson() throws IOException { try (BdioReader reader = new BdioReader(utfBytes("{ \"test\" : \"foo\" }"))) { Object entry = reader.nextEntry(); assertThat(entry).isInstanceOf(Map.class); assertThat((Map<?, ?>) entry).containsEntry("test", "foo"); assertThat(reader.nextEntry()).isNull(); } }
@Test public void singleElementListJson() throws IOException { try (BdioReader reader = new BdioReader(utfBytes("[ \"test\" ]"))) { Object entry = reader.nextEntry(); assertThat(entry).isInstanceOf(List.class); assertThat((List<?>) entry).containsExactly("test"); assertThat(reader.nextEntry()).isNull(); } }
@Test public void singleKeyObjectZip() throws IOException { try (BdioReader reader = new BdioReader(zipBytes("{ \"test\" : \"foo\" }"))) { Object entry = reader.nextEntry(); assertThat(entry).isInstanceOf(Map.class); assertThat((Map<?, ?>) entry).containsEntry("test", "foo"); assertThat(reader.nextEntry()).isNull(); } }
@Test public void singleElementListZip() throws IOException { try (BdioReader reader = new BdioReader(zipBytes("[ \"test\" ]"))) { Object entry = reader.nextEntry(); assertThat(entry).isInstanceOf(List.class); assertThat((List<?>) entry).containsExactly("test"); assertThat(reader.nextEntry()).isNull(); } }
@Test public void multipleEntryZip() throws IOException { try (BdioReader reader = new BdioReader(zipBytes("{ \"test\" : \"foo\" }", "{ \"test\" : \"bar\" }"))) { Object entry1 = reader.nextEntry(); assertThat(entry1).isInstanceOf(Map.class); assertThat((Map<?, ?>) entry1).containsEntry("test", "foo"); Object entry2 = reader.nextEntry(); assertThat(entry2).isInstanceOf(Map.class); assertThat((Map<?, ?>) entry2).containsEntry("test", "bar"); assertThat(reader.nextEntry()).isNull(); } }
@Test public void ignoreNonEntries_onlyOne() throws IOException { HeapOutputStream buffer = new HeapOutputStream(); try (ZipOutputStream zip = new ZipOutputStream(buffer)) { zip.putNextEntry(new ZipEntry(UUID.randomUUID() + ".json")); ByteStreams.copy(utfBytes("{ \"test\" : \"foo\" }"), zip); } try (BdioReader reader = new BdioReader(buffer.getInputStream())) { assertThat(reader.nextEntry()).isNull(); } }
@Test public void ignoreNonEntries_first() throws IOException { HeapOutputStream buffer = new HeapOutputStream(); try (ZipOutputStream zip = new ZipOutputStream(buffer)) { zip.putNextEntry(new ZipEntry(UUID.randomUUID() + ".json")); ByteStreams.copy(utfBytes("{ \"test\" : \"foo\" }"), zip); zip.putNextEntry(new ZipEntry(UUID.randomUUID() + ".jsonld")); ByteStreams.copy(utfBytes("{ \"test\" : \"bar\" }"), zip); } try (BdioReader reader = new BdioReader(buffer.getInputStream())) { assertThat((Map<?, ?>) reader.nextEntry()).containsEntry("test", "bar"); assertThat(reader.nextEntry()).isNull(); } }
@Test public void ignoreNonEntries_last() throws IOException { HeapOutputStream buffer = new HeapOutputStream(); try (ZipOutputStream zip = new ZipOutputStream(buffer)) { zip.putNextEntry(new ZipEntry(UUID.randomUUID() + ".jsonld")); ByteStreams.copy(utfBytes("{ \"test\" : \"bar\" }"), zip); zip.putNextEntry(new ZipEntry(UUID.randomUUID() + ".json")); ByteStreams.copy(utfBytes("{ \"test\" : \"foo\" }"), zip); } try (BdioReader reader = new BdioReader(buffer.getInputStream())) { assertThat((Map<?, ?>) reader.nextEntry()).containsEntry("test", "bar"); assertThat(reader.nextEntry()).isNull(); } }