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.InputStreamReader;
24
25public class ByteArrayInputStreamTest extends junit.framework.TestCase {
26
27    private InputStream is;
28
29    public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\n";
30
31    /**
32     * ByteArrayInputStream#ByteArrayInputStream(byte[])
33     */
34    public void test_Constructor$B() throws IOException {
35        InputStream bis = new ByteArrayInputStream(fileString.getBytes());
36
37        assertTrue("Unable to create ByteArrayInputStream",
38                bis.available() == fileString.length());
39    }
40
41    /**
42     * ByteArrayInputStream#ByteArrayInputStream(byte[], int, int)
43     */
44    public void test_Constructor$BII() throws IOException {
45        byte[] zz = fileString.getBytes();
46        InputStream bis = new ByteArrayInputStream(zz, 0, 100);
47
48        assertEquals("Unable to create ByteArrayInputStream", 100, bis
49                .available());
50
51        // Regression test for Harmony-2405
52        new SubByteArrayInputStream(new byte[] { 1, 2 }, 444, 13);
53        assertEquals(444, SubByteArrayInputStream.pos);
54        assertEquals(444, SubByteArrayInputStream.mark);
55        assertEquals(2, SubByteArrayInputStream.count);
56    }
57
58    static class SubByteArrayInputStream extends ByteArrayInputStream {
59        public static byte[] buf;
60
61        public static int mark, pos, count;
62
63        SubByteArrayInputStream(byte[] buf, int offset, int length)
64                throws IOException {
65            super(buf, offset, length);
66            buf = super.buf;
67            mark = super.mark;
68            pos = super.pos;
69            count = super.count;
70        }
71    }
72
73    /**
74     * ByteArrayInputStream#available()
75     */
76    public void test_available() throws IOException {
77        assertTrue("Returned incorrect number of available bytes", is
78                .available() == fileString.length());
79    }
80
81    /**
82     * ByteArrayInputStream#close()
83     */
84    public void test_close() throws IOException {
85        is.read();
86        is.close();
87        is.read(); // Should be able to read from a closed stream
88    }
89
90    /**
91     * ByteArrayInputStream#mark(int)
92     */
93    public void test_markI() throws IOException {
94        byte[] buf1 = new byte[100];
95        byte[] buf2 = new byte[100];
96        is.skip(3000);
97        is.mark(1000);
98        is.read(buf1, 0, buf1.length);
99        is.reset();
100        is.read(buf2, 0, buf2.length);
101        is.reset();
102        assertTrue("Failed to mark correct position", new String(buf1, 0,
103                buf1.length).equals(new String(buf2, 0, buf2.length)));
104    }
105
106    /**
107     * ByteArrayInputStream#markSupported()
108     */
109    public void test_markSupported() {
110        assertTrue("markSupported returned incorrect value", is.markSupported());
111    }
112
113    /**
114     * ByteArrayInputStream#read()
115     */
116    public void test_read() throws IOException {
117        InputStreamReader isr = new InputStreamReader(is);
118        int c = isr.read();
119        is.reset();
120        assertTrue("read returned incorrect char", c == fileString.charAt(0));
121    }
122
123    /**
124     * ByteArrayInputStream#read(byte[], int, int)
125     */
126    public void test_read$BII() throws IOException {
127        byte[] buf1 = new byte[20];
128        is.skip(50);
129        is.mark(100);
130        is.read(buf1, 0, buf1.length);
131        assertTrue("Failed to read correct data", new String(buf1, 0,
132                buf1.length).equals(fileString.substring(50, 70)));
133    }
134
135    /**
136     * ByteArrayInputStream#reset()
137     */
138    public void test_reset() throws IOException {
139        byte[] buf1 = new byte[10];
140        byte[] buf2 = new byte[10];
141        is.mark(200);
142        is.read(buf1, 0, 10);
143        is.reset();
144        is.read(buf2, 0, 10);
145        is.reset();
146        assertTrue("Reset failed", new String(buf1, 0, buf1.length)
147                .equals(new String(buf2, 0, buf2.length)));
148    }
149
150    /**
151     * ByteArrayInputStream#skip(long)
152     */
153    public void test_skipJ() throws IOException {
154        byte[] buf1 = new byte[10];
155        is.skip(100);
156        is.read(buf1, 0, buf1.length);
157        assertTrue("Failed to skip to correct position", new String(buf1, 0,
158                buf1.length).equals(fileString.substring(100, 110)));
159    }
160
161    /**
162     * Sets up the fixture, for example, open a network connection. This method
163     * is called before a test is executed.
164     */
165    protected void setUp() {
166        is = new ByteArrayInputStream(fileString.getBytes());
167
168    }
169
170    /**
171     * Tears down the fixture, for example, close a network connection. This
172     * method is called after a test is executed.
173     */
174    protected void tearDown() {
175        try {
176            is.close();
177        } catch (Exception e) {
178        }
179    }
180}
181