40 /** 41 * @test 42 * @bug 8013839 43 * @summary tests Logger.setResourceBundle; 44 * @build TestSetResourceBundle resources.ListBundle resources.ListBundle_fr 45 * @run main/othervm TestSetResourceBundle UNSECURE 46 * @run main/othervm TestSetResourceBundle PERMISSION 47 * @run main/othervm TestSetResourceBundle SECURE 48 * @author danielfuchs 49 */ 50 public class TestSetResourceBundle { 51 52 final static String LIST_BUNDLE_NAME = "resources.ListBundle"; 53 final static String PROPERTY_BUNDLE_NAME = "resources.PropertyBundle"; 54 55 /** 56 * A dummy handler class that we can use to check the bundle/bundle name 57 * that was present in the last LogRecord instance published. 58 */ 59 static final class TestHandler extends Handler { 60 ResourceBundle lastBundle = null; 61 String lastBundleName = null; 62 @Override 63 public void publish(LogRecord record) { 64 lastBundle = record.getResourceBundle(); 65 lastBundleName = record.getResourceBundleName(); 66 } 67 68 @Override 69 public void flush() { 70 } 71 72 @Override 73 public void close() throws SecurityException { 74 } 75 } 76 77 /** 78 * We will test setResourceBundle() in 3 configurations. 79 * UNSECURE: No security manager. 80 * SECURE: With the security manager present - and the required 81 * LoggingPermission("control") granted. 169 foobar.setResourceBundle(bundle); 170 throw new RuntimeException("Permission not checked!"); 171 } catch (AccessControlException x) { 172 if (x.getPermission() instanceof LoggingPermission) { 173 if ("control".equals(x.getPermission().getName())) { 174 System.out.println("Got expected exception: " + x); 175 return; 176 } 177 } 178 throw new RuntimeException("Unexpected exception: "+x, x); 179 } 180 181 } 182 183 static String getBaseName(ResourceBundle bundle) { 184 return bundle == null ? null : bundle.getBaseBundleName(); 185 } 186 187 public static void test(String loggerName) throws Exception { 188 189 final ResourceBundle bundle = ResourceBundle.getBundle(LIST_BUNDLE_NAME); 190 Logger foobar = Logger.getLogger(loggerName); 191 192 // Checks that IAE is thrown if the bundle has a null base name. 193 try { 194 foobar.setResourceBundle(new ListBundle()); 195 throw new RuntimeException("Expected exception not raised!"); 196 } catch (IllegalArgumentException x) { 197 System.out.println("Got expected exception: " + x); 198 } 199 200 // Verify that resource bundle was not set. 201 if (foobar.getResourceBundle() != null) { 202 throw new RuntimeException("Unexpected bundle: " 203 + foobar.getResourceBundle()); 204 } 205 if (foobar.getResourceBundleName() != null) { 206 throw new RuntimeException("Unexpected bundle: " 207 + foobar.getResourceBundleName()); 208 } 218 if (!Objects.equals(getBaseName(bundle), foobar.getResourceBundleName())) { 219 throw new RuntimeException("Unexpected bundle name: " 220 + foobar.getResourceBundleName()); 221 } 222 223 // Check that we can replace the bundle with a bundle of the same name. 224 final ResourceBundle bundle_fr = 225 ResourceBundle.getBundle(LIST_BUNDLE_NAME, Locale.FRENCH); 226 foobar.setResourceBundle(bundle_fr); 227 228 if (bundle_fr != foobar.getResourceBundle()) { 229 throw new RuntimeException("Unexpected bundle: " 230 + foobar.getResourceBundle()); 231 } 232 if (!Objects.equals(getBaseName(bundle_fr), foobar.getResourceBundleName())) { 233 throw new RuntimeException("Unexpected bundle name: " 234 + foobar.getResourceBundleName()); 235 } 236 237 // Create a child logger 238 Logger foobaz = Logger.getLogger(loggerName + ".baz"); 239 240 // Check that the child logger does not have a bundle set locally 241 if (foobaz.getResourceBundle() != null) { 242 throw new RuntimeException("Unexpected bundle: " 243 + foobar.getResourceBundle()); 244 } 245 if (foobaz.getResourceBundleName() != null) { 246 throw new RuntimeException("Unexpected bundle: " 247 + foobar.getResourceBundleName()); 248 } 249 250 251 // Add a handler on the child logger. 252 final TestHandler handler = new TestHandler(); 253 foobaz.addHandler(handler); 254 255 // log a message on the child logger 256 foobaz.severe("dummy"); 257 258 // checks that the message has been logged with the bundle 259 // inherited from the parent logger 260 if (!LIST_BUNDLE_NAME.equals(handler.lastBundleName)) { 261 throw new RuntimeException("Unexpected bundle name: " 262 + handler.lastBundleName); 263 } 264 if (!bundle_fr.equals(handler.lastBundle)) { 265 throw new RuntimeException("Unexpected bundle: " 266 + handler.lastBundle); 267 } 268 269 // Check that we can get set a bundle on the child logger 270 // using Logger.getLogger. 271 foobaz = Logger.getLogger(loggerName + ".baz", PROPERTY_BUNDLE_NAME); 272 273 // check that the child logger has the correct bundle. 274 // it should no longer inherit it from its parent. 275 if (!PROPERTY_BUNDLE_NAME.equals(foobaz.getResourceBundleName())) { 276 throw new RuntimeException("Unexpected bundle name: " 277 + foobaz.getResourceBundleName()); 278 } 279 if (!PROPERTY_BUNDLE_NAME.equals(foobaz.getResourceBundle().getBaseBundleName())) { 280 throw new RuntimeException("Unexpected bundle name: " 281 + foobaz.getResourceBundle().getBaseBundleName()); 282 } 283 284 // log a message on the child logger 285 foobaz.severe("dummy"); 286 287 // check that the last published log record has the appropriate 288 // bundle. 289 if (!PROPERTY_BUNDLE_NAME.equals(handler.lastBundleName)) { 290 throw new RuntimeException("Unexpected bundle name: " 291 + handler.lastBundleName); 292 } 293 if (foobaz.getResourceBundle() != handler.lastBundle) { 294 throw new RuntimeException("Unexpected bundle: " 295 + handler.lastBundle); 296 } 297 298 // try to set a bundle that has a different name, and checks that 299 // it fails in IAE. 300 try { 301 foobaz.setResourceBundle(bundle_fr); 302 throw new RuntimeException("Expected exception not raised!"); 303 } catch (IllegalArgumentException x) { 304 System.out.println("Got expected exception: " + x); 305 } 306 307 // Test with a subclass of logger which overrides 308 // getResourceBundle() and getResourceBundleName() 309 Logger customLogger = new Logger(foobar.getName()+".bie", null) { 310 @Override 311 public ResourceBundle getResourceBundle() { 312 return bundle_fr; 313 } 314 315 @Override 316 public String getResourceBundleName() { 317 return PROPERTY_BUNDLE_NAME; 318 } 319 }; 320 321 final TestHandler handler2 = new TestHandler(); 322 customLogger.addHandler(handler2); 323 customLogger.setLevel(Level.FINE); 324 LogManager.getLogManager().addLogger(customLogger); 325 326 Logger l = Logger.getLogger(customLogger.getName()); 327 if (l != customLogger) { 328 throw new RuntimeException("Wrong logger: " + l); 329 } 330 331 // log on the custom logger. 332 customLogger.fine("dummy"); 333 334 // check that the log record had the correct bundle. 335 if (! PROPERTY_BUNDLE_NAME.equals(handler2.lastBundleName)) { 336 throw new RuntimeException("Unexpected bundle name: " 337 + handler2.lastBundleName); 338 } 339 if (! PROPERTY_BUNDLE_NAME.equals(customLogger.getResourceBundleName())) { 340 throw new RuntimeException("Unexpected bundle name: " 341 + customLogger.getResourceBundleName()); 342 } 343 if (bundle_fr != handler2.lastBundle) { 344 throw new RuntimeException("Unexpected bundle: " 345 + handler2.lastBundle); 346 } 347 if (bundle_fr != customLogger.getResourceBundle()) { 348 throw new RuntimeException("Unexpected bundle: " 349 + customLogger.getResourceBundle()); 350 } 351 352 // Do the same thing again with a child of the custom logger. 353 Logger biebar = Logger.getLogger(customLogger.getName() + ".bar"); 354 biebar.fine("dummy"); 355 356 // because getResourceBundleName() is called on parent logger 357 // we will have handler2.lastBundleName = PROPERTY_BUNDLE_NAME 358 if (!PROPERTY_BUNDLE_NAME.equals(handler2.lastBundleName)) { 359 throw new RuntimeException("Unexpected bundle name: " 360 + handler2.lastBundleName); 361 } 362 // because getResourceBundle() is not called on parent logger 363 // we will have getBaseName(handler2.lastBundle) = PROPERTY_BUNDLE_NAME 364 // and not handler2.lastBundle = bundle_fr 365 if (handler2.lastBundle == null) { 366 throw new RuntimeException("Unexpected bundle: " 367 + handler2.lastBundle); 368 } 369 if (!PROPERTY_BUNDLE_NAME.equals(getBaseName(handler2.lastBundle))) { 370 throw new RuntimeException("Unexpected bundle name: " 371 + getBaseName(handler2.lastBundle)); 372 } 373 } 374 375 public static class SimplePolicy extends Policy { 376 377 final Permissions permissions; 378 public SimplePolicy(TestCase test) { 379 permissions = new Permissions(); 380 if (test != TestCase.PERMISSION) { 381 permissions.add(new LoggingPermission("control", null)); 382 } 383 // required for calling Locale.setDefault in the test. 384 permissions.add(new PropertyPermission("user.language", "write")); 385 } 386 387 @Override 388 public boolean implies(ProtectionDomain domain, Permission permission) { 389 return permissions.implies(permission); 390 } 391 } 392 | 40 /** 41 * @test 42 * @bug 8013839 43 * @summary tests Logger.setResourceBundle; 44 * @build TestSetResourceBundle resources.ListBundle resources.ListBundle_fr 45 * @run main/othervm TestSetResourceBundle UNSECURE 46 * @run main/othervm TestSetResourceBundle PERMISSION 47 * @run main/othervm TestSetResourceBundle SECURE 48 * @author danielfuchs 49 */ 50 public class TestSetResourceBundle { 51 52 final static String LIST_BUNDLE_NAME = "resources.ListBundle"; 53 final static String PROPERTY_BUNDLE_NAME = "resources.PropertyBundle"; 54 55 /** 56 * A dummy handler class that we can use to check the bundle/bundle name 57 * that was present in the last LogRecord instance published. 58 */ 59 static final class TestHandler extends Handler { 60 volatile ResourceBundle lastBundle = null; 61 volatile String lastBundleName = null; 62 @Override 63 public void publish(LogRecord record) { 64 lastBundle = record.getResourceBundle(); 65 lastBundleName = record.getResourceBundleName(); 66 } 67 68 @Override 69 public void flush() { 70 } 71 72 @Override 73 public void close() throws SecurityException { 74 } 75 } 76 77 /** 78 * We will test setResourceBundle() in 3 configurations. 79 * UNSECURE: No security manager. 80 * SECURE: With the security manager present - and the required 81 * LoggingPermission("control") granted. 169 foobar.setResourceBundle(bundle); 170 throw new RuntimeException("Permission not checked!"); 171 } catch (AccessControlException x) { 172 if (x.getPermission() instanceof LoggingPermission) { 173 if ("control".equals(x.getPermission().getName())) { 174 System.out.println("Got expected exception: " + x); 175 return; 176 } 177 } 178 throw new RuntimeException("Unexpected exception: "+x, x); 179 } 180 181 } 182 183 static String getBaseName(ResourceBundle bundle) { 184 return bundle == null ? null : bundle.getBaseBundleName(); 185 } 186 187 public static void test(String loggerName) throws Exception { 188 189 System.out.println("Starting test for " + loggerName); 190 191 final ResourceBundle bundle = ResourceBundle.getBundle(LIST_BUNDLE_NAME); 192 Logger foobar = Logger.getLogger(loggerName); 193 194 // Checks that IAE is thrown if the bundle has a null base name. 195 try { 196 foobar.setResourceBundle(new ListBundle()); 197 throw new RuntimeException("Expected exception not raised!"); 198 } catch (IllegalArgumentException x) { 199 System.out.println("Got expected exception: " + x); 200 } 201 202 // Verify that resource bundle was not set. 203 if (foobar.getResourceBundle() != null) { 204 throw new RuntimeException("Unexpected bundle: " 205 + foobar.getResourceBundle()); 206 } 207 if (foobar.getResourceBundleName() != null) { 208 throw new RuntimeException("Unexpected bundle: " 209 + foobar.getResourceBundleName()); 210 } 220 if (!Objects.equals(getBaseName(bundle), foobar.getResourceBundleName())) { 221 throw new RuntimeException("Unexpected bundle name: " 222 + foobar.getResourceBundleName()); 223 } 224 225 // Check that we can replace the bundle with a bundle of the same name. 226 final ResourceBundle bundle_fr = 227 ResourceBundle.getBundle(LIST_BUNDLE_NAME, Locale.FRENCH); 228 foobar.setResourceBundle(bundle_fr); 229 230 if (bundle_fr != foobar.getResourceBundle()) { 231 throw new RuntimeException("Unexpected bundle: " 232 + foobar.getResourceBundle()); 233 } 234 if (!Objects.equals(getBaseName(bundle_fr), foobar.getResourceBundleName())) { 235 throw new RuntimeException("Unexpected bundle name: " 236 + foobar.getResourceBundleName()); 237 } 238 239 // Create a child logger 240 final Logger foobaz = Logger.getLogger(loggerName + ".baz"); 241 242 if (foobar != foobaz.getParent()) { 243 throw new RuntimeException("Unexpected parent: " + 244 foobaz.getParent() + " != " + foobar); 245 } 246 247 // Check that the child logger does not have a bundle set locally 248 if (foobaz.getResourceBundle() != null) { 249 throw new RuntimeException("Unexpected bundle: " 250 + foobaz.getResourceBundle()); 251 } 252 if (foobaz.getResourceBundleName() != null) { 253 throw new RuntimeException("Unexpected bundle: " 254 + foobaz.getResourceBundleName()); 255 } 256 257 258 // Add a handler on the child logger. 259 final TestHandler handler = new TestHandler(); 260 foobaz.addHandler(handler); 261 262 // log a message on the child logger 263 foobaz.severe("dummy"); 264 265 // checks that the message has been logged with the bundle 266 // inherited from the parent logger 267 if (!LIST_BUNDLE_NAME.equals(handler.lastBundleName)) { 268 debugLogger(foobaz, foobar, handler); 269 throw new RuntimeException("Unexpected bundle name: " 270 + handler.lastBundleName); 271 } 272 if (!bundle_fr.equals(handler.lastBundle)) { 273 debugLogger(foobaz, foobar, handler); 274 throw new RuntimeException("Unexpected bundle: " 275 + handler.lastBundle); 276 } 277 278 // Check that we can get set a bundle on the child logger 279 // using Logger.getLogger. 280 final Logger foobaz2 = Logger.getLogger(loggerName + ".baz", PROPERTY_BUNDLE_NAME); 281 if (foobaz2 != foobaz) { 282 throw new RuntimeException("Unexpected logger: " + foobaz2 + " != " + foobaz); 283 } 284 if (foobar != foobaz.getParent()) { 285 throw new RuntimeException("Unexpected parent: " + 286 foobaz.getParent() + " != " + foobar); 287 } 288 289 // check that the child logger has the correct bundle. 290 // it should no longer inherit it from its parent. 291 if (!PROPERTY_BUNDLE_NAME.equals(foobaz2.getResourceBundleName())) { 292 throw new RuntimeException("Unexpected bundle name: " 293 + foobaz2.getResourceBundleName()); 294 } 295 296 if (!PROPERTY_BUNDLE_NAME.equals(foobaz2.getResourceBundle().getBaseBundleName())) { 297 throw new RuntimeException("Unexpected bundle name: " 298 + foobaz2.getResourceBundle().getBaseBundleName()); 299 } 300 301 boolean found = false; 302 for (Handler h : foobaz2.getHandlers()) { 303 if (h == handler) { 304 found = true; 305 break; 306 } 307 } 308 309 if (!found) { 310 throw new RuntimeException("Expected handler not found in: " + 311 foobaz2.getName() + "(" + foobaz2.getClass().getName()+")" ); 312 } 313 314 // log a message on the child logger 315 foobaz2.severe("dummy"); 316 317 318 // check that the last published log record has the appropriate 319 // bundle. 320 if (!PROPERTY_BUNDLE_NAME.equals(handler.lastBundleName)) { 321 debugLogger(foobaz2, foobar, handler); 322 throw new RuntimeException("Unexpected bundle name: " 323 + handler.lastBundleName); 324 } 325 if (foobaz2.getResourceBundle() != handler.lastBundle) { 326 debugLogger(foobaz2, foobar, handler); 327 throw new RuntimeException("Unexpected bundle: " 328 + handler.lastBundle); 329 } 330 331 // try to set a bundle that has a different name, and checks that 332 // it fails in IAE. 333 try { 334 foobaz2.setResourceBundle(bundle_fr); 335 throw new RuntimeException("Expected exception not raised!"); 336 } catch (IllegalArgumentException x) { 337 System.out.println("Got expected exception: " + x); 338 } 339 340 // Test with a subclass of logger which overrides 341 // getResourceBundle() and getResourceBundleName() 342 Logger customLogger = new Logger(foobar.getName()+".bie", null) { 343 @Override 344 public ResourceBundle getResourceBundle() { 345 return bundle_fr; 346 } 347 348 @Override 349 public String getResourceBundleName() { 350 return PROPERTY_BUNDLE_NAME; 351 } 352 }; 353 354 final TestHandler handler2 = new TestHandler(); 355 customLogger.addHandler(handler2); 356 customLogger.setLevel(Level.FINE); 357 LogManager.getLogManager().addLogger(customLogger); 358 359 Logger l = Logger.getLogger(customLogger.getName()); 360 if (l != customLogger) { 361 throw new RuntimeException("Wrong logger: " + l); 362 } 363 364 // log on the custom logger. 365 customLogger.fine("dummy"); 366 367 // check that the log record had the correct bundle. 368 if (! PROPERTY_BUNDLE_NAME.equals(handler2.lastBundleName)) { 369 debugLogger(customLogger, foobar, handler2); 370 throw new RuntimeException("Unexpected bundle name: " 371 + handler2.lastBundleName); 372 } 373 if (! PROPERTY_BUNDLE_NAME.equals(customLogger.getResourceBundleName())) { 374 debugLogger(customLogger, foobar, handler2); 375 throw new RuntimeException("Unexpected bundle name: " 376 + customLogger.getResourceBundleName()); 377 } 378 if (bundle_fr != handler2.lastBundle) { 379 throw new RuntimeException("Unexpected bundle: " 380 + handler2.lastBundle); 381 } 382 if (bundle_fr != customLogger.getResourceBundle()) { 383 throw new RuntimeException("Unexpected bundle: " 384 + customLogger.getResourceBundle()); 385 } 386 387 // Do the same thing again with a child of the custom logger. 388 Logger biebar = Logger.getLogger(customLogger.getName() + ".bar"); 389 biebar.fine("dummy"); 390 391 // because getResourceBundleName() is called on parent logger 392 // we will have handler2.lastBundleName = PROPERTY_BUNDLE_NAME 393 if (!PROPERTY_BUNDLE_NAME.equals(handler2.lastBundleName)) { 394 debugLogger(biebar, customLogger, handler2); 395 throw new RuntimeException("Unexpected bundle name: " 396 + handler2.lastBundleName); 397 } 398 // because getResourceBundle() is not called on parent logger 399 // we will have getBaseName(handler2.lastBundle) = PROPERTY_BUNDLE_NAME 400 // and not handler2.lastBundle = bundle_fr 401 if (handler2.lastBundle == null) { 402 debugLogger(biebar, customLogger, handler2); 403 throw new RuntimeException("Unexpected bundle: " 404 + handler2.lastBundle); 405 } 406 if (!PROPERTY_BUNDLE_NAME.equals(getBaseName(handler2.lastBundle))) { 407 debugLogger(biebar, customLogger, handler2); 408 throw new RuntimeException("Unexpected bundle name: " 409 + getBaseName(handler2.lastBundle)); 410 } 411 412 // Just make sure that these loggers won't be eagerly GCed... 413 if (foobar == null || !loggerName.equals(foobar.getName())) { 414 throw new RuntimeException("foobar is null " 415 + "- or doesn't have the expected name: " + foobar); 416 } 417 if (foobaz == null || !foobaz.getName().startsWith(loggerName)) { 418 throw new RuntimeException("foobaz is null " 419 + "- or doesn't have the expected name: " + foobaz); 420 } 421 if (foobaz2 == null || !foobaz2.getName().startsWith(loggerName)) { 422 throw new RuntimeException("foobaz2 is null " 423 + "- or doesn't have the expected name: " + foobaz2); 424 } 425 if (!customLogger.getName().startsWith(loggerName)) { 426 throw new RuntimeException("customLogger " 427 + "doesn't have the expected name: " + customLogger); 428 } 429 if (!biebar.getName().startsWith(loggerName)) { 430 throw new RuntimeException("biebar " 431 + "doesn't have the expected name: " + biebar.getName()); 432 } 433 System.out.println("Test passed for " + loggerName); 434 } 435 436 static void debugLogger(Logger logger, Logger expectedParent, TestHandler handler) { 437 final String logName = logger.getName(); 438 final String prefix = " " + logName; 439 System.err.println("Logger " + logName 440 + " logged with bundle name " + handler.lastBundleName 441 + " (" + handler.lastBundle + ")"); 442 System.err.println(prefix + ".getResourceBundleName() is " 443 + logger.getResourceBundleName()); 444 System.err.println(prefix + ".getResourceBundle() is " 445 + logger.getResourceBundle()); 446 final Logger parent = logger.getParent(); 447 final String pname = parent == null ? null : parent.getName(); 448 final String pclass = parent == null ? "" 449 : ("(" + parent.getClass().getName() + ")"); 450 final String presn = parent == null ? null 451 : parent.getResourceBundleName(); 452 final ResourceBundle pres = parent == null ? null 453 : parent.getResourceBundle(); 454 System.err.println(prefix + ".getParent() is " 455 + pname + (pname == null ? "" 456 : (" " + pclass + ": " + parent))); 457 System.err.println(" expected parent is :" + expectedParent); 458 System.err.println(prefix + ".parent.getResourceBundleName() is " 459 + presn); 460 System.err.println(prefix + ".parent.getResourceBundle() is " 461 + pres); 462 System.err.println(" expected parent getResourceBundleName() is " 463 + expectedParent.getResourceBundleName()); 464 System.err.println(" expected parent.getResourceBundle() is " 465 + expectedParent.getResourceBundle()); 466 } 467 468 public static class SimplePolicy extends Policy { 469 470 final Permissions permissions; 471 public SimplePolicy(TestCase test) { 472 permissions = new Permissions(); 473 if (test != TestCase.PERMISSION) { 474 permissions.add(new LoggingPermission("control", null)); 475 } 476 // required for calling Locale.setDefault in the test. 477 permissions.add(new PropertyPermission("user.language", "write")); 478 } 479 480 @Override 481 public boolean implies(ProtectionDomain domain, Permission permission) { 482 return permissions.implies(permission); 483 } 484 } 485 |