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