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: import java.io.SequenceInputStream;
024: import java.util.Enumeration;
025:
026: public class SequenceInputStreamTest extends junit.framework.TestCase {
027:
028: SequenceInputStream si;
029:
030: String s1 = "Hello";
031:
032: String s2 = "World";
033:
034: /**
035: * @tests java.io.SequenceInputStream#SequenceInputStream(java.io.InputStream,
036: * java.io.InputStream)
037: */
038: public void test_ConstructorLjava_io_InputStreamLjava_io_InputStream() {
039: // Test for method java.io.SequenceInputStream(java.io.InputStream,
040: // java.io.InputStream)
041: // Used in tests
042: }
043:
044: /**
045: * @tests SequenceInputStream#SequenceInputStream(java.io.InputStream,
046: * java.io.InputStream)
047: */
048: public void test_Constructor_LInputStreamLInputStream_Null() {
049: try {
050: si = new SequenceInputStream(null, null);
051: fail("should throw NullPointerException");
052: } catch (NullPointerException e) {
053: //expected
054: }
055:
056: //will not throw NullPointerException if the first InputStream is not null
057: InputStream is = new ByteArrayInputStream(s1.getBytes());
058: si = new SequenceInputStream(is, null);
059: }
060:
061: /**
062: * @tests java.io.SequenceInputStream#SequenceInputStream(java.util.Enumeration)
063: */
064: @SuppressWarnings("unchecked")
065: public void test_ConstructorLjava_util_Enumeration() {
066: // Test for method java.io.SequenceInputStream(java.util.Enumeration)
067: class StreamEnumerator implements Enumeration {
068: InputStream streams[] = new InputStream[2];
069:
070: int count = 0;
071:
072: public StreamEnumerator() {
073: streams[0] = new ByteArrayInputStream(s1.getBytes());
074: streams[1] = new ByteArrayInputStream(s2.getBytes());
075: }
076:
077: public boolean hasMoreElements() {
078: return count < streams.length;
079: }
080:
081: public Object nextElement() {
082: return streams[count++];
083: }
084: }
085:
086: try {
087: si = new SequenceInputStream(new StreamEnumerator());
088: byte buf[] = new byte[s1.length() + s2.length()];
089: si.read(buf, 0, s1.length());
090: si.read(buf, s1.length(), s2.length());
091: assertTrue("Read incorrect bytes: " + new String(buf),
092: new String(buf).equals(s1 + s2));
093: } catch (IOException e) {
094: fail("IOException during read test : " + e.getMessage());
095: }
096:
097: }
098:
099: /**
100: * @tests java.io.SequenceInputStream#available()
101: */
102: public void test_available() {
103: // Test for method int java.io.SequenceInputStream.available()
104: try {
105:
106: assertTrue("Returned incorrect number of bytes: "
107: + si.available(), si.available() == s1.length());
108: } catch (IOException e) {
109: fail("IOException during available test : "
110: + e.getMessage());
111: }
112: }
113:
114: /**
115: * @tests 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: * @tests 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
131: .charAt(1));
132: } catch (IOException e) {
133: fail("IOException during read test: " + e.getMessage());
134: }
135:
136: //returns -1 if the stream is closed , do not throw IOException
137: si.close();
138: int result = si.read();
139: assertEquals(-1, result);
140: }
141:
142: /**
143: * @tests java.io.SequenceInputStream#read(byte[], int, int)
144: */
145: public void test_read$BII() throws IOException {
146: // Test for method int java.io.SequenceInputStream.read(byte [], int,
147: // int)
148: try {
149: byte buf[] = new byte[s1.length() + s2.length()];
150: si.read(buf, 0, s1.length());
151: si.read(buf, s1.length(), s2.length());
152: assertTrue("Read incorrect bytes: " + new String(buf),
153: new String(buf).equals(s1 + s2));
154: } catch (IOException e) {
155: fail("IOException during read test : " + e.getMessage());
156: }
157:
158: ByteArrayInputStream bis1 = new ByteArrayInputStream(
159: new byte[] { 1, 2, 3, 4 });
160: ByteArrayInputStream bis2 = new ByteArrayInputStream(
161: new byte[] { 5, 6, 7, 8 });
162: SequenceInputStream sis = new SequenceInputStream(bis1, bis2);
163:
164: try {
165: sis.read(null, 0, -1);
166: fail("Expected NullPointerException exception");
167: } catch (NullPointerException e) {
168: // expected
169: }
170:
171: //returns -1 if the stream is closed , do not throw IOException
172: byte[] array = new byte[] { 1, 2, 3, 4 };
173: sis.close();
174: int result = sis.read(array, 0, 5);
175: assertEquals(-1, result);
176:
177: }
178:
179: /**
180: * Sets up the fixture, for example, open a network connection. This method
181: * is called before a test is executed.
182: */
183: protected void setUp() {
184: si = new SequenceInputStream(new ByteArrayInputStream(s1
185: .getBytes()), new ByteArrayInputStream(s2.getBytes()));
186: }
187:
188: /**
189: * Tears down the fixture, for example, close a network connection. This
190: * method is called after a test is executed.
191: */
192: protected void tearDown() {
193: }
194: }
|