Search in sources :

Example 16 with StreamHandler

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

the class StreamHandlerTest method testSetEncoding_Null.

/*
	 * 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, "梁衍轩");
		h.publish(r);
		h.flush();

		byte[] bytes = encoder.encode(
				CharBuffer.wrap("MockFormatter_Head" + "梁衍轩"))
				.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, "梁衍轩");
		h.publish(r);
		h.flush();
		assertTrue(Arrays.equals(aos.toByteArray(), encoder.encode(
				CharBuffer.wrap("MockFormatter_Head" + "梁衍轩"))
				.array()));

		h.setEncoding("iso8859-1");
		assertEquals("iso8859-1", h.getEncoding());
		r = new LogRecord(Level.INFO, "梁衍轩");
		h.publish(r);
		h.flush();
		assertFalse(Arrays.equals(aos.toByteArray(), encoder.encode(
				CharBuffer.wrap("MockFormatter_Head" + "梁衍轩"
						+ "testSetEncoding_Normal2")).array()));
		byte[] b0 = aos.toByteArray();
		byte[] b1 = encoder.encode(
				CharBuffer.wrap("MockFormatter_Head" + "梁衍轩"))
				.array();
		byte[] b2 = encoder.encode(CharBuffer.wrap("梁衍轩"))
				.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());
}
Also used : StreamHandler(java.util.logging.StreamHandler)

Example 17 with StreamHandler

use of java.util.logging.StreamHandler in project android_frameworks_base by ResurrectionRemix.

the class CookiesTest method testCookiesAreNotLogged.

/**
     * Test that we don't log potentially sensitive cookie values.
     * http://b/3095990
     */
public void testCookiesAreNotLogged() throws IOException, URISyntaxException {
    // enqueue an HTTP response with a cookie that will be rejected
    server.enqueue(new MockResponse().addHeader("Set-Cookie: password=secret; Domain=fake.domain"));
    server.play();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Logger logger = Logger.getLogger("org.apache.http");
    StreamHandler handler = new StreamHandler(out, new SimpleFormatter());
    logger.addHandler(handler);
    try {
        HttpClient client = new DefaultHttpClient();
        client.execute(new HttpGet(server.getUrl("/").toURI()));
        handler.close();
        String log = out.toString("UTF-8");
        assertTrue(log, log.contains("password"));
        assertTrue(log, log.contains("fake.domain"));
        assertFalse(log, log.contains("secret"));
    } finally {
        logger.removeHandler(handler);
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) StreamHandler(java.util.logging.StreamHandler) SimpleFormatter(java.util.logging.SimpleFormatter) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(java.util.logging.Logger) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 18 with StreamHandler

use of java.util.logging.StreamHandler in project geode by apache.

the class GemcachedDevelopmentJUnitTest method setUp.

@Before
public void setUp() throws Exception {
    System.setProperty(DistributionConfig.GEMFIRE_PREFIX + MCAST_PORT, "0");
    PORT = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
    this.server = new GemFireMemcachedServer("", PORT, getProtocol());
    server.start();
    logger.addHandler(new StreamHandler());
}
Also used : StreamHandler(java.util.logging.StreamHandler) Before(org.junit.Before)

Example 19 with StreamHandler

use of java.util.logging.StreamHandler in project android_frameworks_base by crdroidandroid.

the class CookiesTest method testCookiesAreNotLogged.

/**
     * Test that we don't log potentially sensitive cookie values.
     * http://b/3095990
     */
public void testCookiesAreNotLogged() throws IOException, URISyntaxException {
    // enqueue an HTTP response with a cookie that will be rejected
    server.enqueue(new MockResponse().addHeader("Set-Cookie: password=secret; Domain=fake.domain"));
    server.play();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Logger logger = Logger.getLogger("org.apache.http");
    StreamHandler handler = new StreamHandler(out, new SimpleFormatter());
    logger.addHandler(handler);
    try {
        HttpClient client = new DefaultHttpClient();
        client.execute(new HttpGet(server.getUrl("/").toURI()));
        handler.close();
        String log = out.toString("UTF-8");
        assertTrue(log, log.contains("password"));
        assertTrue(log, log.contains("fake.domain"));
        assertFalse(log, log.contains("secret"));
    } finally {
        logger.removeHandler(handler);
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) StreamHandler(java.util.logging.StreamHandler) SimpleFormatter(java.util.logging.SimpleFormatter) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(java.util.logging.Logger) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 20 with StreamHandler

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

the class StreamHandlerTest method testIsLoggable_Null_NoOutputStream.

/*
	 * Test isLoggable(), null log record, without output stream
	 */
public void testIsLoggable_Null_NoOutputStream() {
    StreamHandler h = new StreamHandler();
    assertFalse(h.isLoggable(null));
}
Also used : StreamHandler(java.util.logging.StreamHandler)

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