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.jupiter.api.Assertions.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.testclassification.MiscTests;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.junit.jupiter.api.AfterEach;
033import org.junit.jupiter.api.BeforeEach;
034import org.junit.jupiter.api.Tag;
035import org.junit.jupiter.api.Test;
036import org.mockito.invocation.InvocationOnMock;
037import org.mockito.stubbing.Answer;
038
039/**
040 * This should be in the hbase-logging module but the {@link HBaseClassTestRule} is in hbase-common
041 * so we can only put the class in hbase-common module for now...
042 */
043@Tag(MiscTests.TAG)
044@Tag(SmallTests.TAG)
045public class TestJul2Slf4j {
046
047  static {
048    System.setProperty("java.util.logging.config.class", JulToSlf4jInitializer.class.getName());
049  }
050
051  private String loggerName = getClass().getName();
052
053  private org.apache.logging.log4j.core.Appender mockAppender;
054
055  @BeforeEach
056  public void setUp() {
057    mockAppender = mock(org.apache.logging.log4j.core.Appender.class);
058    when(mockAppender.getName()).thenReturn("mockAppender");
059    when(mockAppender.isStarted()).thenReturn(true);
060    ((org.apache.logging.log4j.core.Logger) org.apache.logging.log4j.LogManager
061      .getLogger(loggerName)).addAppender(mockAppender);
062  }
063
064  @AfterEach
065  public void tearDown() {
066    ((org.apache.logging.log4j.core.Logger) org.apache.logging.log4j.LogManager
067      .getLogger(loggerName)).removeAppender(mockAppender);
068  }
069
070  @Test
071  public void test() throws IOException {
072    AtomicReference<org.apache.logging.log4j.Level> level = new AtomicReference<>();
073    AtomicReference<String> msg = new AtomicReference<String>();
074    doAnswer(new Answer<Void>() {
075
076      @Override
077      public Void answer(InvocationOnMock invocation) throws Throwable {
078        org.apache.logging.log4j.core.LogEvent logEvent =
079          invocation.getArgument(0, org.apache.logging.log4j.core.LogEvent.class);
080        level.set(logEvent.getLevel());
081        msg.set(logEvent.getMessage().getFormattedMessage());
082        return null;
083      }
084    }).when(mockAppender).append(any(org.apache.logging.log4j.core.LogEvent.class));
085    java.util.logging.Logger logger = java.util.logging.Logger.getLogger(loggerName);
086    logger.info(loggerName);
087    verify(mockAppender, times(1)).append(any(org.apache.logging.log4j.core.LogEvent.class));
088    assertEquals(org.apache.logging.log4j.Level.INFO, level.get());
089    assertEquals(loggerName, msg.get());
090  }
091}