001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.logging;
019
020import static org.junit.Assert.assertEquals;
021import static org.mockito.ArgumentMatchers.any;
022import static org.mockito.Mockito.doAnswer;
023import static org.mockito.Mockito.mock;
024import static org.mockito.Mockito.times;
025import static org.mockito.Mockito.verify;
026import static org.mockito.Mockito.when;
027
028import java.io.IOException;
029import java.util.concurrent.atomic.AtomicReference;
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.testclassification.MiscTests;
032import org.apache.hadoop.hbase.testclassification.SmallTests;
033import org.junit.After;
034import org.junit.Before;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.mockito.invocation.InvocationOnMock;
039import org.mockito.stubbing.Answer;
040
041/**
042 * This should be in the hbase-logging module but the {@link HBaseClassTestRule} is in hbase-common
043 * so we can only put the class in hbase-common module for now...
044 */
045@Category({ MiscTests.class, SmallTests.class })
046public class TestJul2Slf4j {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050    HBaseClassTestRule.forClass(TestJul2Slf4j.class);
051
052  static {
053    System.setProperty("java.util.logging.config.class", JulToSlf4jInitializer.class.getName());
054  }
055
056  private String loggerName = getClass().getName();
057
058  private org.apache.logging.log4j.core.Appender mockAppender;
059
060  @Before
061  public void setUp() {
062    mockAppender = mock(org.apache.logging.log4j.core.Appender.class);
063    when(mockAppender.getName()).thenReturn("mockAppender");
064    when(mockAppender.isStarted()).thenReturn(true);
065    ((org.apache.logging.log4j.core.Logger) org.apache.logging.log4j.LogManager
066      .getLogger(loggerName)).addAppender(mockAppender);
067  }
068
069  @After
070  public void tearDown() {
071    ((org.apache.logging.log4j.core.Logger) org.apache.logging.log4j.LogManager
072      .getLogger(loggerName)).removeAppender(mockAppender);
073  }
074
075  @Test
076  public void test() throws IOException {
077    AtomicReference<org.apache.logging.log4j.Level> level = new AtomicReference<>();
078    AtomicReference<String> msg = new AtomicReference<String>();
079    doAnswer(new Answer<Void>() {
080
081      @Override
082      public Void answer(InvocationOnMock invocation) throws Throwable {
083        org.apache.logging.log4j.core.LogEvent logEvent =
084          invocation.getArgument(0, org.apache.logging.log4j.core.LogEvent.class);
085        level.set(logEvent.getLevel());
086        msg.set(logEvent.getMessage().getFormattedMessage());
087        return null;
088      }
089    }).when(mockAppender).append(any(org.apache.logging.log4j.core.LogEvent.class));
090    java.util.logging.Logger logger = java.util.logging.Logger.getLogger(loggerName);
091    logger.info(loggerName);
092    verify(mockAppender, times(1)).append(any(org.apache.logging.log4j.core.LogEvent.class));
093    assertEquals(org.apache.logging.log4j.Level.INFO, level.get());
094    assertEquals(loggerName, msg.get());
095  }
096}