1
2
3
4
5
6
7 package com.diasparsoftware.gsbase.test;
8
9 import java.io.*;
10 import java.util.Arrays;
11
12 import junit.framework.TestCase;
13 import junitx.framework.ArrayAssert;
14
15 import com.diasparsoftware.gsbase.StreamUtil;
16 import com.gargoylesoftware.base.util.DetailedNullPointerException;
17
18 public class GetContentAsBytesTest extends TestCase {
19 public void testNull() throws Exception {
20 try {
21 StreamUtil.getContentAsBytes(null);
22 fail("Allowed null parameter");
23 }
24 catch (DetailedNullPointerException expected) {
25
26 }
27 }
28
29 public void testEmptyStream() throws Exception {
30 InputStream emptyStream = new ByteArrayInputStream(new byte[0]);
31 ArrayAssert.assertEquals(
32 new byte[0],
33 StreamUtil.getContentAsBytes(emptyStream));
34 }
35
36 public void testSimpleHappyPath() throws Exception {
37 InputStream happyPathStream =
38 new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 });
39
40 ArrayAssert.assertEquals(
41 new byte[] { 1, 2, 3, 4 },
42 StreamUtil.getContentAsBytes(happyPathStream));
43 }
44
45 public void testLargeBuffer() throws Exception {
46 byte[] largeBuffer = new byte[0xFEDCB];
47 byte[] copyOfLargeBuffer = new byte[0xFEDCB];
48
49 Arrays.fill(largeBuffer, (byte) 62);
50 Arrays.fill(copyOfLargeBuffer, (byte) 62);
51
52 InputStream happyPathStream =
53 new ByteArrayInputStream(largeBuffer);
54
55 ArrayAssert.assertEquals(
56 copyOfLargeBuffer,
57 StreamUtil.getContentAsBytes(happyPathStream));
58 }
59
60 }