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.luni.tests.java.io;
19
20import java.io.ByteArrayInputStream;
21import java.io.IOException;
22import java.io.PushbackInputStream;
23import java.io.UnsupportedEncodingException;
24
25public class PushbackInputStreamTest extends junit.framework.TestCase {
26
27	PushbackInputStream pis;
28
29	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";
30
31    public void test_reset() {
32        PushbackInputStream pb = new PushbackInputStream(
33                new ByteArrayInputStream(new byte[] { 0 }), 2);
34        try {
35            pb.reset();
36            fail("Should throw IOException");
37        } catch (IOException e) {
38            // expected
39        }
40    }
41
42    public void test_mark() {
43        PushbackInputStream pb = new PushbackInputStream(
44                new ByteArrayInputStream(new byte[] { 0 }), 2);
45        pb.mark(Integer.MAX_VALUE);
46        pb.mark(0);
47        pb.mark(-1);
48        pb.mark(Integer.MIN_VALUE);
49    }
50
51
52	/**
53	 * @tests java.io.PushbackInputStream#PushbackInputStream(java.io.InputStream)
54	 */
55	public void test_ConstructorLjava_io_InputStream() {
56        try {
57            PushbackInputStream str = new PushbackInputStream(null);
58            str.read();
59            fail("Expected IOException");
60        } catch (IOException e) {
61            // Expected
62        }
63
64		try {
65			pis = new PushbackInputStream(new ByteArrayInputStream("Hello"
66					.getBytes()));
67			pis.unread("He".getBytes());
68		     fail("Failed to throw exception on unread when buffer full");
69		} catch (IOException e) {
70		    // Expected
71		}
72	}
73
74	/**
75	 * @tests java.io.PushbackInputStream#PushbackInputStream(java.io.InputStream,
76	 *        int)
77	 */
78	public void test_ConstructorLjava_io_InputStreamI() {
79		// Test for method java.io.PushbackInputStream(java.io.InputStream, int)
80		try {
81			pis = new PushbackInputStream(new ByteArrayInputStream("Hello"
82					.getBytes()), 5);
83			pis.unread("Hellos".getBytes());
84		} catch (IOException e) {
85			// Correct
86			// Pushback buffer should be full
87			return;
88
89		}
90		fail("Failed to throw exception on unread when buffer full");
91	}
92
93    /*
94     * @tests java.io.PushBackInputStream(InputStream, int)
95     */
96    public void test_ConstructorLjava_io_InputStreamL() {
97        try {
98            PushbackInputStream str = new PushbackInputStream(null, 1);
99            str.read();
100            fail("Expected IOException");
101        } catch (IOException e) {
102            // Expected
103        }
104    }
105
106	/**
107	 * @tests java.io.PushbackInputStream#available()
108	 */
109	public void test_available() {
110		// Test for method int java.io.PushbackInputStream.available()
111		try {
112			assertTrue("Available returned incorrect number of bytes", pis
113					.available() == fileString.getBytes().length);
114		} catch (IOException e) {
115			fail("Exception during available test: " + e.toString());
116		}
117	}
118
119	/**
120	 * @tests java.io.PushbackInputStream#markSupported()
121	 */
122	public void test_markSupported() {
123		// Test for method boolean java.io.PushbackInputStream.markSupported()
124		assertTrue("markSupported returned true", !pis.markSupported());
125	}
126
127	/**
128	 * @tests java.io.PushbackInputStream#read()
129	 */
130	public void test_read() {
131		// Test for method int java.io.PushbackInputStream.read()
132		try {
133			assertTrue("Incorrect byte read", pis.read() == fileString
134					.getBytes("UTF-8")[0]);
135		} catch (IOException e) {
136			fail("Exception during read test : " + e.getMessage());
137		}
138	}
139
140	/**
141	 * @tests java.io.PushbackInputStream#read(byte[], int, int)
142	 */
143	public void test_read$BII() {
144		// Test for method int java.io.PushbackInputStream.read(byte [], int,
145		// int)
146		try {
147			byte[] buf = new byte[100];
148			pis.read(buf, 0, buf.length);
149			assertTrue("Incorrect bytes read", new String(buf, "UTF-8")
150					.equals(fileString.substring(0, 100)));
151		} catch (IOException e) {
152			fail("Exception during read test : " + e.getMessage());
153		}
154	}
155
156	/**
157	 * @tests java.io.PushbackInputStream#skip(long)
158	 */
159	public void test_skipJ() throws Exception {
160		// Test for method long java.io.PushbackInputStream.skip(long)
161                byte[] buf = new byte[50];
162                pis.skip(50);
163                pis.read(buf, 0, buf.length);
164                assertTrue("a) Incorrect bytes read", new String(buf, "UTF-8")
165                                .equals(fileString.substring(50, 100)));
166                pis.unread(buf);
167                pis.skip(25);
168                byte[] buf2 = new byte[25];
169                pis.read(buf2, 0, buf2.length);
170                assertTrue("b) Incorrect bytes read", new String(buf2, "UTF-8")
171                                .equals(fileString.substring(75, 100)));
172	}
173
174	/**
175	 * @tests java.io.PushbackInputStream#unread(byte[])
176	 */
177	public void test_unread$B() {
178		// Test for method void java.io.PushbackInputStream.unread(byte [])
179		try {
180			byte[] buf = new byte[100];
181			pis.read(buf, 0, buf.length);
182			assertTrue("Incorrect bytes read", new String(buf, "UTF-8")
183					.equals(fileString.substring(0, 100)));
184			pis.unread(buf);
185			pis.read(buf, 0, 50);
186			assertTrue("Failed to unread bytes", new String(buf, 0, 50, "UTF-8")
187					.equals(fileString.substring(0, 50)));
188		} catch (IOException e) {
189			fail("IOException during unread test : " + e.getMessage());
190		}
191	}
192
193	/**
194	 * @tests java.io.PushbackInputStream#unread(byte[], int, int)
195	 */
196	public void test_unread$BII() throws IOException {
197		// Test for method void java.io.PushbackInputStream.unread(byte [], int,
198		// int)
199		byte[] buf = new byte[100];
200		pis.read(buf, 0, buf.length);
201		assertTrue("Incorrect bytes read", new String(buf, "UTF-8")
202				.equals(fileString.substring(0, 100)));
203		pis.unread(buf, 50, 50);
204		pis.read(buf, 0, 50);
205		assertTrue("Failed to unread bytes", new String(buf, 0, 50, "UTF-8")
206				.equals(fileString.substring(50, 100)));
207
208        // Regression for HARMONY-49
209        try {
210            PushbackInputStream pb = new PushbackInputStream(
211                    new ByteArrayInputStream(new byte[] { 0 }), 2);
212            pb.unread(new byte[1], 0, 5);
213            fail("Assert 0: should throw IOE");
214        } catch (IOException e) {
215            // expected
216        }
217	}
218
219	/**
220	 * @tests java.io.PushbackInputStream#unread(int)
221	 */
222	public void test_unreadI() {
223		// Test for method void java.io.PushbackInputStream.unread(int)
224		try {
225			int x;
226			assertTrue("Incorrect byte read", (x = pis.read()) == fileString
227					.getBytes("UTF-8")[0]);
228			pis.unread(x);
229			assertTrue("Failed to unread", pis.read() == x);
230		} catch (IOException e) {
231			fail("IOException during read test : " + e.getMessage());
232		}
233	}
234
235	/**
236	 * Sets up the fixture, for example, open a network connection. This method
237	 * is called before a test is executed.
238	 */
239	protected void setUp() throws UnsupportedEncodingException {
240
241		pis = new PushbackInputStream(new ByteArrayInputStream(fileString
242				.getBytes("UTF-8")), 65535);
243	}
244
245	/**
246	 * Tears down the fixture, for example, close a network connection. This
247	 * method is called after a test is executed.
248	 */
249	protected void tearDown() {
250		try {
251			pis.close();
252		} catch (IOException e) {
253			fail("IOException during tearDown : " + e.getMessage());
254		}
255	}
256}
257