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.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.OutputStream;
27 import java.io.PrintWriter;
28 import java.io.UnsupportedEncodingException;
29 import java.util.IllegalFormatException;
30 import java.util.Locale;
32 import tests.support.Support_StringReader;
33 import tests.support.Support_StringWriter;
34 import dalvik.annotation.KnownFailure;
35 import dalvik.annotation.TestLevel;
36 import dalvik.annotation.TestTargetClass;
37 import dalvik.annotation.TestTargetNew;
39 @TestTargetClass(PrintWriter.class)
40 public class PrintWriterTest extends junit.framework.TestCase {
42 private static class MockPrintWriter extends PrintWriter {
44 public MockPrintWriter(OutputStream os) {
49 public void setError() {
55 public String toString() {
60 private File testFile = null;
61 private String testFilePath = null;
65 ByteArrayOutputStream baos = new ByteArrayOutputStream();
67 ByteArrayInputStream bai;
72 * @tests java.io.PrintWriter#PrintWriter(java.io.OutputStream)
75 level = TestLevel.COMPLETE,
77 method = "PrintWriter",
78 args = {java.io.OutputStream.class}
80 public void test_ConstructorLjava_io_OutputStream() {
81 // Test for method java.io.PrintWriter(java.io.OutputStream)
83 pw = new PrintWriter(baos);
84 pw.println("Random Chars");
85 pw.write("Hello World");
88 br = new BufferedReader(new Support_StringReader(baos.toString()));
90 assertTrue("Incorrect string written/read: " + s, s
91 .equals("Random Chars"));
93 assertTrue("Incorrect string written/read: " + s, s
94 .equals("Hello World"));
95 } catch (IOException e) {
96 fail("IOException during test : " + e.getMessage());
101 * @tests java.io.PrintWriter#PrintWriter(java.io.OutputStream, boolean)
104 level = TestLevel.COMPLETE,
106 method = "PrintWriter",
107 args = {java.io.OutputStream.class, boolean.class}
109 public void test_ConstructorLjava_io_OutputStreamZ() {
110 // Test for method java.io.PrintWriter(java.io.OutputStream, boolean)
112 pw = new PrintWriter(baos, true);
113 pw.println("Random Chars");
114 pw.write("Hello World");
116 br = new BufferedReader(new Support_StringReader(baos.toString()));
118 assertTrue("Incorrect string written/read: " + s, s
119 .equals("Random Chars"));
121 br = new BufferedReader(new Support_StringReader(baos.toString()));
123 assertTrue("Incorrect string written/read: " + s, s
124 .equals("Random Chars"));
126 assertTrue("Incorrect string written/read: " + s, s
127 .equals("Hello World"));
128 } catch (IOException e) {
129 fail("IOException during test : " + e.getMessage());
134 * @tests java.io.PrintWriter#PrintWriter(java.io.Writer)
137 level = TestLevel.COMPLETE,
139 method = "PrintWriter",
140 args = {java.io.Writer.class}
142 public void test_ConstructorLjava_io_Writer() {
143 // Test for method java.io.PrintWriter(java.io.Writer)
144 Support_StringWriter sw;
145 pw = new PrintWriter(sw = new Support_StringWriter());
148 assertEquals("Failed to construct proper writer",
149 "Hello", sw.toString());
153 * @tests java.io.PrintWriter#PrintWriter(java.io.Writer, boolean)
156 level = TestLevel.COMPLETE,
158 method = "PrintWriter",
159 args = {java.io.Writer.class, boolean.class}
161 public void test_ConstructorLjava_io_WriterZ() {
162 // Test for method java.io.PrintWriter(java.io.Writer, boolean)
163 Support_StringWriter sw;
164 pw = new PrintWriter(sw = new Support_StringWriter(), true);
166 // Auto-flush should have happened
167 assertEquals("Failed to construct proper writer",
168 "Hello", sw.toString());
172 * @tests java.io.PrintWriter#PrintWriter(java.io.File)
175 level = TestLevel.COMPLETE,
177 method = "PrintWriter",
178 args = {java.io.File.class}
180 public void test_ConstructorLjava_io_File() throws Exception {
183 tobj = new PrintWriter(testFile);
186 assertEquals("output file has wrong length", 1, testFile.length());
187 tobj = new PrintWriter(testFile);
190 assertEquals("output file should be empty", 0, testFile.length());
192 File file = new File("/invalidDirectory/Dummy");
194 tobj = new PrintWriter(file);
195 fail("FileNotFoundException not thrown.");
196 } catch (FileNotFoundException e) {
202 * @tests java.io.PrintWriter#PrintWriter(java.io.File, java.lang.String)
205 level = TestLevel.COMPLETE,
207 method = "PrintWriter",
208 args = {java.io.File.class, java.lang.String.class}
210 public void test_ConstructorLjava_io_File_Ljava_lang_String() throws Exception {
213 tobj = new PrintWriter(testFile, "utf-8");
216 assertEquals("output file has wrong length", 1, testFile.length());
217 tobj = new PrintWriter(testFile, "utf-8");
220 assertEquals("output file should be empty", 0, testFile.length());
222 File file = new File("/invalidDirectory/Dummy");
224 tobj = new PrintWriter(file, "utf-8");
225 fail("FileNotFoundException not thrown.");
226 } catch (FileNotFoundException e) {
231 tobj = new PrintWriter(testFile, "invalidEncoding");
232 fail("UnsupportedEncodingException not thrown.");
233 } catch (UnsupportedEncodingException e) {
239 * @tests java.io.PrintWriter#PrintWriter(java.lang.String)
242 level = TestLevel.COMPLETE,
244 method = "PrintWriter",
245 args = {java.lang.String.class}
247 public void test_ConstructorLjava_lang_String() throws Exception {
250 tobj = new PrintWriter(testFilePath);
254 assertEquals("output file has wrong length", 1, testFile.length());
255 tobj = new PrintWriter(testFilePath);
258 assertEquals("output file should be empty", 0, testFile.length());
261 tobj = new PrintWriter("/invalidDirectory/Dummy");
262 fail("FileNotFoundException not thrown.");
263 } catch (FileNotFoundException e) {
269 * @tests java.io.PrintWriter#PrintWriter(java.lang.String, java.lang.String)
272 level = TestLevel.COMPLETE,
274 method = "PrintWriter",
275 args = {java.lang.String.class, java.lang.String.class}
277 public void test_ConstructorLjava_lang_String_Ljava_lang_String() throws Exception {
280 tobj = new PrintWriter(testFilePath, "utf-8");
284 assertEquals("output file has wrong length", 1, testFile.length());
285 tobj = new PrintWriter(testFilePath, "utf-8");
288 assertEquals("output file should be empty", 0, testFile.length());
291 tobj = new PrintWriter("/invalidDirectory/", "utf-8");
292 fail("FileNotFoundException not thrown.");
293 } catch (FileNotFoundException e) {
298 tobj = new PrintWriter(testFilePath, "invalidEncoding");
299 fail("UnsupportedEncodingException not thrown.");
300 } catch (UnsupportedEncodingException e) {
306 * @tests java.io.PrintWriter#checkError()
309 level = TestLevel.COMPLETE,
311 method = "checkError",
314 public void test_checkError() {
315 // Test for method boolean java.io.PrintWriter.checkError()
317 pw.print(490000000000.08765);
318 assertTrue("Failed to return error", pw.checkError());
322 * @tests java.io.PrintStream#setError()
325 level = TestLevel.COMPLETE,
329 public void test_setError() throws Exception {
330 MockPrintWriter os = new MockPrintWriter(new ByteArrayOutputStream());
331 assertFalse("Test 1: Error flag should not be set.", os.checkError());
333 assertTrue("Test 2: Error flag should be set.", os.checkError());
337 * @tests java.io.PrintWriter#close()
340 level = TestLevel.COMPLETE,
345 public void test_close() {
346 // Test for method void java.io.PrintWriter.close()
349 assertTrue("Write on closed stream failed to generate error", pw
354 * @tests java.io.PrintWriter#flush()
357 level = TestLevel.COMPLETE,
362 public void test_flush() {
363 // Test for method void java.io.PrintWriter.flush()
364 final double dub = 490000000000.08765;
367 assertTrue("Failed to flush", new String(baos.toByteArray())
368 .equals(String.valueOf(dub)));
372 * @tests java.io.PrintWriter#print(char[])
375 level = TestLevel.COMPLETE,
378 args = {char[].class}
380 public void test_print$C() {
381 // Test for method void java.io.PrintWriter.print(char [])
383 char[] schars = new char[11];
384 "Hello World".getChars(0, 11, schars, 0);
388 br = new BufferedReader(new Support_StringReader(baos.toString()));
390 } catch (IOException e) {
391 fail("IOException during test : " + e.getMessage());
393 assertTrue("Wrote incorrect char[] string: " + s, s
394 .equals("Hello World"));
397 pw.print((char[]) null);
398 } catch (NullPointerException e) {
401 assertEquals("null pointer exception for printing null char[] is not caught",
406 * @tests java.io.PrintWriter#print(char)
409 level = TestLevel.COMPLETE,
414 public void test_printC() {
415 // Test for method void java.io.PrintWriter.print(char)
418 assertEquals("Wrote incorrect char string", "c", new String(baos.toByteArray())
423 * @tests java.io.PrintWriter#print(double)
426 level = TestLevel.COMPLETE,
429 args = {double.class}
431 public void test_printD() {
432 // Test for method void java.io.PrintWriter.print(double)
433 final double dub = 490000000000.08765;
436 assertTrue("Wrote incorrect double string", new String(baos
437 .toByteArray()).equals(String.valueOf(dub)));
441 * @tests java.io.PrintWriter#print(float)
444 level = TestLevel.COMPLETE,
449 public void test_printF() {
450 // Test for method void java.io.PrintWriter.print(float)
451 final float flo = 49.08765f;
454 assertTrue("Wrote incorrect float string",
455 new String(baos.toByteArray()).equals(String.valueOf(flo)));
459 * @tests java.io.PrintWriter#print(int)
462 level = TestLevel.COMPLETE,
467 public void test_printI() {
468 // Test for method void java.io.PrintWriter.print(int)
471 assertEquals("Wrote incorrect int string", "4908765", new String(baos.toByteArray())
476 * @tests java.io.PrintWriter#print(long)
479 level = TestLevel.COMPLETE,
484 public void test_printJ() {
485 // Test for method void java.io.PrintWriter.print(long)
486 pw.print(49087650000L);
488 assertEquals("Wrote incorrect long string", "49087650000", new String(baos.toByteArray())
493 * @tests java.io.PrintWriter#print(java.lang.Object)
496 level = TestLevel.COMPLETE,
499 args = {java.lang.Object.class}
501 public void test_printLjava_lang_Object() {
502 // Test for method void java.io.PrintWriter.print(java.lang.Object)
503 pw.print((Object) null);
505 assertEquals("Did not write null", "null", new String(baos.toByteArray()));
508 pw.print(new Bogus());
510 assertEquals("Wrote in incorrect Object string", "Bogus", new String(baos
515 * @tests java.io.PrintWriter#print(java.lang.String)
518 level = TestLevel.COMPLETE,
521 args = {java.lang.String.class}
523 public void test_printLjava_lang_String() {
524 // Test for method void java.io.PrintWriter.print(java.lang.String)
525 pw.print((String) null);
527 assertEquals("did not write null", "null", new String(baos.toByteArray()));
530 pw.print("Hello World");
532 assertEquals("Wrote incorrect string", "Hello World", new String(baos.toByteArray()));
536 * @tests java.io.PrintWriter#print(boolean)
539 level = TestLevel.COMPLETE,
542 args = {boolean.class}
544 public void test_printZ() {
545 // Test for method void java.io.PrintWriter.print(boolean)
548 assertEquals("Wrote in incorrect boolean string", "true", new String(baos
553 * @tests java.io.PrintWriter#println()
556 level = TestLevel.COMPLETE,
561 public void test_println() {
562 // Test for method void java.io.PrintWriter.println()
569 br = new BufferedReader(new Support_StringReader(baos.toString()));
571 assertTrue("Wrote incorrect line: " + s, s.equals("Blarg"));
573 assertTrue("Wrote incorrect line: " + s, s.equals(""));
575 assertTrue("Wrote incorrect line: " + s, s.equals("Bleep"));
576 } catch (IOException e) {
577 fail("IOException during test : " + e.getMessage());
582 * @tests java.io.PrintWriter#println(char[])
585 level = TestLevel.COMPLETE,
588 args = {char[].class}
590 public void test_println$C() {
591 // Test for method void java.io.PrintWriter.println(char [])
593 char[] schars = new char[11];
594 "Hello World".getChars(0, 11, schars, 0);
595 pw.println("Random Chars");
599 br = new BufferedReader(new Support_StringReader(baos.toString()));
602 } catch (IOException e) {
603 fail("IOException during test : " + e.getMessage());
605 assertTrue("Wrote incorrect char[] string: " + s, s
606 .equals("Hello World"));
610 * @tests java.io.PrintWriter#println(char)
613 level = TestLevel.COMPLETE,
618 public void test_printlnC() {
619 // Test for method void java.io.PrintWriter.println(char)
621 pw.println("Random Chars");
625 br = new BufferedReader(new Support_StringReader(baos.toString()));
628 } catch (IOException e) {
629 fail("IOException during test : " + e.getMessage());
631 assertTrue("Wrote incorrect char string: " + s, s.equals("c"));
635 * @tests java.io.PrintWriter#println(double)
638 level = TestLevel.COMPLETE,
641 args = {double.class}
643 public void test_printlnD() {
644 // Test for method void java.io.PrintWriter.println(double)
646 final double dub = 4000000000000000.657483;
647 pw.println("Random Chars");
651 br = new BufferedReader(new Support_StringReader(baos.toString()));
654 } catch (IOException e) {
655 fail("IOException during test : " + e.getMessage());
657 assertTrue("Wrote incorrect double string: " + s, s.equals(String
662 * @tests java.io.PrintWriter#println(float)
665 level = TestLevel.COMPLETE,
670 public void test_printlnF() {
671 // Test for method void java.io.PrintWriter.println(float)
673 final float flo = 40.4646464f;
674 pw.println("Random Chars");
678 br = new BufferedReader(new Support_StringReader(baos.toString()));
681 assertTrue("Wrote incorrect float string: " + s + " wanted: "
682 + String.valueOf(flo), s.equals(String.valueOf(flo)));
683 } catch (IOException e) {
684 fail("IOException during test : " + e.getMessage());
690 * @tests java.io.PrintWriter#println(int)
693 level = TestLevel.COMPLETE,
698 public void test_printlnI() {
699 // Test for method void java.io.PrintWriter.println(int)
701 pw.println("Random Chars");
705 br = new BufferedReader(new Support_StringReader(baos.toString()));
708 } catch (IOException e) {
709 fail("IOException during test : " + e.getMessage());
711 assertTrue("Wrote incorrect int string: " + s, s.equals("400000"));
715 * @tests java.io.PrintWriter#println(long)
718 level = TestLevel.COMPLETE,
723 public void test_printlnJ() {
724 // Test for method void java.io.PrintWriter.println(long)
726 pw.println("Random Chars");
727 pw.println(4000000000000L);
730 br = new BufferedReader(new Support_StringReader(baos.toString()));
733 } catch (IOException e) {
734 fail("IOException during test : " + e.getMessage());
736 assertTrue("Wrote incorrect long string: " + s, s
737 .equals("4000000000000"));
741 * @tests java.io.PrintWriter#println(java.lang.Object)
744 level = TestLevel.COMPLETE,
747 args = {java.lang.Object.class}
749 public void test_printlnLjava_lang_Object() {
750 // Test for method void java.io.PrintWriter.println(java.lang.Object)
752 pw.println("Random Chars");
753 pw.println(new Bogus());
756 br = new BufferedReader(new Support_StringReader(baos.toString()));
759 } catch (IOException e) {
760 fail("IOException during test : " + e.getMessage());
762 assertTrue("Wrote incorrect Object string: " + s, s.equals("Bogus"));
766 * @tests java.io.PrintWriter#println(java.lang.String)
769 level = TestLevel.COMPLETE,
772 args = {java.lang.String.class}
774 public void test_printlnLjava_lang_String() {
775 // Test for method void java.io.PrintWriter.println(java.lang.String)
777 pw.println("Random Chars");
778 pw.println("Hello World");
781 br = new BufferedReader(new Support_StringReader(baos.toString()));
784 } catch (IOException e) {
785 fail("IOException during test : " + e.getMessage());
787 assertTrue("Wrote incorrect string: " + s, s.equals("Hello World"));
791 * @tests java.io.PrintWriter#println(boolean)
794 level = TestLevel.COMPLETE,
797 args = {boolean.class}
799 public void test_printlnZ() {
800 // Test for method void java.io.PrintWriter.println(boolean)
802 pw.println("Random Chars");
806 br = new BufferedReader(new Support_StringReader(baos.toString()));
809 } catch (IOException e) {
810 fail("IOException during test : " + e.getMessage());
812 assertTrue("Wrote incorrect boolean string: " + s, s.equals("false"));
816 * @tests java.io.PrintWriter#write(char[])
819 level = TestLevel.COMPLETE,
822 args = {char[].class}
824 public void test_write$C() {
825 // Test for method void java.io.PrintWriter.write(char [])
827 char[] schars = new char[11];
828 "Hello World".getChars(0, 11, schars, 0);
829 pw.println("Random Chars");
833 br = new BufferedReader(new Support_StringReader(baos.toString()));
836 } catch (IOException e) {
837 fail("IOException during test: " + e.getMessage());
839 assertTrue("Wrote incorrect char[] string: " + s, s
840 .equals("Hello World"));
844 * @tests java.io.PrintWriter#write(char[], int, int)
847 level = TestLevel.PARTIAL_COMPLETE,
850 args = {char[].class, int.class, int.class}
852 public void test_write$CII() {
853 // Test for method void java.io.PrintWriter.write(char [], int, int)
855 char[] schars = new char[11];
856 "Hello World".getChars(0, 11, schars, 0);
857 pw.println("Random Chars");
858 pw.write(schars, 6, 5);
861 br = new BufferedReader(new Support_StringReader(baos.toString()));
864 } catch (IOException e) {
865 fail("IOException during test : " + e.getMessage());
867 assertTrue("Wrote incorrect char[] string: " + s, s.equals("World"));
871 * @tests java.io.PrintWriter#write(char[], int, int)
874 level = TestLevel.PARTIAL_COMPLETE,
877 args = {char[].class, int.class, int.class}
879 public void test_write$CII_Exception() {
880 // Test for method void java.io.PrintWriter.write(char [], int, int)
881 char[] chars = new char[10];
883 pw.write(chars, 0, -1);
884 fail("IndexOutOfBoundsException was not thrown");
885 } catch (IndexOutOfBoundsException e) {
889 pw.write(chars, -1, 1);
890 fail("IndexOutOfBoundsException was not thrown");
891 } catch (IndexOutOfBoundsException e) {
895 pw.write(chars, 10, 1);
896 fail("IndexOutOfBoundsException was not thrown");
897 } catch (IndexOutOfBoundsException e) {
903 * @tests java.io.PrintWriter#write(int)
906 level = TestLevel.COMPLETE,
911 public void test_writeI() {
912 // Test for method void java.io.PrintWriter.write(int)
913 char[] cab = new char[3];
918 bai = new ByteArrayInputStream(baos.toByteArray());
919 cab[0] = (char) bai.read();
920 cab[1] = (char) bai.read();
921 cab[2] = (char) bai.read();
922 assertTrue("Wrote incorrect ints", cab[0] == 'a' && cab[1] == 'b'
928 * @tests java.io.PrintWriter#write(java.lang.String)
931 level = TestLevel.COMPLETE,
934 args = {java.lang.String.class}
936 public void test_writeLjava_lang_String() {
937 // Test for method void java.io.PrintWriter.write(java.lang.String)
939 pw.println("Random Chars");
940 pw.write("Hello World");
943 br = new BufferedReader(new Support_StringReader(baos.toString()));
946 } catch (IOException e) {
947 fail("IOException during test : " + e.getMessage());
949 assertTrue("Wrote incorrect char[] string: " + s, s
950 .equals("Hello World"));
954 * @tests java.io.PrintWriter#write(java.lang.String, int, int)
957 level = TestLevel.COMPLETE,
960 args = {java.lang.String.class, int.class, int.class}
962 public void test_writeLjava_lang_StringII() {
963 // Test for method void java.io.PrintWriter.write(java.lang.String, int,
966 pw.println("Random Chars");
967 pw.write("Hello World", 6, 5);
970 br = new BufferedReader(new Support_StringReader(baos.toString()));
973 } catch (IOException e) {
974 fail("IOException during test : " + e.getMessage());
976 assertTrue("Wrote incorrect char[] string: " + s, s.equals("World"));
980 * @tests java.io.PrintWriter#append(char)
983 level = TestLevel.COMPLETE,
988 public void test_appendChar() {
990 ByteArrayOutputStream out = new ByteArrayOutputStream();
991 PrintWriter printWriter = new PrintWriter(out);
992 printWriter.append(testChar);
994 assertEquals(String.valueOf(testChar),out.toString());
998 * @tests java.io.PrintWriter#append(CharSequence)
1001 level = TestLevel.COMPLETE,
1004 args = {java.lang.CharSequence.class}
1006 public void test_appendCharSequence() {
1008 String testString = "My Test String";
1009 ByteArrayOutputStream out = new ByteArrayOutputStream();
1010 PrintWriter printWriter = new PrintWriter(out);
1011 printWriter.append(testString);
1012 printWriter.flush();
1013 assertEquals(testString, out.toString());
1014 printWriter.close();
1019 * @tests java.io.PrintWriter#append(CharSequence, int, int)
1022 level = TestLevel.COMPLETE,
1025 args = {java.lang.CharSequence.class, int.class, int.class}
1027 public void test_appendCharSequenceIntInt() {
1028 String testString = "My Test String";
1029 ByteArrayOutputStream out = new ByteArrayOutputStream();
1030 PrintWriter printWriter = new PrintWriter(out);
1031 printWriter.append(testString, 1, 3);
1032 printWriter.flush();
1033 assertEquals(testString.substring(1, 3), out.toString());
1035 printWriter.append(testString, 4, 100);
1036 fail("IndexOutOfBoundsException not thrown");
1037 } catch (IndexOutOfBoundsException e) {
1041 printWriter.append(testString, 100, 1);
1042 fail("IndexOutOfBoundsException not thrown");
1043 } catch (IndexOutOfBoundsException e) {
1046 printWriter.close();
1050 * @tests java.io.PrintWriter#format(java.lang.String, java.lang.Object...)
1053 level = TestLevel.COMPLETE,
1056 args = {java.lang.String.class, java.lang.Object[].class}
1058 public void test_formatLjava_lang_String$Ljava_lang_Object() {
1061 tobj = new PrintWriter(baos, false);
1062 tobj.format("%s %s", "Hello", "World");
1064 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
1065 byte[] rbytes = new byte[11];
1066 bis.read(rbytes, 0, rbytes.length);
1067 assertEquals("Wrote incorrect string", "Hello World",
1068 new String(rbytes));
1071 tobj = new PrintWriter(baos);
1072 tobj.format("%1$.3G, %1$.5f, 0%2$xx", 12345.678, 123456);
1074 assertEquals("Wrong output!", "1.23E+04, 12345.67800, 01e240x", new String(baos.toByteArray()));
1078 tobj = new PrintWriter(baos);
1080 tobj.format("%1$.3G, %1$x", 12345.678);
1081 fail("IllegalFormatException not thrown");
1082 } catch (IllegalFormatException e) {
1087 tobj.format("%s %q", "Hello", "World");
1088 fail("IllegalFormatException not thrown");
1089 } catch (IllegalFormatException e) {
1094 tobj.format("%s %s", "Hello");
1095 fail("IllegalFormatException not thrown");
1096 } catch (IllegalFormatException e) {
1102 * @tests java.io.PrintWriter#format(java.util.Locale, java.lang.String, java.lang.Object...)
1105 level = TestLevel.COMPLETE,
1108 args = {java.util.Locale.class, java.lang.String.class, java.lang.Object[].class}
1110 @KnownFailure("Some locales were removed last minute in cupcake")
1111 public void test_formatLjava_util_Locale_Ljava_lang_String_$Ljava_lang_Object() {
1114 tobj = new PrintWriter(baos, false);
1115 tobj.format(Locale.US, "%s %s", "Hello", "World");
1117 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
1118 byte[] rbytes = new byte[11];
1119 bis.read(rbytes, 0, rbytes.length);
1120 assertEquals("Wrote incorrect string", "Hello World",
1121 new String(rbytes));
1124 tobj = new PrintWriter(baos);
1125 tobj.format(Locale.GERMANY, "%1$.3G; %1$.5f; 0%2$xx", 12345.678, 123456);
1127 assertEquals("Wrong output!", "1,23E+04; 12345,67800; 01e240x", new String(baos.toByteArray()));
1131 tobj = new PrintWriter(baos);
1132 tobj.format(Locale.US, "%1$.3G, %1$.5f, 0%2$xx", 12345.678, 123456);
1134 assertEquals("Wrong output!", "1.23E+04, 12345.67800, 01e240x", new String(baos.toByteArray()));
1138 tobj = new PrintWriter(baos);
1140 tobj.format(Locale.US, "%1$.3G, %1$x", 12345.678);
1141 fail("IllegalFormatException not thrown");
1142 } catch (IllegalFormatException e) {
1147 tobj.format(Locale.US, "%s %q", "Hello", "World");
1148 fail("IllegalFormatException not thrown");
1149 } catch (IllegalFormatException e) {
1154 tobj.format(Locale.US, "%s %s", "Hello");
1155 fail("IllegalFormatException not thrown");
1156 } catch (IllegalFormatException e) {
1162 * @tests java.io.PrintWriter#printf(java.lang.String, java.lang.Object...)
1165 level = TestLevel.COMPLETE,
1168 args = {java.lang.String.class, java.lang.Object[].class}
1170 public void test_printfLjava_lang_String$Ljava_lang_Object() {
1173 tobj = new PrintWriter(baos, false);
1174 tobj.printf("%s %s", "Hello", "World");
1176 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
1177 byte[] rbytes = new byte[11];
1178 bis.read(rbytes, 0, rbytes.length);
1179 assertEquals("Wrote incorrect string", "Hello World",
1180 new String(rbytes));
1183 tobj = new PrintWriter(baos);
1184 tobj.printf("%1$.3G, %1$.5f, 0%2$xx", 12345.678, 123456);
1186 assertEquals("Wrong output!", "1.23E+04, 12345.67800, 01e240x", new String(baos.toByteArray()));
1190 tobj = new PrintWriter(baos);
1192 tobj.printf("%1$.3G, %1$x", 12345.678);
1193 fail("IllegalFormatException not thrown");
1194 } catch (IllegalFormatException e) {
1199 tobj.printf("%s %q", "Hello", "World");
1200 fail("IllegalFormatException not thrown");
1201 } catch (IllegalFormatException e) {
1206 tobj.printf("%s %s", "Hello");
1207 fail("IllegalFormatException not thrown");
1208 } catch (IllegalFormatException e) {
1214 * @tests java.io.PrintWriter#printf(java.util.Locale, java.lang.String, java.lang.Object...)
1217 level = TestLevel.COMPLETE,
1220 args = {java.util.Locale.class, java.lang.String.class, java.lang.Object[].class}
1222 @KnownFailure("Some locales were removed last minute in cupcake")
1223 public void test_printfLjava_util_Locale_Ljava_lang_String_$Ljava_lang_Object() {
1226 tobj = new PrintWriter(baos, false);
1227 tobj.printf(Locale.US, "%s %s", "Hello", "World");
1229 ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
1230 byte[] rbytes = new byte[11];
1231 bis.read(rbytes, 0, rbytes.length);
1232 assertEquals("Wrote incorrect string", "Hello World",
1233 new String(rbytes));
1236 tobj = new PrintWriter(baos);
1237 tobj.printf(Locale.GERMANY, "%1$.3G; %1$.5f; 0%2$xx", 12345.678, 123456);
1239 assertEquals("Wrong output!", "1,23E+04; 12345,67800; 01e240x", new String(baos.toByteArray()));
1243 tobj = new PrintWriter(baos);
1244 tobj.printf(Locale.US, "%1$.3G, %1$.5f, 0%2$xx", 12345.678, 123456);
1246 assertEquals("Wrong output!", "1.23E+04, 12345.67800, 01e240x", new String(baos.toByteArray()));
1250 tobj = new PrintWriter(baos);
1252 tobj.printf(Locale.US, "%1$.3G, %1$x", 12345.678);
1253 fail("IllegalFormatException not thrown");
1254 } catch (IllegalFormatException e) {
1259 tobj.printf(Locale.US, "%s %q", "Hello", "World");
1260 fail("IllegalFormatException not thrown");
1261 } catch (IllegalFormatException e) {
1266 tobj.printf(Locale.US, "%s %s", "Hello");
1267 fail("IllegalFormatException not thrown");
1268 } catch (IllegalFormatException e) {
1274 * Sets up the fixture, for example, open a network connection. This method
1275 * is called before a test is executed.
1278 protected void setUp() throws Exception {
1279 testFile = File.createTempFile("test", null);
1280 testFilePath = testFile.getAbsolutePath();
1281 pw = new PrintWriter(baos, false);
1286 * Tears down the fixture, for example, close a network connection. This
1287 * method is called after a test is executed.
1290 protected void tearDown() throws Exception {
1293 testFilePath = null;
1296 } catch (Exception e) {