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);
}
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);
}
}
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);
}
}
}
}
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());
}
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);
}
}
Aggregations