1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.logging.tests.java.util.logging; 19 20import dalvik.annotation.*; 21 22import java.io.ByteArrayOutputStream; 23import java.io.IOException; 24import java.io.OutputStream; 25import java.io.PrintStream; 26import java.io.UnsupportedEncodingException; 27import java.nio.CharBuffer; 28import java.nio.charset.Charset; 29import java.nio.charset.CharsetEncoder; 30import java.nio.charset.CodingErrorAction; 31import java.security.Permission; 32import java.util.Arrays; 33import java.util.Properties; 34import java.util.logging.Filter; 35import java.util.logging.Formatter; 36import java.util.logging.Handler; 37import java.util.logging.Level; 38import java.util.logging.LogManager; 39import java.util.logging.LogRecord; 40import java.util.logging.LoggingPermission; 41import java.util.logging.SimpleFormatter; 42import java.util.logging.SocketHandler; 43import java.util.logging.StreamHandler; 44 45import junit.framework.TestCase; 46 47import org.apache.harmony.logging.tests.java.util.logging.HandlerTest.NullOutputStream; 48import org.apache.harmony.logging.tests.java.util.logging.util.EnvironmentHelper; 49import tests.util.CallVerificationStack; 50 51/** 52 * Test the class StreamHandler. 53 */ 54@TestTargetClass(StreamHandler.class) 55public class StreamHandlerTest extends TestCase { 56 57 private final static String INVALID_LEVEL = "impossible_level"; 58 59 private final PrintStream err = System.err; 60 61 private OutputStream errSubstituteStream = null; 62 63 private static String className = StreamHandlerTest.class.getName(); 64 65 private static CharsetEncoder encoder; 66 67 static { 68 encoder = Charset.forName("iso-8859-1").newEncoder(); 69 encoder.onMalformedInput(CodingErrorAction.REPLACE); 70 encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); 71 } 72 73 /* 74 * @see TestCase#setUp() 75 */ 76 protected void setUp() throws Exception { 77 super.setUp(); 78 errSubstituteStream = new NullOutputStream(); 79 System.setErr(new PrintStream(errSubstituteStream)); 80 } 81 82 /* 83 * @see TestCase#tearDown() 84 */ 85 protected void tearDown() throws Exception { 86 LogManager.getLogManager().reset(); 87 CallVerificationStack.getInstance().clear(); 88 System.setErr(err); 89 super.tearDown(); 90 } 91 92 /* 93 * Test the constructor with no parameter, and no relevant log manager 94 * properties are set. 95 */ 96 @TestTargetNew( 97 level = TestLevel.PARTIAL_COMPLETE, 98 notes = "Verifies the constructor with no parameter, and no relevant log manager properties are set.", 99 method = "StreamHandler", 100 args = {} 101 ) 102 public void testConstructor_NoParameter_NoProperties() { 103 assertNull(LogManager.getLogManager().getProperty( 104 "java.util.logging.StreamHandler.level")); 105 assertNull(LogManager.getLogManager().getProperty( 106 "java.util.logging.StreamHandler.filter")); 107 assertNull(LogManager.getLogManager().getProperty( 108 "java.util.logging.StreamHandler.formatter")); 109 assertNull(LogManager.getLogManager().getProperty( 110 "java.util.logging.StreamHandler.encoding")); 111 112 StreamHandler h = new StreamHandler(); 113 assertSame(Level.INFO, h.getLevel()); 114 assertTrue(h.getFormatter() instanceof SimpleFormatter); 115 assertNull(h.getFilter()); 116 assertNull(h.getEncoding()); 117 } 118 119 /* 120 * Test the constructor with insufficient privilege. 121 */ 122 @TestTargetNew( 123 level = TestLevel.PARTIAL_COMPLETE, 124 notes = "Verifies the constructor with insufficient privilege.", 125 method = "StreamHandler", 126 args = {java.io.OutputStream.class, java.util.logging.Formatter.class} 127 ) 128 public void testConstructor_NoParameter_InsufficientPrivilege() { 129 assertNull(LogManager.getLogManager().getProperty( 130 "java.util.logging.StreamHandler.level")); 131 assertNull(LogManager.getLogManager().getProperty( 132 "java.util.logging.StreamHandler.filter")); 133 assertNull(LogManager.getLogManager().getProperty( 134 "java.util.logging.StreamHandler.formatter")); 135 assertNull(LogManager.getLogManager().getProperty( 136 "java.util.logging.StreamHandler.encoding")); 137 138 SecurityManager oldMan = System.getSecurityManager(); 139 System.setSecurityManager(new MockSecurityManager()); 140 // set a normal value 141 try { 142 StreamHandler h = new StreamHandler(); 143 assertSame(Level.INFO, h.getLevel()); 144 assertTrue(h.getFormatter() instanceof SimpleFormatter); 145 assertNull(h.getFilter()); 146 assertNull(h.getEncoding()); 147 } finally { 148 System.setSecurityManager(oldMan); 149 } 150 } 151 152 /* 153 * Test the constructor with no parameter, and valid relevant log manager 154 * properties are set. 155 */ 156 @TestTargetNew( 157 level = TestLevel.PARTIAL_COMPLETE, 158 notes = "Verifies the constructor with no parameter, and valid relevant log manager properties are set.", 159 method = "StreamHandler", 160 args = {} 161 ) 162 public void testConstructor_NoParameter_ValidProperties() throws Exception { 163 Properties p = new Properties(); 164 p.put("java.util.logging.StreamHandler.level", "FINE"); 165 p.put("java.util.logging.StreamHandler.filter", className 166 + "$MockFilter"); 167 p.put("java.util.logging.StreamHandler.formatter", className 168 + "$MockFormatter"); 169 p.put("java.util.logging.StreamHandler.encoding", "iso-8859-1"); 170 LogManager.getLogManager().readConfiguration( 171 EnvironmentHelper.PropertiesToInputStream(p)); 172 173 assertEquals("FINE", LogManager.getLogManager().getProperty( 174 "java.util.logging.StreamHandler.level")); 175 assertEquals("iso-8859-1", LogManager.getLogManager().getProperty( 176 "java.util.logging.StreamHandler.encoding")); 177 StreamHandler h = new StreamHandler(); 178 assertSame(h.getLevel(), Level.parse("FINE")); 179 assertTrue(h.getFormatter() instanceof MockFormatter); 180 assertTrue(h.getFilter() instanceof MockFilter); 181 assertEquals("iso-8859-1", h.getEncoding()); 182 } 183 184 /* 185 * Test the constructor with no parameter, and invalid relevant log manager 186 * properties are set. 187 */ 188 @TestTargetNew( 189 level = TestLevel.PARTIAL_COMPLETE, 190 notes = "Verifies the constructor with no parameter, and invalid relevant log manager properties are set.", 191 method = "StreamHandler", 192 args = {} 193 ) 194 public void testConstructor_NoParameter_InvalidProperties() 195 throws Exception { 196 Properties p = new Properties(); 197 p.put("java.util.logging.StreamHandler.level", INVALID_LEVEL); 198 p.put("java.util.logging.StreamHandler.filter", className + ""); 199 p.put("java.util.logging.StreamHandler.formatter", className + ""); 200 p.put("java.util.logging.StreamHandler.encoding", "XXXX"); 201 LogManager.getLogManager().readConfiguration( 202 EnvironmentHelper.PropertiesToInputStream(p)); 203 204 assertEquals(INVALID_LEVEL, LogManager.getLogManager().getProperty( 205 "java.util.logging.StreamHandler.level")); 206 assertEquals("XXXX", LogManager.getLogManager().getProperty( 207 "java.util.logging.StreamHandler.encoding")); 208 StreamHandler h = new StreamHandler(); 209 assertSame(Level.INFO, h.getLevel()); 210 assertTrue(h.getFormatter() instanceof SimpleFormatter); 211 assertNull(h.getFilter()); 212 assertNull(h.getEncoding()); 213 h.publish(new LogRecord(Level.SEVERE, "test")); 214 assertTrue(CallVerificationStack.getInstance().empty()); 215 assertNull(h.getEncoding()); 216 } 217 218 /* 219 * Test the constructor with normal parameter values, and no relevant log 220 * manager properties are set. 221 */ 222 @TestTargetNew( 223 level = TestLevel.PARTIAL_COMPLETE, 224 notes = "Verifies the constructor with normal parameter values, and no relevant log manager properties are set.", 225 method = "StreamHandler", 226 args = {java.io.OutputStream.class, java.util.logging.Formatter.class} 227 ) 228 public void testConstructor_HasParameters_NoProperties() { 229 assertNull(LogManager.getLogManager().getProperty( 230 "java.util.logging.StreamHandler.level")); 231 assertNull(LogManager.getLogManager().getProperty( 232 "java.util.logging.StreamHandler.filter")); 233 assertNull(LogManager.getLogManager().getProperty( 234 "java.util.logging.StreamHandler.formatter")); 235 assertNull(LogManager.getLogManager().getProperty( 236 "java.util.logging.StreamHandler.encoding")); 237 238 StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), 239 new MockFormatter2()); 240 assertSame(Level.INFO, h.getLevel()); 241 assertTrue(h.getFormatter() instanceof MockFormatter2); 242 assertNull(h.getFilter()); 243 assertNull(h.getEncoding()); 244 } 245 246 /* 247 * Test the constructor with insufficient privilege. 248 */ 249 @TestTargetNew( 250 level = TestLevel.PARTIAL_COMPLETE, 251 notes = "Verifies the constructor with insufficient privilege.", 252 method = "StreamHandler", 253 args = {java.io.OutputStream.class, java.util.logging.Formatter.class} 254 ) 255 public void testConstructor_HasParameter_InsufficientPrivilege() { 256 assertNull(LogManager.getLogManager().getProperty( 257 "java.util.logging.StreamHandler.level")); 258 assertNull(LogManager.getLogManager().getProperty( 259 "java.util.logging.StreamHandler.filter")); 260 assertNull(LogManager.getLogManager().getProperty( 261 "java.util.logging.StreamHandler.formatter")); 262 assertNull(LogManager.getLogManager().getProperty( 263 "java.util.logging.StreamHandler.encoding")); 264 265 SecurityManager oldMan = System.getSecurityManager(); 266 System.setSecurityManager(new MockSecurityManager()); 267 // set a normal value 268 try { 269 StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), 270 new MockFormatter2()); 271 assertSame(Level.INFO, h.getLevel()); 272 assertTrue(h.getFormatter() instanceof MockFormatter2); 273 assertNull(h.getFilter()); 274 assertNull(h.getEncoding()); 275 } finally { 276 System.setSecurityManager(oldMan); 277 } 278 } 279 280 /* 281 * Test the constructor with normal parameter values, and valid relevant log 282 * manager properties are set. 283 */ 284 @TestTargetNew( 285 level = TestLevel.PARTIAL_COMPLETE, 286 notes = "Verifies the constructor with normal parameter values, and valid relevant log manager properties are set.", 287 method = "StreamHandler", 288 args = {java.io.OutputStream.class, java.util.logging.Formatter.class} 289 ) 290 public void testConstructor_HasParameters_ValidProperties() 291 throws Exception { 292 Properties p = new Properties(); 293 p.put("java.util.logging.StreamHandler.level", "FINE"); 294 p.put("java.util.logging.StreamHandler.filter", className 295 + "$MockFilter"); 296 p.put("java.util.logging.StreamHandler.formatter", className 297 + "$MockFormatter"); 298 p.put("java.util.logging.StreamHandler.encoding", "iso-8859-1"); 299 LogManager.getLogManager().readConfiguration( 300 EnvironmentHelper.PropertiesToInputStream(p)); 301 302 assertEquals("FINE", LogManager.getLogManager().getProperty( 303 "java.util.logging.StreamHandler.level")); 304 assertEquals("iso-8859-1", LogManager.getLogManager().getProperty( 305 "java.util.logging.StreamHandler.encoding")); 306 StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), 307 new MockFormatter2()); 308 assertSame(h.getLevel(), Level.parse("FINE")); 309 assertTrue(h.getFormatter() instanceof MockFormatter2); 310 assertTrue(h.getFilter() instanceof MockFilter); 311 assertEquals("iso-8859-1", h.getEncoding()); 312 } 313 314 /* 315 * Test the constructor with normal parameter, and invalid relevant log 316 * manager properties are set. 317 */ 318 @TestTargetNew( 319 level = TestLevel.PARTIAL_COMPLETE, 320 notes = "Verifies the constructor with normal parameter, and invalid relevant log manager properties are set.", 321 method = "StreamHandler", 322 args = {java.io.OutputStream.class, java.util.logging.Formatter.class} 323 ) 324 public void testConstructor_HasParameters_InvalidProperties() 325 throws Exception { 326 Properties p = new Properties(); 327 p.put("java.util.logging.StreamHandler.level", INVALID_LEVEL); 328 p.put("java.util.logging.StreamHandler.filter", className + ""); 329 p.put("java.util.logging.StreamHandler.formatter", className + ""); 330 p.put("java.util.logging.StreamHandler.encoding", "XXXX"); 331 LogManager.getLogManager().readConfiguration( 332 EnvironmentHelper.PropertiesToInputStream(p)); 333 334 assertEquals(INVALID_LEVEL, LogManager.getLogManager().getProperty( 335 "java.util.logging.StreamHandler.level")); 336 assertEquals("XXXX", LogManager.getLogManager().getProperty( 337 "java.util.logging.StreamHandler.encoding")); 338 StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), 339 new MockFormatter2()); 340 assertSame(Level.INFO, h.getLevel()); 341 assertTrue(h.getFormatter() instanceof MockFormatter2); 342 assertNull(h.getFilter()); 343 assertNull(h.getEncoding()); 344 } 345 346 /* 347 * Test the constructor with null formatter, and invalid relevant log manager 348 * properties are set. 349 */ 350 @TestTargetNew( 351 level = TestLevel.PARTIAL_COMPLETE, 352 notes = "Verifies the constructor with null formatter, and invalid relevant log manager properties are set.", 353 method = "StreamHandler", 354 args = {java.io.OutputStream.class, java.util.logging.Formatter.class} 355 ) 356 public void testConstructor_HasParameters_ValidPropertiesNullStream() 357 throws Exception { 358 Properties p = new Properties(); 359 p.put("java.util.logging.StreamHandler.level", "FINE"); 360 p.put("java.util.logging.StreamHandler.filter", className 361 + "$MockFilter"); 362 p.put("java.util.logging.StreamHandler.formatter", className 363 + "$MockFormatter"); 364 p.put("java.util.logging.StreamHandler.encoding", "iso-8859-1"); 365 LogManager.getLogManager().readConfiguration( 366 EnvironmentHelper.PropertiesToInputStream(p)); 367 368 assertEquals("FINE", LogManager.getLogManager().getProperty( 369 "java.util.logging.StreamHandler.level")); 370 assertEquals("iso-8859-1", LogManager.getLogManager().getProperty( 371 "java.util.logging.StreamHandler.encoding")); 372 try { 373 new StreamHandler(new ByteArrayOutputStream(), null); 374 fail("Should throw NullPointerException!"); 375 } catch (NullPointerException e) { 376 // expected 377 } 378 } 379 380 /* 381 * Test the constructor with null output stream, and invalid relevant log 382 * manager properties are set. 383 */ 384 @TestTargetNew( 385 level = TestLevel.PARTIAL_COMPLETE, 386 notes = "Verifies the constructor with null output stream, and invalid relevant log manager properties are set.", 387 method = "StreamHandler", 388 args = {java.io.OutputStream.class, java.util.logging.Formatter.class} 389 ) 390 public void testConstructor_HasParameters_ValidPropertiesNullFormatter() 391 throws Exception { 392 Properties p = new Properties(); 393 p.put("java.util.logging.StreamHandler.level", "FINE"); 394 p.put("java.util.logging.StreamHandler.filter", className 395 + "$MockFilter"); 396 p.put("java.util.logging.StreamHandler.formatter", className 397 + "$MockFormatter"); 398 p.put("java.util.logging.StreamHandler.encoding", "iso-8859-1"); 399 LogManager.getLogManager().readConfiguration( 400 EnvironmentHelper.PropertiesToInputStream(p)); 401 402 assertEquals("FINE", LogManager.getLogManager().getProperty( 403 "java.util.logging.StreamHandler.level")); 404 assertEquals("iso-8859-1", LogManager.getLogManager().getProperty( 405 "java.util.logging.StreamHandler.encoding")); 406 try { 407 new StreamHandler(null, new MockFormatter2()); 408 fail("Should throw NullPointerException!"); 409 } catch (NullPointerException e) { 410 // expected 411 } 412 } 413 414 /* 415 * Test close() when having sufficient privilege, and a record has been 416 * written to the output stream. 417 */ 418 @TestTargetNew( 419 level = TestLevel.PARTIAL_COMPLETE, 420 notes = "Verifies close() when having sufficient privilege, and a record has been written to the output stream.", 421 method = "close", 422 args = {} 423 ) 424 public void testClose_SufficientPrivilege_NormalClose() { 425 ByteArrayOutputStream aos = new MockOutputStream(); 426 StreamHandler h = new StreamHandler(aos, new MockFormatter()); 427 h.publish(new LogRecord(Level.SEVERE, 428 "testClose_SufficientPrivilege_NormalClose msg")); 429 h.close(); 430 assertEquals("close", CallVerificationStack.getInstance() 431 .getCurrentSourceMethod()); 432 assertNull(CallVerificationStack.getInstance().pop()); 433 assertEquals("flush", CallVerificationStack.getInstance() 434 .getCurrentSourceMethod()); 435 CallVerificationStack.getInstance().clear(); 436 assertTrue(aos.toString().endsWith("MockFormatter_Tail")); 437 h.close(); 438 } 439 440 /* 441 * Test close() when having sufficient privilege, and an output stream that 442 * always throws exceptions. 443 */ 444 @TestTargetNew( 445 level = TestLevel.PARTIAL_COMPLETE, 446 notes = "Verifies close() when having sufficient privilege, and an output stream that always throws exceptions.", 447 method = "close", 448 args = {} 449 ) 450 public void testClose_SufficientPrivilege_Exception() { 451 ByteArrayOutputStream aos = new MockExceptionOutputStream(); 452 StreamHandler h = new StreamHandler(aos, new MockFormatter()); 453 h.publish(new LogRecord(Level.SEVERE, 454 "testClose_SufficientPrivilege_Exception msg")); 455 h.flush(); 456 h.close(); 457 } 458 459 /* 460 * Test close() when having sufficient privilege, and no record has been 461 * written to the output stream. 462 */ 463 @TestTargetNew( 464 level = TestLevel.PARTIAL_COMPLETE, 465 notes = "Verifies close() method when having sufficient privilege, and no record has been written to the output stream.", 466 method = "close", 467 args = {} 468 ) 469 public void testClose_SufficientPrivilege_DirectClose() { 470 ByteArrayOutputStream aos = new MockOutputStream(); 471 StreamHandler h = new StreamHandler(aos, new MockFormatter()); 472 h.close(); 473 assertEquals("close", CallVerificationStack.getInstance() 474 .getCurrentSourceMethod()); 475 assertNull(CallVerificationStack.getInstance().pop()); 476 assertEquals("flush", CallVerificationStack.getInstance() 477 .getCurrentSourceMethod()); 478 CallVerificationStack.getInstance().clear(); 479 assertEquals("MockFormatter_HeadMockFormatter_Tail", aos.toString()); 480 } 481 482 /* 483 * Test close() when having insufficient privilege. 484 */ 485 @TestTargetNew( 486 level = TestLevel.PARTIAL_COMPLETE, 487 notes = "Verifies SecurityException.", 488 method = "close", 489 args = {} 490 ) 491 public void testClose_InsufficientPrivilege() { 492 StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), 493 new MockFormatter()); 494 495 SecurityManager oldMan = System.getSecurityManager(); 496 System.setSecurityManager(new MockSecurityManager()); 497 try { 498 h.close(); 499 fail("Should throw SecurityException!"); 500 } catch (SecurityException e) { 501 // expected 502 } finally { 503 System.setSecurityManager(oldMan); 504 } 505 } 506 507 /* 508 * Test close() when having no output stream. 509 */ 510 @TestTargetNew( 511 level = TestLevel.PARTIAL_COMPLETE, 512 notes = "Verifies close() method when having no output stream.", 513 method = "close", 514 args = {} 515 ) 516 public void testClose_NoOutputStream() { 517 StreamHandler h = new StreamHandler(); 518 h.close(); 519 } 520 521 /* 522 * Test flush(). 523 */ 524 @TestTargetNew( 525 level = TestLevel.PARTIAL_COMPLETE, 526 notes = "Verifies flush() method.", 527 method = "flush", 528 args = {} 529 ) 530 public void testFlush_Normal() { 531 ByteArrayOutputStream aos = new MockOutputStream(); 532 StreamHandler h = new StreamHandler(aos, new MockFormatter()); 533 h.flush(); 534 assertEquals("flush", CallVerificationStack.getInstance().getCurrentSourceMethod()); 535 assertNull(CallVerificationStack.getInstance().pop()); 536 CallVerificationStack.getInstance().clear(); 537 } 538 539 /* 540 * Test flush() when having no output stream. 541 */ 542 @TestTargetNew( 543 level = TestLevel.PARTIAL_COMPLETE, 544 notes = "Verifies flush() when having no output stream.", 545 method = "flush", 546 args = {} 547 ) 548 public void testFlush_NoOutputStream() { 549 StreamHandler h = new StreamHandler(); 550 h.flush(); 551 } 552 553 /* 554 * Test isLoggable(), use no filter, having no output stream 555 */ 556 @TestTargetNew( 557 level = TestLevel.PARTIAL_COMPLETE, 558 notes = "Verifies isLoggable(), use no filter, having no output stream.", 559 method = "isLoggable", 560 args = {java.util.logging.LogRecord.class} 561 ) 562 public void testIsLoggable_NoOutputStream() { 563 StreamHandler h = new StreamHandler(); 564 LogRecord r = new LogRecord(Level.INFO, null); 565 assertFalse(h.isLoggable(r)); 566 567 h.setLevel(Level.WARNING); 568 assertFalse(h.isLoggable(r)); 569 570 h.setLevel(Level.CONFIG); 571 assertFalse(h.isLoggable(r)); 572 573 r.setLevel(Level.OFF); 574 h.setLevel(Level.OFF); 575 assertFalse(h.isLoggable(r)); 576 } 577 578 /* 579 * Test isLoggable(), use no filter, having output stream 580 */ 581 @TestTargetNew( 582 level = TestLevel.PARTIAL_COMPLETE, 583 notes = "Verifies isLoggable(), use no filter, having output stream.", 584 method = "isLoggable", 585 args = {java.util.logging.LogRecord.class} 586 ) 587 public void testIsLoggable_NoFilter() { 588 StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), 589 new SimpleFormatter()); 590 LogRecord r = new LogRecord(Level.INFO, null); 591 assertTrue(h.isLoggable(r)); 592 593 h.setLevel(Level.WARNING); 594 assertFalse(h.isLoggable(r)); 595 596 h.setLevel(Level.CONFIG); 597 assertTrue(h.isLoggable(r)); 598 599 r.setLevel(Level.OFF); 600 h.setLevel(Level.OFF); 601 assertFalse(h.isLoggable(r)); 602 } 603 604 /* 605 * Test isLoggable(), use a filter, having output stream 606 */ 607 @TestTargetNew( 608 level = TestLevel.PARTIAL_COMPLETE, 609 notes = "Verifies isLoggable(), use a filter, having output stream.", 610 method = "isLoggable", 611 args = {java.util.logging.LogRecord.class} 612 ) 613 public void testIsLoggable_WithFilter() { 614 StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), 615 new SimpleFormatter()); 616 LogRecord r = new LogRecord(Level.INFO, null); 617 h.setFilter(new MockFilter()); 618 assertFalse(h.isLoggable(r)); 619 assertSame(r, CallVerificationStack.getInstance().pop()); 620 621 h.setLevel(Level.CONFIG); 622 assertFalse(h.isLoggable(r)); 623 assertSame(r, CallVerificationStack.getInstance().pop()); 624 625 h.setLevel(Level.WARNING); 626 assertFalse(h.isLoggable(r)); //level to high, data will not reach the filter 627 assertTrue(CallVerificationStack.getInstance().empty()); 628 } 629 630 /* 631 * Test isLoggable(), null log record, having output stream. Handler should 632 * call ErrorManager to handle exceptional case 633 */ 634 @TestTargetNew( 635 level = TestLevel.PARTIAL_COMPLETE, 636 notes = "Verifies isLoggable(), null log record, having output stream. Handler should call ErrorManager to handle exceptional case.", 637 method = "isLoggable", 638 args = {java.util.logging.LogRecord.class} 639 ) 640 public void testIsLoggable_Null() { 641 StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), 642 new SimpleFormatter()); 643 assertFalse(h.isLoggable(null)); 644 } 645 646 /* 647 * Test isLoggable(), null log record, without output stream 648 */ 649 @TestTargetNew( 650 level = TestLevel.PARTIAL_COMPLETE, 651 notes = "Verifies isLoggable(), null log record, without output stream.", 652 method = "isLoggable", 653 args = {java.util.logging.LogRecord.class} 654 ) 655 public void testIsLoggable_Null_NoOutputStream() { 656 StreamHandler h = new StreamHandler(); 657 assertFalse(h.isLoggable(null)); 658 } 659 660 /* 661 * Test publish(), use no filter, having output stream, normal log record. 662 */ 663 @TestTargetNew( 664 level = TestLevel.PARTIAL_COMPLETE, 665 notes = "Verifies publish(), use no filter, having output stream, normal log record.", 666 method = "publish", 667 args = {java.util.logging.LogRecord.class} 668 ) 669 public void testPublish_NoOutputStream() { 670 StreamHandler h = new StreamHandler(); 671 LogRecord r = new LogRecord(Level.INFO, "testPublish_NoOutputStream"); 672 h.publish(r); 673 674 h.setLevel(Level.WARNING); 675 h.publish(r); 676 677 h.setLevel(Level.CONFIG); 678 h.publish(r); 679 680 r.setLevel(Level.OFF); 681 h.setLevel(Level.OFF); 682 h.publish(r); 683 } 684 685 /* 686 * Test publish(), use no filter, having output stream, normal log record. 687 */ 688 @TestTargetNew( 689 level = TestLevel.PARTIAL_COMPLETE, 690 notes = "Verifies publish(), use no filter, having output stream, normal log record.", 691 method = "publish", 692 args = {java.util.logging.LogRecord.class} 693 ) 694 public void testPublish_NoFilter() { 695 ByteArrayOutputStream aos = new ByteArrayOutputStream(); 696 StreamHandler h = new StreamHandler(aos, new MockFormatter()); 697 698 LogRecord r = new LogRecord(Level.INFO, "testPublish_NoFilter"); 699 h.setLevel(Level.INFO); 700 h.publish(r); 701 h.flush(); 702 assertEquals("MockFormatter_Head" + "testPublish_NoFilter", aos 703 .toString()); 704 705 h.setLevel(Level.WARNING); 706 h.publish(r); 707 h.flush(); 708 assertEquals("MockFormatter_Head" + "testPublish_NoFilter", aos 709 .toString()); 710 711 h.setLevel(Level.CONFIG); 712 h.publish(r); 713 h.flush(); 714 assertEquals("MockFormatter_Head" + "testPublish_NoFilter" 715 + "testPublish_NoFilter", aos.toString()); 716 717 r.setLevel(Level.OFF); 718 h.setLevel(Level.OFF); 719 h.publish(r); 720 h.flush(); 721 assertEquals("MockFormatter_Head" + "testPublish_NoFilter" 722 + "testPublish_NoFilter", aos.toString()); 723 } 724 725 /* 726 * Test publish(), use a filter, having output stream, normal log record. 727 */ 728 @TestTargetNew( 729 level = TestLevel.PARTIAL_COMPLETE, 730 notes = "Verifies publish(), use a filter, having output stream, normal log record.", 731 method = "publish", 732 args = {java.util.logging.LogRecord.class} 733 ) 734 public void testPublish_WithFilter() { 735 ByteArrayOutputStream aos = new ByteArrayOutputStream(); 736 StreamHandler h = new StreamHandler(aos, new MockFormatter()); 737 h.setFilter(new MockFilter()); 738 739 LogRecord r = new LogRecord(Level.INFO, "testPublish_WithFilter"); 740 h.setLevel(Level.INFO); 741 h.publish(r); 742 h.flush(); 743 assertEquals("", aos.toString()); 744 assertSame(r, CallVerificationStack.getInstance().pop()); 745 746 h.setLevel(Level.WARNING); 747 h.publish(r); 748 h.flush(); 749 assertEquals("", aos.toString()); 750 assertTrue(CallVerificationStack.getInstance().empty()); 751 752 h.setLevel(Level.CONFIG); 753 h.publish(r); 754 h.flush(); 755 assertEquals("", aos.toString()); 756 assertSame(r, CallVerificationStack.getInstance().pop()); 757 758 r.setLevel(Level.OFF); 759 h.setLevel(Level.OFF); 760 h.publish(r); 761 h.flush(); 762 assertEquals("", aos.toString()); 763 assertTrue(CallVerificationStack.getInstance().empty()); 764 } 765 766 /* 767 * Test publish(), null log record, handler should call ErrorManager to 768 * handle exceptional case 769 */ 770 @TestTargetNew( 771 level = TestLevel.PARTIAL_COMPLETE, 772 notes = "Verifies publish(), null log record, handler should call ErrorManager to handle exceptional case.", 773 method = "publish", 774 args = {java.util.logging.LogRecord.class} 775 ) 776 public void testPublish_Null() { 777 StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), 778 new SimpleFormatter()); 779 h.publish(null); 780 } 781 782 /* 783 * Test publish(), null log record, without output stream 784 */ 785 @TestTargetNew( 786 level = TestLevel.PARTIAL_COMPLETE, 787 notes = "Verifies publish(), null log record, without output stream.", 788 method = "publish", 789 args = {java.util.logging.LogRecord.class} 790 ) 791 public void testPublish_Null_NoOutputStream() { 792 StreamHandler h = new StreamHandler(); 793 h.publish(null); 794 // regression test for Harmony-1279 795 MockFilter filter = new MockFilter(); 796 h.setLevel(Level.FINER); 797 h.setFilter(filter); 798 LogRecord record = new LogRecord(Level.FINE, "abc"); 799 h.publish(record); 800 // verify that filter.isLoggable is not called, because there's no 801 // associated output stream. 802 assertTrue(CallVerificationStack.getInstance().empty()); 803 } 804 805 /* 806 * Test publish(), a log record with empty msg, having output stream 807 */ 808 @TestTargetNew( 809 level = TestLevel.PARTIAL_COMPLETE, 810 notes = "Verifies publish(), a log record with empty msg, having output stream.", 811 method = "publish", 812 args = {java.util.logging.LogRecord.class} 813 ) 814 public void testPublish_EmptyMsg() { 815 ByteArrayOutputStream aos = new ByteArrayOutputStream(); 816 StreamHandler h = new StreamHandler(aos, new MockFormatter()); 817 LogRecord r = new LogRecord(Level.INFO, ""); 818 h.publish(r); 819 h.flush(); 820 assertEquals("MockFormatter_Head", aos.toString()); 821 } 822 823 /* 824 * Test publish(), a log record with null msg, having output stream 825 */ 826 @TestTargetNew( 827 level = TestLevel.PARTIAL_COMPLETE, 828 notes = "Verifies publish(), a log record with null msg, having output stream.", 829 method = "publish", 830 args = {java.util.logging.LogRecord.class} 831 ) 832 public void testPublish_NullMsg() { 833 ByteArrayOutputStream aos = new ByteArrayOutputStream(); 834 StreamHandler h = new StreamHandler(aos, new MockFormatter()); 835 LogRecord r = new LogRecord(Level.INFO, null); 836 h.publish(r); 837 h.flush(); 838 assertEquals("MockFormatter_Head", aos.toString()); 839 } 840 841 /* 842 * Test publish(), after close. 843 */ 844 @TestTargetNew( 845 level = TestLevel.PARTIAL_COMPLETE, 846 notes = "Verifies publish(), after close.", 847 method = "publish", 848 args = {java.util.logging.LogRecord.class} 849 ) 850 public void testPublish_AfterClose() throws Exception { 851 Properties p = new Properties(); 852 p.put("java.util.logging.StreamHandler.level", "FINE"); 853 LogManager.getLogManager().readConfiguration( 854 EnvironmentHelper.PropertiesToInputStream(p)); 855 856 ByteArrayOutputStream aos = new ByteArrayOutputStream(); 857 StreamHandler h = new StreamHandler(aos, new MockFormatter()); 858 assertSame(h.getLevel(), Level.FINE); 859 LogRecord r = new LogRecord(Level.INFO, "testPublish_NoFormatter"); 860 assertTrue(h.isLoggable(r)); 861 h.close(); 862 assertFalse(h.isLoggable(r)); 863 h.publish(r); 864 h.flush(); 865 assertEquals("MockFormatter_HeadMockFormatter_Tail", aos.toString()); 866 } 867 868 /* 869 * Test setEncoding() method with supported encoding. 870 */ 871 @TestTargetNew( 872 level = TestLevel.PARTIAL_COMPLETE, 873 notes = "Verifies setEncoding() method with supported encoding.", 874 method = "setEncoding", 875 args = {java.lang.String.class} 876 ) 877 public void testSetEncoding_Normal() throws Exception { 878 ByteArrayOutputStream aos = new ByteArrayOutputStream(); 879 StreamHandler h = new StreamHandler(aos, new MockFormatter()); 880 h.setEncoding("iso-8859-1"); 881 assertEquals("iso-8859-1", h.getEncoding()); 882 LogRecord r = new LogRecord(Level.INFO, "\u6881\u884D\u8F69"); 883 h.publish(r); 884 h.flush(); 885 886 byte[] bytes = encoder.encode( 887 CharBuffer.wrap("MockFormatter_Head" + "\u6881\u884D\u8F69")) 888 .array(); 889 assertTrue(Arrays.equals(bytes, aos.toByteArray())); 890 } 891 892 /* 893 * Test setEncoding() method with supported encoding, after a log record 894 * has been written. 895 */ 896 @TestTargetNew( 897 level = TestLevel.PARTIAL_COMPLETE, 898 notes = "Verifies setEncoding() method with supported encoding, after a log record has been written.", 899 method = "setEncoding", 900 args = {java.lang.String.class} 901 ) 902 public void testSetEncoding_AfterPublish() throws Exception { 903 ByteArrayOutputStream aos = new ByteArrayOutputStream(); 904 StreamHandler h = new StreamHandler(aos, new MockFormatter()); 905 h.setEncoding("iso-8859-1"); 906 assertEquals("iso-8859-1", h.getEncoding()); 907 LogRecord r = new LogRecord(Level.INFO, "\u6881\u884D\u8F69"); 908 h.publish(r); 909 h.flush(); 910 assertTrue(Arrays.equals(aos.toByteArray(), encoder.encode( 911 CharBuffer.wrap("MockFormatter_Head" + "\u6881\u884D\u8F69")) 912 .array())); 913 914 h.setEncoding("iso8859-1"); 915 assertEquals("iso8859-1", h.getEncoding()); 916 r = new LogRecord(Level.INFO, "\u6881\u884D\u8F69"); 917 h.publish(r); 918 h.flush(); 919 assertFalse(Arrays.equals(aos.toByteArray(), encoder.encode( 920 CharBuffer.wrap("MockFormatter_Head" + "\u6881\u884D\u8F69" 921 + "testSetEncoding_Normal2")).array())); 922 byte[] b0 = aos.toByteArray(); 923 byte[] b1 = encoder.encode( 924 CharBuffer.wrap("MockFormatter_Head" + "\u6881\u884D\u8F69")) 925 .array(); 926 byte[] b2 = encoder.encode(CharBuffer.wrap("\u6881\u884D\u8F69")) 927 .array(); 928 byte[] b3 = new byte[b1.length + b2.length]; 929 System.arraycopy(b1, 0, b3, 0, b1.length); 930 System.arraycopy(b2, 0, b3, b1.length, b2.length); 931 assertTrue(Arrays.equals(b0, b3)); 932 } 933 934 /* 935 * Test setEncoding() methods with null. 936 */ 937 @TestTargetNew( 938 level = TestLevel.PARTIAL_COMPLETE, 939 notes = "Verifies setEncoding() methods with null.", 940 method = "setEncoding", 941 args = {java.lang.String.class} 942 ) 943 public void testSetEncoding_Null() throws Exception { 944 StreamHandler h = new StreamHandler(); 945 h.setEncoding(null); 946 assertNull(h.getEncoding()); 947 } 948 949 /* 950 * Test setEncoding() methods with unsupported encoding. 951 */ 952 @TestTargetNew( 953 level = TestLevel.PARTIAL_COMPLETE, 954 notes = "Verifies setEncoding() methods with unsupported encoding.", 955 method = "setEncoding", 956 args = {java.lang.String.class} 957 ) 958 public void testSetEncoding_Unsupported() { 959 StreamHandler h = new StreamHandler(); 960 try { 961 h.setEncoding("impossible"); 962 fail("Should throw UnsupportedEncodingException!"); 963 } catch (UnsupportedEncodingException e) { 964 // expected 965 } 966 assertNull(h.getEncoding()); 967 } 968 969 /* 970 * Test setEncoding() with insufficient privilege. 971 */ 972 @TestTargetNew( 973 level = TestLevel.PARTIAL_COMPLETE, 974 notes = "Verifies setEncoding() method with insufficient privilege.", 975 method = "setEncoding", 976 args = {java.lang.String.class} 977 ) 978 public void testSetEncoding_InsufficientPrivilege() throws Exception { 979 StreamHandler h = new StreamHandler(); 980 SecurityManager oldMan = System.getSecurityManager(); 981 System.setSecurityManager(new MockSecurityManager()); 982 // set a normal value 983 try { 984 h.setEncoding("iso-8859-1"); 985 fail("Should throw SecurityException!"); 986 } catch (SecurityException e) { 987 // expected 988 } finally { 989 System.setSecurityManager(oldMan); 990 } 991 assertNull(h.getEncoding()); 992 System.setSecurityManager(new MockSecurityManager()); 993 // set an invalid value 994 try { 995 996 h.setEncoding("impossible"); 997 fail("Should throw SecurityException!"); 998 } catch (SecurityException e) { 999 // expected 1000 } finally { 1001 System.setSecurityManager(oldMan); 1002 } 1003 assertNull(h.getEncoding()); 1004 } 1005 1006 /* 1007 * Test setEncoding() methods will flush a stream before setting. 1008 */ 1009 @TestTargetNew( 1010 level = TestLevel.PARTIAL_COMPLETE, 1011 notes = "Verifies that setEncoding() method will flush a stream before setting.", 1012 method = "setEncoding", 1013 args = {java.lang.String.class} 1014 ) 1015 public void testSetEncoding_FlushBeforeSetting() throws Exception { 1016 ByteArrayOutputStream aos = new ByteArrayOutputStream(); 1017 StreamHandler h = new StreamHandler(aos, new MockFormatter()); 1018 LogRecord r = new LogRecord(Level.INFO, "abcd"); 1019 h.publish(r); 1020 assertFalse(aos.toString().indexOf("abcd") > 0); 1021 h.setEncoding("iso-8859-1"); 1022 assertTrue(aos.toString().indexOf("abcd") > 0); 1023 } 1024 1025 /* 1026 * Test setOutputStream() with null. 1027 */ 1028 @TestTargetNew( 1029 level = TestLevel.PARTIAL_COMPLETE, 1030 notes = "Verifies setOutputStream() method with null.", 1031 method = "setOutputStream", 1032 args = {java.io.OutputStream.class} 1033 ) 1034 public void testSetOutputStream_null() { 1035 MockStreamHandler h = new MockStreamHandler( 1036 new ByteArrayOutputStream(), new SimpleFormatter()); 1037 try { 1038 h.setOutputStream(null); 1039 fail("Should throw NullPointerException!"); 1040 } catch (NullPointerException e) { 1041 // expected 1042 } 1043 } 1044 1045 /* 1046 * Test setOutputStream() under normal condition. 1047 */ 1048 @TestTargetNew( 1049 level = TestLevel.PARTIAL_COMPLETE, 1050 notes = "Verifies setOutputStream() method under normal condition.", 1051 method = "setOutputStream", 1052 args = {java.io.OutputStream.class} 1053 ) 1054 public void testSetOutputStream_Normal() { 1055 ByteArrayOutputStream aos = new ByteArrayOutputStream(); 1056 MockStreamHandler h = new MockStreamHandler(aos, new MockFormatter()); 1057 1058 LogRecord r = new LogRecord(Level.INFO, "testSetOutputStream_Normal"); 1059 h.publish(r); 1060 assertSame(r, CallVerificationStack.getInstance().pop()); 1061 assertTrue(CallVerificationStack.getInstance().empty()); 1062 h.flush(); 1063 assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal", aos 1064 .toString()); 1065 1066 ByteArrayOutputStream aos2 = new ByteArrayOutputStream(); 1067 h.setOutputStream(aos2); 1068 assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal" 1069 + "MockFormatter_Tail", aos.toString()); 1070 r = new LogRecord(Level.INFO, "testSetOutputStream_Normal2"); 1071 h.publish(r); 1072 assertSame(r, CallVerificationStack.getInstance().pop()); 1073 assertTrue(CallVerificationStack.getInstance().empty()); 1074 h.flush(); 1075 assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal2", aos2 1076 .toString()); 1077 assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal" 1078 + "MockFormatter_Tail", aos.toString()); 1079 } 1080 1081 /* 1082 * Test setOutputStream() after close. 1083 */ 1084 @TestTargetNew( 1085 level = TestLevel.PARTIAL_COMPLETE, 1086 notes = "Verifies setOutputStream() method after close.", 1087 method = "setOutputStream", 1088 args = {java.io.OutputStream.class} 1089 ) 1090 public void testSetOutputStream_AfterClose() { 1091 ByteArrayOutputStream aos = new ByteArrayOutputStream(); 1092 MockStreamHandler h = new MockStreamHandler(aos, new MockFormatter()); 1093 1094 LogRecord r = new LogRecord(Level.INFO, "testSetOutputStream_Normal"); 1095 h.publish(r); 1096 assertSame(r, CallVerificationStack.getInstance().pop()); 1097 assertTrue(CallVerificationStack.getInstance().empty()); 1098 h.flush(); 1099 assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal", aos 1100 .toString()); 1101 h.close(); 1102 1103 ByteArrayOutputStream aos2 = new ByteArrayOutputStream(); 1104 h.setOutputStream(aos2); 1105 assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal" 1106 + "MockFormatter_Tail", aos.toString()); 1107 r = new LogRecord(Level.INFO, "testSetOutputStream_Normal2"); 1108 h.publish(r); 1109 assertSame(r, CallVerificationStack.getInstance().pop()); 1110 assertTrue(CallVerificationStack.getInstance().empty()); 1111 h.flush(); 1112 assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal2", aos2 1113 .toString()); 1114 assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal" 1115 + "MockFormatter_Tail", aos.toString()); 1116 } 1117 1118 /* 1119 * Test setOutputStream() when having insufficient privilege. 1120 */ 1121 @TestTargetNew( 1122 level = TestLevel.PARTIAL_COMPLETE, 1123 notes = "Verifies setOutputStream() method when having insufficient privilege.", 1124 method = "setOutputStream", 1125 args = {java.io.OutputStream.class} 1126 ) 1127 public void testSetOutputStream_InsufficientPrivilege() { 1128 MockStreamHandler h = new MockStreamHandler(); 1129 SecurityManager oldMan = System.getSecurityManager(); 1130 System.setSecurityManager(new MockSecurityManager()); 1131 1132 try { 1133 h.setOutputStream(new ByteArrayOutputStream()); 1134 fail("Should throw SecurityException!"); 1135 } catch (SecurityException e) { 1136 // expected 1137 } finally { 1138 System.setSecurityManager(oldMan); 1139 } 1140 1141 h = new MockStreamHandler(); 1142 System.setSecurityManager(new MockSecurityManager()); 1143 try { 1144 h.setOutputStream(null); 1145 fail("Should throw NullPointerException!"); 1146 } catch (NullPointerException e) { 1147 // expected 1148 } finally { 1149 System.setSecurityManager(oldMan); 1150 } 1151 } 1152 1153 /* 1154 * A mock stream handler, expose setOutputStream. 1155 */ 1156 public static class MockStreamHandler extends StreamHandler { 1157 public MockStreamHandler() { 1158 super(); 1159 } 1160 1161 public MockStreamHandler(OutputStream out, Formatter formatter) { 1162 super(out, formatter); 1163 } 1164 1165 public void setOutputStream(OutputStream out) { 1166 super.setOutputStream(out); 1167 } 1168 1169 public boolean isLoggable(LogRecord r) { 1170 CallVerificationStack.getInstance().push(r); 1171 return super.isLoggable(r); 1172 } 1173 } 1174 1175 /* 1176 * A mock filter, always return false. 1177 */ 1178 public static class MockFilter implements Filter { 1179 1180 public boolean isLoggable(LogRecord record) { 1181 CallVerificationStack.getInstance().push(record); 1182 return false; 1183 } 1184 } 1185 1186 /* 1187 * A mock formatter. 1188 */ 1189 public static class MockFormatter extends java.util.logging.Formatter { 1190 public String format(LogRecord r) { 1191 // System.out.println("formatter called..."); 1192 return super.formatMessage(r); 1193 } 1194 1195 /* 1196 * (non-Javadoc) 1197 * 1198 * @see java.util.logging.Formatter#getHead(java.util.logging.Handler) 1199 */ 1200 public String getHead(Handler h) { 1201 return "MockFormatter_Head"; 1202 } 1203 1204 /* 1205 * (non-Javadoc) 1206 * 1207 * @see java.util.logging.Formatter#getTail(java.util.logging.Handler) 1208 */ 1209 public String getTail(Handler h) { 1210 return "MockFormatter_Tail"; 1211 } 1212 } 1213 1214 /* 1215 * Another mock formatter. 1216 */ 1217 public static class MockFormatter2 extends java.util.logging.Formatter { 1218 public String format(LogRecord r) { 1219 // System.out.println("formatter2 called..."); 1220 return r.getMessage(); 1221 } 1222 } 1223 1224 /* 1225 * A mock output stream. 1226 */ 1227 public static class MockOutputStream extends ByteArrayOutputStream { 1228 1229 /* 1230 * (non-Javadoc) 1231 * 1232 * @see java.io.OutputStream#close() 1233 */ 1234 public void close() throws IOException { 1235 CallVerificationStack.getInstance().push(null); 1236 super.close(); 1237 } 1238 1239 /* 1240 * (non-Javadoc) 1241 * 1242 * @see java.io.OutputStream#flush() 1243 */ 1244 public void flush() throws IOException { 1245 CallVerificationStack.getInstance().push(null); 1246 super.flush(); 1247 } 1248 1249 /* 1250 * (non-Javadoc) 1251 * 1252 * @see java.io.OutputStream#write(int) 1253 */ 1254 public void write(int oneByte) { 1255 super.write(oneByte); 1256 } 1257 } 1258 1259 /* 1260 * A mock output stream that always throw exception. 1261 */ 1262 public static class MockExceptionOutputStream extends ByteArrayOutputStream { 1263 1264 /* 1265 * (non-Javadoc) 1266 * 1267 * @see java.io.OutputStream#close() 1268 */ 1269 public void close() throws IOException { 1270 throw new IOException(); 1271 } 1272 1273 /* 1274 * (non-Javadoc) 1275 * 1276 * @see java.io.OutputStream#flush() 1277 */ 1278 public void flush() throws IOException { 1279 throw new IOException(); 1280 } 1281 1282 /* 1283 * (non-Javadoc) 1284 * 1285 * @see java.io.OutputStream#write(byte[], int, int) 1286 */ 1287 public synchronized void write(byte[] buffer, int offset, int count) { 1288 throw new NullPointerException(); 1289 } 1290 1291 /* 1292 * (non-Javadoc) 1293 * 1294 * @see java.io.OutputStream#write(int) 1295 */ 1296 public synchronized void write(int oneByte) { 1297 throw new NullPointerException(); 1298 } 1299 } 1300 1301 /* 1302 * Used to grant all permissions except logging control. 1303 */ 1304 public static class MockSecurityManager extends SecurityManager { 1305 1306 public MockSecurityManager() { 1307 } 1308 1309 public void checkPermission(Permission perm) { 1310 // grant all permissions except logging control 1311 if (perm instanceof LoggingPermission) { 1312 throw new SecurityException(); 1313 } 1314 } 1315 1316 public void checkPermission(Permission perm, Object context) { 1317 // grant all permissions except logging control 1318 if (perm instanceof LoggingPermission) { 1319 throw new SecurityException(); 1320 } 1321 } 1322 } 1323 1324} 1325