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 _PrintWriterTestCase extends BaseTestCase { 42 43 public void testConstructors() throws Exception { 44 File file = File.createTempFile("_PrintWriterTestCase", ".tmp"); 45 try { 46 writeAndClose(new PrintWriter(file), "test1"); 47 assertEquals("test1", readLine(file, null)); 48 49 writeAndClose(new PrintWriter(file, "UTF-16"), "test2"); 50 assertEquals("test2", readLine(file, "UTF-16")); 51 52 writeAndClose(new PrintWriter(file.getPath()), "test3"); 53 assertEquals("test3", readLine(file, null)); 54 55 writeAndClose(new PrintWriter(file.getPath(), "UTF-16"), "test4"); 56 assertEquals("test4", readLine(file, "UTF-16")); 57 } finally { 58 file.delete(); 59 } 60 } 61 62 private static void writeAndClose(PrintWriter writer, String s) throws Exception { 63 try { 64 writer.write(s); 65 } finally { 66 writer.close(); 67 } 68 } 69 70 public void testFormat() throws Exception { 71 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 72 PrintWriter writer = new PrintWriter(stream, true); 73 writer.format("Hello, %s!", "World"); 74 writer.format(Locale.FRANCE, " - %f", 1.2); 75 assertEquals("Hello, World! - 1,200000", stream.toString()); 76 } 77 78 public void testPrintf() throws Exception { 79 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 80 PrintWriter writer = new PrintWriter(stream, true); 81 writer.printf("Hello, %s!", "World"); 82 writer.printf(Locale.FRANCE, " - %,d", 1000000); 83 assertEquals("Hello, World! - 1 000 000", stream.toString()); 84 } 85 } | Popular Tags |