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 ******************************************************************************/
22 package org.onap.aaf.cadi.test;
24 import java.io.ByteArrayInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.lang.reflect.*;
28 import junit.framework.Assert;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.aaf.cadi.BufferedServletInputStream;
34 import static junit.framework.Assert.assertEquals;
36 public class JU_BufferedServletInputStream {
37 private BufferedServletInputStream bsis;
38 private String expected;
41 public void setup() throws FileNotFoundException {
42 expected = new String("This is the expected output");
43 bsis = new BufferedServletInputStream(new ByteArrayInputStream(expected.getBytes()));
47 public void tearDown() throws IOException {
52 public void ByteReadNoMarkTest() throws Exception {
55 byte output[] = new byte[100];
56 while ((c = bsis.read()) != -1) {
57 output[i++] = (byte)c;
59 Assert.assertEquals(new String(output, 0, i), expected);
63 public void ByteReadMarkTest() throws Exception {
67 byte output[] = new byte[100];
68 while ((c = bsis.read()) != -1) {
69 output[i++] = (byte)c;
71 Assert.assertEquals(new String(output, 0, i), expected);
75 public void ByteReadStateIsStoreTest() throws Exception {
76 Field state_field = BufferedServletInputStream.class.getDeclaredField("state");
77 state_field.setAccessible(true);
81 byte output[] = new byte[100];
82 while ((c = bsis.read()) != -1) {
83 output[i++] = (byte)c;
86 Assert.assertEquals(state_field.get(bsis), 2); // state == READ
90 public void ByteReadStateIsReadTest() throws Exception {
91 bsis.mark(0); // Initialize the capacitor
92 boolean isReset = false;
95 byte output[] = new byte[100];
96 while ((c = bsis.read()) != -1) {
97 output[i++] = (byte)c;
98 if ((i > 5) && !isReset) {
99 // Close the capacitor and start over. This is done for coverage purposes
102 bsis.reset(); // Sets state to READ
105 Assert.assertEquals(new String(output, 0, i), expected);
109 public void ByteReadStateIsNoneTest() throws Exception {
110 Field state_field = BufferedServletInputStream.class.getDeclaredField("state");
111 state_field.setAccessible(true);
112 bsis.mark(0); // Initialize the capacitor
115 // Close the capacitor. This is done for coverage purposes
116 bsis.reset(); // Sets state to READ
117 state_field.setInt(bsis, 0); // state == NONE
119 Assert.assertEquals(c, -1);
123 public void ByteArrayReadNoMarkTest() throws Exception {
124 byte output[] = new byte[100];
125 int count = bsis.read(output, 0, expected.length());
126 Assert.assertEquals(new String(output, 0, count), expected);
127 Assert.assertEquals(count, expected.length());
131 public void ByteArrayReadTest() throws Exception {
132 byte[] output = new byte[100];
135 Assert.assertEquals(new String(output, 0, expected.length()), expected);
139 public void ByteArrayReadStateIsStoreTest() throws Exception {
140 byte output[] = new byte[100];
142 int count = bsis.read(output, 0, expected.length());
143 Assert.assertEquals(new String(output, 0, count), expected);
144 Assert.assertEquals(count, expected.length());
146 count = bsis.read(output, 0, 0);
147 Assert.assertEquals(count, -1);
151 public void ByteArrayReadStateIsReadTest() throws Exception {
152 byte output[] = new byte[200];
153 for(int i = 0; i < 2; ++i) {
155 bsis.read(output, 0, 100);
156 Assert.assertEquals(new String(output, 0, expected.length()), expected);
159 bsis.read(output, 0, output.length);
160 Assert.assertEquals(new String(output, 0, expected.length()), expected);
161 bsis = new BufferedServletInputStream(new ByteArrayInputStream(output));
163 output = new byte[200];
167 Assert.assertEquals(new String(output, 0, expected.length()), expected);
171 public void ByteArrayReadStateIsNoneTest() throws Exception {
172 byte output[] = new byte[100];
175 Field state_field = BufferedServletInputStream.class.getDeclaredField("state");
176 state_field.setAccessible(true);
177 state_field.setInt(bsis, 0); // state == NONE
179 int count = bsis.read(output, 0, 100);
180 Assert.assertEquals(count, -1);
184 public void skipTest() throws Exception {
185 byte output[] = new byte[100];
187 bsis.read(output, 0, 10);
188 long count = bsis.skip(200);
189 // skip returns the number left _before_ skipping. that number starts at 256
190 Assert.assertEquals(count, 246);
192 count = bsis.skip(200);
193 Assert.assertEquals(count, 17);
197 public void availableTest() throws Exception {
198 int count = bsis.available();
199 Assert.assertEquals(count, 27);
201 count = bsis.available();
202 Assert.assertEquals(count, 27);
206 public void bufferedTest() throws Exception {
208 Assert.assertEquals(bsis.buffered(), 0);
212 public void closeTest() throws Exception {
213 Field capacitor_field = BufferedServletInputStream.class.getDeclaredField("capacitor");
214 capacitor_field.setAccessible(true);
216 Assert.assertNotNull(capacitor_field.get(bsis));
218 Assert.assertNull(capacitor_field.get(bsis));
222 public void markTest() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
223 Field state_field = BufferedServletInputStream.class.getDeclaredField("state");
224 Field capacitor_field = BufferedServletInputStream.class.getDeclaredField("capacitor");
225 capacitor_field.setAccessible(true);
226 state_field.setAccessible(true);
228 // capacitor is null initially
229 Assert.assertNull(capacitor_field.get(bsis));
231 state_field.setInt(bsis, 0); // state == NONE
232 bsis.mark(0); // the value passed into mark is ignored
233 Assert.assertNotNull(capacitor_field.get(bsis));
234 Assert.assertEquals(state_field.get(bsis), 1); // state == STORE
236 state_field.setInt(bsis, 1); // state == STORE
237 bsis.mark(0); // the value passed into mark is ignored
238 Assert.assertEquals(state_field.get(bsis), 1); // state == STORE
240 state_field.setInt(bsis, 2); // state == READ
241 bsis.mark(0); // the value passed into mark is ignored
242 Assert.assertEquals(state_field.get(bsis), 1); // state == STORE
246 public void resetTest() throws Exception {
247 Field state_field = BufferedServletInputStream.class.getDeclaredField("state");
248 state_field.setAccessible(true);
251 Assert.assertEquals(state_field.get(bsis), 1); // state == STORE
253 Assert.assertEquals(state_field.get(bsis), 2); // state == READ
255 Assert.assertEquals(state_field.get(bsis), 2); // state == READ
257 state_field.setInt(bsis, -1); // state is invalid
258 bsis.reset(); // This call does nothing. It is for coverage alone
259 Assert.assertEquals(state_field.get(bsis), -1); // state doesn't change
262 state_field.setInt(bsis, 0); // state == NONE
264 } catch (IOException e) {
265 Assert.assertEquals(e.getMessage(), "InputStream has not been marked");
270 public void markSupportedTest() {
271 Assert.assertTrue(bsis.markSupported());
275 // Some XML code expects Buffered InputStream can never return 0... This isn't actually true, but we'll accommodate as far
277 // Here, we make sure we set and read the Buffered data, making sure the buffer is empty on the last test...
279 public void issue04_22_2013() throws IOException {
280 String testString = "We want to read in and get out with a Buffered Stream seamlessly.";
281 ByteArrayInputStream bais = new ByteArrayInputStream(testString.getBytes());
282 BufferedServletInputStream bsis = new BufferedServletInputStream(bais);
285 byte aa[] = new byte[testString.length()]; // 65 count... important for our test (divisible by 5);
288 for(int i=0;i<aa.length;i+=5) {
289 read = bsis.read(aa, i, 5);
290 assertEquals(5,read);
292 // System.out.println(new String(aa));
296 byte bb[] = new byte[aa.length];
298 for(int i=0;read>=0;i+=read) {
299 read = bsis.read(bb,i,5);
302 assertEquals(read,-1);
305 assertEquals(read,5);
308 // System.out.println(new String(bb));
309 assertEquals(testString,new String(aa));
310 assertEquals(testString,new String(bb));