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