Java

Форк
0
/
TestLogHandlerTest.java 
99 строк · 2.8 Кб
1
/*
2
 * Copyright (C) 2008 The Guava Authors
3
 *
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
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
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.
15
 */
16

17
package com.google.common.testing;
18

19
import java.util.logging.Level;
20
import java.util.logging.LogRecord;
21
import java.util.logging.Logger;
22
import junit.framework.TestCase;
23

24
/**
25
 * Unit test for {@link TestLogHandler}.
26
 *
27
 * @author kevinb
28
 */
29
public class TestLogHandlerTest extends TestCase {
30

31
  private TestLogHandler handler;
32
  private TearDownStack stack = new TearDownStack();
33

34
  @Override
35
  protected void setUp() throws Exception {
36
    super.setUp();
37

38
    handler = new TestLogHandler();
39

40
    // You could also apply it higher up the Logger hierarchy than this
41
    ExampleClassUnderTest.logger.addHandler(handler);
42

43
    ExampleClassUnderTest.logger.setUseParentHandlers(false); // optional
44

45
    stack.addTearDown(
46
        new TearDown() {
47
          @Override
48
          public void tearDown() throws Exception {
49
            ExampleClassUnderTest.logger.setUseParentHandlers(true);
50
            ExampleClassUnderTest.logger.removeHandler(handler);
51
          }
52
        });
53
  }
54

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

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();
72
    }
73
  }
74

75
  @Override
76
  public final void runBare() throws Throwable {
77
    try {
78
      setUp();
79
      runTest();
80
    } finally {
81
      tearDown();
82
    }
83
  }
84

85
  @Override
86
  protected void tearDown() {
87
    stack.runTearDown();
88
  }
89

90
  static final Exception EXCEPTION = new Exception();
91

92
  static class ExampleClassUnderTest {
93
    static final Logger logger = Logger.getLogger(ExampleClassUnderTest.class.getName());
94

95
    static void foo() {
96
      logger.log(Level.INFO, "message", EXCEPTION);
97
    }
98
  }
99
}
100

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.