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.PushbackInputStream;
023:
024: public class PushbackInputStreamTest extends junit.framework.TestCase {
025:
026: PushbackInputStream pis;
027:
028: public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\nTest_java_io_File\nTest_java_io_FileDescriptor\nTest_java_io_FileInputStream\nTest_java_io_FileNotFoundException\nTest_java_io_FileOutputStream\nTest_java_io_FilterInputStream\nTest_java_io_FilterOutputStream\nTest_java_io_InputStream\nTest_java_io_IOException\nTest_java_io_OutputStream\nTest_java_io_PrintStream\nTest_java_io_RandomAccessFile\nTest_java_io_SyncFailedException\nTest_java_lang_AbstractMethodError\nTest_java_lang_ArithmeticException\nTest_java_lang_ArrayIndexOutOfBoundsException\nTest_java_lang_ArrayStoreException\nTest_java_lang_Boolean\nTest_java_lang_Byte\nTest_java_lang_Character\nTest_java_lang_Class\nTest_java_lang_ClassCastException\nTest_java_lang_ClassCircularityError\nTest_java_lang_ClassFormatError\nTest_java_lang_ClassLoader\nTest_java_lang_ClassNotFoundException\nTest_java_lang_CloneNotSupportedException\nTest_java_lang_Double\nTest_java_lang_Error\nTest_java_lang_Exception\nTest_java_lang_ExceptionInInitializerError\nTest_java_lang_Float\nTest_java_lang_IllegalAccessError\nTest_java_lang_IllegalAccessException\nTest_java_lang_IllegalArgumentException\nTest_java_lang_IllegalMonitorStateException\nTest_java_lang_IllegalThreadStateException\nTest_java_lang_IncompatibleClassChangeError\nTest_java_lang_IndexOutOfBoundsException\nTest_java_lang_InstantiationError\nTest_java_lang_InstantiationException\nTest_java_lang_Integer\nTest_java_lang_InternalError\nTest_java_lang_InterruptedException\nTest_java_lang_LinkageError\nTest_java_lang_Long\nTest_java_lang_Math\nTest_java_lang_NegativeArraySizeException\nTest_java_lang_NoClassDefFoundError\nTest_java_lang_NoSuchFieldError\nTest_java_lang_NoSuchMethodError\nTest_java_lang_NullPointerException\nTest_java_lang_Number\nTest_java_lang_NumberFormatException\nTest_java_lang_Object\nTest_java_lang_OutOfMemoryError\nTest_java_lang_RuntimeException\nTest_java_lang_SecurityManager\nTest_java_lang_Short\nTest_java_lang_StackOverflowError\nTest_java_lang_String\nTest_java_lang_StringBuffer\nTest_java_lang_StringIndexOutOfBoundsException\nTest_java_lang_System\nTest_java_lang_Thread\nTest_java_lang_ThreadDeath\nTest_java_lang_ThreadGroup\nTest_java_lang_Throwable\nTest_java_lang_UnknownError\nTest_java_lang_UnsatisfiedLinkError\nTest_java_lang_VerifyError\nTest_java_lang_VirtualMachineError\nTest_java_lang_vm_Image\nTest_java_lang_vm_MemorySegment\nTest_java_lang_vm_ROMStoreException\nTest_java_lang_vm_VM\nTest_java_lang_Void\nTest_java_net_BindException\nTest_java_net_ConnectException\nTest_java_net_DatagramPacket\nTest_java_net_DatagramSocket\nTest_java_net_DatagramSocketImpl\nTest_java_net_InetAddress\nTest_java_net_NoRouteToHostException\nTest_java_net_PlainDatagramSocketImpl\nTest_java_net_PlainSocketImpl\nTest_java_net_Socket\nTest_java_net_SocketException\nTest_java_net_SocketImpl\nTest_java_net_SocketInputStream\nTest_java_net_SocketOutputStream\nTest_java_net_UnknownHostException\nTest_java_util_ArrayEnumerator\nTest_java_util_Date\nTest_java_util_EventObject\nTest_java_util_HashEnumerator\nTest_java_util_Hashtable\nTest_java_util_Properties\nTest_java_util_ResourceBundle\nTest_java_util_tm\nTest_java_util_Vector\n";
029:
030: public void test_reset() {
031: PushbackInputStream pb = new PushbackInputStream(
032: new ByteArrayInputStream(new byte[] { 0 }), 2);
033: try {
034: pb.reset();
035: fail("Should throw IOException");
036: } catch (IOException e) {
037: // expected
038: }
039: }
040:
041: public void test_mark() {
042: PushbackInputStream pb = new PushbackInputStream(
043: new ByteArrayInputStream(new byte[] { 0 }), 2);
044: pb.mark(Integer.MAX_VALUE);
045: pb.mark(0);
046: pb.mark(-1);
047: pb.mark(Integer.MIN_VALUE);
048: }
049:
050: /**
051: * @tests java.io.PushbackInputStream#PushbackInputStream(java.io.InputStream)
052: */
053: public void test_ConstructorLjava_io_InputStream() {
054: try {
055: PushbackInputStream str = new PushbackInputStream(null);
056: str.read();
057: fail("Expected IOException");
058: } catch (IOException e) {
059: // Expected
060: }
061:
062: try {
063: pis = new PushbackInputStream(new ByteArrayInputStream(
064: "Hello".getBytes()));
065: pis.unread("He".getBytes());
066: fail("Failed to throw exception on unread when buffer full");
067: } catch (IOException e) {
068: // Expected
069: }
070: }
071:
072: /**
073: * @tests java.io.PushbackInputStream#PushbackInputStream(java.io.InputStream,
074: * int)
075: */
076: public void test_ConstructorLjava_io_InputStreamI() {
077: // Test for method java.io.PushbackInputStream(java.io.InputStream, int)
078: try {
079: pis = new PushbackInputStream(new ByteArrayInputStream(
080: "Hello".getBytes()), 5);
081: pis.unread("Hellos".getBytes());
082: } catch (IOException e) {
083: // Correct
084: // Pushback buffer should be full
085: return;
086:
087: }
088: fail("Failed to throw exception on unread when buffer full");
089: }
090:
091: /*
092: * @tests java.io.PushBackInputStream(InputStream, int)
093: */
094: public void test_ConstructorLjava_io_InputStreamL() {
095: try {
096: PushbackInputStream str = new PushbackInputStream(null, 1);
097: str.read();
098: fail("Expected IOException");
099: } catch (IOException e) {
100: // Expected
101: }
102: }
103:
104: /**
105: * @tests java.io.PushbackInputStream#available()
106: */
107: public void test_available() {
108: // Test for method int java.io.PushbackInputStream.available()
109: try {
110: assertTrue("Available returned incorrect number of bytes",
111: pis.available() == fileString.getBytes().length);
112: } catch (IOException e) {
113: fail("Exception during available test: " + e.toString());
114: }
115: }
116:
117: /**
118: * @tests java.io.PushbackInputStream#markSupported()
119: */
120: public void test_markSupported() {
121: // Test for method boolean java.io.PushbackInputStream.markSupported()
122: assertTrue("markSupported returned true", !pis.markSupported());
123: }
124:
125: /**
126: * @tests java.io.PushbackInputStream#read()
127: */
128: public void test_read() {
129: // Test for method int java.io.PushbackInputStream.read()
130: try {
131: assertTrue("Incorrect byte read", pis.read() == fileString
132: .getBytes()[0]);
133: } catch (IOException e) {
134: fail("Exception during read test : " + e.getMessage());
135: }
136: }
137:
138: /**
139: * @tests java.io.PushbackInputStream#read(byte[], int, int)
140: */
141: public void test_read$BII() {
142: // Test for method int java.io.PushbackInputStream.read(byte [], int,
143: // int)
144: try {
145: byte[] buf = new byte[100];
146: pis.read(buf, 0, buf.length);
147: assertTrue("Incorrect bytes read", new String(buf)
148: .equals(fileString.substring(0, 100)));
149: } catch (IOException e) {
150: fail("Exception during read test : " + e.getMessage());
151: }
152: }
153:
154: /**
155: * @tests java.io.PushbackInputStream#skip(long)
156: */
157: public void test_skipJ() throws Exception {
158: // Test for method long java.io.PushbackInputStream.skip(long)
159: byte[] buf = new byte[50];
160: pis.skip(50);
161: pis.read(buf, 0, buf.length);
162: assertTrue("a) Incorrect bytes read", new String(buf)
163: .equals(fileString.substring(50, 100)));
164: pis.unread(buf);
165: pis.skip(25);
166: byte[] buf2 = new byte[25];
167: pis.read(buf2, 0, buf2.length);
168: assertTrue("b) Incorrect bytes read", new String(buf2)
169: .equals(fileString.substring(75, 100)));
170: }
171:
172: /**
173: * @tests java.io.PushbackInputStream#unread(byte[])
174: */
175: public void test_unread$B() {
176: // Test for method void java.io.PushbackInputStream.unread(byte [])
177: try {
178: byte[] buf = new byte[100];
179: pis.read(buf, 0, buf.length);
180: assertTrue("Incorrect bytes read", new String(buf)
181: .equals(fileString.substring(0, 100)));
182: pis.unread(buf);
183: pis.read(buf, 0, 50);
184: assertTrue("Failed to unread bytes", new String(buf, 0, 50)
185: .equals(fileString.substring(0, 50)));
186: } catch (IOException e) {
187: fail("IOException during unread test : " + e.getMessage());
188: }
189: }
190:
191: /**
192: * @tests java.io.PushbackInputStream#unread(byte[], int, int)
193: */
194: public void test_unread$BII() throws IOException {
195: // Test for method void java.io.PushbackInputStream.unread(byte [], int,
196: // int)
197: byte[] buf = new byte[100];
198: pis.read(buf, 0, buf.length);
199: assertTrue("Incorrect bytes read", new String(buf)
200: .equals(fileString.substring(0, 100)));
201: pis.unread(buf, 50, 50);
202: pis.read(buf, 0, 50);
203: assertTrue("Failed to unread bytes", new String(buf, 0, 50)
204: .equals(fileString.substring(50, 100)));
205:
206: // Regression for HARMONY-49
207: try {
208: PushbackInputStream pb = new PushbackInputStream(
209: new ByteArrayInputStream(new byte[] { 0 }), 2);
210: pb.unread(new byte[1], 0, 5);
211: fail("Assert 0: should throw IOE");
212: } catch (IOException e) {
213: // expected
214: }
215: }
216:
217: /**
218: * @tests java.io.PushbackInputStream#unread(int)
219: */
220: public void test_unreadI() {
221: // Test for method void java.io.PushbackInputStream.unread(int)
222: try {
223: int x;
224: assertTrue("Incorrect byte read",
225: (x = pis.read()) == fileString.getBytes()[0]);
226: pis.unread(x);
227: assertTrue("Failed to unread", pis.read() == x);
228: } catch (IOException e) {
229: fail("IOException during read test : " + e.getMessage());
230: }
231: }
232:
233: /**
234: * Sets up the fixture, for example, open a network connection. This method
235: * is called before a test is executed.
236: */
237: protected void setUp() {
238:
239: pis = new PushbackInputStream(new ByteArrayInputStream(
240: fileString.getBytes()), 65535);
241: }
242:
243: /**
244: * Tears down the fixture, for example, close a network connection. This
245: * method is called after a test is executed.
246: */
247: protected void tearDown() {
248: try {
249: pis.close();
250: } catch (IOException e) {
251: fail("IOException during tearDown : " + e.getMessage());
252: }
253: }
254: }
|