Search in sources :

Example 26 with StreamHandler

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

the class StreamHandlerTest method testPublish_NoFilter.

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

Example 27 with StreamHandler

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

the class StreamHandlerTest method testPublish_EmptyMsg.

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

Example 28 with StreamHandler

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

the class StreamHandlerTest method testConstructor_NoParameter_ValidProperties.

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

Example 29 with StreamHandler

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

the class StreamHandlerTest method testClose_SufficientPrivilege_NormalClose.

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

Example 30 with StreamHandler

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

the class StreamHandlerTest method testConstructor_HasParameters_NoProperties.

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