1 32 package net.sf.retrotranslator.runtime.java.io; 33 34 import java.io.*; 35 import java.util.Locale ; 36 import net.sf.retrotranslator.tests.BaseTestCase; 37 38 41 public class _PrintStreamTestCase extends BaseTestCase { 42 43 static class MyPrintStream extends PrintStream { 44 public MyPrintStream(File file) throws FileNotFoundException { 45 super(file); 46 } 47 48 public MyPrintStream(String fileName, String csn) 49 throws FileNotFoundException, UnsupportedEncodingException { 50 super(fileName, csn); 51 } 52 } 53 54 public void testConstructors() throws Exception { 55 File file = File.createTempFile("_PrintStreamTestCase", ".tmp"); 56 try { 57 writeAndClose(new PrintStream(file), "test1"); 58 assertEquals("test1", readLine(file, null)); 59 60 writeAndClose(new PrintStream(file, "UTF-16"), "test2"); 61 assertEquals("test2", readLine(file, "UTF-16")); 62 63 writeAndClose(new PrintStream(file.getPath()), "test3"); 64 assertEquals("test3", readLine(file, null)); 65 66 writeAndClose(new PrintStream(file.getPath(), "UTF-16"), "test4"); 67 assertEquals("test4", readLine(file, "UTF-16")); 68 69 writeAndClose(new MyPrintStream(file), "test5"); 70 assertEquals("test5", readLine(file, null)); 71 72 writeAndClose(new MyPrintStream(file.getPath(), "UTF-16"), "test6"); 73 assertEquals("test6", readLine(file, "UTF-16")); 74 75 } finally { 76 file.delete(); 77 } 78 } 79 80 private static void writeAndClose(PrintStream writer, String s) throws Exception { 81 try { 82 writer.print(s); 83 } finally { 84 writer.close(); 85 } 86 } 87 88 public void testAppend() throws Exception { 89 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 90 PrintStream printStream = new PrintStream(stream); 91 printStream.append("Hi,"); 92 printStream.append("Hello", 1, 3); 93 printStream.append('!'); 94 assertEquals("Hi,el!", stream.toString()); 95 } 96 97 public void testFormat() throws Exception { 98 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 99 PrintStream printStream = new PrintStream(stream); 100 printStream.format("Hello, %s!", "World"); 101 printStream.format(Locale.FRANCE, " - %,d", 1000000); 102 assertEquals("Hello, World! - 1 000 000", stream.toString()); 103 } 104 105 public void testPrintf() throws Exception { 106 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 107 PrintStream printStream = new PrintStream(stream); 108 printStream.printf("Hello, %s!", "World"); 109 printStream.printf(Locale.FRANCE, " - %f", 1.2); 110 assertEquals("Hello, World! - 1,200000", stream.toString()); 111 } 112 113 } | Popular Tags |