- private BufferedServletInputStream bsis;
- private String expected;
-
- @Before
- public void setup() throws FileNotFoundException {
- expected = new String("This is the expected output");
- bsis = new BufferedServletInputStream(new ByteArrayInputStream(expected.getBytes()));
- }
-
- @After
- public void tearDown() throws IOException {
- bsis.close();
- }
-
- @Test
- public void ByteReadNoMarkTest() throws Exception {
- int c;
- int i = 0;
- byte output[] = new byte[100];
- while ((c = bsis.read()) != -1) {
- output[i++] = (byte)c;
- }
- Assert.assertEquals(new String(output, 0, i), expected);
- }
-
- @Test
- public void ByteReadMarkTest() throws Exception {
- bsis.mark(0);
- int c;
- int i = 0;
- byte output[] = new byte[100];
- while ((c = bsis.read()) != -1) {
- output[i++] = (byte)c;
- }
- Assert.assertEquals(new String(output, 0, i), expected);
- }
-
- @Test
- public void ByteReadStateIsStoreTest() throws Exception {
- Field state_field = BufferedServletInputStream.class.getDeclaredField("state");
- state_field.setAccessible(true);
- bsis.mark(0);
- int c;
- int i = 0;
- byte output[] = new byte[100];
- while ((c = bsis.read()) != -1) {
- output[i++] = (byte)c;
- }
- bsis.reset();
- Assert.assertEquals(state_field.get(bsis), 2); // state == READ
- }
-
- @Test
- public void ByteReadStateIsReadTest() throws Exception {
- bsis.mark(0); // Initialize the capacitor
- boolean isReset = false;
- int c;
- int i = 0;
- byte output[] = new byte[100];
- while ((c = bsis.read()) != -1) {
- output[i++] = (byte)c;
- if ((i > 5) && !isReset) {
- // Close the capacitor and start over. This is done for coverage purposes
- i = 0;
- isReset = true;
- bsis.reset(); // Sets state to READ
- }
- }
- Assert.assertEquals(new String(output, 0, i), expected);
- }
-
- @Test
- public void ByteReadStateIsNoneTest() throws Exception {
- Field state_field = BufferedServletInputStream.class.getDeclaredField("state");
- state_field.setAccessible(true);
- bsis.mark(0); // Initialize the capacitor
- int c;
- c = bsis.read();
- // Close the capacitor. This is done for coverage purposes
- bsis.reset(); // Sets state to READ
- state_field.setInt(bsis, 0); // state == NONE
- c = bsis.read();
- Assert.assertEquals(c, -1);
- }
-
- @Test
- public void ByteArrayReadNoMarkTest() throws Exception {
- byte output[] = new byte[100];
- int count = bsis.read(output, 0, expected.length());
- Assert.assertEquals(new String(output, 0, count), expected);
- Assert.assertEquals(count, expected.length());
- }
+ private BufferedServletInputStream bsis;
+ private String expected;
+
+ @Before
+ public void setup() throws FileNotFoundException {
+ expected = new String("This is the expected output");
+ bsis = new BufferedServletInputStream(new ByteArrayInputStream(expected.getBytes()));
+ }
+
+ @After
+ public void tearDown() throws IOException {
+ bsis.close();
+ }
+
+ @Test
+ public void ByteReadNoMarkTest() throws Exception {
+ int c;
+ int i = 0;
+ byte output[] = new byte[100];
+ while ((c = bsis.read()) != -1) {
+ output[i++] = (byte)c;
+ }
+ Assert.assertEquals(new String(output, 0, i), expected);
+ }
+
+ @Test
+ public void ByteReadMarkTest() throws Exception {
+ bsis.mark(0);
+ int c;
+ int i = 0;
+ byte output[] = new byte[100];
+ while ((c = bsis.read()) != -1) {
+ output[i++] = (byte)c;
+ }
+ Assert.assertEquals(new String(output, 0, i), expected);
+ }
+
+ @Test
+ public void ByteReadStateIsStoreTest() throws Exception {
+ Field state_field = BufferedServletInputStream.class.getDeclaredField("state");
+ state_field.setAccessible(true);
+ bsis.mark(0);
+ int c;
+ int i = 0;
+ byte output[] = new byte[100];
+ while ((c = bsis.read()) != -1) {
+ output[i++] = (byte)c;
+ }
+ bsis.reset();
+ Assert.assertEquals(state_field.get(bsis), 2); // state == READ
+ }
+
+ @Test
+ public void ByteReadStateIsReadTest() throws Exception {
+ bsis.mark(0); // Initialize the capacitor
+ boolean isReset = false;
+ int c;
+ int i = 0;
+ byte output[] = new byte[100];
+ while ((c = bsis.read()) != -1) {
+ output[i++] = (byte)c;
+ if ((i > 5) && !isReset) {
+ // Close the capacitor and start over. This is done for coverage purposes
+ i = 0;
+ isReset = true;
+ bsis.reset(); // Sets state to READ
+ }
+ }
+ Assert.assertEquals(new String(output, 0, i), expected);
+ }
+
+ @Test
+ public void ByteReadStateIsNoneTest() throws Exception {
+ Field state_field = BufferedServletInputStream.class.getDeclaredField("state");
+ state_field.setAccessible(true);
+ bsis.mark(0); // Initialize the capacitor
+ int c;
+ c = bsis.read();
+ // Close the capacitor. This is done for coverage purposes
+ bsis.reset(); // Sets state to READ
+ state_field.setInt(bsis, 0); // state == NONE
+ c = bsis.read();
+ Assert.assertEquals(c, -1);
+ }
+
+ @Test
+ public void ByteArrayReadNoMarkTest() throws Exception {
+ byte output[] = new byte[100];
+ int count = bsis.read(output, 0, expected.length());
+ Assert.assertEquals(new String(output, 0, count), expected);
+ Assert.assertEquals(count, expected.length());
+ }