/** * This test case initiates a mark after reading 100 bytes from the stream. * Reads the next 100 bytes from the stream. Now the stream is reset and it * is drained. At the last read, the length validation must fail because the * stream was initialized with wrong expected length. */ @Test public void testMarkAndResetWithWrongExpectedLength() throws Exception { InputStream in = new ByteArrayInputStream(sampleData.getBytes(StringUtils.UTF8)); LengthCheckInputStream rvis = new LengthCheckInputStream(in, sampleData.getBytes(StringUtils.UTF8).length + 1, INCLUDE_SKIPPED_BYTES); rvis.read(new byte[100]); rvis.mark(100); rvis.read(new byte[100]); rvis.reset(); rvis.read(new byte[sampleData.getBytes(StringUtils.UTF8).length - 100]); try { rvis.read(); fail(); } catch (Exception e) { System.out .println("Exception occurred. Message: " + e.getMessage()); } rvis.close(); }
/** * This test case initiates a mark at the start, reads the first 100 bytes * from the stream. Now the stream is reset and it is drained. At the last * read, the length validation must be success. */ @Test public void testMarkInitiallyAndReset() throws Exception { InputStream in = new ByteArrayInputStream(sampleData.getBytes(StringUtils.UTF8)); LengthCheckInputStream rvis = new LengthCheckInputStream(in, sampleData.getBytes(StringUtils.UTF8).length, INCLUDE_SKIPPED_BYTES); rvis.mark(100); rvis.read(new byte[100]); rvis.reset(); rvis.read(new byte[sampleData.getBytes(StringUtils.UTF8).length]); try { rvis.read(); } catch (Exception e) { fail(); } rvis.close(); StreamUtils.consumeInputStream(rvis); }
/** * This test case initiates a marks the stream initially, drains the whole * stream. Resets the stream and drains again. */ @Test public void testMarkAndResetAtEnd() throws Exception { InputStream in = new ByteArrayInputStream(sampleData.getBytes(StringUtils.UTF8)); LengthCheckInputStream rvis = new LengthCheckInputStream(in, sampleData.getBytes(StringUtils.UTF8).length, INCLUDE_SKIPPED_BYTES); rvis.mark(100); StreamUtils.consumeInputStream(rvis); rvis.reset(); StreamUtils.consumeInputStream(rvis); try { rvis.read(); } catch (Exception e) { fail(); } rvis.close(); }