5f299bbb901a4b4ee5a26e30db500fa6dc13ea17
[aaf/cadi.git] / core / src / test / java / com / att / 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 com.att.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 \r
33 import com.att.cadi.BufferedServletInputStream;\r
34 \r
35 public class JU_BufferedServletInputStream {\r
36 \r
37         @Test\r
38         public void testByteRead() throws Exception {\r
39                 FileInputStream fis = new FileInputStream("test/CBUSevent.xml");\r
40                 BufferedServletInputStream bsis = new BufferedServletInputStream(fis);\r
41                         try {\r
42                         bsis.mark(0);\r
43                         int c;\r
44                         byte aa[] = new byte[260];\r
45                         for(int i=0;i<aa.length;++i) {\r
46                                 c = bsis.read();\r
47                                 if(c>=0) {\r
48                                         aa[i]=(byte)c;\r
49                                 }\r
50                         }\r
51                         System.out.println(new String(aa));\r
52                         \r
53                         bsis.reset();\r
54 \r
55                         byte bb[] = new byte[400];\r
56                         for(int i=0;i<bb.length;++i) {\r
57                                 c = bsis.read();\r
58                                 if(c>=0) {\r
59                                         bb[i]=(byte)c;\r
60                                 }\r
61                         }\r
62                         System.out.println(new String(bb));\r
63 \r
64                 } finally {\r
65                         bsis.close();\r
66                         fis.close();\r
67                 }\r
68         }\r
69         \r
70         @Test\r
71         public void testByteArray() throws Exception {\r
72                 FileInputStream fis = new FileInputStream("test/CBUSevent.xml");\r
73                 BufferedServletInputStream bsis = new BufferedServletInputStream(fis);\r
74                 try {\r
75                         bsis.mark(0);\r
76                         byte aa[] = new byte[260];\r
77                         bsis.read(aa);\r
78                         System.out.println(new String(aa));\r
79                         \r
80                         bsis.reset();\r
81 \r
82                         byte bb[] = new byte[400];\r
83                         bsis.read(bb);\r
84                         System.out.println(new String(bb));\r
85 \r
86                 } finally {\r
87                         bsis.close();\r
88                         fis.close();\r
89                 }\r
90         }\r
91 \r
92         @Test\r
93         public void testDoubleRead() throws Exception {\r
94                 FileInputStream fis = new FileInputStream("test/CBUSevent.xml");\r
95                 BufferedServletInputStream bsis = new BufferedServletInputStream(fis);\r
96                 try {\r
97                         bsis.mark(0);\r
98                         byte aa[] = new byte[260];\r
99                         bsis.read(aa);\r
100                         System.out.println(new String(aa));\r
101                         \r
102                         bsis.reset();\r
103 \r
104                         byte bb[] = new byte[400];\r
105                         bsis.read(bb);\r
106                         System.out.println(new String(bb));\r
107 \r
108                 } finally {\r
109                         bsis.close();\r
110                         fis.close();\r
111                 }\r
112         }\r
113 \r
114         @Test\r
115         public void testByteArray2() throws Exception {\r
116                 FileInputStream fis = new FileInputStream("test/CBUSevent.xml");\r
117                 try {\r
118                         BufferedServletInputStream bsis = new BufferedServletInputStream(fis);\r
119                         byte[] content = null;\r
120                         byte aa[] = new byte[500];\r
121                         for(int i=0;i<2000;++i) {\r
122                                 bsis.mark(0);\r
123                                 bsis.read(aa,0,260);\r
124                                 if(i==0)System.out.println(new String(aa));\r
125                                 \r
126                                 bsis.reset();\r
127         \r
128                                 bsis.read(aa,0,aa.length);\r
129                                 if(i==0) {\r
130                                         System.out.println(new String(aa));\r
131                                         content = aa;\r
132                                         aa = new byte[400];\r
133                                 }\r
134                                 bsis = new BufferedServletInputStream(new ByteArrayInputStream(content));\r
135                                 \r
136                         }\r
137                         \r
138                         System.out.println(new String(aa));\r
139 \r
140                 } finally {\r
141                         fis.close();\r
142                 }\r
143         }\r
144 \r
145         // "Bug" 4/22/2013 \r
146         // Some XML code expects Buffered InputStream can never return 0...  This isn't actually true, but we'll accommodate as far\r
147         // as we can. \r
148         // Here, we make sure we set and read the Buffered data, making sure the buffer is empty on the last test...\r
149         @Test\r
150         public void issue04_22_2013() throws IOException {\r
151                 String testString = "We want to read in and get out with a Buffered Stream seamlessly.";\r
152                 ByteArrayInputStream bais = new ByteArrayInputStream(testString.getBytes());\r
153                 BufferedServletInputStream bsis = new BufferedServletInputStream(bais);\r
154                         try {\r
155                         bsis.mark(0);\r
156                         byte aa[] = new byte[testString.length()];  // 65 count... important for our test (divisible by 5);\r
157 \r
158                         int read;\r
159                         for(int i=0;i<aa.length;i+=5) {\r
160                                 read = bsis.read(aa, i, 5);\r
161                                 assertEquals(5,read);\r
162                         }\r
163                         System.out.println(new String(aa));\r
164                         \r
165                         bsis.reset();\r
166 \r
167                         byte bb[] = new byte[aa.length];\r
168                         read = 0;\r
169                         for(int i=0;read>=0;i+=read) {\r
170                                 read = bsis.read(bb,i,5);\r
171                                 switch(i) {\r
172                                         case 65:\r
173                                                 assertEquals(read,-1);\r
174                                                 break;\r
175                                         default:\r
176                                                 assertEquals(read,5);\r
177                                 }\r
178                         }\r
179                         System.out.println(new String(bb));\r
180                         assertEquals(testString,new String(aa));\r
181                         assertEquals(testString,new String(bb));\r
182 \r
183                 } finally {\r
184                         bsis.close();\r
185                         bais.close();\r
186                 }\r
187                 \r
188         }\r
189         \r
190 \r
191 }\r