diff --git a/trpc-logger/trpc-logger-admin/src/main/java/com/tencent/trpc/logger/admin/LogbackLoggerProcessUnit.java b/trpc-logger/trpc-logger-admin/src/main/java/com/tencent/trpc/logger/admin/LogbackLoggerProcessUnit.java index 0ae16e3d00..404ecf94f5 100644 --- a/trpc-logger/trpc-logger-admin/src/main/java/com/tencent/trpc/logger/admin/LogbackLoggerProcessUnit.java +++ b/trpc-logger/trpc-logger-admin/src/main/java/com/tencent/trpc/logger/admin/LogbackLoggerProcessUnit.java @@ -1,7 +1,7 @@ /* * Tencent is pleased to support the open source community by making tRPC available. * - * Copyright (C) 2023 THL A29 Limited, a Tencent company. + * Copyright (C) 2023 THL A29 Limited, a Tencent company. * All rights reserved. * * If you have downloaded a copy of the tRPC source code from Tencent, @@ -15,10 +15,12 @@ import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.LoggerContext; import com.tencent.trpc.core.logger.LoggerLevel; + import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; + import org.slf4j.LoggerFactory; public class LogbackLoggerProcessUnit extends AbstractLoggerProcessUnit { @@ -35,7 +37,7 @@ public void init() { addLogger(logger.getName(), logger); } } - Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); + Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME); addLogger(rootLogger.getName(), rootLogger); } diff --git a/trpc-logger/trpc-logger-admin/src/test/java/com/tencent/trpc/logger/admin/Log4j2LoggerProcessUnitTest.java b/trpc-logger/trpc-logger-admin/src/test/java/com/tencent/trpc/logger/admin/Log4j2LoggerProcessUnitTest.java new file mode 100644 index 0000000000..1d04e0d9fb --- /dev/null +++ b/trpc-logger/trpc-logger-admin/src/test/java/com/tencent/trpc/logger/admin/Log4j2LoggerProcessUnitTest.java @@ -0,0 +1,47 @@ +/* + * Tencent is pleased to support the open source community by making tRPC available. + * + * Copyright (C) 2023 THL A29 Limited, a Tencent company. + * All rights reserved. + * + * If you have downloaded a copy of the tRPC source code from Tencent, + * please note that tRPC source code is licensed under the Apache 2.0 License, + * A copy of the Apache 2.0 License can be found in the LICENSE file. + */ + +package com.tencent.trpc.logger.admin; + +import com.tencent.trpc.core.logger.LoggerLevel; +import org.apache.logging.log4j.core.config.LoggerConfig; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class Log4j2LoggerProcessUnitTest { + + public static final String UNIT_TEST = "unit-test"; + + private Log4j2LoggerProcessUnit log4j2LoggerProcessUnit; + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Before + public void setUp() throws Exception { + log4j2LoggerProcessUnit = new Log4j2LoggerProcessUnit(); + } + + @Test + public void testInit() { + log4j2LoggerProcessUnit.init(); + } + + @Test + public void testSetLogger() { + log4j2LoggerProcessUnit.addLogger(UNIT_TEST, new LoggerConfig()); + String logger = log4j2LoggerProcessUnit.setLoggerLevel(UNIT_TEST, LoggerLevel.ALL); + Assert.assertEquals(logger, "ERROR"); + } + +} \ No newline at end of file diff --git a/trpc-logger/trpc-logger-admin/src/test/java/com/tencent/trpc/logger/admin/LogbackLoggerProcessUnitTest.java b/trpc-logger/trpc-logger-admin/src/test/java/com/tencent/trpc/logger/admin/LogbackLoggerProcessUnitTest.java index 272f97d60e..6025f8a2a4 100644 --- a/trpc-logger/trpc-logger-admin/src/test/java/com/tencent/trpc/logger/admin/LogbackLoggerProcessUnitTest.java +++ b/trpc-logger/trpc-logger-admin/src/test/java/com/tencent/trpc/logger/admin/LogbackLoggerProcessUnitTest.java @@ -1,7 +1,7 @@ /* * Tencent is pleased to support the open source community by making tRPC available. * - * Copyright (C) 2023 THL A29 Limited, a Tencent company. + * Copyright (C) 2023 THL A29 Limited, a Tencent company. * All rights reserved. * * If you have downloaded a copy of the tRPC source code from Tencent, @@ -11,6 +11,11 @@ package com.tencent.trpc.logger.admin; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; + +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.LoggerContext; import com.tencent.trpc.core.logger.LoggerLevel; import com.tencent.trpc.core.logger.slf4j.Slf4jLogger; import org.junit.Assert; @@ -18,33 +23,72 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.slf4j.LoggerFactory; +import java.util.List; +@RunWith(PowerMockRunner.class) +@PrepareForTest({LoggerFactory.class}) public class LogbackLoggerProcessUnitTest { + public static final String LOGGER = "logger"; + public static final String LOGGER_ROOT = "ROOT"; + public static final String LOGGER_DEBUG = "DEBUG"; + @Rule public ExpectedException expectedEx = ExpectedException.none(); - private LogbackLoggerProcessUnit logbackLoggerProcessUnit; + private LogbackLoggerProcessUnit logger; @Before public void setUp() throws Exception { - logbackLoggerProcessUnit = new LogbackLoggerProcessUnit(); - logbackLoggerProcessUnit.setLoggerLevel("logger", LoggerLevel.ALL); - logbackLoggerProcessUnit.addLogger("logger", new Slf4jLogger(org.slf4j.LoggerFactory.getLogger("logger"))); + logger = new LogbackLoggerProcessUnit(); + logger.setLoggerLevel(LOGGER, LoggerLevel.ALL); + logger.addLogger(LOGGER, new Slf4jLogger(org.slf4j.LoggerFactory.getLogger(LOGGER))); } @Test public void testInit() { expectedEx.expect(ClassCastException.class); expectedEx.expectMessage("cannot be cast"); - logbackLoggerProcessUnit.init(); + logger.init(); } @Test - public void testGetLoggerLevelInfo() { + @PrepareForTest({LoggerFactory.class}) + public void testInitSuccess() { + mockStatic(LoggerFactory.class); + when(LoggerFactory.getILoggerFactory()).thenReturn(new LoggerContext()); + logger.init(); + } + + @Test + public void testGetLoggerLevelInfoByError() { expectedEx.expect(ClassCastException.class); expectedEx.expectMessage("cannot be cast"); - Assert.assertNotNull(logbackLoggerProcessUnit.getLoggerLevelInfo()); + Assert.assertNotNull(logger.getLoggerLevelInfo()); + } + + @Test + public void testGetLoggerLevelInfo() { + addLoggerToUnit(); + List info = logger.getLoggerLevelInfo(); + Assert.assertNotNull(info); + } + + @Test + public void testSetLogger() { + addLoggerToUnit(); + String loggerLevel = logger.setLoggerLevel(LOGGER, LoggerLevel.ALL); + Assert.assertEquals(LOGGER_DEBUG, loggerLevel); + } + + private void addLoggerToUnit() { + LoggerContext loggerContext = new LoggerContext(); + Logger logger = loggerContext.getLogger(LOGGER_ROOT); + this.logger.addLogger(LOGGER, logger); } } \ No newline at end of file diff --git a/trpc-logger/trpc-logger-admin/src/test/java/com/tencent/trpc/logger/admin/LoggerProcessUnitFactoryTest.java b/trpc-logger/trpc-logger-admin/src/test/java/com/tencent/trpc/logger/admin/LoggerProcessUnitFactoryTest.java new file mode 100644 index 0000000000..1a15cb5b32 --- /dev/null +++ b/trpc-logger/trpc-logger-admin/src/test/java/com/tencent/trpc/logger/admin/LoggerProcessUnitFactoryTest.java @@ -0,0 +1,52 @@ +package com.tencent.trpc.logger.admin; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import ch.qos.logback.classic.LoggerContext; +import org.apache.logging.slf4j.Log4jLoggerFactory; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.slf4j.LoggerFactory; +import org.slf4j.helpers.NOPLoggerFactory; + +@RunWith(PowerMockRunner.class) +public class LoggerProcessUnitFactoryTest { + + @Test + @PrepareForTest({LoggerFactory.class}) + public void testGetGetLoggerProcessUnit() { + PowerMockito.mockStatic(LoggerFactory.class); + PowerMockito.when(LoggerFactory.getILoggerFactory()).thenReturn(new Log4jLoggerFactory()); + LoggerProcessUnit log = LoggerProcessUnitFactory.getLoggerProcessUnit(); + assertNotNull(log); + assertTrue(log instanceof Log4j2LoggerProcessUnit); + } + + + @Test + @PrepareForTest({LoggerFactory.class}) + public void testLogback() { + PowerMockito.mockStatic(LoggerFactory.class); + PowerMockito.when(LoggerFactory.getILoggerFactory()).thenReturn(new LoggerContext()); + LoggerProcessUnit log = LoggerProcessUnitFactory.getLoggerProcessUnit(); + assertNotNull(log); + assertTrue(log instanceof LogbackLoggerProcessUnit); + log = LoggerProcessUnitFactory.getLoggerProcessUnit(); + assertNotNull(log); + } + + @Test + @PrepareForTest({LoggerFactory.class}) + public void testUnSupport() { + PowerMockito.mockStatic(LoggerFactory.class); + PowerMockito.when(LoggerFactory.getILoggerFactory()).thenReturn(new NOPLoggerFactory()); + LoggerProcessUnit log = LoggerProcessUnitFactory.getLoggerProcessUnit(); + assertNotNull(log); + assertTrue(log instanceof UnSupportLoggerProcessUnit); + } + +} \ No newline at end of file