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.BufferedReader;
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.io.OutputStream;
27 import java.io.PrintWriter;
28 import java.nio.charset.Charset;
29 import java.util.Locale;
31 import tests.support.Support_StringReader;
32 import tests.support.Support_StringWriter;
34 public class PrintWriterTest extends junit.framework.TestCase {
37 public String toString() {
45 static class MockPrintWriter extends PrintWriter {
47 public MockPrintWriter(OutputStream out, boolean autoflush) {
48 super(out, autoflush);
52 public void clearError() {
60 ByteArrayOutputStream bao;
62 ByteArrayInputStream bai;
67 * @tests java.io.PrintWriter#PrintWriter(java.io.OutputStream)
69 public void test_ConstructorLjava_io_OutputStream() {
70 // Test for method java.io.PrintWriter(java.io.OutputStream)
72 pw.println("Random Chars");
73 pw.write("Hello World");
76 br = new BufferedReader(new Support_StringReader(bao.toString()));
78 assertTrue("Incorrect string written/read: " + s, s
79 .equals("Random Chars"));
81 assertTrue("Incorrect string written/read: " + s, s
82 .equals("Hello World"));
83 } catch (IOException e) {
84 fail("IOException during test : " + e.getMessage());
89 * @tests java.io.PrintWriter#PrintWriter(java.io.OutputStream, boolean)
91 public void test_ConstructorLjava_io_OutputStreamZ() {
92 // Test for method java.io.PrintWriter(java.io.OutputStream, boolean)
94 pw = new PrintWriter(bao, true);
95 pw.println("Random Chars");
96 pw.write("Hello World");
98 br = new BufferedReader(new Support_StringReader(bao.toString()));
100 assertTrue("Incorrect string written/read: " + s, s
101 .equals("Random Chars"));
103 br = new BufferedReader(new Support_StringReader(bao.toString()));
105 assertTrue("Incorrect string written/read: " + s, s
106 .equals("Random Chars"));
108 assertTrue("Incorrect string written/read: " + s, s
109 .equals("Hello World"));
110 } catch (IOException e) {
111 fail("IOException during test : " + e.getMessage());
116 * @tests java.io.PrintWriter#PrintWriter(java.io.Writer)
118 public void test_ConstructorLjava_io_Writer() {
119 // Test for method java.io.PrintWriter(java.io.Writer)
120 Support_StringWriter sw;
121 pw = new PrintWriter(sw = new Support_StringWriter());
124 assertEquals("Failed to construct proper writer",
125 "Hello", sw.toString());
129 * @tests java.io.PrintWriter#PrintWriter(java.io.Writer, boolean)
131 public void test_ConstructorLjava_io_WriterZ() {
132 // Test for method java.io.PrintWriter(java.io.Writer, boolean)
133 Support_StringWriter sw;
134 pw = new PrintWriter(sw = new Support_StringWriter(), true);
136 // Auto-flush should have happened
137 assertEquals("Failed to construct proper writer",
138 "Hello", sw.toString());
142 * @tests java.io.PrintWriter#PrintWriter(java.io.File)
144 public void test_ConstructorLjava_io_File() throws Exception {
145 File file = File.createTempFile(getClass().getName(), null);
147 PrintWriter writer = new PrintWriter(file);
155 * @tests java.io.PrintWriter#PrintWriter(java.io.File, java.lang.String)
157 public void test_ConstructorLjava_io_File_Ljava_lang_String() throws Exception {
158 File file = File.createTempFile(getClass().getName(), null);
160 PrintWriter writer = new PrintWriter(file,
161 Charset.defaultCharset().name());
169 * @tests java.io.PrintWriter#PrintWriter(java.lang.String)
171 public void test_ConstructorLjava_lang_String() throws Exception {
172 File file = File.createTempFile(getClass().getName(), null);
174 PrintWriter writer = new PrintWriter(file.getPath());
182 * @tests java.io.PrintWriter#PrintWriter(java.lang.String, java.lang.String)
184 public void test_ConstructorLjava_lang_String_Ljava_lang_String() throws Exception {
185 File file = File.createTempFile(getClass().getName(), null);
187 PrintWriter writer = new PrintWriter(file.getPath(),
188 Charset.defaultCharset().name());
196 * @tests java.io.PrintWriter#checkError()
198 public void test_checkError() {
199 // Test for method boolean java.io.PrintWriter.checkError()
201 pw.print(490000000000.08765);
202 assertTrue("Failed to return error", pw.checkError());
206 * @tests java.io.PrintWriter#clearError()
209 public void test_clearError() {
210 // Test for method boolean java.io.PrintWriter.clearError()
211 MockPrintWriter mpw = new MockPrintWriter(new ByteArrayOutputStream(), false);
213 mpw.print(490000000000.08765);
214 assertTrue("Failed to return error", mpw.checkError());
216 assertFalse("Internal error state has not be cleared", mpw.checkError());
220 * @tests java.io.PrintWriter#close()
222 public void test_close() {
223 // Test for method void java.io.PrintWriter.close()
226 assertTrue("Write on closed stream failed to generate error", pw
231 * @tests java.io.PrintWriter#flush()
233 public void test_flush() {
234 // Test for method void java.io.PrintWriter.flush()
235 final double dub = 490000000000.08765;
238 assertTrue("Failed to flush", new String(bao.toByteArray())
239 .equals(String.valueOf(dub)));
243 * @tests java.io.PrintWriter#print(char[])
245 public void test_print$C() {
246 // Test for method void java.io.PrintWriter.print(char [])
248 char[] schars = new char[11];
249 "Hello World".getChars(0, 11, schars, 0);
253 br = new BufferedReader(new Support_StringReader(bao.toString()));
255 } catch (IOException e) {
256 fail("IOException during test : " + e.getMessage());
258 assertTrue("Wrote incorrect char[] string: " + s, s
259 .equals("Hello World"));
262 pw.print((char[]) null);
263 } catch (NullPointerException e) {
266 assertEquals("null pointer exception for printing null char[] is not caught",
271 * @tests java.io.PrintWriter#print(char)
273 public void test_printC() {
274 // Test for method void java.io.PrintWriter.print(char)
277 assertEquals("Wrote incorrect char string", "c", new String(bao.toByteArray())
282 * @tests java.io.PrintWriter#print(double)
284 public void test_printD() {
285 // Test for method void java.io.PrintWriter.print(double)
286 final double dub = 490000000000.08765;
289 assertTrue("Wrote incorrect double string", new String(bao
290 .toByteArray()).equals(String.valueOf(dub)));
294 * @tests java.io.PrintWriter#print(float)
296 public void test_printF() {
297 // Test for method void java.io.PrintWriter.print(float)
298 final float flo = 49.08765f;
301 assertTrue("Wrote incorrect float string",
302 new String(bao.toByteArray()).equals(String.valueOf(flo)));
306 * @tests java.io.PrintWriter#print(int)
308 public void test_printI() {
309 // Test for method void java.io.PrintWriter.print(int)
312 assertEquals("Wrote incorrect int string", "4908765", new String(bao.toByteArray())
317 * @tests java.io.PrintWriter#print(long)
319 public void test_printJ() {
320 // Test for method void java.io.PrintWriter.print(long)
321 pw.print(49087650000L);
323 assertEquals("Wrote incorrect long string", "49087650000", new String(bao.toByteArray())
328 * @tests java.io.PrintWriter#print(java.lang.Object)
330 public void test_printLjava_lang_Object() {
331 // Test for method void java.io.PrintWriter.print(java.lang.Object)
332 pw.print((Object) null);
334 assertEquals("Did not write null", "null", new String(bao.toByteArray())
338 pw.print(new Bogus());
340 assertEquals("Wrote in incorrect Object string", "Bogus", new String(bao
345 * @tests java.io.PrintWriter#print(java.lang.String)
347 public void test_printLjava_lang_String() {
348 // Test for method void java.io.PrintWriter.print(java.lang.String)
349 pw.print((String) null);
351 assertEquals("did not write null", "null", new String(bao.toByteArray())
355 pw.print("Hello World");
357 assertEquals("Wrote incorrect string", "Hello World", new String(bao.toByteArray())
362 * @tests java.io.PrintWriter#print(boolean)
364 public void test_printZ() {
365 // Test for method void java.io.PrintWriter.print(boolean)
368 assertEquals("Wrote in incorrect boolean string", "true", new String(bao
373 * @tests java.io.PrintWriter#println()
375 public void test_println() {
376 // Test for method void java.io.PrintWriter.println()
383 br = new BufferedReader(new Support_StringReader(bao.toString()));
385 assertTrue("Wrote incorrect line: " + s, s.equals("Blarg"));
387 assertTrue("Wrote incorrect line: " + s, s.equals(""));
389 assertTrue("Wrote incorrect line: " + s, s.equals("Bleep"));
390 } catch (IOException e) {
391 fail("IOException during test : " + e.getMessage());
396 * @tests java.io.PrintWriter#println(char[])
398 public void test_println$C() {
399 // Test for method void java.io.PrintWriter.println(char [])
401 char[] schars = new char[11];
402 "Hello World".getChars(0, 11, schars, 0);
403 pw.println("Random Chars");
407 br = new BufferedReader(new Support_StringReader(bao.toString()));
410 } catch (IOException e) {
411 fail("IOException during test : " + e.getMessage());
413 assertTrue("Wrote incorrect char[] string: " + s, s
414 .equals("Hello World"));
418 * @tests java.io.PrintWriter#println(char)
420 public void test_printlnC() {
421 // Test for method void java.io.PrintWriter.println(char)
423 pw.println("Random Chars");
427 br = new BufferedReader(new Support_StringReader(bao.toString()));
430 } catch (IOException e) {
431 fail("IOException during test : " + e.getMessage());
433 assertTrue("Wrote incorrect char string: " + s, s.equals("c"));
437 * @tests java.io.PrintWriter#println(double)
439 public void test_printlnD() {
440 // Test for method void java.io.PrintWriter.println(double)
442 final double dub = 4000000000000000.657483;
443 pw.println("Random Chars");
447 br = new BufferedReader(new Support_StringReader(bao.toString()));
450 } catch (IOException e) {
451 fail("IOException during test : " + e.getMessage());
453 assertTrue("Wrote incorrect double string: " + s, s.equals(String
458 * @tests java.io.PrintWriter#println(float)
460 public void test_printlnF() {
461 // Test for method void java.io.PrintWriter.println(float)
463 final float flo = 40.4646464f;
464 pw.println("Random Chars");
468 br = new BufferedReader(new Support_StringReader(bao.toString()));
471 assertTrue("Wrote incorrect float string: " + s + " wanted: "
472 + String.valueOf(flo), s.equals(String.valueOf(flo)));
473 } catch (IOException e) {
474 fail("IOException during test : " + e.getMessage());
480 * @tests java.io.PrintWriter#println(int)
482 public void test_printlnI() {
483 // Test for method void java.io.PrintWriter.println(int)
485 pw.println("Random Chars");
489 br = new BufferedReader(new Support_StringReader(bao.toString()));
492 } catch (IOException e) {
493 fail("IOException during test : " + e.getMessage());
495 assertTrue("Wrote incorrect int string: " + s, s.equals("400000"));
499 * @tests java.io.PrintWriter#println(long)
501 public void test_printlnJ() {
502 // Test for method void java.io.PrintWriter.println(long)
504 pw.println("Random Chars");
505 pw.println(4000000000000L);
508 br = new BufferedReader(new Support_StringReader(bao.toString()));
511 } catch (IOException e) {
512 fail("IOException during test : " + e.getMessage());
514 assertTrue("Wrote incorrect long string: " + s, s
515 .equals("4000000000000"));
519 * @tests java.io.PrintWriter#println(java.lang.Object)
521 public void test_printlnLjava_lang_Object() {
522 // Test for method void java.io.PrintWriter.println(java.lang.Object)
524 pw.println("Random Chars");
525 pw.println(new Bogus());
528 br = new BufferedReader(new Support_StringReader(bao.toString()));
531 } catch (IOException e) {
532 fail("IOException during test : " + e.getMessage());
534 assertTrue("Wrote incorrect Object string: " + s, s.equals("Bogus"));
538 * @tests java.io.PrintWriter#println(java.lang.String)
540 public void test_printlnLjava_lang_String() {
541 // Test for method void java.io.PrintWriter.println(java.lang.String)
543 pw.println("Random Chars");
544 pw.println("Hello World");
547 br = new BufferedReader(new Support_StringReader(bao.toString()));
550 } catch (IOException e) {
551 fail("IOException during test : " + e.getMessage());
553 assertTrue("Wrote incorrect string: " + s, s.equals("Hello World"));
557 * @tests java.io.PrintWriter#println(boolean)
559 public void test_printlnZ() {
560 // Test for method void java.io.PrintWriter.println(boolean)
562 pw.println("Random Chars");
566 br = new BufferedReader(new Support_StringReader(bao.toString()));
569 } catch (IOException e) {
570 fail("IOException during test : " + e.getMessage());
572 assertTrue("Wrote incorrect boolean string: " + s, s.equals("false"));
576 * @tests java.io.PrintWriter#write(char[])
578 public void test_write$C() {
579 // Test for method void java.io.PrintWriter.write(char [])
581 char[] schars = new char[11];
582 "Hello World".getChars(0, 11, schars, 0);
583 pw.println("Random Chars");
587 br = new BufferedReader(new Support_StringReader(bao.toString()));
590 } catch (IOException e) {
591 fail("IOException during test: " + e.getMessage());
593 assertTrue("Wrote incorrect char[] string: " + s, s
594 .equals("Hello World"));
598 * @tests java.io.PrintWriter#write(char[], int, int)
600 public void test_write$CII() {
601 // Test for method void java.io.PrintWriter.write(char [], int, int)
603 char[] schars = new char[11];
604 "Hello World".getChars(0, 11, schars, 0);
605 pw.println("Random Chars");
606 pw.write(schars, 6, 5);
609 br = new BufferedReader(new Support_StringReader(bao.toString()));
612 } catch (IOException e) {
613 fail("IOException during test : " + e.getMessage());
615 assertTrue("Wrote incorrect char[] string: " + s, s.equals("World"));
619 * @tests java.io.PrintWriter#write(int)
621 public void test_writeI() throws IOException {
622 // Test for method void java.io.PrintWriter.write(int)
623 char[] cab = new char[3];
628 InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(bao.toByteArray()));
629 cab[0] = (char) isr.read();
630 cab[1] = (char) isr.read();
631 cab[2] = (char) isr.read();
632 assertTrue("Wrote incorrect ints", cab[0] == 'a' && cab[1] == 'b'
638 * @tests java.io.PrintWriter#write(java.lang.String)
640 public void test_writeLjava_lang_String() {
641 // Test for method void java.io.PrintWriter.write(java.lang.String)
643 pw.println("Random Chars");
644 pw.write("Hello World");
647 br = new BufferedReader(new Support_StringReader(bao.toString()));
650 } catch (IOException e) {
651 fail("IOException during test : " + e.getMessage());
653 assertTrue("Wrote incorrect char[] string: " + s, s
654 .equals("Hello World"));
658 * @tests java.io.PrintWriter#write(java.lang.String, int, int)
660 public void test_writeLjava_lang_StringII() {
661 // Test for method void java.io.PrintWriter.write(java.lang.String, int,
664 pw.println("Random Chars");
665 pw.write("Hello World", 6, 5);
668 br = new BufferedReader(new Support_StringReader(bao.toString()));
671 } catch (IOException e) {
672 fail("IOException during test : " + e.getMessage());
674 assertTrue("Wrote incorrect char[] string: " + s, s.equals("World"));
678 * @tests java.io.PrintWriter#append(char)
680 public void test_appendChar() {
682 ByteArrayOutputStream out = new ByteArrayOutputStream();
683 PrintWriter printWriter = new PrintWriter(out);
684 printWriter.append(testChar);
686 assertEquals(String.valueOf(testChar),out.toString());
690 * @tests java.io.PrintWriter#append(CharSequence)
692 public void test_appendCharSequence() {
694 String testString = "My Test String";
695 ByteArrayOutputStream out = new ByteArrayOutputStream();
696 PrintWriter printWriter = new PrintWriter(out);
697 printWriter.append(testString);
699 assertEquals(testString, out.toString());
705 * @tests java.io.PrintWriter#append(CharSequence, int, int)
707 public void test_appendCharSequenceIntInt() {
708 String testString = "My Test String";
709 ByteArrayOutputStream out = new ByteArrayOutputStream();
710 PrintWriter printWriter = new PrintWriter(out);
711 printWriter.append(testString, 1, 3);
713 assertEquals(testString.substring(1, 3), out.toString());
719 * @tests java.io.PrintWriter#format(java.lang.String, java.lang.Object...)
721 public void test_formatLjava_lang_String$Ljava_lang_Object() {
722 pw.format("%s %s", "Hello", "World");
724 assertEquals("Wrote incorrect string", "Hello World",
725 new String(bao.toByteArray()));
729 * @tests java.io.PrintWriter#format(java.util.Locale, java.lang.String, java.lang.Object...)
731 public void test_formatLjava_util_Locale_Ljava_lang_String_$Ljava_lang_Object() {
732 pw.format(Locale.US, "%s %s", "Hello", "World");
734 assertEquals("Wrote incorrect string", "Hello World",
735 new String(bao.toByteArray()));
739 * @tests java.io.PrintWriter#printf(java.lang.String, java.lang.Object...)
741 public void test_printfLjava_lang_String$Ljava_lang_Object() {
742 pw.printf("%s %s", "Hello", "World");
744 assertEquals("Wrote incorrect string", "Hello World",
745 new String(bao.toByteArray()));
749 * @tests java.io.PrintWriter#printf(java.util.Locale, java.lang.String, java.lang.Object...)
751 public void test_printfLjava_util_Locale_Ljava_lang_String_$Ljava_lang_Object() {
752 pw.printf(Locale.US, "%s %s", "Hello", "World");
754 assertEquals("Wrote incorrect string", "Hello World",
755 new String(bao.toByteArray()));
759 * Sets up the fixture, for example, open a network connection. This method
760 * is called before a test is executed.
762 protected void setUp() {
763 bao = new ByteArrayOutputStream();
764 pw = new PrintWriter(bao, false);
769 * Tears down the fixture, for example, close a network connection. This
770 * method is called after a test is executed.
772 protected void tearDown() {
775 } catch (Exception e) {