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.BufferedReader;
021: import java.io.ByteArrayInputStream;
022: import java.io.ByteArrayOutputStream;
023: import java.io.File;
024: import java.io.IOException;
025: import java.io.PrintWriter;
026: import java.nio.charset.Charset;
027: import java.util.Locale;
028:
029: import tests.support.Support_StringReader;
030: import tests.support.Support_StringWriter;
031:
032: public class PrintWriterTest extends junit.framework.TestCase {
033:
034: static class Bogus {
035: public String toString() {
036: return "Bogus";
037: }
038: }
039:
040: PrintWriter pw;
041:
042: ByteArrayOutputStream bao;
043:
044: ByteArrayInputStream bai;
045:
046: BufferedReader br;
047:
048: /**
049: * @tests java.io.PrintWriter#PrintWriter(java.io.OutputStream)
050: */
051: public void test_ConstructorLjava_io_OutputStream() {
052: // Test for method java.io.PrintWriter(java.io.OutputStream)
053: String s;
054: pw.println("Random Chars");
055: pw.write("Hello World");
056: pw.flush();
057: try {
058: br = new BufferedReader(new Support_StringReader(bao
059: .toString()));
060: s = br.readLine();
061: assertTrue("Incorrect string written/read: " + s, s
062: .equals("Random Chars"));
063: s = br.readLine();
064: assertTrue("Incorrect string written/read: " + s, s
065: .equals("Hello World"));
066: } catch (IOException e) {
067: fail("IOException during test : " + e.getMessage());
068: }
069: }
070:
071: /**
072: * @tests java.io.PrintWriter#PrintWriter(java.io.OutputStream, boolean)
073: */
074: public void test_ConstructorLjava_io_OutputStreamZ() {
075: // Test for method java.io.PrintWriter(java.io.OutputStream, boolean)
076: String s;
077: pw = new PrintWriter(bao, true);
078: pw.println("Random Chars");
079: pw.write("Hello World");
080: try {
081: br = new BufferedReader(new Support_StringReader(bao
082: .toString()));
083: s = br.readLine();
084: assertTrue("Incorrect string written/read: " + s, s
085: .equals("Random Chars"));
086: pw.flush();
087: br = new BufferedReader(new Support_StringReader(bao
088: .toString()));
089: s = br.readLine();
090: assertTrue("Incorrect string written/read: " + s, s
091: .equals("Random Chars"));
092: s = br.readLine();
093: assertTrue("Incorrect string written/read: " + s, s
094: .equals("Hello World"));
095: } catch (IOException e) {
096: fail("IOException during test : " + e.getMessage());
097: }
098: }
099:
100: /**
101: * @tests java.io.PrintWriter#PrintWriter(java.io.Writer)
102: */
103: public void test_ConstructorLjava_io_Writer() {
104: // Test for method java.io.PrintWriter(java.io.Writer)
105: Support_StringWriter sw;
106: pw = new PrintWriter(sw = new Support_StringWriter());
107: pw.print("Hello");
108: pw.flush();
109: assertEquals("Failed to construct proper writer", "Hello", sw
110: .toString());
111: }
112:
113: /**
114: * @tests java.io.PrintWriter#PrintWriter(java.io.Writer, boolean)
115: */
116: public void test_ConstructorLjava_io_WriterZ() {
117: // Test for method java.io.PrintWriter(java.io.Writer, boolean)
118: Support_StringWriter sw;
119: pw = new PrintWriter(sw = new Support_StringWriter(), true);
120: pw.print("Hello");
121: // Auto-flush should have happened
122: assertEquals("Failed to construct proper writer", "Hello", sw
123: .toString());
124: }
125:
126: /**
127: * @tests java.io.PrintWriter#PrintWriter(java.io.File)
128: */
129: public void test_ConstructorLjava_io_File() throws Exception {
130: File file = File.createTempFile(getClass().getName(), null);
131: try {
132: PrintWriter writer = new PrintWriter(file);
133: writer.close();
134: } finally {
135: file.delete();
136: }
137: }
138:
139: /**
140: * @tests java.io.PrintWriter#PrintWriter(java.io.File, java.lang.String)
141: */
142: public void test_ConstructorLjava_io_File_Ljava_lang_String()
143: throws Exception {
144: File file = File.createTempFile(getClass().getName(), null);
145: try {
146: PrintWriter writer = new PrintWriter(file, Charset
147: .defaultCharset().name());
148: writer.close();
149: } finally {
150: file.delete();
151: }
152: }
153:
154: /**
155: * @tests java.io.PrintWriter#PrintWriter(java.lang.String)
156: */
157: public void test_ConstructorLjava_lang_String() throws Exception {
158: File file = File.createTempFile(getClass().getName(), null);
159: try {
160: PrintWriter writer = new PrintWriter(file.getPath());
161: writer.close();
162: } finally {
163: file.delete();
164: }
165: }
166:
167: /**
168: * @tests java.io.PrintWriter#PrintWriter(java.lang.String, java.lang.String)
169: */
170: public void test_ConstructorLjava_lang_String_Ljava_lang_String()
171: throws Exception {
172: File file = File.createTempFile(getClass().getName(), null);
173: try {
174: PrintWriter writer = new PrintWriter(file.getPath(),
175: Charset.defaultCharset().name());
176: writer.close();
177: } finally {
178: file.delete();
179: }
180: }
181:
182: /**
183: * @tests java.io.PrintWriter#checkError()
184: */
185: public void test_checkError() {
186: // Test for method boolean java.io.PrintWriter.checkError()
187: pw.close();
188: pw.print(490000000000.08765);
189: assertTrue("Failed to return error", pw.checkError());
190: }
191:
192: /**
193: * @tests java.io.PrintWriter#close()
194: */
195: public void test_close() {
196: // Test for method void java.io.PrintWriter.close()
197: pw.close();
198: pw.println("l");
199: assertTrue("Write on closed stream failed to generate error",
200: pw.checkError());
201: }
202:
203: /**
204: * @tests java.io.PrintWriter#flush()
205: */
206: public void test_flush() {
207: // Test for method void java.io.PrintWriter.flush()
208: final double dub = 490000000000.08765;
209: pw.print(dub);
210: pw.flush();
211: assertTrue("Failed to flush", new String(bao.toByteArray())
212: .equals(String.valueOf(dub)));
213: }
214:
215: /**
216: * @tests java.io.PrintWriter#print(char[])
217: */
218: public void test_print$C() {
219: // Test for method void java.io.PrintWriter.print(char [])
220: String s = null;
221: char[] schars = new char[11];
222: "Hello World".getChars(0, 11, schars, 0);
223: pw.print(schars);
224: pw.flush();
225: try {
226: br = new BufferedReader(new Support_StringReader(bao
227: .toString()));
228: s = br.readLine();
229: } catch (IOException e) {
230: fail("IOException during test : " + e.getMessage());
231: }
232: assertTrue("Wrote incorrect char[] string: " + s, s
233: .equals("Hello World"));
234: int r = 0;
235: try {
236: pw.print((char[]) null);
237: } catch (NullPointerException e) {
238: r = 1;
239: }
240: assertEquals(
241: "null pointer exception for printing null char[] is not caught",
242: 1, r);
243: }
244:
245: /**
246: * @tests java.io.PrintWriter#print(char)
247: */
248: public void test_printC() {
249: // Test for method void java.io.PrintWriter.print(char)
250: pw.print('c');
251: pw.flush();
252: assertEquals("Wrote incorrect char string", "c", new String(bao
253: .toByteArray()));
254: }
255:
256: /**
257: * @tests java.io.PrintWriter#print(double)
258: */
259: public void test_printD() {
260: // Test for method void java.io.PrintWriter.print(double)
261: final double dub = 490000000000.08765;
262: pw.print(dub);
263: pw.flush();
264: assertTrue("Wrote incorrect double string", new String(bao
265: .toByteArray()).equals(String.valueOf(dub)));
266: }
267:
268: /**
269: * @tests java.io.PrintWriter#print(float)
270: */
271: public void test_printF() {
272: // Test for method void java.io.PrintWriter.print(float)
273: final float flo = 49.08765f;
274: pw.print(flo);
275: pw.flush();
276: assertTrue("Wrote incorrect float string", new String(bao
277: .toByteArray()).equals(String.valueOf(flo)));
278: }
279:
280: /**
281: * @tests java.io.PrintWriter#print(int)
282: */
283: public void test_printI() {
284: // Test for method void java.io.PrintWriter.print(int)
285: pw.print(4908765);
286: pw.flush();
287: assertEquals("Wrote incorrect int string", "4908765",
288: new String(bao.toByteArray()));
289: }
290:
291: /**
292: * @tests java.io.PrintWriter#print(long)
293: */
294: public void test_printJ() {
295: // Test for method void java.io.PrintWriter.print(long)
296: pw.print(49087650000L);
297: pw.flush();
298: assertEquals("Wrote incorrect long string", "49087650000",
299: new String(bao.toByteArray()));
300: }
301:
302: /**
303: * @tests java.io.PrintWriter#print(java.lang.Object)
304: */
305: public void test_printLjava_lang_Object() {
306: // Test for method void java.io.PrintWriter.print(java.lang.Object)
307: pw.print((Object) null);
308: pw.flush();
309: assertEquals("Did not write null", "null", new String(bao
310: .toByteArray()));
311: bao.reset();
312:
313: pw.print(new Bogus());
314: pw.flush();
315: assertEquals("Wrote in incorrect Object string", "Bogus",
316: new String(bao.toByteArray()));
317: }
318:
319: /**
320: * @tests java.io.PrintWriter#print(java.lang.String)
321: */
322: public void test_printLjava_lang_String() {
323: // Test for method void java.io.PrintWriter.print(java.lang.String)
324: pw.print((String) null);
325: pw.flush();
326: assertEquals("did not write null", "null", new String(bao
327: .toByteArray()));
328: bao.reset();
329:
330: pw.print("Hello World");
331: pw.flush();
332: assertEquals("Wrote incorrect string", "Hello World",
333: new String(bao.toByteArray()));
334: }
335:
336: /**
337: * @tests java.io.PrintWriter#print(boolean)
338: */
339: public void test_printZ() {
340: // Test for method void java.io.PrintWriter.print(boolean)
341: pw.print(true);
342: pw.flush();
343: assertEquals("Wrote in incorrect boolean string", "true",
344: new String(bao.toByteArray()));
345: }
346:
347: /**
348: * @tests java.io.PrintWriter#println()
349: */
350: public void test_println() {
351: // Test for method void java.io.PrintWriter.println()
352: String s;
353: pw.println("Blarg");
354: pw.println();
355: pw.println("Bleep");
356: pw.flush();
357: try {
358: br = new BufferedReader(new Support_StringReader(bao
359: .toString()));
360: s = br.readLine();
361: assertTrue("Wrote incorrect line: " + s, s.equals("Blarg"));
362: s = br.readLine();
363: assertTrue("Wrote incorrect line: " + s, s.equals(""));
364: s = br.readLine();
365: assertTrue("Wrote incorrect line: " + s, s.equals("Bleep"));
366: } catch (IOException e) {
367: fail("IOException during test : " + e.getMessage());
368: }
369: }
370:
371: /**
372: * @tests java.io.PrintWriter#println(char[])
373: */
374: public void test_println$C() {
375: // Test for method void java.io.PrintWriter.println(char [])
376: String s = null;
377: char[] schars = new char[11];
378: "Hello World".getChars(0, 11, schars, 0);
379: pw.println("Random Chars");
380: pw.println(schars);
381: pw.flush();
382: try {
383: br = new BufferedReader(new Support_StringReader(bao
384: .toString()));
385: s = br.readLine();
386: s = br.readLine();
387: } catch (IOException e) {
388: fail("IOException during test : " + e.getMessage());
389: }
390: assertTrue("Wrote incorrect char[] string: " + s, s
391: .equals("Hello World"));
392: }
393:
394: /**
395: * @tests java.io.PrintWriter#println(char)
396: */
397: public void test_printlnC() {
398: // Test for method void java.io.PrintWriter.println(char)
399: String s = null;
400: pw.println("Random Chars");
401: pw.println('c');
402: pw.flush();
403: try {
404: br = new BufferedReader(new Support_StringReader(bao
405: .toString()));
406: s = br.readLine();
407: s = br.readLine();
408: } catch (IOException e) {
409: fail("IOException during test : " + e.getMessage());
410: }
411: assertTrue("Wrote incorrect char string: " + s, s.equals("c"));
412: }
413:
414: /**
415: * @tests java.io.PrintWriter#println(double)
416: */
417: public void test_printlnD() {
418: // Test for method void java.io.PrintWriter.println(double)
419: String s = null;
420: final double dub = 4000000000000000.657483;
421: pw.println("Random Chars");
422: pw.println(dub);
423: pw.flush();
424: try {
425: br = new BufferedReader(new Support_StringReader(bao
426: .toString()));
427: br.readLine();
428: s = br.readLine();
429: } catch (IOException e) {
430: fail("IOException during test : " + e.getMessage());
431: }
432: assertTrue("Wrote incorrect double string: " + s, s
433: .equals(String.valueOf(dub)));
434: }
435:
436: /**
437: * @tests java.io.PrintWriter#println(float)
438: */
439: public void test_printlnF() {
440: // Test for method void java.io.PrintWriter.println(float)
441: String s;
442: final float flo = 40.4646464f;
443: pw.println("Random Chars");
444: pw.println(flo);
445: pw.flush();
446: try {
447: br = new BufferedReader(new Support_StringReader(bao
448: .toString()));
449: br.readLine();
450: s = br.readLine();
451: assertTrue("Wrote incorrect float string: " + s
452: + " wanted: " + String.valueOf(flo), s
453: .equals(String.valueOf(flo)));
454: } catch (IOException e) {
455: fail("IOException during test : " + e.getMessage());
456: }
457:
458: }
459:
460: /**
461: * @tests java.io.PrintWriter#println(int)
462: */
463: public void test_printlnI() {
464: // Test for method void java.io.PrintWriter.println(int)
465: String s = null;
466: pw.println("Random Chars");
467: pw.println(400000);
468: pw.flush();
469: try {
470: br = new BufferedReader(new Support_StringReader(bao
471: .toString()));
472: br.readLine();
473: s = br.readLine();
474: } catch (IOException e) {
475: fail("IOException during test : " + e.getMessage());
476: }
477: assertTrue("Wrote incorrect int string: " + s, s
478: .equals("400000"));
479: }
480:
481: /**
482: * @tests java.io.PrintWriter#println(long)
483: */
484: public void test_printlnJ() {
485: // Test for method void java.io.PrintWriter.println(long)
486: String s = null;
487: pw.println("Random Chars");
488: pw.println(4000000000000L);
489: pw.flush();
490: try {
491: br = new BufferedReader(new Support_StringReader(bao
492: .toString()));
493: br.readLine();
494: s = br.readLine();
495: } catch (IOException e) {
496: fail("IOException during test : " + e.getMessage());
497: }
498: assertTrue("Wrote incorrect long string: " + s, s
499: .equals("4000000000000"));
500: }
501:
502: /**
503: * @tests java.io.PrintWriter#println(java.lang.Object)
504: */
505: public void test_printlnLjava_lang_Object() {
506: // Test for method void java.io.PrintWriter.println(java.lang.Object)
507: String s = null;
508: pw.println("Random Chars");
509: pw.println(new Bogus());
510: pw.flush();
511: try {
512: br = new BufferedReader(new Support_StringReader(bao
513: .toString()));
514: br.readLine();
515: s = br.readLine();
516: } catch (IOException e) {
517: fail("IOException during test : " + e.getMessage());
518: }
519: assertTrue("Wrote incorrect Object string: " + s, s
520: .equals("Bogus"));
521: }
522:
523: /**
524: * @tests java.io.PrintWriter#println(java.lang.String)
525: */
526: public void test_printlnLjava_lang_String() {
527: // Test for method void java.io.PrintWriter.println(java.lang.String)
528: String s = null;
529: pw.println("Random Chars");
530: pw.println("Hello World");
531: pw.flush();
532: try {
533: br = new BufferedReader(new Support_StringReader(bao
534: .toString()));
535: br.readLine();
536: s = br.readLine();
537: } catch (IOException e) {
538: fail("IOException during test : " + e.getMessage());
539: }
540: assertTrue("Wrote incorrect string: " + s, s
541: .equals("Hello World"));
542: }
543:
544: /**
545: * @tests java.io.PrintWriter#println(boolean)
546: */
547: public void test_printlnZ() {
548: // Test for method void java.io.PrintWriter.println(boolean)
549: String s = null;
550: pw.println("Random Chars");
551: pw.println(false);
552: pw.flush();
553: try {
554: br = new BufferedReader(new Support_StringReader(bao
555: .toString()));
556: br.readLine();
557: s = br.readLine();
558: } catch (IOException e) {
559: fail("IOException during test : " + e.getMessage());
560: }
561: assertTrue("Wrote incorrect boolean string: " + s, s
562: .equals("false"));
563: }
564:
565: /**
566: * @tests java.io.PrintWriter#write(char[])
567: */
568: public void test_write$C() {
569: // Test for method void java.io.PrintWriter.write(char [])
570: String s = null;
571: char[] schars = new char[11];
572: "Hello World".getChars(0, 11, schars, 0);
573: pw.println("Random Chars");
574: pw.write(schars);
575: pw.flush();
576: try {
577: br = new BufferedReader(new Support_StringReader(bao
578: .toString()));
579: br.readLine();
580: s = br.readLine();
581: } catch (IOException e) {
582: fail("IOException during test: " + e.getMessage());
583: }
584: assertTrue("Wrote incorrect char[] string: " + s, s
585: .equals("Hello World"));
586: }
587:
588: /**
589: * @tests java.io.PrintWriter#write(char[], int, int)
590: */
591: public void test_write$CII() {
592: // Test for method void java.io.PrintWriter.write(char [], int, int)
593: String s = null;
594: char[] schars = new char[11];
595: "Hello World".getChars(0, 11, schars, 0);
596: pw.println("Random Chars");
597: pw.write(schars, 6, 5);
598: pw.flush();
599: try {
600: br = new BufferedReader(new Support_StringReader(bao
601: .toString()));
602: br.readLine();
603: s = br.readLine();
604: } catch (IOException e) {
605: fail("IOException during test : " + e.getMessage());
606: }
607: assertTrue("Wrote incorrect char[] string: " + s, s
608: .equals("World"));
609: }
610:
611: /**
612: * @tests java.io.PrintWriter#write(int)
613: */
614: public void test_writeI() {
615: // Test for method void java.io.PrintWriter.write(int)
616: char[] cab = new char[3];
617: pw.write('a');
618: pw.write('b');
619: pw.write('c');
620: pw.flush();
621: bai = new ByteArrayInputStream(bao.toByteArray());
622: cab[0] = (char) bai.read();
623: cab[1] = (char) bai.read();
624: cab[2] = (char) bai.read();
625: assertTrue("Wrote incorrect ints", cab[0] == 'a'
626: && cab[1] == 'b' && cab[2] == 'c');
627:
628: }
629:
630: /**
631: * @tests java.io.PrintWriter#write(java.lang.String)
632: */
633: public void test_writeLjava_lang_String() {
634: // Test for method void java.io.PrintWriter.write(java.lang.String)
635: String s = null;
636: pw.println("Random Chars");
637: pw.write("Hello World");
638: pw.flush();
639: try {
640: br = new BufferedReader(new Support_StringReader(bao
641: .toString()));
642: br.readLine();
643: s = br.readLine();
644: } catch (IOException e) {
645: fail("IOException during test : " + e.getMessage());
646: }
647: assertTrue("Wrote incorrect char[] string: " + s, s
648: .equals("Hello World"));
649: }
650:
651: /**
652: * @tests java.io.PrintWriter#write(java.lang.String, int, int)
653: */
654: public void test_writeLjava_lang_StringII() {
655: // Test for method void java.io.PrintWriter.write(java.lang.String, int,
656: // int)
657: String s = null;
658: pw.println("Random Chars");
659: pw.write("Hello World", 6, 5);
660: pw.flush();
661: try {
662: br = new BufferedReader(new Support_StringReader(bao
663: .toString()));
664: br.readLine();
665: s = br.readLine();
666: } catch (IOException e) {
667: fail("IOException during test : " + e.getMessage());
668: }
669: assertTrue("Wrote incorrect char[] string: " + s, s
670: .equals("World"));
671: }
672:
673: /**
674: * @tests java.io.PrintWriter#append(char)
675: */
676: public void test_appendChar() {
677: char testChar = ' ';
678: ByteArrayOutputStream out = new ByteArrayOutputStream();
679: PrintWriter printWriter = new PrintWriter(out);
680: printWriter.append(testChar);
681: printWriter.flush();
682: assertEquals(String.valueOf(testChar), out.toString());
683: printWriter.close();
684: }
685:
686: /**
687: * @tests java.io.PrintWriter#append(CharSequence)
688: */
689: public void test_appendCharSequence() {
690:
691: String testString = "My Test String";
692: ByteArrayOutputStream out = new ByteArrayOutputStream();
693: PrintWriter printWriter = new PrintWriter(out);
694: printWriter.append(testString);
695: printWriter.flush();
696: assertEquals(testString, out.toString());
697: printWriter.close();
698:
699: }
700:
701: /**
702: * @tests java.io.PrintWriter#append(CharSequence, int, int)
703: */
704: public void test_appendCharSequenceIntInt() {
705: String testString = "My Test String";
706: ByteArrayOutputStream out = new ByteArrayOutputStream();
707: PrintWriter printWriter = new PrintWriter(out);
708: printWriter.append(testString, 1, 3);
709: printWriter.flush();
710: assertEquals(testString.substring(1, 3), out.toString());
711: printWriter.close();
712:
713: }
714:
715: /**
716: * @tests java.io.PrintWriter#format(java.lang.String, java.lang.Object...)
717: */
718: public void test_formatLjava_lang_String$Ljava_lang_Object() {
719: pw.format("%s %s", "Hello", "World");
720: pw.flush();
721: assertEquals("Wrote incorrect string", "Hello World",
722: new String(bao.toByteArray()));
723: }
724:
725: /**
726: * @tests java.io.PrintWriter#format(java.util.Locale, java.lang.String, java.lang.Object...)
727: */
728: public void test_formatLjava_util_Locale_Ljava_lang_String_$Ljava_lang_Object() {
729: pw.format(Locale.US, "%s %s", "Hello", "World");
730: pw.flush();
731: assertEquals("Wrote incorrect string", "Hello World",
732: new String(bao.toByteArray()));
733: }
734:
735: /**
736: * @tests java.io.PrintWriter#printf(java.lang.String, java.lang.Object...)
737: */
738: public void test_printfLjava_lang_String$Ljava_lang_Object() {
739: pw.printf("%s %s", "Hello", "World");
740: pw.flush();
741: assertEquals("Wrote incorrect string", "Hello World",
742: new String(bao.toByteArray()));
743: }
744:
745: /**
746: * @tests java.io.PrintWriter#printf(java.util.Locale, java.lang.String, java.lang.Object...)
747: */
748: public void test_printfLjava_util_Locale_Ljava_lang_String_$Ljava_lang_Object() {
749: pw.printf(Locale.US, "%s %s", "Hello", "World");
750: pw.flush();
751: assertEquals("Wrote incorrect string", "Hello World",
752: new String(bao.toByteArray()));
753: }
754:
755: /**
756: * Sets up the fixture, for example, open a network connection. This method
757: * is called before a test is executed.
758: */
759: protected void setUp() {
760: bao = new ByteArrayOutputStream();
761: pw = new PrintWriter(bao, false);
762:
763: }
764:
765: /**
766: * Tears down the fixture, for example, close a network connection. This
767: * method is called after a test is executed.
768: */
769: protected void tearDown() {
770: try {
771: pw.close();
772: } catch (Exception e) {
773: }
774: }
775: }
|