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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package tests.api.java.io;
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.InputStreamReader;
23 import java.io.DataInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.OutputStream;
28 import java.io.PrintStream;
29 import java.io.UnsupportedEncodingException;
30 import java.util.Locale;
32 public class PrintStreamTest extends junit.framework.TestCase {
34 ByteArrayOutputStream bos = new ByteArrayOutputStream();
36 byte[] ibuf = new byte[4096];
38 private File testFile = null;
40 private String testFilePath = null;
42 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_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";
44 private static class MockPrintStream extends PrintStream {
46 public MockPrintStream(String fileName) throws FileNotFoundException {
50 public MockPrintStream(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
54 public MockPrintStream(OutputStream os) {
59 public void clearError() {
64 public void setError() {
70 * @tests {@link java.io.PrintStream#PrintStream(String)}
72 public void test_Constructor_Ljava_lang_String() throws IOException {
73 MockPrintStream os = new MockPrintStream(testFilePath);
79 * @tests {@link java.io.PrintStream#PrintStream(String, String)}
81 public void test_Constructor_Ljava_lang_String_Ljava_lang_String() throws Exception {
82 MockPrintStream os = new MockPrintStream(testFilePath, "utf-8");
88 * @tests java.io.PrintStream#PrintStream(java.io.OutputStream)
90 public void test_ConstructorLjava_io_OutputStream() throws Exception {
91 // Test for method java.io.PrintStream(java.io.OutputStream)
92 PrintStream os = new PrintStream(bos);
93 os.print(2345.76834720202);
96 // regression for HARMONY-1195
98 os = new PrintStream(bos, true, null);
99 fail("Should throw NPE");
100 } catch (NullPointerException e) {}
104 * @tests java.io.PrintStream#PrintStream(java.io.OutputStream, boolean)
106 public void test_ConstructorLjava_io_OutputStreamZ() {
107 // Test for method java.io.PrintStream(java.io.OutputStream, boolean)
108 PrintStream os = new PrintStream(bos);
109 os.println(2345.76834720202);
111 assertTrue("Bytes not written", bos.size() > 0);
116 * @tests java.io.PrintStream#PrintStream(java.io.OutputStream, boolean, String)
118 public void test_ConstructorLjava_io_OutputStreamZLjava_lang_String() {
120 new PrintStream(new ByteArrayOutputStream(), false,
122 fail("Expected UnsupportedEncodingException");
123 } catch (UnsupportedEncodingException e) {
129 * @tests java.io.PrintStream#checkError()
131 public void test_checkError() throws Exception {
132 // Test for method boolean java.io.PrintStream.checkError()
133 PrintStream os = new PrintStream(new OutputStream() {
135 public void write(int b) throws IOException {
136 throw new IOException();
139 public void write(byte[] b, int o, int l) throws IOException {
140 throw new IOException();
143 os.print(fileString.substring(0, 501));
145 assertTrue("Checkerror should return true", os.checkError());
149 * @tests {@link java.io.PrintStream#clearError()}
151 public void test_clearError() throws FileNotFoundException {
152 MockPrintStream os = new MockPrintStream(testFilePath);
153 assertFalse(os.checkError());
155 assertTrue(os.checkError());
157 assertFalse(os.checkError());
162 * @tests java.io.PrintStream#close()
164 public void test_close() throws Exception {
165 // Test for method void java.io.PrintStream.close()
166 PrintStream os = new PrintStream(bos);
172 * @tests java.io.PrintStream#flush()
174 public void test_flush() throws Exception {
175 // Test for method void java.io.PrintStream.flush()
176 PrintStream os = new PrintStream(bos);
177 os.print(fileString.substring(0, 501));
179 assertEquals("Bytes not written after flush", 501, bos.size());
185 * @tests java.io.PrintStream#print(char[])
187 public void test_print$C() {
188 // Test for method void java.io.PrintStream.print(char [])
189 PrintStream os = new PrintStream(bos, true);
191 os.print((char[]) null);
192 fail("NPE expected");
193 } catch (NullPointerException ok) {}
195 os = new PrintStream(bos, true);
196 char[] sc = new char[4000];
197 fileString.getChars(0, fileString.length(), sc, 0);
199 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
202 byte[] rbytes = new byte[4000];
203 bis.read(rbytes, 0, fileString.length());
204 assertEquals("Incorrect char[] written", fileString, new String(rbytes,
205 0, fileString.length()));
209 * @tests java.io.PrintStream#print(char)
211 public void test_printC() throws Exception {
212 // Test for method void java.io.PrintStream.print(char)
213 PrintStream os = new PrintStream(bos, true);
215 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
216 InputStreamReader isr = new InputStreamReader(bis);
217 assertEquals("Incorrect char written", 't', isr.read());
221 * @tests java.io.PrintStream#print(double)
223 public void test_printD() {
224 // Test for method void java.io.PrintStream.print(double)
225 byte[] rbuf = new byte[100];
226 PrintStream os = new PrintStream(bos, true);
227 os.print(2345.76834720202);
228 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
229 bis.read(rbuf, 0, 16);
230 assertEquals("Incorrect double written", "2345.76834720202",
231 new String(rbuf, 0, 16));
235 * @tests java.io.PrintStream#print(float)
237 public void test_printF() {
238 // Test for method void java.io.PrintStream.print(float)
239 PrintStream os = new PrintStream(bos, true);
240 byte rbuf[] = new byte[10];
243 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
244 bis.read(rbuf, 0, 8);
245 assertEquals("Incorrect float written", "29.08764", new String(rbuf, 0,
251 * @tests java.io.PrintStream#print(int)
253 public void test_printI() {
254 // Test for method void java.io.PrintStream.print(int)
255 PrintStream os = new PrintStream(bos, true);
257 byte[] rbuf = new byte[18];
258 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
259 bis.read(rbuf, 0, 9);
260 assertEquals("Incorrect int written", "768347202", new String(rbuf, 0,
265 * @tests java.io.PrintStream#print(long)
267 public void test_printJ() {
268 // Test for method void java.io.PrintStream.print(long)
269 byte[] rbuf = new byte[100];
270 PrintStream os = new PrintStream(bos, true);
271 os.print(9875645283333L);
273 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
274 bis.read(rbuf, 0, 13);
275 assertEquals("Incorrect long written", "9875645283333", new String(
280 * @tests java.io.PrintStream#print(java.lang.Object)
282 public void test_printLjava_lang_Object() throws Exception {
283 // Test for method void java.io.PrintStream.print(java.lang.Object)
284 PrintStream os = new PrintStream(bos, true);
285 os.print((Object) null);
287 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
288 byte[] nullbytes = new byte[4];
289 bis.read(nullbytes, 0, 4);
290 assertEquals("null should be written", "null", new String(nullbytes, 0,
297 ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
298 os = new PrintStream(bos1, true);
299 os.print(new java.util.Vector());
300 bis = new ByteArrayInputStream(bos1.toByteArray());
301 byte[] rbytes = new byte[2];
302 bis.read(rbytes, 0, 2);
303 assertEquals("Incorrect Object written", "[]", new String(rbytes, 0, 2));
307 * @tests java.io.PrintStream#print(java.lang.String)
309 public void test_printLjava_lang_String() throws Exception {
310 // Test for method void java.io.PrintStream.print(java.lang.String)
311 PrintStream os = new PrintStream(bos, true);
312 os.print((String) null);
313 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
314 byte[] nullbytes = new byte[4];
315 bis.read(nullbytes, 0, 4);
316 assertEquals("null should be written", "null", new String(nullbytes, 0,
323 ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
324 os = new PrintStream(bos1, true);
325 os.print("Hello World");
326 bis = new ByteArrayInputStream(bos1.toByteArray());
327 byte rbytes[] = new byte[100];
328 bis.read(rbytes, 0, 11);
329 assertEquals("Incorrect string written", "Hello World", new String(
334 * @tests java.io.PrintStream#print(boolean)
336 public void test_printZ() throws Exception {
337 // Test for method void java.io.PrintStream.print(boolean)
338 PrintStream os = new PrintStream(bos, true);
340 DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bos
343 assertTrue("Incorrect boolean written", dis.readBoolean());
347 * @tests java.io.PrintStream#println()
349 public void test_println() throws Exception {
350 // Test for method void java.io.PrintStream.println()
352 PrintStream os = new PrintStream(bos, true);
354 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
355 InputStreamReader isr = new InputStreamReader(bis);
356 assertTrue("Newline not written", (c = (char) isr.read()) == '\r'
361 * @tests java.io.PrintStream#println(char[])
363 public void test_println$C() throws Exception {
364 // Test for method void java.io.PrintStream.println(char [])
365 PrintStream os = new PrintStream(bos, true);
366 char[] sc = new char[4000];
367 fileString.getChars(0, fileString.length(), sc, 0);
369 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
370 InputStreamReader isr = new InputStreamReader(bis);
371 byte[] rbytes = new byte[4000];
372 bis.read(rbytes, 0, fileString.length());
373 assertEquals("Incorrect char[] written", fileString, new String(rbytes,
374 0, fileString.length()));
376 // In this particular test method, the end of data is not immediately
377 // followed by newLine separator in the reading buffer, instead its
378 // followed by zeros. The newline is written as the last entry
379 // in the inputStream buffer. Therefore, we must keep reading until we
382 boolean newline = false;
383 while ((r = isr.read()) != -1) {
384 if (r == '\r' || r == '\n')
387 assertTrue("Newline not written", newline);
391 * @tests java.io.PrintStream#println(char)
393 public void test_printlnC() throws Exception {
394 // Test for method void java.io.PrintStream.println(char)
396 PrintStream os = new PrintStream(bos, true);
398 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
399 InputStreamReader isr = new InputStreamReader(bis);
400 assertEquals("Incorrect char written", 't', isr.read());
401 assertTrue("Newline not written", (c = isr.read()) == '\r' || c == '\n');
405 * @tests java.io.PrintStream#println(double)
407 public void test_printlnD() throws Exception {
408 // Test for method void java.io.PrintStream.println(double)
410 PrintStream os = new PrintStream(bos, true);
411 os.println(2345.76834720202);
412 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
413 InputStreamReader isr = new InputStreamReader(bis);
414 byte[] rbuf = new byte[100];
415 bis.read(rbuf, 0, 16);
416 assertEquals("Incorrect double written", "2345.76834720202",
417 new String(rbuf, 0, 16));
418 assertTrue("Newline not written", (c = isr.read()) == '\r' || c == '\n');
422 * @tests java.io.PrintStream#println(float)
424 public void test_printlnF() throws Exception {
425 // Test for method void java.io.PrintStream.println(float)
427 byte[] rbuf = new byte[100];
428 PrintStream os = new PrintStream(bos, true);
429 os.println(29.08764f);
430 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
431 InputStreamReader isr = new InputStreamReader(bis);
432 bis.read(rbuf, 0, 8);
433 assertEquals("Incorrect float written", "29.08764", new String(rbuf, 0,
435 assertTrue("Newline not written", (c = isr.read()) == '\r' || c == '\n');
439 * @tests java.io.PrintStream#println(int)
441 public void test_printlnI() throws Exception {
442 // Test for method void java.io.PrintStream.println(int)
444 PrintStream os = new PrintStream(bos, true);
445 os.println(768347202);
446 byte[] rbuf = new byte[100];
447 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
448 InputStreamReader isr = new InputStreamReader(bis);
449 bis.read(rbuf, 0, 9);
450 assertEquals("Incorrect int written", "768347202", new String(rbuf, 0,
452 assertTrue("Newline not written", (c = isr.read()) == '\r' || c == '\n');
456 * @tests java.io.PrintStream#println(long)
458 public void test_printlnJ() throws Exception {
459 // Test for method void java.io.PrintStream.println(long)
461 PrintStream os = new PrintStream(bos, true);
462 os.println(9875645283333L);
463 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
464 InputStreamReader isr = new InputStreamReader(bis);
465 byte[] rbuf = new byte[100];
466 bis.read(rbuf, 0, 13);
467 assertEquals("Incorrect long written", "9875645283333", new String(
469 assertTrue("Newline not written", (c = isr.read()) == '\r' || c == '\n');
473 * @tests java.io.PrintStream#println(java.lang.Object)
475 public void test_printlnLjava_lang_Object() throws Exception {
476 // Test for method void java.io.PrintStream.println(java.lang.Object)
478 PrintStream os = new PrintStream(bos, true);
479 os.println(new java.util.Vector());
480 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
481 InputStreamReader isr = new InputStreamReader(bis);
482 byte[] rbytes = new byte[2];
483 bis.read(rbytes, 0, 2);
484 assertEquals("Incorrect Vector written", "[]", new String(rbytes, 0, 2));
485 assertTrue("Newline not written", (c = (char) isr.read()) == '\r'
490 * @tests java.io.PrintStream#println(java.lang.String)
492 public void test_printlnLjava_lang_String() throws Exception {
493 // Test for method void java.io.PrintStream.println(java.lang.String)
495 PrintStream os = new PrintStream(bos, true);
496 os.println("Hello World");
497 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
498 InputStreamReader isr = new InputStreamReader(bis);
499 byte rbytes[] = new byte[100];
500 bis.read(rbytes, 0, 11);
501 assertEquals("Incorrect string written", "Hello World", new String(
503 assertTrue("Newline not written", (c = (char) isr.read()) == '\r'
508 * @tests java.io.PrintStream#println(boolean)
510 public void test_printlnZ() throws Exception {
511 // Test for method void java.io.PrintStream.println(boolean)
513 PrintStream os = new PrintStream(bos, true);
515 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
516 InputStreamReader isr = new InputStreamReader(bis);
517 byte[] rbuf = new byte[100];
518 bis.read(rbuf, 0, 4);
519 assertEquals("Incorrect boolean written", "true",
520 new String(rbuf, 0, 4));
521 assertTrue("Newline not written", (c = isr.read()) == '\r' || c == '\n');
525 * @tests java.io.PrintStream#write(byte[], int, int)
527 public void test_write$BII() {
528 // Test for method void java.io.PrintStream.write(byte [], int, int)
529 PrintStream os = new PrintStream(bos, true);
530 os.write(fileString.getBytes(), 0, fileString.length());
531 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
532 byte rbytes[] = new byte[4000];
533 bis.read(rbytes, 0, fileString.length());
534 assertTrue("Incorrect bytes written", new String(rbytes, 0, fileString
535 .length()).equals(fileString));
539 * @tests java.io.PrintStream#write(int)
541 public void test_writeI() {
542 // Test for method void java.io.PrintStream.write(int)
543 PrintStream os = new PrintStream(bos, true);
545 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
546 assertEquals("Incorrect char written", 't', bis.read());
550 * @tests java.io.PrintStream#append(char)
552 public void test_appendChar() throws IOException {
554 ByteArrayOutputStream out = new ByteArrayOutputStream();
555 PrintStream printStream = new PrintStream(out);
556 printStream.append(testChar);
558 assertEquals(String.valueOf(testChar), out.toString());
563 * @tests java.io.PrintStream#append(CharSequence)
565 public void test_appendCharSequence() {
566 String testString = "My Test String";
567 ByteArrayOutputStream out = new ByteArrayOutputStream();
568 PrintStream printStream = new PrintStream(out);
569 printStream.append(testString);
571 assertEquals(testString, out.toString());
576 * @tests java.io.PrintStream#append(CharSequence, int, int)
578 public void test_appendCharSequenceIntInt() {
579 String testString = "My Test String";
580 ByteArrayOutputStream out = new ByteArrayOutputStream();
581 PrintStream printStream = new PrintStream(out);
582 printStream.append(testString, 1, 3);
584 assertEquals(testString.substring(1, 3), out.toString());
589 * @tests java.io.PrintStream#format(java.lang.String, java.lang.Object...)
591 public void test_formatLjava_lang_String$Ljava_lang_Object() {
592 PrintStream os = new PrintStream(bos, false);
593 os.format("%s %s", "Hello", "World");
595 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
596 byte[] rbytes = new byte[11];
597 bis.read(rbytes, 0, rbytes.length);
598 assertEquals("Wrote incorrect string", "Hello World",
604 * @tests java.io.PrintStream#format(java.util.Locale, java.lang.String,
605 * java.lang.Object...)
607 public void test_formatLjava_util_Locale_Ljava_lang_String_$Ljava_lang_Object() {
608 PrintStream os = new PrintStream(bos, false);
609 os.format(Locale.US, "%s %s", "Hello", "World");
611 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
612 byte[] rbytes = new byte[11];
613 bis.read(rbytes, 0, rbytes.length);
614 assertEquals("Wrote incorrect string", "Hello World",
619 * @tests java.io.PrintStream#printf(java.lang.String, java.lang.Object...)
621 public void test_printfLjava_lang_String$Ljava_lang_Object() {
622 PrintStream os = new PrintStream(bos, false);
623 os.printf("%s %s", "Hello", "World");
625 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
626 byte[] rbytes = new byte[11];
627 bis.read(rbytes, 0, rbytes.length);
628 assertEquals("Wrote incorrect string", "Hello World",
633 * @tests java.io.PrintStream#printf(java.util.Locale, java.lang.String,
634 * java.lang.Object...)
636 public void test_printfLjava_util_Locale_Ljava_lang_String_$Ljava_lang_Object() {
637 PrintStream os = new PrintStream(bos, false);
638 os.printf(Locale.US, "%s %s", "Hello", "World");
640 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
641 byte[] rbytes = new byte[11];
642 bis.read(rbytes, 0, rbytes.length);
643 assertEquals("Wrote incorrect string", "Hello World",
648 protected void setUp() throws Exception {
650 testFile = File.createTempFile("test", null);
651 testFilePath = testFile.getAbsolutePath();
655 protected void tearDown() throws Exception {