From bd046b927152bcbad045db7d0a5da8346a41843c Mon Sep 17 00:00:00 2001 From: rnmb <19276209+rnmb@users.noreply.github.com> Date: Fri, 26 May 2023 09:47:31 +0800 Subject: [PATCH 1/8] feat(infra): add AbstractSubjectCenter test class #1338 (#1340) * feat(infra): add AbstractSubjectCenter test class #1338 1. add SubjectCenter test class 2. add remove observer method 3. add get size for Observers method Co-authored-by: rnmb<19276209+rnmb@users.noreply.github.com> * feat(infra): add AbstractSubjectCenter test class 1. add SubjectCenter test class 2. rollback SubjectCenter method Co-authored-by: rnmb<19276209+rnmb@users.noreply.github.com> --- .../design/AbstractSubjectCenterTest.java | 146 ++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 infra/common/src/test/java/cn/hippo4j/common/extension/design/AbstractSubjectCenterTest.java 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..44c31294 --- /dev/null +++ b/infra/common/src/test/java/cn/hippo4j/common/extension/design/AbstractSubjectCenterTest.java @@ -0,0 +1,146 @@ +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(); + } + } +} From 7692ed83666860ec361e5b932f7bb50a3d1cc26a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=B6=E9=97=B4=E9=82=AE=E9=80=92=E5=91=98?= <78356082+barret-yzh@users.noreply.github.com> Date: Fri, 26 May 2023 14:38:08 +0800 Subject: [PATCH 2/8] Add test case for ThreadFactoryBuilder (#1341) * Create ThreadFactoryBuilderTest * Update ThreadFactoryBuilderTest Methods are covered by multiple unit test cases . Console printing in English * Update ThreadFactoryBuilderTest Add open source protocol. use @before annotation * Update ThreadFactoryBuilderTest * Update ThreadFactoryBuilderTest * Update ThreadFactoryBuilderTest --- .../common/executor/ThreadFactoryBuilderTest | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 infra/common/src/test/java/cn/hippo4j/common/executor/ThreadFactoryBuilderTest diff --git a/infra/common/src/test/java/cn/hippo4j/common/executor/ThreadFactoryBuilderTest b/infra/common/src/test/java/cn/hippo4j/common/executor/ThreadFactoryBuilderTest new file mode 100644 index 00000000..e1344100 --- /dev/null +++ b/infra/common/src/test/java/cn/hippo4j/common/executor/ThreadFactoryBuilderTest @@ -0,0 +1,81 @@ +/* + * 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.design.builder; + +import cn.hippo4j.common.design.builder.ThreadFactoryBuilder; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import java.util.concurrent.ThreadFactory; +import java.lang.Thread.UncaughtExceptionHandler; + +public class ThreadFactoryBuilderTest { + + ThreadFactoryBuilder builder; + TestUncaughtExceptionHandler uncaughtExceptionHandler; + Thread thread; + + @Before + public void buildThread() { + builder = ThreadFactoryBuilder.builder(); + uncaughtExceptionHandler = new TestUncaughtExceptionHandler(); + builder.uncaughtExceptionHandler(uncaughtExceptionHandler); + builder.prefix("my-thread-factory"); + builder.daemon(true); + builder.priority(Thread.MAX_PRIORITY); + ThreadFactory threadFactory = builder.build(); + thread = threadFactory.newThread(() -> { + System.out.println("Create a new thread."); + }); + thread.start(); + } + + @Test + public void testName() { + Assert.assertEquals("my-thread-factory_0", thread.getName()); + } + + @Test + public void testIsDaemon() { + Assert.assertTrue(thread.isDaemon()); + } + + @Test + public void testExceptionHandler() { + Assert.assertEquals(uncaughtExceptionHandler, thread.getUncaughtExceptionHandler()); + } + + @Test + public void testPriority() { + Assert.assertEquals(Thread.MAX_PRIORITY, thread.getPriority()); + } +} + +class TestUncaughtExceptionHandler implements UncaughtExceptionHandler { + + private volatile boolean exceptionCaught = false; + + @Override + public void uncaughtException(Thread t, Throwable e) { + System.out.println("Exception caught by " + t.getName()); + exceptionCaught = true; + } + + public boolean isExceptionCaught() { + return exceptionCaught; + } +} From ec142d7cc35dd6f0f3e58f65dacaf29de19e6d92 Mon Sep 17 00:00:00 2001 From: rnmb <19276209+rnmb@users.noreply.github.com> Date: Fri, 26 May 2023 15:06:05 +0800 Subject: [PATCH 3/8] feat(infra): add SyncPutQueuePolicyTest test class #1336 (#1344) * feat(infra): add SyncPutQueuePolicyTest test class #1336 1. add SyncPutQueuePolicyTest test class Co-authored-by: rnmb<19276209+rnmb@users.noreply.github.com> * feat(infra): SyncPutQueuePolicyTest #1336 1. Add the open source agreement to the top, and leave a blank line with package Co-authored-by: rnmb<19276209+rnmb@users.noreply.github.com> --- .../support/SyncPutQueuePolicyTest.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 infra/common/src/test/java/cn/hippo4j/common/executor/support/SyncPutQueuePolicyTest.java 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()); + } +} From 8ccf6ba22c663de14cfad0f6999e62e35380dc89 Mon Sep 17 00:00:00 2001 From: Annibale Ippolito <101899632+annippolito@users.noreply.github.com> Date: Fri, 26 May 2023 09:07:02 +0200 Subject: [PATCH 4/8] test: Add test case for LogMessage #1331 (#1343) * test: Add test case for LogMessage #1331 * change method signatures in LogMessage tests #1331 --------- Co-authored-by: annippolito --- .../design/AbstractSubjectCenterTest.java | 23 ++++- .../toolkit/logtracing/LogMessageTest.java | 93 +++++++++++++++++++ 2 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 infra/common/src/test/java/cn/hippo4j/common/toolkit/logtracing/LogMessageTest.java 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 index 44c31294..74bf2eac 100644 --- 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 @@ -1,3 +1,20 @@ +/* + * 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; @@ -13,8 +30,7 @@ import java.util.concurrent.atomic.AtomicInteger; public class AbstractSubjectCenterTest { - - private Map> OBSERVERS_MAP ; + private Map> OBSERVERS_MAP; private SubjectNotifyListener subjectNotifyListener; @@ -130,13 +146,14 @@ public class AbstractSubjectCenterTest { @Getter private static final class NotifyMessage { + private final AtomicInteger count = new AtomicInteger(0); } /** * Subject Response Listener */ - private static final class SubjectNotifyListener implements Observer{ + private static final class SubjectNotifyListener implements Observer { @Override public void accept(ObserverMessage observerMessage) { 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..3d486bcf --- /dev/null +++ b/infra/common/src/test/java/cn/hippo4j/common/toolkit/logtracing/LogMessageTest.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 cn.hippo4j.common.toolkit.logtracing; + +import org.apache.logging.log4j.util.Strings; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.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; + +class LogMessageTest { + + private final static String MESSAGE = "message"; + private final static String THROWABLE_MESSAGE = "throwable message"; + + private LogMessage logMessage; + + @BeforeEach + public void init() { + logMessage = LogMessage.getInstance(); + } + + @Test + void testGetInstanceShouldReturnANewLogMessageInstance() { + final LogMessage newInstance = LogMessage.getInstance(); + assertNotNull(newInstance); + assertNotSame(logMessage, newInstance); + } + + @Test + void testToStringShouldHaveAnEmptyMessage() { + assertEquals(Strings.EMPTY, logMessage.toString()); + } + + @Test + void testSetMsgShouldSetAnewMessageInLogMessage() { + logMessage.setMsg(MESSAGE); + assertEquals(MESSAGE, logMessage.toString()); + } + + @Test + void testMsgShouldContainsMessageAndThrowableMessage() { + final String message = logMessage.msg(MESSAGE, new Throwable(THROWABLE_MESSAGE)); + assertNotNull(message); + assertTrue(message.contains(MESSAGE)); + assertTrue(message.contains(THROWABLE_MESSAGE)); + } + + @Test + void testKvShouldPutKeyAndValue() { + logMessage.kv("key", "value"); + assertEquals("key=value", logMessage.toString()); + } + + @Test + void testKvShouldPutAllKeyAndValuePairs() { + logMessage.kv("key1", "value1"); + logMessage.kv("key2", "value2"); + assertEquals("key1=value1||key2=value2", logMessage.toString()); + } + + @Test + void testToStringShouldPrintMessageAndAllKeyAndValuePairs() { + logMessage.setMsg(MESSAGE); + logMessage.kv("key1", "value1"); + logMessage.kv("key2", "value2"); + assertEquals("messagekey1=value1||key2=value2", logMessage.toString()); + } + + @Test + void testKv2StringShouldPrintMessageAndAllKeyAndValuePairs() { + String result = logMessage.kv2String("key", "value"); + assertEquals("key=value", result); + } +} \ No newline at end of file From 13f35ad7459268e15f463c7a1edbd5e6fd97dce9 Mon Sep 17 00:00:00 2001 From: lucca suen <72333564+lucca-suen@users.noreply.github.com> Date: Fri, 26 May 2023 17:12:35 +0800 Subject: [PATCH 5/8] Add test case for RejectedPolicyTypeEnum (#1345) * test: Add test case for RejectedPolicyTypeEnum * refactor: Add open source protocol for RejectedPolicyTypeEnumTest. --------- Co-authored-by: lucca --- .../support/RejectedPolicyTypeEnumTest.java | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 infra/common/src/test/java/cn/hippo4j/common/executor/support/RejectedPolicyTypeEnumTest.java 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..d9e6c1c5 --- /dev/null +++ b/infra/common/src/test/java/cn/hippo4j/common/executor/support/RejectedPolicyTypeEnumTest.java @@ -0,0 +1,98 @@ +/* + * 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.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +/** + * test for {@link RejectedPolicyTypeEnum} + */ +class RejectedPolicyTypeEnumTest { + + @Test + 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 + 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 + void testValues() { + Assertions.assertNotNull(RejectedPolicyTypeEnum.values()); + } + + @Test + 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 + 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 + 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 + 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")); + } +} From 1ca11dca09a495e287d8dadafb36ed09af533440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=B6=E9=97=B4=E9=82=AE=E9=80=92=E5=91=98?= <78356082+barret-yzh@users.noreply.github.com> Date: Fri, 26 May 2023 17:22:55 +0800 Subject: [PATCH 6/8] Add test case for MessageWrapper (#1346) * Create ThreadFactoryBuilderTest * Update ThreadFactoryBuilderTest Methods are covered by multiple unit test cases . Console printing in English * Update ThreadFactoryBuilderTest Add open source protocol. use @before annotation * Update ThreadFactoryBuilderTest * Update ThreadFactoryBuilderTest * Update ThreadFactoryBuilderTest * Add test case for MessageWrapper * Update MessageWrapperTest --- .../hippo4j/common/toolkit/MessageWrapperTest | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 infra/common/src/test/java/cn/hippo4j/common/toolkit/MessageWrapperTest diff --git a/infra/common/src/test/java/cn/hippo4j/common/toolkit/MessageWrapperTest b/infra/common/src/test/java/cn/hippo4j/common/toolkit/MessageWrapperTest new file mode 100644 index 00000000..7a18ea75 --- /dev/null +++ b/infra/common/src/test/java/cn/hippo4j/common/toolkit/MessageWrapperTest @@ -0,0 +1,80 @@ +/* + * 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.monitor; + +import cn.hippo4j.common.monitor.MessageTypeEnum; +import cn.hippo4j.common.monitor.MessageWrapper; +import org.junit.Assert; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import java.util.HashMap; +import java.util.Map; +import java.util.Arrays; +import java.util.List; +import java.util.Collections; + +public class MessageWrapperTest { + + private static List> contentParams; + private static Class responseClass; + private static MessageTypeEnum messageType; + private static MessageWrapper messageWrapper; + + @BeforeAll + static void setUp() { + // init data + Map data1 = new HashMap<>(); + data1.put("key1", "value1"); + data1.put("key2", 123); + Map data2 = new HashMap<>(); + data2.put("key3", true); + data2.put("key4", 3.14); + contentParams = Arrays.asList(data1, data2); + responseClass = String.class; + messageType = MessageTypeEnum.DEFAULT; + messageWrapper = new MessageWrapper(contentParams, responseClass, messageType); + } + + @Test + void testGetContentParams() { + Assert.assertEquals(contentParams, messageWrapper.getContentParams()); + } + + @Test + void testGetResponseClass() { + Assert.assertEquals(responseClass, messageWrapper.getResponseClass()); + } + + @Test + void testGetMessageType() { + Assert.assertEquals(messageType, messageWrapper.getMessageType()); + } + + @Test + void testSettersAndGetters() { + List> newContentParams = Collections.singletonList(Collections.emptyMap()); + Class newResponseClass = Integer.class; + MessageTypeEnum newMessageType = MessageTypeEnum.DEFAULT; + messageWrapper.setContentParams(newContentParams); + messageWrapper.setResponseClass(newResponseClass); + messageWrapper.setMessageType(newMessageType); + Assert.assertEquals(newContentParams, messageWrapper.getContentParams()); + Assert.assertEquals(newResponseClass, messageWrapper.getResponseClass()); + Assert.assertEquals(newMessageType, messageWrapper.getMessageType()); + } +} From 0e7e6ec6e76dca2187d5531eb68fef58f44a5cf0 Mon Sep 17 00:00:00 2001 From: Annibale Ippolito <101899632+annippolito@users.noreply.github.com> Date: Fri, 26 May 2023 11:46:02 +0200 Subject: [PATCH 7/8] test: Add test case for RunsOldestTaskPolicy #1335 (#1347) * test: Add test case for RunsOldestTaskPolicy #1335 * make test viewable by codecov #1335 --- .../support/RunsOldestTaskPolicyTest.java | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 infra/common/src/test/java/cn/hippo4j/common/executor/support/RunsOldestTaskPolicyTest.java 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 From 3d3de064388c16ab0a696e93a33ce73a3b8bdcf8 Mon Sep 17 00:00:00 2001 From: Annibale Ippolito <101899632+annippolito@users.noreply.github.com> Date: Sat, 27 May 2023 05:46:27 +0200 Subject: [PATCH 8/8] fix: increase codecov coverage by using junit instead of junit.jupiter (#1349) * increase codecov coverage by using junit instead of junit.jupiter * increase codecov coverage in some other packages * remove blank line --- .../java/cn/hippo4j/common/MockitoTests.java | 16 ++++++------ .../support/BlockingQueueTypeEnumTest.java | 16 ++++++------ .../support/RejectedPolicyTypeEnumTest.java | 24 ++++++++---------- .../common/function/NoArgsConsumerTest.java | 6 ++--- .../common/toolkit/UserContextTest.java | 2 +- .../toolkit/logtracing/LogMessageTest.java | 25 +++++++++---------- .../state/AbstractThreadPoolRuntimeTest.java | 18 ++++++------- .../filter/JWTAuthenticationFilterTest.java | 6 ++--- .../service/impl/UserServiceImplTest.java | 6 ++--- .../auth/toolkit/JwtTokenUtilTest.java | 6 ++--- .../server/common/base/ResultsTest.java | 2 +- 11 files changed, 62 insertions(+), 65 deletions(-) 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 index d9e6c1c5..b0ce5b02 100644 --- 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 @@ -17,18 +17,18 @@ package cn.hippo4j.common.executor.support; +import org.junit.Test; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; import java.util.Arrays; /** * test for {@link RejectedPolicyTypeEnum} */ -class RejectedPolicyTypeEnumTest { +public class RejectedPolicyTypeEnumTest { @Test - void testGetType() { + 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()); @@ -38,7 +38,7 @@ class RejectedPolicyTypeEnumTest { } @Test - void testGetName() { + 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()); @@ -48,12 +48,12 @@ class RejectedPolicyTypeEnumTest { } @Test - void testValues() { + public void testValues() { Assertions.assertNotNull(RejectedPolicyTypeEnum.values()); } @Test - void testValueOf() { + 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")); @@ -63,7 +63,7 @@ class RejectedPolicyTypeEnumTest { } @Test - void testCreatePolicy() { + public void testCreatePolicy() { // check legal param: name and type Arrays.stream(RejectedPolicyTypeEnum.values()).forEach(each -> { Assertions.assertNotNull(RejectedPolicyTypeEnum.createPolicy(each.getName())); @@ -78,19 +78,17 @@ class RejectedPolicyTypeEnumTest { } @Test - void testGetRejectedNameByType() { + public void testGetRejectedNameByType() { // check legal range of type - Arrays.stream(RejectedPolicyTypeEnum.values()).forEach(each -> - Assertions.assertEquals(each.getName(), RejectedPolicyTypeEnum.getRejectedNameByType(each.getType()))); + 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 - void testGetRejectedPolicyTypeEnumByName() { + public void testGetRejectedPolicyTypeEnumByName() { // check legal range of name - Arrays.stream(RejectedPolicyTypeEnum.values()).forEach(each -> - Assertions.assertEquals(each, RejectedPolicyTypeEnum.getRejectedPolicyTypeEnumByName(each.getName()))); + 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/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 index 3d486bcf..5750f587 100644 --- 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 @@ -18,46 +18,45 @@ package cn.hippo4j.common.toolkit.logtracing; import org.apache.logging.log4j.util.Strings; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +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; -class LogMessageTest { +public class LogMessageTest { private final static String MESSAGE = "message"; private final static String THROWABLE_MESSAGE = "throwable message"; - private LogMessage logMessage; - @BeforeEach + @Before public void init() { logMessage = LogMessage.getInstance(); } @Test - void testGetInstanceShouldReturnANewLogMessageInstance() { + public void testGetInstanceShouldReturnANewLogMessageInstance() { final LogMessage newInstance = LogMessage.getInstance(); assertNotNull(newInstance); assertNotSame(logMessage, newInstance); } @Test - void testToStringShouldHaveAnEmptyMessage() { + public void testToStringShouldHaveAnEmptyMessage() { assertEquals(Strings.EMPTY, logMessage.toString()); } @Test - void testSetMsgShouldSetAnewMessageInLogMessage() { + public void testSetMsgShouldSetAnewMessageInLogMessage() { logMessage.setMsg(MESSAGE); assertEquals(MESSAGE, logMessage.toString()); } @Test - void testMsgShouldContainsMessageAndThrowableMessage() { + public void testMsgShouldContainsMessageAndThrowableMessage() { final String message = logMessage.msg(MESSAGE, new Throwable(THROWABLE_MESSAGE)); assertNotNull(message); assertTrue(message.contains(MESSAGE)); @@ -65,20 +64,20 @@ class LogMessageTest { } @Test - void testKvShouldPutKeyAndValue() { + public void testKvShouldPutKeyAndValue() { logMessage.kv("key", "value"); assertEquals("key=value", logMessage.toString()); } @Test - void testKvShouldPutAllKeyAndValuePairs() { + public void testKvShouldPutAllKeyAndValuePairs() { logMessage.kv("key1", "value1"); logMessage.kv("key2", "value2"); assertEquals("key1=value1||key2=value2", logMessage.toString()); } @Test - void testToStringShouldPrintMessageAndAllKeyAndValuePairs() { + public void testToStringShouldPrintMessageAndAllKeyAndValuePairs() { logMessage.setMsg(MESSAGE); logMessage.kv("key1", "value1"); logMessage.kv("key2", "value2"); @@ -86,7 +85,7 @@ class LogMessageTest { } @Test - void testKv2StringShouldPrintMessageAndAllKeyAndValuePairs() { + public void testKv2StringShouldPrintMessageAndAllKeyAndValuePairs() { String result = logMessage.kv2String("key", "value"); assertEquals("key=value", result); } 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;