2
* Copyright (C) 2008 The Guava Authors
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package com.google.common.testing;
19
import java.util.logging.Level;
20
import java.util.logging.LogRecord;
21
import java.util.logging.Logger;
22
import junit.framework.TestCase;
25
* Unit test for {@link TestLogHandler}.
29
public class TestLogHandlerTest extends TestCase {
31
private TestLogHandler handler;
32
private TearDownStack stack = new TearDownStack();
35
protected void setUp() throws Exception {
38
handler = new TestLogHandler();
40
// You could also apply it higher up the Logger hierarchy than this
41
ExampleClassUnderTest.logger.addHandler(handler);
43
ExampleClassUnderTest.logger.setUseParentHandlers(false); // optional
48
public void tearDown() throws Exception {
49
ExampleClassUnderTest.logger.setUseParentHandlers(true);
50
ExampleClassUnderTest.logger.removeHandler(handler);
55
public void test() throws Exception {
56
assertTrue(handler.getStoredLogRecords().isEmpty());
57
ExampleClassUnderTest.foo();
58
LogRecord record = handler.getStoredLogRecords().get(0);
59
assertEquals(Level.INFO, record.getLevel());
60
assertEquals("message", record.getMessage());
61
assertSame(EXCEPTION, record.getThrown());
64
public void testConcurrentModification() throws Exception {
65
// Tests for the absence of a bug where logging while iterating over the
66
// stored log records causes a ConcurrentModificationException
67
assertTrue(handler.getStoredLogRecords().isEmpty());
68
ExampleClassUnderTest.foo();
69
ExampleClassUnderTest.foo();
70
for (LogRecord unused : handler.getStoredLogRecords()) {
71
ExampleClassUnderTest.foo();
76
public final void runBare() throws Throwable {
86
protected void tearDown() {
90
static final Exception EXCEPTION = new Exception();
92
static class ExampleClassUnderTest {
93
static final Logger logger = Logger.getLogger(ExampleClassUnderTest.class.getName());
96
logger.log(Level.INFO, "message", EXCEPTION);