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