1 /*******************************************************************************
2 * ============LICENSE_START====================================================
4 * * ===========================================================================
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * * ===========================================================================
7 * * Licensed under the Apache License, Version 2.0 (the "License");
8 * * you may not use this file except in compliance with the License.
9 * * You may obtain a copy of the License at
11 * * http://www.apache.org/licenses/LICENSE-2.0
13 * * Unless required by applicable law or agreed to in writing, software
14 * * distributed under the License is distributed on an "AS IS" BASIS,
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * * See the License for the specific language governing permissions and
17 * * limitations under the License.
18 * * ============LICENSE_END====================================================
21 ******************************************************************************/
23 package org.onap.aaf.cadi.test;
25 import java.io.ByteArrayInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.lang.reflect.*;
29 import junit.framework.Assert;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.aaf.cadi.BufferedServletInputStream;
35 import static junit.framework.Assert.assertEquals;
37 public class JU_BufferedServletInputStream {
38 private BufferedServletInputStream bsis;
39 private String expected;
42 public void setup() throws FileNotFoundException {
43 expected = new String("This is the expected output");
44 bsis = new BufferedServletInputStream(new ByteArrayInputStream(expected.getBytes()));
48 public void tearDown() throws IOException {
53 public void ByteReadNoMarkTest() throws Exception {
56 byte output[] = new byte[100];
57 while ((c = bsis.read()) != -1) {
58 output[i++] = (byte)c;
60 Assert.assertEquals(new String(output, 0, i), expected);
64 public void ByteReadMarkTest() throws Exception {
68 byte output[] = new byte[100];
69 while ((c = bsis.read()) != -1) {
70 output[i++] = (byte)c;
72 Assert.assertEquals(new String(output, 0, i), expected);
76 public void ByteReadStateIsStoreTest() throws Exception {
77 Field state_field = BufferedServletInputStream.class.getDeclaredField("state");
78 state_field.setAccessible(true);
82 byte output[] = new byte[100];
83 while ((c = bsis.read()) != -1) {
84 output[i++] = (byte)c;
87 Assert.assertEquals(state_field.get(bsis), 2); // state == READ
91 public void ByteReadStateIsReadTest() throws Exception {
92 bsis.mark(0); // Initialize the capacitor
93 boolean isReset = false;
96 byte output[] = new byte[100];
97 while ((c = bsis.read()) != -1) {
98 output[i++] = (byte)c;
99 if ((i > 5) && !isReset) {
100 // Close the capacitor and start over. This is done for coverage purposes
103 bsis.reset(); // Sets state to READ
106 Assert.assertEquals(new String(output, 0, i), expected);
110 public void ByteReadStateIsNoneTest() throws Exception {
111 Field state_field = BufferedServletInputStream.class.getDeclaredField("state");
112 state_field.setAccessible(true);
113 bsis.mark(0); // Initialize the capacitor
116 // Close the capacitor. This is done for coverage purposes
117 bsis.reset(); // Sets state to READ
118 state_field.setInt(bsis, 0); // state == NONE
120 Assert.assertEquals(c, -1);
124 public void ByteArrayReadNoMarkTest() throws Exception {
125 byte output[] = new byte[100];
126 int count = bsis.read(output, 0, expected.length());
127 Assert.assertEquals(new String(output, 0, count), expected);
128 Assert.assertEquals(count, expected.length());
132 public void ByteArrayReadTest() throws Exception {
133 byte[] output = new byte[100];
136 Assert.assertEquals(new String(output, 0, expected.length()), expected);
140 public void ByteArrayReadStateIsStoreTest() throws Exception {
141 byte output[] = new byte[100];
143 int count = bsis.read(output, 0, expected.length());
144 Assert.assertEquals(new String(output, 0, count), expected);
145 Assert.assertEquals(count, expected.length());
147 count = bsis.read(output, 0, 0);
148 Assert.assertEquals(count, -1);
152 public void ByteArrayReadStateIsReadTest() throws Exception {
153 byte output[] = new byte[200];
154 for (int i = 0; i < 2; ++i) {
156 bsis.read(output, 0, 100);
157 Assert.assertEquals(new String(output, 0, expected.length()), expected);
160 bsis.read(output, 0, output.length);
161 Assert.assertEquals(new String(output, 0, expected.length()), expected);
162 bsis = new BufferedServletInputStream(new ByteArrayInputStream(output));
164 output = new byte[200];
168 Assert.assertEquals(new String(output, 0, expected.length()), expected);
172 public void ByteArrayReadStateIsNoneTest() throws Exception {
173 byte output[] = new byte[100];
176 Field state_field = BufferedServletInputStream.class.getDeclaredField("state");
177 state_field.setAccessible(true);
178 state_field.setInt(bsis, 0); // state == NONE
180 int count = bsis.read(output, 0, 100);
181 Assert.assertEquals(count, -1);
185 public void skipTest() throws Exception {
186 byte output[] = new byte[100];
188 bsis.read(output, 0, 10);
189 long count = bsis.skip(200);
190 // skip returns the number left _before_ skipping. that number starts at 256
191 Assert.assertEquals(count, 246);
193 count = bsis.skip(200);
194 Assert.assertEquals(count, 17);
198 public void availableTest() throws Exception {
199 int count = bsis.available();
200 Assert.assertEquals(count, 27);
202 count = bsis.available();
203 Assert.assertEquals(count, 27);
207 public void bufferedTest() throws Exception {
209 Assert.assertEquals(bsis.buffered(), 0);
213 public void closeTest() throws Exception {
214 Field capacitor_field = BufferedServletInputStream.class.getDeclaredField("capacitor");
215 capacitor_field.setAccessible(true);
217 Assert.assertNotNull(capacitor_field.get(bsis));
219 Assert.assertNull(capacitor_field.get(bsis));
223 public void markTest() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
224 Field state_field = BufferedServletInputStream.class.getDeclaredField("state");
225 Field capacitor_field = BufferedServletInputStream.class.getDeclaredField("capacitor");
226 capacitor_field.setAccessible(true);
227 state_field.setAccessible(true);
229 // capacitor is null initially
230 Assert.assertNull(capacitor_field.get(bsis));
232 state_field.setInt(bsis, 0); // state == NONE
233 bsis.mark(0); // the value passed into mark is ignored
234 Assert.assertNotNull(capacitor_field.get(bsis));
235 Assert.assertEquals(state_field.get(bsis), 1); // state == STORE
237 state_field.setInt(bsis, 1); // state == STORE
238 bsis.mark(0); // the value passed into mark is ignored
239 Assert.assertEquals(state_field.get(bsis), 1); // state == STORE
241 state_field.setInt(bsis, 2); // state == READ
242 bsis.mark(0); // the value passed into mark is ignored
243 Assert.assertEquals(state_field.get(bsis), 1); // state == STORE
247 public void resetTest() throws Exception {
248 Field state_field = BufferedServletInputStream.class.getDeclaredField("state");
249 state_field.setAccessible(true);
252 Assert.assertEquals(state_field.get(bsis), 1); // state == STORE
254 Assert.assertEquals(state_field.get(bsis), 2); // state == READ
256 Assert.assertEquals(state_field.get(bsis), 2); // state == READ
258 state_field.setInt(bsis, -1); // state is invalid
259 bsis.reset(); // This call does nothing. It is for coverage alone
260 Assert.assertEquals(state_field.get(bsis), -1); // state doesn't change
263 state_field.setInt(bsis, 0); // state == NONE
265 } catch (IOException e) {
266 Assert.assertEquals(e.getMessage(), "InputStream has not been marked");
271 public void markSupportedTest() {
272 Assert.assertTrue(bsis.markSupported());
276 // Some XML code expects Buffered InputStream can never return 0... This isn't actually true, but we'll accommodate as far
278 // Here, we make sure we set and read the Buffered data, making sure the buffer is empty on the last test...
280 public void issue04_22_2013() throws IOException {
281 String testString = "We want to read in and get out with a Buffered Stream seamlessly.";
282 ByteArrayInputStream bais = new ByteArrayInputStream(testString.getBytes());
283 BufferedServletInputStream bsis = new BufferedServletInputStream(bais);
286 byte aa[] = new byte[testString.length()]; // 65 count... important for our test (divisible by 5);
289 for (int i=0;i<aa.length;i+=5) {
290 read = bsis.read(aa, i, 5);
291 assertEquals(5,read);
293 // System.out.println(new String(aa));
297 byte bb[] = new byte[aa.length];
299 for (int i=0;read>=0;i+=read) {
300 read = bsis.read(bb,i,5);
303 assertEquals(read,-1);
306 assertEquals(read,5);
309 // System.out.println(new String(bb));
310 assertEquals(testString,new String(aa));
311 assertEquals(testString,new String(bb));