Home | History | Annotate | Download | only in testing
      1 /*
      2  * Copyright (C) 2008 Google Inc.
      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 junit.framework.TestCase;
     20 
     21 import java.util.logging.Level;
     22 import java.util.logging.LogRecord;
     23 import java.util.logging.Logger;
     24 
     25 /**
     26  * Unit test for {@link TestLogHandler}.
     27  *
     28  * @author kevinb
     29  */
     30 public class TestLogHandlerTest extends TestCase {
     31 
     32   private TestLogHandler handler;
     33   private TearDownStack stack = new TearDownStack();
     34 
     35   @Override 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(new TearDown() {
     46       @Override
     47       public void tearDown() throws Exception {
     48         ExampleClassUnderTest.logger.setUseParentHandlers(true);
     49         ExampleClassUnderTest.logger.removeHandler(handler);
     50       }
     51     });
     52   }
     53 
     54   public void test() throws Exception {
     55     assertTrue(handler.getStoredLogRecords().isEmpty());
     56     ExampleClassUnderTest.foo();
     57     LogRecord record = handler.getStoredLogRecords().iterator().next();
     58     assertEquals(Level.INFO, record.getLevel());
     59     assertEquals("message", record.getMessage());
     60     assertSame(EXCEPTION, record.getThrown());
     61   }
     62 
     63   public void testConcurrentModification() throws Exception {
     64     // Tests for the absence of a bug where logging while iterating over the
     65     // stored log records causes a ConcurrentModificationException
     66     assertTrue(handler.getStoredLogRecords().isEmpty());
     67     ExampleClassUnderTest.foo();
     68     ExampleClassUnderTest.foo();
     69     for (LogRecord record : handler.getStoredLogRecords()) {
     70       ExampleClassUnderTest.foo();
     71     }
     72   }
     73 
     74   @Override public final void runBare() throws Throwable {
     75     try {
     76       setUp();
     77       runTest();
     78     } finally {
     79       tearDown();
     80     }
     81   }
     82 
     83   @Override protected void tearDown() {
     84     stack.runTearDown();
     85   }
     86 
     87   static final Exception EXCEPTION = new Exception();
     88 
     89   static class ExampleClassUnderTest {
     90     static final Logger logger
     91         = Logger.getLogger(ExampleClassUnderTest.class.getName());
     92 
     93     static void foo() {
     94       logger.log(Level.INFO,  "message", EXCEPTION);
     95     }
     96   }
     97 }
     98