001/* 002 * The contents of this file are subject to the license and copyright 003 * detailed in the LICENSE and NOTICE files at the root of the source 004 * tree. 005 */ 006package org.fcrepo.http.commons.responses; 007 008import static java.nio.charset.StandardCharsets.UTF_8; 009import static org.junit.Assert.assertEquals; 010 011import java.io.ByteArrayInputStream; 012import java.io.IOException; 013import java.io.InputStream; 014 015import org.apache.commons.io.IOUtils; 016import org.junit.Test; 017 018/** 019 * <p>RangeRequestInputStreamTest class.</p> 020 * 021 * @author awoods 022 */ 023public class RangeRequestInputStreamTest { 024 @Test 025 public void shouldLimitTheInputStream() throws IOException { 026 final InputStream in = new ByteArrayInputStream("0123456789".getBytes()); 027 try (final RangeRequestInputStream out = new RangeRequestInputStream(in, 5L, 3L)) { 028 final String s = IOUtils.toString(out, UTF_8); 029 assertEquals("567", s); 030 } 031 } 032 033 034 @Test 035 public void shouldAcceptUnboundedRanges() throws IOException { 036 final InputStream in = new ByteArrayInputStream("0123456789".getBytes()); 037 try (final RangeRequestInputStream out = new RangeRequestInputStream(in, 0L, -1L)) { 038 final String s = IOUtils.toString(out, UTF_8); 039 assertEquals("0123456789", s); 040 } 041 } 042 043 @Test 044 public void getGetLongRange() throws IOException { 045 final StringBuilder buf = new StringBuilder(); 046 while ( buf.length() < 9000 ) { 047 buf.append("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); 048 } 049 final InputStream in = new ByteArrayInputStream(buf.toString().getBytes()); 050 try (final RangeRequestInputStream out = new RangeRequestInputStream(in, 0L, 9000)) { 051 assertEquals(9000, IOUtils.toString(out, UTF_8).length()); 052 } 053 } 054}