1 32 package net.sf.retrotranslator.runtime.java.util; 33 34 import java.io.*; 35 import java.util.*; 36 import net.sf.retrotranslator.tests.BaseTestCase; 37 38 41 public class Formatter_TestCase extends BaseTestCase { 42 43 public void testFormatter_1() throws Exception { 44 Formatter formatter = new Formatter(); 45 assertTrue(formatter.out() instanceof StringBuilder ); 46 assertEquals(Locale.getDefault(), formatter.locale()); 47 } 48 49 public void testFormatter_2() throws Exception { 50 StringBuffer buffer = new StringBuffer (); 51 Formatter formatter = new Formatter(buffer); 52 assertSame(buffer, formatter.out()); 53 assertEquals(Locale.getDefault(), formatter.locale()); 54 } 55 56 public void testFormatter_3() throws Exception { 57 Formatter formatter = new Formatter(Locale.CANADA_FRENCH); 58 assertTrue(formatter.out() instanceof StringBuilder ); 59 assertSame(Locale.CANADA_FRENCH, formatter.locale()); 60 } 61 62 public void testFormatter_4() throws Exception { 63 StringBuffer buffer = new StringBuffer (); 64 Formatter formatter = new Formatter(buffer, Locale.CANADA_FRENCH); 65 assertSame(buffer, formatter.out()); 66 assertSame(Locale.CANADA_FRENCH, formatter.locale()); 67 } 68 69 public void testFormatter_5() throws Exception { 70 File file = getFile(); 71 Formatter formatter = new Formatter(file.getPath()); 72 assertEquals(Locale.getDefault(), formatter.locale()); 73 assertToFile(formatter, file); 74 } 75 76 public void testFormatter_6() throws Exception { 77 File file = getFile(); 78 Formatter formatter = new Formatter(file.getPath(), "UTF-8"); 79 assertEquals(Locale.getDefault(), formatter.locale()); 80 assertToFile(formatter, file); 81 } 82 83 public void testFormatter_7() throws Exception { 84 File file = getFile(); 85 Formatter formatter = new Formatter(file.getPath(), "UTF-8", Locale.CANADA_FRENCH); 86 assertSame(Locale.CANADA_FRENCH, formatter.locale()); 87 assertToFile(formatter, file); 88 } 89 90 public void testFormatter_8() throws Exception { 91 File file = getFile(); 92 Formatter formatter = new Formatter(file); 93 assertEquals(Locale.getDefault(), formatter.locale()); 94 assertToFile(formatter, file); 95 } 96 97 public void testFormatter_9() throws Exception { 98 File file = getFile(); 99 Formatter formatter = new Formatter(file, "UTF-8"); 100 assertEquals(Locale.getDefault(), formatter.locale()); 101 assertToFile(formatter, file); 102 } 103 104 public void testFormatter_10() throws Exception { 105 File file = getFile(); 106 Formatter formatter = new Formatter(file, "UTF-8", Locale.CANADA_FRENCH); 107 assertSame(Locale.CANADA_FRENCH, formatter.locale()); 108 assertToFile(formatter, file); 109 } 110 111 public void testFormatter_11() throws Exception { 112 File file = getFile(); 113 Formatter formatter = new Formatter(new PrintStream(new FileOutputStream(file))); 114 assertEquals(Locale.getDefault(), formatter.locale()); 115 assertToFile(formatter, file); 116 } 117 118 public void testFormatter_12() throws Exception { 119 File file = getFile(); 120 Formatter formatter = new Formatter(new FileOutputStream(file)); 121 assertEquals(Locale.getDefault(), formatter.locale()); 122 assertToFile(formatter, file); 123 } 124 125 public void testFormatter_13() throws Exception { 126 File file = getFile(); 127 Formatter formatter = new Formatter(new FileOutputStream(file), "UTF-8"); 128 assertEquals(Locale.getDefault(), formatter.locale()); 129 assertToFile(formatter, file); 130 } 131 132 public void testFormatter_14() throws Exception { 133 File file = getFile(); 134 Formatter formatter = new Formatter(new FileOutputStream(file), "UTF-8", Locale.CANADA_FRENCH); 135 assertSame(Locale.CANADA_FRENCH, formatter.locale()); 136 assertToFile(formatter, file); 137 } 138 139 private File getFile() throws IOException { 140 File file = File.createTempFile("fmt", "tmp"); 141 file.deleteOnExit(); 142 return file; 143 } 144 145 private void assertToFile(Formatter formatter, File file) throws Exception { 146 try { 147 formatter.format("%s", "Hello"); 148 } finally { 149 formatter.close(); 150 } 151 BufferedReader reader = new BufferedReader(new FileReader(file)); 152 try { 153 assertEquals("Hello", reader.readLine()); 154 } finally { 155 reader.close(); 156 } 157 } 158 159 static class MyWriter extends StringWriter { 160 161 private boolean flushed; 162 private boolean closed; 163 164 public void flush() { 165 flushed = true; 166 super.flush(); 167 } 168 169 public void close() throws IOException { 170 closed = true; 171 super.close(); 172 } 173 } 174 175 public void testFlush() throws Exception { 176 new Formatter().flush(); 177 MyWriter writer = new MyWriter(); 178 Formatter formatter = new Formatter(writer); 179 formatter.format("%s", "x"); 180 assertFalse(writer.flushed); 181 formatter.flush(); 182 assertTrue(writer.flushed); 183 assertFalse(writer.closed); 184 } 185 186 public void testClose() throws Exception { 187 new Formatter().close(); 188 MyWriter writer = new MyWriter(); 189 Formatter formatter = new Formatter(writer); 190 formatter.format("%s", "x"); 191 assertFalse(writer.closed); 192 formatter.close(); 193 assertTrue(writer.closed); 194 assertFalse(writer.flushed); 195 } 196 197 public void testIoException() throws Exception { 198 Formatter formatter = new Formatter(new Appendable () { 199 200 public Appendable append(CharSequence csq) throws IOException { 201 throw new IOException("test"); 202 } 203 204 public Appendable append(CharSequence csq, int start, int end) throws IOException { 205 throw new IOException("test"); 206 } 207 208 public Appendable append(char c) throws IOException { 209 throw new IOException("test"); 210 } 211 }); 212 formatter.format("%s", "Hello"); 213 assertEquals("test", formatter.ioException().getMessage()); 214 } 215 216 public void testClosed() throws Exception { 217 Formatter formatter = new Formatter(); 218 formatter.close(); 219 try { 220 formatter.locale(); 221 fail(); 222 } catch (FormatterClosedException e) { 223 } 225 try { 226 formatter.out(); 227 fail(); 228 } catch (FormatterClosedException e) { 229 } 231 try { 232 formatter.toString(); 233 fail(); 234 } catch (FormatterClosedException e) { 235 } 237 try { 238 formatter.flush(); 239 fail(); 240 } catch (FormatterClosedException e) { 241 } 243 try { 244 formatter.format(""); 245 fail(); 246 } catch (FormatterClosedException e) { 247 } 249 } 250 251 public void testFormat() throws Exception { 252 assertFormat("AxByC", "A%sB%sC", "x", "y"); 253 assertFormat("AxBxC", "A%sB%<sC", "x"); 254 assertFormat("AyByC", "A%2$sB%<sC", "x", "y"); 255 assertFormat("AyBxCyD", "A%2$sB%sC%sD", "x", "y"); 256 assertFormat("AyBxC", "A%2$sB%1$sC", "x", "y"); 257 assertFormatException(MissingFormatWidthException.class, "%-%", "x"); 258 assertFormatException(IllegalFormatPrecisionException.class, "%1$10.4%", "x"); 259 assertFormatException(IllegalFormatPrecisionException.class, "%1$.3n", "x"); 260 assertFormatException(IllegalFormatWidthException.class, "%1$10n", "x"); 261 assertFormatException(UnknownFormatConversionException.class, "A%qB", "x"); 262 assertFormatException(UnknownFormatConversionException.class, "A%", "x"); 263 assertFormatException(MissingFormatArgumentException.class, "A%<sB", "x"); 264 assertFormatException(MissingFormatArgumentException.class, "A%3$sB", "x"); 265 } 266 } | Popular Tags |