upgrade module naocs-log from junit4 to junit5 (#12156)

This commit is contained in:
shalk(xiao kun) 2024-05-31 11:50:03 +08:00 committed by GitHub
parent 25155e480a
commit 67da6d1b5f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 110 additions and 107 deletions

View File

@ -22,13 +22,12 @@ import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import java.beans.PropertyChangeListener;
import java.io.IOException;
@ -39,17 +38,18 @@ import java.net.URL;
import java.util.Map;
import java.util.logging.Logger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class Log4J2NacosLoggingAdapterTest {
@ExtendWith(MockitoExtension.class)
class Log4J2NacosLoggingAdapterTest {
private static final String NACOS_LOGGER_PREFIX = "com.alibaba.nacos";
@ -60,16 +60,16 @@ public class Log4J2NacosLoggingAdapterTest {
Log4J2NacosLoggingAdapter log4J2NacosLoggingAdapter;
@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
log4J2NacosLoggingAdapter = new Log4J2NacosLoggingAdapter();
nacosLoggingProperties = new NacosLoggingProperties("classpath:nacos-log4j2.xml", System.getProperties());
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
loggerContext.addPropertyChangeListener(propertyChangeListener);
}
@After
public void tearDown() throws Exception {
@AfterEach
void tearDown() throws Exception {
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
loggerContext.removePropertyChangeListener(propertyChangeListener);
loggerContext.setConfigLocation(loggerContext.getConfigLocation());
@ -78,25 +78,25 @@ public class Log4J2NacosLoggingAdapterTest {
}
@Test
public void testIsAdaptedLogger() {
void testIsAdaptedLogger() {
assertTrue(log4J2NacosLoggingAdapter.isAdaptedLogger(org.apache.logging.slf4j.Log4jLogger.class));
assertFalse(log4J2NacosLoggingAdapter.isAdaptedLogger(Logger.class));
}
@Test
public void testIsNeedReloadConfiguration() {
void testIsNeedReloadConfiguration() {
assertTrue(log4J2NacosLoggingAdapter.isNeedReloadConfiguration());
log4J2NacosLoggingAdapter.loadConfiguration(nacosLoggingProperties);
assertFalse(log4J2NacosLoggingAdapter.isNeedReloadConfiguration());
}
@Test
public void testGetDefaultConfigLocation() {
void testGetDefaultConfigLocation() {
assertEquals("classpath:nacos-log4j2.xml", log4J2NacosLoggingAdapter.getDefaultConfigLocation());
}
@Test
public void testLoadConfiguration() {
void testLoadConfiguration() {
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
Configuration contextConfiguration = loggerContext.getConfiguration();
assertEquals(0, contextConfiguration.getLoggers().size());
@ -109,12 +109,12 @@ public class Log4J2NacosLoggingAdapterTest {
assertEquals(6, nacosClientLoggers.size());
for (Map.Entry<String, LoggerConfig> loggerEntry : nacosClientLoggers.entrySet()) {
String loggerName = loggerEntry.getKey();
Assert.assertTrue(loggerName.startsWith(NACOS_LOGGER_PREFIX));
assertTrue(loggerName.startsWith(NACOS_LOGGER_PREFIX));
}
}
@Test
public void testLoadConfigurationWithoutLocation() {
void testLoadConfigurationWithoutLocation() {
System.setProperty("nacos.logging.default.config.enabled", "false");
nacosLoggingProperties = new NacosLoggingProperties("classpath:nacos-log4j2.xml", System.getProperties());
log4J2NacosLoggingAdapter = new Log4J2NacosLoggingAdapter();
@ -122,27 +122,27 @@ public class Log4J2NacosLoggingAdapterTest {
verify(propertyChangeListener, never()).propertyChange(any());
}
@Test(expected = IllegalStateException.class)
public void testLoadConfigurationWithWrongLocation() {
System.setProperty("nacos.logging.config", "http://localhost");
nacosLoggingProperties = new NacosLoggingProperties("classpath:nacos-log4j2.xml", System.getProperties());
log4J2NacosLoggingAdapter = new Log4J2NacosLoggingAdapter();
log4J2NacosLoggingAdapter.loadConfiguration(nacosLoggingProperties);
verify(propertyChangeListener, never()).propertyChange(any());
@Test
void testLoadConfigurationWithWrongLocation() {
assertThrows(IllegalStateException.class, () -> {
System.setProperty("nacos.logging.config", "http://localhost");
nacosLoggingProperties = new NacosLoggingProperties("classpath:nacos-log4j2.xml", System.getProperties());
log4J2NacosLoggingAdapter = new Log4J2NacosLoggingAdapter();
log4J2NacosLoggingAdapter.loadConfiguration(nacosLoggingProperties);
verify(propertyChangeListener, never()).propertyChange(any());
});
}
@Test
public void testGetConfigurationSourceForNonFileProtocol()
void testGetConfigurationSourceForNonFileProtocol()
throws NoSuchMethodException, IOException, InvocationTargetException, IllegalAccessException {
Method getConfigurationSourceMethod = Log4J2NacosLoggingAdapter.class
.getDeclaredMethod("getConfigurationSource", URL.class);
Method getConfigurationSourceMethod = Log4J2NacosLoggingAdapter.class.getDeclaredMethod("getConfigurationSource", URL.class);
getConfigurationSourceMethod.setAccessible(true);
URL url = mock(URL.class);
InputStream inputStream = mock(InputStream.class);
when(url.openStream()).thenReturn(inputStream);
when(url.getProtocol()).thenReturn("http");
ConfigurationSource actual = (ConfigurationSource) getConfigurationSourceMethod
.invoke(log4J2NacosLoggingAdapter, url);
ConfigurationSource actual = (ConfigurationSource) getConfigurationSourceMethod.invoke(log4J2NacosLoggingAdapter, url);
assertEquals(inputStream, actual.getInputStream());
assertEquals(url, actual.getURL());
}

View File

@ -17,15 +17,15 @@
package com.alibaba.nacos.logger.adapter.log4j2;
import com.alibaba.nacos.common.logging.NacosLoggingAdapter;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class Log4j2NacosLoggingAdapterBuilderTest {
class Log4j2NacosLoggingAdapterBuilderTest {
@Test
public void build() {
void build() {
Log4j2NacosLoggingAdapterBuilder builder = new Log4j2NacosLoggingAdapterBuilder();
NacosLoggingAdapter adapter = builder.build();
assertNotNull(adapter);

View File

@ -17,19 +17,20 @@
package com.alibaba.nacos.logger.adapter.log4j2;
import com.alibaba.nacos.common.logging.NacosLoggingProperties;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
public class NacosClientPropertiesLookupTest {
import static org.junit.jupiter.api.Assertions.assertEquals;
class NacosClientPropertiesLookupTest {
@Test
public void testLookUp() {
void testLookUp() {
System.setProperty("test.nacos.logging.lookup", "true");
NacosLoggingProperties properties = new NacosLoggingProperties("", System.getProperties());
Log4j2NacosLoggingPropertiesHolder.setProperties(properties);
NacosClientPropertiesLookup nacosClientPropertiesLookup = new NacosClientPropertiesLookup();
final String actual = nacosClientPropertiesLookup.lookup("test.nacos.logging.lookup");
Assert.assertEquals("true", actual);
assertEquals("true", actual);
}
}

View File

@ -17,15 +17,15 @@
package com.alibaba.nacos.logger.adapter.logback12;
import com.alibaba.nacos.common.logging.NacosLoggingAdapter;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class LogbackNacosLoggingAdapterBuilderTest {
class LogbackNacosLoggingAdapterBuilderTest {
@Test
public void build() {
void build() {
LogbackNacosLoggingAdapterBuilder builder = new LogbackNacosLoggingAdapterBuilder();
NacosLoggingAdapter adapter = builder.build();
assertNotNull(adapter);

View File

@ -22,12 +22,12 @@ import ch.qos.logback.classic.joran.ReconfigureOnChangeTask;
import ch.qos.logback.classic.spi.LoggerContextListener;
import ch.qos.logback.core.CoreConstants;
import com.alibaba.nacos.common.logging.NacosLoggingProperties;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;
import org.slf4j.impl.StaticLoggerBinder;
@ -35,14 +35,15 @@ import org.slf4j.impl.StaticLoggerBinder;
import java.lang.reflect.Field;
import java.util.Properties;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class LogbackNacosLoggingAdapterTest {
LogbackNacosLoggingAdapter logbackNacosLoggingAdapter;
@ -54,8 +55,8 @@ public class LogbackNacosLoggingAdapterTest {
NacosLoggingProperties loggingProperties;
@Before
public void setUp() throws NoSuchFieldException, IllegalAccessException {
@BeforeEach
void setUp() throws NoSuchFieldException, IllegalAccessException {
logbackNacosLoggingAdapter = new LogbackNacosLoggingAdapter();
ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
if (loggerFactory instanceof LoggerContext) {
@ -70,8 +71,8 @@ public class LogbackNacosLoggingAdapterTest {
loggingProperties = new NacosLoggingProperties("classpath:nacos-logback12.xml", new Properties());
}
@After
public void tearDown() throws NoSuchFieldException, IllegalAccessException {
@AfterEach
void tearDown() throws NoSuchFieldException, IllegalAccessException {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.removeListener(loggerContextListener);
if (null != cachedLoggerFactory) {
@ -89,7 +90,7 @@ public class LogbackNacosLoggingAdapterTest {
}
@Test
public void testLoadConfigurationSuccess() {
void testLoadConfigurationSuccess() {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.putObject(CoreConstants.RECONFIGURE_ON_CHANGE_TASK, new ReconfigureOnChangeTask());
logbackNacosLoggingAdapter.loadConfiguration(loggingProperties);
@ -122,30 +123,32 @@ public class LogbackNacosLoggingAdapterTest {
}
@Test
public void testIsAdaptedLogger() {
void testIsAdaptedLogger() {
assertTrue(logbackNacosLoggingAdapter.isAdaptedLogger(Logger.class));
assertFalse(logbackNacosLoggingAdapter.isAdaptedLogger(java.util.logging.Logger.class));
}
@Test(expected = IllegalStateException.class)
public void testLoadConfigurationFailure() {
System.setProperty("nacos.logging.config", "http://localhost");
loggingProperties = new NacosLoggingProperties("classpath:nacos-logback12.xml", System.getProperties());
logbackNacosLoggingAdapter.loadConfiguration(loggingProperties);
@Test
void testLoadConfigurationFailure() {
assertThrows(IllegalStateException.class, () -> {
System.setProperty("nacos.logging.config", "http://localhost");
loggingProperties = new NacosLoggingProperties("classpath:nacos-logback12.xml", System.getProperties());
logbackNacosLoggingAdapter.loadConfiguration(loggingProperties);
});
}
@Test
public void testIsNeedReloadConfiguration() {
void testIsNeedReloadConfiguration() {
assertFalse(logbackNacosLoggingAdapter.isNeedReloadConfiguration());
}
@Test
public void testGetDefaultConfigLocation() {
void testGetDefaultConfigLocation() {
assertEquals("classpath:nacos-logback12.xml", logbackNacosLoggingAdapter.getDefaultConfigLocation());
}
@Test
public void testLoadConfigurationReload() {
void testLoadConfigurationReload() {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.putObject(CoreConstants.RECONFIGURE_ON_CHANGE_TASK, new ReconfigureOnChangeTask());
logbackNacosLoggingAdapter.loadConfiguration(loggingProperties);
@ -160,7 +163,7 @@ public class LogbackNacosLoggingAdapterTest {
}
@Test
public void testLoadConfigurationStart() {
void testLoadConfigurationStart() {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.putObject(CoreConstants.RECONFIGURE_ON_CHANGE_TASK, new ReconfigureOnChangeTask());
logbackNacosLoggingAdapter.loadConfiguration(loggingProperties);
@ -175,7 +178,7 @@ public class LogbackNacosLoggingAdapterTest {
}
@Test
public void testLoadConfigurationStop() {
void testLoadConfigurationStop() {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.putObject(CoreConstants.RECONFIGURE_ON_CHANGE_TASK, new ReconfigureOnChangeTask());
logbackNacosLoggingAdapter.loadConfiguration(loggingProperties);

View File

@ -22,19 +22,19 @@ import ch.qos.logback.core.joran.spi.InterpretationContext;
import ch.qos.logback.core.status.ErrorStatus;
import ch.qos.logback.core.status.Status;
import com.alibaba.nacos.common.logging.NacosLoggingProperties;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.xml.sax.Attributes;
import java.util.List;
import java.util.Properties;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class NacosClientPropertyActionTest {
class NacosClientPropertyActionTest {
ContextBase context;
@ -42,20 +42,20 @@ public class NacosClientPropertyActionTest {
private Properties properties;
@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
context = new ContextBase();
properties = new Properties();
loggingProperties = new NacosLoggingProperties("classpath:test.xml", properties);
}
@After
public void tearDown() throws Exception {
@AfterEach
void tearDown() throws Exception {
context.stop();
}
@Test
public void testLookUpVar() throws ActionException {
void testLookUpVar() throws ActionException {
properties.setProperty("test.nacos.logging.action.lookup", "true");
@ -78,7 +78,7 @@ public class NacosClientPropertyActionTest {
}
@Test
public void testBeginWithoutName() throws ActionException {
void testBeginWithoutName() throws ActionException {
final InterpretationContext interpretationContext = new InterpretationContext(context, null);
final Attributes mockAttr = Mockito.mock(AttributesForTest.class);
Mockito.when(mockAttr.getValue(Mockito.eq("name"))).thenReturn("");
@ -91,8 +91,7 @@ public class NacosClientPropertyActionTest {
List<Status> statusList = context.getStatusManager().getCopyOfStatusList();
assertEquals(1, statusList.size());
assertTrue(statusList.get(0) instanceof ErrorStatus);
assertEquals("The \"name\" and \"source\" attributes of <nacosClientProperty> must be set",
statusList.get(0).getMessage());
assertEquals("The \"name\" and \"source\" attributes of <nacosClientProperty> must be set", statusList.get(0).getMessage());
}
static class AttributesForTest implements Attributes {

View File

@ -20,12 +20,12 @@ import ch.qos.logback.core.ContextBase;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.status.ErrorStatus;
import ch.qos.logback.core.status.Status;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import java.io.IOException;
import java.io.InputStream;
@ -33,14 +33,18 @@ import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class NacosLogbackConfiguratorAdapterV1Test {
@ExtendWith(MockitoExtension.class)
class NacosLogbackConfiguratorAdapterV1Test {
ContextBase context;
NacosLogbackConfiguratorAdapterV1 nacosLogbackConfiguratorAdapter;
@Mock
private URL url;
@ -51,12 +55,8 @@ public class NacosLogbackConfiguratorAdapterV1Test {
@Mock
private InputStream inputStream;
ContextBase context;
NacosLogbackConfiguratorAdapterV1 nacosLogbackConfiguratorAdapter;
@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
nacosLogbackConfiguratorAdapter = new NacosLogbackConfiguratorAdapterV1();
context = new ContextBase();
nacosLogbackConfiguratorAdapter.setContext(context);
@ -64,13 +64,13 @@ public class NacosLogbackConfiguratorAdapterV1Test {
when(urlConnection.getInputStream()).thenReturn(inputStream);
}
@After
public void tearDown() throws Exception {
@AfterEach
void tearDown() throws Exception {
context.stop();
}
@Test
public void testConfigureWithError() throws Exception {
void testConfigureWithError() throws Exception {
doThrow(new IOException("test")).when(inputStream).close();
try {
nacosLogbackConfiguratorAdapter.configure(url);