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

View File

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

View File

@ -17,19 +17,20 @@
package com.alibaba.nacos.logger.adapter.log4j2; package com.alibaba.nacos.logger.adapter.log4j2;
import com.alibaba.nacos.common.logging.NacosLoggingProperties; import com.alibaba.nacos.common.logging.NacosLoggingProperties;
import org.junit.Assert; import org.junit.jupiter.api.Test;
import org.junit.Test;
public class NacosClientPropertiesLookupTest { import static org.junit.jupiter.api.Assertions.assertEquals;
class NacosClientPropertiesLookupTest {
@Test @Test
public void testLookUp() { void testLookUp() {
System.setProperty("test.nacos.logging.lookup", "true"); System.setProperty("test.nacos.logging.lookup", "true");
NacosLoggingProperties properties = new NacosLoggingProperties("", System.getProperties()); NacosLoggingProperties properties = new NacosLoggingProperties("", System.getProperties());
Log4j2NacosLoggingPropertiesHolder.setProperties(properties); Log4j2NacosLoggingPropertiesHolder.setProperties(properties);
NacosClientPropertiesLookup nacosClientPropertiesLookup = new NacosClientPropertiesLookup(); NacosClientPropertiesLookup nacosClientPropertiesLookup = new NacosClientPropertiesLookup();
final String actual = nacosClientPropertiesLookup.lookup("test.nacos.logging.lookup"); 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; package com.alibaba.nacos.logger.adapter.logback12;
import com.alibaba.nacos.common.logging.NacosLoggingAdapter; 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.jupiter.api.Assertions.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class LogbackNacosLoggingAdapterBuilderTest { class LogbackNacosLoggingAdapterBuilderTest {
@Test @Test
public void build() { void build() {
LogbackNacosLoggingAdapterBuilder builder = new LogbackNacosLoggingAdapterBuilder(); LogbackNacosLoggingAdapterBuilder builder = new LogbackNacosLoggingAdapterBuilder();
NacosLoggingAdapter adapter = builder.build(); NacosLoggingAdapter adapter = builder.build();
assertNotNull(adapter); 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.classic.spi.LoggerContextListener;
import ch.qos.logback.core.CoreConstants; import ch.qos.logback.core.CoreConstants;
import com.alibaba.nacos.common.logging.NacosLoggingProperties; import com.alibaba.nacos.common.logging.NacosLoggingProperties;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.ILoggerFactory; import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.slf4j.impl.StaticLoggerBinder; import org.slf4j.impl.StaticLoggerBinder;
@ -35,14 +35,15 @@ import org.slf4j.impl.StaticLoggerBinder;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Properties; import java.util.Properties;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.Assert.assertTrue; 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.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public class LogbackNacosLoggingAdapterTest { public class LogbackNacosLoggingAdapterTest {
LogbackNacosLoggingAdapter logbackNacosLoggingAdapter; LogbackNacosLoggingAdapter logbackNacosLoggingAdapter;
@ -54,8 +55,8 @@ public class LogbackNacosLoggingAdapterTest {
NacosLoggingProperties loggingProperties; NacosLoggingProperties loggingProperties;
@Before @BeforeEach
public void setUp() throws NoSuchFieldException, IllegalAccessException { void setUp() throws NoSuchFieldException, IllegalAccessException {
logbackNacosLoggingAdapter = new LogbackNacosLoggingAdapter(); logbackNacosLoggingAdapter = new LogbackNacosLoggingAdapter();
ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory(); ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
if (loggerFactory instanceof LoggerContext) { if (loggerFactory instanceof LoggerContext) {
@ -70,8 +71,8 @@ public class LogbackNacosLoggingAdapterTest {
loggingProperties = new NacosLoggingProperties("classpath:nacos-logback12.xml", new Properties()); loggingProperties = new NacosLoggingProperties("classpath:nacos-logback12.xml", new Properties());
} }
@After @AfterEach
public void tearDown() throws NoSuchFieldException, IllegalAccessException { void tearDown() throws NoSuchFieldException, IllegalAccessException {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.removeListener(loggerContextListener); loggerContext.removeListener(loggerContextListener);
if (null != cachedLoggerFactory) { if (null != cachedLoggerFactory) {
@ -89,7 +90,7 @@ public class LogbackNacosLoggingAdapterTest {
} }
@Test @Test
public void testLoadConfigurationSuccess() { void testLoadConfigurationSuccess() {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.putObject(CoreConstants.RECONFIGURE_ON_CHANGE_TASK, new ReconfigureOnChangeTask()); loggerContext.putObject(CoreConstants.RECONFIGURE_ON_CHANGE_TASK, new ReconfigureOnChangeTask());
logbackNacosLoggingAdapter.loadConfiguration(loggingProperties); logbackNacosLoggingAdapter.loadConfiguration(loggingProperties);
@ -122,30 +123,32 @@ public class LogbackNacosLoggingAdapterTest {
} }
@Test @Test
public void testIsAdaptedLogger() { void testIsAdaptedLogger() {
assertTrue(logbackNacosLoggingAdapter.isAdaptedLogger(Logger.class)); assertTrue(logbackNacosLoggingAdapter.isAdaptedLogger(Logger.class));
assertFalse(logbackNacosLoggingAdapter.isAdaptedLogger(java.util.logging.Logger.class)); assertFalse(logbackNacosLoggingAdapter.isAdaptedLogger(java.util.logging.Logger.class));
} }
@Test(expected = IllegalStateException.class) @Test
public void testLoadConfigurationFailure() { void testLoadConfigurationFailure() {
assertThrows(IllegalStateException.class, () -> {
System.setProperty("nacos.logging.config", "http://localhost"); System.setProperty("nacos.logging.config", "http://localhost");
loggingProperties = new NacosLoggingProperties("classpath:nacos-logback12.xml", System.getProperties()); loggingProperties = new NacosLoggingProperties("classpath:nacos-logback12.xml", System.getProperties());
logbackNacosLoggingAdapter.loadConfiguration(loggingProperties); logbackNacosLoggingAdapter.loadConfiguration(loggingProperties);
});
} }
@Test @Test
public void testIsNeedReloadConfiguration() { void testIsNeedReloadConfiguration() {
assertFalse(logbackNacosLoggingAdapter.isNeedReloadConfiguration()); assertFalse(logbackNacosLoggingAdapter.isNeedReloadConfiguration());
} }
@Test @Test
public void testGetDefaultConfigLocation() { void testGetDefaultConfigLocation() {
assertEquals("classpath:nacos-logback12.xml", logbackNacosLoggingAdapter.getDefaultConfigLocation()); assertEquals("classpath:nacos-logback12.xml", logbackNacosLoggingAdapter.getDefaultConfigLocation());
} }
@Test @Test
public void testLoadConfigurationReload() { void testLoadConfigurationReload() {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.putObject(CoreConstants.RECONFIGURE_ON_CHANGE_TASK, new ReconfigureOnChangeTask()); loggerContext.putObject(CoreConstants.RECONFIGURE_ON_CHANGE_TASK, new ReconfigureOnChangeTask());
logbackNacosLoggingAdapter.loadConfiguration(loggingProperties); logbackNacosLoggingAdapter.loadConfiguration(loggingProperties);
@ -160,7 +163,7 @@ public class LogbackNacosLoggingAdapterTest {
} }
@Test @Test
public void testLoadConfigurationStart() { void testLoadConfigurationStart() {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.putObject(CoreConstants.RECONFIGURE_ON_CHANGE_TASK, new ReconfigureOnChangeTask()); loggerContext.putObject(CoreConstants.RECONFIGURE_ON_CHANGE_TASK, new ReconfigureOnChangeTask());
logbackNacosLoggingAdapter.loadConfiguration(loggingProperties); logbackNacosLoggingAdapter.loadConfiguration(loggingProperties);
@ -175,7 +178,7 @@ public class LogbackNacosLoggingAdapterTest {
} }
@Test @Test
public void testLoadConfigurationStop() { void testLoadConfigurationStop() {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.putObject(CoreConstants.RECONFIGURE_ON_CHANGE_TASK, new ReconfigureOnChangeTask()); loggerContext.putObject(CoreConstants.RECONFIGURE_ON_CHANGE_TASK, new ReconfigureOnChangeTask());
logbackNacosLoggingAdapter.loadConfiguration(loggingProperties); 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.ErrorStatus;
import ch.qos.logback.core.status.Status; import ch.qos.logback.core.status.Status;
import com.alibaba.nacos.common.logging.NacosLoggingProperties; import com.alibaba.nacos.common.logging.NacosLoggingProperties;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.xml.sax.Attributes; import org.xml.sax.Attributes;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class NacosClientPropertyActionTest { class NacosClientPropertyActionTest {
ContextBase context; ContextBase context;
@ -42,20 +42,20 @@ public class NacosClientPropertyActionTest {
private Properties properties; private Properties properties;
@Before @BeforeEach
public void setUp() throws Exception { void setUp() throws Exception {
context = new ContextBase(); context = new ContextBase();
properties = new Properties(); properties = new Properties();
loggingProperties = new NacosLoggingProperties("classpath:test.xml", properties); loggingProperties = new NacosLoggingProperties("classpath:test.xml", properties);
} }
@After @AfterEach
public void tearDown() throws Exception { void tearDown() throws Exception {
context.stop(); context.stop();
} }
@Test @Test
public void testLookUpVar() throws ActionException { void testLookUpVar() throws ActionException {
properties.setProperty("test.nacos.logging.action.lookup", "true"); properties.setProperty("test.nacos.logging.action.lookup", "true");
@ -78,7 +78,7 @@ public class NacosClientPropertyActionTest {
} }
@Test @Test
public void testBeginWithoutName() throws ActionException { void testBeginWithoutName() throws ActionException {
final InterpretationContext interpretationContext = new InterpretationContext(context, null); final InterpretationContext interpretationContext = new InterpretationContext(context, null);
final Attributes mockAttr = Mockito.mock(AttributesForTest.class); final Attributes mockAttr = Mockito.mock(AttributesForTest.class);
Mockito.when(mockAttr.getValue(Mockito.eq("name"))).thenReturn(""); Mockito.when(mockAttr.getValue(Mockito.eq("name"))).thenReturn("");
@ -91,8 +91,7 @@ public class NacosClientPropertyActionTest {
List<Status> statusList = context.getStatusManager().getCopyOfStatusList(); List<Status> statusList = context.getStatusManager().getCopyOfStatusList();
assertEquals(1, statusList.size()); assertEquals(1, statusList.size());
assertTrue(statusList.get(0) instanceof ErrorStatus); assertTrue(statusList.get(0) instanceof ErrorStatus);
assertEquals("The \"name\" and \"source\" attributes of <nacosClientProperty> must be set", assertEquals("The \"name\" and \"source\" attributes of <nacosClientProperty> must be set", statusList.get(0).getMessage());
statusList.get(0).getMessage());
} }
static class AttributesForTest implements Attributes { 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.joran.spi.JoranException;
import ch.qos.logback.core.status.ErrorStatus; import ch.qos.logback.core.status.ErrorStatus;
import ch.qos.logback.core.status.Status; import ch.qos.logback.core.status.Status;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -33,14 +33,18 @@ import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.List; import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public class NacosLogbackConfiguratorAdapterV1Test { class NacosLogbackConfiguratorAdapterV1Test {
ContextBase context;
NacosLogbackConfiguratorAdapterV1 nacosLogbackConfiguratorAdapter;
@Mock @Mock
private URL url; private URL url;
@ -51,12 +55,8 @@ public class NacosLogbackConfiguratorAdapterV1Test {
@Mock @Mock
private InputStream inputStream; private InputStream inputStream;
ContextBase context; @BeforeEach
void setUp() throws Exception {
NacosLogbackConfiguratorAdapterV1 nacosLogbackConfiguratorAdapter;
@Before
public void setUp() throws Exception {
nacosLogbackConfiguratorAdapter = new NacosLogbackConfiguratorAdapterV1(); nacosLogbackConfiguratorAdapter = new NacosLogbackConfiguratorAdapterV1();
context = new ContextBase(); context = new ContextBase();
nacosLogbackConfiguratorAdapter.setContext(context); nacosLogbackConfiguratorAdapter.setContext(context);
@ -64,13 +64,13 @@ public class NacosLogbackConfiguratorAdapterV1Test {
when(urlConnection.getInputStream()).thenReturn(inputStream); when(urlConnection.getInputStream()).thenReturn(inputStream);
} }
@After @AfterEach
public void tearDown() throws Exception { void tearDown() throws Exception {
context.stop(); context.stop();
} }
@Test @Test
public void testConfigureWithError() throws Exception { void testConfigureWithError() throws Exception {
doThrow(new IOException("test")).when(inputStream).close(); doThrow(new IOException("test")).when(inputStream).close();
try { try {
nacosLogbackConfiguratorAdapter.configure(url); nacosLogbackConfiguratorAdapter.configure(url);