blob: da8b85de2752736cd942b09993559df7821cd283 [file] [log] [blame]
The Android Open Source Projectb5de22c2012-04-01 00:00:00 -07001/*
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 libcore.java.io;
19
20import java.io.ByteArrayInputStream;
21import java.io.ByteArrayOutputStream;
22import java.io.FilterOutputStream;
23import java.io.IOException;
24import tests.support.Support_OutputStream;
25
26public class OldFilterOutputStreamTest extends junit.framework.TestCase {
27
28 private java.io.FilterOutputStream os;
29
30 java.io.ByteArrayOutputStream bos;
31
32 java.io.ByteArrayInputStream bis;
33
34 byte[] ibuf = new byte[4096];
35
36 private final 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_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";
37
38 private final int testLength = fileString.length();
39
40
41 public void test_ConstructorLjava_io_OutputStream() {
42 // Test for method java.io.FilterOutputStream(java.io.OutputStream)
43 try {
44 bos = new ByteArrayOutputStream();
45 os = new FilterOutputStream(bos);
46 os.write('t');
47 } catch (java.io.IOException e) {
48 fail("Constructor test failed : " + e.getMessage());
49 }
50 }
51
52 public void test_close() throws IOException {
53 Support_OutputStream sos = new Support_OutputStream();
54 os = new FilterOutputStream(sos);
55 os.close();
56
57 try {
58 os.write(42);
59 } catch (java.io.IOException e) {
60 fail("Test 1: Unexpected IOException.");
61 }
62
63 sos.setThrowsException(true);
64 try {
65 os.write(42);
66 fail("Test 2: IOException expected.");
67 } catch (java.io.IOException e) {
68 // Expected.
69 }
70
71 os = new FilterOutputStream(sos);
72 try {
73 os.close();
74 fail("Test 3: IOException expected.");
75 } catch (IOException e) {
76 // Expected.
77 }
78 }
79
80 public void test_flush() throws IOException {
81 Support_OutputStream sos = new Support_OutputStream(550);
82 os = new FilterOutputStream(sos);
83 os.write(fileString.getBytes(), 0, 500);
84 os.flush();
85 assertEquals("Test 1: Bytes not written after flush;",
86 500, sos.size());
87
88 sos.setThrowsException(true);
89 try {
90 os.flush();
91 fail("Test 2: IOException expected.");
92 } catch (IOException e) {
93 // Expected.
94 }
95 sos.setThrowsException(false);
96 }
97
98 public void test_write$B() throws IOException {
99 Support_OutputStream sos = new Support_OutputStream(testLength);
100 os = new FilterOutputStream(sos);
101 os.write(fileString.getBytes());
102
103 bis = new ByteArrayInputStream(sos.toByteArray());
104 assertTrue("Test 1: Bytes have not been written.",
105 bis.available() == testLength);
106 byte[] wbytes = new byte[testLength];
107 bis.read(wbytes, 0, testLength);
108 assertTrue("Test 2: Incorrect bytes written or read.",
109 fileString.equals(new String(wbytes)));
110
111 try {
112 // Support_OutputStream throws an IOException if the internal
113 // buffer is full, which it should be now.
114 os.write(42);
115 fail("Test 2: IOException expected.");
116 } catch (IOException e) {
117 // Expected.
118 }
119 }
120
121 public void test_write$BII() throws IOException {
122 Support_OutputStream sos = new Support_OutputStream(testLength);
123 os = new FilterOutputStream(sos);
124 os.write(fileString.getBytes(), 10, testLength - 10);
125
126 bis = new ByteArrayInputStream(sos.toByteArray());
127 assertTrue("Test 1: Bytes have not been written.",
128 bis.available() == testLength - 10);
129 byte[] wbytes = new byte[testLength - 10];
130 bis.read(wbytes);
131 assertTrue("Test 2: Incorrect bytes written or read.",
132 fileString.substring(10).equals(new String(wbytes)));
133
134 try {
135 // Support_OutputStream throws an IOException if the internal
136 // buffer is full, which it should be eventually.
137 os.write(fileString.getBytes());
138 fail("Test 2: IOException expected.");
139 } catch (IOException e) {
140 // Expected.
141 }
142 }
143
144 public void test_write$BII_Exception() throws IOException {
145 Support_OutputStream sos = new Support_OutputStream(testLength);
146 os = new FilterOutputStream(sos);
147 byte[] buf = new byte[10];
148
149 try {
150 os.write(buf, -1, 1);
151 fail("IndexOutOfBoundsException expected.");
152 } catch (IndexOutOfBoundsException e) {
153 // Expected.
154 }
155
156 try {
157 os.write(buf, 0, -1);
158 fail("IndexOutOfBoundsException expected.");
159 } catch (IndexOutOfBoundsException e) {
160 // Expected.
161 }
162
163 try {
164 os.write(buf, 10, 1);
165 fail("IndexOutOfBoundsException expected.");
166 } catch (IndexOutOfBoundsException e) {
167 // Expected.
168 }
169 }
170
171 public void test_writeI() throws IOException {
172 Support_OutputStream sos = new Support_OutputStream(1);
173 os = new FilterOutputStream(sos);
174 os.write(42);
175
176 bis = new ByteArrayInputStream(sos.toByteArray());
177 assertTrue("Test 1: Byte has not been written.",
178 bis.available() == 1);
179 assertEquals("Test 2: Incorrect byte written or read;",
180 42, bis.read());
181
182 try {
183 // Support_OutputStream throws an IOException if the internal
184 // buffer is full, which it should be now.
185 os.write(42);
186 fail("Test 2: IOException expected.");
187 } catch (IOException e) {
188 // Expected.
189 }
190 }
191
192 protected void tearDown() {
193 try {
194 if (bos != null)
195 bos.close();
196 if (bis != null)
197 bis.close();
198 if (os != null)
199 os.close();
200 } catch (Exception e) {
201 }
202 }
203}