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
18package tests.api.java.io;
19
20import dalvik.annotation.TestTargets;
21import dalvik.annotation.TestLevel;
22import dalvik.annotation.TestTargetNew;
23import dalvik.annotation.TestTargetClass;
24
25import java.io.IOException;
26import java.io.PipedReader;
27import java.io.PipedWriter;
28
29@TestTargetClass(PipedReader.class)
30public class PipedReaderTest extends junit.framework.TestCase {
31
32    static class PWriter implements Runnable {
33        public PipedWriter pw;
34
35        public PWriter(PipedReader reader) {
36            try {
37                pw = new PipedWriter(reader);
38            } catch (Exception e) {
39                System.out.println("Couldn't create writer");
40            }
41        }
42
43        public PWriter() {
44            pw = new PipedWriter();
45        }
46
47        public void run() {
48            try {
49                char[] c = new char[11];
50                "Hello World".getChars(0, 11, c, 0);
51                pw.write(c);
52                Thread.sleep(10000);
53            } catch (InterruptedException e) {
54            } catch (Exception e) {
55                System.out.println("Exception occurred: " + e.toString());
56            }
57        }
58    }
59
60    static class PWriter2 implements Runnable {
61        PipedWriter pw;
62
63        public boolean keepRunning = true;
64
65        public void run() {
66            try {
67                pw.write('H');
68                pw.close();
69                while (keepRunning) {
70                    Thread.sleep(1000);
71                }
72            } catch (Exception e) {
73                e.printStackTrace(System.out);
74                System.out.println("Error while running the writer thread.");
75            }
76        }
77
78        public PWriter2(PipedWriter writer) {
79            pw = writer;
80        }
81    }
82
83    PipedReader preader;
84
85    PWriter pwriter;
86
87    Thread t;
88
89    /**
90     * @tests java.io.PipedReader#PipedReader()
91     */
92    @TestTargetNew(
93        level = TestLevel.COMPLETE,
94        notes = "",
95        method = "PipedReader",
96        args = {}
97    )
98    public void test_Constructor() {
99        preader = new PipedReader();
100        assertNotNull(preader);
101        try {
102            preader.close();
103        } catch (IOException e) {
104            fail("Unexpeceted IOException");
105        }
106    }
107
108    /**
109     * @tests java.io.PipedReader#PipedReader(java.io.PipedWriter)
110     */
111    @TestTargetNew(
112        level = TestLevel.COMPLETE,
113        notes = "",
114        method = "PipedReader",
115        args = {java.io.PipedWriter.class}
116    )
117    public void test_ConstructorLjava_io_PipedWriter() throws IOException {
118        // Test for method java.io.PipedReader(java.io.PipedWriter)
119        try {
120            preader = new PipedReader(new PipedWriter());
121        } catch (Exception e) {
122            fail("Test 1: Constructor failed: " + e.getMessage());
123        }
124        preader.close();
125
126        PipedWriter pw = new PipedWriter(new PipedReader());
127        try {
128            preader = new PipedReader(pw);
129            fail("Test 2: IOException expected because the writer is already connected.");
130        } catch (IOException e) {
131            // Expected.
132        }
133    }
134
135    /**
136     * @tests java.io.PipedReader#close()
137     */
138    @TestTargetNew(
139        level = TestLevel.COMPLETE,
140        notes = "No IOException checking because it is never thrown in the source code.",
141        method = "close",
142        args = {}
143    )
144    public void test_close() throws Exception {
145        // Test for method void java.io.PipedReader.close()
146        char[] c = null;
147        preader = new PipedReader();
148        t = new Thread(new PWriter(preader), "");
149        t.start();
150        Thread.sleep(500); // Allow writer to start
151        c = new char[11];
152        preader.read(c, 0, 5);
153        preader.close();
154
155        try {
156            preader.read(c, 0, 5);
157            fail("IOException expected because the reader is closed.");
158        } catch (IOException e) {
159            // Expected.
160        }
161    }
162
163    /**
164     * @tests java.io.PipedReader#connect(java.io.PipedWriter)
165     */
166    @TestTargetNew(
167        level = TestLevel.COMPLETE,
168        notes = "",
169        method = "connect",
170        args = {java.io.PipedWriter.class}
171    )
172    public void test_connectLjava_io_PipedWriter() throws Exception {
173        // Test for method void java.io.PipedReader.connect(java.io.PipedWriter)
174        char[] c = null;
175
176        preader = new PipedReader();
177        t = new Thread(pwriter = new PWriter(), "");
178        preader.connect(pwriter.pw);
179        t.start();
180        Thread.sleep(500); // Allow writer to start
181        c = new char[11];
182        preader.read(c, 0, 11);
183
184        assertEquals("Test 1: Wrong characters read. ", "Hello World", new String(c));
185        try {
186            preader.connect(new PipedWriter());
187            fail("Test 2: IOException expected because the reader is already connected.");
188        } catch (IOException e) {
189            // Expected.
190        }
191    }
192
193    /**
194     * @tests java.io.PipedReader#read()
195     */
196    @TestTargetNew(
197        level = TestLevel.PARTIAL_COMPLETE,
198        notes = "",
199        method = "read",
200        args = {}
201    )
202    public void test_read_1() throws Exception {
203        // Test for method int java.io.PipedReader.read()
204        char[] c = null;
205        preader = new PipedReader();
206        t = new Thread(new PWriter(preader), "");
207        t.start();
208        Thread.sleep(500); // Allow writer to start
209        c = new char[11];
210        for (int i = 0; i < c.length; i++) {
211            c[i] = (char) preader.read();
212        }
213        assertEquals("Test 1: Wrong characters read. ", "Hello World", new String(c));
214
215        try {
216            preader.read();
217            fail("Test 2: IOException expected since the thread that has " +
218                 "written to the pipe is no longer alive.");
219        } catch (IOException e) {
220            // Expected.
221        }
222
223        preader.close();
224        try {
225            preader.read();
226            fail("Test 3: IOException expected.");
227        } catch (IOException e) {
228            // Expected.
229        }
230    }
231
232    /**
233     * @tests java.io.PipedReader#read()
234     */
235    @TestTargetNew(
236        level = TestLevel.PARTIAL_COMPLETE,
237        notes = "Checks that read() returns -1 if the PipedWriter connectedto this PipedReader is closed.",
238        method = "read",
239        args = {}
240    )
241    public void test_read_2() throws Exception {
242        Thread writerThread;
243        PipedWriter pw;
244        PWriter2 pwriter;
245
246        preader = new PipedReader();
247        pw = new PipedWriter(preader);
248
249        writerThread = new Thread(pwriter = new PWriter2(pw), "PWriter2");
250        writerThread.start();
251        Thread.sleep(500); // Allow writer to start
252
253        preader.read();
254        assertEquals("Test 1: No more data indication expected. ", -1, preader.read());
255        pwriter.keepRunning = false;
256    }
257
258    /**
259     * @tests java.io.PipedReader#read(char[], int, int)
260     */
261    @TestTargetNew(
262        level = TestLevel.PARTIAL_COMPLETE,
263        notes = "IOException checking missed.",
264        method = "read",
265        args = {char[].class, int.class, int.class}
266    )
267    public void test_read$CII_1() throws Exception {
268        // Test for method int java.io.PipedReader.read(char [], int, int)
269        char[] c = null;
270        preader = new PipedReader();
271        t = new Thread(new PWriter(preader), "");
272        t.start();
273        Thread.sleep(500); // Allow writer to start
274        c = new char[11];
275        int n = 0;
276        int x = n;
277        while (x < 11) {
278            n = preader.read(c, x, 11 - x);
279            x = x + n;
280        }
281        assertEquals("Test 1: Wrong characters read. ", "Hello World", new String(c));
282
283        preader.close();
284        try {
285            preader.read(c, 8, 7);
286            fail("Test 2: IOException expected.");
287        } catch (IOException e) {
288            // Expected.
289        }
290    }
291
292    /**
293     * @tests java.io.PipedReader#read(char[], int, int)
294     */
295    @TestTargetNew(
296        level = TestLevel.PARTIAL_COMPLETE,
297        notes = "",
298        method = "read",
299        args = {char[].class, int.class, int.class}
300    )
301    public void test_read$CII_Exception() throws IOException{
302        PipedWriter pw = new PipedWriter();
303        PipedReader obj = new PipedReader(pw);
304        try {
305            obj.read(new char[10], 0, -1);
306            fail("IndexOutOfBoundsException expected.");
307        } catch (IndexOutOfBoundsException e) {
308            // Expected.
309        }
310        try {
311            obj.read(new char[10], -1, 1);
312            fail("IndexOutOfBoundsException expected.");
313        } catch (IndexOutOfBoundsException e) {
314            // Expected.
315        }
316        try {
317            obj.read(new char[10], 2, 9);
318            fail("IndexOutOfBoundsException expected.");
319        } catch (IndexOutOfBoundsException e) {
320            // Expected.
321        }
322        try {
323            obj.read(null, 0, 1);
324            fail("NullPointerException expected.");
325        } catch (NullPointerException e) {
326            // Expected.
327        }
328    }
329
330    /**
331     * @tests java.io.PipedReader#read()
332     */
333    @TestTargetNew(
334        level = TestLevel.PARTIAL_COMPLETE,
335        notes = "Checks that read() returns -1 if the PipedWriter connectedto this PipedReader is closed.",
336        method = "read",
337        args = {}
338    )
339    public void test_read$CII_2() throws Exception {
340        Thread writerThread;
341        PipedWriter pw;
342        PWriter2 pwriter;
343        char[] c = new char[1];
344
345        preader = new PipedReader();
346        pw = new PipedWriter(preader);
347
348        writerThread = new Thread(pwriter = new PWriter2(pw), "PWriter2");
349        writerThread.start();
350        Thread.sleep(500); // Allow writer to start
351
352        preader.read(c, 0, 1);
353        assertEquals("Test 1: No more data indication expected. ",
354                     -1, preader.read(c, 0, 1));
355        pwriter.keepRunning = false;
356    }
357
358    /**
359     * @tests java.io.PipedReader#ready()
360     */
361    @TestTargetNew(
362        level = TestLevel.COMPLETE,
363        notes = "",
364        method = "ready",
365        args = {}
366    )
367    public void test_ready() throws Exception {
368        // Test for method boolean java.io.PipedReader.ready()
369        char[] c = null;
370        preader = new PipedReader();
371
372        try {
373            preader.ready();
374            fail("Test 1: IOException expected.");
375        } catch (IOException e) {
376            // Expected.
377        }
378
379        t = new Thread(new PWriter(preader), "");
380        t.start();
381        Thread.sleep(500); // Allow writer to start
382        assertTrue("Test 2: Reader should be ready", preader.ready());
383        c = new char[11];
384        for (int i = 0; i < c.length; i++)
385            c[i] = (char) preader.read();
386        assertFalse("Test 3: Reader should not be ready after reading all chars",
387                preader.ready());
388
389        preader.close();
390        try {
391            preader.ready();
392            fail("Test 4: IOException expected.");
393        } catch (IOException e) {
394            // Expected.
395        }
396    }
397
398    /**
399     * Tears down the fixture, for example, close a network connection. This
400     * method is called after a test is executed.
401     */
402    protected void tearDown() throws Exception {
403        if (t != null) {
404            t.interrupt();
405        }
406        super.tearDown();
407    }
408}
409