ByteArrayInputStreamTest.java revision 2ad60cfc28e14ee8f0bb038720836a4696c478ad
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 java.io.ByteArrayInputStream;
21import java.io.IOException;
22
23public class ByteArrayInputStreamTest extends junit.framework.TestCase {
24
25	private java.io.InputStream is;
26
27	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";
28
29	/**
30	 * @tests java.io.ByteArrayInputStream#ByteArrayInputStream(byte[])
31	 */
32	public void test_Constructor$B() {
33		// Test for method java.io.ByteArrayInputStream(byte [])
34
35		java.io.InputStream bis = new java.io.ByteArrayInputStream(fileString
36				.getBytes());
37
38		try {
39			assertTrue("Unable to create ByteArrayInputStream",
40					bis.available() == fileString.length());
41		} catch (Exception e) {
42			System.out.println("Exception during Constructor test");
43		}
44	}
45
46	/**
47	 * @throws IOException
48	 * @tests java.io.ByteArrayInputStream#ByteArrayInputStream(byte[], int,
49	 *        int)
50	 */
51	public void test_Constructor$BII() throws IOException {
52		// Test for method java.io.ByteArrayInputStream(byte [], int, int)
53
54		byte[] zz = fileString.getBytes();
55		java.io.InputStream bis = new java.io.ByteArrayInputStream(zz, 0, 100);
56
57		try {
58			assertEquals("Unable to create ByteArrayInputStream",
59					100, bis.available());
60		} catch (Exception e) {
61			fail("Exception during Constructor test");
62		}
63
64		// Regression test for Harmony-2405
65		new SubByteArrayInputStream(new byte[] { 1, 2 }, 444, 13);
66		assertEquals(444, SubByteArrayInputStream.pos);
67		assertEquals(444, SubByteArrayInputStream.mark);
68		assertEquals(2, SubByteArrayInputStream.count);
69	}
70
71	static class SubByteArrayInputStream extends ByteArrayInputStream {
72		public static byte[] buf;
73
74		public static int mark, pos, count;
75
76		SubByteArrayInputStream(byte[] buf, int offset, int length)
77				throws IOException {
78			super(buf, offset, length);
79			buf = super.buf;
80			mark = super.mark;
81			pos = super.pos;
82			count = super.count;
83		}
84	}
85
86	/**
87	 * @tests java.io.ByteArrayInputStream#available()
88	 */
89	public void test_available() {
90		// Test for method int java.io.ByteArrayInputStream.available()
91		try {
92			assertTrue("Returned incorrect number of available bytes", is
93					.available() == fileString.length());
94		} catch (Exception e) {
95			fail("Exception during available test");
96		}
97	}
98
99	/**
100	 * @tests java.io.ByteArrayInputStream#close()
101	 */
102	public void test_close() {
103		// Test for method void java.io.ByteArrayInputStream.close()
104		try {
105			is.read();
106		} catch (java.io.IOException e) {
107			fail("Failed reading from input stream");
108		}
109		try {
110			is.close();
111		} catch (java.io.IOException e) {
112			fail("Failed closing input stream");
113		}
114		try {
115			is.read();
116		} catch (Exception e) {
117			fail("Should be able to read from closed stream");
118		}
119	}
120
121	/**
122	 * @tests java.io.ByteArrayInputStream#mark(int)
123	 */
124	public void test_markI() {
125		// Test for method void java.io.ByteArrayInputStream.mark(int)
126		byte[] buf1 = new byte[100];
127		byte[] buf2 = new byte[100];
128		try {
129			is.skip(3000);
130			is.mark(1000);
131			is.read(buf1, 0, buf1.length);
132			is.reset();
133			is.read(buf2, 0, buf2.length);
134			is.reset();
135			assertTrue("Failed to mark correct position", new String(buf1, 0,
136					buf1.length).equals(new String(buf2, 0, buf2.length)));
137
138		} catch (Exception e) {
139			fail("Exception during mark test");
140		}
141
142	}
143
144	/**
145	 * @tests java.io.ByteArrayInputStream#markSupported()
146	 */
147	public void test_markSupported() {
148		// Test for method boolean java.io.ByteArrayInputStream.markSupported()
149		assertTrue("markSupported returned incorrect value", is.markSupported());
150	}
151
152	/**
153	 * @tests java.io.ByteArrayInputStream#read()
154	 */
155	public void test_read() {
156		// Test for method int java.io.ByteArrayInputStream.read()
157		try {
158
159			int c = is.read();
160			is.reset();
161			assertTrue("read returned incorrect char", c == fileString
162					.charAt(0));
163		} catch (Exception e) {
164			fail("Exception during read test");
165		}
166	}
167
168	/**
169	 * @tests java.io.ByteArrayInputStream#read(byte[], int, int)
170	 */
171	public void test_read$BII() {
172		// Test for method int java.io.ByteArrayInputStream.read(byte [], int,
173		// int)
174		byte[] buf1 = new byte[20];
175		try {
176			is.skip(50);
177			is.mark(100);
178			is.read(buf1, 0, buf1.length);
179			assertTrue("Failed to read correct data", new String(buf1, 0,
180					buf1.length).equals(fileString.substring(50, 70)));
181
182		} catch (Exception e) {
183			fail("Exception during read test: " + e);
184		}
185	}
186
187	/**
188	 * @tests java.io.ByteArrayInputStream#reset()
189	 */
190	public void test_reset() {
191		// Test for method void java.io.ByteArrayInputStream.reset()
192		byte[] buf1 = new byte[10];
193		byte[] buf2 = new byte[10];
194		try {
195			is.mark(200);
196			is.read(buf1, 0, 10);
197			is.reset();
198			is.read(buf2, 0, 10);
199			is.reset();
200			assertTrue("Reset failed", new String(buf1, 0, buf1.length)
201					.equals(new String(buf2, 0, buf2.length)));
202		} catch (Exception e) {
203			fail("Exception during reset test : " + e.getMessage());
204		}
205	}
206
207	/**
208	 * @tests java.io.ByteArrayInputStream#skip(long)
209	 */
210	public void test_skipJ() {
211		// Test for method long java.io.ByteArrayInputStream.skip(long)
212		byte[] buf1 = new byte[10];
213		try {
214			is.skip(100);
215			is.read(buf1, 0, buf1.length);
216			assertTrue("Failed to skip to correct position", new String(buf1,
217					0, buf1.length).equals(fileString.substring(100, 110)));
218		} catch (Exception e) {
219			fail("Exception during skip test : " + e.getMessage());
220		}
221	}
222
223	/**
224	 * Sets up the fixture, for example, open a network connection. This method
225	 * is called before a test is executed.
226	 */
227	protected void setUp() {
228
229		is = new java.io.ByteArrayInputStream(fileString.getBytes());
230
231	}
232
233	/**
234	 * Tears down the fixture, for example, close a network connection. This
235	 * method is called after a test is executed.
236	 */
237	protected void tearDown() {
238
239		try {
240			is.close();
241
242		} catch (Exception e) {
243		}
244	}
245}
246