From fdaf1d9cd974e7e1bf251d16792ae04f0a03aa04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=99=93=E5=8F=8C=20Li=20Xiao=20Shuang?= <644968328@qq.com> Date: Mon, 17 May 2021 09:42:48 +0800 Subject: [PATCH] add unit test for SpiImplPushExecutorHolder (#5716) --- .../SpiImplPushExecutorHolderTest.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 naming/src/test/java/com/alibaba/nacos/naming/push/v2/executor/SpiImplPushExecutorHolderTest.java diff --git a/naming/src/test/java/com/alibaba/nacos/naming/push/v2/executor/SpiImplPushExecutorHolderTest.java b/naming/src/test/java/com/alibaba/nacos/naming/push/v2/executor/SpiImplPushExecutorHolderTest.java new file mode 100644 index 00000000000..1846f8ba2f5 --- /dev/null +++ b/naming/src/test/java/com/alibaba/nacos/naming/push/v2/executor/SpiImplPushExecutorHolderTest.java @@ -0,0 +1,70 @@ +/* + * Copyright 1999-2020 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.naming.push.v2.executor; + +import com.alibaba.nacos.naming.pojo.Subscriber; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; + +import java.lang.reflect.Field; +import java.util.Optional; +import java.util.Set; + +@RunWith(MockitoJUnitRunner.class) +public class SpiImplPushExecutorHolderTest { + + @Mock + private Subscriber subscriber; + + @Mock + private SpiPushExecutor spiPushExecutor; + + private SpiImplPushExecutorHolder spiImplPushExecutorHolder; + + private static final String CLIENT_ID = "CLIENT_ID"; + + @Before + public void setUp() throws NoSuchFieldException, IllegalAccessException { + spiImplPushExecutorHolder = SpiImplPushExecutorHolder.getInstance(); + Class spiImplPushExecutorHolderClass = SpiImplPushExecutorHolder.class; + Field pushExecutors = spiImplPushExecutorHolderClass.getDeclaredField("pushExecutors"); + pushExecutors.setAccessible(true); + Set spiPushExecutorSet = (Set) pushExecutors.get(spiImplPushExecutorHolder); + spiPushExecutorSet.add(spiPushExecutor); + } + + @Test + public void testGetInstance() { + SpiImplPushExecutorHolder instance = SpiImplPushExecutorHolder.getInstance(); + + Assert.assertNotNull(instance); + } + + @Test + public void testFindPushExecutorSpiImpl() { + Mockito.when(spiPushExecutor.isInterest(CLIENT_ID, subscriber)).thenReturn(true); + Optional pushExecutorSpiImpl = spiImplPushExecutorHolder + .findPushExecutorSpiImpl(CLIENT_ID, subscriber); + + Assert.assertTrue(pushExecutorSpiImpl.isPresent()); + } +} \ No newline at end of file