Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/http/impl/TestHttpDataReceiverAndTransmitter.java


1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/test/org/apache/http/impl/TestHttpDataReceiverAndTransmitter.java $
3    * $Revision: 410884 $
4    * $Date: 2006-06-01 18:35:03 +0200 (Thu, 01 Jun 2006) $
5    * ====================================================================
6    *
7    *  Copyright 2002-2004 The Apache Software Foundation
8    *
9    *  Licensed under the Apache License, Version 2.0 (the "License");
10   *  you may not use this file except in compliance with the License.
11   *  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing, software
16   *  distributed under the License is distributed on an "AS IS" BASIS,
17   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   *  See the License for the specific language governing permissions and
19   *  limitations under the License.
20   * ====================================================================
21   *
22   * This software consists of voluntary contributions made by many
23   * individuals on behalf of the Apache Software Foundation.  For more
24   * information on the Apache Software Foundation, please see
25   * <http://www.apache.org/>.
26   *
27   */
28  
29  package org.apache.http.impl;
30  
31  import java.io.ByteArrayInputStream;
32  import java.io.ByteArrayOutputStream;
33  import java.io.IOException;
34  import java.io.InputStream;
35  
36  import junit.framework.Test;
37  import junit.framework.TestCase;
38  import junit.framework.TestSuite;
39  
40  import org.apache.http.impl.DefaultHttpParams;
41  import org.apache.http.io.CharArrayBuffer;
42  import org.apache.http.mockup.HttpDataReceiverMockup;
43  import org.apache.http.mockup.HttpDataTransmitterMockup;
44  import org.apache.http.params.HttpConnectionParams;
45  import org.apache.http.params.HttpParams;
46  import org.apache.http.params.HttpProtocolParams;
47  import org.apache.http.protocol.HTTP;
48  
49  public class TestHttpDataReceiverAndTransmitter extends TestCase {
50  
51      public TestHttpDataReceiverAndTransmitter(String testName) {
52          super(testName);
53      }
54  
55      // ------------------------------------------------------- TestCase Methods
56  
57      public static Test suite() {
58          return new TestSuite(TestHttpDataReceiverAndTransmitter.class);
59      }
60  
61      // ------------------------------------------------------------------- Main
62      public static void main(String args[]) {
63          String[] testCaseName = { TestHttpDataReceiverAndTransmitter.class.getName() };
64          junit.textui.TestRunner.main(testCaseName);
65      }
66  
67      public void testInit() throws Exception {
68          ByteArrayOutputStream out = new ByteArrayOutputStream();
69          new HttpDataTransmitterMockup(out); 
70          try {
71              new HttpDataTransmitterMockup(null); 
72              fail("IllegalArgumentException should have been thrown");
73          } catch (IllegalArgumentException ex) {
74              //expected
75          }
76          ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
77          new HttpDataReceiverMockup(in, 10); 
78          try {
79              new HttpDataReceiverMockup(in, -10); 
80              fail("IllegalArgumentException should have been thrown");
81          } catch (IllegalArgumentException ex) {
82              //expected
83          }
84          try {
85              new HttpDataTransmitterMockup(out, -10); 
86              fail("IllegalArgumentException should have been thrown");
87          } catch (IllegalArgumentException ex) {
88              //expected
89          }
90          try {
91              new HttpDataReceiverMockup((InputStream)null, 1024); 
92              fail("IllegalArgumentException should have been thrown");
93          } catch (IllegalArgumentException ex) {
94              //expected
95          }
96      }
97      
98      public void testBasicReadWriteLine() throws Exception {
99          
100         String[] teststrs = new String[5];
101         teststrs[0] = "Hello";
102         teststrs[1] = "This string should be much longer than the size of the output buffer " +
103                 "which is only 16 bytes for this test";
104         StringBuffer buffer = new StringBuffer();
105         for (int i = 0; i < 15; i++) {
106             buffer.append("123456789 ");
107         }
108         buffer.append("and stuff like that");
109         teststrs[2] = buffer.toString();
110         teststrs[3] = "";
111         teststrs[4] = "And goodbye";
112         
113         CharArrayBuffer chbuffer = new CharArrayBuffer(16); 
114         HttpDataTransmitterMockup transmitter = new HttpDataTransmitterMockup(); 
115         for (int i = 0; i < teststrs.length; i++) {
116             chbuffer.clear();
117             chbuffer.append(teststrs[i]);
118             transmitter.writeLine(chbuffer);
119         }
120         //these write operations should have no effect
121         transmitter.writeLine((String)null);
122         transmitter.writeLine((CharArrayBuffer)null);
123         transmitter.flush();
124         
125         HttpDataReceiverMockup receiver = new HttpDataReceiverMockup(
126             transmitter.getData());
127 
128         for (int i = 0; i < teststrs.length; i++) {
129             assertEquals(teststrs[i], receiver.readLine());
130         }
131         assertNull(receiver.readLine());
132         assertNull(receiver.readLine());
133     }
134 
135     public void testComplexReadWriteLine() throws Exception {
136         HttpDataTransmitterMockup transmitter = new HttpDataTransmitterMockup(); 
137         transmitter.write(new byte[] {'a', '\n'});
138         transmitter.write(new byte[] {'\r', '\n'});
139         transmitter.write(new byte[] {'\r', '\r', '\n'});
140         transmitter.write(new byte[] {'\n'});
141         //these write operations should have no effect
142         transmitter.write(null);
143         transmitter.write(null, 0, 12);
144         
145         transmitter.flush();
146 
147         StringBuffer buffer = new StringBuffer();
148         for (int i = 0; i < 14; i++) {
149             buffer.append("a");
150         }
151         String s1 = buffer.toString();
152         buffer.append("\r\n");
153         transmitter.write(buffer.toString().getBytes("US-ASCII"));
154         transmitter.flush();
155 
156         buffer.setLength(0);
157         for (int i = 0; i < 15; i++) {
158             buffer.append("a");
159         }
160         String s2 = buffer.toString();
161         buffer.append("\r\n");
162         transmitter.write(buffer.toString().getBytes("US-ASCII"));
163         transmitter.flush();
164 
165         buffer.setLength(0);
166         for (int i = 0; i < 16; i++) {
167             buffer.append("a");
168         }
169         String s3 = buffer.toString();
170         buffer.append("\r\n");
171         transmitter.write(buffer.toString().getBytes("US-ASCII"));
172         transmitter.flush();
173 
174         transmitter.write(new byte[] {'a'});
175         transmitter.flush();
176         
177         HttpDataReceiverMockup receiver = new HttpDataReceiverMockup(
178             transmitter.getData());
179 
180         assertEquals("a", receiver.readLine());
181         assertEquals("", receiver.readLine());
182         assertEquals("\r", receiver.readLine());
183         assertEquals("", receiver.readLine());
184         assertEquals(s1, receiver.readLine());
185         assertEquals(s2, receiver.readLine());
186         assertEquals(s3, receiver.readLine());
187         assertEquals("a", receiver.readLine());
188         assertNull(receiver.readLine());
189         assertNull(receiver.readLine());
190     }
191     
192     public void testBasicReadWriteLineLargeBuffer() throws Exception {
193         
194         String[] teststrs = new String[5];
195         teststrs[0] = "Hello";
196         teststrs[1] = "This string should be much longer than the size of the output buffer " +
197                 "which is only 16 bytes for this test";
198         StringBuffer buffer = new StringBuffer();
199         for (int i = 0; i < 15; i++) {
200             buffer.append("123456789 ");
201         }
202         buffer.append("and stuff like that");
203         teststrs[2] = buffer.toString();
204         teststrs[3] = "";
205         teststrs[4] = "And goodbye";
206         
207         CharArrayBuffer chbuffer = new CharArrayBuffer(16); 
208         HttpDataTransmitterMockup transmitter = new HttpDataTransmitterMockup(); 
209         for (int i = 0; i < teststrs.length; i++) {
210             chbuffer.clear();
211             chbuffer.append(teststrs[i]);
212             transmitter.writeLine(chbuffer);
213         }
214         //these write operations should have no effect
215         transmitter.writeLine((String)null);
216         transmitter.writeLine((CharArrayBuffer)null);
217         transmitter.flush();
218         
219         HttpDataReceiverMockup receiver = new HttpDataReceiverMockup(
220             transmitter.getData(), 1024);
221 
222         for (int i = 0; i < teststrs.length; i++) {
223             assertEquals(teststrs[i], receiver.readLine());
224         }
225         assertNull(receiver.readLine());
226         assertNull(receiver.readLine());
227     }
228 
229     public void testReadWriteBytes() throws Exception {
230         // make the buffer larger than that of transmitter
231         byte[] out = new byte[40];
232         for (int i = 0; i < out.length; i++) {
233             out[i] = (byte)('0' + i);
234         }
235         HttpDataTransmitterMockup transmitter = new HttpDataTransmitterMockup();
236         int off = 0;
237         int remaining = out.length;
238         while (remaining > 0) {
239             int chunk = 10;
240             if (chunk > remaining) {
241                 chunk = remaining;
242             }
243             transmitter.write(out, off, chunk);
244             off += chunk;
245             remaining -= chunk;
246         }
247         transmitter.flush();
248 
249         byte[] tmp = transmitter.getData();
250         assertEquals(out.length, tmp.length);
251         for (int i = 0; i < out.length; i++) {
252             assertEquals(out[i], tmp[i]);
253         }
254         
255         HttpDataReceiverMockup receiver = new HttpDataReceiverMockup(tmp);
256 
257         // these read operations will have no effect
258         assertEquals(0, receiver.read(null, 0, 10));
259         assertEquals(0, receiver.read(null));        
260         
261         byte[] in = new byte[40];
262         off = 0;
263         remaining = in.length;
264         while (remaining > 0) {
265             int chunk = 10;
266             if (chunk > remaining) {
267                 chunk = remaining;
268             }
269             int l = receiver.read(in, off, chunk);
270             if (l == -1) {
271                 break;
272             }
273             off += l;
274             remaining -= l;
275         }
276         for (int i = 0; i < out.length; i++) {
277             assertEquals(out[i], in[i]);
278         }
279         assertEquals(-1, receiver.read(tmp));
280         assertEquals(-1, receiver.read(tmp));
281     }
282     
283     public void testReadWriteByte() throws Exception {
284         // make the buffer larger than that of transmitter
285         byte[] out = new byte[40];
286         for (int i = 0; i < out.length; i++) {
287             out[i] = (byte)(120 + i);
288         }
289         HttpDataTransmitterMockup transmitter = new HttpDataTransmitterMockup();
290         for (int i = 0; i < out.length; i++) {
291             transmitter.write(out[i]);
292         }
293         transmitter.flush();
294 
295         byte[] tmp = transmitter.getData();
296         assertEquals(out.length, tmp.length);
297         for (int i = 0; i < out.length; i++) {
298             assertEquals(out[i], tmp[i]);
299         }
300         
301         HttpDataReceiverMockup receiver = new HttpDataReceiverMockup(tmp);
302         byte[] in = new byte[40];
303         for (int i = 0; i < in.length; i++) {
304             in[i] = (byte)receiver.read();
305         }
306         for (int i = 0; i < out.length; i++) {
307             assertEquals(out[i], in[i]);
308         }
309         assertEquals(-1, receiver.read());
310         assertEquals(-1, receiver.read());
311     }
312 
313     public void testLineLimit() throws Exception {
314         HttpParams params = new DefaultHttpParams();
315         String s = "a very looooooooooooooooooooooooooooooooooooooong line\r\n     ";
316         byte[] tmp = s.getBytes("US-ASCII"); 
317         HttpDataReceiverMockup receiver1 = new HttpDataReceiverMockup(tmp, 5);
318         // no limit
319         params.setIntParameter(HttpConnectionParams.MAX_LINE_LENGTH, 0);
320         receiver1.reset(params);
321         assertNotNull(receiver1.readLine());
322         
323         HttpDataReceiverMockup receiver2 = new HttpDataReceiverMockup(tmp, 5);
324         // 15 char limit
325         params.setIntParameter(HttpConnectionParams.MAX_LINE_LENGTH, 15);
326         receiver2.reset(params);
327         try {
328             receiver2.readLine();
329             fail("IOException should have been thrown");
330         } catch (IOException ex) {
331             // expected
332         }
333     }
334 
335     static final int SWISS_GERMAN_HELLO [] = {
336         0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
337     };
338         
339     static final int RUSSIAN_HELLO [] = {
340         0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438, 
341         0x432, 0x435, 0x442 
342     }; 
343     
344     private static String constructString(int [] unicodeChars) {
345         StringBuffer buffer = new StringBuffer();
346         if (unicodeChars != null) {
347             for (int i = 0; i < unicodeChars.length; i++) {
348                 buffer.append((char)unicodeChars[i]); 
349             }
350         }
351         return buffer.toString();
352     }
353 
354     public void testMultibyteCodedReadWriteLine() throws Exception {
355         String s1 = constructString(SWISS_GERMAN_HELLO);
356         String s2 = constructString(RUSSIAN_HELLO);
357         String s3 = "Like hello and stuff";
358         
359         HttpParams params = new DefaultHttpParams(null);
360         HttpProtocolParams.setHttpElementCharset(params, "UTF-8");
361         
362         HttpDataTransmitterMockup transmitter = new HttpDataTransmitterMockup();
363         transmitter.reset(params);
364 
365         CharArrayBuffer chbuffer = new CharArrayBuffer(16); 
366         for (int i = 0; i < 10; i++) {
367             chbuffer.clear();
368             chbuffer.append(s1);
369             transmitter.writeLine(chbuffer);
370             chbuffer.clear();
371             chbuffer.append(s2);
372             transmitter.writeLine(chbuffer);
373             chbuffer.clear();
374             chbuffer.append(s3);
375             transmitter.writeLine(chbuffer);
376         }
377         transmitter.flush();
378         
379         HttpDataReceiverMockup receiver = new HttpDataReceiverMockup(
380             transmitter.getData());
381         receiver.reset(params);
382 
383         for (int i = 0; i < 10; i++) {
384             assertEquals(s1, receiver.readLine());
385             assertEquals(s2, receiver.readLine());
386             assertEquals(s3, receiver.readLine());
387         }
388         assertNull(receiver.readLine());
389         assertNull(receiver.readLine());
390     }
391 
392     public void testNonAsciiReadWriteLine() throws Exception {
393         String s1 = constructString(SWISS_GERMAN_HELLO);
394         
395         HttpParams params = new DefaultHttpParams(null);
396         HttpProtocolParams.setHttpElementCharset(params, HTTP.ISO_8859_1);
397         
398         HttpDataTransmitterMockup transmitter = new HttpDataTransmitterMockup();
399         transmitter.reset(params);
400 
401         CharArrayBuffer chbuffer = new CharArrayBuffer(16); 
402         for (int i = 0; i < 10; i++) {
403             chbuffer.clear();
404             chbuffer.append(s1);
405             transmitter.writeLine(chbuffer);
406         }
407         transmitter.flush();
408         
409         HttpDataReceiverMockup receiver = new HttpDataReceiverMockup(
410                 transmitter.getData());
411         HttpProtocolParams.setHttpElementCharset(params, HTTP.ISO_8859_1);
412         receiver.reset(params);
413 
414         for (int i = 0; i < 10; i++) {
415             assertEquals(s1, receiver.readLine());
416         }
417         assertNull(receiver.readLine());
418         assertNull(receiver.readLine());
419     }
420 
421     public void testInvalidCharArrayBuffer() throws Exception {
422         HttpDataReceiverMockup receiver = new HttpDataReceiverMockup(new byte[] {});
423         try {
424             receiver.readLine(null); 
425             fail("IllegalArgumentException should have been thrown");
426         } catch (IllegalArgumentException ex) {
427             //expected
428         }
429     }
430     
431 }
432