1 /* 2 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* Type-specific source code for unit test 25 * 26 * Regenerate the BasicX classes via genBasic.sh whenever this file changes. 27 * We check in the generated source files so that the test tree can be used 28 * independently of the rest of the source tree. 29 */ 30 31 // -- This file was mechanically generated: Do not edit! -- // 32 33 import java.io.*; 34 import java.math.BigDecimal; 35 import java.math.BigInteger; 36 import java.text.DateFormatSymbols; 37 import java.util.*; 38 39 import static java.util.Calendar.*; 40 41 public class BasicByteObject extends Basic { 42 43 private static void test(String fs, String exp, Object ... args) { 44 Formatter f = new Formatter(new StringBuilder(), Locale.US); 45 f.format(fs, args); 46 ck(fs, exp, f.toString()); 47 48 f = new Formatter(new StringBuilder(), Locale.US); 49 f.format("foo " + fs + " bar", args); 50 ck(fs, "foo " + exp + " bar", f.toString()); 51 } 52 53 private static void test(Locale l, String fs, String exp, Object ... args) 54 { 55 Formatter f = new Formatter(new StringBuilder(), l); 56 f.format(fs, args); 57 ck(fs, exp, f.toString()); 58 59 f = new Formatter(new StringBuilder(), l); 60 f.format("foo " + fs + " bar", args); 61 ck(fs, "foo " + exp + " bar", f.toString()); 62 } 63 64 private static void test(String fs, Object ... args) { 65 Formatter f = new Formatter(new StringBuilder(), Locale.US); 66 f.format(fs, args); 67 ck(fs, "fail", f.toString()); 68 } 69 70 private static void test(String fs) { 71 Formatter f = new Formatter(new StringBuilder(), Locale.US); 72 f.format(fs, "fail"); 73 ck(fs, "fail", f.toString()); 74 } 75 76 private static void testSysOut(String fs, String exp, Object ... args) { 77 FileOutputStream fos = null; 78 FileInputStream fis = null; 79 try { 80 PrintStream saveOut = System.out; 81 fos = new FileOutputStream("testSysOut"); 82 System.setOut(new PrintStream(fos)); 83 System.out.format(Locale.US, fs, args); 84 fos.close(); 85 86 fis = new FileInputStream("testSysOut"); 87 byte [] ba = new byte[exp.length()]; 88 int len = fis.read(ba); 89 String got = new String(ba); 90 if (len != ba.length) 91 fail(fs, exp, got); 92 ck(fs, exp, got); 93 94 System.setOut(saveOut); 95 } catch (FileNotFoundException ex) { 96 fail(fs, ex.getClass()); 97 } catch (IOException ex) { 98 fail(fs, ex.getClass()); 99 } finally { 100 try { 101 if (fos != null) 102 fos.close(); 103 if (fis != null) 104 fis.close(); 105 } catch (IOException ex) { 106 fail(fs, ex.getClass()); 107 } 108 } 109 } 110 111 private static void tryCatch(String fs, Class<?> ex) { 112 boolean caught = false; 113 try { 114 test(fs); 115 } catch (Throwable x) { 116 if (ex.isAssignableFrom(x.getClass())) 117 caught = true; 118 } 119 if (!caught) 120 fail(fs, ex); 121 else 122 pass(); 123 } 124 125 private static void tryCatch(String fs, Class<?> ex, Object ... args) { 126 boolean caught = false; 127 try { 128 test(fs, args); 129 } catch (Throwable x) { 130 if (ex.isAssignableFrom(x.getClass())) 131 caught = true; 132 } 133 if (!caught) 134 fail(fs, ex); 135 else 136 pass(); 137 } 138 139 private static Byte negate(Byte v) { 140 return new Byte((byte) -v.byteValue()); 141 } 142 143 public static void test() { 144 TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800")); 145 146 // Any characters not explicitly defined as conversions, date/time 147 // conversion suffixes, or flags are illegal and are reserved for 148 // future extensions. Use of such a character in a format string will 149 // cause an UnknownFormatConversionException or 150 // UnknownFormatFlagsException to be thrown. 151 tryCatch("%q", UnknownFormatConversionException.class); 152 tryCatch("%t&", UnknownFormatConversionException.class); 153 tryCatch("%&d", UnknownFormatConversionException.class); 154 tryCatch("%^b", UnknownFormatConversionException.class); 155 156 //--------------------------------------------------------------------- 157 // Formatter.java class javadoc examples 158 //--------------------------------------------------------------------- 159 test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E); 160 test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d"); 161 test("Amount gained or lost since last statement: $ %,(.2f", 162 "Amount gained or lost since last statement: $ (6,217.58)", 163 (new BigDecimal("-6217.58"))); 164 Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0); 165 testSysOut("Local time: %tT", "Local time: 16:17:00", c); 166 167 test("Unable to open file '%1$s': %2$s", 168 "Unable to open file 'food': No such file or directory", 169 "food", "No such file or directory"); 170 Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34); 171 duke.set(Calendar.MILLISECOND, 584); 172 test("Duke's Birthday: %1$tB %1$te, %1$tY", 173 "Duke's Birthday: May 23, 1995", 174 duke); 175 test("Duke's Birthday: %1$tB %1$te, %1$tY", 176 "Duke's Birthday: May 23, 1995", 177 duke.getTime()); 178 test("Duke's Birthday: %1$tB %1$te, %1$tY", 179 "Duke's Birthday: May 23, 1995", 180 duke.getTimeInMillis()); 181 182 test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s", 183 "d c b a d c b a", "a", "b", "c", "d"); 184 test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d"); 185 test("%s %s %s %s", "a b c d", "a", "b", "c", "d"); 186 test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d"); 187 188 //--------------------------------------------------------------------- 189 // %b 190 // 191 // General conversion applicable to any argument. 192 //--------------------------------------------------------------------- 193 test("%b", "true", true); 194 test("%b", "false", false); 195 test("%B", "TRUE", true); 196 test("%B", "FALSE", false); 197 test("%b", "true", Boolean.TRUE); 198 test("%b", "false", Boolean.FALSE); 199 test("%B", "TRUE", Boolean.TRUE); 200 test("%B", "FALSE", Boolean.FALSE); 201 test("%14b", " true", true); 202 test("%-14b", "true ", true); 203 test("%5.1b", " f", false); 204 test("%-5.1b", "f ", false); 205 206 test("%b", "true", "foo"); 207 test("%b", "false", (Object)null); 208 209 // Boolean.java hardcodes the Strings for "true" and "false", so no 210 // localization is possible. 211 test(Locale.FRANCE, "%b", "true", true); 212 test(Locale.FRANCE, "%b", "false", false); 213 214 // If you pass in a single array to a varargs method, the compiler 215 // uses it as the array of arguments rather than treating it as a 216 // single array-type argument. 217 test("%b", "false", (Object[])new String[2]); 218 test("%b", "true", new String[2], new String[2]); 219 220 int [] ia = { 1, 2, 3 }; 221 test("%b", "true", ia); 222 223 //--------------------------------------------------------------------- 224 // %b - errors 225 //--------------------------------------------------------------------- 226 tryCatch("%#b", FormatFlagsConversionMismatchException.class); 227 tryCatch("%-b", MissingFormatWidthException.class); 228 // correct or side-effect of implementation? 229 tryCatch("%.b", UnknownFormatConversionException.class); 230 tryCatch("%,b", FormatFlagsConversionMismatchException.class); 231 232 //--------------------------------------------------------------------- 233 // %c 234 // 235 // General conversion applicable to any argument. 236 //--------------------------------------------------------------------- 237 test("%c", "i", 'i'); 238 test("%C", "I", 'i'); 239 test("%4c", " i", 'i'); 240 test("%-4c", "i ", 'i'); 241 test("%4C", " I", 'i'); 242 test("%-4C", "I ", 'i'); 243 test("%c", "i", new Character('i')); 244 test("%c", "H", (byte) 72); 245 test("%c", "i", (short) 105); 246 test("%c", "!", (int) 33); 247 test("%c", "\u007F", Byte.MAX_VALUE); 248 test("%c", new String(Character.toChars(Short.MAX_VALUE)), 249 Short.MAX_VALUE); 250 test("%c", "null", (Object) null); 251 252 //--------------------------------------------------------------------- 253 // %c - errors 254 //--------------------------------------------------------------------- 255 tryCatch("%c", IllegalFormatConversionException.class, 256 Boolean.TRUE); 257 tryCatch("%c", IllegalFormatConversionException.class, 258 (float) 0.1); 259 tryCatch("%c", IllegalFormatConversionException.class, 260 new Object()); 261 tryCatch("%c", IllegalFormatCodePointException.class, 262 Byte.MIN_VALUE); 263 tryCatch("%c", IllegalFormatCodePointException.class, 264 Short.MIN_VALUE); 265 tryCatch("%c", IllegalFormatCodePointException.class, 266 Integer.MIN_VALUE); 267 tryCatch("%c", IllegalFormatCodePointException.class, 268 Integer.MAX_VALUE); 269 270 tryCatch("%#c", FormatFlagsConversionMismatchException.class); 271 tryCatch("%,c", FormatFlagsConversionMismatchException.class); 272 tryCatch("%(c", FormatFlagsConversionMismatchException.class); 273 tryCatch("%$c", UnknownFormatConversionException.class); 274 tryCatch("%.2c", IllegalFormatPrecisionException.class); 275 276 //--------------------------------------------------------------------- 277 // %s 278 // 279 // General conversion applicable to any argument. 280 //--------------------------------------------------------------------- 281 test("%s", "Hello, Duke", "Hello, Duke"); 282 test("%S", "HELLO, DUKE", "Hello, Duke"); 283 test("%20S", " HELLO, DUKE", "Hello, Duke"); 284 test("%20s", " Hello, Duke", "Hello, Duke"); 285 test("%-20s", "Hello, Duke ", "Hello, Duke"); 286 test("%-20.5s", "Hello ", "Hello, Duke"); 287 test("%s", "null", (Object)null); 288 289 StringBuffer sb = new StringBuffer("foo bar"); 290 test("%s", sb.toString(), sb); 291 test("%S", sb.toString().toUpperCase(), sb); 292 293 //--------------------------------------------------------------------- 294 // %s - errors 295 //--------------------------------------------------------------------- 296 tryCatch("%-s", MissingFormatWidthException.class); 297 tryCatch("%--s", DuplicateFormatFlagsException.class); 298 tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0); 299 tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f); 300 tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello"); 301 tryCatch("%#s", FormatFlagsConversionMismatchException.class, null); 302 303 //--------------------------------------------------------------------- 304 // %h 305 // 306 // General conversion applicable to any argument. 307 //--------------------------------------------------------------------- 308 test("%h", Integer.toHexString("Hello, Duke".hashCode()), 309 "Hello, Duke"); 310 test("%10h", " ddf63471", "Hello, Duke"); 311 test("%-10h", "ddf63471 ", "Hello, Duke"); 312 test("%-10H", "DDF63471 ", "Hello, Duke"); 313 test("%10h", " 402e0000", 15.0); 314 test("%10H", " 402E0000", 15.0); 315 316 //--------------------------------------------------------------------- 317 // %h - errors 318 //--------------------------------------------------------------------- 319 tryCatch("%#h", FormatFlagsConversionMismatchException.class); 320 321 //--------------------------------------------------------------------- 322 // flag/conversion errors 323 //--------------------------------------------------------------------- 324 tryCatch("%F", UnknownFormatConversionException.class); 325 326 tryCatch("%#g", FormatFlagsConversionMismatchException.class); 327 328 Byte minByte = new Byte(Byte.MIN_VALUE); 329 330 //--------------------------------------------------------------------- 331 // %d 332 // 333 // Numeric conversion applicable to byte, short, int, long, and 334 // BigInteger. 335 //--------------------------------------------------------------------- 336 test("%d", "null", (Object)null); 337 338 //--------------------------------------------------------------------- 339 // %d - errors 340 //--------------------------------------------------------------------- 341 tryCatch("%#d", FormatFlagsConversionMismatchException.class); 342 tryCatch("%D", UnknownFormatConversionException.class); 343 tryCatch("%0d", MissingFormatWidthException.class); 344 tryCatch("%-d", MissingFormatWidthException.class); 345 tryCatch("%7.3d", IllegalFormatPrecisionException.class); 346 347 //--------------------------------------------------------------------- 348 // %o 349 // 350 // Numeric conversion applicable to byte, short, int, long, and 351 // BigInteger. 352 //--------------------------------------------------------------------- 353 test("%o", "null", (Object)null); 354 355 //--------------------------------------------------------------------- 356 // %o - errors 357 //--------------------------------------------------------------------- 358 tryCatch("%(o", FormatFlagsConversionMismatchException.class, 359 minByte); 360 tryCatch("%+o", FormatFlagsConversionMismatchException.class, 361 minByte); 362 tryCatch("% o", FormatFlagsConversionMismatchException.class, 363 minByte); 364 tryCatch("%0o", MissingFormatWidthException.class); 365 tryCatch("%-o", MissingFormatWidthException.class); 366 tryCatch("%,o", FormatFlagsConversionMismatchException.class); 367 tryCatch("%O", UnknownFormatConversionException.class); 368 369 //--------------------------------------------------------------------- 370 // %x 371 // 372 // Numeric conversion applicable to byte, short, int, long, and 373 // BigInteger. 374 //--------------------------------------------------------------------- 375 test("%x", "null", (Object)null); 376 377 //--------------------------------------------------------------------- 378 // %x - errors 379 //--------------------------------------------------------------------- 380 tryCatch("%,x", FormatFlagsConversionMismatchException.class); 381 tryCatch("%0x", MissingFormatWidthException.class); 382 tryCatch("%-x", MissingFormatWidthException.class); 383 384 //--------------------------------------------------------------------- 385 // %t 386 // 387 // Date/Time conversions applicable to Calendar, Date, and long. 388 //--------------------------------------------------------------------- 389 test("%tA", "null", (Object)null); 390 test("%TA", "NULL", (Object)null); 391 392 //--------------------------------------------------------------------- 393 // %t - errors 394 //--------------------------------------------------------------------- 395 tryCatch("%t", UnknownFormatConversionException.class); 396 tryCatch("%T", UnknownFormatConversionException.class); 397 tryCatch("%tP", UnknownFormatConversionException.class); 398 tryCatch("%TP", UnknownFormatConversionException.class); 399 tryCatch("%.5tB", IllegalFormatPrecisionException.class); 400 tryCatch("%#tB", FormatFlagsConversionMismatchException.class); 401 tryCatch("%-tB", MissingFormatWidthException.class); 402 403 //--------------------------------------------------------------------- 404 // %n 405 //--------------------------------------------------------------------- 406 test("%n", System.getProperty("line.separator"), (Object)null); 407 test("%n", System.getProperty("line.separator"), ""); 408 409 tryCatch("%,n", IllegalFormatFlagsException.class); 410 tryCatch("%.n", UnknownFormatConversionException.class); 411 tryCatch("%5.n", UnknownFormatConversionException.class); 412 tryCatch("%5n", IllegalFormatWidthException.class); 413 tryCatch("%.7n", IllegalFormatPrecisionException.class); 414 tryCatch("%<n", IllegalFormatFlagsException.class); 415 416 //--------------------------------------------------------------------- 417 // %% 418 //--------------------------------------------------------------------- 419 test("%%", "%", (Object)null); 420 test("%%", "%", ""); 421 tryCatch("%%%", UnknownFormatConversionException.class); 422 // perhaps an IllegalFormatArgumentIndexException should be defined? 423 tryCatch("%<%", IllegalFormatFlagsException.class); 424 } 425 }