aboutsummaryrefslogtreecommitdiffstats
path: root/cadi/core/src/test/java/org/onap/ccsdk/apps/cadi/test/JU_BufferedServletInputStream.java
blob: a6ac1cfaa64c470d013eace7c1ebb5320b563308 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.ccsdk
 * * ===========================================================================
 * * Copyright © 2023 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * *
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * *
 ******************************************************************************/

package org.onap.ccsdk.apps.cadi.test;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.*;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onap.ccsdk.apps.cadi.BufferedServletInputStream;

import static junit.framework.Assert.assertEquals;

public class JU_BufferedServletInputStream {
    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());
    }

    @Test
    public void ByteArrayReadTest() throws Exception {
        byte[] output = new byte[100];
        bsis.mark(0);
        bsis.read(output);
        Assert.assertEquals(new String(output, 0, expected.length()), expected);
    }

    @Test
    public void ByteArrayReadStateIsStoreTest() throws Exception {
        byte output[] = new byte[100];
        bsis.mark(0);
        int count = bsis.read(output, 0, expected.length());
        Assert.assertEquals(new String(output, 0, count), expected);
        Assert.assertEquals(count, expected.length());

        count = bsis.read(output, 0, 0);
        Assert.assertEquals(count, -1);
    }

    @Test
    public void ByteArrayReadStateIsReadTest() throws Exception {
        byte output[] = new byte[200];
        for (int i = 0; i < 2; ++i) {
            bsis.mark(0);
            bsis.read(output, 0, 100);
            Assert.assertEquals(new String(output, 0, expected.length()), expected);

            bsis.reset();
            bsis.read(output, 0, output.length);
            Assert.assertEquals(new String(output, 0, expected.length()), expected);
            bsis = new BufferedServletInputStream(new ByteArrayInputStream(output));
            if (i == 0) {
                output = new byte[200];
            }
        }

        Assert.assertEquals(new String(output, 0, expected.length()), expected);
    }

    @Test
    public void ByteArrayReadStateIsNoneTest() throws Exception {
        byte output[] = new byte[100];
        bsis.mark(0);

        Field state_field = BufferedServletInputStream.class.getDeclaredField("state");
        state_field.setAccessible(true);
        state_field.setInt(bsis, 0);  // state == NONE

        int count = bsis.read(output, 0, 100);
        Assert.assertEquals(count, -1);
    }

    @Test
    public void skipTest() throws Exception {
        byte output[] = new byte[100];
        bsis.mark(0);
        bsis.read(output, 0, 10);
        long count = bsis.skip(200);
        // skip returns the number left _before_ skipping. that number starts at 256
        Assert.assertEquals(count, 246);

        count = bsis.skip(200);
        Assert.assertEquals(count, 17);
    }

    @Test
    public void availableTest() throws Exception {
        int count = bsis.available();
        Assert.assertEquals(count, 27);
        bsis.mark(0);
        count = bsis.available();
        Assert.assertEquals(count, 27);
    }

    @Test
    public void bufferedTest() throws Exception {
        bsis.mark(0);
        Assert.assertEquals(bsis.buffered(), 0);
    }

    @Test
    public void closeTest() throws Exception {
        Field capacitor_field = BufferedServletInputStream.class.getDeclaredField("capacitor");
        capacitor_field.setAccessible(true);
        bsis.mark(0);
        Assert.assertNotNull(capacitor_field.get(bsis));
        bsis.close();
        Assert.assertNull(capacitor_field.get(bsis));
    }

    @Test
    public void markTest() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
        Field state_field = BufferedServletInputStream.class.getDeclaredField("state");
        Field capacitor_field = BufferedServletInputStream.class.getDeclaredField("capacitor");
        capacitor_field.setAccessible(true);
        state_field.setAccessible(true);

        // capacitor is null initially
        Assert.assertNull(capacitor_field.get(bsis));

        state_field.setInt(bsis, 0);  // state == NONE
        bsis.mark(0);  // the value passed into mark is ignored
        Assert.assertNotNull(capacitor_field.get(bsis));
        Assert.assertEquals(state_field.get(bsis), 1);  // state == STORE

        state_field.setInt(bsis, 1);  // state == STORE
        bsis.mark(0);  // the value passed into mark is ignored
        Assert.assertEquals(state_field.get(bsis), 1);  // state == STORE

        state_field.setInt(bsis, 2);  // state == READ
        bsis.mark(0);  // the value passed into mark is ignored
        Assert.assertEquals(state_field.get(bsis), 1);  // state == STORE
    }

    @Test
    public void resetTest() throws Exception {
        Field state_field = BufferedServletInputStream.class.getDeclaredField("state");
        state_field.setAccessible(true);

        bsis.mark(0);
        Assert.assertEquals(state_field.get(bsis), 1);  // state == STORE
        bsis.reset();
        Assert.assertEquals(state_field.get(bsis), 2);  // state == READ
        bsis.reset();
        Assert.assertEquals(state_field.get(bsis), 2);  // state == READ

        state_field.setInt(bsis, -1);  // state is invalid
        bsis.reset();  // This call does nothing. It is for coverage alone
        Assert.assertEquals(state_field.get(bsis), -1);  // state doesn't change

        try {
            state_field.setInt(bsis, 0);  // state == NONE
            bsis.reset();
        } catch (IOException e) {
            Assert.assertEquals(e.getMessage(), "InputStream has not been marked");
        }
    }

    @Test
    public void markSupportedTest() {
        Assert.assertTrue(bsis.markSupported());
    }

    // "Bug" 4/22/2013
    // Some XML code expects Buffered InputStream can never return 0...  This isn't actually true, but we'll accommodate as far
    // as we can.
    // Here, we make sure we set and read the Buffered data, making sure the buffer is empty on the last test...
    @Test
    public void issue04_22_2013() throws IOException {
        String testString = "We want to read in and get out with a Buffered Stream seamlessly.";
        ByteArrayInputStream bais = new ByteArrayInputStream(testString.getBytes());
        BufferedServletInputStream bsis = new BufferedServletInputStream(bais);
        try {
            bsis.mark(0);
            byte aa[] = new byte[testString.length()];  // 65 count... important for our test (divisible by 5);

            int read;
            for (int i=0;i<aa.length;i+=5) {
                read = bsis.read(aa, i, 5);
                assertEquals(5,read);
            }
            // System.out.println(new String(aa));

            bsis.reset();

            byte bb[] = new byte[aa.length];
            read = 0;
            for (int i=0;read>=0;i+=read) {
                read = bsis.read(bb,i,5);
                switch(i) {
                    case 65:
                        assertEquals(read,-1);
                        break;
                    default:
                        assertEquals(read,5);
                }
            }
            // System.out.println(new String(bb));
            assertEquals(testString,new String(aa));
            assertEquals(testString,new String(bb));

        } finally {
            bsis.close();
            bais.close();
        }

    }


}