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.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.DataInputStream;
023: import java.io.File;
024: import java.io.FileNotFoundException;
025: import java.io.IOException;
026: import java.io.OutputStream;
027: import java.io.PrintStream;
028: import java.io.UnsupportedEncodingException;
029: import java.util.Locale;
030:
031: public class PrintStreamTest extends junit.framework.TestCase {
032:
033: ByteArrayOutputStream bos = new ByteArrayOutputStream();
034:
035: byte[] ibuf = new byte[4096];
036:
037: private File testFile = null;
038:
039: private String testFilePath = null;
040:
041: 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";
042:
043: private static class MockPrintStream extends PrintStream {
044:
045: public MockPrintStream(String fileName)
046: throws FileNotFoundException {
047: super (fileName);
048: }
049:
050: public MockPrintStream(String fileName, String csn)
051: throws FileNotFoundException,
052: UnsupportedEncodingException {
053: super (fileName, csn);
054: }
055:
056: public MockPrintStream(OutputStream os) {
057: super (os);
058: }
059:
060: @Override
061: public void setError() {
062: super .setError();
063: }
064: }
065:
066: /**
067: * @tests {@link java.io.PrintStream#PrintStream(String)}
068: */
069: public void test_Constructor_Ljava_lang_String() throws IOException {
070: MockPrintStream os = new MockPrintStream(testFilePath);
071: assertNotNull(os);
072: os.close();
073: }
074:
075: /**
076: * @tests {@link java.io.PrintStream#PrintStream(String, String)}
077: */
078: public void test_Constructor_Ljava_lang_String_Ljava_lang_String()
079: throws Exception {
080: MockPrintStream os = new MockPrintStream(testFilePath, "utf-8");
081: assertNotNull(os);
082: os.close();
083: }
084:
085: /**
086: * @tests java.io.PrintStream#PrintStream(java.io.OutputStream)
087: */
088: public void test_ConstructorLjava_io_OutputStream()
089: throws Exception {
090: // Test for method java.io.PrintStream(java.io.OutputStream)
091: PrintStream os = new PrintStream(bos);
092: os.print(2345.76834720202);
093: os.close();
094:
095: // regression for HARMONY-1195
096: try {
097: os = new PrintStream(bos, true, null);
098: fail("Should throw NPE");
099: } catch (NullPointerException e) {
100: }
101: }
102:
103: /**
104: * @tests java.io.PrintStream#PrintStream(java.io.OutputStream, boolean)
105: */
106: public void test_ConstructorLjava_io_OutputStreamZ() {
107: // Test for method java.io.PrintStream(java.io.OutputStream, boolean)
108: PrintStream os = new PrintStream(bos);
109: os.println(2345.76834720202);
110: os.flush();
111: assertTrue("Bytes not written", bos.size() > 0);
112: os.close();
113: }
114:
115: /**
116: * @tests java.io.PrintStream#PrintStream(java.io.OutputStream, boolean, String)
117: */
118: public void test_ConstructorLjava_io_OutputStreamZLjava_lang_String() {
119: try {
120: new PrintStream(new ByteArrayOutputStream(), false,
121: "%Illegal_name!");
122: fail("Expected UnsupportedEncodingException");
123: } catch (UnsupportedEncodingException e) {
124: // expected
125: }
126: }
127:
128: /**
129: * @tests java.io.PrintStream#checkError()
130: */
131: public void test_checkError() throws Exception {
132: // Test for method boolean java.io.PrintStream.checkError()
133: PrintStream os = new PrintStream(new OutputStream() {
134:
135: public void write(int b) throws IOException {
136: throw new IOException();
137: }
138:
139: public void write(byte[] b, int o, int l)
140: throws IOException {
141: throw new IOException();
142: }
143: });
144: os.print(fileString.substring(0, 501));
145:
146: assertTrue("Checkerror should return true", os.checkError());
147: }
148:
149: /**
150: * @tests java.io.PrintStream#close()
151: */
152: public void test_close() throws Exception {
153: // Test for method void java.io.PrintStream.close()
154: PrintStream os = new PrintStream(bos);
155: os.close();
156: bos.close();
157: }
158:
159: /**
160: * @tests java.io.PrintStream#flush()
161: */
162: public void test_flush() throws Exception {
163: // Test for method void java.io.PrintStream.flush()
164: PrintStream os = new PrintStream(bos);
165: os.print(fileString.substring(0, 501));
166: os.flush();
167: assertEquals("Bytes not written after flush", 501, bos.size());
168: bos.close();
169: os.close();
170: }
171:
172: /**
173: * @tests java.io.PrintStream#print(char[])
174: */
175: public void test_print$C() {
176: // Test for method void java.io.PrintStream.print(char [])
177: PrintStream os = new PrintStream(bos, true);
178: try {
179: os.print((char[]) null);
180: fail("NPE expected");
181: } catch (NullPointerException ok) {
182: }
183:
184: os = new PrintStream(bos, true);
185: char[] sc = new char[4000];
186: fileString.getChars(0, fileString.length(), sc, 0);
187: os.print(sc);
188: ByteArrayInputStream bis = new ByteArrayInputStream(bos
189: .toByteArray());
190: os.close();
191:
192: byte[] rbytes = new byte[4000];
193: bis.read(rbytes, 0, fileString.length());
194: assertEquals("Incorrect char[] written", fileString,
195: new String(rbytes, 0, fileString.length()));
196: }
197:
198: /**
199: * @tests java.io.PrintStream#print(char)
200: */
201: public void test_printC() {
202: // Test for method void java.io.PrintStream.print(char)
203: PrintStream os = new PrintStream(bos, true);
204: os.print('t');
205: ByteArrayInputStream bis = new ByteArrayInputStream(bos
206: .toByteArray());
207: assertEquals("Incorrect char written", 't', bis.read());
208: }
209:
210: /**
211: * @tests java.io.PrintStream#print(double)
212: */
213: public void test_printD() {
214: // Test for method void java.io.PrintStream.print(double)
215: byte[] rbuf = new byte[100];
216: PrintStream os = new PrintStream(bos, true);
217: os.print(2345.76834720202);
218: ByteArrayInputStream bis = new ByteArrayInputStream(bos
219: .toByteArray());
220: bis.read(rbuf, 0, 16);
221: assertEquals("Incorrect double written", "2345.76834720202",
222: new String(rbuf, 0, 16));
223: }
224:
225: /**
226: * @tests java.io.PrintStream#print(float)
227: */
228: public void test_printF() {
229: // Test for method void java.io.PrintStream.print(float)
230: PrintStream os = new PrintStream(bos, true);
231: byte rbuf[] = new byte[10];
232: os.print(29.08764f);
233: os.flush();
234: ByteArrayInputStream bis = new ByteArrayInputStream(bos
235: .toByteArray());
236: bis.read(rbuf, 0, 8);
237: assertEquals("Incorrect float written", "29.08764", new String(
238: rbuf, 0, 8));
239:
240: }
241:
242: /**
243: * @tests java.io.PrintStream#print(int)
244: */
245: public void test_printI() {
246: // Test for method void java.io.PrintStream.print(int)
247: PrintStream os = new PrintStream(bos, true);
248: os.print(768347202);
249: byte[] rbuf = new byte[18];
250: ByteArrayInputStream bis = new ByteArrayInputStream(bos
251: .toByteArray());
252: bis.read(rbuf, 0, 9);
253: assertEquals("Incorrect int written", "768347202", new String(
254: rbuf, 0, 9));
255: }
256:
257: /**
258: * @tests java.io.PrintStream#print(long)
259: */
260: public void test_printJ() {
261: // Test for method void java.io.PrintStream.print(long)
262: byte[] rbuf = new byte[100];
263: PrintStream os = new PrintStream(bos, true);
264: os.print(9875645283333L);
265: os.close();
266: ByteArrayInputStream bis = new ByteArrayInputStream(bos
267: .toByteArray());
268: bis.read(rbuf, 0, 13);
269: assertEquals("Incorrect long written", "9875645283333",
270: new String(rbuf, 0, 13));
271: }
272:
273: /**
274: * @tests java.io.PrintStream#print(java.lang.Object)
275: */
276: public void test_printLjava_lang_Object() throws Exception {
277: // Test for method void java.io.PrintStream.print(java.lang.Object)
278: PrintStream os = new PrintStream(bos, true);
279: os.print((Object) null);
280: os.flush();
281: ByteArrayInputStream bis = new ByteArrayInputStream(bos
282: .toByteArray());
283: byte[] nullbytes = new byte[4];
284: bis.read(nullbytes, 0, 4);
285: assertEquals("null should be written", "null", new String(
286: nullbytes, 0, 4));
287:
288: bis.close();
289: bos.close();
290: os.close();
291:
292: ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
293: os = new PrintStream(bos1, true);
294: os.print(new java.util.Vector());
295: bis = new ByteArrayInputStream(bos1.toByteArray());
296: byte[] rbytes = new byte[2];
297: bis.read(rbytes, 0, 2);
298: assertEquals("Incorrect Object written", "[]", new String(
299: rbytes, 0, 2));
300: }
301:
302: /**
303: * @tests java.io.PrintStream#print(java.lang.String)
304: */
305: public void test_printLjava_lang_String() throws Exception {
306: // Test for method void java.io.PrintStream.print(java.lang.String)
307: PrintStream os = new PrintStream(bos, true);
308: os.print((String) null);
309: ByteArrayInputStream bis = new ByteArrayInputStream(bos
310: .toByteArray());
311: byte[] nullbytes = new byte[4];
312: bis.read(nullbytes, 0, 4);
313: assertEquals("null should be written", "null", new String(
314: nullbytes, 0, 4));
315:
316: bis.close();
317: bos.close();
318: os.close();
319:
320: ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
321: os = new PrintStream(bos1, true);
322: os.print("Hello World");
323: bis = new ByteArrayInputStream(bos1.toByteArray());
324: byte rbytes[] = new byte[100];
325: bis.read(rbytes, 0, 11);
326: assertEquals("Incorrect string written", "Hello World",
327: new String(rbytes, 0, 11));
328: }
329:
330: /**
331: * @tests java.io.PrintStream#print(boolean)
332: */
333: public void test_printZ() throws Exception {
334: // Test for method void java.io.PrintStream.print(boolean)
335: PrintStream os = new PrintStream(bos, true);
336: os.print(true);
337: DataInputStream dis = new DataInputStream(
338: new ByteArrayInputStream(bos.toByteArray()));
339:
340: assertTrue("Incorrect boolean written", dis.readBoolean());
341: }
342:
343: /**
344: * @tests java.io.PrintStream#println()
345: */
346: public void test_println() {
347: // Test for method void java.io.PrintStream.println()
348: char c;
349: PrintStream os = new PrintStream(bos, true);
350: os.println("");
351: ByteArrayInputStream bis = new ByteArrayInputStream(bos
352: .toByteArray());
353: assertTrue("Newline not written",
354: (c = (char) bis.read()) == '\r' || c == '\n');
355: }
356:
357: /**
358: * @tests java.io.PrintStream#println(char[])
359: */
360: public void test_println$C() {
361: // Test for method void java.io.PrintStream.println(char [])
362: PrintStream os = new PrintStream(bos, true);
363: char[] sc = new char[4000];
364: fileString.getChars(0, fileString.length(), sc, 0);
365: os.println(sc);
366: ByteArrayInputStream bis = new ByteArrayInputStream(bos
367: .toByteArray());
368: byte[] rbytes = new byte[4000];
369: bis.read(rbytes, 0, fileString.length());
370: assertEquals("Incorrect char[] written", fileString,
371: new String(rbytes, 0, fileString.length()));
372:
373: // In this particular test method, the end of data is not immediately
374: // followed by newLine separator in the reading buffer, instead its
375: // followed by zeros. The newline is written as the last entry
376: // in the inputStream buffer. Therefore, we must keep reading until we
377: // hit a new line.
378: int r;
379: boolean newline = false;
380: while ((r = bis.read()) != -1) {
381: if (r == '\r' || r == '\n')
382: newline = true;
383: }
384: assertTrue("Newline not written", newline);
385: }
386:
387: /**
388: * @tests java.io.PrintStream#println(char)
389: */
390: public void test_printlnC() {
391: // Test for method void java.io.PrintStream.println(char)
392: int c;
393: PrintStream os = new PrintStream(bos, true);
394: os.println('t');
395: ByteArrayInputStream bis = new ByteArrayInputStream(bos
396: .toByteArray());
397: assertEquals("Incorrect char written", 't', bis.read());
398: assertTrue("Newline not written", (c = bis.read()) == '\r'
399: || c == '\n');
400: }
401:
402: /**
403: * @tests java.io.PrintStream#println(double)
404: */
405: public void test_printlnD() {
406: // Test for method void java.io.PrintStream.println(double)
407: int c;
408: PrintStream os = new PrintStream(bos, true);
409: os.println(2345.76834720202);
410: ByteArrayInputStream bis = new ByteArrayInputStream(bos
411: .toByteArray());
412: byte[] rbuf = new byte[100];
413: bis.read(rbuf, 0, 16);
414: assertEquals("Incorrect double written", "2345.76834720202",
415: new String(rbuf, 0, 16));
416: assertTrue("Newline not written", (c = bis.read()) == '\r'
417: || c == '\n');
418: }
419:
420: /**
421: * @tests java.io.PrintStream#println(float)
422: */
423: public void test_printlnF() {
424: // Test for method void java.io.PrintStream.println(float)
425: int c;
426: byte[] rbuf = new byte[100];
427: PrintStream os = new PrintStream(bos, true);
428: os.println(29.08764f);
429: ByteArrayInputStream bis = new ByteArrayInputStream(bos
430: .toByteArray());
431: bis.read(rbuf, 0, 8);
432: assertEquals("Incorrect float written", "29.08764", new String(
433: rbuf, 0, 8));
434: assertTrue("Newline not written", (c = bis.read()) == '\r'
435: || c == '\n');
436: }
437:
438: /**
439: * @tests java.io.PrintStream#println(int)
440: */
441: public void test_printlnI() {
442: // Test for method void java.io.PrintStream.println(int)
443: int c;
444: PrintStream os = new PrintStream(bos, true);
445: os.println(768347202);
446: byte[] rbuf = new byte[100];
447: ByteArrayInputStream bis = new ByteArrayInputStream(bos
448: .toByteArray());
449: bis.read(rbuf, 0, 9);
450: assertEquals("Incorrect int written", "768347202", new String(
451: rbuf, 0, 9));
452: assertTrue("Newline not written", (c = bis.read()) == '\r'
453: || c == '\n');
454: }
455:
456: /**
457: * @tests java.io.PrintStream#println(long)
458: */
459: public void test_printlnJ() {
460: // Test for method void java.io.PrintStream.println(long)
461: int c;
462: PrintStream os = new PrintStream(bos, true);
463: os.println(9875645283333L);
464: ByteArrayInputStream bis = new ByteArrayInputStream(bos
465: .toByteArray());
466: byte[] rbuf = new byte[100];
467: bis.read(rbuf, 0, 13);
468: assertEquals("Incorrect long written", "9875645283333",
469: new String(rbuf, 0, 13));
470: assertTrue("Newline not written", (c = bis.read()) == '\r'
471: || c == '\n');
472: }
473:
474: /**
475: * @tests java.io.PrintStream#println(java.lang.Object)
476: */
477: public void test_printlnLjava_lang_Object() {
478: // Test for method void java.io.PrintStream.println(java.lang.Object)
479: char c;
480: PrintStream os = new PrintStream(bos, true);
481: os.println(new java.util.Vector());
482: ByteArrayInputStream bis = new ByteArrayInputStream(bos
483: .toByteArray());
484: byte[] rbytes = new byte[2];
485: bis.read(rbytes, 0, 2);
486: assertEquals("Incorrect Vector written", "[]", new String(
487: rbytes, 0, 2));
488: assertTrue("Newline not written",
489: (c = (char) bis.read()) == '\r' || c == '\n');
490: }
491:
492: /**
493: * @tests java.io.PrintStream#println(java.lang.String)
494: */
495: public void test_printlnLjava_lang_String() {
496: // Test for method void java.io.PrintStream.println(java.lang.String)
497: char c;
498: PrintStream os = new PrintStream(bos, true);
499: os.println("Hello World");
500: ByteArrayInputStream bis = new ByteArrayInputStream(bos
501: .toByteArray());
502: byte rbytes[] = new byte[100];
503: bis.read(rbytes, 0, 11);
504: assertEquals("Incorrect string written", "Hello World",
505: new String(rbytes, 0, 11));
506: assertTrue("Newline not written",
507: (c = (char) bis.read()) == '\r' || c == '\n');
508: }
509:
510: /**
511: * @tests java.io.PrintStream#println(boolean)
512: */
513: public void test_printlnZ() {
514: // Test for method void java.io.PrintStream.println(boolean)
515: int c;
516: PrintStream os = new PrintStream(bos, true);
517: os.println(true);
518: ByteArrayInputStream bis = new ByteArrayInputStream(bos
519: .toByteArray());
520: byte[] rbuf = new byte[100];
521: bis.read(rbuf, 0, 4);
522: assertEquals("Incorrect boolean written", "true", new String(
523: rbuf, 0, 4));
524: assertTrue("Newline not written", (c = bis.read()) == '\r'
525: || c == '\n');
526: }
527:
528: /**
529: * @tests java.io.PrintStream#write(byte[], int, int)
530: */
531: public void test_write$BII() {
532: // Test for method void java.io.PrintStream.write(byte [], int, int)
533: PrintStream os = new PrintStream(bos, true);
534: os.write(fileString.getBytes(), 0, fileString.length());
535: ByteArrayInputStream bis = new ByteArrayInputStream(bos
536: .toByteArray());
537: byte rbytes[] = new byte[4000];
538: bis.read(rbytes, 0, fileString.length());
539: assertTrue("Incorrect bytes written", new String(rbytes, 0,
540: fileString.length()).equals(fileString));
541: }
542:
543: /**
544: * @tests java.io.PrintStream#write(int)
545: */
546: public void test_writeI() {
547: // Test for method void java.io.PrintStream.write(int)
548: PrintStream os = new PrintStream(bos, true);
549: os.write('t');
550: ByteArrayInputStream bis = new ByteArrayInputStream(bos
551: .toByteArray());
552: assertEquals("Incorrect char written", 't', bis.read());
553: }
554:
555: /**
556: * @tests java.io.PrintStream#append(char)
557: */
558: public void test_appendChar() throws IOException {
559: char testChar = ' ';
560: ByteArrayOutputStream out = new ByteArrayOutputStream();
561: PrintStream printStream = new PrintStream(out);
562: printStream.append(testChar);
563: printStream.flush();
564: assertEquals(String.valueOf(testChar), out.toString());
565: printStream.close();
566: }
567:
568: /**
569: * @tests java.io.PrintStream#append(CharSequence)
570: */
571: public void test_appendCharSequence() {
572: String testString = "My Test String";
573: ByteArrayOutputStream out = new ByteArrayOutputStream();
574: PrintStream printStream = new PrintStream(out);
575: printStream.append(testString);
576: printStream.flush();
577: assertEquals(testString, out.toString());
578: printStream.close();
579: }
580:
581: /**
582: * @tests java.io.PrintStream#append(CharSequence, int, int)
583: */
584: public void test_appendCharSequenceIntInt() {
585: String testString = "My Test String";
586: ByteArrayOutputStream out = new ByteArrayOutputStream();
587: PrintStream printStream = new PrintStream(out);
588: printStream.append(testString, 1, 3);
589: printStream.flush();
590: assertEquals(testString.substring(1, 3), out.toString());
591: printStream.close();
592: }
593:
594: /**
595: * @tests java.io.PrintStream#format(java.lang.String, java.lang.Object...)
596: */
597: public void test_formatLjava_lang_String$Ljava_lang_Object() {
598: PrintStream os = new PrintStream(bos, false);
599: os.format("%s %s", "Hello", "World");
600: os.flush();
601: ByteArrayInputStream bis = new ByteArrayInputStream(bos
602: .toByteArray());
603: byte[] rbytes = new byte[11];
604: bis.read(rbytes, 0, rbytes.length);
605: assertEquals("Wrote incorrect string", "Hello World",
606: new String(rbytes));
607:
608: }
609:
610: /**
611: * @tests java.io.PrintStream#format(java.util.Locale, java.lang.String,
612: * java.lang.Object...)
613: */
614: public void test_formatLjava_util_Locale_Ljava_lang_String_$Ljava_lang_Object() {
615: PrintStream os = new PrintStream(bos, false);
616: os.format(Locale.US, "%s %s", "Hello", "World");
617: os.flush();
618: ByteArrayInputStream bis = new ByteArrayInputStream(bos
619: .toByteArray());
620: byte[] rbytes = new byte[11];
621: bis.read(rbytes, 0, rbytes.length);
622: assertEquals("Wrote incorrect string", "Hello World",
623: new String(rbytes));
624: }
625:
626: /**
627: * @tests java.io.PrintStream#printf(java.lang.String, java.lang.Object...)
628: */
629: public void test_printfLjava_lang_String$Ljava_lang_Object() {
630: PrintStream os = new PrintStream(bos, false);
631: os.printf("%s %s", "Hello", "World");
632: os.flush();
633: ByteArrayInputStream bis = new ByteArrayInputStream(bos
634: .toByteArray());
635: byte[] rbytes = new byte[11];
636: bis.read(rbytes, 0, rbytes.length);
637: assertEquals("Wrote incorrect string", "Hello World",
638: new String(rbytes));
639: }
640:
641: /**
642: * @tests java.io.PrintStream#printf(java.util.Locale, java.lang.String,
643: * java.lang.Object...)
644: */
645: public void test_printfLjava_util_Locale_Ljava_lang_String_$Ljava_lang_Object() {
646: PrintStream os = new PrintStream(bos, false);
647: os.printf(Locale.US, "%s %s", "Hello", "World");
648: os.flush();
649: ByteArrayInputStream bis = new ByteArrayInputStream(bos
650: .toByteArray());
651: byte[] rbytes = new byte[11];
652: bis.read(rbytes, 0, rbytes.length);
653: assertEquals("Wrote incorrect string", "Hello World",
654: new String(rbytes));
655: }
656:
657: @Override
658: protected void setUp() throws Exception {
659: super .setUp();
660: testFile = File.createTempFile("test", null);
661: testFilePath = testFile.getAbsolutePath();
662: }
663:
664: @Override
665: protected void tearDown() throws Exception {
666: testFile.delete();
667: testFile = null;
668: testFilePath = null;
669: super.tearDown();
670: }
671:
672: }
|