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 org.apache.harmony.tests.java.io;
19
20import java.io.ByteArrayInputStream;
21import java.io.IOException;
22import java.io.InputStream;
23import java.io.SequenceInputStream;
24import java.io.UnsupportedEncodingException;
25import java.util.Enumeration;
26
27public class SequenceInputStreamTest extends junit.framework.TestCase {
28
29    SequenceInputStream si;
30
31    String s1 = "Hello";
32
33    String s2 = "World";
34
35    /**
36     * java.io.SequenceInputStream#SequenceInputStream(java.io.InputStream,
37     *java.io.InputStream)
38     */
39    public void test_ConstructorLjava_io_InputStreamLjava_io_InputStream() {
40        // Test for method java.io.SequenceInputStream(java.io.InputStream,
41        // java.io.InputStream)
42        // Used in tests
43    }
44
45    /**
46     * SequenceInputStream#SequenceInputStream(java.io.InputStream,
47     *java.io.InputStream)
48     */
49    public void test_Constructor_LInputStreamLInputStream_Null() throws UnsupportedEncodingException {
50        try {
51            si = new SequenceInputStream(null, null);
52            fail("should throw NullPointerException");
53        } catch (NullPointerException e) {
54            //expected
55        }
56
57        //will not throw NullPointerException if the first InputStream is not null
58        InputStream is = new ByteArrayInputStream(s1.getBytes("UTF-8"));
59        si = new SequenceInputStream(is, null);
60    }
61
62    /**
63     * java.io.SequenceInputStream#SequenceInputStream(java.util.Enumeration)
64     */
65    @SuppressWarnings("unchecked")
66    public void test_ConstructorLjava_util_Enumeration() {
67        // Test for method java.io.SequenceInputStream(java.util.Enumeration)
68        class StreamEnumerator implements Enumeration {
69            InputStream streams[] = new InputStream[2];
70
71            int count = 0;
72
73            public StreamEnumerator() throws UnsupportedEncodingException {
74                streams[0] = new ByteArrayInputStream(s1.getBytes("UTF-8"));
75                streams[1] = new ByteArrayInputStream(s2.getBytes("UTF-8"));
76            }
77
78            public boolean hasMoreElements() {
79                return count < streams.length;
80            }
81
82            public Object nextElement() {
83                return streams[count++];
84            }
85        }
86
87        try {
88            si = new SequenceInputStream(new StreamEnumerator());
89            byte buf[] = new byte[s1.length() + s2.length()];
90            si.read(buf, 0, s1.length());
91            si.read(buf, s1.length(), s2.length());
92            assertTrue("Read incorrect bytes: " + new String(buf), new String(
93                    buf, "UTF-8").equals(s1 + s2));
94        } catch (IOException e) {
95            fail("IOException during read test : " + e.getMessage());
96        }
97
98    }
99
100    /**
101     * java.io.SequenceInputStream#available()
102     */
103    public void test_available() {
104        // Test for method int java.io.SequenceInputStream.available()
105        try {
106
107            assertTrue("Returned incorrect number of bytes: " + si.available(),
108                    si.available() == s1.length());
109        } catch (IOException e) {
110            fail("IOException during available test : " + e.getMessage());
111        }
112    }
113
114    /**
115     * java.io.SequenceInputStream#close()
116     */
117    public void test_close() throws IOException {
118        si.close();
119        //will not throw IOException to close a stream which is closed already
120        si.close();
121    }
122
123    /**
124     * java.io.SequenceInputStream#read()
125     */
126    public void test_read() throws IOException {
127        // Test for method int java.io.SequenceInputStream.read()
128        try {
129            si.read();
130            assertTrue("Read incorrect char", (char) si.read() == s1.charAt(1));
131        } catch (IOException e) {
132            fail("IOException during read test: " + e.getMessage());
133        }
134
135        //returns -1 if the stream is closed , do not throw IOException
136        si.close();
137        int result = si.read();
138        assertEquals(-1, result);
139    }
140
141    /**
142     * java.io.SequenceInputStream#read(byte[], int, int)
143     */
144    public void test_read$BII() throws IOException {
145        // Test for method int java.io.SequenceInputStream.read(byte [], int,
146        // int)
147        try {
148            byte buf[] = new byte[s1.length() + s2.length()];
149            si.read(buf, 0, s1.length());
150            si.read(buf, s1.length(), s2.length());
151            assertTrue("Read incorrect bytes: " + new String(buf), new String(
152                    buf, "UTF-8").equals(s1 + s2));
153        } catch (IOException e) {
154            fail("IOException during read test : " + e.getMessage());
155        }
156
157        ByteArrayInputStream bis1 = new ByteArrayInputStream(
158                new byte[] { 1, 2, 3, 4 });
159        ByteArrayInputStream bis2 = new ByteArrayInputStream(
160                new byte[] { 5, 6, 7, 8 });
161        SequenceInputStream sis = new SequenceInputStream(bis1, bis2);
162
163        try {
164            sis.read(null, 0, -1);
165            fail("Expected NullPointerException exception");
166        } catch (NullPointerException e) {
167            // expected
168        }
169
170        //returns -1 if the stream is closed , do not throw IOException
171        byte[] array = new byte[] { 1, 2, 3, 4 };
172        sis.close();
173        int result = sis.read(array, 0, 5);
174        assertEquals(-1, result);
175
176    }
177
178    /**
179     * Sets up the fixture, for example, open a network connection. This method
180     * is called before a test is executed.
181     */
182    protected void setUp() throws UnsupportedEncodingException {
183        si = new SequenceInputStream(new ByteArrayInputStream(s1.getBytes("UTF-8")),
184                new ByteArrayInputStream(s2.getBytes("UTF-8")));
185    }
186
187    /**
188     * Tears down the fixture, for example, close a network connection. This
189     * method is called after a test is executed.
190     */
191    protected void tearDown() {
192    }
193}
194