|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.logging; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | +import static org.mockito.ArgumentMatchers.any; |
| 22 | +import static org.mockito.Mockito.doAnswer; |
| 23 | +import static org.mockito.Mockito.mock; |
| 24 | +import static org.mockito.Mockito.times; |
| 25 | +import static org.mockito.Mockito.verify; |
| 26 | +import static org.mockito.Mockito.when; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.concurrent.atomic.AtomicReference; |
| 30 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 31 | +import org.apache.hadoop.hbase.testclassification.MiscTests; |
| 32 | +import org.apache.hadoop.hbase.testclassification.SmallTests; |
| 33 | +import org.junit.After; |
| 34 | +import org.junit.Before; |
| 35 | +import org.junit.ClassRule; |
| 36 | +import org.junit.Test; |
| 37 | +import org.junit.experimental.categories.Category; |
| 38 | +import org.mockito.invocation.InvocationOnMock; |
| 39 | +import org.mockito.stubbing.Answer; |
| 40 | + |
| 41 | +/** |
| 42 | + * This should be in the hbase-logging module but the {@link HBaseClassTestRule} is in hbase-common |
| 43 | + * so we can only put the class in hbase-common module for now... |
| 44 | + */ |
| 45 | +@Category({ MiscTests.class, SmallTests.class }) |
| 46 | +public class TestJul2Slf4j { |
| 47 | + |
| 48 | + @ClassRule |
| 49 | + public static final HBaseClassTestRule CLASS_RULE = |
| 50 | + HBaseClassTestRule.forClass(TestJul2Slf4j.class); |
| 51 | + |
| 52 | + static { |
| 53 | + System.setProperty("java.util.logging.config.class", JulToSlf4jInitializer.class.getName()); |
| 54 | + } |
| 55 | + |
| 56 | + private String loggerName = getClass().getName(); |
| 57 | + |
| 58 | + private org.apache.logging.log4j.core.Appender mockAppender; |
| 59 | + |
| 60 | + @Before |
| 61 | + public void setUp() { |
| 62 | + mockAppender = mock(org.apache.logging.log4j.core.Appender.class); |
| 63 | + when(mockAppender.getName()).thenReturn("mockAppender"); |
| 64 | + when(mockAppender.isStarted()).thenReturn(true); |
| 65 | + ((org.apache.logging.log4j.core.Logger) org.apache.logging.log4j.LogManager |
| 66 | + .getLogger(loggerName)).addAppender(mockAppender); |
| 67 | + } |
| 68 | + |
| 69 | + @After |
| 70 | + public void tearDown() { |
| 71 | + ((org.apache.logging.log4j.core.Logger) org.apache.logging.log4j.LogManager |
| 72 | + .getLogger(loggerName)).removeAppender(mockAppender); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void test() throws IOException { |
| 77 | + AtomicReference<org.apache.logging.log4j.Level> level = new AtomicReference<>(); |
| 78 | + AtomicReference<String> msg = new AtomicReference<String>(); |
| 79 | + doAnswer(new Answer<Void>() { |
| 80 | + |
| 81 | + @Override |
| 82 | + public Void answer(InvocationOnMock invocation) throws Throwable { |
| 83 | + org.apache.logging.log4j.core.LogEvent logEvent = |
| 84 | + invocation.getArgument(0, org.apache.logging.log4j.core.LogEvent.class); |
| 85 | + level.set(logEvent.getLevel()); |
| 86 | + msg.set(logEvent.getMessage().getFormattedMessage()); |
| 87 | + return null; |
| 88 | + } |
| 89 | + }).when(mockAppender).append(any(org.apache.logging.log4j.core.LogEvent.class)); |
| 90 | + java.util.logging.Logger logger = java.util.logging.Logger.getLogger(loggerName); |
| 91 | + logger.info(loggerName); |
| 92 | + verify(mockAppender, times(1)).append(any(org.apache.logging.log4j.core.LogEvent.class)); |
| 93 | + assertEquals(org.apache.logging.log4j.Level.INFO, level.get()); |
| 94 | + assertEquals(loggerName, msg.get()); |
| 95 | + } |
| 96 | +} |
0 commit comments