diff options
author | Elliott Hughes <enh@google.com> | 2010-02-18 17:20:15 -0800 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2010-02-19 10:35:06 -0800 |
commit | 7ee3a061452c5a7e5c8e661219a1f08a14171858 (patch) | |
tree | f995fb4c053e4c68a86909aa72aee284cb39133a /logging/src | |
parent | 1d1f9244611a8ba3be65795d4ea6e84bcecdb89d (diff) | |
download | libcore-7ee3a061452c5a7e5c8e661219a1f08a14171858.zip libcore-7ee3a061452c5a7e5c8e661219a1f08a14171858.tar.gz libcore-7ee3a061452c5a7e5c8e661219a1f08a14171858.tar.bz2 |
Resync a load of tests with upstream, make our build faster.
I started off with a mission to remove uses of dalvik.annotation.* (stuff
like @TestTargetNew and other useless junk that just makes it harder to
stay in sync with upstream). I wrote a script to go through tests showing
me the diff between what we have and what upstream has, thinking that in
cases where upstream has also added tests, I may as well pull them in at
the same time...
...but I didn't realize how close we were to having dx fill its 1.5GiB heap.
After trying various alternatives, I decided to bite the bullet and break
core-tests up into one .jar per module. This adds parallelism back into this,
the slowest part of our build. (I can do even better, but I'll do that in a
separate patch, preferably after we've merged recent changes from master.)
Only a couple of dependencies were problematic: the worthless TestSuiteFactory
which already contained a comment suggesting we get rid of it, and the fact
that some tests -- most notably the concurrent ones -- also contained main
methods that started the JUnit tty-based TestRunner.
(In the long run, we want to be running the harmony tests directly from a
pristine "svn co" of upstream, using DalvikRunner. But this will be a big
help in the meantime, and starts the work of getting our current copy of
the tests into a state where we can start to extract any meaningful
changes/additions we've made.)
Diffstat (limited to 'logging/src')
8 files changed, 2141 insertions, 2729 deletions
diff --git a/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/AllTests.java b/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/AllTests.java index 1094f25..91b3e1a 100644 --- a/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/AllTests.java +++ b/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/AllTests.java @@ -25,8 +25,7 @@ import junit.framework.TestSuite; public class AllTests { public static Test suite() { - TestSuite suite = tests.TestSuiteFactory.createTestSuite( - "Suite for org.apache.harmony.logging.tests.java.util.logging"); + TestSuite suite = new TestSuite("Suite for org.apache.harmony.logging.tests.java.util.logging"); // $JUnit-BEGIN$ suite.addTestSuite(ConsoleHandlerTest.class); suite.addTestSuite(ErrorManagerTest.class); diff --git a/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/ConsoleHandlerTest.java b/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/ConsoleHandlerTest.java index 91de2a5..b2204a3 100644 --- a/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/ConsoleHandlerTest.java +++ b/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/ConsoleHandlerTest.java @@ -17,17 +17,11 @@ package org.apache.harmony.logging.tests.java.util.logging; -import dalvik.annotation.TestTargetClass; -import dalvik.annotation.TestTargetNew; -import dalvik.annotation.TestLevel; -import dalvik.annotation.TestTargets; - import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; import java.security.Permission; -import java.util.Currency; import java.util.Properties; import java.util.logging.ConsoleHandler; import java.util.logging.Filter; @@ -48,638 +42,542 @@ import tests.util.CallVerificationStack; /** * Test class java.util.logging.ConsoleHandler */ -@TestTargetClass(ConsoleHandler.class) public class ConsoleHandlerTest extends TestCase { - private final static String INVALID_LEVEL = "impossible_level"; - - private final PrintStream err = System.err; - - private OutputStream errSubstituteStream = null; - - private static String className = ConsoleHandlerTest.class.getName(); - - /* - * @see TestCase#setUp() - */ - protected void setUp() throws Exception { - super.setUp(); - errSubstituteStream = new MockOutputStream(); - System.setErr(new PrintStream(errSubstituteStream)); - LogManager.getLogManager().reset(); - } - - /* - * @see TestCase#tearDown() - */ - protected void tearDown() throws Exception { - super.tearDown(); - LogManager.getLogManager().reset(); - CallVerificationStack.getInstance().clear(); - System.setErr(err); - } - - /* - * Test the constructor with no relevant log manager properties are set. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies the constructor with no relevant log manager properties are set.", - method = "ConsoleHandler", - args = {} - ) - public void testConstructor_NoProperties() { - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.ConsoleHandler.level")); - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.ConsoleHandler.filter")); - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.ConsoleHandler.formatter")); - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.ConsoleHandler.encoding")); - - ConsoleHandler h = new ConsoleHandler(); - assertSame(h.getLevel(), Level.INFO); - assertTrue(h.getFormatter() instanceof SimpleFormatter); - assertNull(h.getFilter()); - assertSame(h.getEncoding(), null); - } - - /* - * Test the constructor with insufficient privilege. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies the constructor with insufficient privilege.", - method = "ConsoleHandler", - args = {} - ) - public void testConstructor_InsufficientPrivilege() { - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.ConsoleHandler.level")); - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.ConsoleHandler.filter")); - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.ConsoleHandler.formatter")); - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.ConsoleHandler.encoding")); - - SecurityManager oldMan = System.getSecurityManager(); - System.setSecurityManager(new MockSecurityManager()); - // set a normal value - try { - ConsoleHandler h = new ConsoleHandler(); - assertSame(h.getLevel(), Level.INFO); - assertTrue(h.getFormatter() instanceof SimpleFormatter); - assertNull(h.getFilter()); - assertSame(h.getEncoding(), null); - } finally { - System.setSecurityManager(oldMan); - } - } - - /* - * Test the constructor with valid relevant log manager properties are set. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies the constructor with valid relevant log manager properties are set.", - method = "ConsoleHandler", - args = {} - ) - public void testConstructor_ValidProperties() throws Exception { - Properties p = new Properties(); - p.put("java.util.logging.ConsoleHandler.level", "FINE"); - p.put("java.util.logging.ConsoleHandler.filter", className - + "$MockFilter"); - p.put("java.util.logging.ConsoleHandler.formatter", className - + "$MockFormatter"); - p.put("java.util.logging.ConsoleHandler.encoding", "iso-8859-1"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - - assertEquals(LogManager.getLogManager().getProperty( - "java.util.logging.ConsoleHandler.level"), "FINE"); - assertEquals(LogManager.getLogManager().getProperty( - "java.util.logging.ConsoleHandler.encoding"), "iso-8859-1"); - ConsoleHandler h = new ConsoleHandler(); - assertSame(h.getLevel(), Level.parse("FINE")); - assertTrue(h.getFormatter() instanceof MockFormatter); - assertTrue(h.getFilter() instanceof MockFilter); - assertEquals(h.getEncoding(), "iso-8859-1"); - } - - /* - * Test the constructor with invalid relevant log manager properties are - * set. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies the constructor with invalid relevant log manager properties are set.", - method = "ConsoleHandler", - args = {} - ) - public void testConstructor_InvalidProperties() throws Exception { - Properties p = new Properties(); - p.put("java.util.logging.ConsoleHandler.level", INVALID_LEVEL); - p.put("java.util.logging.ConsoleHandler.filter", className); - p.put("java.util.logging.ConsoleHandler.formatter", className); - p.put("java.util.logging.ConsoleHandler.encoding", "XXXX"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - - assertEquals(LogManager.getLogManager().getProperty( - "java.util.logging.ConsoleHandler.level"), INVALID_LEVEL); - assertEquals(LogManager.getLogManager().getProperty( - "java.util.logging.ConsoleHandler.encoding"), "XXXX"); - ConsoleHandler h = new ConsoleHandler(); - assertSame(h.getLevel(), Level.INFO); - assertTrue(h.getFormatter() instanceof SimpleFormatter); - assertNull(h.getFilter()); - assertNull(h.getEncoding()); - h.publish(new LogRecord(Level.SEVERE, "test")); - assertNull(h.getEncoding()); - } - - /* - * Test close() when having sufficient privilege, and a record has been - * written to the output stream. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies close() when having sufficient privilege, and a record has been written to the output stream.", - method = "close", - args = {} - ) - public void testClose_SufficientPrivilege_NormalClose() throws Exception { - Properties p = new Properties(); - p.put("java.util.logging.ConsoleHandler.formatter", className - + "$MockFormatter"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - ConsoleHandler h = new ConsoleHandler(); - h.publish(new LogRecord(Level.SEVERE, - "testClose_SufficientPrivilege_NormalClose msg")); - h.close(); - assertEquals("flush", CallVerificationStack.getInstance() - .getCurrentSourceMethod()); - assertNull(CallVerificationStack.getInstance().pop()); - h.close(); - } - - /* - * Test close() when having sufficient privilege, and an output stream that - * always throws exceptions. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies close() when having sufficient privilege, and an output stream that always throws exceptions", - method = "close", - args = {} - ) - public void testClose_SufficientPrivilege_Exception() throws Exception { - Properties p = new Properties(); - p.put("java.util.logging.ConsoleHandler.formatter", className - + "$MockFormatter"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - ConsoleHandler h = new ConsoleHandler(); - - h.publish(new LogRecord(Level.SEVERE, - "testClose_SufficientPrivilege_Exception msg")); - h.flush(); - h.close(); - } - - /* - * Test close() when having sufficient privilege, and no record has been - * written to the output stream. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Checks close() when having sufficient privilege, and no record has been written to the output stream", - method = "close", - args = {} - ) - public void testClose_SufficientPrivilege_DirectClose() throws Exception { - Properties p = new Properties(); - p.put("java.util.logging.ConsoleHandler.formatter", className - + "$MockFormatter"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - ConsoleHandler h = new ConsoleHandler(); - - h.close(); - assertEquals("flush", CallVerificationStack.getInstance() - .getCurrentSourceMethod()); - assertNull(CallVerificationStack.getInstance().pop()); - assertTrue(CallVerificationStack.getInstance().empty()); - } - - /* - * Test close() when having insufficient privilege. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies close() when insufficient privilege is set up.", - method = "close", - args = {} - ) - public void testClose_InsufficientPrivilege() throws Exception { - Properties p = new Properties(); - p.put("java.util.logging.ConsoleHandler.formatter", className - + "$MockFormatter"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - ConsoleHandler h = new ConsoleHandler(); - SecurityManager oldMan = System.getSecurityManager(); - System.setSecurityManager(new MockSecurityManager()); - try { - h.close(); - } finally { - System.setSecurityManager(oldMan); - } - } - - /* - * Test publish(), use no filter, having output stream, normal log record. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies publish(), use no filter, having output stream, normal log record.", - method = "publish", - args = {java.util.logging.LogRecord.class} - ) - public void testPublish_NoFilter() throws Exception { - Properties p = new Properties(); - p.put("java.util.logging.ConsoleHandler.formatter", className - + "$MockFormatter"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - ConsoleHandler h = new ConsoleHandler(); - - LogRecord r = new LogRecord(Level.INFO, "testPublish_NoFilter"); - h.setLevel(Level.INFO); - h.publish(r); - h.flush(); - assertEquals("MockFormatter_Head" + "testPublish_NoFilter", - this.errSubstituteStream.toString()); - - h.setLevel(Level.WARNING); - h.publish(r); - h.flush(); - assertEquals("MockFormatter_Head" + "testPublish_NoFilter", - this.errSubstituteStream.toString()); - - h.setLevel(Level.CONFIG); - h.publish(r); - h.flush(); - assertEquals("MockFormatter_Head" + "testPublish_NoFilter" - + "testPublish_NoFilter", this.errSubstituteStream.toString()); - - r.setLevel(Level.OFF); - h.setLevel(Level.OFF); - h.publish(r); - h.flush(); - assertEquals("MockFormatter_Head" + "testPublish_NoFilter" - + "testPublish_NoFilter", this.errSubstituteStream.toString()); - } - - /* - * Test publish(), after system err is reset. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies publish(), after system err is reset.", - method = "publish", - args = {java.util.logging.LogRecord.class} - ) - public void testPublish_AfterResetSystemErr() throws Exception { - Properties p = new Properties(); - p.put("java.util.logging.ConsoleHandler.formatter", className - + "$MockFormatter"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - ConsoleHandler h = new ConsoleHandler(); - h.setFilter(new MockFilter()); - - System.setErr(new PrintStream(new ByteArrayOutputStream())); - - LogRecord r = new LogRecord(Level.INFO, "testPublish_WithFilter"); - h.setLevel(Level.INFO); - h.publish(r); - assertNull(CallVerificationStack.getInstance().pop()); - assertSame(r, CallVerificationStack.getInstance().pop()); - assertEquals("", this.errSubstituteStream.toString()); - } - - /* - * Test publish(), use a filter, having output stream, normal log record. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies publish(), use a filter, having output stream, normal log record.", - method = "publish", - args = {java.util.logging.LogRecord.class} - ) - public void testPublish_WithFilter() throws Exception { - Properties p = new Properties(); - p.put("java.util.logging.ConsoleHandler.formatter", className - + "$MockFormatter"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - ConsoleHandler h = new ConsoleHandler(); - h.setFilter(new MockFilter()); - - LogRecord r = new LogRecord(Level.INFO, "testPublish_WithFilter"); - h.setLevel(Level.INFO); - h.publish(r); - assertNull(CallVerificationStack.getInstance().pop()); - assertSame(r, CallVerificationStack.getInstance().pop()); - assertEquals("", this.errSubstituteStream.toString()); - - h.setLevel(Level.WARNING); - h.publish(r); - assertNull(CallVerificationStack.getInstance().pop()); - assertTrue(CallVerificationStack.getInstance().empty()); - assertEquals("", this.errSubstituteStream.toString()); - - h.setLevel(Level.CONFIG); - h.publish(r); - assertNull(CallVerificationStack.getInstance().pop()); - assertSame(r, CallVerificationStack.getInstance().pop()); - assertEquals("", this.errSubstituteStream.toString()); - - r.setLevel(Level.OFF); - h.setLevel(Level.OFF); - h.publish(r); - assertNull(CallVerificationStack.getInstance().pop()); - assertEquals("", this.errSubstituteStream.toString()); - assertTrue(CallVerificationStack.getInstance().empty()); - } - - /* - * Test publish(), null log record, having output stream, spec said - * rather than throw exception, handler should call errormanager to handle - * exception case, so NullPointerException shouldn't be thrown. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies publish(), null log record, having output stream, spec said rather than throw exception, handler should call errormanager to handle exception case, so NullPointerException shouldn't be thrown.", - method = "publish", - args = {java.util.logging.LogRecord.class} - ) - public void testPublish_Null() throws Exception { - Properties p = new Properties(); - p.put("java.util.logging.ConsoleHandler.formatter", className - + "$MockFormatter"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - ConsoleHandler h = new ConsoleHandler(); - h.publish(null); - } - - /* - * Test publish(), a log record with empty msg, having output stream - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies publish(), a log record with empty msg, having output stream.", - method = "publish", - args = {java.util.logging.LogRecord.class} - ) - public void testPublish_EmptyMsg() throws Exception { - Properties p = new Properties(); - p.put("java.util.logging.ConsoleHandler.formatter", className - + "$MockFormatter"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - ConsoleHandler h = new ConsoleHandler(); - LogRecord r = new LogRecord(Level.INFO, ""); - h.publish(r); - h.flush(); - assertEquals("MockFormatter_Head", this.errSubstituteStream.toString()); - } - - /* - * Test publish(), a log record with null msg, having output stream - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies publish(), a log record with null msg, having output stream.", - method = "publish", - args = {java.util.logging.LogRecord.class} - ) - public void testPublish_NullMsg() throws Exception { - Properties p = new Properties(); - p.put("java.util.logging.ConsoleHandler.formatter", className - + "$MockFormatter"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - ConsoleHandler h = new ConsoleHandler(); - LogRecord r = new LogRecord(Level.INFO, null); - h.publish(r); - h.flush(); - // assertEquals("MockFormatter_Head", - // this.errSubstituteStream.toString()); - } - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies publish method after close.", - method = "publish", - args = {java.util.logging.LogRecord.class} - ) - public void testPublish_AfterClose() throws Exception { - PrintStream backup = System.err; - try { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - System.setErr(new PrintStream(bos)); - Properties p = new Properties(); - p.put("java.util.logging.ConsoleHandler.level", "FINE"); - p.put("java.util.logging.ConsoleHandler.formatter", className - + "$MockFormatter"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - ConsoleHandler h = new ConsoleHandler(); - assertSame(h.getLevel(), Level.FINE); - LogRecord r1 = new LogRecord(Level.INFO, "testPublish_Record1"); - LogRecord r2 = new LogRecord(Level.INFO, "testPublish_Record2"); - assertTrue(h.isLoggable(r1)); - h.publish(r1); - assertTrue(bos.toString().indexOf("testPublish_Record1") >= 0); - h.close(); - // assertFalse(h.isLoggable(r)); - assertTrue(h.isLoggable(r2)); - h.publish(r2); - assertTrue(bos.toString().indexOf("testPublish_Record2") >= 0); - h.flush(); - // assertEquals("MockFormatter_Head", - // this.errSubstituteStream.toString()); - } catch (IOException e) { - e.printStackTrace(); - } finally { - System.setErr(backup); - } - } - - /* - * Test setOutputStream() under normal condition. - */ - @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "Verifies setOutputStream() under normal condition.", - method = "setOutputStream", - args = {java.io.OutputStream.class} - ) - public void testSetOutputStream_Normal() { - MockStreamHandler h = new MockStreamHandler(); - h.setFormatter(new MockFormatter()); - - LogRecord r = new LogRecord(Level.INFO, "testSetOutputStream_Normal"); - h.publish(r); - assertNull(CallVerificationStack.getInstance().pop()); - assertSame(r, CallVerificationStack.getInstance().pop()); - assertTrue(CallVerificationStack.getInstance().empty()); - assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal", - this.errSubstituteStream.toString()); - - ByteArrayOutputStream aos2 = new ByteArrayOutputStream(); - h.setOutputStream(aos2); - - // assertEquals("close", DelegationParameterStack.getInstance() - // .getCurrentSourceMethod()); - // assertNull(DelegationParameterStack.getInstance().pop()); - // assertEquals("flush", DelegationParameterStack.getInstance() - // .getCurrentSourceMethod()); - // assertNull(DelegationParameterStack.getInstance().pop()); - // assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal" - // + "MockFormatter_Tail", this.errSubstituteStream.toString()); - // r = new LogRecord(Level.INFO, "testSetOutputStream_Normal2"); - // h.publish(r); - // assertSame(r, DelegationParameterStack.getInstance().pop()); - // assertTrue(DelegationParameterStack.getInstance().empty()); - // assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal2", - // aos2 - // .toString()); - // assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal" - // + "MockFormatter_Tail", this.errSubstituteStream.toString()); - } - - /* - * A mock filter, always return false. - */ - public static class MockFilter implements Filter { - - public boolean isLoggable(LogRecord record) { - CallVerificationStack.getInstance().push(record); - // System.out.println("filter called..."); - return false; - } - } - - /* - * A mock formatter. - */ - public static class MockFormatter extends Formatter { - public String format(LogRecord r) { - // System.out.println("formatter called..."); - return super.formatMessage(r); - } - - /* - * (non-Javadoc) - * - * @see java.util.logging.Formatter#getHead(java.util.logging.Handler) - */ - public String getHead(Handler h) { - return "MockFormatter_Head"; - } - - /* - * (non-Javadoc) - * - * @see java.util.logging.Formatter#getTail(java.util.logging.Handler) - */ - public String getTail(Handler h) { - return "MockFormatter_Tail"; - } - } - - /* - * A mock output stream. - */ - public static class MockOutputStream extends ByteArrayOutputStream { - - /* - * (non-Javadoc) - * - * @see java.io.OutputStream#close() - */ - public void close() throws IOException { - CallVerificationStack.getInstance().push(null); - super.close(); - } - - /* - * (non-Javadoc) - * - * @see java.io.OutputStream#flush() - */ - public void flush() throws IOException { - CallVerificationStack.getInstance().push(null); - super.flush(); - } - - /* - * (non-Javadoc) - * - * @see java.io.OutputStream#write(int) - */ - public void write(int oneByte) { - // TODO Auto-generated method stub - super.write(oneByte); - } - } - - /* - * Used to grant all permissions except logging control. - */ - public static class MockSecurityManager extends SecurityManager { - - public MockSecurityManager() { - } - - public void checkPermission(Permission perm) { - // grant all permissions except logging control - if (perm instanceof LoggingPermission) { - throw new SecurityException(); - } - } - - public void checkPermission(Permission perm, Object context) { - // grant all permissions except logging control - if (perm instanceof LoggingPermission) { - throw new SecurityException(); - } - } - } - - /* - * A mock stream handler, expose setOutputStream. - */ - public static class MockStreamHandler extends ConsoleHandler { - public MockStreamHandler() { - super(); - } - - public void setOutputStream(OutputStream out) { - super.setOutputStream(out); - } - - public boolean isLoggable(LogRecord r) { - CallVerificationStack.getInstance().push(r); - return super.isLoggable(r); - } - } + private final static String INVALID_LEVEL = "impossible_level"; + + private final PrintStream err = System.err; + + private OutputStream errSubstituteStream = null; + + private static String className = ConsoleHandlerTest.class.getName(); + + /* + * @see TestCase#setUp() + */ + protected void setUp() throws Exception { + super.setUp(); + errSubstituteStream = new MockOutputStream(); + System.setErr(new PrintStream(errSubstituteStream)); + LogManager.getLogManager().reset(); + } + + /* + * @see TestCase#tearDown() + */ + protected void tearDown() throws Exception { + super.tearDown(); + LogManager.getLogManager().reset(); + CallVerificationStack.getInstance().clear(); + System.setErr(err); + } + + /* + * Test the constructor with no relevant log manager properties are set. + */ + public void testConstructor_NoProperties() { + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.ConsoleHandler.level")); + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.ConsoleHandler.filter")); + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.ConsoleHandler.formatter")); + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.ConsoleHandler.encoding")); + + ConsoleHandler h = new ConsoleHandler(); + assertSame(h.getLevel(), Level.INFO); + assertTrue(h.getFormatter() instanceof SimpleFormatter); + assertNull(h.getFilter()); + assertSame(h.getEncoding(), null); + } + + /* + * Test the constructor with insufficient privilege. + */ + public void testConstructor_InsufficientPrivilege() { + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.ConsoleHandler.level")); + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.ConsoleHandler.filter")); + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.ConsoleHandler.formatter")); + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.ConsoleHandler.encoding")); + + SecurityManager oldMan = System.getSecurityManager(); + System.setSecurityManager(new MockSecurityManager()); + // set a normal value + try { + ConsoleHandler h = new ConsoleHandler(); + assertSame(h.getLevel(), Level.INFO); + assertTrue(h.getFormatter() instanceof SimpleFormatter); + assertNull(h.getFilter()); + assertSame(h.getEncoding(), null); + } finally { + System.setSecurityManager(oldMan); + } + } + + /* + * Test the constructor with valid relevant log manager properties are set. + */ + public void testConstructor_ValidProperties() throws Exception { + Properties p = new Properties(); + p.put("java.util.logging.ConsoleHandler.level", "FINE"); + p.put("java.util.logging.ConsoleHandler.filter", className + + "$MockFilter"); + p.put("java.util.logging.ConsoleHandler.formatter", className + + "$MockFormatter"); + p.put("java.util.logging.ConsoleHandler.encoding", "iso-8859-1"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + + assertEquals(LogManager.getLogManager().getProperty( + "java.util.logging.ConsoleHandler.level"), "FINE"); + assertEquals(LogManager.getLogManager().getProperty( + "java.util.logging.ConsoleHandler.encoding"), "iso-8859-1"); + ConsoleHandler h = new ConsoleHandler(); + assertSame(h.getLevel(), Level.parse("FINE")); + assertTrue(h.getFormatter() instanceof MockFormatter); + assertTrue(h.getFilter() instanceof MockFilter); + assertEquals(h.getEncoding(), "iso-8859-1"); + } + + /* + * Test the constructor with invalid relevant log manager properties are + * set. + */ + public void testConstructor_InvalidProperties() throws Exception { + Properties p = new Properties(); + p.put("java.util.logging.ConsoleHandler.level", INVALID_LEVEL); + p.put("java.util.logging.ConsoleHandler.filter", className); + p.put("java.util.logging.ConsoleHandler.formatter", className); + p.put("java.util.logging.ConsoleHandler.encoding", "XXXX"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + + assertEquals(LogManager.getLogManager().getProperty( + "java.util.logging.ConsoleHandler.level"), INVALID_LEVEL); + assertEquals(LogManager.getLogManager().getProperty( + "java.util.logging.ConsoleHandler.encoding"), "XXXX"); + ConsoleHandler h = new ConsoleHandler(); + assertSame(h.getLevel(), Level.INFO); + assertTrue(h.getFormatter() instanceof SimpleFormatter); + assertNull(h.getFilter()); + assertNull(h.getEncoding()); + h.publish(new LogRecord(Level.SEVERE, "test")); + assertNull(h.getEncoding()); + } + + /* + * Test close() when having sufficient privilege, and a record has been + * written to the output stream. + */ + public void testClose_SufficientPrivilege_NormalClose() throws Exception { + Properties p = new Properties(); + p.put("java.util.logging.ConsoleHandler.formatter", className + + "$MockFormatter"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + ConsoleHandler h = new ConsoleHandler(); + h.publish(new LogRecord(Level.SEVERE, + "testClose_SufficientPrivilege_NormalClose msg")); + h.close(); + assertEquals("flush", CallVerificationStack.getInstance() + .getCurrentSourceMethod()); + assertNull(CallVerificationStack.getInstance().pop()); + h.close(); + } + + /* + * Test close() when having sufficient privilege, and an output stream that + * always throws exceptions. + */ + public void testClose_SufficientPrivilege_Exception() throws Exception { + Properties p = new Properties(); + p.put("java.util.logging.ConsoleHandler.formatter", className + + "$MockFormatter"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + ConsoleHandler h = new ConsoleHandler(); + + h.publish(new LogRecord(Level.SEVERE, + "testClose_SufficientPrivilege_Exception msg")); + h.flush(); + h.close(); + } + + /* + * Test close() when having sufficient privilege, and no record has been + * written to the output stream. + */ + public void testClose_SufficientPrivilege_DirectClose() throws Exception { + Properties p = new Properties(); + p.put("java.util.logging.ConsoleHandler.formatter", className + + "$MockFormatter"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + ConsoleHandler h = new ConsoleHandler(); + + h.close(); + assertEquals("flush", CallVerificationStack.getInstance() + .getCurrentSourceMethod()); + assertNull(CallVerificationStack.getInstance().pop()); + assertTrue(CallVerificationStack.getInstance().empty()); + } + + /* + * Test close() when having insufficient privilege. + */ + public void testClose_InsufficientPrivilege() throws Exception { + Properties p = new Properties(); + p.put("java.util.logging.ConsoleHandler.formatter", className + + "$MockFormatter"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + ConsoleHandler h = new ConsoleHandler(); + SecurityManager oldMan = System.getSecurityManager(); + System.setSecurityManager(new MockSecurityManager()); + try { + h.close(); + } finally { + System.setSecurityManager(oldMan); + } + } + + /* + * Test publish(), use no filter, having output stream, normal log record. + */ + public void testPublish_NoFilter() throws Exception { + Properties p = new Properties(); + p.put("java.util.logging.ConsoleHandler.formatter", className + + "$MockFormatter"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + ConsoleHandler h = new ConsoleHandler(); + + LogRecord r = new LogRecord(Level.INFO, "testPublish_NoFilter"); + h.setLevel(Level.INFO); + h.publish(r); + h.flush(); + assertEquals("MockFormatter_Head" + "testPublish_NoFilter", + this.errSubstituteStream.toString()); + + h.setLevel(Level.WARNING); + h.publish(r); + h.flush(); + assertEquals("MockFormatter_Head" + "testPublish_NoFilter", + this.errSubstituteStream.toString()); + + h.setLevel(Level.CONFIG); + h.publish(r); + h.flush(); + assertEquals("MockFormatter_Head" + "testPublish_NoFilter" + + "testPublish_NoFilter", this.errSubstituteStream.toString()); + + r.setLevel(Level.OFF); + h.setLevel(Level.OFF); + h.publish(r); + h.flush(); + assertEquals("MockFormatter_Head" + "testPublish_NoFilter" + + "testPublish_NoFilter", this.errSubstituteStream.toString()); + } + + /* + * Test publish(), after system err is reset. + */ + public void testPublish_AfterResetSystemErr() throws Exception { + Properties p = new Properties(); + p.put("java.util.logging.ConsoleHandler.formatter", className + + "$MockFormatter"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + ConsoleHandler h = new ConsoleHandler(); + h.setFilter(new MockFilter()); + + System.setErr(new PrintStream(new ByteArrayOutputStream())); + + LogRecord r = new LogRecord(Level.INFO, "testPublish_WithFilter"); + h.setLevel(Level.INFO); + h.publish(r); + assertNull(CallVerificationStack.getInstance().pop()); + assertSame(r, CallVerificationStack.getInstance().pop()); + assertEquals("", this.errSubstituteStream.toString()); + } + + /* + * Test publish(), use a filter, having output stream, normal log record. + */ + public void testPublish_WithFilter() throws Exception { + Properties p = new Properties(); + p.put("java.util.logging.ConsoleHandler.formatter", className + + "$MockFormatter"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + ConsoleHandler h = new ConsoleHandler(); + h.setFilter(new MockFilter()); + + LogRecord r = new LogRecord(Level.INFO, "testPublish_WithFilter"); + h.setLevel(Level.INFO); + h.publish(r); + assertNull(CallVerificationStack.getInstance().pop()); + assertSame(r, CallVerificationStack.getInstance().pop()); + assertEquals("", this.errSubstituteStream.toString()); + + h.setLevel(Level.WARNING); + h.publish(r); + assertNull(CallVerificationStack.getInstance().pop()); + assertTrue(CallVerificationStack.getInstance().empty()); + assertEquals("", this.errSubstituteStream.toString()); + + h.setLevel(Level.CONFIG); + h.publish(r); + assertNull(CallVerificationStack.getInstance().pop()); + assertSame(r, CallVerificationStack.getInstance().pop()); + assertEquals("", this.errSubstituteStream.toString()); + + r.setLevel(Level.OFF); + h.setLevel(Level.OFF); + h.publish(r); + assertNull(CallVerificationStack.getInstance().pop()); + assertEquals("", this.errSubstituteStream.toString()); + assertTrue(CallVerificationStack.getInstance().empty()); + } + + /* + * Test publish(), null log record, having output stream, spec said + * rather than throw exception, handler should call errormanager to handle + * exception case, so NullPointerException shouldn't be thrown. + */ + public void testPublish_Null() throws Exception { + Properties p = new Properties(); + p.put("java.util.logging.ConsoleHandler.formatter", className + + "$MockFormatter"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + ConsoleHandler h = new ConsoleHandler(); + h.publish(null); + } + + /* + * Test publish(), a log record with empty msg, having output stream + */ + public void testPublish_EmptyMsg() throws Exception { + Properties p = new Properties(); + p.put("java.util.logging.ConsoleHandler.formatter", className + + "$MockFormatter"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + ConsoleHandler h = new ConsoleHandler(); + LogRecord r = new LogRecord(Level.INFO, ""); + h.publish(r); + h.flush(); + assertEquals("MockFormatter_Head", this.errSubstituteStream.toString()); + } + + /* + * Test publish(), a log record with null msg, having output stream + */ + public void testPublish_NullMsg() throws Exception { + Properties p = new Properties(); + p.put("java.util.logging.ConsoleHandler.formatter", className + + "$MockFormatter"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + ConsoleHandler h = new ConsoleHandler(); + LogRecord r = new LogRecord(Level.INFO, null); + h.publish(r); + h.flush(); + // assertEquals("MockFormatter_Head", + // this.errSubstituteStream.toString()); + } + + public void testPublish_AfterClose() throws Exception { + PrintStream backup = System.err; + try { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + System.setErr(new PrintStream(bos)); + Properties p = new Properties(); + p.put("java.util.logging.ConsoleHandler.level", "FINE"); + p.put("java.util.logging.ConsoleHandler.formatter", className + + "$MockFormatter"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + ConsoleHandler h = new ConsoleHandler(); + assertSame(h.getLevel(), Level.FINE); + LogRecord r1 = new LogRecord(Level.INFO, "testPublish_Record1"); + LogRecord r2 = new LogRecord(Level.INFO, "testPublish_Record2"); + assertTrue(h.isLoggable(r1)); + h.publish(r1); + assertTrue(bos.toString().indexOf("testPublish_Record1") >= 0); + h.close(); + // assertFalse(h.isLoggable(r)); + assertTrue(h.isLoggable(r2)); + h.publish(r2); + assertTrue(bos.toString().indexOf("testPublish_Record2") >= 0); + h.flush(); + // assertEquals("MockFormatter_Head", + // this.errSubstituteStream.toString()); + } catch (IOException e) { + e.printStackTrace(); + } finally { + System.setErr(backup); + } + } + + /* + * Test setOutputStream() under normal condition. + */ + public void testSetOutputStream_Normal() { + MockStreamHandler h = new MockStreamHandler(); + h.setFormatter(new MockFormatter()); + + LogRecord r = new LogRecord(Level.INFO, "testSetOutputStream_Normal"); + h.publish(r); + assertNull(CallVerificationStack.getInstance().pop()); + assertSame(r, CallVerificationStack.getInstance().pop()); + assertTrue(CallVerificationStack.getInstance().empty()); + assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal", + this.errSubstituteStream.toString()); + + ByteArrayOutputStream aos2 = new ByteArrayOutputStream(); + h.setOutputStream(aos2); + + // assertEquals("close", DelegationParameterStack.getInstance() + // .getCurrentSourceMethod()); + // assertNull(DelegationParameterStack.getInstance().pop()); + // assertEquals("flush", DelegationParameterStack.getInstance() + // .getCurrentSourceMethod()); + // assertNull(DelegationParameterStack.getInstance().pop()); + // assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal" + // + "MockFormatter_Tail", this.errSubstituteStream.toString()); + // r = new LogRecord(Level.INFO, "testSetOutputStream_Normal2"); + // h.publish(r); + // assertSame(r, DelegationParameterStack.getInstance().pop()); + // assertTrue(DelegationParameterStack.getInstance().empty()); + // assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal2", + // aos2 + // .toString()); + // assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal" + // + "MockFormatter_Tail", this.errSubstituteStream.toString()); + } + + /* + * A mock filter, always return false. + */ + public static class MockFilter implements Filter { + + public boolean isLoggable(LogRecord record) { + CallVerificationStack.getInstance().push(record); + // System.out.println("filter called..."); + return false; + } + } + + /* + * A mock formatter. + */ + public static class MockFormatter extends Formatter { + public String format(LogRecord r) { + // System.out.println("formatter called..."); + return super.formatMessage(r); + } + + /* + * (non-Javadoc) + * + * @see java.util.logging.Formatter#getHead(java.util.logging.Handler) + */ + public String getHead(Handler h) { + return "MockFormatter_Head"; + } + + /* + * (non-Javadoc) + * + * @see java.util.logging.Formatter#getTail(java.util.logging.Handler) + */ + public String getTail(Handler h) { + return "MockFormatter_Tail"; + } + } + + /* + * A mock output stream. + */ + public static class MockOutputStream extends ByteArrayOutputStream { + + /* + * (non-Javadoc) + * + * @see java.io.OutputStream#close() + */ + public void close() throws IOException { + CallVerificationStack.getInstance().push(null); + super.close(); + } + + /* + * (non-Javadoc) + * + * @see java.io.OutputStream#flush() + */ + public void flush() throws IOException { + CallVerificationStack.getInstance().push(null); + super.flush(); + } + + /* + * (non-Javadoc) + * + * @see java.io.OutputStream#write(int) + */ + public void write(int oneByte) { + // TODO Auto-generated method stub + super.write(oneByte); + } + } + + /* + * Used to grant all permissions except logging control. + */ + public static class MockSecurityManager extends SecurityManager { + + public MockSecurityManager() { + } + + public void checkPermission(Permission perm) { + // grant all permissions except logging control + if (perm instanceof LoggingPermission) { + throw new SecurityException(); + } + } + + public void checkPermission(Permission perm, Object context) { + // grant all permissions except logging control + if (perm instanceof LoggingPermission) { + throw new SecurityException(); + } + } + } + + /* + * A mock stream handler, expose setOutputStream. + */ + public static class MockStreamHandler extends ConsoleHandler { + public MockStreamHandler() { + super(); + } + + public void setOutputStream(OutputStream out) { + super.setOutputStream(out); + } + + public boolean isLoggable(LogRecord r) { + CallVerificationStack.getInstance().push(r); + return super.isLoggable(r); + } + } } diff --git a/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/FilterTest.java b/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/FilterTest.java index d7ce843..1d105e0 100644 --- a/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/FilterTest.java +++ b/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/FilterTest.java @@ -15,26 +15,8 @@ * limitations under the License. */ -//TODO : -/* - * 1. Don't forget to write tests for org.apache.harmony.logging.internal.nls/Messages.java this file is in logging/src/main/java folder - * 2. inrteface filter / LoggingMXBean tests machen - * 3. XMLFormatter.java should be finish for Monday (not a lot to do) but as I beginn want to finish. - * 3. In my case - * I didn't use the PARTIAL_OK, so I believe that 98% of COMPLETE are PARTIAL_OK - * COMPLETE = Tests finish and should be working. If error check the test before to make a ticket. - * PARTIAL = Tests finish, but need special reviewing - * TODO = A test to do (or not). Mostly a test to complete - * 4. For questions christian.wiederseiner - */ - package org.apache.harmony.logging.tests.java.util.logging; -import dalvik.annotation.TestTargetClass; -import dalvik.annotation.TestTargets; -import dalvik.annotation.TestLevel; -import dalvik.annotation.TestTargetNew; - import java.util.logging.Filter; import java.util.logging.LogRecord; @@ -44,26 +26,19 @@ import junit.framework.TestCase; * This testcase verifies the signature of the interface Filter. * */ -@TestTargetClass(Filter.class) public class FilterTest extends TestCase { - @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "Verifies interface.", - method = "isLoggable", - args = {java.util.logging.LogRecord.class} - ) - public void testFilter() { - MockFilter f = new MockFilter(); - assertFalse(f.isLoggable(null)); - } - - /* - * This inner class implements the interface Filter to verify the signature. - */ - private class MockFilter implements Filter { - - public boolean isLoggable(LogRecord record) { - return false; - } - } + public void testFilter() { + MockFilter f = new MockFilter(); + f.isLoggable(null); + } + + /* + * This inner class implements the interface Filter to verify the signature. + */ + private class MockFilter implements Filter { + + public boolean isLoggable(LogRecord record) { + return false; + } + } } diff --git a/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/HandlerTest.java b/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/HandlerTest.java index 5868b1f..2f13f2d 100644 --- a/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/HandlerTest.java +++ b/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/HandlerTest.java @@ -17,16 +17,10 @@ package org.apache.harmony.logging.tests.java.util.logging; -import dalvik.annotation.TestTargetClass; -import dalvik.annotation.TestTargets; -import dalvik.annotation.TestTargetNew; -import dalvik.annotation.TestLevel; - import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.security.Permission; -import java.util.EmptyStackException; import java.util.Properties; import java.util.logging.ErrorManager; import java.util.logging.Filter; @@ -46,730 +40,551 @@ import tests.util.CallVerificationStack; * Test suite for the class java.util.logging.Handler. * */ -@TestTargetClass(Handler.class) public class HandlerTest extends TestCase { - private static String className = HandlerTest.class.getName(); - - /* - * @see TestCase#tearDown() - */ - protected void tearDown() throws Exception { - super.tearDown(); + private static String className = HandlerTest.class.getName(); + + /* + * @see TestCase#setUp() + */ + protected void setUp() throws Exception { + super.setUp(); + } + + /* + * @see TestCase#tearDown() + */ + protected void tearDown() throws Exception { + super.tearDown(); CallVerificationStack.getInstance().clear(); - } - - /* - * Test the constructor. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "", - method = "Handler", - args = {} - ) - public void testConstructor() { - MockHandler h = new MockHandler(); - assertSame(h.getLevel(), Level.ALL); - assertNull(h.getFormatter()); - assertNull(h.getFilter()); - assertNull(h.getEncoding()); - assertTrue(h.getErrorManager() instanceof ErrorManager); - } - - /* - * Test the constructor, with properties set - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "", - method = "Handler", - args = {} - ) - public void testConstructor_Properties() throws Exception { - Properties p = new Properties(); - p.put("java.util.logging.MockHandler.level", "FINE"); - p.put("java.util.logging.MockHandler.filter", className - + "$MockFilter"); - p.put("java.util.logging.Handler.formatter", className - + "$MockFormatter"); - p.put("java.util.logging.MockHandler.encoding", "utf-8"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - - assertEquals(LogManager.getLogManager().getProperty( - "java.util.logging.MockHandler.level"), "FINE"); - assertEquals(LogManager.getLogManager().getProperty( - "java.util.logging.MockHandler.encoding"), "utf-8"); - MockHandler h = new MockHandler(); - assertSame(h.getLevel(), Level.ALL); - assertNull(h.getFormatter()); - assertNull(h.getFilter()); - assertNull(h.getEncoding()); - assertTrue(h.getErrorManager() instanceof ErrorManager); - LogManager.getLogManager().reset(); - } - - /* - * Test getEncoding & setEncoding methods with supported encoding. - */ - @TestTargets({ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Doesn't verify exceptions.", - method = "getEncoding", - args = {} - ), - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Doesn't verify exceptions.", - method = "setEncoding", - args = {java.lang.String.class} - ) - }) - public void testGetSetEncoding_Normal() throws Exception { - MockHandler h = new MockHandler(); - h.setEncoding("iso-8859-1"); - assertEquals("iso-8859-1", h.getEncoding()); - } - - /* - * Test getEncoding & setEncoding methods with null. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies null as a parameter.", - method = "getEncoding", - args = {} - ) - public void testGetSetEncoding_Null() throws Exception { - MockHandler h = new MockHandler(); - h.setEncoding(null); - assertNull(h.getEncoding()); - } - - /* - * Test getEncoding & setEncoding methods with unsupported encoding. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies UnsupportedEncodingException.", - method = "setEncoding", - args = {java.lang.String.class} - ) - public void testGetSetEncoding_Unsupported() { - MockHandler h = new MockHandler(); - try { - h.setEncoding("impossible"); - fail("Should throw UnsupportedEncodingException!"); - } catch (UnsupportedEncodingException e) { - } - assertNull(h.getEncoding()); - } - - /* - * Test setEncoding with insufficient privilege. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Doesn't verify UnsupportedEncodingException.", - method = "setEncoding", - args = {java.lang.String.class} - ) - public void testSetEncoding_InsufficientPrivilege() throws Exception { - MockHandler h = new MockHandler(); - SecurityManager oldMan = System.getSecurityManager(); - System.setSecurityManager(new MockSecurityManager()); - // set a normal value - try { - h.setEncoding("iso-8859-1"); - fail("Should throw SecurityException!"); - } catch (SecurityException e) { - } finally { - System.setSecurityManager(oldMan); - } - assertNull(h.getEncoding()); - System.setSecurityManager(new MockSecurityManager()); - // set an invalid value - try { - - h.setEncoding("impossible"); - fail("Should throw SecurityException!"); - } catch (SecurityException e) { - } finally { - System.setSecurityManager(oldMan); - } - assertNull(h.getEncoding()); - } - - /* - * Test getErrorManager & setErrorManager methods with non-null value. - */ - @TestTargets({ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "", - method = "setErrorManager", - args = {java.util.logging.ErrorManager.class} - ), - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "", - method = "getErrorManager", - args = {} - ) - }) - public void testGetSetErrorManager_Normal() throws Exception { - MockHandler h = new MockHandler(); - ErrorManager man = new ErrorManager(); - h.setErrorManager(man); - assertSame(man, h.getErrorManager()); - } - - /* - * Test getErrorManager & setErrorManager methods with null. - */ - @TestTargets({ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies null as a parameter.", - method = "getErrorManager", - args = {} - ), - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies null as a parameter.", - method = "setErrorManager", - args = {java.util.logging.ErrorManager.class} - ) - }) - public void testGetSetErrorManager_Null() throws Exception { - MockHandler h = new MockHandler(); - // test set null - try { - h.setErrorManager(null); - fail("Should throw NullPointerException!"); - } catch (NullPointerException e) { - } - - // test reset null - try { - h.setErrorManager(new ErrorManager()); - h.setErrorManager(null); - fail("Should throw NullPointerException!"); - } catch (NullPointerException e) { - } - } - - /* - * Test getErrorManager with insufficient privilege. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies SecurityException.", - method = "getErrorManager", - args = {} - ) - public void testGetErrorManager_InsufficientPrivilege() throws Exception { - MockHandler h = new MockHandler(); - SecurityManager oldMan = System.getSecurityManager(); - System.setSecurityManager(new MockSecurityManager()); - - try { - h.getErrorManager(); - fail("Should throw SecurityException!"); - } catch (SecurityException e) { - } finally { - System.setSecurityManager(oldMan); - } - } - - /* - * Test setErrorManager with insufficient privilege. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies setErrorManager with insufficient privilege.", - method = "setErrorManager", - args = {java.util.logging.ErrorManager.class} - ) - public void testSetErrorManager_InsufficientPrivilege() throws Exception { - MockHandler h = new MockHandler(); - SecurityManager oldMan = System.getSecurityManager(); - System.setSecurityManager(new MockSecurityManager()); - - // set null - try { - - h.setErrorManager(null); - fail("Should throw SecurityException!"); - } catch (SecurityException e) { - } finally { - System.setSecurityManager(oldMan); - } - // set a normal value - System.setSecurityManager(new MockSecurityManager()); - try { - - h.setErrorManager(new ErrorManager()); - fail("Should throw SecurityException!"); - } catch (SecurityException e) { - } finally { - System.setSecurityManager(oldMan); - } - } - - /* - * Test getFilter & setFilter methods with non-null value. - */ - @TestTargets({ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Doesn't verify SecurityException.", - method = "setFilter", - args = {java.util.logging.Filter.class} - ), - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Doesn't verify SecurityException.", - method = "getFilter", - args = {} - ) - }) - public void testGetSetFilter_Normal() throws Exception { - MockHandler h = new MockHandler(); - Filter f = new MockFilter(); - h.setFilter(f); - assertSame(f, h.getFilter()); - } - - /* - * Test getFilter & setFilter methods with null. - */ - @TestTargets({ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies null as a parameter.", - method = "getFilter", - args = {} - ), - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies null as a parameter.", - method = "setFilter", - args = {java.util.logging.Filter.class} - ) - }) - public void testGetSetFilter_Null() throws Exception { - MockHandler h = new MockHandler(); - // test set null - h.setFilter(null); - - // test reset null - h.setFilter(new MockFilter()); - h.setFilter(null); - } - - /* - * Test setFilter with insufficient privilege. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies SecurityException.", - method = "setFilter", - args = {java.util.logging.Filter.class} - ) - public void testSetFilter_InsufficientPrivilege() throws Exception { - MockHandler h = new MockHandler(); - SecurityManager oldMan = System.getSecurityManager(); - System.setSecurityManager(new MockSecurityManager()); - - // set null - try { - - h.setFilter(null); - fail("Should throw SecurityException!"); - } catch (SecurityException e) { - } finally { - System.setSecurityManager(oldMan); - } - // set a normal value - System.setSecurityManager(new MockSecurityManager()); - try { - - h.setFilter(new MockFilter()); - fail("Should throw SecurityException!"); - } catch (SecurityException e) { - } finally { - System.setSecurityManager(oldMan); - } - } - - /* - * Test getFormatter & setFormatter methods with non-null value. - */ - @TestTargets({ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Doesn't verify SecurityException.", - method = "getFormatter", - args = {} - ), - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Doesn't verify SecurityException.", - method = "setFormatter", - args = {java.util.logging.Formatter.class} - ) - }) - public void testGetSetFormatter_Normal() throws Exception { - MockHandler h = new MockHandler(); - Formatter f = new SimpleFormatter(); - h.setFormatter(f); - assertSame(f, h.getFormatter()); - } - - /* - * Test getFormatter & setFormatter methods with null. - */ - @TestTargets({ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies null as a parameter.", - method = "getFormatter", - args = {} - ), - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies null as a parameter.", - method = "setFormatter", - args = {java.util.logging.Formatter.class} - ) - }) - public void testGetSetFormatter_Null() throws Exception { - MockHandler h = new MockHandler(); - // test set null - try { - h.setFormatter(null); - fail("Should throw NullPointerException!"); - } catch (NullPointerException e) { - } - - // test reset null - try { - h.setFormatter(new SimpleFormatter()); - h.setFormatter(null); - fail("Should throw NullPointerException!"); - } catch (NullPointerException e) { - } - } - - /* - * Test setFormatter with insufficient privilege. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies SecurityException.", - method = "getFormatter", - args = {} - ) - public void testSetFormatter_InsufficientPrivilege() throws Exception { - MockHandler h = new MockHandler(); - SecurityManager oldMan = System.getSecurityManager(); - System.setSecurityManager(new MockSecurityManager()); - - // set null - try { - - h.setFormatter(null); - fail("Should throw SecurityException!"); - } catch (SecurityException e) { - } finally { - System.setSecurityManager(oldMan); - } - // set a normal value - System.setSecurityManager(new MockSecurityManager()); - try { - - h.setFormatter(new SimpleFormatter()); - fail("Should throw SecurityException!"); - } catch (SecurityException e) { - } finally { - System.setSecurityManager(oldMan); - } - } - - /* - * Test getLevel & setLevel methods with non-null value. - */ - @TestTargets({ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Doesn't verify SecurityException.", - method = "getLevel", - args = {} - ), - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Doesn't verify SecurityException.", - method = "setLevel", - args = {java.util.logging.Level.class} - ) - }) - public void testGetSetLevel_Normal() throws Exception { - MockHandler h = new MockHandler(); - Level f = Level.CONFIG; - h.setLevel(f); - assertSame(f, h.getLevel()); - } - - /* - * Test getLevel & setLevel methods with null. - */ - @TestTargets({ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies getLevel & setLevel methods with null.", - method = "getLevel", - args = {} - ), - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies getLevel & setLevel methods with null.", - method = "setLevel", - args = {java.util.logging.Level.class} - ) - }) - public void testGetSetLevel_Null() throws Exception { - MockHandler h = new MockHandler(); - // test set null - try { - h.setLevel(null); - fail("Should throw NullPointerException!"); - } catch (NullPointerException e) { - } - - // test reset null - try { - h.setLevel(Level.CONFIG); - h.setLevel(null); - fail("Should throw NullPointerException!"); - } catch (NullPointerException e) { - } - } - - /* - * Test setLevel with insufficient privilege. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies NullPointerException, SecurityException.", - method = "setLevel", - args = {java.util.logging.Level.class} - ) - public void testSetLevel_InsufficientPrivilege() throws Exception { - MockHandler h = new MockHandler(); - SecurityManager oldMan = System.getSecurityManager(); - System.setSecurityManager(new MockSecurityManager()); - - // set null - try { - - h.setLevel(null); - fail("Should throw NullPointerException!"); - } catch (NullPointerException e) { - } finally { - System.setSecurityManager(oldMan); - } - // set a normal value - System.setSecurityManager(new MockSecurityManager()); - try { - - h.setLevel(Level.CONFIG); - fail("Should throw SecurityException!"); - } catch (SecurityException e) { - } finally { - System.setSecurityManager(oldMan); - } - } - - /* - * Use no filter - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "", - method = "isLoggable", - args = {java.util.logging.LogRecord.class} - ) - public void testIsLoggable_NoFilter() { - MockHandler h = new MockHandler(); - LogRecord r = new LogRecord(Level.CONFIG, null); - assertTrue(h.isLoggable(r)); - - h.setLevel(Level.CONFIG); - assertTrue(h.isLoggable(r)); - - h.setLevel(Level.SEVERE); - assertFalse(h.isLoggable(r)); - - r.setLevel(Level.OFF); - h.setLevel(Level.OFF); - assertFalse(h.isLoggable(r)); - } - - /* - * Use a filter - */ - @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "Verifies isLoggable method with filter.", - method = "isLoggable", - args = {java.util.logging.LogRecord.class} - ) - public void testIsLoggable_WithFilter() { - MockHandler h = new MockHandler(); - LogRecord r = new LogRecord(Level.CONFIG, null); - LogRecord r1 = new LogRecord(Level.CONFIG, null); - LogRecord r2 = new LogRecord(Level.CONFIG, null); - - h.setFilter(new MockFilter()); - assertFalse(h.isLoggable(r)); - assertSame(r,CallVerificationStack.getInstance().pop()); - - h.setLevel(Level.CONFIG); - assertFalse(h.isLoggable(r1)); - assertSame(r1, CallVerificationStack.getInstance().pop()); - - h.setLevel(Level.SEVERE); - assertFalse(h.isLoggable(r2)); - - try{ - CallVerificationStack.getInstance().pop(); - }catch(EmptyStackException e){ - //normal - } - } - - /** - * @tests java.util.logging.Handler#isLoggable(LogRecord) - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies null as a parameter.", - method = "isLoggable", - args = {java.util.logging.LogRecord.class} - ) - public void testIsLoggable_Null() { - MockHandler h = new MockHandler(); - try { - h.isLoggable(null); - fail("should throw NullPointerException"); - } catch (NullPointerException e) { - // expected - } - } - - /* - * Test whether the error manager is actually called with expected - * parameters. - */ - @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "", - method = "reportError", - args = {java.lang.String.class, java.lang.Exception.class, int.class} - ) - public void testReportError() { - MockHandler h = new MockHandler(); - h.setErrorManager(new MockErrorManager()); - SecurityManager oldMan = System.getSecurityManager(); - System.setSecurityManager(new MockSecurityManager()); - - try { - Exception ex = new Exception("test exception"); - // with non-null parameters - h.reportError("test msg", ex, -1); - assertEquals(-1, CallVerificationStack.getInstance().popInt()); - assertSame(ex, CallVerificationStack.getInstance().pop()); - assertEquals("test msg", CallVerificationStack.getInstance().pop()); - // with null parameters - h.reportError(null, null, 0); - assertEquals(0, CallVerificationStack.getInstance().popInt()); - assertSame(null, CallVerificationStack.getInstance().pop()); - assertNull(CallVerificationStack.getInstance().pop()); - } catch (SecurityException e) { - fail("Should not throw SecurityException!"); - } finally { - System.setSecurityManager(oldMan); - } - } - - /* - * Used to enable the testing of Handler because Handler is an abstract - * class. - */ - public static class MockHandler extends Handler { - - public void close() { - } - - public void flush() { - } - - public void publish(LogRecord record) { - } - - public void reportError(String msg, Exception ex, int code) { - super.reportError(msg, ex, code); - } - } - - /* - * Used to grant all permissions except logging control. - */ - public static class MockSecurityManager extends SecurityManager { - - public MockSecurityManager() { - } - - public void checkPermission(Permission perm) { - // grant all permissions except logging control - if (perm instanceof LoggingPermission) { - throw new SecurityException(); - } - } - - public void checkPermission(Permission perm, Object context) { - // grant all permissions except logging control - if (perm instanceof LoggingPermission) { - throw new SecurityException(); - } - } - } - - /* - * A mock filter, always return false. - */ - public static class MockFilter implements Filter { - - public boolean isLoggable(LogRecord record) { - CallVerificationStack.getInstance().push(record); - return false; - } - } - - /* - * A mock error manager, used to validate the expected method is called with - * the expected parameters. - */ - public static class MockErrorManager extends ErrorManager { - - public void error(String msg, Exception ex, int errorCode) { - CallVerificationStack.getInstance().push(msg); - CallVerificationStack.getInstance().push(ex); - CallVerificationStack.getInstance().push(errorCode); - } - } + } + + /** + * Constructor for HandlerTest. + * + * @param arg0 + */ + public HandlerTest(String arg0) { + super(arg0); + } + + /* + * Test the constructor. + */ + public void testConstructor() { + MockHandler h = new MockHandler(); + assertSame(h.getLevel(), Level.ALL); + assertNull(h.getFormatter()); + assertNull(h.getFilter()); + assertNull(h.getEncoding()); + assertTrue(h.getErrorManager() instanceof ErrorManager); + } + + /* + * Test the constructor, with properties set + */ + public void testConstructor_Properties() throws Exception { + Properties p = new Properties(); + p.put("java.util.logging.MockHandler.level", "FINE"); + p + .put("java.util.logging.MockHandler.filter", className + + "$MockFilter"); + p.put("java.util.logging.Handler.formatter", className + + "$MockFormatter"); + p.put("java.util.logging.MockHandler.encoding", "utf-8"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + + assertEquals(LogManager.getLogManager().getProperty( + "java.util.logging.MockHandler.level"), "FINE"); + assertEquals(LogManager.getLogManager().getProperty( + "java.util.logging.MockHandler.encoding"), "utf-8"); + MockHandler h = new MockHandler(); + assertSame(h.getLevel(), Level.ALL); + assertNull(h.getFormatter()); + assertNull(h.getFilter()); + assertNull(h.getEncoding()); + assertTrue(h.getErrorManager() instanceof ErrorManager); + LogManager.getLogManager().reset(); + } + + /* + * Abstract method, no test needed. + */ + public void testClose() { + MockHandler h = new MockHandler(); + h.close(); + } + + /* + * Abstract method, no test needed. + */ + public void testFlush() { + MockHandler h = new MockHandler(); + h.flush(); + } + + /* + * Abstract method, no test needed. + */ + public void testPublish() { + MockHandler h = new MockHandler(); + h.publish(null); + } + + /* + * Test getEncoding & setEncoding methods with supported encoding. + */ + public void testGetSetEncoding_Normal() throws Exception { + MockHandler h = new MockHandler(); + h.setEncoding("iso-8859-1"); + assertEquals("iso-8859-1", h.getEncoding()); + } + + /* + * Test getEncoding & setEncoding methods with null. + */ + public void testGetSetEncoding_Null() throws Exception { + MockHandler h = new MockHandler(); + h.setEncoding(null); + assertNull(h.getEncoding()); + } + + /* + * Test getEncoding & setEncoding methods with unsupported encoding. + */ + public void testGetSetEncoding_Unsupported() { + MockHandler h = new MockHandler(); + try { + h.setEncoding("impossible"); + fail("Should throw UnsupportedEncodingException!"); + } catch (UnsupportedEncodingException e) { + } + assertNull(h.getEncoding()); + } + + /* + * Test setEncoding with insufficient privilege. + */ + public void testSetEncoding_InsufficientPrivilege() throws Exception { + MockHandler h = new MockHandler(); + SecurityManager oldMan = System.getSecurityManager(); + System.setSecurityManager(new MockSecurityManager()); + // set a normal value + try { + h.setEncoding("iso-8859-1"); + fail("Should throw SecurityException!"); + } catch (SecurityException e) { + } finally { + System.setSecurityManager(oldMan); + } + assertNull(h.getEncoding()); + System.setSecurityManager(new MockSecurityManager()); + // set an invalid value + try { + + h.setEncoding("impossible"); + fail("Should throw SecurityException!"); + } catch (SecurityException e) { + } finally { + System.setSecurityManager(oldMan); + } + assertNull(h.getEncoding()); + } + + /* + * Test getErrorManager & setErrorManager methods with non-null value. + */ + public void testGetSetErrorManager_Normal() throws Exception { + MockHandler h = new MockHandler(); + ErrorManager man = new ErrorManager(); + h.setErrorManager(man); + assertSame(man, h.getErrorManager()); + } + + /* + * Test getErrorManager & setErrorManager methods with null. + */ + public void testGetSetErrorManager_Null() throws Exception { + MockHandler h = new MockHandler(); + // test set null + try { + h.setErrorManager(null); + fail("Should throw NullPointerException!"); + } catch (NullPointerException e) { + } + + // test reset null + try { + h.setErrorManager(new ErrorManager()); + h.setErrorManager(null); + fail("Should throw NullPointerException!"); + } catch (NullPointerException e) { + } + } + + /* + * Test getErrorManager with insufficient privilege. + */ + public void testGetErrorManager_InsufficientPrivilege() throws Exception { + MockHandler h = new MockHandler(); + SecurityManager oldMan = System.getSecurityManager(); + System.setSecurityManager(new MockSecurityManager()); + + try { + h.getErrorManager(); + fail("Should throw SecurityException!"); + } catch (SecurityException e) { + } finally { + System.setSecurityManager(oldMan); + } + } + + /* + * Test setErrorManager with insufficient privilege. + */ + public void testSetErrorManager_InsufficientPrivilege() throws Exception { + MockHandler h = new MockHandler(); + SecurityManager oldMan = System.getSecurityManager(); + System.setSecurityManager(new MockSecurityManager()); + + // set null + try { + + h.setErrorManager(null); + fail("Should throw SecurityException!"); + } catch (SecurityException e) { + } finally { + System.setSecurityManager(oldMan); + } + // set a normal value + System.setSecurityManager(new MockSecurityManager()); + try { + + h.setErrorManager(new ErrorManager()); + fail("Should throw SecurityException!"); + } catch (SecurityException e) { + } finally { + System.setSecurityManager(oldMan); + } + } + + /* + * Test getFilter & setFilter methods with non-null value. + */ + public void testGetSetFilter_Normal() throws Exception { + MockHandler h = new MockHandler(); + Filter f = new MockFilter(); + h.setFilter(f); + assertSame(f, h.getFilter()); + } + + /* + * Test getFilter & setFilter methods with null. + */ + public void testGetSetFilter_Null() throws Exception { + MockHandler h = new MockHandler(); + // test set null + h.setFilter(null); + + // test reset null + h.setFilter(new MockFilter()); + h.setFilter(null); + } + + /* + * Test setFilter with insufficient privilege. + */ + public void testSetFilter_InsufficientPrivilege() throws Exception { + MockHandler h = new MockHandler(); + SecurityManager oldMan = System.getSecurityManager(); + System.setSecurityManager(new MockSecurityManager()); + + // set null + try { + + h.setFilter(null); + fail("Should throw SecurityException!"); + } catch (SecurityException e) { + } finally { + System.setSecurityManager(oldMan); + } + // set a normal value + System.setSecurityManager(new MockSecurityManager()); + try { + + h.setFilter(new MockFilter()); + fail("Should throw SecurityException!"); + } catch (SecurityException e) { + } finally { + System.setSecurityManager(oldMan); + } + } + + /* + * Test getFormatter & setFormatter methods with non-null value. + */ + public void testGetSetFormatter_Normal() throws Exception { + MockHandler h = new MockHandler(); + Formatter f = new SimpleFormatter(); + h.setFormatter(f); + assertSame(f, h.getFormatter()); + } + + /* + * Test getFormatter & setFormatter methods with null. + */ + public void testGetSetFormatter_Null() throws Exception { + MockHandler h = new MockHandler(); + // test set null + try { + h.setFormatter(null); + fail("Should throw NullPointerException!"); + } catch (NullPointerException e) { + } + + // test reset null + try { + h.setFormatter(new SimpleFormatter()); + h.setFormatter(null); + fail("Should throw NullPointerException!"); + } catch (NullPointerException e) { + } + } + + /* + * Test setFormatter with insufficient privilege. + */ + public void testSetFormatter_InsufficientPrivilege() throws Exception { + MockHandler h = new MockHandler(); + SecurityManager oldMan = System.getSecurityManager(); + System.setSecurityManager(new MockSecurityManager()); + + // set null + try { + + h.setFormatter(null); + fail("Should throw SecurityException!"); + } catch (SecurityException e) { + } finally { + System.setSecurityManager(oldMan); + } + // set a normal value + System.setSecurityManager(new MockSecurityManager()); + try { + + h.setFormatter(new SimpleFormatter()); + fail("Should throw SecurityException!"); + } catch (SecurityException e) { + } finally { + System.setSecurityManager(oldMan); + } + } + + /* + * Test getLevel & setLevel methods with non-null value. + */ + public void testGetSetLevel_Normal() throws Exception { + MockHandler h = new MockHandler(); + Level f = Level.CONFIG; + h.setLevel(f); + assertSame(f, h.getLevel()); + } + + /* + * Test getLevel & setLevel methods with null. + */ + public void testGetSetLevel_Null() throws Exception { + MockHandler h = new MockHandler(); + // test set null + try { + h.setLevel(null); + fail("Should throw NullPointerException!"); + } catch (NullPointerException e) { + } + + // test reset null + try { + h.setLevel(Level.CONFIG); + h.setLevel(null); + fail("Should throw NullPointerException!"); + } catch (NullPointerException e) { + } + } + + /* + * Test setLevel with insufficient privilege. + */ + public void testSetLevel_InsufficientPrivilege() throws Exception { + MockHandler h = new MockHandler(); + SecurityManager oldMan = System.getSecurityManager(); + System.setSecurityManager(new MockSecurityManager()); + + // set null + try { + + h.setLevel(null); + fail("Should throw NullPointerException!"); + } catch (NullPointerException e) { + } finally { + System.setSecurityManager(oldMan); + } + // set a normal value + System.setSecurityManager(new MockSecurityManager()); + try { + + h.setLevel(Level.CONFIG); + fail("Should throw SecurityException!"); + } catch (SecurityException e) { + } finally { + System.setSecurityManager(oldMan); + } + } + + /* + * Use no filter + */ + public void testIsLoggable_NoFilter() { + MockHandler h = new MockHandler(); + LogRecord r = new LogRecord(Level.CONFIG, null); + assertTrue(h.isLoggable(r)); + + h.setLevel(Level.CONFIG); + assertTrue(h.isLoggable(r)); + + h.setLevel(Level.SEVERE); + assertFalse(h.isLoggable(r)); + + r.setLevel(Level.OFF); + h.setLevel(Level.OFF); + assertFalse(h.isLoggable(r)); + } + + /* + * Use a filter + */ + public void testIsLoggable_WithFilter() { + MockHandler h = new MockHandler(); + LogRecord r = new LogRecord(Level.CONFIG, null); + h.setFilter(new MockFilter()); + assertFalse(h.isLoggable(r)); + + h.setLevel(Level.CONFIG); + assertFalse(h.isLoggable(r)); + assertSame(r, CallVerificationStack.getInstance().pop()); + + h.setLevel(Level.SEVERE); + assertFalse(h.isLoggable(r)); + assertSame(r, CallVerificationStack.getInstance().pop()); + } + + /** + * @tests java.util.logging.Handler#isLoggable(LogRecord) + */ + public void testIsLoggable_Null() { + MockHandler h = new MockHandler(); + try { + h.isLoggable(null); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + } + + /* + * Test whether the error manager is actually called with expected + * parameters. + */ + public void testReportError() { + MockHandler h = new MockHandler(); + h.setErrorManager(new MockErrorManager()); + SecurityManager oldMan = System.getSecurityManager(); + System.setSecurityManager(new MockSecurityManager()); + + try { + Exception ex = new Exception("test exception"); + // with non-null parameters + h.reportError("test msg", ex, -1); + assertEquals(-1, CallVerificationStack.getInstance().popInt()); + assertSame(ex, CallVerificationStack.getInstance().pop()); + assertEquals("test msg", CallVerificationStack.getInstance().pop()); + // with null parameters + h.reportError(null, null, 0); + assertEquals(0, CallVerificationStack.getInstance().popInt()); + assertSame(null, CallVerificationStack.getInstance().pop()); + assertNull(CallVerificationStack.getInstance().pop()); + } catch (SecurityException e) { + fail("Should not throw SecurityException!"); + } finally { + System.setSecurityManager(oldMan); + } + } + + /* + * Used to enable the testing of Handler because Handler is an abstract + * class. + */ + public static class MockHandler extends Handler { + + public void close() { + } + + public void flush() { + } + + public void publish(LogRecord record) { + } + + public void reportError(String msg, Exception ex, int code) { + super.reportError(msg, ex, code); + } + } + + /* + * Used to grant all permissions except logging control. + */ + public static class MockSecurityManager extends SecurityManager { + + public MockSecurityManager() { + } + + public void checkPermission(Permission perm) { + // grant all permissions except logging control + if (perm instanceof LoggingPermission) { + throw new SecurityException(); + } + } + + public void checkPermission(Permission perm, Object context) { + // grant all permissions except logging control + if (perm instanceof LoggingPermission) { + throw new SecurityException(); + } + } + } + + /* + * A mock filter, always return false. + */ + public static class MockFilter implements Filter { + + public boolean isLoggable(LogRecord record) { + CallVerificationStack.getInstance().push(record); + return false; + } + } + + /* + * A mock error manager, used to validate the expected method is called with + * the expected parameters. + */ + public static class MockErrorManager extends ErrorManager { + + public void error(String msg, Exception ex, int errorCode) { + CallVerificationStack.getInstance().push(msg); + CallVerificationStack.getInstance().push(ex); + CallVerificationStack.getInstance().push(errorCode); + } + } public static class NullOutputStream extends OutputStream{ @Override diff --git a/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LevelTestResource.java b/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LevelTestResource.java index 9b41a9d..5e06b70 100644 --- a/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LevelTestResource.java +++ b/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LevelTestResource.java @@ -17,12 +17,8 @@ package org.apache.harmony.logging.tests.java.util.logging; -import dalvik.annotation.TestTargetClass; - import java.util.ListResourceBundle; -import java.util.logging.Level; -//@TestTargetClass = No test needed, just test class helper public class LevelTestResource extends ListResourceBundle { public Object[][] getContents() { return contents; diff --git a/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LoggingPermissionTest.java b/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LoggingPermissionTest.java index 9507bbd..aa8f7fa 100644 --- a/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LoggingPermissionTest.java +++ b/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LoggingPermissionTest.java @@ -17,29 +17,17 @@ package org.apache.harmony.logging.tests.java.util.logging; -import dalvik.annotation.TestTargets; -import dalvik.annotation.TestLevel; -import dalvik.annotation.TestTargetNew; -import dalvik.annotation.TestTargetClass; - import java.util.logging.LoggingPermission; import junit.framework.TestCase; import org.apache.harmony.testframework.serialization.SerializationTest; -@TestTargetClass(LoggingPermission.class) public class LoggingPermissionTest extends TestCase { /** * @tests serialization/deserialization compatibility. */ - @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "", - method = "!SerializationSelf", - args = {} - ) public void testSerializationSelf() throws Exception { SerializationTest.verifySelf(new LoggingPermission("control", "")); } @@ -47,55 +35,43 @@ public class LoggingPermissionTest extends TestCase { /** * @tests serialization/deserialization compatibility with RI. */ - @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "", - method = "!SerializationGolden", - args = {} - ) public void testSerializationCompatibility() throws Exception { SerializationTest.verifyGolden(this, new LoggingPermission("control", "")); } - @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "", - method = "LoggingPermission", - args = {java.lang.String.class, java.lang.String.class} - ) - public void testLoggingPermission() { - try { - new LoggingPermission(null, null); - fail("should throw IllegalArgumentException"); - } catch (NullPointerException e) { - } - try { - new LoggingPermission("", null); - fail("should throw IllegalArgumentException"); - } catch (IllegalArgumentException e) { - } - try { - new LoggingPermission("bad name", null); - fail("should throw IllegalArgumentException"); - } catch (IllegalArgumentException e) { - } - try { - new LoggingPermission("Control", null); - fail("should throw IllegalArgumentException"); - } catch (IllegalArgumentException e) { - } - try { - new LoggingPermission("control", - "bad action"); - fail("should throw IllegalArgumentException"); - } catch (IllegalArgumentException e) { - } - + public void testLoggingPermission() { + try { + new LoggingPermission(null, null); + fail("should throw IllegalArgumentException"); + } catch (NullPointerException e) { + } + try { + new LoggingPermission("", null); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + } + try { + new LoggingPermission("bad name", null); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + } + try { + new LoggingPermission("Control", null); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + } + try { + new LoggingPermission("control", + "bad action"); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + } + new LoggingPermission("control", ""); - + new LoggingPermission("control", null); - } + } } diff --git a/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/StreamHandlerTest.java b/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/StreamHandlerTest.java index 72270e5..76380e6 100644 --- a/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/StreamHandlerTest.java +++ b/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/StreamHandlerTest.java @@ -17,8 +17,6 @@ package org.apache.harmony.logging.tests.java.util.logging; -import dalvik.annotation.*; - import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; @@ -39,7 +37,6 @@ import java.util.logging.LogManager; import java.util.logging.LogRecord; import java.util.logging.LoggingPermission; import java.util.logging.SimpleFormatter; -import java.util.logging.SocketHandler; import java.util.logging.StreamHandler; import junit.framework.TestCase; @@ -51,1274 +48,1035 @@ import tests.util.CallVerificationStack; /** * Test the class StreamHandler. */ -@TestTargetClass(StreamHandler.class) public class StreamHandlerTest extends TestCase { - private final static String INVALID_LEVEL = "impossible_level"; + private final static String INVALID_LEVEL = "impossible_level"; private final PrintStream err = System.err; private OutputStream errSubstituteStream = null; - private static String className = StreamHandlerTest.class.getName(); + private static String className = StreamHandlerTest.class.getName(); - private static CharsetEncoder encoder; + private static CharsetEncoder encoder; - static { - encoder = Charset.forName("iso-8859-1").newEncoder(); - encoder.onMalformedInput(CodingErrorAction.REPLACE); - encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); - } + static { + encoder = Charset.forName("iso-8859-1").newEncoder(); + encoder.onMalformedInput(CodingErrorAction.REPLACE); + encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); + } - /* - * @see TestCase#setUp() - */ - protected void setUp() throws Exception { - super.setUp(); + /* + * @see TestCase#setUp() + */ + protected void setUp() throws Exception { + super.setUp(); errSubstituteStream = new NullOutputStream(); System.setErr(new PrintStream(errSubstituteStream)); - } - - /* - * @see TestCase#tearDown() - */ - protected void tearDown() throws Exception { - LogManager.getLogManager().reset(); - CallVerificationStack.getInstance().clear(); + } + + /* + * @see TestCase#tearDown() + */ + protected void tearDown() throws Exception { + LogManager.getLogManager().reset(); + CallVerificationStack.getInstance().clear(); System.setErr(err); super.tearDown(); - } - - /* - * Test the constructor with no parameter, and no relevant log manager - * properties are set. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies the constructor with no parameter, and no relevant log manager properties are set.", - method = "StreamHandler", - args = {} - ) - public void testConstructor_NoParameter_NoProperties() { - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.level")); - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.filter")); - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.formatter")); - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.encoding")); - - StreamHandler h = new StreamHandler(); - assertSame(Level.INFO, h.getLevel()); - assertTrue(h.getFormatter() instanceof SimpleFormatter); - assertNull(h.getFilter()); - assertNull(h.getEncoding()); - } - - /* - * Test the constructor with insufficient privilege. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies the constructor with insufficient privilege.", - method = "StreamHandler", - args = {java.io.OutputStream.class, java.util.logging.Formatter.class} - ) - public void testConstructor_NoParameter_InsufficientPrivilege() { - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.level")); - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.filter")); - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.formatter")); - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.encoding")); - - SecurityManager oldMan = System.getSecurityManager(); - System.setSecurityManager(new MockSecurityManager()); - // set a normal value - try { - StreamHandler h = new StreamHandler(); - assertSame(Level.INFO, h.getLevel()); - assertTrue(h.getFormatter() instanceof SimpleFormatter); - assertNull(h.getFilter()); - assertNull(h.getEncoding()); - } finally { - System.setSecurityManager(oldMan); - } - } - - /* - * Test the constructor with no parameter, and valid relevant log manager - * properties are set. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies the constructor with no parameter, and valid relevant log manager properties are set.", - method = "StreamHandler", - args = {} - ) - public void testConstructor_NoParameter_ValidProperties() throws Exception { - Properties p = new Properties(); - p.put("java.util.logging.StreamHandler.level", "FINE"); - p.put("java.util.logging.StreamHandler.filter", className - + "$MockFilter"); - p.put("java.util.logging.StreamHandler.formatter", className - + "$MockFormatter"); - p.put("java.util.logging.StreamHandler.encoding", "iso-8859-1"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - - assertEquals("FINE", LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.level")); - assertEquals("iso-8859-1", LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.encoding")); - StreamHandler h = new StreamHandler(); - assertSame(h.getLevel(), Level.parse("FINE")); - assertTrue(h.getFormatter() instanceof MockFormatter); - assertTrue(h.getFilter() instanceof MockFilter); - assertEquals("iso-8859-1", h.getEncoding()); - } - - /* - * Test the constructor with no parameter, and invalid relevant log manager - * properties are set. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies the constructor with no parameter, and invalid relevant log manager properties are set.", - method = "StreamHandler", - args = {} - ) - public void testConstructor_NoParameter_InvalidProperties() - throws Exception { - Properties p = new Properties(); - p.put("java.util.logging.StreamHandler.level", INVALID_LEVEL); - p.put("java.util.logging.StreamHandler.filter", className + ""); - p.put("java.util.logging.StreamHandler.formatter", className + ""); - p.put("java.util.logging.StreamHandler.encoding", "XXXX"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - - assertEquals(INVALID_LEVEL, LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.level")); - assertEquals("XXXX", LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.encoding")); - StreamHandler h = new StreamHandler(); - assertSame(Level.INFO, h.getLevel()); - assertTrue(h.getFormatter() instanceof SimpleFormatter); - assertNull(h.getFilter()); - assertNull(h.getEncoding()); - h.publish(new LogRecord(Level.SEVERE, "test")); - assertTrue(CallVerificationStack.getInstance().empty()); - assertNull(h.getEncoding()); - } - - /* - * Test the constructor with normal parameter values, and no relevant log - * manager properties are set. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies the constructor with normal parameter values, and no relevant log manager properties are set.", - method = "StreamHandler", - args = {java.io.OutputStream.class, java.util.logging.Formatter.class} - ) - public void testConstructor_HasParameters_NoProperties() { - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.level")); - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.filter")); - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.formatter")); - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.encoding")); - - StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), - new MockFormatter2()); - assertSame(Level.INFO, h.getLevel()); - assertTrue(h.getFormatter() instanceof MockFormatter2); - assertNull(h.getFilter()); - assertNull(h.getEncoding()); - } - - /* - * Test the constructor with insufficient privilege. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies the constructor with insufficient privilege.", - method = "StreamHandler", - args = {java.io.OutputStream.class, java.util.logging.Formatter.class} - ) - public void testConstructor_HasParameter_InsufficientPrivilege() { - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.level")); - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.filter")); - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.formatter")); - assertNull(LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.encoding")); - - SecurityManager oldMan = System.getSecurityManager(); - System.setSecurityManager(new MockSecurityManager()); - // set a normal value - try { - StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), - new MockFormatter2()); - assertSame(Level.INFO, h.getLevel()); - assertTrue(h.getFormatter() instanceof MockFormatter2); - assertNull(h.getFilter()); - assertNull(h.getEncoding()); - } finally { - System.setSecurityManager(oldMan); - } - } - - /* - * Test the constructor with normal parameter values, and valid relevant log - * manager properties are set. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies the constructor with normal parameter values, and valid relevant log manager properties are set.", - method = "StreamHandler", - args = {java.io.OutputStream.class, java.util.logging.Formatter.class} - ) - public void testConstructor_HasParameters_ValidProperties() - throws Exception { - Properties p = new Properties(); - p.put("java.util.logging.StreamHandler.level", "FINE"); - p.put("java.util.logging.StreamHandler.filter", className - + "$MockFilter"); - p.put("java.util.logging.StreamHandler.formatter", className - + "$MockFormatter"); - p.put("java.util.logging.StreamHandler.encoding", "iso-8859-1"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - - assertEquals("FINE", LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.level")); - assertEquals("iso-8859-1", LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.encoding")); - StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), - new MockFormatter2()); - assertSame(h.getLevel(), Level.parse("FINE")); - assertTrue(h.getFormatter() instanceof MockFormatter2); - assertTrue(h.getFilter() instanceof MockFilter); - assertEquals("iso-8859-1", h.getEncoding()); - } - - /* - * Test the constructor with normal parameter, and invalid relevant log - * manager properties are set. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies the constructor with normal parameter, and invalid relevant log manager properties are set.", - method = "StreamHandler", - args = {java.io.OutputStream.class, java.util.logging.Formatter.class} - ) - public void testConstructor_HasParameters_InvalidProperties() - throws Exception { - Properties p = new Properties(); - p.put("java.util.logging.StreamHandler.level", INVALID_LEVEL); - p.put("java.util.logging.StreamHandler.filter", className + ""); - p.put("java.util.logging.StreamHandler.formatter", className + ""); - p.put("java.util.logging.StreamHandler.encoding", "XXXX"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - - assertEquals(INVALID_LEVEL, LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.level")); - assertEquals("XXXX", LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.encoding")); - StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), - new MockFormatter2()); - assertSame(Level.INFO, h.getLevel()); - assertTrue(h.getFormatter() instanceof MockFormatter2); - assertNull(h.getFilter()); - assertNull(h.getEncoding()); - } - - /* - * Test the constructor with null formatter, and invalid relevant log manager - * properties are set. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies the constructor with null formatter, and invalid relevant log manager properties are set.", - method = "StreamHandler", - args = {java.io.OutputStream.class, java.util.logging.Formatter.class} - ) - public void testConstructor_HasParameters_ValidPropertiesNullStream() - throws Exception { - Properties p = new Properties(); - p.put("java.util.logging.StreamHandler.level", "FINE"); - p.put("java.util.logging.StreamHandler.filter", className - + "$MockFilter"); - p.put("java.util.logging.StreamHandler.formatter", className - + "$MockFormatter"); - p.put("java.util.logging.StreamHandler.encoding", "iso-8859-1"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - - assertEquals("FINE", LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.level")); - assertEquals("iso-8859-1", LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.encoding")); - try { - new StreamHandler(new ByteArrayOutputStream(), null); - fail("Should throw NullPointerException!"); - } catch (NullPointerException e) { - // expected - } - } - - /* - * Test the constructor with null output stream, and invalid relevant log - * manager properties are set. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies the constructor with null output stream, and invalid relevant log manager properties are set.", - method = "StreamHandler", - args = {java.io.OutputStream.class, java.util.logging.Formatter.class} - ) - public void testConstructor_HasParameters_ValidPropertiesNullFormatter() - throws Exception { - Properties p = new Properties(); - p.put("java.util.logging.StreamHandler.level", "FINE"); - p.put("java.util.logging.StreamHandler.filter", className - + "$MockFilter"); - p.put("java.util.logging.StreamHandler.formatter", className - + "$MockFormatter"); - p.put("java.util.logging.StreamHandler.encoding", "iso-8859-1"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - - assertEquals("FINE", LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.level")); - assertEquals("iso-8859-1", LogManager.getLogManager().getProperty( - "java.util.logging.StreamHandler.encoding")); - try { - new StreamHandler(null, new MockFormatter2()); - fail("Should throw NullPointerException!"); - } catch (NullPointerException e) { - // expected - } - } - - /* - * Test close() when having sufficient privilege, and a record has been - * written to the output stream. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies close() when having sufficient privilege, and a record has been written to the output stream.", - method = "close", - args = {} - ) - public void testClose_SufficientPrivilege_NormalClose() { - ByteArrayOutputStream aos = new MockOutputStream(); - StreamHandler h = new StreamHandler(aos, new MockFormatter()); - h.publish(new LogRecord(Level.SEVERE, - "testClose_SufficientPrivilege_NormalClose msg")); - h.close(); - assertEquals("close", CallVerificationStack.getInstance() - .getCurrentSourceMethod()); - assertNull(CallVerificationStack.getInstance().pop()); - assertEquals("flush", CallVerificationStack.getInstance() - .getCurrentSourceMethod()); - CallVerificationStack.getInstance().clear(); - assertTrue(aos.toString().endsWith("MockFormatter_Tail")); - h.close(); - } - - /* - * Test close() when having sufficient privilege, and an output stream that - * always throws exceptions. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies close() when having sufficient privilege, and an output stream that always throws exceptions.", - method = "close", - args = {} - ) - public void testClose_SufficientPrivilege_Exception() { - ByteArrayOutputStream aos = new MockExceptionOutputStream(); - StreamHandler h = new StreamHandler(aos, new MockFormatter()); - h.publish(new LogRecord(Level.SEVERE, - "testClose_SufficientPrivilege_Exception msg")); - h.flush(); - h.close(); - } - - /* - * Test close() when having sufficient privilege, and no record has been - * written to the output stream. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies close() method when having sufficient privilege, and no record has been written to the output stream.", - method = "close", - args = {} - ) - public void testClose_SufficientPrivilege_DirectClose() { - ByteArrayOutputStream aos = new MockOutputStream(); - StreamHandler h = new StreamHandler(aos, new MockFormatter()); - h.close(); - assertEquals("close", CallVerificationStack.getInstance() - .getCurrentSourceMethod()); - assertNull(CallVerificationStack.getInstance().pop()); - assertEquals("flush", CallVerificationStack.getInstance() - .getCurrentSourceMethod()); - CallVerificationStack.getInstance().clear(); - assertEquals("MockFormatter_HeadMockFormatter_Tail", aos.toString()); - } - - /* - * Test close() when having insufficient privilege. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies SecurityException.", - method = "close", - args = {} - ) - public void testClose_InsufficientPrivilege() { - StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), - new MockFormatter()); - - SecurityManager oldMan = System.getSecurityManager(); - System.setSecurityManager(new MockSecurityManager()); - try { - h.close(); - fail("Should throw SecurityException!"); - } catch (SecurityException e) { - // expected - } finally { - System.setSecurityManager(oldMan); - } - } - - /* - * Test close() when having no output stream. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies close() method when having no output stream.", - method = "close", - args = {} - ) - public void testClose_NoOutputStream() { - StreamHandler h = new StreamHandler(); - h.close(); - } - - /* - * Test flush(). - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies flush() method.", - method = "flush", - args = {} - ) - public void testFlush_Normal() { - ByteArrayOutputStream aos = new MockOutputStream(); - StreamHandler h = new StreamHandler(aos, new MockFormatter()); - h.flush(); - assertEquals("flush", CallVerificationStack.getInstance().getCurrentSourceMethod()); - assertNull(CallVerificationStack.getInstance().pop()); - CallVerificationStack.getInstance().clear(); - } - - /* - * Test flush() when having no output stream. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies flush() when having no output stream.", - method = "flush", - args = {} - ) - public void testFlush_NoOutputStream() { - StreamHandler h = new StreamHandler(); - h.flush(); - } - - /* - * Test isLoggable(), use no filter, having no output stream - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies isLoggable(), use no filter, having no output stream.", - method = "isLoggable", - args = {java.util.logging.LogRecord.class} - ) - public void testIsLoggable_NoOutputStream() { - StreamHandler h = new StreamHandler(); - LogRecord r = new LogRecord(Level.INFO, null); - assertFalse(h.isLoggable(r)); - - h.setLevel(Level.WARNING); - assertFalse(h.isLoggable(r)); - - h.setLevel(Level.CONFIG); - assertFalse(h.isLoggable(r)); - - r.setLevel(Level.OFF); - h.setLevel(Level.OFF); - assertFalse(h.isLoggable(r)); - } - - /* - * Test isLoggable(), use no filter, having output stream - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies isLoggable(), use no filter, having output stream.", - method = "isLoggable", - args = {java.util.logging.LogRecord.class} - ) - public void testIsLoggable_NoFilter() { - StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), - new SimpleFormatter()); - LogRecord r = new LogRecord(Level.INFO, null); - assertTrue(h.isLoggable(r)); - - h.setLevel(Level.WARNING); - assertFalse(h.isLoggable(r)); - - h.setLevel(Level.CONFIG); - assertTrue(h.isLoggable(r)); - - r.setLevel(Level.OFF); - h.setLevel(Level.OFF); - assertFalse(h.isLoggable(r)); - } - - /* - * Test isLoggable(), use a filter, having output stream - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies isLoggable(), use a filter, having output stream.", - method = "isLoggable", - args = {java.util.logging.LogRecord.class} - ) - public void testIsLoggable_WithFilter() { - StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), - new SimpleFormatter()); - LogRecord r = new LogRecord(Level.INFO, null); - h.setFilter(new MockFilter()); - assertFalse(h.isLoggable(r)); - assertSame(r, CallVerificationStack.getInstance().pop()); - - h.setLevel(Level.CONFIG); - assertFalse(h.isLoggable(r)); - assertSame(r, CallVerificationStack.getInstance().pop()); - - h.setLevel(Level.WARNING); - assertFalse(h.isLoggable(r)); //level to high, data will not reach the filter - assertTrue(CallVerificationStack.getInstance().empty()); - } - - /* - * Test isLoggable(), null log record, having output stream. Handler should - * call ErrorManager to handle exceptional case - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies isLoggable(), null log record, having output stream. Handler should call ErrorManager to handle exceptional case.", - method = "isLoggable", - args = {java.util.logging.LogRecord.class} - ) - public void testIsLoggable_Null() { - StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), - new SimpleFormatter()); - assertFalse(h.isLoggable(null)); - } - - /* - * Test isLoggable(), null log record, without output stream - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies isLoggable(), null log record, without output stream.", - method = "isLoggable", - args = {java.util.logging.LogRecord.class} - ) - public void testIsLoggable_Null_NoOutputStream() { - StreamHandler h = new StreamHandler(); - assertFalse(h.isLoggable(null)); - } - - /* - * Test publish(), use no filter, having output stream, normal log record. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies publish(), use no filter, having output stream, normal log record.", - method = "publish", - args = {java.util.logging.LogRecord.class} - ) - public void testPublish_NoOutputStream() { - StreamHandler h = new StreamHandler(); - LogRecord r = new LogRecord(Level.INFO, "testPublish_NoOutputStream"); - h.publish(r); - - h.setLevel(Level.WARNING); - h.publish(r); - - h.setLevel(Level.CONFIG); - h.publish(r); - - r.setLevel(Level.OFF); - h.setLevel(Level.OFF); - h.publish(r); - } - - /* - * Test publish(), use no filter, having output stream, normal log record. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies publish(), use no filter, having output stream, normal log record.", - method = "publish", - args = {java.util.logging.LogRecord.class} - ) - public void testPublish_NoFilter() { - ByteArrayOutputStream aos = new ByteArrayOutputStream(); - StreamHandler h = new StreamHandler(aos, new MockFormatter()); - - LogRecord r = new LogRecord(Level.INFO, "testPublish_NoFilter"); - h.setLevel(Level.INFO); - h.publish(r); - h.flush(); - assertEquals("MockFormatter_Head" + "testPublish_NoFilter", aos - .toString()); - - h.setLevel(Level.WARNING); - h.publish(r); - h.flush(); - assertEquals("MockFormatter_Head" + "testPublish_NoFilter", aos - .toString()); - - h.setLevel(Level.CONFIG); - h.publish(r); - h.flush(); - assertEquals("MockFormatter_Head" + "testPublish_NoFilter" - + "testPublish_NoFilter", aos.toString()); - - r.setLevel(Level.OFF); - h.setLevel(Level.OFF); - h.publish(r); - h.flush(); - assertEquals("MockFormatter_Head" + "testPublish_NoFilter" - + "testPublish_NoFilter", aos.toString()); - } - - /* - * Test publish(), use a filter, having output stream, normal log record. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies publish(), use a filter, having output stream, normal log record.", - method = "publish", - args = {java.util.logging.LogRecord.class} - ) - public void testPublish_WithFilter() { - ByteArrayOutputStream aos = new ByteArrayOutputStream(); - StreamHandler h = new StreamHandler(aos, new MockFormatter()); - h.setFilter(new MockFilter()); - - LogRecord r = new LogRecord(Level.INFO, "testPublish_WithFilter"); - h.setLevel(Level.INFO); - h.publish(r); - h.flush(); - assertEquals("", aos.toString()); - assertSame(r, CallVerificationStack.getInstance().pop()); - - h.setLevel(Level.WARNING); - h.publish(r); - h.flush(); - assertEquals("", aos.toString()); - assertTrue(CallVerificationStack.getInstance().empty()); - - h.setLevel(Level.CONFIG); - h.publish(r); - h.flush(); - assertEquals("", aos.toString()); - assertSame(r, CallVerificationStack.getInstance().pop()); - - r.setLevel(Level.OFF); - h.setLevel(Level.OFF); - h.publish(r); - h.flush(); - assertEquals("", aos.toString()); - assertTrue(CallVerificationStack.getInstance().empty()); - } - - /* - * Test publish(), null log record, handler should call ErrorManager to - * handle exceptional case - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies publish(), null log record, handler should call ErrorManager to handle exceptional case.", - method = "publish", - args = {java.util.logging.LogRecord.class} - ) - public void testPublish_Null() { - StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), - new SimpleFormatter()); - h.publish(null); - } - - /* - * Test publish(), null log record, without output stream - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies publish(), null log record, without output stream.", - method = "publish", - args = {java.util.logging.LogRecord.class} - ) - public void testPublish_Null_NoOutputStream() { - StreamHandler h = new StreamHandler(); - h.publish(null); - // regression test for Harmony-1279 - MockFilter filter = new MockFilter(); - h.setLevel(Level.FINER); - h.setFilter(filter); - LogRecord record = new LogRecord(Level.FINE, "abc"); - h.publish(record); - // verify that filter.isLoggable is not called, because there's no - // associated output stream. - assertTrue(CallVerificationStack.getInstance().empty()); - } - - /* - * Test publish(), a log record with empty msg, having output stream - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies publish(), a log record with empty msg, having output stream.", - method = "publish", - args = {java.util.logging.LogRecord.class} - ) - public void testPublish_EmptyMsg() { - ByteArrayOutputStream aos = new ByteArrayOutputStream(); - StreamHandler h = new StreamHandler(aos, new MockFormatter()); - LogRecord r = new LogRecord(Level.INFO, ""); - h.publish(r); - h.flush(); - assertEquals("MockFormatter_Head", aos.toString()); - } - - /* - * Test publish(), a log record with null msg, having output stream - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies publish(), a log record with null msg, having output stream.", - method = "publish", - args = {java.util.logging.LogRecord.class} - ) - public void testPublish_NullMsg() { - ByteArrayOutputStream aos = new ByteArrayOutputStream(); - StreamHandler h = new StreamHandler(aos, new MockFormatter()); - LogRecord r = new LogRecord(Level.INFO, null); - h.publish(r); - h.flush(); - assertEquals("MockFormatter_Head", aos.toString()); - } - - /* - * Test publish(), after close. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies publish(), after close.", - method = "publish", - args = {java.util.logging.LogRecord.class} - ) - public void testPublish_AfterClose() throws Exception { - Properties p = new Properties(); - p.put("java.util.logging.StreamHandler.level", "FINE"); - LogManager.getLogManager().readConfiguration( - EnvironmentHelper.PropertiesToInputStream(p)); - - ByteArrayOutputStream aos = new ByteArrayOutputStream(); - StreamHandler h = new StreamHandler(aos, new MockFormatter()); - assertSame(h.getLevel(), Level.FINE); - LogRecord r = new LogRecord(Level.INFO, "testPublish_NoFormatter"); - assertTrue(h.isLoggable(r)); - h.close(); - assertFalse(h.isLoggable(r)); - h.publish(r); - h.flush(); - assertEquals("MockFormatter_HeadMockFormatter_Tail", aos.toString()); - } - - /* - * Test setEncoding() method with supported encoding. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies setEncoding() method with supported encoding.", - method = "setEncoding", - args = {java.lang.String.class} - ) - public void testSetEncoding_Normal() throws Exception { - ByteArrayOutputStream aos = new ByteArrayOutputStream(); - StreamHandler h = new StreamHandler(aos, new MockFormatter()); - h.setEncoding("iso-8859-1"); - assertEquals("iso-8859-1", h.getEncoding()); - LogRecord r = new LogRecord(Level.INFO, "\u6881\u884D\u8F69"); - h.publish(r); - h.flush(); - - byte[] bytes = encoder.encode( - CharBuffer.wrap("MockFormatter_Head" + "\u6881\u884D\u8F69")) - .array(); - assertTrue(Arrays.equals(bytes, aos.toByteArray())); - } - - /* - * Test setEncoding() method with supported encoding, after a log record - * has been written. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies setEncoding() method with supported encoding, after a log record has been written.", - method = "setEncoding", - args = {java.lang.String.class} - ) - public void testSetEncoding_AfterPublish() throws Exception { - ByteArrayOutputStream aos = new ByteArrayOutputStream(); - StreamHandler h = new StreamHandler(aos, new MockFormatter()); - h.setEncoding("iso-8859-1"); - assertEquals("iso-8859-1", h.getEncoding()); - LogRecord r = new LogRecord(Level.INFO, "\u6881\u884D\u8F69"); - h.publish(r); - h.flush(); - assertTrue(Arrays.equals(aos.toByteArray(), encoder.encode( - CharBuffer.wrap("MockFormatter_Head" + "\u6881\u884D\u8F69")) - .array())); - - h.setEncoding("iso8859-1"); - assertEquals("iso8859-1", h.getEncoding()); - r = new LogRecord(Level.INFO, "\u6881\u884D\u8F69"); - h.publish(r); - h.flush(); - assertFalse(Arrays.equals(aos.toByteArray(), encoder.encode( - CharBuffer.wrap("MockFormatter_Head" + "\u6881\u884D\u8F69" - + "testSetEncoding_Normal2")).array())); - byte[] b0 = aos.toByteArray(); - byte[] b1 = encoder.encode( - CharBuffer.wrap("MockFormatter_Head" + "\u6881\u884D\u8F69")) - .array(); - byte[] b2 = encoder.encode(CharBuffer.wrap("\u6881\u884D\u8F69")) - .array(); - byte[] b3 = new byte[b1.length + b2.length]; - System.arraycopy(b1, 0, b3, 0, b1.length); - System.arraycopy(b2, 0, b3, b1.length, b2.length); - assertTrue(Arrays.equals(b0, b3)); - } - - /* - * Test setEncoding() methods with null. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies setEncoding() methods with null.", - method = "setEncoding", - args = {java.lang.String.class} - ) - public void testSetEncoding_Null() throws Exception { - StreamHandler h = new StreamHandler(); - h.setEncoding(null); - assertNull(h.getEncoding()); - } - - /* - * Test setEncoding() methods with unsupported encoding. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies setEncoding() methods with unsupported encoding.", - method = "setEncoding", - args = {java.lang.String.class} - ) - public void testSetEncoding_Unsupported() { - StreamHandler h = new StreamHandler(); - try { - h.setEncoding("impossible"); - fail("Should throw UnsupportedEncodingException!"); - } catch (UnsupportedEncodingException e) { - // expected - } - assertNull(h.getEncoding()); - } - - /* - * Test setEncoding() with insufficient privilege. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies setEncoding() method with insufficient privilege.", - method = "setEncoding", - args = {java.lang.String.class} - ) - public void testSetEncoding_InsufficientPrivilege() throws Exception { - StreamHandler h = new StreamHandler(); - SecurityManager oldMan = System.getSecurityManager(); - System.setSecurityManager(new MockSecurityManager()); - // set a normal value - try { - h.setEncoding("iso-8859-1"); - fail("Should throw SecurityException!"); - } catch (SecurityException e) { - // expected - } finally { - System.setSecurityManager(oldMan); - } - assertNull(h.getEncoding()); - System.setSecurityManager(new MockSecurityManager()); - // set an invalid value - try { - - h.setEncoding("impossible"); - fail("Should throw SecurityException!"); - } catch (SecurityException e) { - // expected - } finally { - System.setSecurityManager(oldMan); - } - assertNull(h.getEncoding()); - } - - /* - * Test setEncoding() methods will flush a stream before setting. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies that setEncoding() method will flush a stream before setting.", - method = "setEncoding", - args = {java.lang.String.class} - ) - public void testSetEncoding_FlushBeforeSetting() throws Exception { - ByteArrayOutputStream aos = new ByteArrayOutputStream(); - StreamHandler h = new StreamHandler(aos, new MockFormatter()); - LogRecord r = new LogRecord(Level.INFO, "abcd"); - h.publish(r); - assertFalse(aos.toString().indexOf("abcd") > 0); - h.setEncoding("iso-8859-1"); - assertTrue(aos.toString().indexOf("abcd") > 0); - } - - /* - * Test setOutputStream() with null. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies setOutputStream() method with null.", - method = "setOutputStream", - args = {java.io.OutputStream.class} - ) - public void testSetOutputStream_null() { - MockStreamHandler h = new MockStreamHandler( - new ByteArrayOutputStream(), new SimpleFormatter()); - try { - h.setOutputStream(null); - fail("Should throw NullPointerException!"); - } catch (NullPointerException e) { - // expected - } - } - - /* - * Test setOutputStream() under normal condition. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies setOutputStream() method under normal condition.", - method = "setOutputStream", - args = {java.io.OutputStream.class} - ) - public void testSetOutputStream_Normal() { - ByteArrayOutputStream aos = new ByteArrayOutputStream(); - MockStreamHandler h = new MockStreamHandler(aos, new MockFormatter()); - - LogRecord r = new LogRecord(Level.INFO, "testSetOutputStream_Normal"); - h.publish(r); - assertSame(r, CallVerificationStack.getInstance().pop()); - assertTrue(CallVerificationStack.getInstance().empty()); - h.flush(); - assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal", aos - .toString()); - - ByteArrayOutputStream aos2 = new ByteArrayOutputStream(); - h.setOutputStream(aos2); - assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal" - + "MockFormatter_Tail", aos.toString()); - r = new LogRecord(Level.INFO, "testSetOutputStream_Normal2"); - h.publish(r); - assertSame(r, CallVerificationStack.getInstance().pop()); - assertTrue(CallVerificationStack.getInstance().empty()); - h.flush(); - assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal2", aos2 - .toString()); - assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal" - + "MockFormatter_Tail", aos.toString()); - } - - /* - * Test setOutputStream() after close. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies setOutputStream() method after close.", - method = "setOutputStream", - args = {java.io.OutputStream.class} - ) - public void testSetOutputStream_AfterClose() { - ByteArrayOutputStream aos = new ByteArrayOutputStream(); - MockStreamHandler h = new MockStreamHandler(aos, new MockFormatter()); - - LogRecord r = new LogRecord(Level.INFO, "testSetOutputStream_Normal"); - h.publish(r); - assertSame(r, CallVerificationStack.getInstance().pop()); - assertTrue(CallVerificationStack.getInstance().empty()); - h.flush(); - assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal", aos - .toString()); - h.close(); - - ByteArrayOutputStream aos2 = new ByteArrayOutputStream(); - h.setOutputStream(aos2); - assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal" - + "MockFormatter_Tail", aos.toString()); - r = new LogRecord(Level.INFO, "testSetOutputStream_Normal2"); - h.publish(r); - assertSame(r, CallVerificationStack.getInstance().pop()); - assertTrue(CallVerificationStack.getInstance().empty()); - h.flush(); - assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal2", aos2 - .toString()); - assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal" - + "MockFormatter_Tail", aos.toString()); - } - - /* - * Test setOutputStream() when having insufficient privilege. - */ - @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, - notes = "Verifies setOutputStream() method when having insufficient privilege.", - method = "setOutputStream", - args = {java.io.OutputStream.class} - ) - public void testSetOutputStream_InsufficientPrivilege() { - MockStreamHandler h = new MockStreamHandler(); - SecurityManager oldMan = System.getSecurityManager(); - System.setSecurityManager(new MockSecurityManager()); - - try { - h.setOutputStream(new ByteArrayOutputStream()); - fail("Should throw SecurityException!"); - } catch (SecurityException e) { - // expected - } finally { - System.setSecurityManager(oldMan); - } - - h = new MockStreamHandler(); - System.setSecurityManager(new MockSecurityManager()); - try { - h.setOutputStream(null); - fail("Should throw NullPointerException!"); - } catch (NullPointerException e) { - // expected - } finally { - System.setSecurityManager(oldMan); - } - } - - /* - * A mock stream handler, expose setOutputStream. - */ - public static class MockStreamHandler extends StreamHandler { - public MockStreamHandler() { - super(); - } - - public MockStreamHandler(OutputStream out, Formatter formatter) { - super(out, formatter); - } - - public void setOutputStream(OutputStream out) { - super.setOutputStream(out); - } - - public boolean isLoggable(LogRecord r) { - CallVerificationStack.getInstance().push(r); - return super.isLoggable(r); - } - } - - /* - * A mock filter, always return false. - */ - public static class MockFilter implements Filter { - - public boolean isLoggable(LogRecord record) { - CallVerificationStack.getInstance().push(record); - return false; - } - } - - /* - * A mock formatter. - */ - public static class MockFormatter extends java.util.logging.Formatter { - public String format(LogRecord r) { - // System.out.println("formatter called..."); - return super.formatMessage(r); - } - - /* - * (non-Javadoc) - * - * @see java.util.logging.Formatter#getHead(java.util.logging.Handler) - */ - public String getHead(Handler h) { - return "MockFormatter_Head"; - } - - /* - * (non-Javadoc) - * - * @see java.util.logging.Formatter#getTail(java.util.logging.Handler) - */ - public String getTail(Handler h) { - return "MockFormatter_Tail"; - } - } - - /* - * Another mock formatter. - */ - public static class MockFormatter2 extends java.util.logging.Formatter { - public String format(LogRecord r) { - // System.out.println("formatter2 called..."); - return r.getMessage(); - } - } - - /* - * A mock output stream. - */ - public static class MockOutputStream extends ByteArrayOutputStream { - - /* - * (non-Javadoc) - * - * @see java.io.OutputStream#close() - */ - public void close() throws IOException { - CallVerificationStack.getInstance().push(null); - super.close(); - } - - /* - * (non-Javadoc) - * - * @see java.io.OutputStream#flush() - */ - public void flush() throws IOException { - CallVerificationStack.getInstance().push(null); - super.flush(); - } - - /* - * (non-Javadoc) - * - * @see java.io.OutputStream#write(int) - */ - public void write(int oneByte) { - super.write(oneByte); - } - } - - /* - * A mock output stream that always throw exception. - */ - public static class MockExceptionOutputStream extends ByteArrayOutputStream { - - /* - * (non-Javadoc) - * - * @see java.io.OutputStream#close() - */ - public void close() throws IOException { - throw new IOException(); - } - - /* - * (non-Javadoc) - * - * @see java.io.OutputStream#flush() - */ - public void flush() throws IOException { - throw new IOException(); - } - - /* - * (non-Javadoc) - * - * @see java.io.OutputStream#write(byte[], int, int) - */ - public synchronized void write(byte[] buffer, int offset, int count) { - throw new NullPointerException(); - } - - /* - * (non-Javadoc) - * - * @see java.io.OutputStream#write(int) - */ - public synchronized void write(int oneByte) { - throw new NullPointerException(); - } - } - - /* - * Used to grant all permissions except logging control. - */ - public static class MockSecurityManager extends SecurityManager { - - public MockSecurityManager() { - } - - public void checkPermission(Permission perm) { - // grant all permissions except logging control - if (perm instanceof LoggingPermission) { - throw new SecurityException(); - } - } - - public void checkPermission(Permission perm, Object context) { - // grant all permissions except logging control - if (perm instanceof LoggingPermission) { - throw new SecurityException(); - } - } - } + } + + /* + * Test the constructor with no parameter, and no relevant log manager + * properties are set. + */ + public void testConstructor_NoParameter_NoProperties() { + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.level")); + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.filter")); + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.formatter")); + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.encoding")); + + StreamHandler h = new StreamHandler(); + assertSame(Level.INFO, h.getLevel()); + assertTrue(h.getFormatter() instanceof SimpleFormatter); + assertNull(h.getFilter()); + assertNull(h.getEncoding()); + } + + /* + * Test the constructor with insufficient privilege. + */ + public void testConstructor_NoParameter_InsufficientPrivilege() { + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.level")); + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.filter")); + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.formatter")); + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.encoding")); + + SecurityManager oldMan = System.getSecurityManager(); + System.setSecurityManager(new MockSecurityManager()); + // set a normal value + try { + StreamHandler h = new StreamHandler(); + assertSame(Level.INFO, h.getLevel()); + assertTrue(h.getFormatter() instanceof SimpleFormatter); + assertNull(h.getFilter()); + assertNull(h.getEncoding()); + } finally { + System.setSecurityManager(oldMan); + } + } + + /* + * Test the constructor with no parameter, and valid relevant log manager + * properties are set. + */ + public void testConstructor_NoParameter_ValidProperties() throws Exception { + Properties p = new Properties(); + p.put("java.util.logging.StreamHandler.level", "FINE"); + p.put("java.util.logging.StreamHandler.filter", className + + "$MockFilter"); + p.put("java.util.logging.StreamHandler.formatter", className + + "$MockFormatter"); + p.put("java.util.logging.StreamHandler.encoding", "iso-8859-1"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + + assertEquals("FINE", LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.level")); + assertEquals("iso-8859-1", LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.encoding")); + StreamHandler h = new StreamHandler(); + assertSame(h.getLevel(), Level.parse("FINE")); + assertTrue(h.getFormatter() instanceof MockFormatter); + assertTrue(h.getFilter() instanceof MockFilter); + assertEquals("iso-8859-1", h.getEncoding()); + } + + /* + * Test the constructor with no parameter, and invalid relevant log manager + * properties are set. + */ + public void testConstructor_NoParameter_InvalidProperties() + throws Exception { + Properties p = new Properties(); + p.put("java.util.logging.StreamHandler.level", INVALID_LEVEL); + p.put("java.util.logging.StreamHandler.filter", className + ""); + p.put("java.util.logging.StreamHandler.formatter", className + ""); + p.put("java.util.logging.StreamHandler.encoding", "XXXX"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + + assertEquals(INVALID_LEVEL, LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.level")); + assertEquals("XXXX", LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.encoding")); + StreamHandler h = new StreamHandler(); + assertSame(Level.INFO, h.getLevel()); + assertTrue(h.getFormatter() instanceof SimpleFormatter); + assertNull(h.getFilter()); + assertNull(h.getEncoding()); + h.publish(new LogRecord(Level.SEVERE, "test")); + assertTrue(CallVerificationStack.getInstance().empty()); + assertNull(h.getEncoding()); + } + + /* + * Test the constructor with normal parameter values, and no relevant log + * manager properties are set. + */ + public void testConstructor_HasParameters_NoProperties() { + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.level")); + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.filter")); + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.formatter")); + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.encoding")); + + StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), + new MockFormatter2()); + assertSame(Level.INFO, h.getLevel()); + assertTrue(h.getFormatter() instanceof MockFormatter2); + assertNull(h.getFilter()); + assertNull(h.getEncoding()); + } + + /* + * Test the constructor with insufficient privilege. + */ + public void testConstructor_HasParameter_InsufficientPrivilege() { + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.level")); + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.filter")); + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.formatter")); + assertNull(LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.encoding")); + + SecurityManager oldMan = System.getSecurityManager(); + System.setSecurityManager(new MockSecurityManager()); + // set a normal value + try { + StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), + new MockFormatter2()); + assertSame(Level.INFO, h.getLevel()); + assertTrue(h.getFormatter() instanceof MockFormatter2); + assertNull(h.getFilter()); + assertNull(h.getEncoding()); + } finally { + System.setSecurityManager(oldMan); + } + } + + /* + * Test the constructor with normal parameter values, and valid relevant log + * manager properties are set. + */ + public void testConstructor_HasParameters_ValidProperties() + throws Exception { + Properties p = new Properties(); + p.put("java.util.logging.StreamHandler.level", "FINE"); + p.put("java.util.logging.StreamHandler.filter", className + + "$MockFilter"); + p.put("java.util.logging.StreamHandler.formatter", className + + "$MockFormatter"); + p.put("java.util.logging.StreamHandler.encoding", "iso-8859-1"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + + assertEquals("FINE", LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.level")); + assertEquals("iso-8859-1", LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.encoding")); + StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), + new MockFormatter2()); + assertSame(h.getLevel(), Level.parse("FINE")); + assertTrue(h.getFormatter() instanceof MockFormatter2); + assertTrue(h.getFilter() instanceof MockFilter); + assertEquals("iso-8859-1", h.getEncoding()); + } + + /* + * Test the constructor with normal parameter, and invalid relevant log + * manager properties are set. + */ + public void testConstructor_HasParameters_InvalidProperties() + throws Exception { + Properties p = new Properties(); + p.put("java.util.logging.StreamHandler.level", INVALID_LEVEL); + p.put("java.util.logging.StreamHandler.filter", className + ""); + p.put("java.util.logging.StreamHandler.formatter", className + ""); + p.put("java.util.logging.StreamHandler.encoding", "XXXX"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + + assertEquals(INVALID_LEVEL, LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.level")); + assertEquals("XXXX", LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.encoding")); + StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), + new MockFormatter2()); + assertSame(Level.INFO, h.getLevel()); + assertTrue(h.getFormatter() instanceof MockFormatter2); + assertNull(h.getFilter()); + assertNull(h.getEncoding()); + } + + /* + * Test the constructor with null formatter, and invalid relevant log manager + * properties are set. + */ + public void testConstructor_HasParameters_ValidPropertiesNullStream() + throws Exception { + Properties p = new Properties(); + p.put("java.util.logging.StreamHandler.level", "FINE"); + p.put("java.util.logging.StreamHandler.filter", className + + "$MockFilter"); + p.put("java.util.logging.StreamHandler.formatter", className + + "$MockFormatter"); + p.put("java.util.logging.StreamHandler.encoding", "iso-8859-1"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + + assertEquals("FINE", LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.level")); + assertEquals("iso-8859-1", LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.encoding")); + try { + new StreamHandler(new ByteArrayOutputStream(), null); + fail("Should throw NullPointerException!"); + } catch (NullPointerException e) { + // expected + } + } + + /* + * Test the constructor with null output stream, and invalid relevant log + * manager properties are set. + */ + public void testConstructor_HasParameters_ValidPropertiesNullFormatter() + throws Exception { + Properties p = new Properties(); + p.put("java.util.logging.StreamHandler.level", "FINE"); + p.put("java.util.logging.StreamHandler.filter", className + + "$MockFilter"); + p.put("java.util.logging.StreamHandler.formatter", className + + "$MockFormatter"); + p.put("java.util.logging.StreamHandler.encoding", "iso-8859-1"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + + assertEquals("FINE", LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.level")); + assertEquals("iso-8859-1", LogManager.getLogManager().getProperty( + "java.util.logging.StreamHandler.encoding")); + try { + new StreamHandler(null, new MockFormatter2()); + fail("Should throw NullPointerException!"); + } catch (NullPointerException e) { + // expected + } + } + + /* + * Test close() when having sufficient privilege, and a record has been + * written to the output stream. + */ + public void testClose_SufficientPrivilege_NormalClose() { + ByteArrayOutputStream aos = new MockOutputStream(); + StreamHandler h = new StreamHandler(aos, new MockFormatter()); + h.publish(new LogRecord(Level.SEVERE, + "testClose_SufficientPrivilege_NormalClose msg")); + h.close(); + assertEquals("close", CallVerificationStack.getInstance() + .getCurrentSourceMethod()); + assertNull(CallVerificationStack.getInstance().pop()); + assertEquals("flush", CallVerificationStack.getInstance() + .getCurrentSourceMethod()); + CallVerificationStack.getInstance().clear(); + assertTrue(aos.toString().endsWith("MockFormatter_Tail")); + h.close(); + } + + /* + * Test close() when having sufficient privilege, and an output stream that + * always throws exceptions. + */ + public void testClose_SufficientPrivilege_Exception() { + ByteArrayOutputStream aos = new MockExceptionOutputStream(); + StreamHandler h = new StreamHandler(aos, new MockFormatter()); + h.publish(new LogRecord(Level.SEVERE, + "testClose_SufficientPrivilege_Exception msg")); + h.flush(); + h.close(); + } + + /* + * Test close() when having sufficient privilege, and no record has been + * written to the output stream. + */ + public void testClose_SufficientPrivilege_DirectClose() { + ByteArrayOutputStream aos = new MockOutputStream(); + StreamHandler h = new StreamHandler(aos, new MockFormatter()); + h.close(); + assertEquals("close", CallVerificationStack.getInstance() + .getCurrentSourceMethod()); + assertNull(CallVerificationStack.getInstance().pop()); + assertEquals("flush", CallVerificationStack.getInstance() + .getCurrentSourceMethod()); + CallVerificationStack.getInstance().clear(); + assertEquals("MockFormatter_HeadMockFormatter_Tail", aos.toString() + ); + } + + /* + * Test close() when having insufficient privilege. + */ + public void testClose_InsufficientPrivilege() { + SecurityManager oldMan = System.getSecurityManager(); + System.setSecurityManager(new MockSecurityManager()); + try { + StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), + new MockFormatter()); + h.close(); + fail("Should throw SecurityException!"); + } catch (SecurityException e) { + // expected + } finally { + System.setSecurityManager(oldMan); + } + } + + /* + * Test close() when having no output stream. + */ + public void testClose_NoOutputStream() { + StreamHandler h = new StreamHandler(); + h.close(); + } + + /* + * Test flush(). + */ + public void testFlush_Normal() { + ByteArrayOutputStream aos = new MockOutputStream(); + StreamHandler h = new StreamHandler(aos, new MockFormatter()); + h.flush(); + assertEquals("flush", CallVerificationStack.getInstance() + .getCurrentSourceMethod()); + assertNull(CallVerificationStack.getInstance().pop()); + CallVerificationStack.getInstance().clear(); + } + + /* + * Test flush() when having no output stream. + */ + public void testFlush_NoOutputStream() { + StreamHandler h = new StreamHandler(); + h.flush(); + } + + /* + * Test isLoggable(), use no filter, having output stream + */ + public void testIsLoggable_NoOutputStream() { + StreamHandler h = new StreamHandler(); + LogRecord r = new LogRecord(Level.INFO, null); + assertFalse(h.isLoggable(r)); + + h.setLevel(Level.WARNING); + assertFalse(h.isLoggable(r)); + + h.setLevel(Level.CONFIG); + assertFalse(h.isLoggable(r)); + + r.setLevel(Level.OFF); + h.setLevel(Level.OFF); + assertFalse(h.isLoggable(r)); + } + + /* + * Test isLoggable(), use no filter, having output stream + */ + public void testIsLoggable_NoFilter() { + StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), + new SimpleFormatter()); + LogRecord r = new LogRecord(Level.INFO, null); + assertTrue(h.isLoggable(r)); + + h.setLevel(Level.WARNING); + assertFalse(h.isLoggable(r)); + + h.setLevel(Level.CONFIG); + assertTrue(h.isLoggable(r)); + + r.setLevel(Level.OFF); + h.setLevel(Level.OFF); + assertFalse(h.isLoggable(r)); + } + + /* + * Test isLoggable(), use a filter, having output stream + */ + public void testIsLoggable_WithFilter() { + StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), + new SimpleFormatter()); + LogRecord r = new LogRecord(Level.INFO, null); + h.setFilter(new MockFilter()); + assertFalse(h.isLoggable(r)); + assertSame(r, CallVerificationStack.getInstance().pop()); + + h.setLevel(Level.CONFIG); + assertFalse(h.isLoggable(r)); + assertSame(r, CallVerificationStack.getInstance().pop()); + + h.setLevel(Level.WARNING); + assertFalse(h.isLoggable(r)); + assertTrue(CallVerificationStack.getInstance().empty()); + } + + /* + * Test isLoggable(), null log record, having output stream. Handler should + * call ErrorManager to handle exceptional case + */ + public void testIsLoggable_Null() { + StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), + new SimpleFormatter()); + assertFalse(h.isLoggable(null)); + } + + /* + * Test isLoggable(), null log record, without output stream + */ + public void testIsLoggable_Null_NoOutputStream() { + StreamHandler h = new StreamHandler(); + assertFalse(h.isLoggable(null)); + } + + /* + * Test publish(), use no filter, having output stream, normal log record. + */ + public void testPublish_NoOutputStream() { + StreamHandler h = new StreamHandler(); + LogRecord r = new LogRecord(Level.INFO, "testPublish_NoOutputStream"); + h.publish(r); + + h.setLevel(Level.WARNING); + h.publish(r); + + h.setLevel(Level.CONFIG); + h.publish(r); + + r.setLevel(Level.OFF); + h.setLevel(Level.OFF); + h.publish(r); + } + + /* + * Test publish(), use no filter, having output stream, normal log record. + */ + public void testPublish_NoFilter() { + ByteArrayOutputStream aos = new ByteArrayOutputStream(); + StreamHandler h = new StreamHandler(aos, new MockFormatter()); + + LogRecord r = new LogRecord(Level.INFO, "testPublish_NoFilter"); + h.setLevel(Level.INFO); + h.publish(r); + h.flush(); + assertEquals("MockFormatter_Head" + "testPublish_NoFilter", aos + .toString()); + + h.setLevel(Level.WARNING); + h.publish(r); + h.flush(); + assertEquals("MockFormatter_Head" + "testPublish_NoFilter", aos + .toString()); + + h.setLevel(Level.CONFIG); + h.publish(r); + h.flush(); + assertEquals("MockFormatter_Head" + "testPublish_NoFilter" + + "testPublish_NoFilter", aos.toString()); + + r.setLevel(Level.OFF); + h.setLevel(Level.OFF); + h.publish(r); + h.flush(); + assertEquals("MockFormatter_Head" + "testPublish_NoFilter" + + "testPublish_NoFilter", aos.toString()); + } + + /* + * Test publish(), use a filter, having output stream, normal log record. + */ + public void testPublish_WithFilter() { + ByteArrayOutputStream aos = new ByteArrayOutputStream(); + StreamHandler h = new StreamHandler(aos, new MockFormatter()); + h.setFilter(new MockFilter()); + + LogRecord r = new LogRecord(Level.INFO, "testPublish_WithFilter"); + h.setLevel(Level.INFO); + h.publish(r); + h.flush(); + assertEquals("", aos.toString()); + assertSame(r, CallVerificationStack.getInstance().pop()); + + h.setLevel(Level.WARNING); + h.publish(r); + h.flush(); + assertEquals("", aos.toString()); + assertTrue(CallVerificationStack.getInstance().empty()); + + h.setLevel(Level.CONFIG); + h.publish(r); + h.flush(); + assertEquals("", aos.toString()); + assertSame(r, CallVerificationStack.getInstance().pop()); + + r.setLevel(Level.OFF); + h.setLevel(Level.OFF); + h.publish(r); + h.flush(); + assertEquals("", aos.toString()); + assertTrue(CallVerificationStack.getInstance().empty()); + } + + /* + * Test publish(), null log record, handler should call ErrorManager to + * handle exceptional case + */ + public void testPublish_Null() { + StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), + new SimpleFormatter()); + h.publish(null); + } + + /* + * Test publish(), null log record, without output stream + */ + public void testPublish_Null_NoOutputStream() { + StreamHandler h = new StreamHandler(); + h.publish(null); + // regression test for Harmony-1279 + MockFilter filter = new MockFilter(); + h.setLevel(Level.FINER); + h.setFilter(filter); + LogRecord record = new LogRecord(Level.FINE, "abc"); + h.publish(record); + // verify that filter.isLoggable is not called, because there's no + // associated output stream. + assertTrue(CallVerificationStack.getInstance().empty()); + } + + /* + * Test publish(), a log record with empty msg, having output stream + */ + public void testPublish_EmptyMsg() { + ByteArrayOutputStream aos = new ByteArrayOutputStream(); + StreamHandler h = new StreamHandler(aos, new MockFormatter()); + LogRecord r = new LogRecord(Level.INFO, ""); + h.publish(r); + h.flush(); + assertEquals("MockFormatter_Head", aos.toString()); + } + + /* + * Test publish(), a log record with null msg, having output stream + */ + public void testPublish_NullMsg() { + ByteArrayOutputStream aos = new ByteArrayOutputStream(); + StreamHandler h = new StreamHandler(aos, new MockFormatter()); + LogRecord r = new LogRecord(Level.INFO, null); + h.publish(r); + h.flush(); + assertEquals("MockFormatter_Head", aos.toString()); + } + + /* + * Test publish(), after close. + */ + public void testPublish_AfterClose() throws Exception { + Properties p = new Properties(); + p.put("java.util.logging.StreamHandler.level", "FINE"); + LogManager.getLogManager().readConfiguration( + EnvironmentHelper.PropertiesToInputStream(p)); + + ByteArrayOutputStream aos = new ByteArrayOutputStream(); + StreamHandler h = new StreamHandler(aos, new MockFormatter()); + assertSame(h.getLevel(), Level.FINE); + LogRecord r = new LogRecord(Level.INFO, "testPublish_NoFormatter"); + assertTrue(h.isLoggable(r)); + h.close(); + assertFalse(h.isLoggable(r)); + h.publish(r); + h.flush(); + assertEquals("MockFormatter_HeadMockFormatter_Tail", aos.toString()); + } + + /* + * Test setEncoding() method with supported encoding. + */ + public void testSetEncoding_Normal() throws Exception { + ByteArrayOutputStream aos = new ByteArrayOutputStream(); + StreamHandler h = new StreamHandler(aos, new MockFormatter()); + h.setEncoding("iso-8859-1"); + assertEquals("iso-8859-1", h.getEncoding()); + LogRecord r = new LogRecord(Level.INFO, "\u6881\u884D\u8F69"); + h.publish(r); + h.flush(); + + byte[] bytes = encoder.encode( + CharBuffer.wrap("MockFormatter_Head" + "\u6881\u884D\u8F69")) + .array(); + assertTrue(Arrays.equals(bytes, aos.toByteArray())); + } + + /* + * Test setEncoding() method with supported encoding, after a log record + * has been written. + */ + public void testSetEncoding_AfterPublish() throws Exception { + ByteArrayOutputStream aos = new ByteArrayOutputStream(); + StreamHandler h = new StreamHandler(aos, new MockFormatter()); + h.setEncoding("iso-8859-1"); + assertEquals("iso-8859-1", h.getEncoding()); + LogRecord r = new LogRecord(Level.INFO, "\u6881\u884D\u8F69"); + h.publish(r); + h.flush(); + assertTrue(Arrays.equals(aos.toByteArray(), encoder.encode( + CharBuffer.wrap("MockFormatter_Head" + "\u6881\u884D\u8F69")) + .array())); + + h.setEncoding("iso8859-1"); + assertEquals("iso8859-1", h.getEncoding()); + r = new LogRecord(Level.INFO, "\u6881\u884D\u8F69"); + h.publish(r); + h.flush(); + assertFalse(Arrays.equals(aos.toByteArray(), encoder.encode( + CharBuffer.wrap("MockFormatter_Head" + "\u6881\u884D\u8F69" + + "testSetEncoding_Normal2")).array())); + byte[] b0 = aos.toByteArray(); + byte[] b1 = encoder.encode( + CharBuffer.wrap("MockFormatter_Head" + "\u6881\u884D\u8F69")) + .array(); + byte[] b2 = encoder.encode(CharBuffer.wrap("\u6881\u884D\u8F69")) + .array(); + byte[] b3 = new byte[b1.length + b2.length]; + System.arraycopy(b1, 0, b3, 0, b1.length); + System.arraycopy(b2, 0, b3, b1.length, b2.length); + assertTrue(Arrays.equals(b0, b3)); + } + + /* + * Test setEncoding() methods with null. + */ + public void testSetEncoding_Null() throws Exception { + StreamHandler h = new StreamHandler(); + h.setEncoding(null); + assertNull(h.getEncoding()); + } + + /* + * Test setEncoding() methods with unsupported encoding. + */ + public void testSetEncoding_Unsupported() { + StreamHandler h = new StreamHandler(); + try { + h.setEncoding("impossible"); + fail("Should throw UnsupportedEncodingException!"); + } catch (UnsupportedEncodingException e) { + // expected + } + assertNull(h.getEncoding()); + } + + /* + * Test setEncoding() with insufficient privilege. + */ + public void testSetEncoding_InsufficientPrivilege() throws Exception { + StreamHandler h = new StreamHandler(); + SecurityManager oldMan = System.getSecurityManager(); + System.setSecurityManager(new MockSecurityManager()); + // set a normal value + try { + h.setEncoding("iso-8859-1"); + fail("Should throw SecurityException!"); + } catch (SecurityException e) { + // expected + } finally { + System.setSecurityManager(oldMan); + } + assertNull(h.getEncoding()); + System.setSecurityManager(new MockSecurityManager()); + // set an invalid value + try { + + h.setEncoding("impossible"); + fail("Should throw SecurityException!"); + } catch (SecurityException e) { + // expected + } finally { + System.setSecurityManager(oldMan); + } + assertNull(h.getEncoding()); + } + + /* + * Test setEncoding() methods will flush a stream before setting. + */ + public void testSetEncoding_FlushBeforeSetting() throws Exception { + ByteArrayOutputStream aos = new ByteArrayOutputStream(); + StreamHandler h = new StreamHandler(aos, new MockFormatter()); + LogRecord r = new LogRecord(Level.INFO, "abcd"); + h.publish(r); + assertFalse(aos.toString().indexOf("abcd") > 0); + h.setEncoding("iso-8859-1"); + assertTrue(aos.toString().indexOf("abcd") > 0); + } + + /* + * Test setOutputStream() with null. + */ + public void testSetOutputStream_null() { + MockStreamHandler h = new MockStreamHandler( + new ByteArrayOutputStream(), new SimpleFormatter()); + try { + h.setOutputStream(null); + fail("Should throw NullPointerException!"); + } catch (NullPointerException e) { + // expected + } + } + + /* + * Test setOutputStream() under normal condition. + */ + public void testSetOutputStream_Normal() { + ByteArrayOutputStream aos = new ByteArrayOutputStream(); + MockStreamHandler h = new MockStreamHandler(aos, new MockFormatter()); + + LogRecord r = new LogRecord(Level.INFO, "testSetOutputStream_Normal"); + h.publish(r); + assertSame(r, CallVerificationStack.getInstance().pop()); + assertTrue(CallVerificationStack.getInstance().empty()); + h.flush(); + assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal", aos + .toString()); + + ByteArrayOutputStream aos2 = new ByteArrayOutputStream(); + h.setOutputStream(aos2); + assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal" + + "MockFormatter_Tail", aos.toString()); + r = new LogRecord(Level.INFO, "testSetOutputStream_Normal2"); + h.publish(r); + assertSame(r, CallVerificationStack.getInstance().pop()); + assertTrue(CallVerificationStack.getInstance().empty()); + h.flush(); + assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal2", aos2 + .toString()); + assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal" + + "MockFormatter_Tail", aos.toString()); + } + + /* + * Test setOutputStream() after close. + */ + public void testSetOutputStream_AfterClose() { + ByteArrayOutputStream aos = new ByteArrayOutputStream(); + MockStreamHandler h = new MockStreamHandler(aos, new MockFormatter()); + + LogRecord r = new LogRecord(Level.INFO, "testSetOutputStream_Normal"); + h.publish(r); + assertSame(r, CallVerificationStack.getInstance().pop()); + assertTrue(CallVerificationStack.getInstance().empty()); + h.flush(); + assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal", aos + .toString()); + h.close(); + + ByteArrayOutputStream aos2 = new ByteArrayOutputStream(); + h.setOutputStream(aos2); + assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal" + + "MockFormatter_Tail", aos.toString()); + r = new LogRecord(Level.INFO, "testSetOutputStream_Normal2"); + h.publish(r); + assertSame(r, CallVerificationStack.getInstance().pop()); + assertTrue(CallVerificationStack.getInstance().empty()); + h.flush(); + assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal2", aos2 + .toString()); + assertEquals("MockFormatter_Head" + "testSetOutputStream_Normal" + + "MockFormatter_Tail", aos.toString()); + } + + /* + * Test setOutputStream() when having insufficient privilege. + */ + public void testSetOutputStream_InsufficientPrivilege() { + MockStreamHandler h = new MockStreamHandler(); + SecurityManager oldMan = System.getSecurityManager(); + System.setSecurityManager(new MockSecurityManager()); + + try { + h.setOutputStream(new ByteArrayOutputStream()); + fail("Should throw SecurityException!"); + } catch (SecurityException e) { + // expected + } finally { + System.setSecurityManager(oldMan); + } + + h = new MockStreamHandler(); + System.setSecurityManager(new MockSecurityManager()); + try { + h.setOutputStream(null); + fail("Should throw NullPointerException!"); + } catch (NullPointerException e) { + // expected + } finally { + System.setSecurityManager(oldMan); + } + } + + /* + * A mock stream handler, expose setOutputStream. + */ + public static class MockStreamHandler extends StreamHandler { + public MockStreamHandler() { + super(); + } + + public MockStreamHandler(OutputStream out, Formatter formatter) { + super(out, formatter); + } + + public void setOutputStream(OutputStream out) { + super.setOutputStream(out); + } + + public boolean isLoggable(LogRecord r) { + CallVerificationStack.getInstance().push(r); + return super.isLoggable(r); + } + } + + /* + * A mock filter, always return false. + */ + public static class MockFilter implements Filter { + + public boolean isLoggable(LogRecord record) { + CallVerificationStack.getInstance().push(record); + return false; + } + } + + /* + * A mock formatter. + */ + public static class MockFormatter extends java.util.logging.Formatter { + public String format(LogRecord r) { + // System.out.println("formatter called..."); + return super.formatMessage(r); + } + + /* + * (non-Javadoc) + * + * @see java.util.logging.Formatter#getHead(java.util.logging.Handler) + */ + public String getHead(Handler h) { + return "MockFormatter_Head"; + } + + /* + * (non-Javadoc) + * + * @see java.util.logging.Formatter#getTail(java.util.logging.Handler) + */ + public String getTail(Handler h) { + return "MockFormatter_Tail"; + } + } + + /* + * Another mock formatter. + */ + public static class MockFormatter2 extends java.util.logging.Formatter { + public String format(LogRecord r) { + // System.out.println("formatter2 called..."); + return r.getMessage(); + } + } + + /* + * A mock output stream. + */ + public static class MockOutputStream extends ByteArrayOutputStream { + + /* + * (non-Javadoc) + * + * @see java.io.OutputStream#close() + */ + public void close() throws IOException { + CallVerificationStack.getInstance().push(null); + super.close(); + } + + /* + * (non-Javadoc) + * + * @see java.io.OutputStream#flush() + */ + public void flush() throws IOException { + CallVerificationStack.getInstance().push(null); + super.flush(); + } + + /* + * (non-Javadoc) + * + * @see java.io.OutputStream#write(int) + */ + public void write(int oneByte) { + // TODO Auto-generated method stub + super.write(oneByte); + } + } + + /* + * A mock output stream that always throw exception. + */ + public static class MockExceptionOutputStream extends ByteArrayOutputStream { + + /* + * (non-Javadoc) + * + * @see java.io.OutputStream#close() + */ + public void close() throws IOException { + throw new IOException(); + } + + /* + * (non-Javadoc) + * + * @see java.io.OutputStream#flush() + */ + public void flush() throws IOException { + throw new IOException(); + } + + /* + * (non-Javadoc) + * + * @see java.io.OutputStream#write(byte[], int, int) + */ + public synchronized void write(byte[] buffer, int offset, int count) { + throw new NullPointerException(); + } + + /* + * (non-Javadoc) + * + * @see java.io.OutputStream#write(int) + */ + public synchronized void write(int oneByte) { + throw new NullPointerException(); + } + } + + /* + * Used to grant all permissions except logging control. + */ + public static class MockSecurityManager extends SecurityManager { + + public MockSecurityManager() { + } + + public void checkPermission(Permission perm) { + // grant all permissions except logging control + if (perm instanceof LoggingPermission) { + throw new SecurityException(); + } + } + + public void checkPermission(Permission perm, Object context) { + // grant all permissions except logging control + if (perm instanceof LoggingPermission) { + throw new SecurityException(); + } + } + } } diff --git a/logging/src/test/java/tests/logging/AllTests.java b/logging/src/test/java/tests/logging/AllTests.java index c6032df..aa0cb15 100644 --- a/logging/src/test/java/tests/logging/AllTests.java +++ b/logging/src/test/java/tests/logging/AllTests.java @@ -24,16 +24,11 @@ import junit.framework.TestSuite; * Test suite that includes all tests for the Logging project. */ public class AllTests { - - public static void main(String[] args) { - junit.textui.TestRunner.run(AllTests.suite()); - } - public static Test suite() { - TestSuite suite = tests.TestSuiteFactory.createTestSuite("All Logging test suites"); + TestSuite suite = new TestSuite("All Logging test suites"); // $JUnit-BEGIN$ suite.addTest(org.apache.harmony.logging.tests.java.util.logging.AllTests.suite()); // $JUnit-END$ return suite; } -}
\ No newline at end of file +} |