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 BasicBoolean 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 public static void test() { 140 TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800")); 141 142 // Any characters not explicitly defined as conversions, date/time 143 // conversion suffixes, or flags are illegal and are reserved for 144 // future extensions. Use of such a character in a format string will 145 // cause an UnknownFormatConversionException or 146 // UnknownFormatFlagsException to be thrown. 147 tryCatch("%q", UnknownFormatConversionException.class); 148 tryCatch("%t&", UnknownFormatConversionException.class); 149 tryCatch("%&d", UnknownFormatConversionException.class); 150 tryCatch("%^b", UnknownFormatConversionException.class); 151 152 //--------------------------------------------------------------------- 153 // Formatter.java class javadoc examples 154 //--------------------------------------------------------------------- 155 test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E); 156 test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d"); 157 test("Amount gained or lost since last statement: $ %,(.2f", 158 "Amount gained or lost since last statement: $ (6,217.58)", 159 (new BigDecimal("-6217.58"))); 160 Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0); 161 testSysOut("Local time: %tT", "Local time: 16:17:00", c); 162 163 test("Unable to open file '%1$s': %2$s", 164 "Unable to open file 'food': No such file or directory", 165 "food", "No such file or directory"); 166 Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34); 167 duke.set(Calendar.MILLISECOND, 584); 168 test("Duke's Birthday: %1$tB %1$te, %1$tY", 169 "Duke's Birthday: May 23, 1995", 170 duke); 171 test("Duke's Birthday: %1$tB %1$te, %1$tY", 172 "Duke's Birthday: May 23, 1995", 173 duke.getTime()); 174 test("Duke's Birthday: %1$tB %1$te, %1$tY", 175 "Duke's Birthday: May 23, 1995", 176 duke.getTimeInMillis()); 177 178 test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s", 179 "d c b a d c b a", "a", "b", "c", "d"); 180 test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d"); 181 test("%s %s %s %s", "a b c d", "a", "b", "c", "d"); 182 test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d"); 183 184 //--------------------------------------------------------------------- 185 // %b 186 // 187 // General conversion applicable to any argument. 188 //--------------------------------------------------------------------- 189 test("%b", "true", true); 190 test("%b", "false", false); 191 test("%B", "TRUE", true); 192 test("%B", "FALSE", false); 193 test("%b", "true", Boolean.TRUE); 194 test("%b", "false", Boolean.FALSE); 195 test("%B", "TRUE", Boolean.TRUE); 196 test("%B", "FALSE", Boolean.FALSE); 197 test("%14b", " true", true); 198 test("%-14b", "true ", true); 199 test("%5.1b", " f", false); 200 test("%-5.1b", "f ", false); 201 202 test("%b", "true", "foo"); 203 test("%b", "false", (Object)null); 204 205 // Boolean.java hardcodes the Strings for "true" and "false", so no 206 // localization is possible. 207 test(Locale.FRANCE, "%b", "true", true); 208 test(Locale.FRANCE, "%b", "false", false); 209 210 // If you pass in a single array to a varargs method, the compiler 211 // uses it as the array of arguments rather than treating it as a 212 // single array-type argument. 213 test("%b", "false", (Object[])new String[2]); 214 test("%b", "true", new String[2], new String[2]); 215 216 int [] ia = { 1, 2, 3 }; 217 test("%b", "true", ia); 218 219 //--------------------------------------------------------------------- 220 // %b - errors 221 //--------------------------------------------------------------------- 222 tryCatch("%#b", FormatFlagsConversionMismatchException.class); 223 tryCatch("%-b", MissingFormatWidthException.class); 224 // correct or side-effect of implementation? 225 tryCatch("%.b", UnknownFormatConversionException.class); 226 tryCatch("%,b", FormatFlagsConversionMismatchException.class); 227 228 //--------------------------------------------------------------------- 229 // %c 230 // 231 // General conversion applicable to any argument. 232 //--------------------------------------------------------------------- 233 test("%c", "i", 'i'); 234 test("%C", "I", 'i'); 235 test("%4c", " i", 'i'); 236 test("%-4c", "i ", 'i'); 237 test("%4C", " I", 'i'); 238 test("%-4C", "I ", 'i'); 239 test("%c", "i", new Character('i')); 240 test("%c", "H", (byte) 72); 241 test("%c", "i", (short) 105); 242 test("%c", "!", (int) 33); 243 test("%c", "\u007F", Byte.MAX_VALUE); 244 test("%c", new String(Character.toChars(Short.MAX_VALUE)), 245 Short.MAX_VALUE); 246 test("%c", "null", (Object) null); 247 248 //--------------------------------------------------------------------- 249 // %c - errors 250 //--------------------------------------------------------------------- 251 tryCatch("%c", IllegalFormatConversionException.class, 252 Boolean.TRUE); 253 tryCatch("%c", IllegalFormatConversionException.class, 254 (float) 0.1); 255 tryCatch("%c", IllegalFormatConversionException.class, 256 new Object()); 257 tryCatch("%c", IllegalFormatCodePointException.class, 258 Byte.MIN_VALUE); 259 tryCatch("%c", IllegalFormatCodePointException.class, 260 Short.MIN_VALUE); 261 tryCatch("%c", IllegalFormatCodePointException.class, 262 Integer.MIN_VALUE); 263 tryCatch("%c", IllegalFormatCodePointException.class, 264 Integer.MAX_VALUE); 265 266 tryCatch("%#c", FormatFlagsConversionMismatchException.class); 267 tryCatch("%,c", FormatFlagsConversionMismatchException.class); 268 tryCatch("%(c", FormatFlagsConversionMismatchException.class); 269 tryCatch("%$c", UnknownFormatConversionException.class); 270 tryCatch("%.2c", IllegalFormatPrecisionException.class); 271 272 //--------------------------------------------------------------------- 273 // %s 274 // 275 // General conversion applicable to any argument. 276 //--------------------------------------------------------------------- 277 test("%s", "Hello, Duke", "Hello, Duke"); 278 test("%S", "HELLO, DUKE", "Hello, Duke"); 279 test("%20S", " HELLO, DUKE", "Hello, Duke"); 280 test("%20s", " Hello, Duke", "Hello, Duke"); 281 test("%-20s", "Hello, Duke ", "Hello, Duke"); 282 test("%-20.5s", "Hello ", "Hello, Duke"); 283 test("%s", "null", (Object)null); 284 285 StringBuffer sb = new StringBuffer("foo bar"); 286 test("%s", sb.toString(), sb); 287 test("%S", sb.toString().toUpperCase(), sb); 288 289 //--------------------------------------------------------------------- 290 // %s - errors 291 //--------------------------------------------------------------------- 292 tryCatch("%-s", MissingFormatWidthException.class); 293 tryCatch("%--s", DuplicateFormatFlagsException.class); 294 tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0); 295 tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f); 296 tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello"); 297 tryCatch("%#s", FormatFlagsConversionMismatchException.class, null); 298 299 //--------------------------------------------------------------------- 300 // %h 301 // 302 // General conversion applicable to any argument. 303 //--------------------------------------------------------------------- 304 test("%h", Integer.toHexString("Hello, Duke".hashCode()), 305 "Hello, Duke"); 306 test("%10h", " ddf63471", "Hello, Duke"); 307 test("%-10h", "ddf63471 ", "Hello, Duke"); 308 test("%-10H", "DDF63471 ", "Hello, Duke"); 309 test("%10h", " 402e0000", 15.0); 310 test("%10H", " 402E0000", 15.0); 311 312 //--------------------------------------------------------------------- 313 // %h - errors 314 //--------------------------------------------------------------------- 315 tryCatch("%#h", FormatFlagsConversionMismatchException.class); 316 317 //--------------------------------------------------------------------- 318 // flag/conversion errors 319 //--------------------------------------------------------------------- 320 tryCatch("%F", UnknownFormatConversionException.class); 321 322 tryCatch("%#g", FormatFlagsConversionMismatchException.class); 323 324 //--------------------------------------------------------------------- 325 // %t 326 // 327 // Date/Time conversions applicable to Calendar, Date, and long. 328 //--------------------------------------------------------------------- 329 test("%tA", "null", (Object)null); 330 test("%TA", "NULL", (Object)null); 331 332 //--------------------------------------------------------------------- 333 // %t - errors 334 //--------------------------------------------------------------------- 335 tryCatch("%t", UnknownFormatConversionException.class); 336 tryCatch("%T", UnknownFormatConversionException.class); 337 tryCatch("%tP", UnknownFormatConversionException.class); 338 tryCatch("%TP", UnknownFormatConversionException.class); 339 tryCatch("%.5tB", IllegalFormatPrecisionException.class); 340 tryCatch("%#tB", FormatFlagsConversionMismatchException.class); 341 tryCatch("%-tB", MissingFormatWidthException.class); 342 343 //--------------------------------------------------------------------- 344 // %n 345 //--------------------------------------------------------------------- 346 test("%n", System.getProperty("line.separator"), (Object)null); 347 test("%n", System.getProperty("line.separator"), ""); 348 349 tryCatch("%,n", IllegalFormatFlagsException.class); 350 tryCatch("%.n", UnknownFormatConversionException.class); 351 tryCatch("%5.n", UnknownFormatConversionException.class); 352 tryCatch("%5n", IllegalFormatWidthException.class); 353 tryCatch("%.7n", IllegalFormatPrecisionException.class); 354 tryCatch("%<n", IllegalFormatFlagsException.class); 355 356 //--------------------------------------------------------------------- 357 // %% 358 //--------------------------------------------------------------------- 359 test("%%", "%", (Object)null); 360 test("%%", "%", ""); 361 tryCatch("%%%", UnknownFormatConversionException.class); 362 // perhaps an IllegalFormatArgumentIndexException should be defined? 363 tryCatch("%<%", IllegalFormatFlagsException.class); 364 } 365 }