Search in sources :

Example 6 with StreamHandler

use of java.util.logging.StreamHandler in project j2objc by google.

the class StreamHandlerTest method testPublish_Null.

/*
	 * 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);
}
Also used : StreamHandler(java.util.logging.StreamHandler) SimpleFormatter(java.util.logging.SimpleFormatter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 7 with StreamHandler

use of java.util.logging.StreamHandler in project j2objc by google.

the class StreamHandlerTest method testSetEncoding_FlushBeforeSetting.

/*
	 * 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);
}
Also used : LogRecord(java.util.logging.LogRecord) StreamHandler(java.util.logging.StreamHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 8 with StreamHandler

use of java.util.logging.StreamHandler in project j2objc by google.

the class StreamHandlerTest method testPublish_Null_NoOutputStream.

/*
	 * 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());
}
Also used : LogRecord(java.util.logging.LogRecord) StreamHandler(java.util.logging.StreamHandler)

Example 9 with StreamHandler

use of java.util.logging.StreamHandler in project j2objc by google.

the class StreamHandlerTest method testConstructor_HasParameters_ValidProperties.

/*
	 * 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());
}
Also used : StreamHandler(java.util.logging.StreamHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties)

Example 10 with StreamHandler

use of java.util.logging.StreamHandler in project j2objc by google.

the class StreamHandlerTest method testIsLoggable_Null.

/*
	 * 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));
}
Also used : StreamHandler(java.util.logging.StreamHandler) SimpleFormatter(java.util.logging.SimpleFormatter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

StreamHandler (java.util.logging.StreamHandler)41 ByteArrayOutputStream (java.io.ByteArrayOutputStream)25 SimpleFormatter (java.util.logging.SimpleFormatter)15 LogRecord (java.util.logging.LogRecord)14 Logger (java.util.logging.Logger)8 Properties (java.util.Properties)7 MockResponse (com.google.mockwebserver.MockResponse)6 HttpClient (org.apache.http.client.HttpClient)6 HttpGet (org.apache.http.client.methods.HttpGet)6 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)6 Handler (java.util.logging.Handler)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Level (java.util.logging.Level)1 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)1 Before (org.junit.Before)1 Test (org.junit.Test)1