1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package tests.api.java.io;
19
20import java.io.ByteArrayInputStream;
21import java.io.ByteArrayOutputStream;
22import java.io.DataInputStream;
23import java.io.File;
24import java.io.FileNotFoundException;
25import java.io.IOException;
26import java.io.OutputStream;
27import java.io.PrintStream;
28import java.io.UnsupportedEncodingException;
29import java.util.IllegalFormatException;
30import java.util.Locale;
31
32import dalvik.annotation.TestLevel;
33import dalvik.annotation.TestTargetClass;
34import dalvik.annotation.TestTargetNew;
35
36import tests.support.Support_Locale;
37
38@TestTargetClass(PrintStream.class)
39public class PrintStreamTest extends junit.framework.TestCase {
40
41    ByteArrayOutputStream baos = new ByteArrayOutputStream();
42
43    byte[] ibuf = new byte[4096];
44
45    private File testFile = null;
46    private String testFilePath = null;
47
48    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";
49
50    private static class MockPrintStream extends PrintStream {
51
52        public MockPrintStream(String fileName) throws FileNotFoundException {
53            super(fileName);
54        }
55
56        public MockPrintStream(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
57            super(fileName, csn);
58        }
59
60        public MockPrintStream(OutputStream os) {
61            super(os);
62        }
63
64        @Override
65        public void setError() {
66            super.setError();
67        }
68    }
69
70    @TestTargetNew(
71        level = TestLevel.COMPLETE,
72        notes = "",
73        method = "PrintStream",
74        args = {java.io.File.class}
75    )
76    public void test_Constructor_Ljava_io_File() throws IOException {
77        PrintStream tobj;
78
79        tobj = new PrintStream(testFile);
80        assertNotNull(tobj);
81        tobj.write(1);
82        tobj.close();
83        assertEquals("output file has wrong length", 1, testFile.length());
84        tobj = new PrintStream(testFile);
85        assertNotNull(tobj);
86        tobj.close();
87        assertEquals("output file should be empty", 0, testFile.length());
88
89        File file = new File("/invalidDirectory/Dummy");
90        try {
91            tobj = new PrintStream(file);
92            fail("FileNotFoundException not thrown.");
93        } catch (FileNotFoundException e) {
94            // expected
95        }
96    }
97
98    @TestTargetNew(
99        level = TestLevel.COMPLETE,
100        notes = "",
101        method = "PrintStream",
102        args = {java.io.File.class, java.lang.String.class}
103    )
104    public void test_Constructor_Ljava_io_File_Ljava_lang_String() throws Exception {
105        PrintStream tobj;
106
107        tobj = new PrintStream(testFile, "utf-8");
108        assertNotNull(tobj);
109        tobj.write(1);
110        tobj.close();
111        assertEquals("output file has wrong length", 1, testFile.length());
112        tobj = new PrintStream(testFile, "utf-8");
113        assertNotNull(tobj);
114        tobj.close();
115        assertEquals("output file should be empty", 0, testFile.length());
116
117        File file = new File("/invalidDirectory/Dummy");
118        try {
119            tobj = new PrintStream(file, "utf-8");
120            fail("FileNotFoundException not thrown.");
121        } catch (FileNotFoundException e) {
122            // expected
123        }
124
125        try {
126            tobj = new PrintStream(testFile, "invalidEncoding");
127            fail("UnsupportedEncodingException not thrown.");
128        } catch (UnsupportedEncodingException e) {
129            // expected
130        }
131    }
132
133    /**
134     * @tests {@link java.io.PrintStream#PrintStream(String)}
135     */
136    @TestTargetNew(
137        level = TestLevel.COMPLETE,
138        notes = "",
139        method = "PrintStream",
140        args = {java.lang.String.class}
141    )
142    public void test_Constructor_Ljava_lang_String() throws IOException {
143        PrintStream tobj;
144
145        tobj = new PrintStream(testFilePath);
146        assertNotNull(tobj);
147        tobj.write(1);
148        tobj.close();
149        assertEquals("output file has wrong length", 1, testFile.length());
150        tobj = new PrintStream(testFilePath);
151        assertNotNull(tobj);
152        tobj.close();
153        assertEquals("output file should be empty", 0, testFile.length());
154
155        try {
156            tobj = new PrintStream("/invalidDirectory/Dummy");
157            fail("FileNotFoundException not thrown.");
158        } catch (FileNotFoundException e) {
159            // expected
160        }
161    }
162
163    /**
164     * @tests {@link java.io.PrintStream#PrintStream(String, String)}
165     */
166    @TestTargetNew(
167        level = TestLevel.COMPLETE,
168        notes = "",
169        method = "PrintStream",
170        args = {java.lang.String.class, java.lang.String.class}
171    )
172    public void test_Constructor_Ljava_lang_String_Ljava_lang_String() throws Exception {
173        PrintStream tobj;
174
175        tobj = new PrintStream(testFilePath, "utf-8");
176        assertNotNull(tobj);
177        tobj.write(1);
178        tobj.close();
179        assertEquals("output file has wrong length", 1, testFile.length());
180        tobj = new PrintStream(testFilePath, "utf-8");
181        assertNotNull(tobj);
182        tobj.close();
183        assertEquals("output file should be empty", 0, testFile.length());
184
185        try {
186            tobj = new PrintStream("/invalidDirectory/", "utf-8");
187            fail("FileNotFoundException not thrown.");
188        } catch (FileNotFoundException e) {
189            // expected
190        }
191
192        try {
193            tobj = new PrintStream(testFilePath, "invalidEncoding");
194            fail("UnsupportedEncodingException not thrown.");
195        } catch (UnsupportedEncodingException e) {
196            // expected
197        }
198    }
199
200    /**
201     * @tests java.io.PrintStream#PrintStream(java.io.OutputStream)
202     */
203    @TestTargetNew(
204        level = TestLevel.COMPLETE,
205        notes = "",
206        method = "PrintStream",
207        args = {java.io.OutputStream.class}
208    )
209    public void test_ConstructorLjava_io_OutputStream() throws Exception {
210        // Test for method java.io.PrintStream(java.io.OutputStream)
211        PrintStream os = new PrintStream(baos);
212        os.print(2345.76834720202);
213        os.close();
214
215        // regression for HARMONY-1195
216        try {
217            os = new PrintStream(baos, true, null);
218            fail("Should throw NPE");
219        } catch (NullPointerException e) {}
220    }
221
222    /**
223     * @throws FileNotFoundException
224     * @tests java.io.PrintStream#PrintStream(java.io.OutputStream, boolean)
225     */
226    @TestTargetNew(
227        level = TestLevel.SUFFICIENT,
228        notes = "Passing FALSE for autoFlush not tested.",
229        method = "PrintStream",
230        args = {java.io.OutputStream.class, boolean.class}
231    )
232    public void test_ConstructorLjava_io_OutputStreamZ() throws FileNotFoundException {
233        PrintStream tobj;
234
235        tobj = new PrintStream(baos, true);
236        tobj.println(2345.76834720202);
237        assertTrue("Bytes not written", baos.size() > 0);
238        tobj.close();
239
240//        tobj = new PrintStream(bos, false);
241//        tobj.println(2345.76834720202);
242//        assertEquals("Bytes should not be written, yet", 0, bos.size());
243//        tobj.flush();
244//        assertTrue("Bytes not written", bos.size() > 0);
245//        tobj.close();
246
247//        FileOutputStream fos = new FileOutputStream(testFile);
248//
249//        tobj = new PrintStream(fos, true);
250//        tobj.println(2345.76834720202);
251//        assertTrue("Bytes not written", testFile.length() > 0);
252//        tobj.close();
253//
254//        tobj = new PrintStream(fos, false);
255//        tobj.println(2345.76834720202);
256//        assertTrue("Bytes not written", testFile.length() > 0);
257//        tobj.close();
258//
259    }
260
261    @TestTargetNew(
262        level = TestLevel.COMPLETE,
263        notes = "",
264        method = "PrintStream",
265        args = {java.io.OutputStream.class, boolean.class, java.lang.String.class}
266    )
267    public void test_ConstructorLjava_io_OutputStream_Z_Ljava_lang_String() {
268        PrintStream tobj;
269
270        tobj = new PrintStream(baos, true);
271        tobj.println(2345.76834720202);
272        assertTrue("Bytes not written", baos.size() > 0);
273        tobj.close();
274
275        try {
276            tobj = new PrintStream(baos, true, "invalidEncoding");
277            fail("UnsupportedEncodingException not thrown.");
278        } catch (UnsupportedEncodingException e) {
279            // expected
280        }
281    }
282
283    /**
284     * @tests java.io.PrintStream#checkError()
285     */
286    @TestTargetNew(
287        level = TestLevel.COMPLETE,
288        notes = "",
289        method = "checkError",
290        args = {}
291    )
292    public void test_checkError() throws Exception {
293        // Test for method boolean java.io.PrintStream.checkError()
294        PrintStream os = new PrintStream(new OutputStream() {
295
296            public void write(int b) throws IOException {
297                throw new IOException();
298            }
299
300            public void write(byte[] b, int o, int l) throws IOException {
301                throw new IOException();
302            }
303        });
304        os.print(fileString.substring(0, 501));
305
306        assertTrue("Checkerror should return true", os.checkError());
307    }
308
309    /**
310     * @tests java.io.PrintStream#setError()
311     */
312    @TestTargetNew(
313        level = TestLevel.COMPLETE,
314        method = "setError",
315        args = {}
316    )
317    public void test_setError() throws Exception {
318        MockPrintStream os = new MockPrintStream(new ByteArrayOutputStream());
319        assertFalse("Test 1: Error flag should not be set.", os.checkError());
320        os.setError();
321        assertTrue("Test 2: Error flag should be set.", os.checkError());
322    }
323
324    /**
325     * @tests java.io.PrintStream#close()
326     */
327    @TestTargetNew(
328        level = TestLevel.COMPLETE,
329        notes = "",
330        method = "close",
331        args = {}
332    )
333    public void test_close() throws Exception {
334        // Test for method void java.io.PrintStream.close()
335        PrintStream os = new PrintStream(baos);
336        os.close();
337        baos.close();
338    }
339
340    /**
341     * @tests java.io.PrintStream#flush()
342     */
343    @TestTargetNew(
344        level = TestLevel.COMPLETE,
345        notes = "",
346        method = "flush",
347        args = {}
348    )
349    public void test_flush() throws Exception {
350        // Test for method void java.io.PrintStream.flush()
351        PrintStream os = new PrintStream(baos);
352        os.print(fileString.substring(0, 501));
353        os.flush();
354        assertEquals("Bytes not written after flush", 501, baos.size());
355        baos.close();
356        os.close();
357    }
358
359    /**
360     * @tests java.io.PrintStream#print(char[])
361     */
362    @TestTargetNew(
363        level = TestLevel.COMPLETE,
364        notes = "",
365        method = "print",
366        args = {char[].class}
367    )
368    public void test_print$C() {
369        // Test for method void java.io.PrintStream.print(char [])
370        PrintStream os = new PrintStream(baos, true);
371        try {
372            os.print((char[]) null);
373            fail("NPE expected");
374        } catch (NullPointerException ok) {}
375
376        os = new PrintStream(baos, true);
377        char[] sc = new char[4000];
378        fileString.getChars(0, fileString.length(), sc, 0);
379        os.print(sc);
380        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
381        os.close();
382
383        byte[] rbytes = new byte[4000];
384        bis.read(rbytes, 0, fileString.length());
385        assertEquals("Incorrect char[] written", fileString, new String(rbytes,
386                0, fileString.length()));
387    }
388
389    /**
390     * @tests java.io.PrintStream#print(char)
391     */
392    @TestTargetNew(
393        level = TestLevel.COMPLETE,
394        notes = "",
395        method = "print",
396        args = {char.class}
397    )
398    public void test_printC() {
399        // Test for method void java.io.PrintStream.print(char)
400        PrintStream os = new PrintStream(baos, true);
401        os.print('t');
402        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
403        assertEquals("Incorrect char written", 't', bis.read());
404    }
405
406    /**
407     * @tests java.io.PrintStream#print(double)
408     */
409    @TestTargetNew(
410        level = TestLevel.COMPLETE,
411        notes = "",
412        method = "print",
413        args = {double.class}
414    )
415    public void test_printD() {
416        // Test for method void java.io.PrintStream.print(double)
417        byte[] rbuf = new byte[100];
418        PrintStream os = new PrintStream(baos, true);
419        os.print(2345.76834720202);
420        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
421        bis.read(rbuf, 0, 16);
422        assertEquals("Incorrect double written", "2345.76834720202",
423                new String(rbuf, 0, 16));
424    }
425
426    /**
427     * @tests java.io.PrintStream#print(float)
428     */
429    @TestTargetNew(
430        level = TestLevel.COMPLETE,
431        notes = "",
432        method = "print",
433        args = {float.class}
434    )
435    public void test_printF() {
436        // Test for method void java.io.PrintStream.print(float)
437        PrintStream os = new PrintStream(baos, true);
438        byte rbuf[] = new byte[10];
439        os.print(29.08764f);
440        os.flush();
441        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
442        bis.read(rbuf, 0, 8);
443        assertEquals("Incorrect float written", "29.08764", new String(rbuf, 0,
444                8));
445
446    }
447
448    /**
449     * @tests java.io.PrintStream#print(int)
450     */
451    @TestTargetNew(
452        level = TestLevel.COMPLETE,
453        notes = "",
454        method = "print",
455        args = {int.class}
456    )
457    public void test_printI() {
458        // Test for method void java.io.PrintStream.print(int)
459        PrintStream os = new PrintStream(baos, true);
460        os.print(768347202);
461        byte[] rbuf = new byte[18];
462        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
463        bis.read(rbuf, 0, 9);
464        assertEquals("Incorrect int written", "768347202", new String(rbuf, 0,
465                9));
466    }
467
468    /**
469     * @tests java.io.PrintStream#print(long)
470     */
471    @TestTargetNew(
472        level = TestLevel.COMPLETE,
473        notes = "",
474        method = "print",
475        args = {long.class}
476    )
477    public void test_printJ() {
478        // Test for method void java.io.PrintStream.print(long)
479        byte[] rbuf = new byte[100];
480        PrintStream os = new PrintStream(baos, true);
481        os.print(9875645283333L);
482        os.close();
483        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
484        bis.read(rbuf, 0, 13);
485        assertEquals("Incorrect long written", "9875645283333", new String(
486                rbuf, 0, 13));
487    }
488
489    /**
490     * @tests java.io.PrintStream#print(java.lang.Object)
491     */
492    @TestTargetNew(
493        level = TestLevel.COMPLETE,
494        notes = "",
495        method = "print",
496        args = {java.lang.Object.class}
497    )
498    public void test_printLjava_lang_Object() throws Exception {
499        // Test for method void java.io.PrintStream.print(java.lang.Object)
500        PrintStream os = new PrintStream(baos, true);
501        os.print((Object) null);
502        os.flush();
503        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
504        byte[] nullbytes = new byte[4];
505        bis.read(nullbytes, 0, 4);
506        assertEquals("null should be written", "null", new String(nullbytes, 0,
507                4));
508
509        bis.close();
510        baos.close();
511        os.close();
512
513        ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
514        os = new PrintStream(bos1, true);
515        os.print(new java.util.Vector());
516        bis = new ByteArrayInputStream(bos1.toByteArray());
517        byte[] rbytes = new byte[2];
518        bis.read(rbytes, 0, 2);
519        assertEquals("Incorrect Object written", "[]", new String(rbytes, 0, 2));
520    }
521
522    /**
523     * @tests java.io.PrintStream#print(java.lang.String)
524     */
525    @TestTargetNew(
526        level = TestLevel.COMPLETE,
527        notes = "",
528        method = "print",
529        args = {java.lang.String.class}
530    )
531    public void test_printLjava_lang_String() throws Exception {
532        // Test for method void java.io.PrintStream.print(java.lang.String)
533        PrintStream os = new PrintStream(baos, true);
534        os.print((String) null);
535        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
536        byte[] nullbytes = new byte[4];
537        bis.read(nullbytes, 0, 4);
538        assertEquals("null should be written", "null", new String(nullbytes, 0,
539                4));
540
541        bis.close();
542        baos.close();
543        os.close();
544
545        ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
546        os = new PrintStream(bos1, true);
547        os.print("Hello World");
548        bis = new ByteArrayInputStream(bos1.toByteArray());
549        byte rbytes[] = new byte[100];
550        bis.read(rbytes, 0, 11);
551        assertEquals("Incorrect string written", "Hello World", new String(
552                rbytes, 0, 11));
553    }
554
555    /**
556     * @tests java.io.PrintStream#print(boolean)
557     */
558    @TestTargetNew(
559        level = TestLevel.COMPLETE,
560        notes = "",
561        method = "print",
562        args = {boolean.class}
563    )
564    public void test_printZ() throws Exception {
565        // Test for method void java.io.PrintStream.print(boolean)
566        PrintStream os = new PrintStream(baos, true);
567        os.print(true);
568        DataInputStream dis = new DataInputStream(new ByteArrayInputStream(baos
569                .toByteArray()));
570
571        assertTrue("Incorrect boolean written", dis.readBoolean());
572    }
573
574    /**
575     * @tests java.io.PrintStream#println()
576     */
577    @TestTargetNew(
578        level = TestLevel.COMPLETE,
579        notes = "",
580        method = "println",
581        args = {}
582    )
583    public void test_println() {
584        // Test for method void java.io.PrintStream.println()
585        char c;
586        PrintStream os = new PrintStream(baos, true);
587        os.println("");
588        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
589        assertTrue("Newline not written", (c = (char) bis.read()) == '\r'
590                || c == '\n');
591    }
592
593    /**
594     * @tests java.io.PrintStream#println(char[])
595     */
596    @TestTargetNew(
597        level = TestLevel.COMPLETE,
598        notes = "",
599        method = "println",
600        args = {char[].class}
601    )
602    public void test_println$C() {
603        // Test for method void java.io.PrintStream.println(char [])
604        PrintStream os = new PrintStream(baos, true);
605        char[] sc = new char[4000];
606        fileString.getChars(0, fileString.length(), sc, 0);
607        os.println(sc);
608        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
609        byte[] rbytes = new byte[4000];
610        bis.read(rbytes, 0, fileString.length());
611        assertEquals("Incorrect char[] written", fileString, new String(rbytes,
612                0, fileString.length()));
613
614        // In this particular test method, the end of data is not immediately
615        // followed by newLine separator in the reading buffer, instead its
616        // followed by zeros. The newline is written as the last entry
617        // in the inputStream buffer. Therefore, we must keep reading until we
618        // hit a new line.
619        int r;
620        boolean newline = false;
621        while ((r = bis.read()) != -1) {
622            if (r == '\r' || r == '\n')
623                newline = true;
624        }
625        assertTrue("Newline not written", newline);
626    }
627
628    /**
629     * @tests java.io.PrintStream#println(char)
630     */
631    @TestTargetNew(
632        level = TestLevel.COMPLETE,
633        notes = "",
634        method = "println",
635        args = {char.class}
636    )
637    public void test_printlnC() {
638        // Test for method void java.io.PrintStream.println(char)
639        int c;
640        PrintStream os = new PrintStream(baos, true);
641        os.println('t');
642        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
643        assertEquals("Incorrect char written", 't', bis.read());
644        assertTrue("Newline not written", (c = bis.read()) == '\r' || c == '\n');
645    }
646
647    /**
648     * @tests java.io.PrintStream#println(double)
649     */
650    @TestTargetNew(
651        level = TestLevel.COMPLETE,
652        notes = "",
653        method = "println",
654        args = {double.class}
655    )
656    public void test_printlnD() {
657        // Test for method void java.io.PrintStream.println(double)
658        int c;
659        PrintStream os = new PrintStream(baos, true);
660        os.println(2345.76834720202);
661        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
662        byte[] rbuf = new byte[100];
663        bis.read(rbuf, 0, 16);
664        assertEquals("Incorrect double written", "2345.76834720202",
665                new String(rbuf, 0, 16));
666        assertTrue("Newline not written", (c = bis.read()) == '\r' || c == '\n');
667    }
668
669    /**
670     * @tests java.io.PrintStream#println(float)
671     */
672    @TestTargetNew(
673        level = TestLevel.COMPLETE,
674        notes = "",
675        method = "println",
676        args = {float.class}
677    )
678    public void test_printlnF() {
679        // Test for method void java.io.PrintStream.println(float)
680        int c;
681        byte[] rbuf = new byte[100];
682        PrintStream os = new PrintStream(baos, true);
683        os.println(29.08764f);
684        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
685        bis.read(rbuf, 0, 8);
686        assertEquals("Incorrect float written", "29.08764", new String(rbuf, 0,
687                8));
688        assertTrue("Newline not written", (c = bis.read()) == '\r' || c == '\n');
689    }
690
691    /**
692     * @tests java.io.PrintStream#println(int)
693     */
694    @TestTargetNew(
695        level = TestLevel.COMPLETE,
696        notes = "",
697        method = "println",
698        args = {int.class}
699    )
700    public void test_printlnI() {
701        // Test for method void java.io.PrintStream.println(int)
702        int c;
703        PrintStream os = new PrintStream(baos, true);
704        os.println(768347202);
705        byte[] rbuf = new byte[100];
706        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
707        bis.read(rbuf, 0, 9);
708        assertEquals("Incorrect int written", "768347202", new String(rbuf, 0,
709                9));
710        assertTrue("Newline not written", (c = bis.read()) == '\r' || c == '\n');
711    }
712
713    /**
714     * @tests java.io.PrintStream#println(long)
715     */
716    @TestTargetNew(
717        level = TestLevel.COMPLETE,
718        notes = "",
719        method = "println",
720        args = {long.class}
721    )
722    public void test_printlnJ() {
723        // Test for method void java.io.PrintStream.println(long)
724        int c;
725        PrintStream os = new PrintStream(baos, true);
726        os.println(9875645283333L);
727        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
728        byte[] rbuf = new byte[100];
729        bis.read(rbuf, 0, 13);
730        assertEquals("Incorrect long written", "9875645283333", new String(
731                rbuf, 0, 13));
732        assertTrue("Newline not written", (c = bis.read()) == '\r' || c == '\n');
733    }
734
735    /**
736     * @tests java.io.PrintStream#println(java.lang.Object)
737     */
738    @TestTargetNew(
739        level = TestLevel.COMPLETE,
740        notes = "",
741        method = "println",
742        args = {java.lang.Object.class}
743    )
744    public void test_printlnLjava_lang_Object() {
745        // Test for method void java.io.PrintStream.println(java.lang.Object)
746        char c;
747        PrintStream os = new PrintStream(baos, true);
748        os.println(new java.util.Vector());
749        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
750        byte[] rbytes = new byte[2];
751        bis.read(rbytes, 0, 2);
752        assertEquals("Incorrect Vector written", "[]", new String(rbytes, 0, 2));
753        assertTrue("Newline not written", (c = (char) bis.read()) == '\r'
754                || c == '\n');
755    }
756
757    /**
758     * @tests java.io.PrintStream#println(java.lang.String)
759     */
760    @TestTargetNew(
761        level = TestLevel.COMPLETE,
762        notes = "",
763        method = "println",
764        args = {java.lang.String.class}
765    )
766    public void test_printlnLjava_lang_String() {
767        // Test for method void java.io.PrintStream.println(java.lang.String)
768        char c;
769        PrintStream os = new PrintStream(baos, true);
770        os.println("Hello World");
771        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
772        byte rbytes[] = new byte[100];
773        bis.read(rbytes, 0, 11);
774        assertEquals("Incorrect string written", "Hello World", new String(
775                rbytes, 0, 11));
776        assertTrue("Newline not written", (c = (char) bis.read()) == '\r'
777                || c == '\n');
778    }
779
780    /**
781     * @tests java.io.PrintStream#println(boolean)
782     */
783    @TestTargetNew(
784        level = TestLevel.COMPLETE,
785        notes = "",
786        method = "println",
787        args = {boolean.class}
788    )
789    public void test_printlnZ() {
790        // Test for method void java.io.PrintStream.println(boolean)
791        int c;
792        PrintStream os = new PrintStream(baos, true);
793        os.println(true);
794        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
795        byte[] rbuf = new byte[100];
796        bis.read(rbuf, 0, 4);
797        assertEquals("Incorrect boolean written", "true",
798                new String(rbuf, 0, 4));
799        assertTrue("Newline not written", (c = bis.read()) == '\r' || c == '\n');
800    }
801
802    /**
803     * @tests java.io.PrintStream#write(byte[], int, int)
804     */
805    @TestTargetNew(
806        level = TestLevel.COMPLETE,
807        notes = "",
808        method = "write",
809        args = {byte[].class, int.class, int.class}
810    )
811    public void test_write$BII() {
812        // Test for method void java.io.PrintStream.write(byte [], int, int)
813        PrintStream os = new PrintStream(baos, true);
814        os.write(fileString.getBytes(), 0, fileString.length());
815        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
816        byte rbytes[] = new byte[4000];
817        bis.read(rbytes, 0, fileString.length());
818        assertTrue("Incorrect bytes written", new String(rbytes, 0, fileString
819                .length()).equals(fileString));
820
821        try {
822            os.write(rbytes, -1, 1);
823            fail("IndexOutOfBoundsException should have been thrown.");
824        } catch (IndexOutOfBoundsException e) {
825            // expected
826        }
827
828        try {
829            os.write(rbytes, 0, -1);
830            fail("IndexOutOfBoundsException should have been thrown.");
831        } catch (IndexOutOfBoundsException e) {
832            // expected
833        }
834
835        try {
836            os.write(rbytes, 1, rbytes.length);
837            fail("IndexOutOfBoundsException should have been thrown.");
838        } catch (IndexOutOfBoundsException e) {
839            // expected
840        }
841    }
842
843    /**
844     * @tests java.io.PrintStream#write(int)
845     */
846    @TestTargetNew(
847        level = TestLevel.COMPLETE,
848        notes = "",
849        method = "write",
850        args = {int.class}
851    )
852    public void test_writeI() {
853        // Test for method void java.io.PrintStream.write(int)
854        PrintStream os = new PrintStream(baos, true);
855        os.write('t');
856        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
857        assertEquals("Incorrect char written", 't', bis.read());
858    }
859
860    /**
861     * @tests java.io.PrintStream#append(char)
862     */
863    @TestTargetNew(
864        level = TestLevel.COMPLETE,
865        notes = "",
866        method = "append",
867        args = {char.class}
868    )
869    public void test_appendChar() throws IOException {
870        char testChar = ' ';
871        ByteArrayOutputStream out = new ByteArrayOutputStream();
872        PrintStream printStream = new PrintStream(out);
873        printStream.append(testChar);
874        printStream.flush();
875        assertEquals(String.valueOf(testChar), out.toString());
876        printStream.close();
877    }
878
879    /**
880     * @tests java.io.PrintStream#append(CharSequence)
881     */
882    @TestTargetNew(
883        level = TestLevel.COMPLETE,
884        notes = "",
885        method = "append",
886        args = {java.lang.CharSequence.class}
887    )
888    public void test_appendCharSequence() {
889        String testString = "My Test String";
890        ByteArrayOutputStream out = new ByteArrayOutputStream();
891        PrintStream printStream = new PrintStream(out);
892        printStream.append(testString);
893        printStream.flush();
894        assertEquals(testString, out.toString());
895        printStream.close();
896    }
897
898    /**
899     * @tests java.io.PrintStream#append(CharSequence, int, int)
900     */
901    @TestTargetNew(
902        level = TestLevel.COMPLETE,
903        notes = "",
904        method = "append",
905        args = {java.lang.CharSequence.class, int.class, int.class}
906    )
907    public void test_appendCharSequenceIntInt() {
908        String testString = "My Test String";
909        ByteArrayOutputStream out = new ByteArrayOutputStream();
910        PrintStream printStream = new PrintStream(out);
911        printStream.append(testString, 1, 3);
912        printStream.flush();
913        assertEquals(testString.substring(1, 3), out.toString());
914        try {
915            printStream.append(testString, 4, 100);
916            fail("IndexOutOfBoundsException not thrown");
917        } catch (IndexOutOfBoundsException e) {
918            // expected
919        }
920        try {
921            printStream.append(testString, 100, 1);
922            fail("IndexOutOfBoundsException not thrown");
923        } catch (IndexOutOfBoundsException e) {
924            // expected
925        }
926        printStream.close();
927    }
928
929    /**
930     * @tests java.io.PrintStream#format(java.lang.String, java.lang.Object...)
931     */
932    @TestTargetNew(
933        level = TestLevel.COMPLETE,
934        notes = "",
935        method = "format",
936        args = {java.lang.String.class, java.lang.Object[].class}
937    )
938    public void test_formatLjava_lang_String$Ljava_lang_Object() {
939        PrintStream tobj;
940
941        tobj = new PrintStream(baos, false);
942        tobj.format("%s %s", "Hello", "World");
943        tobj.flush();
944        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
945        byte[] rbytes = new byte[11];
946        bis.read(rbytes, 0, rbytes.length);
947        assertEquals("Wrote incorrect string", "Hello World",
948                new String(rbytes));
949
950        baos.reset();
951        tobj = new PrintStream(baos);
952        tobj.format("%1$.3G, %1$.5f, 0%2$xx", 12345.678, 123456);
953        tobj.flush();
954        assertEquals("Wrong output!", "1.23E+04, 12345.67800, 01e240x", new String(baos.toByteArray()));
955        tobj.close();
956
957        baos.reset();
958        tobj = new PrintStream(baos);
959        try {
960            tobj.format("%1$.3G, %1$x", 12345.678);
961            fail("IllegalFormatException not thrown");
962        } catch (IllegalFormatException e) {
963            // expected
964        }
965
966        try {
967            tobj.format("%s %q", "Hello", "World");
968            fail("IllegalFormatException not thrown");
969        } catch (IllegalFormatException e) {
970            // expected
971        }
972
973        try {
974            tobj.format("%s %s", "Hello");
975            fail("IllegalFormatException not thrown");
976        } catch (IllegalFormatException e) {
977            // expected
978        }
979    }
980
981    /**
982     * @tests java.io.PrintStream#format(java.util.Locale, java.lang.String,
983     *        java.lang.Object...)
984     */
985    @TestTargetNew(
986        level = TestLevel.COMPLETE,
987        notes = "",
988        method = "format",
989        args = {java.util.Locale.class, java.lang.String.class, java.lang.Object[].class}
990    )
991    public void test_formatLjava_util_Locale_Ljava_lang_String_$Ljava_lang_Object() {
992        Locale[] requiredLocales = {Locale.US, Locale.GERMANY};
993        if (!Support_Locale.areLocalesAvailable(requiredLocales)) {
994            // locale dependent test, bug 1943269
995            return;
996        }
997        PrintStream tobj;
998
999        tobj = new PrintStream(baos, false);
1000        tobj.format(Locale.US, "%s %s", "Hello", "World");
1001        tobj.flush();
1002        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
1003        byte[] rbytes = new byte[11];
1004        bis.read(rbytes, 0, rbytes.length);
1005        assertEquals("Wrote incorrect string", "Hello World",
1006                new String(rbytes));
1007
1008        baos.reset();
1009        tobj = new PrintStream(baos);
1010        tobj.format(Locale.GERMANY, "%1$.3G; %1$.5f; 0%2$xx", 12345.678, 123456);
1011        tobj.flush();
1012        assertEquals("Wrong output!", "1,23E+04; 12345,67800; 01e240x", new String(baos.toByteArray()));
1013        tobj.close();
1014
1015        baos.reset();
1016        tobj = new PrintStream(baos);
1017        tobj.format(Locale.US, "%1$.3G, %1$.5f, 0%2$xx", 12345.678, 123456);
1018        tobj.flush();
1019        assertEquals("Wrong output!", "1.23E+04, 12345.67800, 01e240x", new String(baos.toByteArray()));
1020        tobj.close();
1021
1022        baos.reset();
1023        tobj = new PrintStream(baos);
1024        try {
1025            tobj.format(Locale.US, "%1$.3G, %1$x", 12345.678);
1026            fail("IllegalFormatException not thrown");
1027        } catch (IllegalFormatException e) {
1028            // expected
1029        }
1030
1031        try {
1032            tobj.format(Locale.US, "%s %q", "Hello", "World");
1033            fail("IllegalFormatException not thrown");
1034        } catch (IllegalFormatException e) {
1035            // expected
1036        }
1037
1038        try {
1039            tobj.format(Locale.US, "%s %s", "Hello");
1040            fail("IllegalFormatException not thrown");
1041        } catch (IllegalFormatException e) {
1042            // expected
1043        }
1044    }
1045
1046    /**
1047     * @tests java.io.PrintStream#printf(java.lang.String, java.lang.Object...)
1048     */
1049    @TestTargetNew(
1050        level = TestLevel.COMPLETE,
1051        notes = "",
1052        method = "printf",
1053        args = {java.lang.String.class, java.lang.Object[].class}
1054    )
1055    public void test_printfLjava_lang_String$Ljava_lang_Object() {
1056        PrintStream tobj;
1057
1058        tobj = new PrintStream(baos, false);
1059        tobj.printf("%s %s", "Hello", "World");
1060        tobj.flush();
1061        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
1062        byte[] rbytes = new byte[11];
1063        bis.read(rbytes, 0, rbytes.length);
1064        assertEquals("Wrote incorrect string", "Hello World",
1065                new String(rbytes));
1066
1067        baos.reset();
1068        tobj = new PrintStream(baos);
1069        tobj.printf("%1$.3G, %1$.5f, 0%2$xx", 12345.678, 123456);
1070        tobj.flush();
1071        assertEquals("Wrong output!", "1.23E+04, 12345.67800, 01e240x", new String(baos.toByteArray()));
1072        tobj.close();
1073
1074        baos.reset();
1075        tobj = new PrintStream(baos);
1076        try {
1077            tobj.printf("%1$.3G, %1$x", 12345.678);
1078            fail("IllegalFormatException not thrown");
1079        } catch (IllegalFormatException e) {
1080            // expected
1081        }
1082
1083        try {
1084            tobj.printf("%s %q", "Hello", "World");
1085            fail("IllegalFormatException not thrown");
1086        } catch (IllegalFormatException e) {
1087            // expected
1088        }
1089
1090        try {
1091            tobj.printf("%s %s", "Hello");
1092            fail("IllegalFormatException not thrown");
1093        } catch (IllegalFormatException e) {
1094            // expected
1095        }
1096    }
1097
1098    /**
1099     * @tests java.io.PrintStream#printf(java.util.Locale, java.lang.String,
1100     *        java.lang.Object...)
1101     */
1102    @TestTargetNew(
1103        level = TestLevel.COMPLETE,
1104        notes = "",
1105        method = "printf",
1106        args = {java.util.Locale.class, java.lang.String.class, java.lang.Object[].class}
1107    )
1108    public void test_printfLjava_util_Locale_Ljava_lang_String_$Ljava_lang_Object() {
1109        Locale[] requiredLocales = {Locale.US, Locale.GERMANY};
1110        if (!Support_Locale.areLocalesAvailable(requiredLocales)) {
1111            // locale dependent test, bug 1943269
1112            return;
1113        }
1114        PrintStream tobj;
1115
1116        tobj = new PrintStream(baos, false);
1117        tobj.printf(Locale.US, "%s %s", "Hello", "World");
1118        tobj.flush();
1119        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
1120        byte[] rbytes = new byte[11];
1121        bis.read(rbytes, 0, rbytes.length);
1122        assertEquals("Wrote incorrect string", "Hello World",
1123                new String(rbytes));
1124
1125        baos.reset();
1126        tobj = new PrintStream(baos);
1127        tobj.printf(Locale.GERMANY, "%1$.3G; %1$.5f; 0%2$xx", 12345.678, 123456);
1128        tobj.flush();
1129        assertEquals("Wrong output!", "1,23E+04; 12345,67800; 01e240x", new String(baos.toByteArray()));
1130        tobj.close();
1131
1132        baos.reset();
1133        tobj = new PrintStream(baos);
1134        tobj.printf(Locale.US, "%1$.3G, %1$.5f, 0%2$xx", 12345.678, 123456);
1135        tobj.flush();
1136        assertEquals("Wrong output!", "1.23E+04, 12345.67800, 01e240x", new String(baos.toByteArray()));
1137        tobj.close();
1138
1139        baos.reset();
1140        tobj = new PrintStream(baos);
1141        try {
1142            tobj.printf(Locale.US, "%1$.3G, %1$x", 12345.678);
1143            fail("IllegalFormatException not thrown");
1144        } catch (IllegalFormatException e) {
1145            // expected
1146        }
1147
1148        try {
1149            tobj.printf(Locale.US, "%s %q", "Hello", "World");
1150            fail("IllegalFormatException not thrown");
1151        } catch (IllegalFormatException e) {
1152            // expected
1153        }
1154
1155        try {
1156            tobj.printf(Locale.US, "%s %s", "Hello");
1157            fail("IllegalFormatException not thrown");
1158        } catch (IllegalFormatException e) {
1159            // expected
1160        }
1161    }
1162
1163    @Override
1164    protected void setUp() throws Exception {
1165        super.setUp();
1166        testFile = File.createTempFile("test", null);
1167        testFilePath = testFile.getAbsolutePath();
1168    }
1169
1170    @Override
1171    protected void tearDown() throws Exception {
1172        testFile.delete();
1173        testFile = null;
1174        testFilePath = null;
1175        super.tearDown();
1176    }
1177
1178
1179}
1180