Search in sources :

Example 1 with TestLogHandler

use of com.google.common.testing.TestLogHandler in project caffeine by ben-manes.

the class CacheLoadingTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    logHandler = new TestLogHandler();
    logger.addHandler(logHandler);
}
Also used : TestLogHandler(com.google.common.testing.TestLogHandler)

Example 2 with TestLogHandler

use of com.google.common.testing.TestLogHandler in project guava by google.

the class CloserTest method testLoggingSuppressor.

public static void testLoggingSuppressor() throws IOException {
    TestLogHandler logHandler = new TestLogHandler();
    Closeables.logger.addHandler(logHandler);
    try {
        Closer closer = new Closer(new Closer.LoggingSuppressor());
        TestCloseable c1 = closer.register(TestCloseable.throwsOnClose(new IOException()));
        TestCloseable c2 = closer.register(TestCloseable.throwsOnClose(new RuntimeException()));
        try {
            throw closer.rethrow(new IOException("thrown"), IOException.class);
        } catch (IOException expected) {
        }
        assertTrue(logHandler.getStoredLogRecords().isEmpty());
        closer.close();
        assertEquals(2, logHandler.getStoredLogRecords().size());
        LogRecord record = logHandler.getStoredLogRecords().get(0);
        assertEquals("Suppressing exception thrown when closing " + c2, record.getMessage());
        record = logHandler.getStoredLogRecords().get(1);
        assertEquals("Suppressing exception thrown when closing " + c1, record.getMessage());
    } finally {
        Closeables.logger.removeHandler(logHandler);
    }
}
Also used : TestLogHandler(com.google.common.testing.TestLogHandler) LogRecord(java.util.logging.LogRecord) IOException(java.io.IOException)

Example 3 with TestLogHandler

use of com.google.common.testing.TestLogHandler in project guava by google.

the class CharSourceTest method testCopyExceptions.

public void testCopyExceptions() {
    if (!Closer.SuppressingSuppressor.isAvailable()) {
        // test that exceptions are logged
        TestLogHandler logHandler = new TestLogHandler();
        Closeables.logger.addHandler(logHandler);
        try {
            for (CharSource in : BROKEN_SOURCES) {
                runFailureTest(in, newNormalCharSink());
                assertTrue(logHandler.getStoredLogRecords().isEmpty());
                runFailureTest(in, BROKEN_CLOSE_SINK);
                assertEquals((in == BROKEN_OPEN_SOURCE) ? 0 : 1, getAndResetRecords(logHandler));
            }
            for (CharSink out : BROKEN_SINKS) {
                runFailureTest(newNormalCharSource(), out);
                assertTrue(logHandler.getStoredLogRecords().isEmpty());
                runFailureTest(BROKEN_CLOSE_SOURCE, out);
                assertEquals(1, getAndResetRecords(logHandler));
            }
            for (CharSource in : BROKEN_SOURCES) {
                for (CharSink out : BROKEN_SINKS) {
                    runFailureTest(in, out);
                    assertTrue(getAndResetRecords(logHandler) <= 1);
                }
            }
        } finally {
            Closeables.logger.removeHandler(logHandler);
        }
    } else {
        for (CharSource in : BROKEN_SOURCES) {
            int suppressed = runSuppressionFailureTest(in, newNormalCharSink());
            assertEquals(0, suppressed);
            suppressed = runSuppressionFailureTest(in, BROKEN_CLOSE_SINK);
            assertEquals((in == BROKEN_OPEN_SOURCE) ? 0 : 1, suppressed);
        }
        for (CharSink out : BROKEN_SINKS) {
            int suppressed = runSuppressionFailureTest(newNormalCharSource(), out);
            assertEquals(0, suppressed);
            suppressed = runSuppressionFailureTest(BROKEN_CLOSE_SOURCE, out);
            assertEquals(1, suppressed);
        }
        for (CharSource in : BROKEN_SOURCES) {
            for (CharSink out : BROKEN_SINKS) {
                int suppressed = runSuppressionFailureTest(in, out);
                assertTrue(suppressed <= 1);
            }
        }
    }
}
Also used : TestLogHandler(com.google.common.testing.TestLogHandler)

Example 4 with TestLogHandler

use of com.google.common.testing.TestLogHandler in project guava by google.

the class ListenerCallQueueTest method testEnqueueAndDispatch_withLabeledExceptions.

public void testEnqueueAndDispatch_withLabeledExceptions() {
    Object listener = new MyListener();
    ListenerCallQueue<Object> queue = new ListenerCallQueue<>();
    queue.addListener(listener, directExecutor());
    queue.enqueue(THROWING_EVENT, "custom-label");
    Logger logger = Logger.getLogger(ListenerCallQueue.class.getName());
    logger.setLevel(Level.SEVERE);
    TestLogHandler logHandler = new TestLogHandler();
    logger.addHandler(logHandler);
    try {
        queue.dispatch();
    } finally {
        logger.removeHandler(logHandler);
    }
    assertEquals(1, logHandler.getStoredLogRecords().size());
    assertEquals("Exception while executing callback: MyListener custom-label", logHandler.getStoredLogRecords().get(0).getMessage());
}
Also used : TestLogHandler(com.google.common.testing.TestLogHandler) Logger(java.util.logging.Logger)

Example 5 with TestLogHandler

use of com.google.common.testing.TestLogHandler in project guava by google.

the class FuturesTest method testSuccessfulAsList_resultCancelledRacingInputDone.

public void testSuccessfulAsList_resultCancelledRacingInputDone() throws Exception {
    TestLogHandler listenerLoggerHandler = new TestLogHandler();
    Logger exceptionLogger = Logger.getLogger(AbstractFuture.class.getName());
    exceptionLogger.addHandler(listenerLoggerHandler);
    try {
        doTestSuccessfulAsList_resultCancelledRacingInputDone();
        assertWithMessage("Nothing should be logged").that(listenerLoggerHandler.getStoredLogRecords()).isEmpty();
    } finally {
        exceptionLogger.removeHandler(listenerLoggerHandler);
    }
}
Also used : TestLogHandler(com.google.common.testing.TestLogHandler) Logger(java.util.logging.Logger)

Aggregations

TestLogHandler (com.google.common.testing.TestLogHandler)12 Logger (java.util.logging.Logger)4 LogRecord (java.util.logging.LogRecord)2 IOException (java.io.IOException)1 Formatter (java.util.logging.Formatter)1