@Override public boolean isEmpty() { DatasetReader<E> reader = null; try { // use a reader because files may be present but empty reader = newReader(); return !reader.hasNext(); } finally { if (reader != null) { reader.close(); } } }
@Override public T read() throws IOException { if (closed) { throw new StoreException("Reader is already closed"); } if (reader == null) { reader = createReader(); } if (reader.hasNext()) { return convertEntity(reader.next()); } else { return null; } }
@Test(expected = IllegalStateException.class) public void testHasNextOnClosedWriterFails() throws IOException { checkReaderBehavior(reader, totalRecords, validator); try { reader.hasNext(); } finally { reader.close(); } }
public static <R> void checkReaderIteration(DatasetReader<R> reader, int expectedRecordCount, RecordValidator<R> validator) { int recordCount = 0; Assert.assertTrue("Reader is not open", reader.isOpen()); Assert.assertTrue("Reader has no records, expected " + expectedRecordCount, (expectedRecordCount == 0) || reader.hasNext()); for (R record : reader) { // add calls to hasNext, which should not affect the iteration validator.validate(record, recordCount); Assert.assertNotNull(record); reader.hasNext(); recordCount++; } Assert.assertFalse("Reader is empty, but hasNext is true", reader.hasNext()); // verify that NoSuchElementException is thrown when hasNext returns false try { reader.next(); Assert.fail("Reader did not throw NoSuchElementException"); } catch (NoSuchElementException ex) { // this is the correct behavior } Assert.assertTrue("Reader is empty, but should be open", reader.isOpen()); // verify the correct number of records were produced // if hasNext advances the reader, then this will be wrong Assert.assertEquals("Incorrect number of records", expectedRecordCount, recordCount); }