diff --git a/infra/common/src/test/java/cn/hippo4j/common/MockitoTests.java b/infra/common/src/test/java/cn/hippo4j/common/MockitoTests.java index 7673709d..c86d5054 100644 --- a/infra/common/src/test/java/cn/hippo4j/common/MockitoTests.java +++ b/infra/common/src/test/java/cn/hippo4j/common/MockitoTests.java @@ -17,25 +17,25 @@ package cn.hippo4j.common; +import java.util.List; + import org.apache.commons.lang3.StringUtils; +import org.junit.Test; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.Mockito; -import org.mockito.junit.jupiter.MockitoExtension; - -import java.util.List; +import org.mockito.junit.MockitoJUnitRunner; -@ExtendWith(MockitoExtension.class) -class MockitoTests { +@RunWith(MockitoJUnitRunner.class) +public class MockitoTests { @Mock List list; @Test - void mockTests() { + public void mockTests() { Mockito.when(list.get(1)).thenReturn("mock success."); Assertions.assertEquals("mock success.", list.get(1)); try (final MockedStatic mockStatic = Mockito.mockStatic(StringUtils.class)) { diff --git a/infra/common/src/test/java/cn/hippo4j/common/executor/support/BlockingQueueTypeEnumTest.java b/infra/common/src/test/java/cn/hippo4j/common/executor/support/BlockingQueueTypeEnumTest.java index 3ba148f8..e208165f 100644 --- a/infra/common/src/test/java/cn/hippo4j/common/executor/support/BlockingQueueTypeEnumTest.java +++ b/infra/common/src/test/java/cn/hippo4j/common/executor/support/BlockingQueueTypeEnumTest.java @@ -18,7 +18,7 @@ package cn.hippo4j.common.executor.support; import org.junit.Assert; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.Arrays; import java.util.List; @@ -34,7 +34,7 @@ public final class BlockingQueueTypeEnumTest { private static final List BLOCKING_QUEUE_NAMES = Arrays.stream(BlockingQueueTypeEnum.values()).map(BlockingQueueTypeEnum::getName).collect(Collectors.toList()); @Test - void assertCreateBlockingQueueNormal() { + public void testAssertCreateBlockingQueueNormal() { // check legal param: name and capacity for (String name : BLOCKING_QUEUE_NAMES) { BlockingQueue blockingQueueByName = BlockingQueueTypeEnum.createBlockingQueue(name, 10); @@ -43,7 +43,7 @@ public final class BlockingQueueTypeEnumTest { } @Test - void assertCreateBlockingQueueWithIllegalName() { + public void testAssertCreateBlockingQueueWithIllegalName() { // check illegal null name Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue(null, 10)); // check unexistent name @@ -51,7 +51,7 @@ public final class BlockingQueueTypeEnumTest { } @Test - void assertCreateBlockingQueueWithIllegalCapacity() { + public void testAssertCreateBlockingQueueWithIllegalCapacity() { // check illegal null capacity for (String name : BLOCKING_QUEUE_NAMES) { BlockingQueue blockingQueueWithNullCapacity = BlockingQueueTypeEnum.createBlockingQueue(name, null); @@ -81,14 +81,14 @@ public final class BlockingQueueTypeEnumTest { } @Test - void assertCreateBlockingQueueWithIllegalParams() { + public void testAssertCreateBlockingQueueWithIllegalParams() { // check illegal name and capacity Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue("HelloWorld", null)); Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue(null, null)); } @Test - void assertCreateBlockingQueueWithType() { + public void testAssertCreateBlockingQueueWithType() { Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue(1, null)); Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue(2, null)); Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue(3, null)); @@ -102,7 +102,7 @@ public final class BlockingQueueTypeEnumTest { } @Test - void assertGetBlockingQueueNameByType() { + public void testAssertGetBlockingQueueNameByType() { // check legal range of type Assert.assertEquals("ArrayBlockingQueue", BlockingQueueTypeEnum.getBlockingQueueNameByType(1)); Assert.assertEquals("LinkedBlockingQueue", BlockingQueueTypeEnum.getBlockingQueueNameByType(2)); @@ -118,7 +118,7 @@ public final class BlockingQueueTypeEnumTest { } @Test - void assertGetBlockingQueueTypeEnumByName() { + public void testAssertGetBlockingQueueTypeEnumByName() { // check legal range of name Assert.assertEquals(BlockingQueueTypeEnum.ARRAY_BLOCKING_QUEUE, BlockingQueueTypeEnum.getBlockingQueueTypeEnumByName("ArrayBlockingQueue")); Assert.assertEquals(BlockingQueueTypeEnum.LINKED_BLOCKING_QUEUE, BlockingQueueTypeEnum.getBlockingQueueTypeEnumByName("LinkedBlockingQueue")); diff --git a/infra/common/src/test/java/cn/hippo4j/common/executor/support/RejectedPolicyTypeEnumTest.java b/infra/common/src/test/java/cn/hippo4j/common/executor/support/RejectedPolicyTypeEnumTest.java new file mode 100644 index 00000000..b0ce5b02 --- /dev/null +++ b/infra/common/src/test/java/cn/hippo4j/common/executor/support/RejectedPolicyTypeEnumTest.java @@ -0,0 +1,96 @@ +/* + * 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 cn.hippo4j.common.executor.support; + +import org.junit.Test; +import org.junit.jupiter.api.Assertions; + +import java.util.Arrays; + +/** + * test for {@link RejectedPolicyTypeEnum} + */ +public class RejectedPolicyTypeEnumTest { + + @Test + public void testGetType() { + Assertions.assertEquals(1, RejectedPolicyTypeEnum.CALLER_RUNS_POLICY.getType()); + Assertions.assertEquals(2, RejectedPolicyTypeEnum.ABORT_POLICY.getType()); + Assertions.assertEquals(3, RejectedPolicyTypeEnum.DISCARD_POLICY.getType()); + Assertions.assertEquals(4, RejectedPolicyTypeEnum.DISCARD_OLDEST_POLICY.getType()); + Assertions.assertEquals(5, RejectedPolicyTypeEnum.RUNS_OLDEST_TASK_POLICY.getType()); + Assertions.assertEquals(6, RejectedPolicyTypeEnum.SYNC_PUT_QUEUE_POLICY.getType()); + } + + @Test + public void testGetName() { + Assertions.assertEquals("CallerRunsPolicy", RejectedPolicyTypeEnum.CALLER_RUNS_POLICY.getName()); + Assertions.assertEquals("AbortPolicy", RejectedPolicyTypeEnum.ABORT_POLICY.getName()); + Assertions.assertEquals("DiscardPolicy", RejectedPolicyTypeEnum.DISCARD_POLICY.getName()); + Assertions.assertEquals("DiscardOldestPolicy", RejectedPolicyTypeEnum.DISCARD_OLDEST_POLICY.getName()); + Assertions.assertEquals("RunsOldestTaskPolicy", RejectedPolicyTypeEnum.RUNS_OLDEST_TASK_POLICY.getName()); + Assertions.assertEquals("SyncPutQueuePolicy", RejectedPolicyTypeEnum.SYNC_PUT_QUEUE_POLICY.getName()); + } + + @Test + public void testValues() { + Assertions.assertNotNull(RejectedPolicyTypeEnum.values()); + } + + @Test + public void testValueOf() { + Assertions.assertEquals(RejectedPolicyTypeEnum.CALLER_RUNS_POLICY, RejectedPolicyTypeEnum.valueOf("CALLER_RUNS_POLICY")); + Assertions.assertEquals(RejectedPolicyTypeEnum.ABORT_POLICY, RejectedPolicyTypeEnum.valueOf("ABORT_POLICY")); + Assertions.assertEquals(RejectedPolicyTypeEnum.DISCARD_POLICY, RejectedPolicyTypeEnum.valueOf("DISCARD_POLICY")); + Assertions.assertEquals(RejectedPolicyTypeEnum.DISCARD_OLDEST_POLICY, RejectedPolicyTypeEnum.valueOf("DISCARD_OLDEST_POLICY")); + Assertions.assertEquals(RejectedPolicyTypeEnum.RUNS_OLDEST_TASK_POLICY, RejectedPolicyTypeEnum.valueOf("RUNS_OLDEST_TASK_POLICY")); + Assertions.assertEquals(RejectedPolicyTypeEnum.SYNC_PUT_QUEUE_POLICY, RejectedPolicyTypeEnum.valueOf("SYNC_PUT_QUEUE_POLICY")); + } + + @Test + public void testCreatePolicy() { + // check legal param: name and type + Arrays.stream(RejectedPolicyTypeEnum.values()).forEach(each -> { + Assertions.assertNotNull(RejectedPolicyTypeEnum.createPolicy(each.getName())); + Assertions.assertNotNull(RejectedPolicyTypeEnum.createPolicy(each.getType())); + }); + // check illegal null name + Assertions.assertNotNull(RejectedPolicyTypeEnum.createPolicy(null)); + // check nonexistent name + Assertions.assertNotNull(RejectedPolicyTypeEnum.createPolicy("ABC")); + // check nonexistent type + Assertions.assertNotNull(RejectedPolicyTypeEnum.createPolicy(-1)); + } + + @Test + public void testGetRejectedNameByType() { + // check legal range of type + Arrays.stream(RejectedPolicyTypeEnum.values()).forEach(each -> Assertions.assertEquals(each.getName(), RejectedPolicyTypeEnum.getRejectedNameByType(each.getType()))); + // check illegal range of type + Assertions.assertEquals("AbortPolicy", RejectedPolicyTypeEnum.getRejectedNameByType(-1)); + } + + @Test + public void testGetRejectedPolicyTypeEnumByName() { + // check legal range of name + Arrays.stream(RejectedPolicyTypeEnum.values()).forEach(each -> Assertions.assertEquals(each, RejectedPolicyTypeEnum.getRejectedPolicyTypeEnumByName(each.getName()))); + // check illegal name + Assertions.assertEquals(RejectedPolicyTypeEnum.ABORT_POLICY, + RejectedPolicyTypeEnum.getRejectedPolicyTypeEnumByName("XXX")); + } +} diff --git a/infra/common/src/test/java/cn/hippo4j/common/executor/support/RunsOldestTaskPolicyTest.java b/infra/common/src/test/java/cn/hippo4j/common/executor/support/RunsOldestTaskPolicyTest.java new file mode 100644 index 00000000..0b59807a --- /dev/null +++ b/infra/common/src/test/java/cn/hippo4j/common/executor/support/RunsOldestTaskPolicyTest.java @@ -0,0 +1,100 @@ +/* + * 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 cn.hippo4j.common.executor.support; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class RunsOldestTaskPolicyTest { + + private final RunsOldestTaskPolicy runsOldestTaskPolicy = new RunsOldestTaskPolicy(); + + @Mock + private Runnable runnable; + + @Mock + private Runnable runnableInTheQueue; + + @Mock + private ThreadPoolExecutor threadPoolExecutor; + + @Mock + private BlockingQueue workQueue; + + @Test + public void testRejectedExecutionWhenExecutorIsShutDown() { + when(threadPoolExecutor.isShutdown()).thenReturn(true); + + runsOldestTaskPolicy.rejectedExecution(runnable, threadPoolExecutor); + + verify(threadPoolExecutor, never()).execute(runnable); + verify(runnable, never()).run(); + } + + @Test + public void testRejectedExecutionWhenATaskIsInTheQueueTheExecutorShouldNotExecute() { + when(threadPoolExecutor.isShutdown()).thenReturn(false); + when(threadPoolExecutor.getQueue()).thenReturn(workQueue); + when(workQueue.poll()).thenReturn(runnableInTheQueue); + when(workQueue.offer(runnable)).thenReturn(true); + + runsOldestTaskPolicy.rejectedExecution(runnable, threadPoolExecutor); + + verify(runnableInTheQueue).run(); + verify(threadPoolExecutor, never()).execute(runnable); + verify(runnable, never()).run(); + } + + @Test + public void testRejectedExecutionWhenATaskIsInTheQueueTheExecutorShouldExecute() { + when(threadPoolExecutor.isShutdown()).thenReturn(false); + when(threadPoolExecutor.getQueue()).thenReturn(workQueue); + when(workQueue.poll()).thenReturn(runnableInTheQueue); + when(workQueue.offer(runnable)).thenReturn(false); + + runsOldestTaskPolicy.rejectedExecution(runnable, threadPoolExecutor); + + verify(runnableInTheQueue).run(); + verify(threadPoolExecutor).execute(runnable); + verify(runnable, never()).run(); + } + + @Test + public void testRejectedExecutionWhenATaskIsInTheQueueAndThePollReturnANullValue() { + when(threadPoolExecutor.isShutdown()).thenReturn(false); + when(threadPoolExecutor.getQueue()).thenReturn(workQueue); + when(workQueue.poll()).thenReturn(null); + when(workQueue.offer(runnable)).thenReturn(false); + + runsOldestTaskPolicy.rejectedExecution(runnable, threadPoolExecutor); + + verify(runnableInTheQueue, never()).run(); + verify(threadPoolExecutor).execute(runnable); + verify(runnable, never()).run(); + } +} \ No newline at end of file diff --git a/infra/common/src/test/java/cn/hippo4j/common/executor/support/SyncPutQueuePolicyTest.java b/infra/common/src/test/java/cn/hippo4j/common/executor/support/SyncPutQueuePolicyTest.java new file mode 100644 index 00000000..c2f0a359 --- /dev/null +++ b/infra/common/src/test/java/cn/hippo4j/common/executor/support/SyncPutQueuePolicyTest.java @@ -0,0 +1,53 @@ +/* + * 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 cn.hippo4j.common.executor.support; + +import cn.hippo4j.common.toolkit.ThreadUtil; +import org.junit.Assert; +import org.junit.Test; + +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.stream.IntStream; + +/** + * Synchronous placement queue policy implementation test + */ +public class SyncPutQueuePolicyTest { + + /** + * test thread pool rejected execution + */ + @Test + public void testRejectedExecution() { + SyncPutQueuePolicy syncPutQueuePolicy = new SyncPutQueuePolicy(); + ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 2, + 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1), syncPutQueuePolicy); + threadPoolExecutor.prestartAllCoreThreads(); + + Assert.assertSame(syncPutQueuePolicy, threadPoolExecutor.getRejectedExecutionHandler()); + IntStream.range(0, 4).forEach(s -> { + threadPoolExecutor.execute(() -> ThreadUtil.sleep(200L)); + }); + threadPoolExecutor.shutdown(); + while (!threadPoolExecutor.isTerminated()) { + } + Assert.assertEquals(4, threadPoolExecutor.getCompletedTaskCount()); + } +} diff --git a/infra/common/src/test/java/cn/hippo4j/common/extension/design/AbstractSubjectCenterTest.java b/infra/common/src/test/java/cn/hippo4j/common/extension/design/AbstractSubjectCenterTest.java new file mode 100644 index 00000000..74bf2eac --- /dev/null +++ b/infra/common/src/test/java/cn/hippo4j/common/extension/design/AbstractSubjectCenterTest.java @@ -0,0 +1,163 @@ +/* + * 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 cn.hippo4j.common.extension.design; + +import lombok.Getter; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.lang.reflect.Field; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; + +public class AbstractSubjectCenterTest { + + private Map> OBSERVERS_MAP; + + private SubjectNotifyListener subjectNotifyListener; + + @Before + public void setUp() throws NoSuchFieldException, IllegalAccessException { + subjectNotifyListener = new SubjectNotifyListener(); + + Field field = AbstractSubjectCenter.class.getDeclaredField("OBSERVERS_MAP"); + field.setAccessible(true); + OBSERVERS_MAP = (Map>) field.get(AbstractSubjectCenter.class); + } + + /** + * test register listener + */ + @Test + public void testDefaultRegister() { + AbstractSubjectCenter.register(subjectNotifyListener); + List list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.SPRING_CONTENT_REFRESHED.name()); + Assert.assertNotNull(list); + Assert.assertEquals(1, list.size()); + Assert.assertSame(subjectNotifyListener, list.get(0)); + OBSERVERS_MAP.clear(); + } + + /** + * test register listener + */ + @Test + public void testSubjectTypeEnumRegister() { + AbstractSubjectCenter.register(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH, subjectNotifyListener); + List list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH.name()); + Assert.assertNotNull(list); + Assert.assertEquals(1, list.size()); + Assert.assertSame(subjectNotifyListener, list.get(0)); + OBSERVERS_MAP.clear(); + } + + /** + * test register listener + */ + @Test + public void testSubjectTypeNameRegister() { + AbstractSubjectCenter.register(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH.name(), subjectNotifyListener); + List list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH.name()); + Assert.assertNotNull(list); + Assert.assertEquals(1, list.size()); + Assert.assertSame(subjectNotifyListener, list.get(0)); + OBSERVERS_MAP.clear(); + } + + /** + * test remove listener + */ + @Test + public void testDefaultRemoveListener() { + AbstractSubjectCenter.register(subjectNotifyListener); + List list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.SPRING_CONTENT_REFRESHED.name()); + Assert.assertNotNull(list); + Assert.assertEquals(1, list.size()); + Assert.assertSame(subjectNotifyListener, list.get(0)); + + AbstractSubjectCenter.remove(subjectNotifyListener); + Assert.assertEquals(0, list.size()); + } + + /** + * test remove listener + */ + @Test + public void testRemoveSubjectTypeNameListener() { + AbstractSubjectCenter.register(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH, subjectNotifyListener); + List list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH.name()); + Assert.assertNotNull(list); + Assert.assertEquals(1, list.size()); + Assert.assertSame(subjectNotifyListener, list.get(0)); + + AbstractSubjectCenter.remove(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH.name(), subjectNotifyListener); + Assert.assertEquals(0, list.size()); + } + + /** + * test notify + */ + @Test + public void testNotifyBySubjectType() { + AbstractSubjectCenter.register(subjectNotifyListener); + List list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.SPRING_CONTENT_REFRESHED.name()); + Assert.assertNotNull(list); + + NotifyMessage notifyMessage = new NotifyMessage(); + Assert.assertEquals(0, notifyMessage.getCount().get()); + AbstractSubjectCenter.notify(AbstractSubjectCenter.SubjectType.SPRING_CONTENT_REFRESHED, () -> notifyMessage); + Assert.assertEquals(1, notifyMessage.getCount().get()); + OBSERVERS_MAP.clear(); + } + + /** + * test notify + */ + @Test + public void testNotifyBySubjectTypeName() { + AbstractSubjectCenter.register(subjectNotifyListener); + List list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.SPRING_CONTENT_REFRESHED.name()); + Assert.assertNotNull(list); + + NotifyMessage notifyMessage = new NotifyMessage(); + Assert.assertEquals(0, notifyMessage.getCount().get()); + AbstractSubjectCenter.notify(AbstractSubjectCenter.SubjectType.SPRING_CONTENT_REFRESHED.name(), () -> notifyMessage); + Assert.assertEquals(1, notifyMessage.getCount().get()); + OBSERVERS_MAP.clear(); + } + + @Getter + private static final class NotifyMessage { + + private final AtomicInteger count = new AtomicInteger(0); + } + + /** + * Subject Response Listener + */ + private static final class SubjectNotifyListener implements Observer { + + @Override + public void accept(ObserverMessage observerMessage) { + observerMessage.message().getCount().incrementAndGet(); + } + } +} diff --git a/infra/common/src/test/java/cn/hippo4j/common/function/NoArgsConsumerTest.java b/infra/common/src/test/java/cn/hippo4j/common/function/NoArgsConsumerTest.java index f0ed0b6f..553dda98 100644 --- a/infra/common/src/test/java/cn/hippo4j/common/function/NoArgsConsumerTest.java +++ b/infra/common/src/test/java/cn/hippo4j/common/function/NoArgsConsumerTest.java @@ -19,14 +19,14 @@ package cn.hippo4j.common.function; import cn.hippo4j.common.extension.function.NoArgsConsumer; import cn.hippo4j.common.toolkit.Assert; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.concurrent.atomic.AtomicBoolean; -final class NoArgsConsumerTest { +public final class NoArgsConsumerTest { @Test - void accept() { + public void accept() { AtomicBoolean checkValue = new AtomicBoolean(false); NoArgsConsumer noArgsConsumer = () -> checkValue.set(true); noArgsConsumer.accept(); diff --git a/infra/common/src/test/java/cn/hippo4j/common/toolkit/UserContextTest.java b/infra/common/src/test/java/cn/hippo4j/common/toolkit/UserContextTest.java index a361d139..ffbc5a83 100644 --- a/infra/common/src/test/java/cn/hippo4j/common/toolkit/UserContextTest.java +++ b/infra/common/src/test/java/cn/hippo4j/common/toolkit/UserContextTest.java @@ -17,7 +17,7 @@ package cn.hippo4j.common.toolkit; -import org.junit.jupiter.api.Test; +import org.junit.Test; public class UserContextTest { diff --git a/infra/common/src/test/java/cn/hippo4j/common/toolkit/logtracing/LogMessageTest.java b/infra/common/src/test/java/cn/hippo4j/common/toolkit/logtracing/LogMessageTest.java new file mode 100644 index 00000000..5750f587 --- /dev/null +++ b/infra/common/src/test/java/cn/hippo4j/common/toolkit/logtracing/LogMessageTest.java @@ -0,0 +1,92 @@ +/* + * 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 cn.hippo4j.common.toolkit.logtracing; + +import org.apache.logging.log4j.util.Strings; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class LogMessageTest { + + private final static String MESSAGE = "message"; + private final static String THROWABLE_MESSAGE = "throwable message"; + private LogMessage logMessage; + + @Before + public void init() { + logMessage = LogMessage.getInstance(); + } + + @Test + public void testGetInstanceShouldReturnANewLogMessageInstance() { + final LogMessage newInstance = LogMessage.getInstance(); + assertNotNull(newInstance); + assertNotSame(logMessage, newInstance); + } + + @Test + public void testToStringShouldHaveAnEmptyMessage() { + assertEquals(Strings.EMPTY, logMessage.toString()); + } + + @Test + public void testSetMsgShouldSetAnewMessageInLogMessage() { + logMessage.setMsg(MESSAGE); + assertEquals(MESSAGE, logMessage.toString()); + } + + @Test + public void testMsgShouldContainsMessageAndThrowableMessage() { + final String message = logMessage.msg(MESSAGE, new Throwable(THROWABLE_MESSAGE)); + assertNotNull(message); + assertTrue(message.contains(MESSAGE)); + assertTrue(message.contains(THROWABLE_MESSAGE)); + } + + @Test + public void testKvShouldPutKeyAndValue() { + logMessage.kv("key", "value"); + assertEquals("key=value", logMessage.toString()); + } + + @Test + public void testKvShouldPutAllKeyAndValuePairs() { + logMessage.kv("key1", "value1"); + logMessage.kv("key2", "value2"); + assertEquals("key1=value1||key2=value2", logMessage.toString()); + } + + @Test + public void testToStringShouldPrintMessageAndAllKeyAndValuePairs() { + logMessage.setMsg(MESSAGE); + logMessage.kv("key1", "value1"); + logMessage.kv("key2", "value2"); + assertEquals("messagekey1=value1||key2=value2", logMessage.toString()); + } + + @Test + public void testKv2StringShouldPrintMessageAndAllKeyAndValuePairs() { + String result = logMessage.kv2String("key", "value"); + assertEquals("key=value", result); + } +} \ No newline at end of file diff --git a/threadpool/core/src/test/java/cn/hippo4j/core/executor/state/AbstractThreadPoolRuntimeTest.java b/threadpool/core/src/test/java/cn/hippo4j/core/executor/state/AbstractThreadPoolRuntimeTest.java index d08fb953..7ff7db6e 100644 --- a/threadpool/core/src/test/java/cn/hippo4j/core/executor/state/AbstractThreadPoolRuntimeTest.java +++ b/threadpool/core/src/test/java/cn/hippo4j/core/executor/state/AbstractThreadPoolRuntimeTest.java @@ -17,24 +17,24 @@ package cn.hippo4j.core.executor.state; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + import cn.hippo4j.common.model.ThreadPoolRunStateInfo; import cn.hippo4j.core.executor.DynamicThreadPoolExecutor; import cn.hippo4j.core.executor.DynamicThreadPoolWrapper; import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage; +import org.junit.Test; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.junit.jupiter.MockitoExtension; - -import java.util.concurrent.ArrayBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; -@ExtendWith(MockitoExtension.class) +@RunWith(MockitoJUnitRunner.class) public class AbstractThreadPoolRuntimeTest { @Test - void testPoolRunState() { + public void testPoolRunState() { AbstractThreadPoolRuntime threadPoolRuntime = new AbstractThreadPoolRuntime() { @Override diff --git a/threadpool/server/auth/src/test/java/cn/hippo4j/auth/filter/JWTAuthenticationFilterTest.java b/threadpool/server/auth/src/test/java/cn/hippo4j/auth/filter/JWTAuthenticationFilterTest.java index 8d3b412c..debc76a1 100644 --- a/threadpool/server/auth/src/test/java/cn/hippo4j/auth/filter/JWTAuthenticationFilterTest.java +++ b/threadpool/server/auth/src/test/java/cn/hippo4j/auth/filter/JWTAuthenticationFilterTest.java @@ -18,15 +18,15 @@ package cn.hippo4j.auth.filter; import cn.hippo4j.common.toolkit.ReflectUtil; +import org.junit.Test; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.core.userdetails.UsernameNotFoundException; -class JWTAuthenticationFilterTest { +public class JWTAuthenticationFilterTest { @Test - void getMessageTest() { + public void getMessageTest() { JWTAuthenticationFilter filter = new JWTAuthenticationFilter(null); Assertions.assertEquals("用户不存在", ReflectUtil.invoke(filter, "getMessage", new UsernameNotFoundException(""))); diff --git a/threadpool/server/auth/src/test/java/cn/hippo4j/auth/service/impl/UserServiceImplTest.java b/threadpool/server/auth/src/test/java/cn/hippo4j/auth/service/impl/UserServiceImplTest.java index 8f92e423..42c9cd91 100644 --- a/threadpool/server/auth/src/test/java/cn/hippo4j/auth/service/impl/UserServiceImplTest.java +++ b/threadpool/server/auth/src/test/java/cn/hippo4j/auth/service/impl/UserServiceImplTest.java @@ -18,12 +18,12 @@ package cn.hippo4j.auth.service.impl; import org.junit.Assert; -import org.junit.jupiter.api.Test; +import org.junit.Test; -class UserServiceImplTest { +public class UserServiceImplTest { @Test - void checkPasswordLength() { + public void testCheckPasswordLength() { // 密码为null、空串、过短、过长都会抛出异常 UserServiceImpl userService = new UserServiceImpl(null, null, null); Assert.assertThrows(RuntimeException.class, () -> userService.checkPasswordLength(null)); diff --git a/threadpool/server/auth/src/test/java/cn/hippo4j/auth/toolkit/JwtTokenUtilTest.java b/threadpool/server/auth/src/test/java/cn/hippo4j/auth/toolkit/JwtTokenUtilTest.java index 77b5f0c1..8066d859 100644 --- a/threadpool/server/auth/src/test/java/cn/hippo4j/auth/toolkit/JwtTokenUtilTest.java +++ b/threadpool/server/auth/src/test/java/cn/hippo4j/auth/toolkit/JwtTokenUtilTest.java @@ -18,8 +18,8 @@ package cn.hippo4j.auth.toolkit; import cn.hippo4j.common.toolkit.Assert; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Before; +import org.junit.Test; public final class JwtTokenUtilTest { @@ -33,7 +33,7 @@ public final class JwtTokenUtilTest { private String token; - @BeforeEach + @Before public void setUp() { token = JwtTokenUtil.createToken(userId, username, role, isRememberMe); } diff --git a/threadpool/server/common/src/test/java/cn/hippo4j/server/common/base/ResultsTest.java b/threadpool/server/common/src/test/java/cn/hippo4j/server/common/base/ResultsTest.java index e76d94b6..9837439d 100644 --- a/threadpool/server/common/src/test/java/cn/hippo4j/server/common/base/ResultsTest.java +++ b/threadpool/server/common/src/test/java/cn/hippo4j/server/common/base/ResultsTest.java @@ -21,7 +21,7 @@ import cn.hippo4j.common.model.Result; import cn.hippo4j.common.toolkit.Assert; import cn.hippo4j.server.common.base.exception.AbstractException; import cn.hippo4j.server.common.base.exception.ErrorCode; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static cn.hippo4j.server.common.base.exception.ErrorCodeEnum.SERVICE_ERROR;