Add Notice of aaf/cadi source moving to aaf/authz
[aaf/cadi.git] / core / src / test / java / org / onap / aaf / cadi / test / JU_BufferedServletInputStream.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.onap.aaf.cadi.test;\r
24 \r
25 import static junit.framework.Assert.assertEquals;\r
26 \r
27 import java.io.ByteArrayInputStream;\r
28 import java.io.FileInputStream;\r
29 import java.io.IOException;\r
30 \r
31 import org.junit.Test;\r
32 import org.onap.aaf.cadi.BufferedServletInputStream;\r
33 \r
34 public class JU_BufferedServletInputStream {\r
35 \r
36         @Test\r
37         public void testByteRead() throws Exception {\r
38                 FileInputStream fis = new FileInputStream("test/CBUSevent.xml");\r
39                 BufferedServletInputStream bsis = new BufferedServletInputStream(fis);\r
40                         try {\r
41                         bsis.mark(0);\r
42                         int c;\r
43                         byte aa[] = new byte[260];\r
44                         for(int i=0;i<aa.length;++i) {\r
45                                 c = bsis.read();\r
46                                 if(c>=0) {\r
47                                         aa[i]=(byte)c;\r
48                                 }\r
49                         }\r
50                         System.out.println(new String(aa));\r
51                         \r
52                         bsis.reset();\r
53 \r
54                         byte bb[] = new byte[400];\r
55                         for(int i=0;i<bb.length;++i) {\r
56                                 c = bsis.read();\r
57                                 if(c>=0) {\r
58                                         bb[i]=(byte)c;\r
59                                 }\r
60                         }\r
61                         System.out.println(new String(bb));\r
62 \r
63                 } finally {\r
64                         bsis.close();\r
65                         fis.close();\r
66                 }\r
67         }\r
68         \r
69         @Test\r
70         public void testByteArray() throws Exception {\r
71                 FileInputStream fis = new FileInputStream("test/CBUSevent.xml");\r
72                 BufferedServletInputStream bsis = new BufferedServletInputStream(fis);\r
73                 try {\r
74                         bsis.mark(0);\r
75                         byte aa[] = new byte[260];\r
76                         bsis.read(aa);\r
77                         System.out.println(new String(aa));\r
78                         \r
79                         bsis.reset();\r
80 \r
81                         byte bb[] = new byte[400];\r
82                         bsis.read(bb);\r
83                         System.out.println(new String(bb));\r
84 \r
85                 } finally {\r
86                         bsis.close();\r
87                         fis.close();\r
88                 }\r
89         }\r
90 \r
91         @Test\r
92         public void testDoubleRead() throws Exception {\r
93                 FileInputStream fis = new FileInputStream("test/CBUSevent.xml");\r
94                 BufferedServletInputStream bsis = new BufferedServletInputStream(fis);\r
95                 try {\r
96                         bsis.mark(0);\r
97                         byte aa[] = new byte[260];\r
98                         bsis.read(aa);\r
99                         System.out.println(new String(aa));\r
100                         \r
101                         bsis.reset();\r
102 \r
103                         byte bb[] = new byte[400];\r
104                         bsis.read(bb);\r
105                         System.out.println(new String(bb));\r
106 \r
107                 } finally {\r
108                         bsis.close();\r
109                         fis.close();\r
110                 }\r
111         }\r
112 \r
113         @Test\r
114         public void testByteArray2() throws Exception {\r
115                 FileInputStream fis = new FileInputStream("test/CBUSevent.xml");\r
116                 try {\r
117                         BufferedServletInputStream bsis = new BufferedServletInputStream(fis);\r
118                         byte[] content = null;\r
119                         byte aa[] = new byte[500];\r
120                         for(int i=0;i<2000;++i) {\r
121                                 bsis.mark(0);\r
122                                 bsis.read(aa,0,260);\r
123                                 if(i==0)System.out.println(new String(aa));\r
124                                 \r
125                                 bsis.reset();\r
126         \r
127                                 bsis.read(aa,0,aa.length);\r
128                                 if(i==0) {\r
129                                         System.out.println(new String(aa));\r
130                                         content = aa;\r
131                                         aa = new byte[400];\r
132                                 }\r
133                                 bsis = new BufferedServletInputStream(new ByteArrayInputStream(content));\r
134                                 \r
135                         }\r
136                         \r
137                         System.out.println(new String(aa));\r
138 \r
139                 } finally {\r
140                         fis.close();\r
141                 }\r
142         }\r
143 \r
144         // "Bug" 4/22/2013 \r
145         // Some XML code expects Buffered InputStream can never return 0...  This isn't actually true, but we'll accommodate as far\r
146         // as we can. \r
147         // Here, we make sure we set and read the Buffered data, making sure the buffer is empty on the last test...\r
148         @Test\r
149         public void issue04_22_2013() throws IOException {\r
150                 String testString = "We want to read in and get out with a Buffered Stream seamlessly.";\r
151                 ByteArrayInputStream bais = new ByteArrayInputStream(testString.getBytes());\r
152                 BufferedServletInputStream bsis = new BufferedServletInputStream(bais);\r
153                         try {\r
154                         bsis.mark(0);\r
155                         byte aa[] = new byte[testString.length()];  // 65 count... important for our test (divisible by 5);\r
156 \r
157                         int read;\r
158                         for(int i=0;i<aa.length;i+=5) {\r
159                                 read = bsis.read(aa, i, 5);\r
160                                 assertEquals(5,read);\r
161                         }\r
162                         System.out.println(new String(aa));\r
163                         \r
164                         bsis.reset();\r
165 \r
166                         byte bb[] = new byte[aa.length];\r
167                         read = 0;\r
168                         for(int i=0;read>=0;i+=read) {\r
169                                 read = bsis.read(bb,i,5);\r
170                                 switch(i) {\r
171                                         case 65:\r
172                                                 assertEquals(read,-1);\r
173                                                 break;\r
174                                         default:\r
175                                                 assertEquals(read,5);\r
176                                 }\r
177                         }\r
178                         System.out.println(new String(bb));\r
179                         assertEquals(testString,new String(aa));\r
180                         assertEquals(testString,new String(bb));\r
181 \r
182                 } finally {\r
183                         bsis.close();\r
184                         bais.close();\r
185                 }\r
186                 \r
187         }\r
188         \r
189 \r
190 }\r