001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.io;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023:
024: public class ByteArrayInputStreamTest extends junit.framework.TestCase {
025:
026: private InputStream is;
027:
028: 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";
029:
030: /**
031: * @tests ByteArrayInputStream#ByteArrayInputStream(byte[])
032: */
033: public void test_Constructor$B() throws IOException {
034: InputStream bis = new ByteArrayInputStream(fileString
035: .getBytes());
036:
037: assertTrue("Unable to create ByteArrayInputStream", bis
038: .available() == fileString.length());
039: }
040:
041: /**
042: * @tests ByteArrayInputStream#ByteArrayInputStream(byte[], int, int)
043: */
044: public void test_Constructor$BII() throws IOException {
045: byte[] zz = fileString.getBytes();
046: InputStream bis = new ByteArrayInputStream(zz, 0, 100);
047:
048: assertEquals("Unable to create ByteArrayInputStream", 100, bis
049: .available());
050:
051: // Regression test for Harmony-2405
052: new SubByteArrayInputStream(new byte[] { 1, 2 }, 444, 13);
053: assertEquals(444, SubByteArrayInputStream.pos);
054: assertEquals(444, SubByteArrayInputStream.mark);
055: assertEquals(2, SubByteArrayInputStream.count);
056: }
057:
058: static class SubByteArrayInputStream extends ByteArrayInputStream {
059: public static byte[] buf;
060:
061: public static int mark, pos, count;
062:
063: SubByteArrayInputStream(byte[] buf, int offset, int length)
064: throws IOException {
065: super (buf, offset, length);
066: buf = super .buf;
067: mark = super .mark;
068: pos = super .pos;
069: count = super .count;
070: }
071: }
072:
073: /**
074: * @tests ByteArrayInputStream#available()
075: */
076: public void test_available() throws IOException {
077: assertTrue("Returned incorrect number of available bytes", is
078: .available() == fileString.length());
079: }
080:
081: /**
082: * @tests ByteArrayInputStream#close()
083: */
084: public void test_close() throws IOException {
085: is.read();
086: is.close();
087: is.read(); // Should be able to read from a closed stream
088: }
089:
090: /**
091: * @tests ByteArrayInputStream#mark(int)
092: */
093: public void test_markI() throws IOException {
094: byte[] buf1 = new byte[100];
095: byte[] buf2 = new byte[100];
096: is.skip(3000);
097: is.mark(1000);
098: is.read(buf1, 0, buf1.length);
099: is.reset();
100: is.read(buf2, 0, buf2.length);
101: is.reset();
102: assertTrue("Failed to mark correct position", new String(buf1,
103: 0, buf1.length)
104: .equals(new String(buf2, 0, buf2.length)));
105: }
106:
107: /**
108: * @tests ByteArrayInputStream#markSupported()
109: */
110: public void test_markSupported() {
111: assertTrue("markSupported returned incorrect value", is
112: .markSupported());
113: }
114:
115: /**
116: * @tests ByteArrayInputStream#read()
117: */
118: public void test_read() throws IOException {
119: int c = is.read();
120: is.reset();
121: assertTrue("read returned incorrect char", c == fileString
122: .charAt(0));
123: }
124:
125: /**
126: * @tests ByteArrayInputStream#read(byte[], int, int)
127: */
128: public void test_read$BII() throws IOException {
129: byte[] buf1 = new byte[20];
130: is.skip(50);
131: is.mark(100);
132: is.read(buf1, 0, buf1.length);
133: assertTrue("Failed to read correct data", new String(buf1, 0,
134: buf1.length).equals(fileString.substring(50, 70)));
135: }
136:
137: /**
138: * @tests ByteArrayInputStream#reset()
139: */
140: public void test_reset() throws IOException {
141: byte[] buf1 = new byte[10];
142: byte[] buf2 = new byte[10];
143: is.mark(200);
144: is.read(buf1, 0, 10);
145: is.reset();
146: is.read(buf2, 0, 10);
147: is.reset();
148: assertTrue("Reset failed", new String(buf1, 0, buf1.length)
149: .equals(new String(buf2, 0, buf2.length)));
150: }
151:
152: /**
153: * @tests ByteArrayInputStream#skip(long)
154: */
155: public void test_skipJ() throws IOException {
156: byte[] buf1 = new byte[10];
157: is.skip(100);
158: is.read(buf1, 0, buf1.length);
159: assertTrue("Failed to skip to correct position", new String(
160: buf1, 0, buf1.length).equals(fileString.substring(100,
161: 110)));
162: }
163:
164: /**
165: * Sets up the fixture, for example, open a network connection. This method
166: * is called before a test is executed.
167: */
168: protected void setUp() {
169: is = new ByteArrayInputStream(fileString.getBytes());
170:
171: }
172:
173: /**
174: * Tears down the fixture, for example, close a network connection. This
175: * method is called after a test is executed.
176: */
177: protected void tearDown() {
178: try {
179: is.close();
180: } catch (Exception e) {
181: }
182: }
183: }
|