OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / tests / api / java / io / PipedReaderTest.java
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 package tests.api.java.io;
19
20 import java.io.IOException;
21 import java.io.PipedReader;
22 import java.io.PipedWriter;
23
24 import junit.framework.TestCase;
25
26 public class PipedReaderTest extends TestCase {
27
28     static class PWriter implements Runnable {
29         public PipedWriter pw;
30
31         public PWriter(PipedReader reader) {
32             try {
33                 pw = new PipedWriter(reader);
34             } catch (Exception e) {
35                 System.out.println("Couldn't create writer");
36             }
37         }
38
39         public PWriter() {
40             pw = new PipedWriter();
41         }
42
43         public void run() {
44             try {
45                 char[] c = new char[11];
46                 "Hello World".getChars(0, 11, c, 0);
47                 pw.write(c);
48                 Thread.sleep(10000);
49             } catch (InterruptedException e) {
50             } catch (Exception e) {
51                 System.out.println("Exception occurred: " + e.toString());
52             }
53         }
54     }
55
56     PipedReader preader;
57
58     PWriter pwriter;
59
60     Thread t;
61
62     /**
63      * @tests java.io.PipedReader#PipedReader()
64      */
65     public void test_Constructor() {
66         // Used in test
67     }
68
69     /**
70      * @tests java.io.PipedReader#PipedReader(java.io.PipedWriter)
71      */
72     public void test_ConstructorLjava_io_PipedWriter() throws IOException {
73         preader = new PipedReader(new PipedWriter());
74     }
75
76     /**
77      * @tests java.io.PipedReader#PipedReader(java.io.PipedWriter,
78      *        int)
79      * @since 1.6
80      */
81     public void test_Constructor_LPipedWriter_I() throws Exception {
82         // Test for method java.io.PipedReader(java.io.PipedWriter,
83         // int)
84         try {
85             preader = new PipedReader(null, -1);
86             fail("Should throw IllegalArgumentException");
87         } catch (IllegalArgumentException e) {
88             // expected
89         }
90
91         try {
92             preader = new PipedReader(null, 0);
93             fail("Should throw IllegalArgumentException");
94         } catch (IllegalArgumentException e) {
95             // expected
96         }
97     }
98
99     /**
100      * @tests java.io.PipedReader#PipedReader(int)
101      * @since 1.6
102      */
103     public void test_Constructor_I() throws Exception {
104         // Test for method java.io.PipedReader(int)
105         try {
106             preader = new PipedReader(-1);
107             fail("Should throw IllegalArgumentException");
108         } catch (IllegalArgumentException e) {
109             // expected
110         }
111
112         try {
113             preader = new PipedReader(0);
114             fail("Should throw IllegalArgumentException");
115         } catch (IllegalArgumentException e) {
116             // expected
117         }
118     }
119
120     /**
121      * @tests java.io.PipedReader#close()
122      */
123     public void test_close() throws Exception {
124         char[] c = null;
125         preader = new PipedReader();
126         t = new Thread(new PWriter(preader), "");
127         t.start();
128         Thread.sleep(500); // Allow writer to start
129         c = new char[11];
130         preader.read(c, 0, 11);
131         preader.close();
132         assertEquals("Read incorrect chars", "Hello World", new String(c));
133     }
134
135     /**
136      * @tests java.io.PipedReader#connect(java.io.PipedWriter)
137      */
138     public void test_connectLjava_io_PipedWriter() throws Exception {
139         char[] c = null;
140
141         preader = new PipedReader();
142         t = new Thread(pwriter = new PWriter(), "");
143         preader.connect(pwriter.pw);
144         t.start();
145         Thread.sleep(500); // Allow writer to start
146         c = new char[11];
147         preader.read(c, 0, 11);
148
149         assertEquals("Read incorrect chars", "Hello World", new String(c));
150         try {
151             preader.connect(pwriter.pw);
152             fail("Failed to throw exception connecting to pre-connected reader");
153         } catch (IOException e) {
154             // Expected
155         }
156     }
157
158     /**
159      * @tests java.io.PipedReader#read()
160      */
161     public void test_read() throws Exception {
162         char[] c = null;
163         preader = new PipedReader();
164         t = new Thread(new PWriter(preader), "");
165         t.start();
166         Thread.sleep(500); // Allow writer to start
167         c = new char[11];
168         for (int i = 0; i < c.length; i++) {
169             c[i] = (char) preader.read();
170         }
171         assertEquals("Read incorrect chars", "Hello World", new String(c));
172     }
173
174     /**
175      * @tests java.io.PipedReader#read(char[], int, int)
176      */
177     public void test_read$CII() throws Exception {
178         char[] c = null;
179         preader = new PipedReader();
180         t = new Thread(new PWriter(preader), "");
181         t.start();
182         Thread.sleep(500); // Allow writer to start
183         c = new char[11];
184         int n = 0;
185         int x = n;
186         while (x < 11) {
187             n = preader.read(c, x, 11 - x);
188             x = x + n;
189         }
190         assertEquals("Read incorrect chars", "Hello World", new String(c));
191         try {
192             preader.close();
193             preader.read(c, 8, 7);
194             fail("Failed to throw exception reading from closed reader");
195         } catch (IOException e) {
196             // Expected
197         }
198     }
199
200     /**
201      * @tests java.io.PipedReader#read(char[], int, int)
202      */
203     public void test_read$CII_2() throws IOException {
204         // Regression for HARMONY-387
205         PipedWriter pw = new PipedWriter();
206         PipedReader obj = null;
207         try {
208             obj = new PipedReader(pw);
209             obj.read(new char[0], (int) 0, (int) -1);
210             fail("IndexOutOfBoundsException expected");
211         } catch (IndexOutOfBoundsException t) {
212             assertEquals(
213                     "IndexOutOfBoundsException rather than a subclass expected",
214                     IndexOutOfBoundsException.class, t.getClass());
215         }
216     }
217
218     /**
219      * @tests java.io.PipedReader#read(char[], int, int)
220      */
221     public void test_read$CII_3() throws IOException {
222         PipedWriter pw = new PipedWriter();
223         PipedReader obj = null;
224         try {
225             obj = new PipedReader(pw);
226             obj.read(new char[0], (int) -1, (int) 0);
227             fail("IndexOutOfBoundsException expected");
228         } catch (ArrayIndexOutOfBoundsException t) {
229             fail("IndexOutOfBoundsException expected");
230         } catch (IndexOutOfBoundsException t) {
231             // Expected
232         }
233     }
234
235     /**
236      * @tests java.io.PipedReader#read(char[], int, int)
237      */
238     public void test_read$CII_4() throws IOException {
239         PipedWriter pw = new PipedWriter();
240         PipedReader obj = null;
241         try {
242             obj = new PipedReader(pw);
243             obj.read(new char[0], (int) -1, (int) -1);
244             fail("IndexOutOfBoundsException expected");
245         } catch (ArrayIndexOutOfBoundsException t) {
246             fail("IndexOutOfBoundsException expected");
247         } catch (IndexOutOfBoundsException t) {
248             // Expected
249         }
250     }
251
252     /**
253      * @tests java.io.PipedReader#read(char[], int, int)
254      */
255     public void test_read_$CII_IOException() throws IOException {
256         PipedWriter pw = new PipedWriter();
257         PipedReader pr = new PipedReader(pw);
258         char[] buf = null;
259         pr.close();
260         try {
261             pr.read(buf, 0, 10);
262             fail("Should throws IOException");
263         } catch (IOException e) {
264             // expected
265         } finally {
266             pw = null;
267             pr = null;
268         }
269
270         pr = new PipedReader();
271         buf = null;
272         pr.close();
273         try {
274             pr.read(buf, 0, 10);
275             fail("Should throws IOException");
276         } catch (IOException e) {
277             // expected
278         } finally {
279             pr = null;
280         }
281
282         pw = new PipedWriter();
283         pr = new PipedReader(pw);
284         buf = new char[10];
285         pr.close();
286         try {
287             pr.read(buf, -1, 0);
288             fail("Should throws IOException");
289         } catch (IOException e) {
290             // expected
291         } finally {
292             pw = null;
293             pr = null;
294         }
295
296         pw = new PipedWriter();
297         pr = new PipedReader(pw);
298         buf = new char[10];
299         pr.close();
300         try {
301             pr.read(buf, 0, -1);
302             fail("Should throws IOException");
303         } catch (IOException e) {
304             // expected
305         } finally {
306             pw = null;
307             pr = null;
308         }
309
310         pw = new PipedWriter();
311         pr = new PipedReader(pw);
312         buf = new char[10];
313         pr.close();
314         try {
315             pr.read(buf, 1, 10);
316             fail("Should throws IOException");
317         } catch (IOException e) {
318             // expected
319         } finally {
320             pw = null;
321             pr = null;
322         }
323
324         pw = new PipedWriter();
325         pr = new PipedReader(pw);
326         pr.close();
327         try {
328             pr.read(new char[0], -1, -1);
329             fail("should throw IOException");
330         } catch (IOException e) {
331             // expected
332         } finally {
333             pw = null;
334             pr = null;
335         }
336
337         pw = new PipedWriter();
338         pr = new PipedReader(pw);
339         pr.close();
340         try {
341             pr.read(null, 0, 1);
342             fail("should throw IOException");
343         } catch (IOException e) {
344             // expected
345         } finally {
346             pw = null;
347             pr = null;
348         }
349
350         pw = new PipedWriter();
351         pr = new PipedReader(pw);
352         try {
353             pr.read(null, -1, 1);
354             fail("should throw IndexOutOfBoundsException");
355         } catch (NullPointerException expected) { // android-added
356         } catch (IndexOutOfBoundsException e) {
357             // expected
358         } finally {
359             pw = null;
360             pr = null;
361         }
362
363         pw = new PipedWriter();
364         pr = new PipedReader(pw);
365         try {
366             pr.read(null, 0, -1);
367             fail("should throw NullPointerException");
368         } catch (NullPointerException e) {
369             // expected
370         } finally {
371             pw = null;
372             pr = null;
373         }
374
375         pw = new PipedWriter();
376         pr = new PipedReader(pw);
377         try {
378             pr.read(new char[10], 11, 0);
379             fail("should throw IndexOutOfBoundsException");
380         } catch (IndexOutOfBoundsException e) {
381             // expected
382         } finally {
383             pw = null;
384             pr = null;
385         }
386
387         pw = new PipedWriter();
388         pr = new PipedReader(pw);
389         try {
390             pr.read(null, 1, 0);
391             fail("should throw NullPointerException");
392         } catch (NullPointerException e) {
393             // expected
394         } finally {
395             pw = null;
396             pr = null;
397         }
398     }
399
400     /**
401      * @tests java.io.PipedReader#ready()
402      */
403     public void test_ready() throws Exception {
404         char[] c = null;
405         preader = new PipedReader();
406         t = new Thread(new PWriter(preader), "");
407         t.start();
408         Thread.sleep(500); // Allow writer to start
409         assertTrue("Reader should be ready", preader.ready());
410         c = new char[11];
411         for (int i = 0; i < c.length; i++)
412             c[i] = (char) preader.read();
413         assertFalse("Reader should not be ready after reading all chars",
414                 preader.ready());
415     }
416
417     /**
418      * Tears down the fixture, for example, close a network connection. This
419      * method is called after a test is executed.
420      */
421     protected void tearDown() throws Exception {
422         if (t != null) {
423             t.interrupt();
424         }
425         super.tearDown();
426     }
427 }