/** * Tests if the content length set in the stream equals the bytes read from * the stream. If any exception is thrown, then the test fails. */ @Test public void testContentLength() throws Exception { InputStream in = new ByteArrayInputStream(sampleData.getBytes(StringUtils.UTF8)); LengthCheckInputStream rvis = new LengthCheckInputStream(in, sampleData.getBytes(StringUtils.UTF8).length, INCLUDE_SKIPPED_BYTES); try { StreamUtils.consumeInputStream(rvis); } catch (Exception e) { fail(); } rvis.close(); }
/** * Skipped more than what's expected. */ @Test public void testOverSkipped() throws IOException { byte[] bytes = new byte[100]; LengthCheckInputStream is = new LengthCheckInputStream( new ByteArrayInputStream(bytes), 99, INCLUDE_SKIPPED_BYTES); try { is.skip(100); fail(); } catch (AmazonClientException ex) { // expected } is.close(); } }
/** * Actual number of bytes consumed is less than what's expected, when * skipped bytes are excluded. */ @Test public void testSkipExcludedFailure() throws IOException { byte[] bytes = new byte[100]; LengthCheckInputStream is = new LengthCheckInputStream( new ByteArrayInputStream(bytes), 100, EXCLUDE_SKIPPED_BYTES); assertTrue(10 == is.skip(10)); try { StreamUtils.consumeInputStream(is); fail(); } catch (AmazonClientException ex) { // expected } is.close(); }
/** * 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 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(); }
/** * 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); }