diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/LogHelperTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/LogHelperTest.java new file mode 100644 index 00000000000..b3ba78321b2 --- /dev/null +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/LogHelperTest.java @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.dubbo.common.utils; + +import com.alibaba.dubbo.common.logger.Logger; +import org.junit.Test; +import org.mockito.Mockito; + +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class LogHelperTest { + + @Test + public void testTrace() throws Exception { + Logger logger = Mockito.mock(Logger.class); + when(logger.isTraceEnabled()).thenReturn(true); + LogHelper.trace(logger, "trace"); + verify(logger).trace("trace"); + Throwable t = new RuntimeException(); + LogHelper.trace(logger, t); + verify(logger).trace(t); + LogHelper.trace(logger, "trace", t); + verify(logger).trace("trace", t); + } + + @Test + public void testDebug() throws Exception { + Logger logger = Mockito.mock(Logger.class); + when(logger.isDebugEnabled()).thenReturn(true); + LogHelper.debug(logger, "debug"); + verify(logger).debug("debug"); + Throwable t = new RuntimeException(); + LogHelper.debug(logger, t); + verify(logger).debug(t); + LogHelper.debug(logger, "debug", t); + verify(logger).debug("debug", t); + } + + @Test + public void testInfo() throws Exception { + Logger logger = Mockito.mock(Logger.class); + when(logger.isInfoEnabled()).thenReturn(true); + LogHelper.info(logger, "info"); + verify(logger).info("info"); + Throwable t = new RuntimeException(); + LogHelper.info(logger, t); + verify(logger).info(t); + LogHelper.info(logger, "info", t); + verify(logger).info("info", t); + } + + @Test + public void testWarn() throws Exception { + Logger logger = Mockito.mock(Logger.class); + when(logger.isWarnEnabled()).thenReturn(true); + LogHelper.warn(logger, "warn"); + verify(logger).warn("warn"); + Throwable t = new RuntimeException(); + LogHelper.warn(logger, t); + verify(logger).warn(t); + LogHelper.warn(logger, "warn", t); + verify(logger).warn("warn", t); + } + + @Test + public void testError() throws Exception { + Logger logger = Mockito.mock(Logger.class); + when(logger.isErrorEnabled()).thenReturn(true); + LogHelper.error(logger, "error"); + verify(logger).error("error"); + Throwable t = new RuntimeException(); + LogHelper.error(logger, t); + verify(logger).error(t); + LogHelper.error(logger, "error", t); + verify(logger).error("error", t); + } +} diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/LogTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/LogTest.java new file mode 100644 index 00000000000..3901a93bafc --- /dev/null +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/LogTest.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.dubbo.common.utils; + +import org.apache.log4j.Level; +import org.junit.Test; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +public class LogTest { + @Test + public void testLogName() throws Exception { + Log log = new Log(); + log.setLogName("log-name"); + assertThat(log.getLogName(), equalTo("log-name")); + } + + @Test + public void testLogLevel() throws Exception { + Log log = new Log(); + log.setLogLevel(Level.ALL); + assertThat(log.getLogLevel(), is(Level.ALL)); + } + + @Test + public void testLogMessage() throws Exception { + Log log = new Log(); + log.setLogMessage("log-message"); + assertThat(log.getLogMessage(), equalTo("log-message")); + } + + @Test + public void testLogThread() throws Exception { + Log log = new Log(); + log.setLogThread("log-thread"); + assertThat(log.getLogThread(), equalTo("log-thread")); + } + +} diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/LogUtilTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/LogUtilTest.java new file mode 100644 index 00000000000..a4f7b15ab96 --- /dev/null +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/LogUtilTest.java @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.dubbo.common.utils; + +import org.apache.log4j.Level; +import org.junit.After; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class LogUtilTest { + @After + public void tearDown() throws Exception { + DubboAppender.logList.clear(); + } + + @Test + public void testStartStop() throws Exception { + LogUtil.start(); + assertThat(DubboAppender.available, is(true)); + LogUtil.stop(); + assertThat(DubboAppender.available, is(false)); + } + + @Test + public void testCheckNoError() throws Exception { + Log log = mock(Log.class); + DubboAppender.logList.add(log); + when(log.getLogLevel()).thenReturn(Level.ERROR); + assertThat(LogUtil.checkNoError(), is(false)); + when(log.getLogLevel()).thenReturn(Level.INFO); + assertThat(LogUtil.checkNoError(), is(true)); + } + + @Test + public void testFindName() throws Exception { + Log log = mock(Log.class); + DubboAppender.logList.add(log); + when(log.getLogName()).thenReturn("a"); + assertThat(LogUtil.findName("a"), equalTo(1)); + } + + @Test + public void testFindLevel() throws Exception { + Log log = mock(Log.class); + DubboAppender.logList.add(log); + when(log.getLogLevel()).thenReturn(Level.ERROR); + assertThat(LogUtil.findLevel(Level.ERROR), equalTo(1)); + assertThat(LogUtil.findLevel(Level.INFO), equalTo(0)); + } + + @Test + public void testFindLevelWithThreadName() throws Exception { + Log log = mock(Log.class); + DubboAppender.logList.add(log); + when(log.getLogLevel()).thenReturn(Level.ERROR); + when(log.getLogThread()).thenReturn("thread-1"); + log = mock(Log.class); + DubboAppender.logList.add(log); + when(log.getLogLevel()).thenReturn(Level.ERROR); + when(log.getLogThread()).thenReturn("thread-2"); + assertThat(LogUtil.findLevelWithThreadName(Level.ERROR, "thread-2"), equalTo(1)); + } + + @Test + public void testFindThread() throws Exception { + Log log = mock(Log.class); + DubboAppender.logList.add(log); + when(log.getLogThread()).thenReturn("thread-1"); + assertThat(LogUtil.findThread("thread-1"), equalTo(1)); + } + + @Test + public void testFindMessage1() throws Exception { + Log log = mock(Log.class); + DubboAppender.logList.add(log); + when(log.getLogMessage()).thenReturn("message"); + assertThat(LogUtil.findMessage("message"), equalTo(1)); + } + + @Test + public void testFindMessage2() throws Exception { + Log log = mock(Log.class); + DubboAppender.logList.add(log); + when(log.getLogMessage()).thenReturn("message"); + when(log.getLogLevel()).thenReturn(Level.ERROR); + log = mock(Log.class); + DubboAppender.logList.add(log); + when(log.getLogMessage()).thenReturn("message"); + when(log.getLogLevel()).thenReturn(Level.INFO); + assertThat(LogUtil.findMessage(Level.ERROR, "message"), equalTo(1)); + } + +}