[ISSUE #5806]Add unit tests for package com.alibaba.nacos.client.logging in nacos (#6137)

* add ut for package c.a.nacos.client.logging

* fix Log4J2NacosLoggingTest
This commit is contained in:
shalk(xiao kun) 2021-06-25 18:00:06 +08:00 committed by GitHub
parent 041983269c
commit 9e516839ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 176 additions and 0 deletions

View File

@ -0,0 +1,39 @@
/*
*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.alibaba.nacos.client.logging;
import org.junit.Assert;
import org.junit.Test;
public class AbstractNacosLoggingTest {
@Test
public void testGetLocation() {
AbstractNacosLogging logging = new AbstractNacosLogging() {
@Override
public void loadConfiguration() {
}
};
String defaultLocation = "aa";
String actual = logging.getLocation(defaultLocation);
Assert.assertEquals(defaultLocation, actual);
}
}

View File

@ -0,0 +1,45 @@
/*
*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.alibaba.nacos.client.logging;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import java.lang.reflect.Field;
public class NacosLoggingTest {
@Test
public void testGetInstance() {
NacosLogging instance = NacosLogging.getInstance();
Assert.assertNotNull(instance);
}
@Test
public void testLoadConfiguration() throws NoSuchFieldException, IllegalAccessException {
NacosLogging instance = NacosLogging.getInstance();
AbstractNacosLogging mockLogging = Mockito.mock(AbstractNacosLogging.class);
Field nacosLogging = NacosLogging.class.getDeclaredField("nacosLogging");
nacosLogging.setAccessible(true);
nacosLogging.set(instance, mockLogging);
instance.loadConfiguration();
Mockito.verify(mockLogging, Mockito.times(1)).loadConfiguration();
}
}

View File

@ -0,0 +1,53 @@
/*
*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.alibaba.nacos.client.logging.log4j2;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.junit.Assert;
import org.junit.Test;
import java.util.Map;
public class Log4J2NacosLoggingTest {
private static final String NACOS_LOGGER_PREFIX = "com.alibaba.nacos";
@Test
public void testLoadConfiguration() {
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
Configuration contextConfiguration = loggerContext.getConfiguration();
Assert.assertEquals(0, contextConfiguration.getLoggers().size());
Log4J2NacosLogging log4J2NacosLogging = new Log4J2NacosLogging();
//when
log4J2NacosLogging.loadConfiguration();
//then
loggerContext = (LoggerContext) LogManager.getContext(false);
contextConfiguration = loggerContext.getConfiguration();
Map<String, LoggerConfig> nacosClientLoggers = contextConfiguration.getLoggers();
Assert.assertEquals(4, nacosClientLoggers.size());
for (Map.Entry<String, LoggerConfig> loggerEntry : nacosClientLoggers.entrySet()) {
String loggerName = loggerEntry.getKey();
Assert.assertTrue(loggerName.startsWith(NACOS_LOGGER_PREFIX));
}
}
}

View File

@ -0,0 +1,39 @@
/*
*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.alibaba.nacos.client.logging.logback;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.hamcrest.CoreMatchers.isA;
public class LogbackNacosLoggingTest {
@Rule
public ExpectedException exceptionRule = ExpectedException.none();
@Test
public void testLoadConfiguration() {
exceptionRule.expectCause(isA(ClassCastException.class));
exceptionRule.expectMessage("Could not initialize Logback Nacos logging from classpath:nacos-logback.xml");
LogbackNacosLogging logbackNacosLogging = new LogbackNacosLogging();
logbackNacosLogging.loadConfiguration();
}
}