Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add logger unit test #33

Merged
merged 6 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 {
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 {

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());
honlyc marked this conversation as resolved.
Show resolved Hide resolved
String logger = log4j2LoggerProcessUnit.setLoggerLevel("unit-test", LoggerLevel.ALL);
Assert.assertEquals(logger, "ERROR");
}

}
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -11,14 +11,27 @@

package com.tencent.trpc.logger.admin;

import ch.qos.logback.classic.Level;
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;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
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;

honlyc marked this conversation as resolved.
Show resolved Hide resolved
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@RunWith(PowerMockRunner.class)
public class LogbackLoggerProcessUnitTest {

@Rule
Expand All @@ -41,10 +54,47 @@ public void testInit() {
}

@Test
public void testGetLoggerLevelInfo() {
@PrepareForTest({LoggerFactory.class})
public void testInitSuccess() {
PowerMockito.mockStatic(LoggerFactory.class);
PowerMockito.when(LoggerFactory.getILoggerFactory()).thenReturn(new LoggerContext());
logbackLoggerProcessUnit.init();
honlyc marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void testGetLoggerLevelInfoByError() {
expectedEx.expect(ClassCastException.class);
expectedEx.expectMessage("cannot be cast");
Assert.assertNotNull(logbackLoggerProcessUnit.getLoggerLevelInfo());
}

@Test
public void testGetLoggerLevelInfo() {
addLoggerToUnit();
List<LoggerLevelInfo> info = logbackLoggerProcessUnit.getLoggerLevelInfo();
Assert.assertNotNull(info);
}

@Test
public void testSetLogger() {
addLoggerToUnit();
String loggerLevel = logbackLoggerProcessUnit.setLoggerLevel("logger", LoggerLevel.ALL);
Assert.assertEquals("ALL", loggerLevel);
}

private void addLoggerToUnit() {
try {
Class<?> loggerClass = Class.forName("ch.qos.logback.classic.Logger");
Constructor<?> constructor = loggerClass.getDeclaredConstructor(String.class, Logger.class,
LoggerContext.class);
constructor.setAccessible(true);
honlyc marked this conversation as resolved.
Show resolved Hide resolved
Logger logger = (Logger) constructor.newInstance("logger", null, new LoggerContext());
logger.setLevel(Level.ALL);
logbackLoggerProcessUnit.addLogger("logger", logger);
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException
| InvocationTargetException e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.tencent.trpc.logger.admin;

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;

import static org.junit.Assert.assertNotNull;
honlyc marked this conversation as resolved.
Show resolved Hide resolved
import static org.junit.Assert.assertTrue;

honlyc marked this conversation as resolved.
Show resolved Hide resolved
@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);
}

}
Loading