2222import org .slf4j .LoggerFactory ;
2323
2424import com .google .common .base .Charsets ;
25+ import com .google .common .io .ByteSource ;
26+ import com .google .common .io .ByteStreams ;
2527import com .google .common .io .CharStreams ;
2628import com .google .common .io .InputSupplier ;
2729
30+ @ SuppressWarnings ("unused" )
2831public class JavaInputStreamToXUnitTest {
2932 protected final Logger logger = LoggerFactory .getLogger (getClass ());
30- private static final int DEFAULT_SIZE = 150000000 ;
31-
32- // private static final int DEFAULT_SIZE = 8;
33+ private static final int DEFAULT_SIZE = 1500000 ;
3334
3435 // tests - InputStream to String
3536
36- // 11s
3737 @ Test
38- public void givenUsingJava5_whenConvertingAnInputStreamToAString_thenCorrect () throws IOException {
38+ public final void givenUsingJava5_whenConvertingAnInputStreamToAString_thenCorrect () throws IOException {
3939 final String originalString = randomAlphabetic (DEFAULT_SIZE );
4040 final InputStream inputStream = new ByteArrayInputStream (originalString .getBytes ());
4141
@@ -49,7 +49,6 @@ public void givenUsingJava5_whenConvertingAnInputStreamToAString_thenCorrect() t
4949 assertEquals (textBuilder .toString (), originalString );
5050 }
5151
52- // 8s
5352 @ Test
5453 public final void givenUsingJava7_whenConvertingAnInputStreamToAString_thenCorrect () throws IOException {
5554 final String originalString = randomAlphabetic (DEFAULT_SIZE );
@@ -120,4 +119,25 @@ public final void givenUsingCommonsIoWithCopy_whenConvertingAnInputStreamToAStri
120119 assertThat (writer .toString (), equalTo (originalString ));
121120 }
122121
122+ // tests - InputStream to byte[]
123+
124+ @ Test
125+ public final void givenUsingPlainJava_whenConvertingAnInputStreamToAByteArray_thenCorrect () throws IOException {
126+ final InputStream initialStream = new ByteArrayInputStream (new byte [] { 0 , 1 , 2 });
127+ final byte [] targetArray = new byte [initialStream .available ()];
128+ initialStream .read (targetArray );
129+ }
130+
131+ @ Test
132+ public final void givenUsingGuava_whenConvertingAnInputStreamToAByteArray_thenCorrect () throws IOException {
133+ final InputStream initialStream = ByteSource .wrap (new byte [] { 0 , 1 , 2 }).openStream ();
134+ final byte [] targetArray = ByteStreams .toByteArray (initialStream );
135+ }
136+
137+ @ Test
138+ public final void givenUsingCommonsIO_whenConvertingAnInputStreamToAByteArray_thenCorrect () throws IOException {
139+ final ByteArrayInputStream initialStream = new ByteArrayInputStream (new byte [] { 0 , 1 , 2 });
140+ final byte [] targetArray = IOUtils .toByteArray (initialStream );
141+ }
142+
123143}
0 commit comments