Mass removal of all Tabs (Style Warnings)
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / test / JU_BufferedServletInputStream.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
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
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
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====================================================
19  * *
20  * *
21  ******************************************************************************/
22 package org.onap.aaf.cadi.test;
23
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;
33
34 import static junit.framework.Assert.assertEquals;
35
36 public class JU_BufferedServletInputStream {
37     private BufferedServletInputStream bsis;
38     private String expected;
39
40     @Before
41     public void setup() throws FileNotFoundException {
42         expected = new String("This is the expected output");
43         bsis = new BufferedServletInputStream(new ByteArrayInputStream(expected.getBytes()));
44     }
45
46     @After
47     public void tearDown() throws IOException {
48         bsis.close();
49     }
50
51     @Test
52     public void ByteReadNoMarkTest() throws Exception {
53         int c;
54         int i = 0;
55         byte output[] = new byte[100];
56         while ((c = bsis.read()) != -1) {
57             output[i++] = (byte)c;
58         }
59         Assert.assertEquals(new String(output, 0, i), expected);
60     }
61
62     @Test
63     public void ByteReadMarkTest() throws Exception {
64         bsis.mark(0);
65         int c;
66         int i = 0;
67         byte output[] = new byte[100];
68         while ((c = bsis.read()) != -1) {
69             output[i++] = (byte)c;
70         }
71         Assert.assertEquals(new String(output, 0, i), expected);
72     }
73
74     @Test
75     public void ByteReadStateIsStoreTest() throws Exception {
76         Field state_field = BufferedServletInputStream.class.getDeclaredField("state");
77         state_field.setAccessible(true);
78         bsis.mark(0);
79         int c;
80         int i = 0;
81         byte output[] = new byte[100];
82         while ((c = bsis.read()) != -1) {
83             output[i++] = (byte)c;
84         }
85         bsis.reset();
86         Assert.assertEquals(state_field.get(bsis), 2);  // state == READ
87     }
88
89     @Test
90     public void ByteReadStateIsReadTest() throws Exception {
91         bsis.mark(0);  // Initialize the capacitor
92         boolean isReset = false;
93         int c;
94         int i = 0;
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
100                 i = 0;
101                 isReset = true;
102                 bsis.reset();  // Sets state to READ
103             }
104         }
105         Assert.assertEquals(new String(output, 0, i), expected);
106     }
107
108     @Test
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
113         int c;
114         c = bsis.read();
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
118         c = bsis.read();
119         Assert.assertEquals(c, -1);
120     }
121
122     @Test
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());
128     }
129
130     @Test
131     public void ByteArrayReadTest() throws Exception {
132         byte[] output = new byte[100];
133         bsis.mark(0);
134         bsis.read(output);
135         Assert.assertEquals(new String(output, 0, expected.length()), expected);
136     }
137
138     @Test
139     public void ByteArrayReadStateIsStoreTest() throws Exception {
140         byte output[] = new byte[100];
141         bsis.mark(0);
142         int count = bsis.read(output, 0, expected.length());
143         Assert.assertEquals(new String(output, 0, count), expected);
144         Assert.assertEquals(count, expected.length());
145
146         count = bsis.read(output, 0, 0);
147         Assert.assertEquals(count, -1);
148     }
149
150     @Test
151     public void ByteArrayReadStateIsReadTest() throws Exception {
152         byte output[] = new byte[200];
153         for(int i = 0; i < 2; ++i) {
154             bsis.mark(0);
155             bsis.read(output, 0, 100);
156             Assert.assertEquals(new String(output, 0, expected.length()), expected);
157
158             bsis.reset();
159             bsis.read(output, 0, output.length);
160             Assert.assertEquals(new String(output, 0, expected.length()), expected);
161             bsis = new BufferedServletInputStream(new ByteArrayInputStream(output));
162             if(i == 0) {
163                 output = new byte[200];
164             }
165         }
166
167         Assert.assertEquals(new String(output, 0, expected.length()), expected);
168     }
169
170     @Test
171     public void ByteArrayReadStateIsNoneTest() throws Exception {
172         byte output[] = new byte[100];
173         bsis.mark(0);
174
175         Field state_field = BufferedServletInputStream.class.getDeclaredField("state");
176         state_field.setAccessible(true);
177         state_field.setInt(bsis, 0);  // state == NONE
178
179         int count = bsis.read(output, 0, 100);
180         Assert.assertEquals(count, -1);
181     }
182
183     @Test
184     public void skipTest() throws Exception {
185         byte output[] = new byte[100];
186         bsis.mark(0);
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);
191
192         count = bsis.skip(200);
193         Assert.assertEquals(count, 17);
194     }
195
196     @Test
197     public void availableTest() throws Exception {
198         int count = bsis.available();
199         Assert.assertEquals(count, 27);
200         bsis.mark(0);
201         count = bsis.available();
202         Assert.assertEquals(count, 27);
203     }
204
205     @Test
206     public void bufferedTest() throws Exception {
207         bsis.mark(0);
208         Assert.assertEquals(bsis.buffered(), 0);
209     }
210
211     @Test
212     public void closeTest() throws Exception {
213         Field capacitor_field = BufferedServletInputStream.class.getDeclaredField("capacitor");
214         capacitor_field.setAccessible(true);
215         bsis.mark(0);
216         Assert.assertNotNull(capacitor_field.get(bsis));
217         bsis.close();
218         Assert.assertNull(capacitor_field.get(bsis));
219     }
220
221     @Test
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);
227
228         // capacitor is null initially
229         Assert.assertNull(capacitor_field.get(bsis));
230
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
235
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
239
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
243     }
244
245     @Test
246     public void resetTest() throws Exception {
247         Field state_field = BufferedServletInputStream.class.getDeclaredField("state");
248         state_field.setAccessible(true);
249
250         bsis.mark(0);
251         Assert.assertEquals(state_field.get(bsis), 1);  // state == STORE
252         bsis.reset();
253         Assert.assertEquals(state_field.get(bsis), 2);  // state == READ
254         bsis.reset();
255         Assert.assertEquals(state_field.get(bsis), 2);  // state == READ
256
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
260
261         try {
262             state_field.setInt(bsis, 0);  // state == NONE
263             bsis.reset();
264         } catch (IOException e) {
265             Assert.assertEquals(e.getMessage(), "InputStream has not been marked");
266         }
267     }
268
269     @Test
270     public void markSupportedTest() {
271         Assert.assertTrue(bsis.markSupported());
272     }
273
274     // "Bug" 4/22/2013
275     // Some XML code expects Buffered InputStream can never return 0...  This isn't actually true, but we'll accommodate as far
276     // as we can.
277     // Here, we make sure we set and read the Buffered data, making sure the buffer is empty on the last test...
278     @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);
283         try {
284             bsis.mark(0);
285             byte aa[] = new byte[testString.length()];  // 65 count... important for our test (divisible by 5);
286
287             int read;
288             for(int i=0;i<aa.length;i+=5) {
289                 read = bsis.read(aa, i, 5);
290                 assertEquals(5,read);
291             }
292             // System.out.println(new String(aa));
293
294             bsis.reset();
295
296             byte bb[] = new byte[aa.length];
297             read = 0;
298             for(int i=0;read>=0;i+=read) {
299                 read = bsis.read(bb,i,5);
300                 switch(i) {
301                     case 65:
302                         assertEquals(read,-1);
303                         break;
304                     default:
305                         assertEquals(read,5);
306                 }
307             }
308             // System.out.println(new String(bb));
309             assertEquals(testString,new String(aa));
310             assertEquals(testString,new String(bb));
311
312         } finally {
313             bsis.close();
314             bais.close();
315         }
316
317     }
318
319
320 }