|
| 1 | +/* |
| 2 | + * Tencent is pleased to support the open source community by making dubbo-polaris-java available. |
| 3 | + * |
| 4 | + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the BSD 3-Clause License (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://opensource.org/licenses/BSD-3-Clause |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software distributed |
| 13 | + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 14 | + * CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 15 | + * specific language governing permissions and limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.tencent.polaris.common.utils; |
| 19 | + |
| 20 | +import org.apache.dubbo.common.URL; |
| 21 | +import org.apache.dubbo.common.constants.RegistryConstants; |
| 22 | +import org.apache.dubbo.common.url.component.ServiceConfigURL; |
| 23 | +import org.apache.dubbo.registry.client.InstanceAddressURL; |
| 24 | +import org.apache.dubbo.rpc.Invoker; |
| 25 | +import org.junit.Test; |
| 26 | +import org.mockito.Mockito; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | +import static org.mockito.Mockito.when; |
| 30 | + |
| 31 | +/** |
| 32 | + * Test for {@link DubboUtils}. |
| 33 | + * |
| 34 | + * @author Haotian Zhang |
| 35 | + */ |
| 36 | +public class DubboUtilsTest { |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testCheckIsApplicationModeWithInstanceAddressURL() { |
| 40 | + // 准备:创建InstanceAddressURL类型的mock Invoker |
| 41 | + Invoker<?> invoker = Mockito.mock(Invoker.class); |
| 42 | + InstanceAddressURL url = Mockito.mock(InstanceAddressURL.class); |
| 43 | + when(invoker.getUrl()).thenReturn(url); |
| 44 | + |
| 45 | + // 执行 & 验证:InstanceAddressURL应该返回true |
| 46 | + assertThat(DubboUtils.checkIsApplicationMode(invoker)).isTrue(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testCheckIsApplicationModeWithServiceConfigURL_RegisterModeAll() { |
| 51 | + // 准备:创建ServiceConfigURL类型的mock Invoker,设置registerMode为ALL |
| 52 | + Invoker<?> invoker = Mockito.mock(Invoker.class); |
| 53 | + ServiceConfigURL url = Mockito.mock(ServiceConfigURL.class); |
| 54 | + when(invoker.getUrl()).thenReturn(url); |
| 55 | + when(url.getParameter(RegistryConstants.REGISTER_MODE_KEY, RegistryConstants.DEFAULT_REGISTER_MODE_INSTANCE)) |
| 56 | + .thenReturn(RegistryConstants.DEFAULT_REGISTER_MODE_ALL); |
| 57 | + |
| 58 | + // 执行 & 验证:registerMode为ALL应该返回true |
| 59 | + assertThat(DubboUtils.checkIsApplicationMode(invoker)).isTrue(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testCheckIsApplicationModeWithServiceConfigURL_RegisterModeInstance() { |
| 64 | + // 准备:创建ServiceConfigURL类型的mock Invoker,设置registerMode为INSTANCE |
| 65 | + Invoker<?> invoker = Mockito.mock(Invoker.class); |
| 66 | + ServiceConfigURL url = Mockito.mock(ServiceConfigURL.class); |
| 67 | + when(invoker.getUrl()).thenReturn(url); |
| 68 | + when(url.getParameter(RegistryConstants.REGISTER_MODE_KEY, RegistryConstants.DEFAULT_REGISTER_MODE_INSTANCE)) |
| 69 | + .thenReturn(RegistryConstants.DEFAULT_REGISTER_MODE_INSTANCE); |
| 70 | + |
| 71 | + // 执行 & 验证:registerMode为INSTANCE应该返回true |
| 72 | + assertThat(DubboUtils.checkIsApplicationMode(invoker)).isTrue(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testCheckIsApplicationModeWithServiceConfigURL_RegisterModeInterface() { |
| 77 | + // 准备:创建ServiceConfigURL类型的mock Invoker,设置registerMode为INTERFACE |
| 78 | + Invoker<?> invoker = Mockito.mock(Invoker.class); |
| 79 | + ServiceConfigURL url = Mockito.mock(ServiceConfigURL.class); |
| 80 | + when(invoker.getUrl()).thenReturn(url); |
| 81 | + when(url.getParameter(RegistryConstants.REGISTER_MODE_KEY, RegistryConstants.DEFAULT_REGISTER_MODE_INSTANCE)) |
| 82 | + .thenReturn(RegistryConstants.DEFAULT_REGISTER_MODE_INTERFACE); |
| 83 | + |
| 84 | + // 执行 & 验证:registerMode为INTERFACE应该返回false |
| 85 | + assertThat(DubboUtils.checkIsApplicationMode(invoker)).isFalse(); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testCheckIsApplicationModeWithOtherURL() { |
| 90 | + // 准备:创建普通URL类型的mock Invoker |
| 91 | + Invoker<?> invoker = Mockito.mock(Invoker.class); |
| 92 | + URL url = Mockito.mock(URL.class); |
| 93 | + when(invoker.getUrl()).thenReturn(url); |
| 94 | + |
| 95 | + // 执行 & 验证:普通URL类型应该返回false |
| 96 | + assertThat(DubboUtils.checkIsApplicationMode(invoker)).isFalse(); |
| 97 | + } |
| 98 | +} |
0 commit comments