From 5f641a9452e05b4be4716ba07be9738a5ae5c9a6 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Tue, 26 Mar 2019 15:47:34 +0800 Subject: [PATCH] Revert "Merge master branch to 3.x dev branch (#3718)" This reverts commit 897b80de3840c0bc9d9928dc1fb3f28ca8d78cc7. --- .travis.yml | 5 - CHANGES.md | 69 +- CONTRIBUTING.md | 2 +- LICENSE | 14 +- README.md | 69 +- codestyle/checkstyle.xml | 1 - dubbo-all/pom.xml | 85 +- dubbo-bom/pom.xml | 34 +- dubbo-cluster/pom.xml | 2 +- .../dubbo/rpc/cluster/Configurator.java | 1 - .../org/apache/dubbo/rpc/cluster/Router.java | 11 - .../override/OverrideConfigurator.java | 2 +- .../override/OverrideConfiguratorFactory.java | 2 +- .../configurator/parser/ConfigParser.java | 2 +- .../cluster/directory/StaticDirectory.java | 7 +- .../loadbalance/AbstractLoadBalance.java | 3 +- .../ConsistentHashLoadBalance.java | 10 +- .../rpc/cluster/router/AbstractRouter.java | 9 +- .../router/condition/ConditionRouter.java | 12 +- .../router/condition/config/AppRouter.java | 5 - .../condition/config/ListenableRouter.java | 4 +- .../condition/config/ServiceRouter.java | 5 - .../router/file/FileRouterFactory.java | 8 +- .../router/mock/MockInvokersSelector.java | 18 +- .../cluster/router/script/ScriptRouter.java | 112 +- .../rpc/cluster/router/tag/TagRouter.java | 83 +- .../support/AbstractClusterInvoker.java | 26 +- .../rpc/cluster/support/ClusterUtils.java | 4 +- .../support/wrapper/MockClusterInvoker.java | 3 +- .../apache/dubbo/rpc/cluster/StickyTest.java | 20 +- .../absent/AbsentConfiguratorTest.java | 20 +- .../override/OverrideConfiguratorTest.java | 20 +- .../configurator/parser/ConfigParserTest.java | 193 +- .../directory/StaticDirectoryTest.java | 10 +- .../ConsistentHashLoadBalanceTest.java | 48 +- .../loadbalance/LeastActiveBalanceTest.java | 17 +- .../loadbalance/LoadBalanceBaseTest.java | 42 +- .../loadbalance/RandomLoadBalanceTest.java | 19 +- .../RoundRobinLoadBalanceTest.java | 39 +- .../rpc/cluster/merger/ResultMergerTest.java | 122 +- .../router/ConfigConditionRouterTest.java | 10 +- .../rpc/cluster/router/TagRouterTest.java | 10 +- .../router/condition/ConditionRouterTest.java | 84 +- .../router/file/FileRouterEngineTest.java | 20 +- .../router/script/ScriptRouterTest.java | 81 +- .../support/AbstractClusterInvokerTest.java | 94 +- .../rpc/cluster/support/ClusterUtilsTest.java | 50 +- .../support/FailSafeClusterInvokerTest.java | 14 +- .../support/FailbackClusterInvokerTest.java | 60 +- .../support/FailfastClusterInvokerTest.java | 28 +- .../support/FailoverClusterInvokerTest.java | 16 +- .../support/ForkingClusterInvokerTest.java | 24 +- .../support/MergeableClusterInvokerTest.java | 14 +- .../wrapper/MockClusterInvokerTest.java | 120 +- dubbo-cluster/src/test/resources/log4j.xml | 1 - dubbo-common/pom.xml | 2 +- .../org/apache/dubbo/common/Constants.java | 65 +- .../org/apache/dubbo/common/Parameters.java | 38 +- .../org/apache/dubbo/common/Resetable.java | 60 +- .../java/org/apache/dubbo/common/URL.java | 189 +- .../org/apache/dubbo/common/URLBuilder.java | 350 --- .../java/org/apache/dubbo/common/Version.java | 158 +- .../dubbo/common/bytecode/ClassGenerator.java | 8 +- .../apache/dubbo/common/bytecode/Proxy.java | 22 +- .../apache/dubbo/common/bytecode/Wrapper.java | 78 +- .../common/compiler/support/ClassUtils.java | 27 +- .../compiler/support/CtClassBuilder.java | 174 -- .../compiler/support/JavassistCompiler.java | 97 +- .../common/concurrent/ExecutionList.java | 180 ++ .../common/config/ConfigurationUtils.java | 6 +- .../extension/AdaptiveClassCodeGenerator.java | 381 --- .../common/extension/ExtensionLoader.java | 481 ++-- .../extension/support/ActivateComparator.java | 129 +- .../org/apache/dubbo/common/json/JSON.java | 2 - .../apache/dubbo/common/json/JSONWriter.java | 1 - .../org/apache/dubbo/common/json/Yylex.java | 1 - .../apache/dubbo/common/logger/Logger.java | 40 +- .../dubbo/common/logger/LoggerFactory.java | 73 +- .../support/AbortPolicyWithReport.java | 63 +- .../dubbo/common/timer/HashedWheelTimer.java | 18 +- .../apache/dubbo/common/utils/CIDRUtils.java | 140 -- .../dubbo/common/utils/ClassHelper.java | 38 +- .../dubbo/common/utils/CollectionUtils.java | 14 +- .../common/utils/CompatibleTypeUtils.java | 13 +- .../dubbo/common/utils/ConfigUtils.java | 4 +- .../dubbo/common/utils/DubboAppender.java | 13 +- .../dubbo/common/utils/ExecutorUtil.java | 13 +- .../apache/dubbo/common/utils/NetUtils.java | 279 +-- .../apache/dubbo/common/utils/PojoUtils.java | 20 +- .../dubbo/common/utils/StringUtils.java | 76 +- .../apache/dubbo/common/utils/UrlUtils.java | 47 +- .../apache/dubbo/common/URLBuilderTest.java | 100 - .../java/org/apache/dubbo/common/URLTest.java | 40 +- .../common/beanutil/JavaBeanAccessorTest.java | 16 +- .../beanutil/JavaBeanSerializeUtilTest.java | 319 ++- .../common/bytecode/ClassGeneratorTest.java | 5 +- .../dubbo/common/bytecode/MixinTest.java | 7 +- .../dubbo/common/bytecode/ProxyTest.java | 48 +- .../dubbo/common/bytecode/WrapperTest.java | 60 +- .../support/AdaptiveCompilerTest.java | 6 +- .../compiler/support/ClassUtilsTest.java | 136 +- .../support/JavassistCompilerTest.java | 26 +- .../compiler/support/JdkCompilerTest.java | 36 +- .../concurrent/CompletableFutureTaskTest.java | 4 +- .../common/concurrent/ExecutionListTest.java | 94 + .../common/config/ConfigurationUtilsTest.java | 69 - .../config/PropertiesConfigurationTest.java | 6 +- .../common/extension/ExtensionLoaderTest.java | 77 +- .../ExtensionLoader_Adaptive_Test.java | 36 +- ...onLoader_Adaptive_UseJdkCompiler_Test.java | 8 +- .../ExtensionLoader_Compatible_Test.java | 4 +- .../extension/ext6_inject/impl/Ext6Impl1.java | 6 +- .../support/ActivateComparatorTest.java | 14 +- .../org/apache/dubbo/common/io/BytesTest.java | 29 +- .../dubbo/common/io/StreamUtilsTest.java | 87 +- .../io/UnsafeByteArrayInputStreamTest.java | 29 +- .../io/UnsafeByteArrayOutputStreamTest.java | 9 +- .../common/io/UnsafeStringReaderTest.java | 15 +- .../common/io/UnsafeStringWriterTest.java | 31 +- .../dubbo/common/json/JSONReaderTest.java | 4 +- .../apache/dubbo/common/json/JSONTest.java | 26 +- .../dubbo/common/json/JSONWriterTest.java | 5 +- .../common/logger/LoggerAdapterTest.java | 56 +- .../common/logger/LoggerFactoryTest.java | 14 +- .../dubbo/common/logger/LoggerTest.java | 51 +- .../common/logger/slf4j/Slf4jLoggerTest.java | 2 +- .../logger/support/FailsafeLoggerTest.java | 16 +- .../dubbo/common/status/StatusTest.java | 4 +- .../status/support/LoadStatusCheckerTest.java | 4 +- .../support/MemoryStatusCheckerTest.java | 4 +- .../status/support/StatusUtilsTest.java | 4 +- .../store/support/SimpleDataStoreTest.java | 10 +- .../threadlocal/InternalThreadLocalTest.java | 36 +- .../NamedInternalThreadFactoryTest.java | 6 +- .../support/AbortPolicyWithReportTest.java | 2 +- .../support/cached/CachedThreadPoolTest.java | 4 +- .../eager/EagerThreadPoolExecutorTest.java | 10 +- .../support/eager/EagerThreadPoolTest.java | 4 +- .../support/eager/TaskQueueTest.java | 27 +- .../support/fixed/FixedThreadPoolTest.java | 4 +- .../limited/LimitedThreadPoolTest.java | 4 +- .../common/timer/HashedWheelTimerTest.java | 2 +- .../dubbo/common/utils/ArrayUtilsTest.java | 6 +- .../apache/dubbo/common/utils/AssertTest.java | 32 +- .../utils/AtomicPositiveIntegerTest.java | 29 +- .../dubbo/common/utils/CIDRUtilsTest.java | 56 - .../dubbo/common/utils/ClassHelperTest.java | 44 +- .../common/utils/CollectionUtilsTest.java | 21 +- .../common/utils/CompatibleTypeUtilsTest.java | 10 +- .../dubbo/common/utils/ConfigUtilsTest.java | 22 +- .../dubbo/common/utils/DubboAppenderTest.java | 12 +- .../dubbo/common/utils/ExecutorUtilTest.java | 4 +- .../apache/dubbo/common/utils/HolderTest.java | 4 +- .../dubbo/common/utils/IOUtilsTest.java | 29 +- .../dubbo/common/utils/LRUCacheTest.java | 8 +- .../dubbo/common/utils/LogHelperTest.java | 2 +- .../apache/dubbo/common/utils/LogTest.java | 4 +- .../dubbo/common/utils/LogUtilTest.java | 8 +- .../common/utils/NamedThreadFactoryTest.java | 10 +- .../dubbo/common/utils/NetUtilsTest.java | 123 +- .../dubbo/common/utils/ParametersTest.java | 6 +- .../dubbo/common/utils/PojoUtilsTest.java | 64 +- .../dubbo/common/utils/ReflectUtilsTest.java | 34 +- .../apache/dubbo/common/utils/StackTest.java | 45 +- .../dubbo/common/utils/StringUtilsTest.java | 39 +- .../dubbo/common/utils/UrlUtilsTest.java | 12 +- .../dubbo/common/version/VersionTest.java | 44 +- dubbo-common/src/test/resources/log4j.xml | 28 +- dubbo-compatible/pom.xml | 2 +- .../java/com/alibaba/dubbo/common/URL.java | 7 +- .../dubbo/config/annotation/Reference.java | 2 +- .../java/com/alibaba/dubbo/rpc/Invoker.java | 19 +- .../java/com/alibaba/dubbo/rpc/Protocol.java | 16 - .../com/alibaba/dubbo/rpc/cluster/Router.java | 12 +- .../CompatibleReferenceBeanBuilder.java | 51 +- ...patibleAnnotationBeanDefinitionParser.java | 4 +- .../org/apache/dubbo/cache/CacheTest.java | 6 +- .../dubbo/common/extension/ExtensionTest.java | 16 +- .../common/extension/MockDispatcher.java | 29 - .../dubbo/config/ApplicationConfigTest.java | 13 +- .../dubbo/config/ArgumentConfigTest.java | 4 +- .../org/apache/dubbo/config/ConfigTest.java | 14 +- .../dubbo/config/ConsumerConfigTest.java | 4 +- .../apache/dubbo/config/MethodConfigTest.java | 6 +- .../apache/dubbo/config/ModuleConfigTest.java | 15 +- .../dubbo/config/ProtocolConfigTest.java | 4 +- .../dubbo/config/ProviderConfigTest.java | 4 +- .../dubbo/config/ReferenceConfigTest.java | 10 +- .../dubbo/config/RegistryConfigTest.java | 4 +- .../apache/dubbo/echo/EchoServiceTest.java | 8 +- .../org/apache/dubbo/filter/FilterTest.java | 31 +- .../apache/dubbo/filter/LegacyInvocation.java | 88 - .../{LegacyInvoker.java => MyInvoker.java} | 148 +- .../dubbo/generic/GenericServiceTest.java | 67 +- .../dubbo/rpc/cluster/CompatibleRouter.java | 1 - .../apache/dubbo/rpc/cluster/RouterTest.java | 53 - .../serialization/SerializationTest.java | 8 +- .../apache/dubbo/service/ComplexObject.java | 15 +- .../org.apache.dubbo.remoting.Dispatcher | 18 - dubbo-config/dubbo-config-api/pom.xml | 2 +- .../apache/dubbo/config/AbstractConfig.java | 38 +- .../dubbo/config/AbstractInterfaceConfig.java | 46 +- .../dubbo/config/AbstractMethodConfig.java | 2 +- .../dubbo/config/AbstractReferenceConfig.java | 4 +- .../dubbo/config/AbstractServiceConfig.java | 9 +- .../dubbo/config/ApplicationConfig.java | 3 +- .../apache/dubbo/config/ArgumentConfig.java | 12 +- .../dubbo/config/ConfigCenterConfig.java | 18 +- .../apache/dubbo/config/ConsumerConfig.java | 16 +- .../dubbo/config/MetadataReportConfig.java | 2 +- .../org/apache/dubbo/config/MethodConfig.java | 45 +- .../org/apache/dubbo/config/ModuleConfig.java | 6 +- .../apache/dubbo/config/ProtocolConfig.java | 8 +- .../apache/dubbo/config/ProviderConfig.java | 4 +- .../apache/dubbo/config/ReferenceConfig.java | 143 +- .../apache/dubbo/config/ServiceConfig.java | 189 +- .../dubbo/config/annotation/Argument.java | 44 - .../dubbo/config/annotation/Method.java | 69 - .../dubbo/config/annotation/Reference.java | 10 +- .../dubbo/config/annotation/Service.java | 6 - .../config/builders/AbstractBuilder.java | 69 - .../builders/AbstractInterfaceBuilder.java | 294 --- .../builders/AbstractMethodBuilder.java | 188 -- .../builders/AbstractReferenceBuilder.java | 142 -- .../builders/AbstractServiceBuilder.java | 246 -- .../config/builders/ApplicationBuilder.java | 193 -- .../config/builders/ArgumentBuilder.java | 63 - .../config/builders/ConfigCenterBuilder.java | 148 -- .../config/builders/ConsumerBuilder.java | 118 - .../dubbo/config/builders/DubboBuilders.java | 72 - .../builders/MetadataReportBuilder.java | 140 -- .../dubbo/config/builders/MethodBuilder.java | 237 -- .../dubbo/config/builders/ModuleBuilder.java | 132 -- .../dubbo/config/builders/MonitorBuilder.java | 137 -- .../config/builders/ProtocolBuilder.java | 419 ---- .../config/builders/ProviderBuilder.java | 293 --- .../config/builders/ReferenceBuilder.java | 134 -- .../config/builders/RegistryBuilder.java | 309 --- .../dubbo/config/builders/ServiceBuilder.java | 152 -- .../config/utils/ReferenceConfigCache.java | 55 +- .../dubbo/config/AbstractConfigTest.java | 226 +- .../config/AbstractInterfaceConfigTest.java | 324 ++- .../config/AbstractMethodConfigTest.java | 4 +- .../config/AbstractReferenceConfigTest.java | 4 +- .../config/AbstractServiceConfigTest.java | 4 +- .../dubbo/config/ApplicationConfigTest.java | 13 +- .../dubbo/config/ArgumentConfigTest.java | 4 +- .../dubbo/config/ConfigCenterConfigTest.java | 7 +- .../dubbo/config/ConsumerConfigTest.java | 4 +- .../apache/dubbo/config/MethodConfigTest.java | 56 +- .../apache/dubbo/config/ModuleConfigTest.java | 18 +- .../dubbo/config/MonitorConfigTest.java | 4 +- .../dubbo/config/ProtocolConfigTest.java | 4 +- .../dubbo/config/ProviderConfigTest.java | 4 +- .../dubbo/config/ReferenceConfigTest.java | 53 +- .../dubbo/config/RegistryConfigTest.java | 4 +- .../dubbo/config/ServiceConfigTest.java | 83 +- .../config/builders/AbstractBuilderTest.java | 125 - .../AbstractInterfaceBuilderTest.java | 310 --- .../builders/AbstractMethodBuilderTest.java | 195 -- .../AbstractReferenceBuilderTest.java | 148 -- .../builders/AbstractServiceBuilderTest.java | 245 -- .../builders/ApplicationBuilderTest.java | 255 -- .../config/builders/ArgumentBuilderTest.java | 63 - .../builders/ConfigCenterBuilderTest.java | 177 -- .../config/builders/ConsumerBuilderTest.java | 95 - .../builders/MetadataReportBuilderTest.java | 151 -- .../config/builders/MethodBuilderTest.java | 189 -- .../config/builders/ModuleBuilderTest.java | 112 - .../config/builders/MonitorBuilderTest.java | 135 -- .../config/builders/ProtocolBuilderTest.java | 338 --- .../config/builders/ProviderBuilderTest.java | 227 -- .../config/builders/ReferenceBuilderTest.java | 114 - .../config/builders/RegistryBuilderTest.java | 256 --- .../config/builders/ServiceBuilderTest.java | 131 -- .../apache/dubbo/config/cache/CacheTest.java | 16 +- .../DelegateProviderMetaDataInvokerTest.java | 8 +- .../config/url/ExporterSideConfigUrlTest.java | 14 +- .../config/url/InvokerSideConfigUrlTest.java | 18 +- .../apache/dubbo/config/url/UrlTestBase.java | 2 +- .../utils/ReferenceConfigCacheTest.java | 20 +- dubbo-config/dubbo-config-spring/pom.xml | 37 +- .../dubbo/config/spring/AnnotationBean.java | 321 +++ .../dubbo/config/spring/ReferenceBean.java | 13 +- .../dubbo/config/spring/ServiceBean.java | 662 +++--- .../AbstractAnnotationConfigBeanBuilder.java | 4 +- .../AnnotationInjectedBeanPostProcessor.java | 531 ----- .../DubboConfigBindingBeanPostProcessor.java | 2 +- .../ReferenceAnnotationBeanPostProcessor.java | 591 ++++- .../annotation/ReferenceBeanBuilder.java | 59 +- .../ServiceAnnotationBeanPostProcessor.java | 39 +- .../annotation/ServiceBeanNameBuilder.java | 113 - .../DubboConfigBindingRegistrar.java | 8 +- .../annotation/DubboConfigConfiguration.java | 1 - ... => DubboConfigConfigurationSelector.java} | 26 +- .../context/annotation/EnableDubboConfig.java | 2 +- .../annotation/EnableDubboConfigBinding.java | 2 - .../event/ServiceBeanExportedEvent.java | 51 - .../properties/DefaultDubboConfigBinder.java | 3 +- .../converter/StringArrayToMapConverter.java | 30 +- .../StringArrayToStringConverter.java} | 21 +- .../AnnotationBeanDefinitionParser.java | 4 +- .../schema/DubboBeanDefinitionParser.java | 10 +- .../status/DataSourceStatusChecker.java | 34 +- .../AnnotatedBeanDefinitionRegistryUtils.java | 62 - .../config/spring/util/AnnotationUtils.java | 230 +- .../config/spring/util/BeanFactoryUtils.java | 59 +- .../dubbo/config/spring/util/ClassUtils.java | 37 - .../dubbo/config/spring/util/ObjectUtils.java | 14 +- .../spring/util/PropertySourcesUtils.java | 60 +- .../main/resources/META-INF/compat/dubbo.xsd | 25 + .../src/main/resources/META-INF/dubbo.xsd | 6 - .../spring/AbstractRegistryService.java | 6 +- .../dubbo/config/spring/ConfigTest.java | 2041 ++++++++--------- .../dubbo/config/spring/ServiceBeanTest.java | 7 +- .../config/spring/SimpleRegistryExporter.java | 8 +- .../config/spring/SimpleRegistryService.java | 3 +- .../spring/action/DemoActionByAnnotation.java | 67 +- .../annotation/consumer/AnnotationAction.java | 74 +- .../AnnotationPropertyValuesAdapterTest.java | 6 +- ...bboConfigBindingBeanPostProcessorTest.java | 6 +- ...erenceAnnotationBeanPostProcessorTest.java | 215 +- .../annotation/ReferenceBeanBuilderTest.java | 126 - ...erviceAnnotationBeanPostProcessorTest.java | 46 +- .../ServiceAnnotationTestConfiguration.java | 116 - .../ServiceBeanNameBuilderTest.java | 75 - .../config/YamlPropertySourceFactory.java | 76 - .../config/YamlPropertySourceFactoryTest.java | 71 - .../DubboComponentScanRegistrarTest.java | 32 +- .../DubboConfigBindingRegistrarTest.java | 12 +- .../DubboConfigBindingsRegistrarTest.java | 8 +- .../DubboConfigConfigurationTest.java | 30 +- .../annotation/EnableDubboConfigTest.java | 28 +- .../context/annotation/EnableDubboTest.java | 75 +- .../consumer/ConsumerConfiguration.java | 6 +- .../test/TestConsumerConfiguration.java | 10 +- .../provider/DefaultHelloService.java | 1 - .../annotation/provider/DemoServiceImpl.java | 1 - .../provider/ProviderConfiguration.java | 8 +- .../DubboComponentScanRegistrarTest.java | 116 - .../DubboConfigBindingRegistrarTest.java | 88 - .../DubboConfigBindingsRegistrarTest.java | 63 - .../DubboConfigConfigurationTest.java | 99 - .../annotation/EnableDubboConfigTest.java | 109 - .../context/annotation/EnableDubboTest.java | 162 -- .../consumer/ConsumerConfiguration.java | 126 - .../test/TestConsumerConfiguration.java | 96 - .../annotation/provider/DemoServiceImpl.java | 55 - .../provider/ProviderConfiguration.java | 107 - .../DefaultDubboConfigBinderTest.java | 94 - .../DefaultDubboConfigBinderTest.java | 15 +- .../StringArrayToMapConverterTest.java | 52 +- .../StringArrayToStringConverterTest.java | 40 +- .../extension/SpringExtensionFactoryTest.java | 22 +- .../schema/DubboNamespaceHandlerTest.java | 39 +- .../status/DataSourceStatusCheckerTest.java | 12 +- .../status/SpringStatusCheckerTest.java | 14 +- .../spring/util/BeanFactoryUtilsTest.java | 20 +- .../spring/util/PropertySourcesUtilsTest.java | 49 +- .../resources/META-INF/default.properties | 2 +- .../META-INF/dubbb-consumer.properties | 12 - .../META-INF/dubbb-provider.properties | 24 - .../META-INF/dubbo-consumer.properties | 4 +- .../META-INF/dubbo-provider.properties | 8 +- .../src/test/resources/META-INF/dubbo.yml | 8 - dubbo-config/pom.xml | 2 +- .../dubbo-configcenter-api/pom.xml | 5 +- .../dubbo/configcenter/ConfigChangeEvent.java | 25 +- .../AbstractDynamicConfigurationTest.java | 8 +- .../dubbo-configcenter-apollo/pom.xml | 5 +- .../apollo/ApolloDynamicConfiguration.java | 39 +- .../dubbo-configcenter-consul/pom.xml | 44 - .../consul/ConsulDynamicConfiguration.java | 166 -- .../ConsulDynamicConfigurationFactory.java | 32 - ...o.configcenter.DynamicConfigurationFactory | 1 - .../dubbo-configcenter-etcd/pom.xml | 46 - .../etcd/EtcdDynamicConfiguration.java | 187 -- .../etcd/EtcdDynamicConfigurationFactory.java | 33 - ...o.configcenter.DynamicConfigurationFactory | 1 - .../etcd/EtcdDynamicConfigurationTest.java | 141 -- .../dubbo-configcenter-zookeeper/pom.xml | 20 +- .../support/zookeeper/CacheListener.java | 91 +- .../ZookeeperDynamicConfiguration.java | 71 +- .../ZookeeperDynamicConfigurationFactory.java | 11 +- .../ZookeeperDynamicConfigurationTest.java | 35 +- dubbo-configcenter/pom.xml | 9 +- dubbo-container/dubbo-container-api/pom.xml | 4 +- .../org/apache/dubbo/container/Container.java | 4 +- .../java/org/apache/dubbo/container/Main.java | 6 +- .../resources/META-INF/assembly/bin/start.sh | 2 +- dubbo-container/dubbo-container-log4j/pom.xml | 2 +- .../dubbo/container/log4j/Log4jContainer.java | 5 +- .../container/log4j/Log4jContainerTest.java | 2 +- .../dubbo-container-logback/pom.xml | 2 +- .../container/logback/LogbackContainer.java | 4 +- .../logback/LogbackContainerTest.java | 2 +- .../dubbo-container-spring/pom.xml | 84 +- .../container/spring/SpringContainer.java | 8 +- .../container/spring/SpringContainerTest.java | 6 +- dubbo-container/pom.xml | 2 +- .../dubbo-demo-annotation-consumer/pom.xml | 6 +- .../dubbo-demo-annotation-provider/pom.xml | 6 +- dubbo-demo/dubbo-demo-annotation/pom.xml | 6 +- .../dubbo-demo-api-consumer/pom.xml | 6 +- .../dubbo-demo-api-provider/pom.xml | 6 +- dubbo-demo/dubbo-demo-api/pom.xml | 6 +- dubbo-demo/dubbo-demo-interface/pom.xml | 2 +- .../dubbo-demo-xml-consumer/pom.xml | 2 +- .../dubbo-demo-xml-provider/pom.xml | 2 +- dubbo-demo/dubbo-demo-xml/pom.xml | 6 +- dubbo-demo/pom.xml | 2 +- dubbo-dependencies-bom/pom.xml | 95 +- .../dubbo-dependencies-zookeeper/pom.xml | 81 - dubbo-dependencies/pom.xml | 36 - dubbo-distribution/pom.xml | 4 +- .../src/assembly/source-release.xml | 4 - dubbo-filter/dubbo-filter-cache/pom.xml | 94 +- .../dubbo/cache/support/jcache/JCache.java | 3 +- .../dubbo/cache/filter/CacheFilterTest.java | 82 +- .../expiring/ExpiringCacheFactoryTest.java | 4 +- .../support/jcache/JCacheFactoryTest.java | 6 +- .../support/lru/LruCacheFactoryTest.java | 4 +- .../ThreadLocalCacheFactoryTest.java | 4 +- dubbo-filter/dubbo-filter-validation/pom.xml | 142 +- .../filter/ValidationFilterTest.java | 40 +- .../support/jvalidation/JValidationTest.java | 24 +- .../support/jvalidation/JValidatorTest.java | 24 +- dubbo-filter/pom.xml | 2 +- .../dubbo-metadata-definition/pom.xml | 6 +- .../definition/TypeDefinitionBuilder.java | 23 +- .../definition/builder/ArrayTypeBuilder.java | 10 +- .../builder/CollectionTypeBuilder.java | 16 +- .../definition/builder/EnumTypeBuilder.java | 7 +- .../definition/builder/MapTypeBuilder.java | 10 +- .../definition/model/MethodDefinition.java | 8 +- .../definition/model/ServiceDefinition.java | 8 +- .../definition/model/TypeDefinition.java | 8 +- .../util/JaketConfigurationUtils.java | 8 +- .../metadata/definition/MetadataTest.java | 56 +- .../ServiceDefinitionBuildderTest.java | 44 +- .../definition/service/ComplexObject.java | 4 +- .../dubbo-metadata-report-api/pom.xml | 15 +- .../integration/MetadataReportService.java | 8 +- .../support/AbstractMetadataReport.java | 71 +- .../identifier/MetadataIdentifierTest.java | 9 +- .../MetadataReportServiceTest.java | 46 +- .../store/test/JTestMetadataReport4Test.java | 3 +- .../test/JTestMetadataReportFactory4Test.java | 9 +- .../AbstractMetadataReportFactoryTest.java | 12 +- .../support/AbstractMetadataReportTest.java | 86 +- .../dubbo-metadata-report-consul/pom.xml | 43 - .../store/consul/ConsulMetadataReport.java | 64 - .../consul/ConsulMetadataReportFactory.java | 32 - ...dubbo.metadata.store.MetadataReportFactory | 1 - .../dubbo-metadata-report-redis/pom.xml | 6 +- .../store/redis/RedisMetadataReportTest.java | 18 +- .../dubbo-metadata-report-zookeeper/pom.xml | 6 +- .../ZookeeperMetadataReportTest.java | 30 +- dubbo-metadata-report/pom.xml | 7 +- dubbo-metrics/dubbo-metrics-api/pom.xml | 30 + .../apache/dubbo/metrics/BucketCounter.java | 57 + .../org/apache/dubbo/metrics/Compass.java | 75 + .../org/apache/dubbo/metrics/Counter.java | 37 +- .../org/apache/dubbo/metrics/Counting.java | 14 +- .../java/org/apache/dubbo/metrics/Gauge.java | 32 +- .../apache/dubbo/metrics/IMetricManager.java | 104 + .../java/org/apache/dubbo/metrics/Metric.java | 21 +- .../apache/dubbo/metrics/MetricFilter.java | 42 +- .../org/apache/dubbo/metrics/MetricLevel.java | 53 + .../apache/dubbo/metrics/MetricManager.java | 108 + .../org/apache/dubbo/metrics/MetricName.java | 389 ++++ .../apache/dubbo/metrics/MetricRegistry.java | 157 ++ .../org/apache/dubbo/metrics/MetricSet.java | 22 +- .../dubbo/metrics/NOPMetricManager.java | 292 +++ .../dubbo/metrics/MetricManagerTest.java | 53 + .../apache/dubbo/metrics/MetricNameTest.java | 138 ++ dubbo-metrics/pom.xml | 41 + dubbo-monitor/dubbo-monitor-api/pom.xml | 46 +- .../support/AbstractMonitorFactory.java | 2 +- .../dubbo/monitor/support/MetricsFilter.java | 102 - .../internal/org.apache.dubbo.rpc.Filter | 3 +- .../support/AbstractMonitorFactoryTest.java | 10 +- .../monitor/support/MetricsFilterTest.java | 167 -- .../monitor/support/MonitorFilterTest.java | 44 +- dubbo-monitor/dubbo-monitor-default/pom.xml | 2 +- .../dubbo/monitor/dubbo/DubboMonitor.java | 3 +- .../monitor/dubbo/DubboMonitorFactory.java | 135 +- .../dubbo/DubboMonitorFactoryTest.java | 8 +- .../dubbo/monitor/dubbo/DubboMonitorTest.java | 58 +- .../dubbo/monitor/dubbo/StatisticsTest.java | 45 +- dubbo-monitor/pom.xml | 2 +- dubbo-plugin/dubbo-qos/pom.xml | 2 +- .../apache/dubbo/qos/command/impl/Online.java | 3 +- .../qos/protocol/QosProtocolWrapper.java | 12 +- .../org/apache/dubbo/qos/server/Server.java | 2 +- .../server/handler/HttpProcessHandler.java | 14 +- .../qos/server/handler/QosProcessHandler.java | 10 - .../java/org/apache/dubbo/qos/textui/TKv.java | 8 +- .../org/apache/dubbo/qos/textui/TTable.java | 5 +- .../org/apache/dubbo/qos/textui/TTree.java | 8 +- .../command/CommandContextFactoryTest.java | 6 +- .../dubbo/qos/command/CommandContextTest.java | 8 +- .../command/DefaultCommandExecutorTest.java | 13 +- .../dubbo/qos/command/GreetingCommand.java | 3 +- .../decoder/HttpCommandDecoderTest.java | 4 +- .../decoder/TelnetCommandDecoderTest.java | 4 +- .../dubbo/qos/command/impl/HelpTest.java | 4 +- .../apache/dubbo/qos/command/impl/LsTest.java | 4 +- .../dubbo/qos/command/impl/OfflineTest.java | 4 +- .../dubbo/qos/command/impl/OnlineTest.java | 6 +- .../dubbo/qos/command/impl/QuitTest.java | 4 +- .../qos/command/util/CommandHelperTest.java | 10 +- .../qos/protocol/QosProtocolWrapperTest.java | 12 +- .../handler/HttpProcessHandlerTest.java | 4 +- .../handler/LocalHostPermitHandlerTest.java | 4 +- .../server/handler/QosProcessHandlerTest.java | 2 +- .../handler/TelnetProcessHandlerTest.java | 4 +- .../org/apache/dubbo/qos/textui/TKvTest.java | 10 +- .../apache/dubbo/qos/textui/TLadderTest.java | 12 +- .../apache/dubbo/qos/textui/TTableTest.java | 20 +- .../apache/dubbo/qos/textui/TTreeTest.java | 4 +- dubbo-plugin/pom.xml | 2 +- dubbo-registry/dubbo-registry-api/pom.xml | 2 +- .../integration/RegistryDirectory.java | 105 +- .../integration/RegistryProtocol.java | 57 +- .../registry/support/AbstractRegistry.java | 119 +- .../support/AbstractRegistryFactory.java | 8 +- .../registry/support/FailbackRegistry.java | 3 +- .../support/ProviderConsumerRegTable.java | 6 +- .../registry/PerformanceRegistryTest.java | 6 +- .../support/AbstractRegistryFactoryTest.java | 18 +- .../support/AbstractRegistryTest.java | 307 ++- .../support/FailbackRegistryTest.java | 20 +- dubbo-registry/dubbo-registry-consul/pom.xml | 43 - .../dubbo/registry/consul/ConsulRegistry.java | 300 --- .../consul/ConsulRegistryFactory.java | 32 - .../org.apache.dubbo.registry.RegistryFactory | 1 - dubbo-registry/dubbo-registry-default/pom.xml | 2 +- .../dubbo/registry/dubbo/DubboRegistry.java | 20 +- .../registry/dubbo/DubboRegistryFactory.java | 13 +- .../registry/dubbo/DubboRegistryTest.java | 8 +- .../registry/dubbo/RegistryDirectoryTest.java | 290 ++- .../registry/dubbo/RegistryProtocolTest.java | 31 +- .../dubbo/RegistryStatusCheckerTest.java | 14 +- .../dubbo/SimpleRegistryExporter.java | 8 +- .../src/test/resources/log4j.xml | 7 +- dubbo-registry/dubbo-registry-etcd3/pom.xml | 53 - .../dubbo/registry/etcd/EtcdRegistry.java | 362 --- .../registry/etcd/EtcdRegistryFactory.java | 53 - .../org.apache.dubbo.registry.RegistryFactory | 1 - .../dubbo/registry/etcd/EtcdRegistryTest.java | 316 --- .../dubbo-registry-multicast/pom.xml | 2 +- .../registry/multicast/MulticastRegistry.java | 74 +- .../MulticastRegistryFactoryTest.java | 4 +- .../multicast/MulticastRegistryTest.java | 77 +- dubbo-registry/dubbo-registry-redis/pom.xml | 2 +- .../dubbo/registry/redis/RedisRegistry.java | 109 +- .../registry/redis/RedisRegistryTest.java | 22 +- .../dubbo-registry-zookeeper/pom.xml | 2 +- .../registry/zookeeper/ZookeeperRegistry.java | 74 +- .../zookeeper/ZookeeperRegistryFactory.java | 4 - .../zookeeper/ZookeeperRegistryTest.java | 44 +- dubbo-registry/pom.xml | 4 +- dubbo-remoting/dubbo-remoting-api/pom.xml | 2 +- .../org/apache/dubbo/remoting/Client.java | 4 +- .../apache/dubbo/remoting/IdleSensible.java | 37 - .../org/apache/dubbo/remoting/Server.java | 4 +- .../dubbo/remoting/exchange/Request.java | 8 +- .../dubbo/remoting/exchange/Response.java | 4 - .../exchange/codec/ExchangeCodec.java | 7 +- .../exchange/support/DefaultFuture.java | 7 +- .../support/header/AbstractTimerTask.java | 10 - .../support/header/CloseTimerTask.java | 55 - .../support/header/HeaderExchangeClient.java | 78 +- .../support/header/HeaderExchangeServer.java | 84 +- .../support/header/ReconnectTimerTask.java | 34 +- .../remoting/telnet/codec/TelnetCodec.java | 16 +- .../telnet/support/TelnetHandlerAdapter.java | 19 +- .../support/command/HelpTelnetHandler.java | 3 +- .../support/command/LogTelnetHandler.java | 10 +- .../support/command/StatusTelnetHandler.java | 5 +- .../remoting/transport/AbstractClient.java | 131 +- .../remoting/transport/AbstractServer.java | 2 +- .../transport/ChannelHandlerDispatcher.java | 3 +- .../dubbo/remoting/ChanelHandlerTest.java | 6 +- .../remoting/PerformanceClientCloseTest.java | 6 +- .../remoting/PerformanceClientFixedTest.java | 6 +- .../dubbo/remoting/PerformanceClientTest.java | 6 +- .../dubbo/remoting/PerformanceServerTest.java | 6 +- .../buffer/AbstractChannelBufferTest.java | 162 +- .../buffer/ChannelBufferStreamTest.java | 12 +- .../buffer/DirectChannelBufferTest.java | 4 +- .../buffer/DynamicChannelBufferTest.java | 4 +- .../buffer/HeapChannelBufferTest.java | 4 +- .../remoting/codec/ExchangeCodecTest.java | 132 +- .../dubbo/remoting/codec/TelnetCodecTest.java | 58 +- .../exchange/support/DefaultFutureTest.java | 15 +- .../header/HeaderExchangeChannelTest.java | 139 +- .../support/header/HeartBeatTaskTest.java | 14 +- .../handler/ConnectChannelHandlerTest.java | 64 +- .../handler/HeaderExchangeHandlerTest.java | 56 +- .../handler/WrappedChannelHandlerTest.java | 24 +- .../remoting/transport/AbstractCodecTest.java | 6 +- .../transport/codec/CodecAdapterTest.java | 4 +- .../codec/DeprecatedExchangeCodec.java | 4 +- .../codec/DeprecatedTelnetCodec.java | 2 +- dubbo-remoting/dubbo-remoting-etcd3/pom.xml | 52 - .../dubbo/remoting/etcd/ChildListener.java | 25 - .../dubbo/remoting/etcd/EtcdClient.java | 183 -- .../dubbo/remoting/etcd/EtcdTransporter.java | 47 - .../remoting/etcd/jetcd/JEtcdClient.java | 433 ---- .../etcd/jetcd/JEtcdClientWrapper.java | 714 ------ .../dubbo/remoting/etcd/jetcd/RetryLoops.java | 95 - .../remoting/etcd/jetcd/RetryNTimes.java | 36 - .../remoting/etcd/option/OptionUtil.java | 76 - .../etcd/support/AbstractEtcdClient.java | 194 -- ...apache.dubbo.remoting.etcd.EtcdTransporter | 1 - .../remoting/etcd/jetcd/JEtcdClientTest.java | 426 ---- .../etcd/jetcd/JEtcdClientWrapperTest.java | 186 -- dubbo-remoting/dubbo-remoting-grizzly/pom.xml | 2 +- .../grizzly/GrizzlyTransporterTest.java | 4 +- dubbo-remoting/dubbo-remoting-http/pom.xml | 2 +- .../http/tomcat/TomcatHttpServer.java | 3 - .../http/jetty/JettyHttpBinderTest.java | 2 +- .../http/tomcat/TomcatHttpBinderTest.java | 3 +- dubbo-remoting/dubbo-remoting-mina/pom.xml | 2 +- .../remoting/transport/mina/MinaServer.java | 2 +- .../transport/mina/ClientToServerTest.java | 23 +- .../remoting/transport/mina/ClientsTest.java | 29 +- dubbo-remoting/dubbo-remoting-netty/pom.xml | 29 +- .../netty/NettyBackedChannelBuffer.java | 2 +- .../remoting/transport/netty/NettyServer.java | 3 +- .../support/header/HeartbeatHandlerTest.java | 19 +- .../transport/netty/ClientReconnectTest.java | 47 +- .../transport/netty/ClientToServerTest.java | 19 +- .../remoting/transport/netty/ClientsTest.java | 29 +- .../transport/netty/NettyClientTest.java | 14 +- .../transport/netty/NettyStringTest.java | 12 +- .../transport/netty/ThreadNameTest.java | 14 +- .../src/test/resources/log4j.xml | 1 - dubbo-remoting/dubbo-remoting-netty4/pom.xml | 2 +- .../netty4/NettyBackedChannelBuffer.java | 122 +- .../transport/netty4/NettyClient.java | 12 +- .../transport/netty4/NettyClientHandler.java | 81 +- .../transport/netty4/NettyCodecAdapter.java | 14 +- .../transport/netty4/NettyServer.java | 12 - .../transport/netty4/NettyServerHandler.java | 20 +- .../netty4/logging/FormattingTuple.java | 4 +- .../netty4/logging/MessageFormatter.java | 9 +- .../transport/netty4/ClientReconnectTest.java | 20 +- .../transport/netty4/ClientToServerTest.java | 20 +- .../transport/netty4/ClientsTest.java | 28 +- .../netty4/NettyTransporterTest.java | 7 +- dubbo-remoting/dubbo-remoting-p2p/pom.xml | 5 +- .../exchange/support/FileExchangeGroup.java | 5 +- .../dubbo/remoting/p2p/support/FileGroup.java | 5 +- .../MulticastExchangeNetworkerTest.java | 4 +- .../p2p/support/FileNetworkerTest.java | 30 +- .../p2p/support/MulticastNetworkerTest.java | 4 +- .../dubbo-remoting-zookeeper/pom.xml | 45 +- .../dubbo/remoting/zookeeper/EventType.java | 65 - .../remoting/zookeeper/ZookeeperClient.java | 16 - .../curator/CuratorZookeeperClient.java | 158 +- .../curator/CuratorZookeeperTransporter.java | 2 +- .../support/AbstractZookeeperClient.java | 47 +- .../support/AbstractZookeeperTransporter.java | 1 - .../zookeeper/zkclient/ZkClientWrapper.java | 144 ++ .../zkclient/ZkclientZookeeperClient.java | 168 ++ .../ZkclientZookeeperTransporter.java} | 13 +- ...bo.remoting.zookeeper.ZookeeperTransporter | 3 +- .../curator/CuratorZookeeperClientTest.java | 81 +- .../CuratorZookeeperTransporterTest.java | 20 +- .../AbstractZookeeperTransporterTest.java | 100 +- .../zkclient/ZkClientWrapperTest.java | 56 + .../zkclient/ZkclientZookeeperClientTest.java | 140 ++ .../ZkclientZookeeperTransporterTest.java | 53 + dubbo-remoting/pom.xml | 3 +- dubbo-rpc/dubbo-rpc-api/pom.xml | 94 +- .../org/apache/dubbo/rpc/AbstractResult.java | 4 +- .../java/org/apache/dubbo/rpc/RpcContext.java | 6 +- .../org/apache/dubbo/rpc/RpcException.java | 5 +- .../org/apache/dubbo/rpc/RpcInvocation.java | 3 +- .../dubbo/rpc/filter/AccessLogFilter.java | 222 +- .../dubbo/rpc/filter/ActiveLimitFilter.java | 6 +- .../dubbo/rpc/filter/ContextFilter.java | 3 +- .../dubbo/rpc/filter/GenericFilter.java | 3 +- .../dubbo/rpc/filter/TimeoutFilter.java | 40 +- .../rpc/filter/tps/DefaultTPSLimiter.java | 15 +- .../apache/dubbo/rpc/filter/tps/StatItem.java | 37 +- .../rpc/listener/ListenerExporterWrapper.java | 5 +- .../rpc/listener/ListenerInvokerWrapper.java | 5 +- .../dubbo/rpc/protocol/AbstractInvoker.java | 9 +- .../rpc/protocol/AbstractProxyProtocol.java | 6 +- .../dubbo/rpc/proxy/jdk/JdkProxyFactory.java | 2 +- .../wrapper/StubProxyFactoryWrapper.java | 14 +- .../dubbo/rpc/service/GenericService.java | 2 +- .../dubbo/rpc/support/AccessLogData.java | 266 --- .../apache/dubbo/rpc/support/MockInvoker.java | 5 +- .../dubbo/rpc/support/ProtocolUtils.java | 3 +- .../apache/dubbo/rpc/support/RpcUtils.java | 5 +- .../org/apache/dubbo/rpc/RpcContextTest.java | 80 +- .../org/apache/dubbo/rpc/RpcResultTest.java | 14 +- .../dubbo/rpc/filter/AccessLogFilterTest.java | 6 +- .../rpc/filter/ActiveLimitFilterTest.java | 43 +- .../rpc/filter/ClassLoaderFilterTest.java | 6 +- .../filter/CompatibleFilterFilterTest.java | 10 +- .../rpc/filter/ConsumerContextFilterTest.java | 4 +- .../dubbo/rpc/filter/ContextFilterTest.java | 4 +- .../rpc/filter/DeprecatedFilterTest.java | 4 +- .../dubbo/rpc/filter/EchoFilterTest.java | 4 +- .../dubbo/rpc/filter/ExceptionFilterTest.java | 16 +- .../rpc/filter/ExecuteLimitFilterTest.java | 14 +- .../dubbo/rpc/filter/GenericFilterTest.java | 64 +- .../rpc/filter/GenericImplFilterTest.java | 18 +- .../dubbo/rpc/filter/TimeoutFilterTest.java | 8 +- .../dubbo/rpc/filter/TokenFilterTest.java | 48 +- .../filter/{tps => }/TpsLimitFilterTest.java | 42 +- .../rpc/filter/tps/DefaultTPSLimiterTest.java | 79 - .../dubbo/rpc/filter/tps/StatItemTest.java | 10 +- .../dubbo/rpc/proxy/AbstractProxyTest.java | 18 +- .../proxy/InvokerInvocationHandlerTest.java | 10 +- .../dubbo/rpc/support/RpcUtilsTest.java | 48 +- .../src/test/resources/log4j.xml | 1 - dubbo-rpc/dubbo-rpc-dubbo/pom.xml | 2 +- .../protocol/dubbo/CallbackServiceCodec.java | 34 +- .../protocol/dubbo/DecodeableRpcResult.java | 91 +- .../dubbo/rpc/protocol/dubbo/DubboCodec.java | 8 +- .../rpc/protocol/dubbo/DubboProtocol.java | 375 +-- .../dubbo/LazyConnectExchangeClient.java | 22 +- .../dubbo/ReferenceCountExchangeClient.java | 45 +- .../protocol/dubbo/filter/FutureFilter.java | 10 +- .../protocol/dubbo/filter/TraceFilter.java | 11 +- .../dubbo/telnet/InvokeTelnetHandler.java | 269 +-- .../dubbo/telnet/LogTelnetHandler.java | 11 +- .../dubbo/telnet/SelectTelnetHandler.java | 60 - .../dubbo/telnet/ShutdownTelnetHandler.java | 63 - .../dubbo/telnet/TraceTelnetHandler.java | 9 +- ...apache.dubbo.remoting.telnet.TelnetHandler | 4 +- .../dubbo/DubboInvokerAvilableTest.java | 46 +- .../protocol/dubbo/DubboLazyConnectTest.java | 41 +- .../rpc/protocol/dubbo/DubboProtocolTest.java | 18 +- .../protocol/dubbo/ExplicitCallbackTest.java | 128 +- .../rpc/protocol/dubbo/FutureFilterTest.java | 33 +- .../protocol/dubbo/ImplicitCallBackTest.java | 96 +- .../rpc/protocol/dubbo/MultiThreadTest.java | 14 +- .../ReferenceCountExchangeClientTest.java | 152 +- .../rpc/protocol/dubbo/RpcFilterTest.java | 16 +- .../dubbo/decode/DubboTelnetDecodeTest.java | 477 ---- .../dubbo/decode/LocalEmbeddedChannel.java | 35 - .../protocol/dubbo/decode/MockChannel.java | 115 - .../dubbo/decode/MockChannelHandler.java | 61 - .../protocol/dubbo/decode/MockHandler.java | 40 - .../protocol/dubbo/support/DemoService.java | 3 +- .../dubbo/support/DemoServiceImpl.java | 10 +- .../rpc/protocol/dubbo/support/EnumBak.java | 22 +- .../dubbo/telnet/ChangeTelnetHandlerTest.java | 16 +- .../telnet/CurrentTelnetHandlerTest.java | 6 +- .../telnet/InvokerTelnetHandlerTest.java | 16 +- .../dubbo/telnet/ListTelnetHandlerTest.java | 18 +- .../dubbo/telnet/LogTelnetHandlerTest.java | 4 +- .../dubbo/telnet/PortTelnetHandlerTest.java | 16 +- .../dubbo/telnet/SelectTelnetHandlerTest.java | 117 - .../telnet/ShutdownTelnetHandlerTest.java | 56 - .../dubbo/rpc/service/GenericServiceTest.java | 70 +- .../dubbo/rpc/validation/ValidationTest.java | 88 +- .../src/test/resources/log4j.xml | 12 +- dubbo-rpc/dubbo-rpc-hessian/pom.xml | 7 +- .../protocol/hessian/HessianProtocolTest.java | 46 +- dubbo-rpc/dubbo-rpc-http/pom.xml | 112 +- .../dubbo/rpc/protocol/http/HttpProtocol.java | 3 +- .../rpc/protocol/http/HttpProtocolTest.java | 51 +- dubbo-rpc/dubbo-rpc-injvm/pom.xml | 2 +- .../rpc/protocol/injvm/InjvmInvoker.java | 4 +- .../rpc/protocol/injvm/InjvmProtocol.java | 19 +- .../rpc/protocol/injvm/InjvmProtocolTest.java | 12 +- .../rpc/protocol/injvm/ProtocolTest.java | 12 +- dubbo-rpc/dubbo-rpc-memcached/pom.xml | 2 +- dubbo-rpc/dubbo-rpc-redis/pom.xml | 104 +- .../rpc/protocol/redis/RedisProtocolTest.java | 84 +- dubbo-rpc/dubbo-rpc-rest/pom.xml | 29 +- .../dubbo/rpc/protocol/rest/RestProtocol.java | 82 +- .../rpc/protocol/rest/RpcContextFilter.java | 6 +- .../dubbo/rpc/protocol/rest/DemoService.java | 29 +- .../dubbo/rpc/protocol/rest/IDemoService.java | 19 +- .../rpc/protocol/rest/RestProtocolTest.java | 139 +- .../protocol/rest/RpcExceptionMapperTest.java | 8 +- .../DubboSwaggerApiListingResourceTest.java | 10 +- .../rpc/protol/rest/RestProtocolTest.java | 78 + .../dubbo/rpc/protol/rest/RestService.java | 66 +- .../rest/RestServiceImpl.java} | 78 +- dubbo-rpc/dubbo-rpc-rmi/pom.xml | 2 +- .../dubbo/rpc/protocol/rmi/RmiProtocol.java | 59 +- .../rpc/protocol/rmi/RmiRemoteInvocation.java | 8 +- .../dubbo/rpc/protocol/rmi/DemoService.java | 2 - .../rpc/protocol/rmi/DemoServiceImpl.java | 4 - .../rpc/protocol/rmi/RmiProtocolTest.java | 24 +- .../rpc/protocol/rsocket/RSocketProtocol.java | 23 +- .../protocol/rsocket/RSocketProtocolTest.java | 9 +- dubbo-rpc/dubbo-rpc-thrift/pom.xml | 2 +- .../rpc/protocol/thrift/ThriftCodec.java | 17 +- .../rpc/protocol/thrift/ThriftProtocol.java | 4 +- .../src/test/java/ClassNameTest.java | 12 +- .../rpc/protocol/thrift/AbstractTest.java | 8 +- .../protocol/thrift/ServerExceptionTest.java | 25 +- .../thrift/ServiceMethodNotFoundTest.java | 16 +- .../rpc/protocol/thrift/ThriftCodecTest.java | 98 +- .../protocol/thrift/ThriftProtocolTest.java | 12 +- .../rpc/protocol/thrift/ThriftUtilsTest.java | 20 +- dubbo-rpc/dubbo-rpc-webservice/pom.xml | 150 +- .../webservice/WebserviceProtocolTest.java | 15 +- dubbo-rpc/pom.xml | 2 +- .../dubbo-serialization-api/pom.xml | 2 +- .../dubbo/common/serialize/Cleanable.java | 6 - .../dubbo/common/serialize/DataInput.java | 2 +- .../dubbo/common/serialize/DataOutput.java | 8 +- .../dubbo/common/serialize/ObjectInput.java | 25 +- .../dubbo/common/serialize/ObjectOutput.java | 2 +- .../dubbo/common/serialize/Serialization.java | 23 +- .../support/SerializableClassRegistry.java | 14 - .../support/SerializationOptimizer.java | 8 +- .../dubbo-serialization-fastjson/pom.xml | 2 +- .../fastjson/FastJsonObjectInput.java | 3 - .../fastjson/FastJsonObjectOutput.java | 3 - .../fastjson/FastJsonSerialization.java | 7 - .../dubbo-serialization-fst/pom.xml | 2 +- .../common/serialize/fst/FstFactory.java | 3 - .../common/serialize/fst/FstObjectInput.java | 4 +- .../common/serialize/fst/FstObjectOutput.java | 4 +- .../serialize/fst/FstSerialization.java | 7 - .../dubbo-serialization-hessian2/pom.xml | 2 +- .../hessian2/Hessian2ObjectInput.java | 2 +- .../hessian2/Hessian2ObjectOutput.java | 2 +- .../hessian2/Hessian2Serialization.java | 7 - .../dubbo-serialization-jdk/pom.xml | 2 +- .../java/CompactedJavaSerialization.java | 7 - .../java/CompactedObjectInputStream.java | 2 +- .../java/CompactedObjectOutputStream.java | 2 +- .../serialize/java/JavaObjectInput.java | 2 +- .../serialize/java/JavaObjectOutput.java | 2 +- .../serialize/java/JavaSerialization.java | 7 - .../nativejava/NativeJavaObjectInput.java | 3 - .../nativejava/NativeJavaObjectOutput.java | 3 - .../nativejava/NativeJavaSerialization.java | 7 - .../dubbo-serialization-kryo/pom.xml | 2 +- .../serialize/kryo/KryoObjectInput.java | 3 - .../serialize/kryo/KryoObjectOutput.java | 3 - .../serialize/kryo/KryoSerialization.java | 3 - .../dubbo-serialization-protostuff/pom.xml | 6 +- .../protostuff/ProtostuffObjectInput.java | 15 +- .../protostuff/ProtostuffObjectOutput.java | 15 +- .../protostuff/ProtostuffSerialization.java | 7 - .../protostuff/delegate/TimeDelegate.java | 3 - .../protostuff/utils/WrapperUtils.java | 15 - .../dubbo-serialization-test/pom.xml | 6 +- .../AbstractSerializationPersonFailTest.java | 9 +- .../AbstractSerializationPersonOkTest.java | 2 +- .../base/AbstractSerializationTest.java | 48 +- .../fastjson/FastJsonObjectInputTest.java | 23 +- .../fastjson/FastJsonObjectOutputTest.java | 8 +- .../fastjson/FastJsonSerializationTest.java | 8 +- .../common/serialize/fst/FstFactoryTest.java | 4 +- .../serialize/fst/FstObjectInputTest.java | 14 +- .../serialize/fst/FstObjectOutputTest.java | 12 +- .../serialize/fst/FstSerializationTest.java | 7 +- .../hessian2/Hessian2PersonOkTest.java | 16 +- .../hessian2/Hessian2SerializationTest.java | 16 +- .../serialize/kryo/ReflectionUtilsTest.java | 6 +- .../ProtostuffSerializationTest.java | 7 +- .../SerializableClassRegistryTest.java | 4 +- .../src/test/resources/log4j.xml | 1 - dubbo-serialization/pom.xml | 2 +- .../dubbo-test-spring3/pom.xml | 2 +- dubbo-test/dubbo-test-compatibility/pom.xml | 29 +- dubbo-test/dubbo-test-integration/pom.xml | 2 +- dubbo-test/pom.xml | 430 ++-- pom.xml | 147 +- 876 files changed, 13907 insertions(+), 29913 deletions(-) delete mode 100644 dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java delete mode 100644 dubbo-common/src/main/java/org/apache/dubbo/common/compiler/support/CtClassBuilder.java create mode 100644 dubbo-common/src/main/java/org/apache/dubbo/common/concurrent/ExecutionList.java delete mode 100644 dubbo-common/src/main/java/org/apache/dubbo/common/extension/AdaptiveClassCodeGenerator.java delete mode 100644 dubbo-common/src/main/java/org/apache/dubbo/common/utils/CIDRUtils.java delete mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/URLBuilderTest.java create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/concurrent/ExecutionListTest.java delete mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/config/ConfigurationUtilsTest.java delete mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/utils/CIDRUtilsTest.java delete mode 100644 dubbo-compatible/src/test/java/org/apache/dubbo/common/extension/MockDispatcher.java delete mode 100644 dubbo-compatible/src/test/java/org/apache/dubbo/filter/LegacyInvocation.java rename dubbo-compatible/src/test/java/org/apache/dubbo/filter/{LegacyInvoker.java => MyInvoker.java} (79%) delete mode 100644 dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/RouterTest.java delete mode 100644 dubbo-compatible/src/test/resources/META-INF/services/org.apache.dubbo.remoting.Dispatcher delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Argument.java delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Method.java delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/AbstractBuilder.java delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/AbstractInterfaceBuilder.java delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/AbstractMethodBuilder.java delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/AbstractReferenceBuilder.java delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/AbstractServiceBuilder.java delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ApplicationBuilder.java delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ArgumentBuilder.java delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ConfigCenterBuilder.java delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ConsumerBuilder.java delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/DubboBuilders.java delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/MetadataReportBuilder.java delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/MethodBuilder.java delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ModuleBuilder.java delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/MonitorBuilder.java delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ProtocolBuilder.java delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ProviderBuilder.java delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ReferenceBuilder.java delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/RegistryBuilder.java delete mode 100644 dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ServiceBuilder.java delete mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/AbstractBuilderTest.java delete mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/AbstractInterfaceBuilderTest.java delete mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/AbstractMethodBuilderTest.java delete mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/AbstractReferenceBuilderTest.java delete mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/AbstractServiceBuilderTest.java delete mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ApplicationBuilderTest.java delete mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ArgumentBuilderTest.java delete mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ConfigCenterBuilderTest.java delete mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ConsumerBuilderTest.java delete mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/MetadataReportBuilderTest.java delete mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/MethodBuilderTest.java delete mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ModuleBuilderTest.java delete mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/MonitorBuilderTest.java delete mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ProtocolBuilderTest.java delete mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ProviderBuilderTest.java delete mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ReferenceBuilderTest.java delete mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/RegistryBuilderTest.java delete mode 100644 dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ServiceBuilderTest.java create mode 100644 dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/AnnotationBean.java delete mode 100644 dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationInjectedBeanPostProcessor.java delete mode 100644 dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java rename dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/{DubboConfigConfigurationRegistrar.java => DubboConfigConfigurationSelector.java} (66%) delete mode 100644 dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/event/ServiceBeanExportedEvent.java rename dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/CompatibleRouter2.java => dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/convert/converter/StringArrayToMapConverter.java (58%) rename dubbo-config/dubbo-config-spring/src/{test/java/org/apache/dubbo/config/spring/context/context/annotation/provider/DefaultHelloService.java => main/java/org/apache/dubbo/config/spring/convert/converter/StringArrayToStringConverter.java} (59%) delete mode 100644 dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtils.java delete mode 100644 dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/ClassUtils.java delete mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilderTest.java delete mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationTestConfiguration.java delete mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilderTest.java delete mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/config/YamlPropertySourceFactory.java delete mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/config/YamlPropertySourceFactoryTest.java delete mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/DubboComponentScanRegistrarTest.java delete mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/DubboConfigBindingRegistrarTest.java delete mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/DubboConfigBindingsRegistrarTest.java delete mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/DubboConfigConfigurationTest.java delete mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/EnableDubboConfigTest.java delete mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/EnableDubboTest.java delete mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/consumer/ConsumerConfiguration.java delete mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/consumer/test/TestConsumerConfiguration.java delete mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/provider/DemoServiceImpl.java delete mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/provider/ProviderConfiguration.java delete mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/properties/DefaultDubboConfigBinderTest.java rename dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/NewRouter.java => dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/convert/converter/StringArrayToMapConverterTest.java (51%) rename dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/Man.java => dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/convert/converter/StringArrayToStringConverterTest.java (57%) delete mode 100644 dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbb-consumer.properties delete mode 100644 dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbb-provider.properties delete mode 100644 dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbo.yml delete mode 100644 dubbo-configcenter/dubbo-configcenter-consul/pom.xml delete mode 100644 dubbo-configcenter/dubbo-configcenter-consul/src/main/java/org/apache/dubbo/configcenter/consul/ConsulDynamicConfiguration.java delete mode 100644 dubbo-configcenter/dubbo-configcenter-consul/src/main/java/org/apache/dubbo/configcenter/consul/ConsulDynamicConfigurationFactory.java delete mode 100644 dubbo-configcenter/dubbo-configcenter-consul/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.configcenter.DynamicConfigurationFactory delete mode 100644 dubbo-configcenter/dubbo-configcenter-etcd/pom.xml delete mode 100644 dubbo-configcenter/dubbo-configcenter-etcd/src/main/java/org/apache/dubbo/configcenter/support/etcd/EtcdDynamicConfiguration.java delete mode 100644 dubbo-configcenter/dubbo-configcenter-etcd/src/main/java/org/apache/dubbo/configcenter/support/etcd/EtcdDynamicConfigurationFactory.java delete mode 100644 dubbo-configcenter/dubbo-configcenter-etcd/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.configcenter.DynamicConfigurationFactory delete mode 100644 dubbo-configcenter/dubbo-configcenter-etcd/src/test/java/org/apache/dubbo/configcenter/support/etcd/EtcdDynamicConfigurationTest.java delete mode 100644 dubbo-dependencies/dubbo-dependencies-zookeeper/pom.xml delete mode 100644 dubbo-dependencies/pom.xml delete mode 100644 dubbo-metadata-report/dubbo-metadata-report-consul/pom.xml delete mode 100644 dubbo-metadata-report/dubbo-metadata-report-consul/src/main/java/org/apache/dubbo/metadata/store/consul/ConsulMetadataReport.java delete mode 100644 dubbo-metadata-report/dubbo-metadata-report-consul/src/main/java/org/apache/dubbo/metadata/store/consul/ConsulMetadataReportFactory.java delete mode 100644 dubbo-metadata-report/dubbo-metadata-report-consul/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.metadata.store.MetadataReportFactory create mode 100644 dubbo-metrics/dubbo-metrics-api/pom.xml create mode 100644 dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/BucketCounter.java create mode 100644 dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Compass.java rename dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/RetryPolicy.java => dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Counter.java (58%) rename dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/DataListener.java => dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Counting.java (78%) rename dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/provider/HelloServiceImpl.java => dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Gauge.java (56%) create mode 100644 dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/IMetricManager.java rename dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/service/DemoService.java => dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Metric.java (74%) rename dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/AbstractRetryPolicy.java => dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricFilter.java (53%) create mode 100644 dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricLevel.java create mode 100644 dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricManager.java create mode 100644 dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricName.java create mode 100644 dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricRegistry.java rename dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/ConnectionStateListener.java => dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricSet.java (73%) create mode 100644 dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/NOPMetricManager.java create mode 100644 dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/MetricManagerTest.java create mode 100644 dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/MetricNameTest.java create mode 100644 dubbo-metrics/pom.xml delete mode 100644 dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/MetricsFilter.java delete mode 100644 dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/support/MetricsFilterTest.java delete mode 100644 dubbo-registry/dubbo-registry-consul/pom.xml delete mode 100644 dubbo-registry/dubbo-registry-consul/src/main/java/org/apache/dubbo/registry/consul/ConsulRegistry.java delete mode 100644 dubbo-registry/dubbo-registry-consul/src/main/java/org/apache/dubbo/registry/consul/ConsulRegistryFactory.java delete mode 100644 dubbo-registry/dubbo-registry-consul/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.RegistryFactory delete mode 100644 dubbo-registry/dubbo-registry-etcd3/pom.xml delete mode 100644 dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdRegistry.java delete mode 100644 dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdRegistryFactory.java delete mode 100644 dubbo-registry/dubbo-registry-etcd3/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.RegistryFactory delete mode 100644 dubbo-registry/dubbo-registry-etcd3/src/test/java/org/apache/dubbo/registry/etcd/EtcdRegistryTest.java delete mode 100644 dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/IdleSensible.java delete mode 100644 dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/CloseTimerTask.java delete mode 100644 dubbo-remoting/dubbo-remoting-etcd3/pom.xml delete mode 100644 dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/ChildListener.java delete mode 100644 dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/EtcdClient.java delete mode 100644 dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/EtcdTransporter.java delete mode 100644 dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClient.java delete mode 100644 dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientWrapper.java delete mode 100644 dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/RetryLoops.java delete mode 100644 dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/RetryNTimes.java delete mode 100644 dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/option/OptionUtil.java delete mode 100644 dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/support/AbstractEtcdClient.java delete mode 100644 dubbo-remoting/dubbo-remoting-etcd3/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.etcd.EtcdTransporter delete mode 100644 dubbo-remoting/dubbo-remoting-etcd3/src/test/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientTest.java delete mode 100644 dubbo-remoting/dubbo-remoting-etcd3/src/test/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientWrapperTest.java delete mode 100644 dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/EventType.java create mode 100644 dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkClientWrapper.java create mode 100644 dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperClient.java rename dubbo-remoting/{dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdTransporter.java => dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperTransporter.java} (68%) create mode 100644 dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkClientWrapperTest.java create mode 100644 dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperClientTest.java create mode 100644 dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperTransporterTest.java delete mode 100644 dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/AccessLogData.java rename dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/{tps => }/TpsLimitFilterTest.java (64%) delete mode 100644 dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/tps/DefaultTPSLimiterTest.java delete mode 100644 dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/SelectTelnetHandler.java delete mode 100644 dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ShutdownTelnetHandler.java delete mode 100644 dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/DubboTelnetDecodeTest.java delete mode 100644 dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/LocalEmbeddedChannel.java delete mode 100644 dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/MockChannel.java delete mode 100644 dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/MockChannelHandler.java delete mode 100644 dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/MockHandler.java delete mode 100644 dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/SelectTelnetHandlerTest.java delete mode 100644 dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ShutdownTelnetHandlerTest.java rename dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/StateListener.java => dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/IDemoService.java (70%) create mode 100644 dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestProtocolTest.java rename dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/option/Constants.java => dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestService.java (69%) rename dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/{protocol/rest/DemoServiceImpl.java => protol/rest/RestServiceImpl.java} (78%) diff --git a/.travis.yml b/.travis.yml index e3821cb4c32..32306778b30 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,9 +2,7 @@ language: java sudo: false # faster builds jdk: - - openjdk11 - oraclejdk11 - - openjdk8 - oraclejdk8 cache: @@ -18,6 +16,3 @@ script: after_success: - bash <(curl -s https://codecov.io/bash) - -after_failure: - - if [ -f dubbo.log ]; then echo "------TAIL of dubbo.log------"; tail -n 1000 dubbo.log; echo "------END of dubbo.log------"; fi diff --git a/CHANGES.md b/CHANGES.md index 78711ebbb49..12e003dbc4a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,68 +1,61 @@ # Release Notes ## 2.7.0 +环境要求:需要Java 8及以上版本支持 -Requirements: **Java 8+** required +请在[这里]()了解关于升级2.7.x版本的注意事项和兼容性问题 -Please check [here](https://github.com/apache/incubator-dubbo/blob/2.7.0-release/CHANGES.md#upgrading-and-compatibility-notifications) for notes and possible compatibility issues for upgrading from 2.6.x or lower to 2.7.0. +## New Features -### New Features +- 服务治理规则增强。 + - 更丰富的服务治理规则,新增应用级别条件路由、Tag路由等 + - 治理规则与注册中心解耦,增加对Apollo等第三方专业配置中心的支持,更易于扩展 + - 新增应用级别的动态配置规则 + - 规则体使用更易读、易用YAML格式 -- Enhancement of service governance rules. - - Enriched Routing Rules. - 1. Conditional Routing. Supports both application-level and service-level conditions. - 2. Tag Routing. Newly introduced to better support traffic isolation, such as grey deployment. - - Decoupling governance rules with the registry, making it easier to extend. Apollo and Zookeeper are available in this version. Nacos support is on the way... - - Application-level Dynamic Configuration support. - - Use YAML as the configuration language, which is more friendly to read and use. +- 外部化配置。支持读取托管在远程的集中式配置中心的dubbo.properties,实现应用配置的集中式管控。 -- Externalized Configuration. Supports reading `dubbo.properties` hosted in remote centralized configuration center - centralized configuration. +- 更精炼的注册中心URL,进一步减轻注册中心存储和同步压力,初步实现地址和配置的职责分离。 -- Simplified registry URL. With lower Registry memory use and less notification pressure from Service Directory, separates Configuration notification from Service Discovery. +- 新增服务元数据中心,负责存储包括服务静态化配置、服务定义(如方法签名)等数据,默认提供Zookeeper, Redis支持。此功能也是OPS实现服务测试、Mock等治理能力的基础。 -- Metadata Center. A totally new concept since 2.7.0, used to store service metadata including static configuration, service definition, method signature, etc.. By default, Zookeeper and Redis are supported as the backend storage. Will work as the basis of service testing, mock and other service governance features going to be supported in [Dubbo-Admin](https://github.com/apache/incubator-dubbo-admin). +- 异步编程模式增强(限定于Dubbo协议) + - 原生CompletableFuture签名接口支持 + - 服务端异步支持 + - 异步Filter链 -- Asynchronous Programming Model (only works for Dubbo protocol now) - - Built-in support for the method with CompletableFuture signature. - - Server-side asynchronous support, with an AsyncContext API works like Servlet 3.0. - - Asynchronous filter chain callback. +- 新增Protobuf序列化协议扩展 -- Serialization Extension: Protobuf. +- 新增ExpiringCache缓存策略扩展 -- Caching Policy Extension: Expiring Cache. +## Enhancements / Bugfixes -### Enhancements / Bugfixes +- 负载均衡策略优化,包括ConsitentHash(#2190) 、LeastActive(#2171)、Random(#2597) 、RoundRobin(#2586) (#2650) -- Load Balancing strategy enhancement: ConsitentHash #2190, LeastActive #2171, Random #2597, RoundRobin #2650. +- 升级第三方依赖:默认通信框架为netty 4、默认ZK客户端为Curator、Jetty 9k -- Third-party dependency upgrading. - - Switch default remoting to Netty 4. - - Switch default Zookeeper client to Curator. - - Upgrade Jetty to 9.x. +- 增加地址读取时对IPV6的支持(#2079) -- IPV6 support #2079. +- 性能优化,链接关闭的情况下使得Consumer快速返回 (#2185) -- Performance tuning, check hanging requests on a closed channel, make them return directly #2185. +- 修复Jdk原生类型在kryo中的序列化问题 (#2178) -- Fixed the serialization problem of JDK primitive types in Kryo #2178. +- 修复Provider端反序列化失败后,没有及早通知Consumer端的问题 (#1903) -- Fixed the problem of failing to notify Consumer as early as possible after the Provider side deserialization failed #1903. -### Upgrading and Compatibility Notifications +## 升级与兼容性 -We have always keep compatibility in mind during the whole process of 2.7.0. We even want old users to upgrade with only on pom version upgrade, but it's hard to achieve that, especially when considering that we have the package renamed in this version, so we had some tradeoffs. If you only used the Dubbo's most basic features, you may have little problems of upgrading, but if you have used some advanced features or have some SPI extensions inside, you'd better read the upgrade notifications carefully. The compatibility issues can be classified into the following 5 categories, for each part, there will have detailed dos and don'ts published later in the official website. +此次版本发布我们遵循了保持和老版本兼容的原则,尤其是在一些可能会破坏2.7版本与低版本互操作性的问题上,我们增加了一些兼容性代码,典型如服务治理规则、Package重命名、注册URL简化等。 -1. Interoperability between 2.7.0 and lower versions +1. Package重命名 -2. Package renaming +com.alibaba.dubbo -> org.apache.dubbo - com.alibaba.dubbo -> org.apache.dubbo +2. 注册URL简化 -3. Simplification of registered URLs +3. 服务治理规则 -4. Service Governance Rules - -5. Configuration +4. 配置 ## 2.6.5 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e5ed3a9ee9b..b987b16e658 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -13,7 +13,7 @@ The mailing list is the recommended way for discussing almost anything that rela - [dev@dubbo.incubator.apache.org](mailto:dev-subscribe@dubbo.incubator.apache.org): the develop mailing list, you can ask question here if you have encountered any problem when using or developing Dubbo. - [commits@dubbo.incubator.apache.org](mailto:commits-subscribe@dubbo.incubator.apache.org): all the commits will be sent to this mailing list. You can subscribe to it if you are interested in Dubbo's development. -- [notifications@dubbo.incubator.apache.org](mailto:notifications-subscribe@dubbo.incubator.apache.org): all the Github [issue](https://github.com/apache/incubator-dubbo/issues) updates and [pull request](https://github.com/apache/incubator-dubbo/pulls) updates will be sent to this mailing list. +- [notification@dubbo.incubator.apache.org](mailto:notification-subscribe@dubbo.incubator.apache.org): all the Github [issue](https://github.com/apache/incubator-dubbo/issues) updates and [pull request](https://github.com/apache/incubator-dubbo/pulls) updates will be sent to this mailing list. ### Reporting issue diff --git a/LICENSE b/LICENSE index 11eea3b82db..b85622380cf 100644 --- a/LICENSE +++ b/LICENSE @@ -208,6 +208,15 @@ Apache Dubbo includes a number of submodules with separate copyright notices and license terms. Your use of these submodules is subject to the terms and conditions of the following licenses. +For the org.apache.dubbo.common.concurrent package: + +This product bundles and repackages the following code in Google Guava 16.0.1, which is available under a +"Apache License 2.0" license. For details, see https://github.com/google/guava/blob/v16.0.1/COPYING. + + * com.google.common.util.concurrent.ExecutionList + * com.google.common.util.concurrent.ListenableFuture + * com.google.common.util.concurrent.ListenableFutureTask + For the package org.apache.dubbo.common.threadlocal and org.apache.dubbo.common.timer: This product contains a modified portion of 'Netty', an event-driven asynchronous network application framework also @@ -220,8 +229,3 @@ This product contains a modified portion of 'Netty', an event-driven asynchronou * io.netty.util.Timeout * io.netty.util.HashedWheelTimer -For the org.apache.dubbo.common.utils.CIDRUtils : - -This product contains a modified portion of 'edazdarevic.commons.net.CIDRUtils', - under a "MIT License" license, see https://github.com/edazdarevic/CIDRUtils/blob/master/CIDRUtils.java - diff --git a/README.md b/README.md index 09c12ffa974..5b00da21169 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Build Status](https://travis-ci.org/apache/incubator-dubbo.svg?branch=master)](https://travis-ci.org/apache/incubator-dubbo) [![codecov](https://codecov.io/gh/apache/incubator-dubbo/branch/master/graph/badge.svg)](https://codecov.io/gh/apache/incubator-dubbo) -![maven](https://img.shields.io/maven-central/v/org.apache.dubbo/dubbo.svg) +![maven](https://img.shields.io/maven-central/v/com.alibaba/dubbo.svg) ![license](https://img.shields.io/github/license/alibaba/dubbo.svg) [![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/apache/incubator-dubbo.svg)](http://isitmaintained.com/project/apache/incubator-dubbo "Average time to resolve an issue") [![Percentage of issues still open](http://isitmaintained.com/badge/open/apache/incubator-dubbo.svg)](http://isitmaintained.com/project/apache/incubator-dubbo "Percentage of issues still open") @@ -29,26 +29,26 @@ We are now collecting dubbo user info in order to help us to improve Dubbo bette ## Getting started -The following code snippet comes from [Dubbo Samples](https://github.com/apache/incubator-dubbo-samples/tree/master/dubbo-samples-api). You may clone the sample project and step into `dubbo-samples-api` sub directory before read on. +The following code snippet comes from [Dubbo Samples](https://github.com/dubbo/dubbo-samples/tree/master/dubbo-samples-api). You may clone the sample project and step into `dubbo-samples-api` sub directory before read on. ```bash -# git clone https://github.com/apache/incubator-dubbo-samples.git -# cd incubator-dubbo-samples/dubbo-samples-api +# git clone https://github.com/dubbo/dubbo-samples.git +# cd dubbo-samples/dubbo-samples-api ``` -There's a [README](https://github.com/apache/incubator-dubbo-samples/tree/master/dubbo-samples-api/README.md) file under `dubbo-samples-api` directory. Read it and try this sample out by following the instructions. +There's a [README](https://github.com/dubbo/dubbo-samples/blob/master/dubbo-samples-api/README.md) file under `dubbo-samples-api` directory. Read it and try this sample out by following the instructions. ### Maven dependency ```xml - 2.7.0 + 2.6.5 - org.apache.dubbo + com.alibaba dubbo-dependencies-bom ${dubbo.version} pom @@ -59,7 +59,7 @@ There's a [README](https://github.com/apache/incubator-dubbo-samples/tree/master - org.apache.dubbo + com.alibaba dubbo ${dubbo.version} @@ -67,39 +67,27 @@ There's a [README](https://github.com/apache/incubator-dubbo-samples/tree/master io.netty netty-all - - org.apache.curator - curator-framework - - - org.apache.curator - curator-recipes - - - org.apache.zookeeper - zookeeper - ``` ### Define service interfaces ```java -package org.apache.dubbo.samples.api; +package org.apache.dubbo.demo.api; public interface GreetingService { String sayHello(String name); } ``` -*See [api/GreetingService.java](https://github.com/apache/incubator-dubbo-samples/blob/master/dubbo-samples-api/src/main/java/org/apache/dubbo/samples/api/GreetingsService.java) on GitHub.* +*See [api/GreetingService.java](https://github.com/dubbo/dubbo-samples/blob/master/dubbo-samples-api/src/main/java/org/apache/dubbo/samples/api/GreetingsService.java) on GitHub.* ### Implement service interface for the provider ```java -package org.apache.dubbo.samples.provider; +package org.apache.dubbo.demo.provider; -import org.apache.dubbo.samples.api.GreetingService; +import org.apache.dubbo.demo.GreetingService; public class GreetingServiceImpl implements GreetingService { public String sayHello(String name) { @@ -108,17 +96,17 @@ public class GreetingServiceImpl implements GreetingService { } ``` -*See [provider/GreetingServiceImpl.java](https://github.com/apache/incubator-dubbo-samples/blob/master/dubbo-samples-api/src/main/java/org/apache/dubbo/samples/provider/GreetingsServiceImpl.java) on GitHub.* +*See [provider/GreetingServiceImpl.java](https://github.com/dubbo/dubbo-samples/blob/master/dubbo-samples-api/src/main/java/org/apache/dubbo/samples/provider/GreetingsServiceImpl.java) on GitHub.* ### Start service provider ```java package org.apache.dubbo.demo.provider; -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.config.RegistryConfig; -import org.apache.dubbo.config.ServiceConfig; -import org.apache.dubbo.samples.api.GreetingService; +import com.alibaba.dubbo.config.ApplicationConfig; +import com.alibaba.dubbo.config.RegistryConfig; +import com.alibaba.dubbo.config.ServiceConfig; +import org.apache.dubbo.demo.GreetingService; import java.io.IOException; @@ -136,7 +124,7 @@ public class Application { } ``` -*See [provider/Application.java](https://github.com/apache/incubator-dubbo-samples/blob/master/dubbo-samples-api/src/main/java/org/apache/dubbo/samples/provider/Application.java) on GitHub.* +*See [provider/Application.java](https://github.com/dubbo/dubbo-samples/blob/master/dubbo-samples-api/src/main/java/org/apache/dubbo/samples/provider/Application.java) on GitHub.* ### Build and run the provider @@ -150,10 +138,10 @@ public class Application { ```java package org.apache.dubbo.demo.consumer; -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.config.ReferenceConfig; -import org.apache.dubbo.config.RegistryConfig; -import org.apache.dubbo.samples.api.GreetingService; +import com.alibaba.dubbo.config.ApplicationConfig; +import com.alibaba.dubbo.config.ReferenceConfig; +import com.alibaba.dubbo.config.RegistryConfig; +import org.apache.dubbo.demo.GreetingService; public class Application { public static void main(String[] args) { @@ -176,7 +164,7 @@ public class Application { The consumer will print out `Hello world` on the screen. -*See [consumer/Application.java](https://github.com/apache/incubator-dubbo-samples/blob/master/dubbo-samples-api/src/main/java/org/apache/dubbo/samples/consumer/Application.java) on GitHub.* +*See [consumer/Application.java](https://github.com/dubbo/dubbo-samples/blob/master/dubbo-samples-api/src/main/java/org/apache/dubbo/samples/consumer/Application.java) on GitHub.* ### Next steps @@ -209,7 +197,7 @@ See [CONTRIBUTING](https://github.com/apache/incubator-dubbo/blob/master/CONTRI * blog post * translation on documentation * use cases about how Dubbo is being used in enterprise system. -* Improve the [dubbo-admin/dubbo-monitor](https://github.com/apache/incubator-dubbo-admin). +* Improve the [dubbo-ops/dubbo-monitor](https://github.com/apache/incubator-dubbo-ops). * Contribute to the projects listed in [ecosystem](https://github.com/dubbo). * Any form of contribution that is not mentioned above. * If you would like to contribute, please send an email to dev@dubbo.incubator.apache.org to let us know! @@ -220,22 +208,21 @@ Please follow the [template](https://github.com/apache/incubator-dubbo/issues/ne ## Reporting a security vulnerability -Please report security vulnerability to [us](mailto:security@dubbo.incubator.apache.org) privately. +Please report security vulnerability to [us](security@dubbo.incubator.apache.org) privately. -## Dubbo ecosystem +## Dubbo eco system * [Dubbo Ecosystem Entry](https://github.com/dubbo) - A GitHub group `dubbo` to gather all Dubbo relevant projects not appropriate in [apache](https://github.com/apache) group yet * [Dubbo Website](https://github.com/apache/incubator-dubbo-website) - Apache Dubbo (incubating) official website -* [Dubbo Samples](https://github.com/apache/incubator-dubbo-samples) - samples for Apache Dubbo (incubating) +* [Dubbo Samples](https://github.com/dubbo/dubbo-samples) - samples for Apache Dubbo (incubating) * [Dubbo Spring Boot](https://github.com/apache/incubator-dubbo-spring-boot-project) - Spring Boot Project for Dubbo -* [Dubbo Admin](https://github.com/apache/incubator-dubbo-admin) - The reference implementation for Dubbo admin +* [Dubbo OPS](https://github.com/apache/incubator-dubbo-ops) - The reference implementation for Dubbo admin #### Language * [Node.js](https://github.com/dubbo/dubbo2.js) * [Python](https://github.com/dubbo/dubbo-client-py) * [PHP](https://github.com/dubbo/dubbo-php-framework) -* [Go](https://github.com/dubbo/dubbo-go) ## License diff --git a/codestyle/checkstyle.xml b/codestyle/checkstyle.xml index fa4d735a785..f721d510d4c 100644 --- a/codestyle/checkstyle.xml +++ b/codestyle/checkstyle.xml @@ -20,7 +20,6 @@ - diff --git a/dubbo-all/pom.xml b/dubbo-all/pom.xml index a4a0b5cc3cd..0c67ab9b58f 100644 --- a/dubbo-all/pom.xml +++ b/dubbo-all/pom.xml @@ -19,11 +19,11 @@ org.apache.dubbo dubbo-parent - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT ../pom.xml dubbo - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT jar dubbo-all The all in one project of dubbo @@ -234,20 +234,6 @@ compile true - - org.apache.dubbo - dubbo-registry-consul - ${project.version} - compile - true - - - org.apache.dubbo - dubbo-registry-etcd3 - ${project.version} - compile - true - org.apache.dubbo dubbo-monitor-api @@ -360,20 +346,6 @@ compile true - - org.apache.dubbo - dubbo-configcenter-consul - ${project.version} - compile - true - - - org.apache.dubbo - dubbo-configcenter-etcd - ${project.version} - compile - true - org.apache.dubbo dubbo-compatible @@ -409,13 +381,6 @@ compile true - - org.apache.dubbo - dubbo-metadata-report-consul - ${project.version} - compile - true - @@ -430,27 +395,14 @@ io.netty netty-all - - com.google.code.gson - gson - - - org.yaml - snakeyaml - - org.junit.jupiter - junit-jupiter-engine - ${junit_jupiter_version} - test - - - org.junit.jupiter - junit-jupiter-params - ${junit_jupiter_version} + junit + junit + ${junit_version} test + true cglib @@ -507,8 +459,6 @@ org.apache.dubbo:dubbo-registry-multicast org.apache.dubbo:dubbo-registry-zookeeper org.apache.dubbo:dubbo-registry-redis - org.apache.dubbo:dubbo-registry-consul - org.apache.dubbo:dubbo-registry-etcd3 org.apache.dubbo:dubbo-monitor-api org.apache.dubbo:dubbo-monitor-default org.apache.dubbo:dubbo-config-api @@ -529,13 +479,11 @@ org.apache.dubbo:dubbo-configcenter-definition org.apache.dubbo:dubbo-configcenter-apollo org.apache.dubbo:dubbo-configcenter-zookeeper - org.apache.dubbo:dubbo-configcenter-consul - org.apache.dubbo:dubbo-configcenter-etcd org.apache.dubbo:dubbo-metadata-report-api org.apache.dubbo:dubbo-metadata-definition org.apache.dubbo:dubbo-metadata-report-redis org.apache.dubbo:dubbo-metadata-report-zookeeper - org.apache.dubbo:dubbo-metadata-report-consul + com.google.code.gson:gson @@ -653,27 +601,12 @@ META-INF/dubbo/internal/org.apache.dubbo.qos.command.BaseCommand - + META-INF/dubbo/internal/org.apache.dubbo.configcenter.DynamicConfigurationFactory - - META-INF/dubbo/internal/org.apache.dubbo.metadata.store.MetadataReportFactory - - - - - org.apache.dubbo:dubbo - - - com/** - org/** - - META-INF/dubbo/** - - - diff --git a/dubbo-bom/pom.xml b/dubbo-bom/pom.xml index 47875b83ffb..dd87f685970 100644 --- a/dubbo-bom/pom.xml +++ b/dubbo-bom/pom.xml @@ -6,12 +6,12 @@ org.apache.dubbo dubbo-parent - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT org.apache.dubbo dubbo-bom - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT pom dubbo-bom @@ -138,11 +138,6 @@ dubbo-remoting-http ${project.version} - - org.apache.dubbo - dubbo-remoting-etcd3 - ${project.version} - org.apache.dubbo dubbo-rpc-api @@ -223,16 +218,6 @@ dubbo-registry-redis ${project.version} - - org.apache.dubbo - dubbo-registry-etcd3 - ${project.version} - - - org.apache.dubbo - dubbo-registry-consul - ${project.version} - org.apache.dubbo dubbo-monitor-api @@ -318,11 +303,6 @@ dubbo-metadata-report-redis ${project.version} - - org.apache.dubbo - dubbo-metadata-report-consul - ${project.version} - org.apache.dubbo dubbo-configcenter-api @@ -338,16 +318,6 @@ dubbo-configcenter-apollo ${project.version} - - org.apache.dubbo - dubbo-configcenter-consul - ${project.version} - - - org.apache.dubbo - dubbo-configcenter-etcd - ${project.version} - org.apache.dubbo dubbo-metadata-definition diff --git a/dubbo-cluster/pom.xml b/dubbo-cluster/pom.xml index 58d0367d873..6688160109c 100644 --- a/dubbo-cluster/pom.xml +++ b/dubbo-cluster/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-parent - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-cluster jar diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/Configurator.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/Configurator.java index b85a0461312..8d5f02c2861 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/Configurator.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/Configurator.java @@ -98,7 +98,6 @@ static Optional> toConfigurators(List urls) { * 1. the url with a specific host ip should have higher priority than 0.0.0.0 * 2. if two url has the same host, compare by priority value; */ - @Override default int compareTo(Configurator o) { if (o == null) { return -1; diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/Router.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/Router.java index fadefdeae0f..5addb8e4911 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/Router.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/Router.java @@ -32,9 +32,6 @@ * @see org.apache.dubbo.rpc.cluster.Directory#list(Invocation) */ public interface Router extends Comparable { - - int DEFAULT_PRIORITY = Integer.MAX_VALUE; - /** * Get the router url. * @@ -88,12 +85,4 @@ default void notify(List> invokers) { * @return router's priority */ int getPriority(); - - @Override - default int compareTo(Router o) { - if (o == null) { - throw new IllegalArgumentException(); - } - return Integer.compare(this.getPriority(), o.getPriority()); - } } diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/configurator/override/OverrideConfigurator.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/configurator/override/OverrideConfigurator.java index bc9beb4f8b4..1d57773edbc 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/configurator/override/OverrideConfigurator.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/configurator/override/OverrideConfigurator.java @@ -20,7 +20,7 @@ import org.apache.dubbo.rpc.cluster.configurator.AbstractConfigurator; /** - * OverrideConfigurator + * AbsentConfigurator * */ public class OverrideConfigurator extends AbstractConfigurator { diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/configurator/override/OverrideConfiguratorFactory.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/configurator/override/OverrideConfiguratorFactory.java index a63aa55004e..051b4bd82fd 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/configurator/override/OverrideConfiguratorFactory.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/configurator/override/OverrideConfiguratorFactory.java @@ -21,7 +21,7 @@ import org.apache.dubbo.rpc.cluster.ConfiguratorFactory; /** - * OverrideConfiguratorFactory + * AbsentConfiguratorFactory * */ public class OverrideConfiguratorFactory implements ConfiguratorFactory { diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/configurator/parser/ConfigParser.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/configurator/parser/ConfigParser.java index 76d6f2db3a3..bffa1d628ad 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/configurator/parser/ConfigParser.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/configurator/parser/ConfigParser.java @@ -130,7 +130,7 @@ private static String toParameterString(ConfigItem item) { sb.append(item.getSide()); } Map parameters = item.getParameters(); - if (CollectionUtils.isEmptyMap(parameters)) { + if (parameters == null || parameters.isEmpty()) { throw new IllegalStateException("Invalid configurator rule, please specify at least one parameter " + "you want to change in the rule."); } diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectory.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectory.java index c5a26a3f7d3..385fa107e39 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectory.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectory.java @@ -19,7 +19,6 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.RpcException; @@ -30,6 +29,7 @@ /** * StaticDirectory + * */ public class StaticDirectory extends AbstractDirectory { private static final Logger logger = LoggerFactory.getLogger(StaticDirectory.class); @@ -49,10 +49,9 @@ public StaticDirectory(URL url, List> invokers) { } public StaticDirectory(URL url, List> invokers, RouterChain routerChain) { - super(url == null && CollectionUtils.isNotEmpty(invokers) ? invokers.get(0).getUrl() : url, routerChain); - if (CollectionUtils.isEmpty(invokers)) { + super(url == null && invokers != null && !invokers.isEmpty() ? invokers.get(0).getUrl() : url, routerChain); + if (invokers == null || invokers.isEmpty()) throw new IllegalArgumentException("invokers == null"); - } this.invokers = invokers; } diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/AbstractLoadBalance.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/AbstractLoadBalance.java index 78151ed5af4..feb9202d50f 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/AbstractLoadBalance.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/AbstractLoadBalance.java @@ -18,7 +18,6 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.cluster.LoadBalance; @@ -45,7 +44,7 @@ static int calculateWarmupWeight(int uptime, int warmup, int weight) { @Override public Invoker select(List> invokers, URL url, Invocation invocation) { - if (CollectionUtils.isEmpty(invokers)) { + if (invokers == null || invokers.isEmpty()) { return null; } if (invokers.size() == 1) { diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalance.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalance.java index 83d6f8c9112..8671cdeca82 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalance.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalance.java @@ -22,7 +22,7 @@ import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.support.RpcUtils; -import java.nio.charset.StandardCharsets; +import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.List; @@ -33,6 +33,7 @@ /** * ConsistentHashLoadBalance + * */ public class ConsistentHashLoadBalance extends AbstractLoadBalance { public static final String NAME = "consistenthash"; @@ -125,7 +126,12 @@ private byte[] md5(String value) { throw new IllegalStateException(e.getMessage(), e); } md5.reset(); - byte[] bytes = value.getBytes(StandardCharsets.UTF_8); + byte[] bytes; + try { + bytes = value.getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new IllegalStateException(e.getMessage(), e); + } md5.update(bytes); return md5.digest(); } diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/AbstractRouter.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/AbstractRouter.java index 657ad866c28..da4bd7c37eb 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/AbstractRouter.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/AbstractRouter.java @@ -20,8 +20,11 @@ import org.apache.dubbo.configcenter.DynamicConfiguration; import org.apache.dubbo.rpc.cluster.Router; +/** + * TODO Extract more code to here if necessary + */ public abstract class AbstractRouter implements Router { - protected int priority = DEFAULT_PRIORITY; + protected int priority; protected boolean force = false; protected URL url; @@ -63,6 +66,10 @@ public void setForce(boolean force) { } @Override + public int compareTo(Router o) { + return (this.getPriority() >= o.getPriority()) ? 1 : -1; + } + public int getPriority() { return priority; } diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/ConditionRouter.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/ConditionRouter.java index 6549cdade59..434ef8cb23b 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/ConditionRouter.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/ConditionRouter.java @@ -20,13 +20,13 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.common.utils.UrlUtils; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.RpcException; +import org.apache.dubbo.rpc.cluster.Router; import org.apache.dubbo.rpc.cluster.router.AbstractRouter; import java.text.ParseException; @@ -43,7 +43,7 @@ * ConditionRouter * */ -public class ConditionRouter extends AbstractRouter { +public class ConditionRouter extends AbstractRouter implements Comparable { public static final String NAME = "condition"; private static final Logger logger = LoggerFactory.getLogger(ConditionRouter.class); @@ -101,7 +101,7 @@ private static Map parseRule(String rule) String separator = matcher.group(1); String content = matcher.group(2); // Start part of the condition expression. - if (StringUtils.isEmpty(separator)) { + if (separator == null || separator.length() == 0) { pair = new MatchPair(); condition.put(content, pair); } @@ -163,7 +163,7 @@ public List> route(List> invokers, URL url, Invocation return invokers; } - if (CollectionUtils.isEmpty(invokers)) { + if (invokers == null || invokers.isEmpty()) { return invokers; } try { @@ -205,11 +205,11 @@ public URL getUrl() { } boolean matchWhen(URL url, Invocation invocation) { - return CollectionUtils.isEmptyMap(whenCondition) || matchCondition(whenCondition, url, null, invocation); + return whenCondition == null || whenCondition.isEmpty() || matchCondition(whenCondition, url, null, invocation); } private boolean matchThen(URL url, URL param) { - return CollectionUtils.isNotEmptyMap(thenCondition) && matchCondition(thenCondition, url, param, null); + return !(thenCondition == null || thenCondition.isEmpty()) && matchCondition(thenCondition, url, param, null); } private boolean matchCondition(Map condition, URL url, URL param, Invocation invocation) { diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/config/AppRouter.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/config/AppRouter.java index 31a5df174c1..9c723df54f3 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/config/AppRouter.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/config/AppRouter.java @@ -25,13 +25,8 @@ */ public class AppRouter extends ListenableRouter { public static final String NAME = "APP_ROUTER"; - /** - * AppRouter should after ServiceRouter - */ - private static final int APP_ROUTER_DEFAULT_PRIORITY = 150; public AppRouter(DynamicConfiguration configuration, URL url) { super(configuration, url, url.getParameter(Constants.APPLICATION_KEY)); - this.priority = APP_ROUTER_DEFAULT_PRIORITY; } } diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/config/ListenableRouter.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/config/ListenableRouter.java index 564f371eac0..65a4eec7d52 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/config/ListenableRouter.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/config/ListenableRouter.java @@ -44,7 +44,7 @@ public abstract class ListenableRouter extends AbstractRouter implements ConfigurationListener { public static final String NAME = "LISTENABLE_ROUTER"; private static final String RULE_SUFFIX = ".condition-router"; - + public static final int DEFAULT_PRIORITY = 200; private static final Logger logger = LoggerFactory.getLogger(ListenableRouter.class); private ConditionRouterRule routerRule; private List conditionRouters = Collections.emptyList(); @@ -120,7 +120,7 @@ private synchronized void init(String ruleKey) { String routerKey = ruleKey + RULE_SUFFIX; configuration.addListener(routerKey, this); String rule = configuration.getConfig(routerKey); - if (StringUtils.isNotEmpty(rule)) { + if (rule != null) { this.process(new ConfigChangeEvent(routerKey, rule)); } } diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/config/ServiceRouter.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/config/ServiceRouter.java index ed3748e0fd5..c76f2a7c054 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/config/ServiceRouter.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/config/ServiceRouter.java @@ -24,13 +24,8 @@ */ public class ServiceRouter extends ListenableRouter { public static final String NAME = "SERVICE_ROUTER"; - /** - * ServiceRouter should before AppRouter - */ - private static final int SERVICE_ROUTER_DEFAULT_PRIORITY = 140; public ServiceRouter(DynamicConfiguration configuration, URL url) { super(configuration, url, url.getEncodedServiceKey()); - this.priority = SERVICE_ROUTER_DEFAULT_PRIORITY; } } diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/file/FileRouterFactory.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/file/FileRouterFactory.java index 1dd446cc187..2d509422d12 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/file/FileRouterFactory.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/file/FileRouterFactory.java @@ -18,7 +18,6 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.utils.IOUtils; import org.apache.dubbo.rpc.cluster.Router; import org.apache.dubbo.rpc.cluster.RouterFactory; @@ -56,12 +55,9 @@ public Router getRouter(URL url) { // FIXME: this code looks useless boolean runtime = url.getParameter(Constants.RUNTIME_KEY, false); - URL script = URLBuilder.from(url) - .setProtocol(protocol) - .addParameter(Constants.TYPE_KEY, type) + URL script = url.setProtocol(protocol).addParameter(Constants.TYPE_KEY, type) .addParameter(Constants.RUNTIME_KEY, runtime) - .addParameterAndEncoded(Constants.RULE_KEY, rule) - .build(); + .addParameterAndEncoded(Constants.RULE_KEY, rule); return routerFactory.getRouter(script); } catch (IOException e) { diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mock/MockInvokersSelector.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mock/MockInvokersSelector.java index 78ada664229..896638a2c04 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mock/MockInvokersSelector.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mock/MockInvokersSelector.java @@ -22,6 +22,7 @@ import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.RpcException; +import org.apache.dubbo.rpc.cluster.Router; import org.apache.dubbo.rpc.cluster.router.AbstractRouter; import java.util.ArrayList; @@ -30,15 +31,11 @@ /** * A specific Router designed to realize mock feature. * If a request is configured to use mock, then this router guarantees that only the invokers with protocol MOCK appear in final the invoker list, all other invokers will be excluded. + * */ public class MockInvokersSelector extends AbstractRouter { public static final String NAME = "MOCK_ROUTER"; - private static final int MOCK_INVOKERS_DEFAULT_PRIORITY = Integer.MIN_VALUE; - - public MockInvokersSelector() { - this.priority = MOCK_INVOKERS_DEFAULT_PRIORITY; - } @Override public List> route(final List> invokers, @@ -98,4 +95,15 @@ private boolean hasMockProviders(final List> invokers) { return hasMockProvider; } + /** + * Always stay on the top of the list + * + * @param o + * @return + */ + @Override + public int compareTo(Router o) { + return 1; + } + } diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouter.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouter.java index 0b47d2e7530..496a0dce122 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouter.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouter.java @@ -20,7 +20,6 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.RpcContext; @@ -38,75 +37,73 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import java.util.stream.Collectors; /** * ScriptRouter */ public class ScriptRouter extends AbstractRouter { public static final String NAME = "SCRIPT_ROUTER"; - private static final int SCRIPT_ROUTER_DEFAULT_PRIORITY = 0; private static final Logger logger = LoggerFactory.getLogger(ScriptRouter.class); private static final Map engines = new ConcurrentHashMap<>(); private final ScriptEngine engine; - private final String rule; + private final int priority; - private CompiledScript function; + private final String rule; public ScriptRouter(URL url) { this.url = url; - this.priority = url.getParameter(Constants.PRIORITY_KEY, SCRIPT_ROUTER_DEFAULT_PRIORITY); - - engine = getEngine(url); - rule = getRule(url); - try { - Compilable compilable = (Compilable) engine; - function = compilable.compile(rule); - } catch (ScriptException e) { - logger.error("route error, rule has been ignored. rule: " + rule + - ", url: " + RpcContext.getContext().getUrl(), e); + String type = url.getParameter(Constants.TYPE_KEY); + this.priority = url.getParameter(Constants.PRIORITY_KEY, 0); + String rule = url.getParameterAndDecoded(Constants.RULE_KEY); + if (type == null || type.length() == 0) { + type = Constants.DEFAULT_SCRIPT_TYPE_KEY; } - - - } - - /** - * get rule from url parameters. - */ - private String getRule(URL url) { - String vRule = url.getParameterAndDecoded(Constants.RULE_KEY); - if (StringUtils.isEmpty(vRule)) { - throw new IllegalStateException("route rule can not be empty."); + if (rule == null || rule.length() == 0) { + throw new IllegalStateException("route rule can not be empty. rule:" + rule); + } + ScriptEngine engine = engines.get(type); + if (engine == null) { + engine = new ScriptEngineManager().getEngineByName(type); + if (engine == null) { + throw new IllegalStateException("unsupported route rule type: " + type + ", rule: " + rule); + } + engines.put(type, engine); } - return vRule; + this.engine = engine; + this.rule = rule; } - /** - * create ScriptEngine instance by type from url parameters, then cache it - */ - private ScriptEngine getEngine(URL url) { - String type = url.getParameter(Constants.TYPE_KEY, Constants.DEFAULT_SCRIPT_TYPE_KEY); - - return engines.computeIfAbsent(type, t -> { - ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName(type); - if (scriptEngine == null) { - throw new IllegalStateException("unsupported route engine type: " + type); - } - return scriptEngine; - }); + @Override + public URL getUrl() { + return url; } @Override + @SuppressWarnings("unchecked") public List> route(List> invokers, URL url, Invocation invocation) throws RpcException { try { - Bindings bindings = createBindings(invokers, invocation); - if (function == null) { - return invokers; + List> invokersCopy = new ArrayList<>(invokers); + Compilable compilable = (Compilable) engine; + Bindings bindings = engine.createBindings(); + bindings.put("invokers", invokersCopy); + bindings.put("invocation", invocation); + bindings.put("context", RpcContext.getContext()); + CompiledScript function = compilable.compile(rule); + Object obj = function.eval(bindings); + if (obj instanceof Invoker[]) { + invokersCopy = Arrays.asList((Invoker[]) obj); + } else if (obj instanceof Object[]) { + invokersCopy = new ArrayList>(); + for (Object inv : (Object[]) obj) { + invokersCopy.add((Invoker) inv); + } + } else { + invokersCopy = (List>) obj; } - return getRoutedInvokers(function.eval(bindings)); + return invokersCopy; } catch (ScriptException e) { logger.error("route error, rule has been ignored. rule: " + rule + ", method:" + invocation.getMethodName() + ", url: " + RpcContext.getContext().getUrl(), e); @@ -114,32 +111,6 @@ public List> route(List> invokers, URL url, Invocation } } - /** - * get routed invokers from result of script rule evaluation - */ - @SuppressWarnings("unchecked") - protected List> getRoutedInvokers(Object obj) { - if (obj instanceof Invoker[]) { - return Arrays.asList((Invoker[]) obj); - } else if (obj instanceof Object[]) { - return Arrays.stream((Object[]) obj).map(item -> (Invoker) item).collect(Collectors.toList()); - } else { - return (List>) obj; - } - } - - /** - * create bindings for script engine - */ - private Bindings createBindings(List> invokers, Invocation invocation) { - Bindings bindings = engine.createBindings(); - // create a new List of invokers - bindings.put("invokers", new ArrayList<>(invokers)); - bindings.put("invocation", invocation); - bindings.put("context", RpcContext.getContext()); - return bindings; - } - @Override public boolean isRuntime() { return this.url.getParameter(Constants.RUNTIME_KEY, false); @@ -149,5 +120,4 @@ public boolean isRuntime() { public boolean isForce() { return url.getParameter(Constants.FORCE_KEY, false); } - } diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java index d03efca6e4d..6c4e94e5c50 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java @@ -21,7 +21,6 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.CollectionUtils; -import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.configcenter.ConfigChangeEvent; import org.apache.dubbo.configcenter.ConfigChangeType; @@ -30,11 +29,11 @@ import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.RpcException; +import org.apache.dubbo.rpc.cluster.Router; import org.apache.dubbo.rpc.cluster.router.AbstractRouter; import org.apache.dubbo.rpc.cluster.router.tag.model.TagRouterRule; import org.apache.dubbo.rpc.cluster.router.tag.model.TagRuleParser; -import java.net.UnknownHostException; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -45,9 +44,9 @@ /** * TagRouter, "application.tag-router" */ -public class TagRouter extends AbstractRouter implements ConfigurationListener { +public class TagRouter extends AbstractRouter implements Comparable, ConfigurationListener { public static final String NAME = "TAG_ROUTER"; - private static final int TAG_ROUTER_DEFAULT_PRIORITY = 100; + private static final int DEFAULT_PRIORITY = 100; private static final Logger logger = LoggerFactory.getLogger(TagRouter.class); private static final String RULE_SUFFIX = ".tag-router"; @@ -56,7 +55,6 @@ public class TagRouter extends AbstractRouter implements ConfigurationListener { public TagRouter(DynamicConfiguration configuration, URL url) { super(configuration, url); - this.priority = TAG_ROUTER_DEFAULT_PRIORITY; } @Override @@ -89,10 +87,8 @@ public List> route(List> invokers, URL url, Invocation return invokers; } - // since the rule can be changed by config center, we should copy one to use. - final TagRouterRule tagRouterRuleCopy = tagRouterRule; - if (tagRouterRuleCopy == null || !tagRouterRuleCopy.isValid() || !tagRouterRuleCopy.isEnabled()) { - return filterUsingStaticTag(invokers, url, invocation); + if (tagRouterRule == null || !tagRouterRule.isValid() || !tagRouterRule.isEnabled()) { + return invokers; } List> result = invokers; @@ -101,12 +97,12 @@ public List> route(List> invokers, URL url, Invocation // if we are requesting for a Provider with a specific tag if (StringUtils.isNotEmpty(tag)) { - List addresses = tagRouterRuleCopy.getTagnameToAddresses().get(tag); + List addresses = tagRouterRule.getTagnameToAddresses().get(tag); // filter by dynamic tag group first if (CollectionUtils.isNotEmpty(addresses)) { result = filterInvoker(invokers, invoker -> addressMatches(invoker.getUrl(), addresses)); // if result is not null OR it's null but force=true, return result directly - if (CollectionUtils.isNotEmpty(result) || tagRouterRuleCopy.isForce()) { + if (CollectionUtils.isNotEmpty(result) || tagRouterRule.isForce()) { return result; } } else { @@ -116,19 +112,19 @@ public List> route(List> invokers, URL url, Invocation } // If there's no tagged providers that can match the current tagged request. force.tag is set by default // to false, which means it will invoke any providers without a tag unless it's explicitly disallowed. - if (CollectionUtils.isNotEmpty(result) || isForceUseTag(invocation)) { + if (CollectionUtils.isNotEmpty(result) || isForceUse(invocation)) { return result; } // FAILOVER: return all Providers without any tags. else { List> tmp = filterInvoker(invokers, invoker -> addressNotMatches(invoker.getUrl(), - tagRouterRuleCopy.getAddresses())); + tagRouterRule.getAddresses())); return filterInvoker(tmp, invoker -> StringUtils.isEmpty(invoker.getUrl().getParameter(TAG_KEY))); } } else { // List addresses = tagRouterRule.filter(providerApp); // return all addresses in dynamic tag group. - List addresses = tagRouterRuleCopy.getAddresses(); + List addresses = tagRouterRule.getAddresses(); if (CollectionUtils.isNotEmpty(addresses)) { result = filterInvoker(invokers, invoker -> addressNotMatches(invoker.getUrl(), addresses)); // 1. all addresses are in dynamic tag group, return empty list. @@ -140,40 +136,14 @@ public List> route(List> invokers, URL url, Invocation } return filterInvoker(result, invoker -> { String localTag = invoker.getUrl().getParameter(TAG_KEY); - return StringUtils.isEmpty(localTag) || !tagRouterRuleCopy.getTagNames().contains(localTag); + return StringUtils.isEmpty(localTag) || !tagRouterRule.getTagNames().contains(localTag); }); } } - /** - * If there's no dynamic tag rule being set, use static tag in URL. - *

- * A typical scenario is a Consumer using version 2.7.x calls Providers using version 2.6.x or lower, - * the Consumer should always respect the tag in provider URL regardless of whether a dynamic tag rule has been set to it or not. - *

- * TODO, to guarantee consistent behavior of interoperability between 2.6- and 2.7+, this method should has the same logic with the TagRouter in 2.6.x. - * - * @param invokers - * @param url - * @param invocation - * @param - * @return - */ - private List> filterUsingStaticTag(List> invokers, URL url, Invocation invocation) { - List> result = invokers; - // Dynamic param - String tag = StringUtils.isEmpty(invocation.getAttachment(TAG_KEY)) ? url.getParameter(TAG_KEY) : - invocation.getAttachment(TAG_KEY); - // Tag request - if (!StringUtils.isEmpty(tag)) { - result = filterInvoker(invokers, invoker -> tag.equals(invoker.getUrl().getParameter(Constants.TAG_KEY))); - if (CollectionUtils.isEmpty(result) && !isForceUseTag(invocation)) { - result = filterInvoker(invokers, invoker -> StringUtils.isEmpty(invoker.getUrl().getParameter(Constants.TAG_KEY))); - } - } else { - result = filterInvoker(invokers, invoker -> StringUtils.isEmpty(invoker.getUrl().getParameter(Constants.TAG_KEY))); - } - return result; + @Override + public int getPriority() { + return DEFAULT_PRIORITY; } @Override @@ -187,7 +157,7 @@ public boolean isForce() { return tagRouterRule != null && tagRouterRule.isForce(); } - private boolean isForceUseTag(Invocation invocation) { + private boolean isForceUse(Invocation invocation) { return Boolean.valueOf(invocation.getAttachment(FORCE_USE_TAG, url.getParameter(FORCE_USE_TAG, "false"))); } @@ -198,26 +168,11 @@ private List> filterInvoker(List> invokers, Predicate< } private boolean addressMatches(URL url, List addresses) { - return addresses != null && checkAddressMatch(addresses, url.getHost(), url.getPort()); + return addresses != null && addresses.contains(url.getAddress()); } private boolean addressNotMatches(URL url, List addresses) { - return addresses == null || !checkAddressMatch(addresses, url.getHost(), url.getPort()); - } - - private boolean checkAddressMatch(List addresses, String host, int port) { - for (String address : addresses) { - try { - if (NetUtils.matchIpExpression(address, host, port)) { - return true; - } - } catch (UnknownHostException e) { - logger.error("The format of ip address is invalid in tag route. Address :" + address, e); - } catch (Exception e) { - logger.error("The format of ip address is invalid in tag route. Address :" + address, e); - } - } - return false; + return addresses == null || !addresses.contains(url.getAddress()); } public void setApplication(String app) { @@ -226,7 +181,7 @@ public void setApplication(String app) { @Override public void notify(List> invokers) { - if (CollectionUtils.isEmpty(invokers)) { + if (invokers == null || invokers.isEmpty()) { return; } @@ -249,7 +204,7 @@ public void notify(List> invokers) { configuration.addListener(key, this); application = providerApplication; String rawRule = configuration.getConfig(key); - if (StringUtils.isNotEmpty(rawRule)) { + if (rawRule != null) { this.process(new ConfigChangeEvent(key, rawRule)); } } diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvoker.java index abc9cf21afb..63486aa5c5c 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvoker.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvoker.java @@ -109,10 +109,10 @@ public void destroy() { * @param invokers invoker candidates * @param selected exclude selected invokers or not * @return the invoker which will final to do invoke. - * @throws RpcException exception + * @throws RpcException */ protected Invoker select(LoadBalance loadbalance, Invocation invocation, - List> invokers, List> selected) throws RpcException { + List> invokers, List> selected) throws RpcException { if (CollectionUtils.isEmpty(invokers)) { return null; @@ -120,7 +120,7 @@ protected Invoker select(LoadBalance loadbalance, Invocation invocation, String methodName = invocation == null ? StringUtils.EMPTY : invocation.getMethodName(); boolean sticky = invokers.get(0).getUrl() - .getMethodParameter(methodName, Constants.CLUSTER_STICKY_KEY, Constants.DEFAULT_CLUSTER_STICKY); + .getMethodParameter(methodName, Constants.CLUSTER_STICKY_KEY, Constants.DEFAULT_CLUSTER_STICKY); //ignore overloaded method if (stickyInvoker != null && !invokers.contains(stickyInvoker)) { @@ -142,7 +142,7 @@ protected Invoker select(LoadBalance loadbalance, Invocation invocation, } private Invoker doSelect(LoadBalance loadbalance, Invocation invocation, - List> invokers, List> selected) throws RpcException { + List> invokers, List> selected) throws RpcException { if (CollectionUtils.isEmpty(invokers)) { return null; @@ -180,20 +180,19 @@ private Invoker doSelect(LoadBalance loadbalance, Invocation invocation, * Reselect, use invokers not in `selected` first, if all invokers are in `selected`, * just pick an available one using loadbalance policy. * - * @param loadbalance load balance policy - * @param invocation invocation - * @param invokers invoker candidates - * @param selected exclude selected invokers or not - * @param availablecheck check invoker available if true - * @return the reselect result to do invoke - * @throws RpcException exception + * @param loadbalance + * @param invocation + * @param invokers + * @param selected + * @return + * @throws RpcException */ private Invoker reselect(LoadBalance loadbalance, Invocation invocation, - List> invokers, List> selected, boolean availablecheck) throws RpcException { + List> invokers, List> selected, boolean availablecheck) throws RpcException { //Allocating one in advance, this list is certain to be used. List> reselectInvokers = new ArrayList<>( - invokers.size() > 1 ? (invokers.size() - 1) : invokers.size()); + invokers.size() > 1 ? (invokers.size() - 1) : invokers.size()); // First, try picking a invoker not in `selected`. for (Invoker invoker : invokers) { @@ -243,6 +242,7 @@ public Result invoke(final Invocation invocation) throws RpcException { } protected void checkWhetherDestroyed() { + if (destroyed.get()) { throw new RpcException("Rpc cluster invoker for " + getInterface() + " on consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ClusterUtils.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ClusterUtils.java index 37b3e719283..8901e4cacec 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ClusterUtils.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ClusterUtils.java @@ -84,9 +84,7 @@ public static URL mergeUrl(URL remoteUrl, Map localMap) { String remoteGroup = map.get(Constants.GROUP_KEY); String remoteRelease = map.get(Constants.RELEASE_KEY); map.putAll(localMap); - if (StringUtils.isNotEmpty(remoteGroup)) { - map.put(Constants.GROUP_KEY, remoteGroup); - } + map.put(Constants.GROUP_KEY, remoteGroup); // we should always keep the Provider RELEASE_KEY not overrode by the the value on Consumer side. map.remove(Constants.RELEASE_KEY); if (StringUtils.isNotEmpty(remoteRelease)) { diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockClusterInvoker.java index 009f4df4428..293d3bda158 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockClusterInvoker.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockClusterInvoker.java @@ -20,7 +20,6 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; @@ -104,7 +103,7 @@ private Result doMockInvoke(Invocation invocation, RpcException e) { Invoker minvoker; List> mockInvokers = selectMockInvoker(invocation); - if (CollectionUtils.isEmpty(mockInvokers)) { + if (mockInvokers == null || mockInvokers.isEmpty()) { minvoker = (Invoker) new MockInvoker(directory.getUrl()); } else { minvoker = mockInvokers.get(0); diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/StickyTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/StickyTest.java index b10fa01c802..f8fdb3178b1 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/StickyTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/StickyTest.java @@ -28,9 +28,9 @@ import org.apache.dubbo.rpc.RpcResult; import org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import java.util.ArrayList; import java.util.List; @@ -56,7 +56,7 @@ public class StickyTest { ); private int runs = 1; - @BeforeEach + @Before public void setUp() throws Exception { dic = mock(Directory.class); invocation = new RpcInvocation(); @@ -77,26 +77,26 @@ public void setUp() throws Exception { public void testStickyNoCheck() { int count = testSticky(null, false); System.out.println(count); - Assertions.assertTrue(count > 0 && count <= runs); + Assert.assertTrue(count > 0 && count <= runs); } @Test public void testStickyForceCheck() { int count = testSticky(null, true); - Assertions.assertTrue(count == 0 || count == runs); + Assert.assertTrue(count == 0 || count == runs); } @Test public void testMethodStickyNoCheck() { int count = testSticky("method1", false); System.out.println(count); - Assertions.assertTrue(count > 0 && count <= runs); + Assert.assertTrue(count > 0 && count <= runs); } @Test public void testMethodStickyForceCheck() { int count = testSticky("method1", true); - Assertions.assertTrue(count == 0 || count == runs); + Assert.assertTrue(count == 0 || count == runs); } @Test @@ -104,7 +104,7 @@ public void testMethodsSticky() { for (int i = 0; i < 100; i++) {//Two different methods should always use the same invoker every time. int count1 = testSticky("method1", true); int count2 = testSticky("method2", true); - Assertions.assertTrue(count1 == count2); + Assert.assertTrue(count1 == count2); } } @@ -129,7 +129,7 @@ public int testSticky(String methodName, boolean check) { int count = 0; for (int i = 0; i < runs; i++) { - Assertions.assertEquals(null, clusterinvoker.invoke(invocation)); + Assert.assertEquals(null, clusterinvoker.invoke(invocation)); if (invoker1 == clusterinvoker.getSelectedInvoker()) { count++; } diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/configurator/absent/AbsentConfiguratorTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/configurator/absent/AbsentConfiguratorTest.java index 36ef868b134..9b3789d3825 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/configurator/absent/AbsentConfiguratorTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/configurator/absent/AbsentConfiguratorTest.java @@ -20,8 +20,8 @@ import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.rpc.cluster.configurator.consts.UrlConstant; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; /** * OverrideConfiguratorTest @@ -34,16 +34,16 @@ public void testOverrideApplication() { AbsentConfigurator configurator = new AbsentConfigurator(URL.valueOf("override://foo@0.0.0.0/com.foo.BarService?timeout=200")); URL url = configurator.configure(URL.valueOf(UrlConstant.URL_CONSUMER)); - Assertions.assertEquals("200", url.getParameter("timeout")); + Assert.assertEquals("200", url.getParameter("timeout")); url = configurator.configure(URL.valueOf(UrlConstant.URL_ONE)); - Assertions.assertEquals("1000", url.getParameter("timeout")); + Assert.assertEquals("1000", url.getParameter("timeout")); url = configurator.configure(URL.valueOf(UrlConstant.APPLICATION_BAR_SIDE_CONSUMER_11)); - Assertions.assertNull(url.getParameter("timeout")); + Assert.assertNull(url.getParameter("timeout")); url = configurator.configure(URL.valueOf(UrlConstant.TIMEOUT_1000_SIDE_CONSUMER_11)); - Assertions.assertEquals("1000", url.getParameter("timeout")); + Assert.assertEquals("1000", url.getParameter("timeout")); } @Test @@ -51,18 +51,18 @@ public void testOverrideHost() { AbsentConfigurator configurator = new AbsentConfigurator(URL.valueOf("override://" + NetUtils.getLocalHost() + "/com.foo.BarService?timeout=200")); URL url = configurator.configure(URL.valueOf(UrlConstant.URL_CONSUMER)); - Assertions.assertEquals("200", url.getParameter("timeout")); + Assert.assertEquals("200", url.getParameter("timeout")); url = configurator.configure(URL.valueOf(UrlConstant.URL_ONE)); - Assertions.assertEquals("1000", url.getParameter("timeout")); + Assert.assertEquals("1000", url.getParameter("timeout")); AbsentConfigurator configurator1 = new AbsentConfigurator(URL.valueOf(UrlConstant.SERVICE_TIMEOUT_200)); url = configurator1.configure(URL.valueOf(UrlConstant.APPLICATION_BAR_SIDE_CONSUMER_10)); - Assertions.assertNull(url.getParameter("timeout")); + Assert.assertNull(url.getParameter("timeout")); url = configurator1.configure(URL.valueOf(UrlConstant.TIMEOUT_1000_SIDE_CONSUMER_10)); - Assertions.assertEquals("1000", url.getParameter("timeout")); + Assert.assertEquals("1000", url.getParameter("timeout")); } } diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/configurator/override/OverrideConfiguratorTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/configurator/override/OverrideConfiguratorTest.java index f2b7d3d2ac2..1f665ba4d12 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/configurator/override/OverrideConfiguratorTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/configurator/override/OverrideConfiguratorTest.java @@ -21,8 +21,8 @@ import org.apache.dubbo.rpc.cluster.configurator.absent.AbsentConfigurator; import org.apache.dubbo.rpc.cluster.configurator.consts.UrlConstant; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; /** * OverrideConfiguratorTest @@ -34,16 +34,16 @@ public void testOverride_Application() { OverrideConfigurator configurator = new OverrideConfigurator(URL.valueOf("override://foo@0.0.0.0/com.foo.BarService?timeout=200")); URL url = configurator.configure(URL.valueOf(UrlConstant.URL_CONSUMER)); - Assertions.assertEquals("200", url.getParameter("timeout")); + Assert.assertEquals("200", url.getParameter("timeout")); url = configurator.configure(URL.valueOf(UrlConstant.URL_ONE)); - Assertions.assertEquals("200", url.getParameter("timeout")); + Assert.assertEquals("200", url.getParameter("timeout")); url = configurator.configure(URL.valueOf(UrlConstant.APPLICATION_BAR_SIDE_CONSUMER_11)); - Assertions.assertNull(url.getParameter("timeout")); + Assert.assertNull(url.getParameter("timeout")); url = configurator.configure(URL.valueOf(UrlConstant.TIMEOUT_1000_SIDE_CONSUMER_11)); - Assertions.assertEquals("1000", url.getParameter("timeout")); + Assert.assertEquals("1000", url.getParameter("timeout")); } @Test @@ -51,18 +51,18 @@ public void testOverride_Host() { OverrideConfigurator configurator = new OverrideConfigurator(URL.valueOf("override://" + NetUtils.getLocalHost() + "/com.foo.BarService?timeout=200")); URL url = configurator.configure(URL.valueOf(UrlConstant.URL_CONSUMER)); - Assertions.assertEquals("200", url.getParameter("timeout")); + Assert.assertEquals("200", url.getParameter("timeout")); url = configurator.configure(URL.valueOf(UrlConstant.URL_ONE)); - Assertions.assertEquals("200", url.getParameter("timeout")); + Assert.assertEquals("200", url.getParameter("timeout")); AbsentConfigurator configurator1 = new AbsentConfigurator(URL.valueOf("override://10.20.153.10/com.foo.BarService?timeout=200")); url = configurator1.configure(URL.valueOf(UrlConstant.APPLICATION_BAR_SIDE_CONSUMER_10)); - Assertions.assertNull(url.getParameter("timeout")); + Assert.assertNull(url.getParameter("timeout")); url = configurator1.configure(URL.valueOf(UrlConstant.TIMEOUT_1000_SIDE_CONSUMER_10)); - Assertions.assertEquals("1000", url.getParameter("timeout")); + Assert.assertEquals("1000", url.getParameter("timeout")); } } diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/configurator/parser/ConfigParserTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/configurator/parser/ConfigParserTest.java index 6bfe3e0c9e1..0c365c02ad8 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/configurator/parser/ConfigParserTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/configurator/parser/ConfigParserTest.java @@ -21,13 +21,12 @@ import org.apache.dubbo.rpc.cluster.configurator.parser.model.ConfigItem; import org.apache.dubbo.rpc.cluster.configurator.parser.model.ConfiguratorConfig; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import org.yaml.snakeyaml.TypeDescription; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; -import java.io.IOException; import java.io.InputStream; import java.util.List; @@ -36,135 +35,129 @@ */ public class ConfigParserTest { - private String streamToString(InputStream stream) throws IOException { - byte[] bytes = new byte[stream.available()]; - stream.read(bytes); - return new String(bytes); + private String streamToString(InputStream stream) { + try { + byte[] bytes = new byte[stream.available()]; + stream.read(bytes); + return new String(bytes); + } catch (Exception e) { + e.printStackTrace(); + } + return null; } @Test - public void snakeYamlBasicTest() throws IOException { - try (InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoApp.yml")) { + public void snakeYamlBasicTest() { + InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoApp.yml"); - Constructor constructor = new Constructor(ConfiguratorConfig.class); - TypeDescription carDescription = new TypeDescription(ConfiguratorConfig.class); - carDescription.addPropertyParameters("items", ConfigItem.class); - constructor.addTypeDescription(carDescription); + Constructor constructor = new Constructor(ConfiguratorConfig.class); + TypeDescription carDescription = new TypeDescription(ConfiguratorConfig.class); + carDescription.addPropertyParameters("items", ConfigItem.class); + constructor.addTypeDescription(carDescription); - Yaml yaml = new Yaml(constructor); - ConfiguratorConfig config = yaml.load(yamlStream); - System.out.println(config); - } + Yaml yaml = new Yaml(constructor); + ConfiguratorConfig config = yaml.load(yamlStream); + System.out.println(config); } @Test public void parseConfiguratorsServiceNoAppTest() throws Exception { - try (InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoApp.yml")) { - List urls = ConfigParser.parseConfigurators(streamToString(yamlStream)); - Assertions.assertNotNull(urls); - Assertions.assertEquals(2, urls.size()); - URL url = urls.get(0); - Assertions.assertEquals(url.getAddress(), "127.0.0.1:20880"); - Assertions.assertEquals(url.getParameter(Constants.WEIGHT_KEY, 0), 222); - } + InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoApp.yml"); + List urls = ConfigParser.parseConfigurators(streamToString(yamlStream)); + Assert.assertNotNull(urls); + Assert.assertEquals(2, urls.size()); + URL url = urls.get(0); + Assert.assertEquals(url.getAddress(), "127.0.0.1:20880"); + Assert.assertEquals(url.getParameter(Constants.WEIGHT_KEY, 0), 222); } @Test public void parseConfiguratorsServiceGroupVersionTest() throws Exception { - try (InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceGroupVersion.yml")) { - List urls = ConfigParser.parseConfigurators(streamToString(yamlStream)); - Assertions.assertNotNull(urls); - Assertions.assertEquals(1, urls.size()); - URL url = urls.get(0); - Assertions.assertEquals("testgroup", url.getParameter(Constants.GROUP_KEY)); - Assertions.assertEquals("1.0.0", url.getParameter(Constants.VERSION_KEY)); - } + InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceGroupVersion.yml"); + List urls = ConfigParser.parseConfigurators(streamToString(yamlStream)); + Assert.assertNotNull(urls); + Assert.assertEquals(1, urls.size()); + URL url = urls.get(0); + Assert.assertEquals("testgroup", url.getParameter(Constants.GROUP_KEY)); + Assert.assertEquals("1.0.0", url.getParameter(Constants.VERSION_KEY)); } @Test - public void parseConfiguratorsServiceMultiAppsTest() throws IOException { - try (InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceMultiApps.yml")) { - List urls = ConfigParser.parseConfigurators(streamToString(yamlStream)); - Assertions.assertNotNull(urls); - Assertions.assertEquals(4, urls.size()); - URL url = urls.get(0); - Assertions.assertEquals("127.0.0.1", url.getAddress()); - Assertions.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0)); - Assertions.assertNotNull(url.getParameter(Constants.APPLICATION_KEY)); - } + public void parseConfiguratorsServiceMultiAppsTest() { + InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceMultiApps.yml"); + List urls = ConfigParser.parseConfigurators(streamToString(yamlStream)); + Assert.assertNotNull(urls); + Assert.assertEquals(4, urls.size()); + URL url = urls.get(0); + Assert.assertEquals("127.0.0.1", url.getAddress()); + Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0)); + Assert.assertNotNull(url.getParameter(Constants.APPLICATION_KEY)); } - @Test + @Test(expected = IllegalStateException.class) public void parseConfiguratorsServiceNoRuleTest() { - Assertions.assertThrows(IllegalStateException.class, () -> { - try (InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoRule.yml")) { - ConfigParser.parseConfigurators(streamToString(yamlStream)); - Assertions.fail(); - } - }); + InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoRule.yml"); + ConfigParser.parseConfigurators(streamToString(yamlStream)); + Assert.fail(); } @Test - public void parseConfiguratorsAppMultiServicesTest() throws IOException { - try (InputStream yamlStream = this.getClass().getResourceAsStream("/AppMultiServices.yml")) { - String yamlFile = streamToString(yamlStream); - List urls = ConfigParser.parseConfigurators(yamlFile); - Assertions.assertNotNull(urls); - Assertions.assertEquals(4, urls.size()); - URL url = urls.get(0); - Assertions.assertEquals("127.0.0.1", url.getAddress()); - Assertions.assertEquals("service1", url.getServiceInterface()); - Assertions.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0)); - Assertions.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY)); - Assertions.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer"); - } + public void parseConfiguratorsAppMultiServicesTest() { + InputStream yamlStream = this.getClass().getResourceAsStream("/AppMultiServices.yml"); + String yamlFile = streamToString(yamlStream); + List urls = ConfigParser.parseConfigurators(yamlFile); + Assert.assertNotNull(urls); + Assert.assertEquals(4, urls.size()); + URL url = urls.get(0); + Assert.assertEquals("127.0.0.1", url.getAddress()); + Assert.assertEquals("service1", url.getServiceInterface()); + Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0)); + Assert.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY)); + Assert.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer"); } @Test - public void parseConfiguratorsAppAnyServicesTest() throws IOException { - try (InputStream yamlStream = this.getClass().getResourceAsStream("/AppAnyServices.yml")) { - List urls = ConfigParser.parseConfigurators(streamToString(yamlStream)); - Assertions.assertNotNull(urls); - Assertions.assertEquals(2, urls.size()); - URL url = urls.get(0); - Assertions.assertEquals("127.0.0.1", url.getAddress()); - Assertions.assertEquals("*", url.getServiceInterface()); - Assertions.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0)); - Assertions.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY)); - Assertions.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer"); - } + public void parseConfiguratorsAppAnyServicesTest() { + InputStream yamlStream = this.getClass().getResourceAsStream("/AppAnyServices.yml"); + List urls = ConfigParser.parseConfigurators(streamToString(yamlStream)); + Assert.assertNotNull(urls); + Assert.assertEquals(2, urls.size()); + URL url = urls.get(0); + Assert.assertEquals("127.0.0.1", url.getAddress()); + Assert.assertEquals("*", url.getServiceInterface()); + Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0)); + Assert.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY)); + Assert.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer"); } @Test - public void parseConfiguratorsAppNoServiceTest() throws IOException { - try (InputStream yamlStream = this.getClass().getResourceAsStream("/AppNoService.yml")) { - List urls = ConfigParser.parseConfigurators(streamToString(yamlStream)); - Assertions.assertNotNull(urls); - Assertions.assertEquals(1, urls.size()); - URL url = urls.get(0); - Assertions.assertEquals("127.0.0.1", url.getAddress()); - Assertions.assertEquals("*", url.getServiceInterface()); - Assertions.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0)); - Assertions.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY)); - Assertions.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer"); - } + public void parseConfiguratorsAppNoServiceTest() { + InputStream yamlStream = this.getClass().getResourceAsStream("/AppNoService.yml"); + List urls = ConfigParser.parseConfigurators(streamToString(yamlStream)); + Assert.assertNotNull(urls); + Assert.assertEquals(1, urls.size()); + URL url = urls.get(0); + Assert.assertEquals("127.0.0.1", url.getAddress()); + Assert.assertEquals("*", url.getServiceInterface()); + Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0)); + Assert.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY)); + Assert.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer"); } @Test - public void parseConsumerSpecificProvidersTest() throws IOException { - try (InputStream yamlStream = this.getClass().getResourceAsStream("/ConsumerSpecificProviders.yml")) { - List urls = ConfigParser.parseConfigurators(streamToString(yamlStream)); - Assertions.assertNotNull(urls); - Assertions.assertEquals(1, urls.size()); - URL url = urls.get(0); - Assertions.assertEquals("127.0.0.1", url.getAddress()); - Assertions.assertEquals("*", url.getServiceInterface()); - Assertions.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0)); - Assertions.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY)); - Assertions.assertEquals("127.0.0.1:20880", url.getParameter(Constants.OVERRIDE_PROVIDERS_KEY)); - Assertions.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer"); - } + public void parseConsumerSpecificProvidersTest() { + InputStream yamlStream = this.getClass().getResourceAsStream("/ConsumerSpecificProviders.yml"); + List urls = ConfigParser.parseConfigurators(streamToString(yamlStream)); + Assert.assertNotNull(urls); + Assert.assertEquals(1, urls.size()); + URL url = urls.get(0); + Assert.assertEquals("127.0.0.1", url.getAddress()); + Assert.assertEquals("*", url.getServiceInterface()); + Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0)); + Assert.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY)); + Assert.assertEquals("127.0.0.1:20880", url.getParameter(Constants.OVERRIDE_PROVIDERS_KEY)); + Assert.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer"); } } diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectoryTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectoryTest.java index 706f1416b5b..ac538b7c9dc 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectoryTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectoryTest.java @@ -25,8 +25,8 @@ import org.apache.dubbo.rpc.cluster.router.MockInvoker; import org.apache.dubbo.rpc.cluster.router.condition.ConditionRouterFactory; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.util.ArrayList; import java.util.List; @@ -56,10 +56,10 @@ public void testStaticDirectory() { List> filteredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation()); StaticDirectory staticDirectory = new StaticDirectory<>(filteredInvokers); boolean isAvailable = staticDirectory.isAvailable(); - Assertions.assertTrue(!isAvailable); + Assert.assertTrue(!isAvailable); List> newInvokers = staticDirectory.list(new MockDirInvocation()); - Assertions.assertTrue(newInvokers.size() > 0); + Assert.assertTrue(newInvokers.size() > 0); staticDirectory.destroy(); - Assertions.assertEquals(0, newInvokers.size()); + Assert.assertEquals(0, newInvokers.size()); } } diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalanceTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalanceTest.java index 48bef62a205..278725f80bb 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalanceTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalanceTest.java @@ -18,8 +18,8 @@ import org.apache.dubbo.rpc.Invoker; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.util.HashMap; import java.util.Map; @@ -28,27 +28,27 @@ @SuppressWarnings("rawtypes") public class ConsistentHashLoadBalanceTest extends LoadBalanceBaseTest { - @Test - public void testConsistentHashLoadBalance() { - int runs = 10000; - long unHitedInvokerCount = 0; - Map hitedInvokers = new HashMap<>(); - Map counter = getInvokeCounter(runs, ConsistentHashLoadBalance.NAME); - for (Invoker minvoker : counter.keySet()) { - Long count = counter.get(minvoker).get(); - - if (count == 0) { - unHitedInvokerCount++; - } else { - hitedInvokers.put(minvoker, count); - } - } - - Assertions.assertEquals(counter.size() - 1, - unHitedInvokerCount, "the number of unHitedInvoker should be counter.size() - 1"); - Assertions.assertEquals(1, hitedInvokers.size(), "the number of hitedInvoker should be 1"); - Assertions.assertEquals(runs, - hitedInvokers.values().iterator().next().intValue(), "the number of hited count should be the number of runs"); - } + @Test + public void testConsistentHashLoadBalance() { + int runs = 10000; + long unHitedInvokerCount = 0; + Map hitedInvokers = new HashMap<>(); + Map counter = getInvokeCounter(runs, ConsistentHashLoadBalance.NAME); + for (Invoker minvoker : counter.keySet()) { + Long count = counter.get(minvoker).get(); + + if (count == 0) { + unHitedInvokerCount++; + } else { + hitedInvokers.put(minvoker, count); + } + } + + Assert.assertEquals("the number of unHitedInvoker should be counter.size() - 1", counter.size() - 1, + unHitedInvokerCount); + Assert.assertEquals("the number of hitedInvoker should be 1", 1, hitedInvokers.size()); + Assert.assertEquals("the number of hited count should be the number of runs", runs, + hitedInvokers.values().iterator().next().intValue()); + } } diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/LeastActiveBalanceTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/LeastActiveBalanceTest.java index 00b5c14db1b..c54e9a00304 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/LeastActiveBalanceTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/LeastActiveBalanceTest.java @@ -17,16 +17,15 @@ package org.apache.dubbo.rpc.cluster.loadbalance; import org.apache.dubbo.rpc.Invoker; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Test; import java.util.Map; import java.util.concurrent.atomic.AtomicLong; public class LeastActiveBalanceTest extends LoadBalanceBaseTest { - @Disabled + @Ignore @Test public void testLeastActiveLoadBalance_select() { int runs = 10000; @@ -34,8 +33,8 @@ public void testLeastActiveLoadBalance_select() { for (Map.Entry entry : counter.entrySet()) { Long count = entry.getValue().get(); // System.out.println(count); - Assertions.assertTrue( - Math.abs(count - runs / (0f + invokers.size())) < runs / (0f + invokers.size()), "abs diff shoud < avg"); + Assert.assertTrue("abs diff shoud < avg", + Math.abs(count - runs / (0f + invokers.size())) < runs / (0f + invokers.size())); } } @@ -57,13 +56,13 @@ public void testSelectByWeight() { sumInvoker2++; } // never select invoker3 because it's active is more than invoker1 and invoker2 - Assertions.assertTrue(!selected.getUrl().getProtocol().equals("test3"), "select is not the least active one"); + Assert.assertTrue("select is not the least active one", !selected.getUrl().getProtocol().equals("test3")); } // the sumInvoker1 : sumInvoker2 approximately equal to 1: 9 System.out.println(sumInvoker1); System.out.println(sumInvoker2); - Assertions.assertEquals(sumInvoker1 + sumInvoker2, loop, "select failed!"); + Assert.assertEquals("select failed!", sumInvoker1 + sumInvoker2, loop); } } diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/LoadBalanceBaseTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/LoadBalanceBaseTest.java index ec43e3871b7..2248aef376a 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/LoadBalanceBaseTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/LoadBalanceBaseTest.java @@ -24,10 +24,10 @@ import org.apache.dubbo.rpc.RpcInvocation; import org.apache.dubbo.rpc.RpcStatus; import org.apache.dubbo.rpc.cluster.LoadBalance; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; import com.alibaba.fastjson.JSON; import org.mockito.Mockito; @@ -63,14 +63,14 @@ public class LoadBalanceBaseTest { /** * @throws java.lang.Exception */ - @BeforeAll + @BeforeClass public static void setUpBeforeClass() throws Exception { } /** * @throws java.lang.Exception */ - @BeforeEach + @Before public void setUp() throws Exception { invocation = mock(Invocation.class); @@ -136,20 +136,20 @@ protected AbstractLoadBalance getLoadBalance(String loadbalanceName) { @Test public void testLoadBalanceWarmup() { - Assertions.assertEquals(1, calculateDefaultWarmupWeight(0)); - Assertions.assertEquals(1, calculateDefaultWarmupWeight(13)); - Assertions.assertEquals(1, calculateDefaultWarmupWeight(6 * 1000)); - Assertions.assertEquals(2, calculateDefaultWarmupWeight(12 * 1000)); - Assertions.assertEquals(10, calculateDefaultWarmupWeight(60 * 1000)); - Assertions.assertEquals(50, calculateDefaultWarmupWeight(5 * 60 * 1000)); - Assertions.assertEquals(50, calculateDefaultWarmupWeight(5 * 60 * 1000 + 23)); - Assertions.assertEquals(50, calculateDefaultWarmupWeight(5 * 60 * 1000 + 5999)); - Assertions.assertEquals(51, calculateDefaultWarmupWeight(5 * 60 * 1000 + 6000)); - Assertions.assertEquals(90, calculateDefaultWarmupWeight(9 * 60 * 1000)); - Assertions.assertEquals(98, calculateDefaultWarmupWeight(10 * 60 * 1000 - 12 * 1000)); - Assertions.assertEquals(99, calculateDefaultWarmupWeight(10 * 60 * 1000 - 6 * 1000)); - Assertions.assertEquals(100, calculateDefaultWarmupWeight(10 * 60 * 1000)); - Assertions.assertEquals(100, calculateDefaultWarmupWeight(20 * 60 * 1000)); + Assert.assertEquals(1, calculateDefaultWarmupWeight(0)); + Assert.assertEquals(1, calculateDefaultWarmupWeight(13)); + Assert.assertEquals(1, calculateDefaultWarmupWeight(6 * 1000)); + Assert.assertEquals(2, calculateDefaultWarmupWeight(12 * 1000)); + Assert.assertEquals(10, calculateDefaultWarmupWeight(60 * 1000)); + Assert.assertEquals(50, calculateDefaultWarmupWeight(5 * 60 * 1000)); + Assert.assertEquals(50, calculateDefaultWarmupWeight(5 * 60 * 1000 + 23)); + Assert.assertEquals(50, calculateDefaultWarmupWeight(5 * 60 * 1000 + 5999)); + Assert.assertEquals(51, calculateDefaultWarmupWeight(5 * 60 * 1000 + 6000)); + Assert.assertEquals(90, calculateDefaultWarmupWeight(9 * 60 * 1000)); + Assert.assertEquals(98, calculateDefaultWarmupWeight(10 * 60 * 1000 - 12 * 1000)); + Assert.assertEquals(99, calculateDefaultWarmupWeight(10 * 60 * 1000 - 6 * 1000)); + Assert.assertEquals(100, calculateDefaultWarmupWeight(10 * 60 * 1000)); + Assert.assertEquals(100, calculateDefaultWarmupWeight(20 * 60 * 1000)); } /** @@ -209,7 +209,7 @@ public String toString() { protected Invoker weightInvoker3; protected Invoker weightInvokerTmp; - @BeforeEach + @Before public void before() throws Exception { weightInvoker1 = mock(Invoker.class, Mockito.withSettings().stubOnly()); weightInvoker2 = mock(Invoker.class, Mockito.withSettings().stubOnly()); diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalanceTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalanceTest.java index f18a07d0e00..3b65ee39580 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalanceTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalanceTest.java @@ -18,9 +18,8 @@ import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.RpcStatus; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.util.Map; import java.util.concurrent.atomic.AtomicLong; @@ -35,7 +34,7 @@ public void testRandomLoadBalanceSelect() { Map counter = getInvokeCounter(runs, RandomLoadBalance.NAME); for (Map.Entry entry : counter.entrySet()) { Long count = entry.getValue().get(); - Assertions.assertTrue(Math.abs(count - runs / (0f + invokers.size())) < runs / (0f + invokers.size()), "abs diff should < avg"); + Assert.assertTrue("abs diff should < avg", Math.abs(count - runs / (0f + invokers.size())) < runs / (0f + invokers.size())); } for (int i = 0; i < 5; i++) { @@ -47,11 +46,11 @@ public void testRandomLoadBalanceSelect() { for (Map.Entry entry : counter.entrySet()) { Long count = entry.getValue().get(); } - Assertions.assertEquals(runs, counter.get(invoker1).intValue()); - Assertions.assertEquals(0, counter.get(invoker2).intValue()); - Assertions.assertEquals(0, counter.get(invoker3).intValue()); - Assertions.assertEquals(0, counter.get(invoker4).intValue()); - Assertions.assertEquals(0, counter.get(invoker5).intValue()); + Assert.assertEquals(runs, counter.get(invoker1).intValue()); + Assert.assertEquals(0, counter.get(invoker2).intValue()); + Assert.assertEquals(0, counter.get(invoker3).intValue()); + Assert.assertEquals(0, counter.get(invoker4).intValue()); + Assert.assertEquals(0, counter.get(invoker5).intValue()); } @Test @@ -82,7 +81,7 @@ public void testSelectByWeight() { System.out.println(sumInvoker1); System.out.println(sumInvoker2); System.out.println(sumInvoker3); - Assertions.assertEquals(sumInvoker1 + sumInvoker2 + sumInvoker3, loop, "select failed!"); + Assert.assertEquals("select failed!", sumInvoker1 + sumInvoker2 + sumInvoker3, loop); } } diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalanceTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalanceTest.java index 86d4130a21d..66488bf10ee 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalanceTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalanceTest.java @@ -17,10 +17,9 @@ package org.apache.dubbo.rpc.cluster.loadbalance; import org.apache.dubbo.rpc.Invoker; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Test; import java.lang.reflect.Field; import java.util.ArrayList; @@ -31,7 +30,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; -@Disabled +@Ignore public class RoundRobinLoadBalanceTest extends LoadBalanceBaseTest { private void assertStrictWRRResult(int loop, Map resultMap) { @@ -39,11 +38,11 @@ private void assertStrictWRRResult(int loop, Map resultMa for (InvokeResult invokeResult : resultMap.values()) { int count = (int) invokeResult.getCount().get(); // Because it's a strictly round robin, so the abs delta should be < 10 too - Assertions.assertTrue( - Math.abs(invokeResult.getExpected(loop) - count) < 10, "delta with expected count should < 10"); + Assert.assertTrue("delta with expected count should < 10", + Math.abs(invokeResult.getExpected(loop) - count) < 10); invokeCount += count; } - Assertions.assertEquals(invokeCount, loop, "select failed!"); + Assert.assertEquals("select failed!", invokeCount, loop); } @Test @@ -52,7 +51,7 @@ public void testRoundRobinLoadBalanceSelect() { Map counter = getInvokeCounter(runs, RoundRobinLoadBalance.NAME); for (Map.Entry entry : counter.entrySet()) { Long count = entry.getValue().get(); - Assertions.assertTrue(Math.abs(count - runs / (0f + invokers.size())) < 1f, "abs diff should < 1"); + Assert.assertTrue("abs diff should < 1", Math.abs(count - runs / (0f + invokers.size())) < 1f); } } @@ -63,7 +62,7 @@ public void testSelectByWeight() { final int runs = 10000; List threads = new ArrayList(); int threadNum = 10; - for (int i = 0; i < threadNum; i++) { + for (int i = 0; i < threadNum; i ++) { threads.add(new Thread() { @Override public void run() { @@ -110,15 +109,15 @@ public void testNodeCacheShouldNotRecycle() { assertStrictWRRResult(loop, resultMap); // inner nodes cache judgement - RoundRobinLoadBalance lb = (RoundRobinLoadBalance) getLoadBalance(RoundRobinLoadBalance.NAME); - Assertions.assertEquals(weightInvokers.size(), lb.getInvokerAddrList(weightInvokers, weightTestInvocation).size()); + RoundRobinLoadBalance lb = (RoundRobinLoadBalance)getLoadBalance(RoundRobinLoadBalance.NAME); + Assert.assertEquals(weightInvokers.size(), lb.getInvokerAddrList(weightInvokers, weightTestInvocation).size()); weightInvokers.remove(weightInvokerTmp); resultMap = getWeightedInvokeResult(loop, RoundRobinLoadBalance.NAME); assertStrictWRRResult(loop, resultMap); - Assertions.assertNotEquals(weightInvokers.size(), lb.getInvokerAddrList(weightInvokers, weightTestInvocation).size()); + Assert.assertNotEquals(weightInvokers.size(), lb.getInvokerAddrList(weightInvokers, weightTestInvocation).size()); } finally { //prevent other UT's failure weightInvokers.remove(weightInvokerTmp); @@ -135,13 +134,13 @@ public void testNodeCacheShouldRecycle() { recycleTimeField.setAccessible(true); recycleTimeField.setInt(RoundRobinLoadBalance.class, 10); } catch (NoSuchFieldException e) { - Assertions.assertTrue(true, "getField failed"); + Assert.assertTrue("getField failed", true); } catch (SecurityException e) { - Assertions.assertTrue(true, "getField failed"); + Assert.assertTrue("getField failed", true); } catch (IllegalArgumentException e) { - Assertions.assertTrue(true, "getField failed"); + Assert.assertTrue("getField failed", true); } catch (IllegalAccessException e) { - Assertions.assertTrue(true, "getField failed"); + Assert.assertTrue("getField failed", true); } } @@ -153,15 +152,15 @@ public void testNodeCacheShouldRecycle() { assertStrictWRRResult(loop, resultMap); // inner nodes cache judgement - RoundRobinLoadBalance lb = (RoundRobinLoadBalance) getLoadBalance(RoundRobinLoadBalance.NAME); - Assertions.assertEquals(weightInvokers.size(), lb.getInvokerAddrList(weightInvokers, weightTestInvocation).size()); + RoundRobinLoadBalance lb = (RoundRobinLoadBalance)getLoadBalance(RoundRobinLoadBalance.NAME); + Assert.assertEquals(weightInvokers.size(), lb.getInvokerAddrList(weightInvokers, weightTestInvocation).size()); weightInvokers.remove(weightInvokerTmp); resultMap = getWeightedInvokeResult(loop, RoundRobinLoadBalance.NAME); assertStrictWRRResult(loop, resultMap); - Assertions.assertEquals(weightInvokers.size(), lb.getInvokerAddrList(weightInvokers, weightTestInvocation).size()); + Assert.assertEquals(weightInvokers.size(), lb.getInvokerAddrList(weightInvokers, weightTestInvocation).size()); } finally { //prevent other UT's failure weightInvokers.remove(weightInvokerTmp); diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/merger/ResultMergerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/merger/ResultMergerTest.java index 788d1af160f..e4c30a4553f 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/merger/ResultMergerTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/merger/ResultMergerTest.java @@ -16,8 +16,8 @@ */ package org.apache.dubbo.rpc.cluster.merger; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.lang.reflect.Array; import java.util.ArrayList; @@ -36,9 +36,9 @@ public class ResultMergerTest { public void testMergerFactoryIllegalArgumentException() { try { MergerFactory.getMerger(null); - Assertions.fail("expected IllegalArgumentException for null argument"); + Assert.fail("expected IllegalArgumentException for null argument"); } catch (IllegalArgumentException exception) { - Assertions.assertEquals("returnType is null", exception.getMessage()); + Assert.assertEquals("returnType is null", exception.getMessage()); } } @@ -51,9 +51,9 @@ public void testArrayMergerIllegalArgumentException() { Integer[] integerArray = {3, 4, 5}; try { Object result = ArrayMerger.INSTANCE.merge(stringArray, null, integerArray); - Assertions.fail("expected IllegalArgumentException for different arguments' types"); + Assert.fail("expected IllegalArgumentException for different arguments' types"); } catch (IllegalArgumentException exception) { - Assertions.assertEquals("Arguments' types are different", exception.getMessage()); + Assert.assertEquals("Arguments' types are different", exception.getMessage()); } } @@ -67,11 +67,11 @@ public void testArrayMerger() { String[] stringArray3 = {}; Object result = ArrayMerger.INSTANCE.merge(stringArray1, stringArray2, stringArray3, null); - Assertions.assertTrue(result.getClass().isArray()); - Assertions.assertEquals(6, Array.getLength(result)); - Assertions.assertTrue(String.class.isInstance(Array.get(result, 0))); + Assert.assertTrue(result.getClass().isArray()); + Assert.assertEquals(6, Array.getLength(result)); + Assert.assertTrue(String.class.isInstance(Array.get(result, 0))); for (int i = 0; i < 6; i++) { - Assertions.assertEquals(String.valueOf(i + 1), Array.get(result, i)); + Assert.assertEquals(String.valueOf(i + 1), Array.get(result, i)); } Integer[] intArray1 = {1, 2, 3}; @@ -79,21 +79,21 @@ public void testArrayMerger() { Integer[] intArray3 = {7}; // trigger ArrayMerger result = MergerFactory.getMerger(Integer[].class).merge(intArray1, intArray2, intArray3, null); - Assertions.assertTrue(result.getClass().isArray()); - Assertions.assertEquals(7, Array.getLength(result)); - Assertions.assertTrue(Integer.class == result.getClass().getComponentType()); + Assert.assertTrue(result.getClass().isArray()); + Assert.assertEquals(7, Array.getLength(result)); + Assert.assertTrue(Integer.class == result.getClass().getComponentType()); for (int i = 0; i < 7; i++) { - Assertions.assertEquals(i + 1, Array.get(result, i)); + Assert.assertEquals(i + 1, Array.get(result, i)); } result = ArrayMerger.INSTANCE.merge(null); - Assertions.assertEquals(0, Array.getLength(result)); + Assert.assertEquals(0, Array.getLength(result)); result = ArrayMerger.INSTANCE.merge(null, null); - Assertions.assertEquals(0, Array.getLength(result)); + Assert.assertEquals(0, Array.getLength(result)); result = ArrayMerger.INSTANCE.merge(null, new Object[0]); - Assertions.assertEquals(0, Array.getLength(result)); + Assert.assertEquals(0, Array.getLength(result)); } /** @@ -104,17 +104,17 @@ public void testBooleanArrayMerger() { boolean[] arrayOne = {true, false}; boolean[] arrayTwo = {false}; boolean[] result = MergerFactory.getMerger(boolean[].class).merge(arrayOne, arrayTwo, null); - Assertions.assertEquals(3, result.length); + Assert.assertEquals(3, result.length); boolean[] mergedResult = {true, false, false}; for (int i = 0; i < mergedResult.length; i++) { - Assertions.assertEquals(mergedResult[i], result[i]); + Assert.assertEquals(mergedResult[i], result[i]); } result = MergerFactory.getMerger(boolean[].class).merge(null); - Assertions.assertEquals(0, result.length); + Assert.assertEquals(0, result.length); result = MergerFactory.getMerger(boolean[].class).merge(null, null); - Assertions.assertEquals(0, result.length); + Assert.assertEquals(0, result.length); } /** @@ -125,17 +125,17 @@ public void testByteArrayMerger() { byte[] arrayOne = {1, 2}; byte[] arrayTwo = {1, 32}; byte[] result = MergerFactory.getMerger(byte[].class).merge(arrayOne, arrayTwo, null); - Assertions.assertEquals(4, result.length); + Assert.assertEquals(4, result.length); byte[] mergedResult = {1, 2, 1, 32}; for (int i = 0; i < mergedResult.length; i++) { - Assertions.assertEquals(mergedResult[i], result[i]); + Assert.assertEquals(mergedResult[i], result[i]); } result = MergerFactory.getMerger(byte[].class).merge(null); - Assertions.assertEquals(0, result.length); + Assert.assertEquals(0, result.length); result = MergerFactory.getMerger(byte[].class).merge(null, null); - Assertions.assertEquals(0, result.length); + Assert.assertEquals(0, result.length); } /** @@ -146,17 +146,17 @@ public void testCharArrayMerger() { char[] arrayOne = "hello".toCharArray(); char[] arrayTwo = "world".toCharArray(); char[] result = MergerFactory.getMerger(char[].class).merge(arrayOne, arrayTwo, null); - Assertions.assertEquals(10, result.length); + Assert.assertEquals(10, result.length); char[] mergedResult = "helloworld".toCharArray(); for (int i = 0; i < mergedResult.length; i++) { - Assertions.assertEquals(mergedResult[i], result[i]); + Assert.assertEquals(mergedResult[i], result[i]); } result = MergerFactory.getMerger(char[].class).merge(null); - Assertions.assertEquals(0, result.length); + Assert.assertEquals(0, result.length); result = MergerFactory.getMerger(char[].class).merge(null, null); - Assertions.assertEquals(0, result.length); + Assert.assertEquals(0, result.length); } /** @@ -167,17 +167,17 @@ public void testDoubleArrayMerger() { double[] arrayOne = {1.2d, 3.5d}; double[] arrayTwo = {2d, 34d}; double[] result = MergerFactory.getMerger(double[].class).merge(arrayOne, arrayTwo, null); - Assertions.assertEquals(4, result.length); + Assert.assertEquals(4, result.length); double[] mergedResult = {1.2d, 3.5d, 2d, 34d}; for (int i = 0; i < mergedResult.length; i++) { - Assertions.assertTrue(mergedResult[i] == result[i]); + Assert.assertTrue(mergedResult[i] == result[i]); } result = MergerFactory.getMerger(double[].class).merge(null); - Assertions.assertEquals(0, result.length); + Assert.assertEquals(0, result.length); result = MergerFactory.getMerger(double[].class).merge(null, null); - Assertions.assertEquals(0, result.length); + Assert.assertEquals(0, result.length); } /** @@ -188,17 +188,17 @@ public void testFloatArrayMerger() { float[] arrayOne = {1.2f, 3.5f}; float[] arrayTwo = {2f, 34f}; float[] result = MergerFactory.getMerger(float[].class).merge(arrayOne, arrayTwo, null); - Assertions.assertEquals(4, result.length); + Assert.assertEquals(4, result.length); double[] mergedResult = {1.2f, 3.5f, 2f, 34f}; for (int i = 0; i < mergedResult.length; i++) { - Assertions.assertTrue(mergedResult[i] == result[i]); + Assert.assertTrue(mergedResult[i] == result[i]); } result = MergerFactory.getMerger(float[].class).merge(null); - Assertions.assertEquals(0, result.length); + Assert.assertEquals(0, result.length); result = MergerFactory.getMerger(float[].class).merge(null, null); - Assertions.assertEquals(0, result.length); + Assert.assertEquals(0, result.length); } /** @@ -209,17 +209,17 @@ public void testIntArrayMerger() { int[] arrayOne = {1, 2}; int[] arrayTwo = {2, 34}; int[] result = MergerFactory.getMerger(int[].class).merge(arrayOne, arrayTwo, null); - Assertions.assertEquals(4, result.length); + Assert.assertEquals(4, result.length); double[] mergedResult = {1, 2, 2, 34}; for (int i = 0; i < mergedResult.length; i++) { - Assertions.assertTrue(mergedResult[i] == result[i]); + Assert.assertTrue(mergedResult[i] == result[i]); } result = MergerFactory.getMerger(int[].class).merge(null); - Assertions.assertEquals(0, result.length); + Assert.assertEquals(0, result.length); result = MergerFactory.getMerger(int[].class).merge(null, null); - Assertions.assertEquals(0, result.length); + Assert.assertEquals(0, result.length); } /** @@ -238,7 +238,7 @@ public void testListMerger() { }}; List result = MergerFactory.getMerger(List.class).merge(list1, list2, null); - Assertions.assertEquals(5, result.size()); + Assert.assertEquals(5, result.size()); ArrayList expected = new ArrayList() {{ add(null); add("1"); @@ -246,13 +246,13 @@ public void testListMerger() { add("3"); add("4"); }}; - Assertions.assertEquals(expected, result); + Assert.assertEquals(expected, result); result = MergerFactory.getMerger(List.class).merge(null); - Assertions.assertEquals(0, result.size()); + Assert.assertEquals(0, result.size()); result = MergerFactory.getMerger(List.class).merge(null, null); - Assertions.assertEquals(0, result.size()); + Assert.assertEquals(0, result.size()); } /** @@ -269,20 +269,20 @@ public void testMapArrayMerger() { put("444", 2323); }}; Map result = MergerFactory.getMerger(Map.class).merge(mapOne, mapTwo, null); - Assertions.assertEquals(4, result.size()); + Assert.assertEquals(4, result.size()); Map mergedResult = new HashMap() {{ put("11", 222); put("223", 11); put("3333", 3232); put("444", 2323); }}; - Assertions.assertEquals(mergedResult, result); + Assert.assertEquals(mergedResult, result); result = MergerFactory.getMerger(Map.class).merge(null); - Assertions.assertEquals(0, result.size()); + Assert.assertEquals(0, result.size()); result = MergerFactory.getMerger(Map.class).merge(null, null); - Assertions.assertEquals(0, result.size()); + Assert.assertEquals(0, result.size()); } /** @@ -293,17 +293,17 @@ public void testLongArrayMerger() { long[] arrayOne = {1l, 2l}; long[] arrayTwo = {2l, 34l}; long[] result = MergerFactory.getMerger(long[].class).merge(arrayOne, arrayTwo, null); - Assertions.assertEquals(4, result.length); + Assert.assertEquals(4, result.length); double[] mergedResult = {1l, 2l, 2l, 34l}; for (int i = 0; i < mergedResult.length; i++) { - Assertions.assertTrue(mergedResult[i] == result[i]); + Assert.assertTrue(mergedResult[i] == result[i]); } result = MergerFactory.getMerger(long[].class).merge(null); - Assertions.assertEquals(0, result.length); + Assert.assertEquals(0, result.length); result = MergerFactory.getMerger(long[].class).merge(null, null); - Assertions.assertEquals(0, result.length); + Assert.assertEquals(0, result.length); } /** @@ -324,8 +324,8 @@ public void testSetMerger() { Set result = MergerFactory.getMerger(Set.class).merge(set1, set2, null); - Assertions.assertEquals(4, result.size()); - Assertions.assertEquals(new HashSet() { + Assert.assertEquals(4, result.size()); + Assert.assertEquals(new HashSet() { { add(null); add("1"); @@ -335,10 +335,10 @@ public void testSetMerger() { }, result); result = MergerFactory.getMerger(Set.class).merge(null); - Assertions.assertEquals(0, result.size()); + Assert.assertEquals(0, result.size()); result = MergerFactory.getMerger(Set.class).merge(null, null); - Assertions.assertEquals(0, result.size()); + Assert.assertEquals(0, result.size()); } /** @@ -349,16 +349,16 @@ public void testShortArrayMerger() { short[] arrayOne = {1, 2}; short[] arrayTwo = {2, 34}; short[] result = MergerFactory.getMerger(short[].class).merge(arrayOne, arrayTwo, null); - Assertions.assertEquals(4, result.length); + Assert.assertEquals(4, result.length); double[] mergedResult = {1, 2, 2, 34}; for (int i = 0; i < mergedResult.length; i++) { - Assertions.assertTrue(mergedResult[i] == result[i]); + Assert.assertTrue(mergedResult[i] == result[i]); } result = MergerFactory.getMerger(short[].class).merge(null); - Assertions.assertEquals(0, result.length); + Assert.assertEquals(0, result.length); result = MergerFactory.getMerger(short[].class).merge(null, null); - Assertions.assertEquals(0, result.length); + Assert.assertEquals(0, result.length); } } diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/ConfigConditionRouterTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/ConfigConditionRouterTest.java index 1235869a2e7..76d9ef7a05d 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/ConfigConditionRouterTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/ConfigConditionRouterTest.java @@ -19,15 +19,15 @@ import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.retry.ExponentialBackoffRetry; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; -@Disabled("FIXME This is not a formal UT") +@Ignore("FIXME This is not a formal UT") public class ConfigConditionRouterTest { private static CuratorFramework client; - @BeforeEach + @Before public void init() { client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", 60 * 1000, 60 * 1000, new ExponentialBackoffRetry(1000, 3)); diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/TagRouterTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/TagRouterTest.java index 98cdaf4d4df..a2e45e168cd 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/TagRouterTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/TagRouterTest.java @@ -19,15 +19,15 @@ import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.retry.ExponentialBackoffRetry; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; -@Disabled("FIXME This is not a formal UT") +@Ignore("FIXME This is not a formal UT") public class TagRouterTest { private static CuratorFramework client; - @BeforeEach + @Before public void init() { client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", 60 * 1000, 60 * 1000, new ExponentialBackoffRetry(1000, 3)); diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/condition/ConditionRouterTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/condition/ConditionRouterTest.java index a10949d3016..f7b85b84f1d 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/condition/ConditionRouterTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/condition/ConditionRouterTest.java @@ -26,10 +26,10 @@ import org.apache.dubbo.rpc.cluster.Router; import org.apache.dubbo.rpc.cluster.router.MockInvoker; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; import java.util.ArrayList; import java.util.List; @@ -38,11 +38,11 @@ public class ConditionRouterTest { private URL SCRIPT_URL = URL.valueOf("condition://0.0.0.0/com.foo.BarService"); - @BeforeAll + @BeforeClass public static void setUpBeforeClass() throws Exception { } - @BeforeEach + @Before public void setUp() throws Exception { } @@ -56,31 +56,31 @@ public void testRoute_matchWhen() { Router router = new ConditionRouterFactory().getRouter(getRouteUrl(" => host = 1.2.3.4")); boolean matchWhen = ((ConditionRouter) router).matchWhen(URL.valueOf("consumer://1.1.1.1/com.foo.BarService"), invocation); - Assertions.assertEquals(true, matchWhen); + Assert.assertEquals(true, matchWhen); router = new ConditionRouterFactory().getRouter(getRouteUrl("host = 2.2.2.2,1.1.1.1,3.3.3.3 => host = 1.2.3.4")); matchWhen = ((ConditionRouter) router).matchWhen(URL.valueOf("consumer://1.1.1.1/com.foo.BarService"), invocation); - Assertions.assertEquals(true, matchWhen); + Assert.assertEquals(true, matchWhen); router = new ConditionRouterFactory().getRouter(getRouteUrl("host = 2.2.2.2,1.1.1.1,3.3.3.3 & host !=1.1.1.1 => host = 1.2.3.4")); matchWhen = ((ConditionRouter) router).matchWhen(URL.valueOf("consumer://1.1.1.1/com.foo.BarService"), invocation); - Assertions.assertEquals(false, matchWhen); + Assert.assertEquals(false, matchWhen); router = new ConditionRouterFactory().getRouter(getRouteUrl("host !=4.4.4.4 & host = 2.2.2.2,1.1.1.1,3.3.3.3 => host = 1.2.3.4")); matchWhen = ((ConditionRouter) router).matchWhen(URL.valueOf("consumer://1.1.1.1/com.foo.BarService"), invocation); - Assertions.assertEquals(true, matchWhen); + Assert.assertEquals(true, matchWhen); router = new ConditionRouterFactory().getRouter(getRouteUrl("host !=4.4.4.* & host = 2.2.2.2,1.1.1.1,3.3.3.3 => host = 1.2.3.4")); matchWhen = ((ConditionRouter) router).matchWhen(URL.valueOf("consumer://1.1.1.1/com.foo.BarService"), invocation); - Assertions.assertEquals(true, matchWhen); + Assert.assertEquals(true, matchWhen); router = new ConditionRouterFactory().getRouter(getRouteUrl("host = 2.2.2.2,1.1.1.*,3.3.3.3 & host != 1.1.1.1 => host = 1.2.3.4")); matchWhen = ((ConditionRouter) router).matchWhen(URL.valueOf("consumer://1.1.1.1/com.foo.BarService"), invocation); - Assertions.assertEquals(false, matchWhen); + Assert.assertEquals(false, matchWhen); router = new ConditionRouterFactory().getRouter(getRouteUrl("host = 2.2.2.2,1.1.1.*,3.3.3.3 & host != 1.1.1.2 => host = 1.2.3.4")); matchWhen = ((ConditionRouter) router).matchWhen(URL.valueOf("consumer://1.1.1.1/com.foo.BarService"), invocation); - Assertions.assertEquals(true, matchWhen); + Assert.assertEquals(true, matchWhen); } @Test @@ -121,12 +121,12 @@ public void testRoute_matchFilter() { List> filteredInvokers4 = router4.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation()); List> filteredInvokers5 = router5.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation()); List> filteredInvokers6 = router6.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation()); - Assertions.assertEquals(1, filteredInvokers1.size()); - Assertions.assertEquals(0, filteredInvokers2.size()); - Assertions.assertEquals(0, filteredInvokers3.size()); - Assertions.assertEquals(1, filteredInvokers4.size()); - Assertions.assertEquals(2, filteredInvokers5.size()); - Assertions.assertEquals(1, filteredInvokers6.size()); + Assert.assertEquals(1, filteredInvokers1.size()); + Assert.assertEquals(0, filteredInvokers2.size()); + Assert.assertEquals(0, filteredInvokers3.size()); + Assert.assertEquals(1, filteredInvokers4.size()); + Assert.assertEquals(2, filteredInvokers5.size()); + Assert.assertEquals(1, filteredInvokers6.size()); } @Test @@ -136,23 +136,23 @@ public void testRoute_methodRoute() { Router router = new ConditionRouterFactory().getRouter(getRouteUrl("methods=getFoo => host = 1.2.3.4")); boolean matchWhen = ((ConditionRouter) router).matchWhen( URL.valueOf("consumer://1.1.1.1/com.foo.BarService?methods=setFoo,getFoo,findFoo"), invocation); - Assertions.assertEquals(true, matchWhen); + Assert.assertEquals(true, matchWhen); // Exactly one method, match matchWhen = ((ConditionRouter) router).matchWhen( URL.valueOf("consumer://1.1.1.1/com.foo.BarService?methods=getFoo"), invocation); - Assertions.assertEquals(true, matchWhen); + Assert.assertEquals(true, matchWhen); // Method routing and Other condition routing can work together Router router2 = new ConditionRouterFactory() .getRouter(getRouteUrl("methods=getFoo & host!=1.1.1.1 => host = 1.2.3.4")); matchWhen = ((ConditionRouter) router2).matchWhen( URL.valueOf("consumer://1.1.1.1/com.foo.BarService?methods=getFoo"), invocation); - Assertions.assertEquals(false, matchWhen); + Assert.assertEquals(false, matchWhen); Router router3 = new ConditionRouterFactory() .getRouter(getRouteUrl("methods=getFoo & host=1.1.1.1 => host = 1.2.3.4")); matchWhen = ((ConditionRouter) router3).matchWhen( URL.valueOf("consumer://1.1.1.1/com.foo.BarService?methods=getFoo"), invocation); - Assertions.assertEquals(true, matchWhen); + Assert.assertEquals(true, matchWhen); // Test filter condition List> invokers = new ArrayList>(); Invoker invoker1 = new MockInvoker(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService")); @@ -169,14 +169,14 @@ public void testRoute_methodRoute() { Constants.FORCE_KEY, String.valueOf(true))); List> filteredInvokers1 = router4.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), invocation); - Assertions.assertEquals(1, filteredInvokers1.size()); + Assert.assertEquals(1, filteredInvokers1.size()); Router router5 = new ConditionRouterFactory().getRouter(getRouteUrl( "host = " + NetUtils.getLocalHost() + " & methods = unvalidmethod => " + " host = 10.20.3.3") .addParameter(Constants.FORCE_KEY, String.valueOf(true))); List> filteredInvokers2 = router5.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), invocation); - Assertions.assertEquals(3, filteredInvokers2.size()); + Assert.assertEquals(3, filteredInvokers2.size()); // Request a non-exists method } @@ -188,7 +188,7 @@ public void testRoute_ReturnFalse() { invokers.add(new MockInvoker()); invokers.add(new MockInvoker()); List> filteredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation()); - Assertions.assertEquals(0, filteredInvokers.size()); + Assert.assertEquals(0, filteredInvokers.size()); } @Test @@ -199,7 +199,7 @@ public void testRoute_ReturnEmpty() { invokers.add(new MockInvoker()); invokers.add(new MockInvoker()); List> filteredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation()); - Assertions.assertEquals(0, filteredInvokers.size()); + Assert.assertEquals(0, filteredInvokers.size()); } @Test @@ -210,7 +210,7 @@ public void testRoute_ReturnAll() { invokers.add(new MockInvoker(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService"))); invokers.add(new MockInvoker(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService"))); List> filteredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation()); - Assertions.assertEquals(invokers, filteredInvokers); + Assert.assertEquals(invokers, filteredInvokers); } @Test @@ -224,9 +224,9 @@ public void testRoute_HostFilter() { invokers.add(invoker2); invokers.add(invoker3); List> filteredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation()); - Assertions.assertEquals(2, filteredInvokers.size()); - Assertions.assertEquals(invoker2, filteredInvokers.get(0)); - Assertions.assertEquals(invoker3, filteredInvokers.get(1)); + Assert.assertEquals(2, filteredInvokers.size()); + Assert.assertEquals(invoker2, filteredInvokers.get(0)); + Assert.assertEquals(invoker3, filteredInvokers.get(1)); } @Test @@ -240,9 +240,9 @@ public void testRoute_Empty_HostFilter() { invokers.add(invoker2); invokers.add(invoker3); List> filteredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation()); - Assertions.assertEquals(2, filteredInvokers.size()); - Assertions.assertEquals(invoker2, filteredInvokers.get(0)); - Assertions.assertEquals(invoker3, filteredInvokers.get(1)); + Assert.assertEquals(2, filteredInvokers.size()); + Assert.assertEquals(invoker2, filteredInvokers.get(0)); + Assert.assertEquals(invoker3, filteredInvokers.get(1)); } @Test @@ -256,9 +256,9 @@ public void testRoute_False_HostFilter() { invokers.add(invoker2); invokers.add(invoker3); List> filteredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation()); - Assertions.assertEquals(2, filteredInvokers.size()); - Assertions.assertEquals(invoker2, filteredInvokers.get(0)); - Assertions.assertEquals(invoker3, filteredInvokers.get(1)); + Assert.assertEquals(2, filteredInvokers.size()); + Assert.assertEquals(invoker2, filteredInvokers.get(0)); + Assert.assertEquals(invoker3, filteredInvokers.get(1)); } @Test @@ -272,9 +272,9 @@ public void testRoute_Placeholder() { invokers.add(invoker2); invokers.add(invoker3); List> filteredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation()); - Assertions.assertEquals(2, filteredInvokers.size()); - Assertions.assertEquals(invoker2, filteredInvokers.get(0)); - Assertions.assertEquals(invoker3, filteredInvokers.get(1)); + Assert.assertEquals(2, filteredInvokers.size()); + Assert.assertEquals(invoker2, filteredInvokers.get(0)); + Assert.assertEquals(invoker3, filteredInvokers.get(1)); } @Test @@ -288,7 +288,7 @@ public void testRoute_NoForce() { invokers.add(invoker2); invokers.add(invoker3); List> filteredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation()); - Assertions.assertEquals(invokers, filteredInvokers); + Assert.assertEquals(invokers, filteredInvokers); } @Test @@ -302,7 +302,7 @@ public void testRoute_Force() { invokers.add(invoker2); invokers.add(invoker3); List> filteredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation()); - Assertions.assertEquals(0, filteredInvokers.size()); + Assert.assertEquals(0, filteredInvokers.size()); } } \ No newline at end of file diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/file/FileRouterEngineTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/file/FileRouterEngineTest.java index c4cb85daf8b..1ccb947bd29 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/file/FileRouterEngineTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/file/FileRouterEngineTest.java @@ -31,10 +31,10 @@ import org.apache.dubbo.rpc.cluster.directory.StaticDirectory; import org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; import javax.script.ScriptEngineManager; import java.util.ArrayList; @@ -55,11 +55,11 @@ public class FileRouterEngineTest { Result result = new RpcResult(); private RouterFactory routerFactory = ExtensionLoader.getExtensionLoader(RouterFactory.class).getAdaptiveExtension(); - @BeforeAll + @BeforeClass public static void setUpBeforeClass() throws Exception { } - @BeforeEach + @Before public void setUp() throws Exception { invokers.add(invoker1); invokers.add(invoker2); @@ -78,7 +78,7 @@ public void testRouteNotAvailable() { for (int i = 0; i < 100; i++) { sinvoker.invoke(invocation); Invoker invoker = sinvoker.getSelectedInvoker(); - Assertions.assertEquals(invoker2, invoker); + Assert.assertEquals(invoker2, invoker); } } @@ -95,7 +95,7 @@ public void testRouteAvailable() { for (int i = 0; i < 100; i++) { sinvoker.invoke(invocation); Invoker invoker = sinvoker.getSelectedInvoker(); - Assertions.assertEquals(invoker1, invoker); + Assert.assertEquals(invoker1, invoker); } } @@ -113,7 +113,7 @@ public void testRouteByMethodName() { for (int i = 0; i < 100; i++) { sinvoker.invoke(invocation); Invoker invoker = sinvoker.getSelectedInvoker(); - Assertions.assertEquals(invoker1, invoker); + Assert.assertEquals(invoker1, invoker); } } { @@ -125,7 +125,7 @@ public void testRouteByMethodName() { for (int i = 0; i < 100; i++) { sinvoker.invoke(invocation); Invoker invoker = sinvoker.getSelectedInvoker(); - Assertions.assertEquals(invoker2, invoker); + Assert.assertEquals(invoker2, invoker); } } } diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouterTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouterTest.java index 45e4e7fb9e4..3d94b16b820 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouterTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouterTest.java @@ -17,30 +17,29 @@ package org.apache.dubbo.rpc.cluster.router.script; +import java.util.ArrayList; +import java.util.List; + import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.RpcInvocation; import org.apache.dubbo.rpc.cluster.Router; import org.apache.dubbo.rpc.cluster.router.MockInvoker; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; -import java.util.List; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; public class ScriptRouterTest { private URL SCRIPT_URL = URL.valueOf("script://javascript?type=javascript"); - @BeforeAll + @BeforeClass public static void setUpBeforeClass() throws Exception { } - @BeforeEach + @Before public void setUp() throws Exception { } @@ -56,7 +55,7 @@ public void testRouteReturnAll() { invokers.add(new MockInvoker()); invokers.add(new MockInvoker()); List> filteredInvokers = router.route(invokers, invokers.get(0).getUrl(), new RpcInvocation()); - Assertions.assertEquals(invokers, filteredInvokers); + Assert.assertEquals(invokers, filteredInvokers); } @Test @@ -79,53 +78,53 @@ public void testRoutePickInvokers() { invokers.add(invoker2); invokers.add(invoker3); List> filteredInvokers = router.route(invokers, invokers.get(0).getUrl(), new RpcInvocation()); - Assertions.assertEquals(2, filteredInvokers.size()); - Assertions.assertEquals(invoker2, filteredInvokers.get(0)); - Assertions.assertEquals(invoker3, filteredInvokers.get(1)); + Assert.assertEquals(2, filteredInvokers.size()); + Assert.assertEquals(invoker2, filteredInvokers.get(0)); + Assert.assertEquals(invoker3, filteredInvokers.get(1)); } @Test public void testRouteHostFilter() { - List> invokers = new ArrayList>(); - MockInvoker invoker1 = new MockInvoker(URL.valueOf("dubbo://10.134.108.1:20880/com.dubbo.HelloService")); - MockInvoker invoker2 = new MockInvoker(URL.valueOf("dubbo://10.134.108.2:20880/com.dubbo.HelloService")); - MockInvoker invoker3 = new MockInvoker(URL.valueOf("dubbo://10.134.108.3:20880/com.dubbo.HelloService")); + List> invokers = new ArrayList>(); + MockInvoker invoker1 = new MockInvoker(URL.valueOf("dubbo://10.134.108.1:20880/com.dubbo.HelloService")); + MockInvoker invoker2 = new MockInvoker(URL.valueOf("dubbo://10.134.108.2:20880/com.dubbo.HelloService")); + MockInvoker invoker3 = new MockInvoker(URL.valueOf("dubbo://10.134.108.3:20880/com.dubbo.HelloService")); invokers.add(invoker1); invokers.add(invoker2); invokers.add(invoker3); - - String script = "function route(invokers, invocation, context){ " + - " var result = new java.util.ArrayList(invokers.size()); " + - " var targetHost = new java.util.ArrayList(); " + - " targetHost.add(\"10.134.108.2\"); " + - " for (var i = 0; i < invokers.length; i++) { " + - " if(targetHost.contains(invokers[i].getUrl().getHost())){ " + - " result.add(invokers[i]); " + - " } " + - " } " + - " return result; " + - "} " + - "route(invokers, invocation, context) "; - + + String script = "function route(invokers, invocation, context){ " + + " var result = new java.util.ArrayList(invokers.size()); " + + " var targetHost = new java.util.ArrayList(); " + + " targetHost.add(\"10.134.108.2\"); " + + " for (var i = 0; i < invokers.length; i++) { " + + " if(targetHost.contains(invokers[i].getUrl().getHost())){ " + + " result.add(invokers[i]); " + + " } " + + " } " + + " return result; " + + "} " + + "route(invokers, invocation, context) "; + Router router = new ScriptRouterFactory().getRouter(getRouteUrl(script)); List> routeResult = router.route(invokers, invokers.get(0).getUrl(), new RpcInvocation()); - Assertions.assertEquals(1, routeResult.size()); - Assertions.assertEquals(invoker2, routeResult.get(0)); + Assert.assertEquals(1, routeResult.size()); + Assert.assertEquals(invoker2,routeResult.get(0)); } - + @Test public void testRoute_throwException() { - List> invokers = new ArrayList>(); - MockInvoker invoker1 = new MockInvoker(URL.valueOf("dubbo://10.134.108.1:20880/com.dubbo.HelloService")); - MockInvoker invoker2 = new MockInvoker(URL.valueOf("dubbo://10.134.108.2:20880/com.dubbo.HelloService")); - MockInvoker invoker3 = new MockInvoker(URL.valueOf("dubbo://10.134.108.3:20880/com.dubbo.HelloService")); + List> invokers = new ArrayList>(); + MockInvoker invoker1 = new MockInvoker(URL.valueOf("dubbo://10.134.108.1:20880/com.dubbo.HelloService")); + MockInvoker invoker2 = new MockInvoker(URL.valueOf("dubbo://10.134.108.2:20880/com.dubbo.HelloService")); + MockInvoker invoker3 = new MockInvoker(URL.valueOf("dubbo://10.134.108.3:20880/com.dubbo.HelloService")); invokers.add(invoker1); invokers.add(invoker2); invokers.add(invoker3); - + String script = "/"; Router router = new ScriptRouterFactory().getRouter(getRouteUrl(script)); List> routeResult = router.route(invokers, invokers.get(0).getUrl(), new RpcInvocation()); - Assertions.assertEquals(3, routeResult.size()); + Assert.assertEquals(3, routeResult.size()); } } \ No newline at end of file diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvokerTest.java index 130cdb5dd81..76bfc0d7ae5 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvokerTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvokerTest.java @@ -35,11 +35,11 @@ import org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance; import org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; import org.mockito.Mockito; import java.util.ArrayList; @@ -72,17 +72,17 @@ public class AbstractClusterInvokerTest { Invoker mockedInvoker1; - @BeforeAll + @BeforeClass public static void setUpBeforeClass() throws Exception { } - @AfterEach + @After public void teardown() throws Exception { RpcContext.getContext().clearAttachments(); } @SuppressWarnings({"unchecked"}) - @BeforeEach + @Before public void setUp() throws Exception { invocation.setMethodName("sayHello"); @@ -148,7 +148,7 @@ public void testBindingAttachment() { // setup attachment RpcContext.getContext().setAttachment(attachKey, attachValue); Map attachments = RpcContext.getContext().getAttachments(); - Assertions.assertTrue( attachments != null && attachments.size() == 1,"set attachment failed!"); + Assert.assertTrue("set attachment failed!", attachments != null && attachments.size() == 1); cluster = new AbstractClusterInvoker(dic) { @Override @@ -156,7 +156,7 @@ protected Result doInvoke(Invocation invocation, List invokers, LoadBalance load throws RpcException { // attachment will be bind to invocation String value = invocation.getAttachment(attachKey); - Assertions.assertTrue(value != null && value.equals(attachValue),"binding attachment failed!"); + Assert.assertTrue("binding attachment failed!", value != null && value.equals(attachValue)); return null; } }; @@ -168,16 +168,16 @@ protected Result doInvoke(Invocation invocation, List invokers, LoadBalance load @Test public void testSelect_Invokersize0() throws Exception { LoadBalance l = cluster.initLoadBalance(invokers, invocation); - Assertions.assertNotNull(l,"cluster.initLoadBalance returns null!"); + Assert.assertNotNull("cluster.initLoadBalance returns null!", l); { Invoker invoker = cluster.select(l, null, null, null); - Assertions.assertEquals(null, invoker); + Assert.assertEquals(null, invoker); } { invokers.clear(); selectedInvokers.clear(); Invoker invoker = cluster.select(l, null, invokers, null); - Assertions.assertEquals(null, invoker); + Assert.assertEquals(null, invoker); } } @@ -186,9 +186,9 @@ public void testSelect_Invokersize1() throws Exception { invokers.clear(); invokers.add(invoker1); LoadBalance l = cluster.initLoadBalance(invokers, invocation); - Assertions.assertNotNull(l,"cluster.initLoadBalance returns null!"); + Assert.assertNotNull("cluster.initLoadBalance returns null!", l); Invoker invoker = cluster.select(l, null, invokers, null); - Assertions.assertEquals(invoker1, invoker); + Assert.assertEquals(invoker1, invoker); } @Test @@ -197,18 +197,18 @@ public void testSelect_Invokersize2AndselectNotNull() throws Exception { invokers.add(invoker2); invokers.add(invoker4); LoadBalance l = cluster.initLoadBalance(invokers, invocation); - Assertions.assertNotNull(l,"cluster.initLoadBalance returns null!"); + Assert.assertNotNull("cluster.initLoadBalance returns null!", l); { selectedInvokers.clear(); selectedInvokers.add(invoker4); Invoker invoker = cluster.select(l, invocation, invokers, selectedInvokers); - Assertions.assertEquals(invoker2, invoker); + Assert.assertEquals(invoker2, invoker); } { selectedInvokers.clear(); selectedInvokers.add(invoker2); Invoker invoker = cluster.select(l, invocation, invokers, selectedInvokers); - Assertions.assertEquals(invoker4, invoker); + Assert.assertEquals(invoker4, invoker); } } @@ -228,8 +228,8 @@ public void testCloseAvailablecheck() { initlistsize5(); Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers); - Assertions.assertEquals(false, sinvoker.isAvailable()); - Assertions.assertEquals(invoker1, sinvoker); + Assert.assertEquals(false, sinvoker.isAvailable()); + Assert.assertEquals(invoker1, sinvoker); } @@ -246,7 +246,7 @@ public void testDonotSelectAgainAndNoCheckAvailable() { selectedInvokers.add(invoker4); selectedInvokers.add(invoker5); Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers); - Assertions.assertSame(invoker1, sinvoker); + Assert.assertSame(invoker1, sinvoker); } { //Boundary condition test . @@ -256,7 +256,7 @@ public void testDonotSelectAgainAndNoCheckAvailable() { selectedInvokers.add(invoker4); selectedInvokers.add(invoker5); Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers); - Assertions.assertSame(invoker2, sinvoker); + Assert.assertSame(invoker2, sinvoker); } { //Boundary condition test . @@ -266,7 +266,7 @@ public void testDonotSelectAgainAndNoCheckAvailable() { selectedInvokers.add(invoker4); selectedInvokers.add(invoker5); Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers); - Assertions.assertSame(invoker3, sinvoker); + Assert.assertSame(invoker3, sinvoker); } { //Boundary condition test . @@ -276,7 +276,7 @@ public void testDonotSelectAgainAndNoCheckAvailable() { selectedInvokers.add(invoker3); selectedInvokers.add(invoker4); Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers); - Assertions.assertSame(invoker5, sinvoker); + Assert.assertSame(invoker5, sinvoker); } { //Boundary condition test . @@ -287,7 +287,7 @@ public void testDonotSelectAgainAndNoCheckAvailable() { selectedInvokers.add(invoker4); selectedInvokers.add(invoker5); Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers); - Assertions.assertTrue(invokers.contains(sinvoker)); + Assert.assertTrue(invokers.contains(sinvoker)); } } @@ -305,7 +305,7 @@ public void testSelectAgainAndCheckAvailable() { selectedInvokers.add(invoker3); selectedInvokers.add(invoker5); Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers); - Assertions.assertTrue(sinvoker == invoker4); + Assert.assertTrue(sinvoker == invoker4); } { //Boundary condition test . @@ -315,14 +315,14 @@ public void testSelectAgainAndCheckAvailable() { selectedInvokers.add(invoker4); selectedInvokers.add(invoker5); Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers); - Assertions.assertTrue(sinvoker == invoker2 || sinvoker == invoker4); + Assert.assertTrue(sinvoker == invoker2 || sinvoker == invoker4); } { //Boundary condition test . for (int i = 0; i < 100; i++) { selectedInvokers.clear(); Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers); - Assertions.assertTrue(sinvoker == invoker2 || sinvoker == invoker4); + Assert.assertTrue(sinvoker == invoker2 || sinvoker == invoker4); } } { @@ -333,7 +333,7 @@ public void testSelectAgainAndCheckAvailable() { selectedInvokers.add(invoker3); selectedInvokers.add(invoker5); Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers); - Assertions.assertTrue(sinvoker == invoker2 || sinvoker == invoker4); + Assert.assertTrue(sinvoker == invoker2 || sinvoker == invoker4); } } { @@ -346,7 +346,7 @@ public void testSelectAgainAndCheckAvailable() { selectedInvokers.add(invoker4); selectedInvokers.add(invoker5); Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers); - Assertions.assertTrue(sinvoker == invoker2 || sinvoker == invoker4); + Assert.assertTrue(sinvoker == invoker2 || sinvoker == invoker4); } } } @@ -357,12 +357,12 @@ public void testSelect_multiInvokers(String lbname) throws Exception { int min = 1000, max = 5000; Double d = (Math.random() * (max - min + 1) + min); int runs = d.intValue(); - Assertions.assertTrue(runs > min); + Assert.assertTrue(runs > min); LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(lbname); initlistsize5(); for (int i = 0; i < runs; i++) { Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers); - Assertions.assertEquals(true, sinvoker.isAvailable()); + Assert.assertEquals(true, sinvoker.isAvailable()); Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5); } @@ -370,7 +370,7 @@ public void testSelect_multiInvokers(String lbname) throws Exception { selectedInvokers.clear(); selectedInvokers.add(invoker1); Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers); - Assertions.assertEquals(true, sinvoker.isAvailable()); + Assert.assertEquals(true, sinvoker.isAvailable()); Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5); } @@ -378,7 +378,7 @@ public void testSelect_multiInvokers(String lbname) throws Exception { selectedInvokers.clear(); selectedInvokers.add(invoker2); Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers); - Assertions.assertEquals(true, sinvoker.isAvailable()); + Assert.assertEquals(true, sinvoker.isAvailable()); Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5); } @@ -387,7 +387,7 @@ public void testSelect_multiInvokers(String lbname) throws Exception { selectedInvokers.add(invoker2); selectedInvokers.add(invoker4); Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers); - Assertions.assertEquals(true, sinvoker.isAvailable()); + Assert.assertEquals(true, sinvoker.isAvailable()); Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5); } @@ -397,7 +397,7 @@ public void testSelect_multiInvokers(String lbname) throws Exception { selectedInvokers.add(invoker3); selectedInvokers.add(invoker5); Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers); - Assertions.assertEquals(true, sinvoker.isAvailable()); + Assert.assertEquals(true, sinvoker.isAvailable()); Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5); } @@ -408,7 +408,7 @@ public void testSelect_multiInvokers(String lbname) throws Exception { selectedInvokers.add(invoker2); selectedInvokers.add(invoker3); Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers); - Assertions.assertEquals(true, sinvoker.isAvailable()); + Assert.assertEquals(true, sinvoker.isAvailable()); Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5); } @@ -438,10 +438,10 @@ public void testSelectBalance() { Long count = entry.getValue().get(); // System.out.println(count); if (entry.getKey().isAvailable()) - Assertions.assertTrue(count > runs / invokers.size(),"count should > avg"); + Assert.assertTrue("count should > avg", count > runs / invokers.size()); } - Assertions.assertEquals(runs, counter.get(invoker2).get() + counter.get(invoker4).get()); + Assert.assertEquals(runs, counter.get(invoker2).get() + counter.get(invoker4).get()); ; } @@ -491,23 +491,23 @@ public void destroy() { FailoverClusterInvoker failoverClusterInvoker = new FailoverClusterInvoker(directory); try { failoverClusterInvoker.invoke(new RpcInvocation("sayHello", new Class[0], new Object[0])); - Assertions.fail(); + Assert.fail(); } catch (RpcException e) { - Assertions.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode()); + Assert.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode()); } ForkingClusterInvoker forkingClusterInvoker = new ForkingClusterInvoker(directory); try { forkingClusterInvoker.invoke(new RpcInvocation("sayHello", new Class[0], new Object[0])); - Assertions.fail(); + Assert.fail(); } catch (RpcException e) { - Assertions.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode()); + Assert.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode()); } FailfastClusterInvoker failfastClusterInvoker = new FailfastClusterInvoker(directory); try { failfastClusterInvoker.invoke(new RpcInvocation("sayHello", new Class[0], new Object[0])); - Assertions.fail(); + Assert.fail(); } catch (RpcException e) { - Assertions.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode()); + Assert.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode()); } } @@ -525,10 +525,10 @@ public void testMockedInvokerSelect() { mockedInvocation.setMethodName("sayHello"); mockedInvocation.setAttachment(Constants.INVOCATION_NEED_MOCK, "true"); List> mockedInvokers = dic.list(mockedInvocation); - Assertions.assertEquals(1, mockedInvokers.size()); + Assert.assertEquals(1, mockedInvokers.size()); List> invokers = dic.list(invocation); - Assertions.assertEquals(5, invokers.size()); + Assert.assertEquals(5, invokers.size()); } public static interface IHelloService { diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ClusterUtilsTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ClusterUtilsTest.java index c010a157dcc..65cc1b2ce5d 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ClusterUtilsTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ClusterUtilsTest.java @@ -19,9 +19,8 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.URLBuilder; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; public class ClusterUtilsTest { @@ -32,8 +31,7 @@ public void testMergeUrl() throws Exception { .setUsername("username") .setPassword("password"); - providerURL = URLBuilder.from(providerURL) - .addParameter(Constants.GROUP_KEY, "dubbo") + providerURL = providerURL.addParameter(Constants.GROUP_KEY, "dubbo") .addParameter(Constants.VERSION_KEY, "1.2.3") .addParameter(Constants.DUBBO_VERSION_KEY, "2.3.7") .addParameter(Constants.THREADPOOL_KEY, "fixed") @@ -47,38 +45,36 @@ public void testMergeUrl() throws Exception { .addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.CORE_THREADS_KEY, Integer.MAX_VALUE) .addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.QUEUES_KEY, Integer.MAX_VALUE) .addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.ALIVE_KEY, Integer.MAX_VALUE) - .addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.THREAD_NAME_KEY, "test") - .build(); + .addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.THREAD_NAME_KEY, "test"); - URL consumerURL = new URLBuilder(Constants.DUBBO_PROTOCOL, "localhost", 55555) - .addParameter(Constants.PID_KEY, "1234") - .addParameter(Constants.THREADPOOL_KEY, "foo") - .build(); + URL consumerURL = URL.valueOf("dubbo://localhost:55555"); + consumerURL = consumerURL.addParameter(Constants.PID_KEY, "1234"); + consumerURL = consumerURL.addParameter(Constants.THREADPOOL_KEY, "foo"); URL url = ClusterUtils.mergeUrl(providerURL, consumerURL.getParameters()); - Assertions.assertFalse(url.hasParameter(Constants.THREADS_KEY)); - Assertions.assertFalse(url.hasParameter(Constants.DEFAULT_KEY_PREFIX + Constants.THREADS_KEY)); + Assert.assertFalse(url.hasParameter(Constants.THREADS_KEY)); + Assert.assertFalse(url.hasParameter(Constants.DEFAULT_KEY_PREFIX + Constants.THREADS_KEY)); - Assertions.assertFalse(url.hasParameter(Constants.DEFAULT_KEY_PREFIX + Constants.THREADPOOL_KEY)); + Assert.assertFalse(url.hasParameter(Constants.DEFAULT_KEY_PREFIX + Constants.THREADPOOL_KEY)); - Assertions.assertFalse(url.hasParameter(Constants.CORE_THREADS_KEY)); - Assertions.assertFalse(url.hasParameter(Constants.DEFAULT_KEY_PREFIX + Constants.CORE_THREADS_KEY)); + Assert.assertFalse(url.hasParameter(Constants.CORE_THREADS_KEY)); + Assert.assertFalse(url.hasParameter(Constants.DEFAULT_KEY_PREFIX + Constants.CORE_THREADS_KEY)); - Assertions.assertFalse(url.hasParameter(Constants.QUEUES_KEY)); - Assertions.assertFalse(url.hasParameter(Constants.DEFAULT_KEY_PREFIX + Constants.QUEUES_KEY)); + Assert.assertFalse(url.hasParameter(Constants.QUEUES_KEY)); + Assert.assertFalse(url.hasParameter(Constants.DEFAULT_KEY_PREFIX + Constants.QUEUES_KEY)); - Assertions.assertFalse(url.hasParameter(Constants.ALIVE_KEY)); - Assertions.assertFalse(url.hasParameter(Constants.DEFAULT_KEY_PREFIX + Constants.ALIVE_KEY)); + Assert.assertFalse(url.hasParameter(Constants.ALIVE_KEY)); + Assert.assertFalse(url.hasParameter(Constants.DEFAULT_KEY_PREFIX + Constants.ALIVE_KEY)); - Assertions.assertFalse(url.hasParameter(Constants.THREAD_NAME_KEY)); - Assertions.assertFalse(url.hasParameter(Constants.DEFAULT_KEY_PREFIX + Constants.THREAD_NAME_KEY)); + Assert.assertFalse(url.hasParameter(Constants.THREAD_NAME_KEY)); + Assert.assertFalse(url.hasParameter(Constants.DEFAULT_KEY_PREFIX + Constants.THREAD_NAME_KEY)); - Assertions.assertEquals(url.getPath(), "path"); - Assertions.assertEquals(url.getUsername(), "username"); - Assertions.assertEquals(url.getPassword(), "password"); - Assertions.assertEquals(url.getParameter(Constants.PID_KEY), "1234"); - Assertions.assertEquals(url.getParameter(Constants.THREADPOOL_KEY), "foo"); + Assert.assertEquals(url.getPath(), "path"); + Assert.assertEquals(url.getUsername(), "username"); + Assert.assertEquals(url.getPassword(), "password"); + Assert.assertEquals(url.getParameter(Constants.PID_KEY), "1234"); + Assert.assertEquals(url.getParameter(Constants.THREADPOOL_KEY), "foo"); } } diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailSafeClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailSafeClusterInvokerTest.java index 413d9b9dc8a..fa9ac1a8686 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailSafeClusterInvokerTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailSafeClusterInvokerTest.java @@ -26,14 +26,14 @@ import org.apache.dubbo.rpc.cluster.Directory; import org.apache.dubbo.rpc.cluster.filter.DemoService; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import java.util.ArrayList; import java.util.List; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertTrue; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -54,7 +54,7 @@ public class FailSafeClusterInvokerTest { * @throws java.lang.Exception */ - @BeforeEach + @Before public void setUp() throws Exception { dic = mock(Directory.class); @@ -85,7 +85,7 @@ public void testInvokeExceptoin() { resetInvokerToException(); FailsafeClusterInvoker invoker = new FailsafeClusterInvoker(dic); invoker.invoke(invocation); - Assertions.assertNull(RpcContext.getContext().getInvoker()); + Assert.assertNull(RpcContext.getContext().getInvoker()); } @Test() @@ -95,7 +95,7 @@ public void testInvokeNoExceptoin() { FailsafeClusterInvoker invoker = new FailsafeClusterInvoker(dic); Result ret = invoker.invoke(invocation); - Assertions.assertSame(result, ret); + Assert.assertSame(result, ret); } @Test() diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailbackClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailbackClusterInvokerTest.java index ad96a66830d..581d52ee9df 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailbackClusterInvokerTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailbackClusterInvokerTest.java @@ -1,4 +1,3 @@ - /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -28,30 +27,27 @@ import org.apache.dubbo.rpc.cluster.Directory; import org.apache.log4j.Level; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.MethodOrderer; -import org.junit.jupiter.api.Order; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestMethodOrder; +import org.junit.Assert; +import org.junit.Before; +import org.junit.FixMethodOrder; +import org.junit.Test; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; -/** - * FailbackClusterInvokerTest - *

- * add annotation @TestMethodOrder, the testARetryFailed Method must to first execution + /** + * FailbackClusterInvokerTest + * + * add annotation @FixMethodOrder, the testARetryFailed Method must to first execution */ -@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +@SuppressWarnings("unchecked") +@FixMethodOrder(org.junit.runners.MethodSorters.NAME_ASCENDING) public class FailbackClusterInvokerTest { List> invokers = new ArrayList>(); @@ -65,7 +61,7 @@ public class FailbackClusterInvokerTest { * @throws java.lang.Exception */ - @BeforeEach + @Before public void setUp() throws Exception { dic = mock(Directory.class); @@ -78,15 +74,6 @@ public void setUp() throws Exception { invokers.add(invoker); } - @AfterEach - public void tearDown() { - - dic = null; - invocation = new RpcInvocation(); - invokers.clear(); - } - - private void resetInvokerToException() { given(invoker.invoke(invocation)).willThrow(new RuntimeException()); given(invoker.getUrl()).willReturn(url); @@ -100,18 +87,16 @@ private void resetInvokerToNoException() { } @Test - @Order(1) public void testInvokeException() { resetInvokerToException(); FailbackClusterInvoker invoker = new FailbackClusterInvoker( dic); invoker.invoke(invocation); - Assertions.assertNull(RpcContext.getContext().getInvoker()); + Assert.assertNull(RpcContext.getContext().getInvoker()); DubboAppender.clear(); } - @Test - @Order(2) + @Test() public void testInvokeNoException() { resetInvokerToNoException(); @@ -119,11 +104,10 @@ public void testInvokeNoException() { FailbackClusterInvoker invoker = new FailbackClusterInvoker( dic); Result ret = invoker.invoke(invocation); - Assertions.assertSame(result, ret); + Assert.assertSame(result, ret); } - @Test - @Order(3) + @Test() public void testNoInvoke() { dic = mock(Directory.class); @@ -146,9 +130,7 @@ public void testNoInvoke() { LogUtil.stop(); } - @Disabled - @Test - @Order(4) + @Test() public void testARetryFailed() throws Exception { //Test retries and @@ -161,15 +143,15 @@ public void testARetryFailed() throws Exception { invoker.invoke(invocation); invoker.invoke(invocation); invoker.invoke(invocation); - Assertions.assertNull(RpcContext.getContext().getInvoker()); + Assert.assertNull(RpcContext.getContext().getInvoker()); // invoker.retryFailed();// when retry the invoker which get from failed map already is not the mocked invoker,so //Ensure that the main thread is online CountDownLatch countDown = new CountDownLatch(1); countDown.await(15000L, TimeUnit.MILLISECONDS); LogUtil.stop(); - Assertions.assertEquals(4, LogUtil.findMessage(Level.ERROR, "Failed retry to invoke method"), "must have four error message "); - Assertions.assertEquals(2, LogUtil.findMessage(Level.ERROR, "Failed retry times exceed threshold"), "must have two error message "); - Assertions.assertEquals(1, LogUtil.findMessage(Level.ERROR, "Failback background works error"), "must have one error message "); + Assert.assertEquals("must have four error message ", 4, LogUtil.findMessage(Level.ERROR, "Failed retry to invoke method")); + Assert.assertEquals("must have two error message ", 2, LogUtil.findMessage(Level.ERROR, "Failed retry times exceed threshold")); + Assert.assertEquals("must have one error message ", 1, LogUtil.findMessage(Level.ERROR, "Failback background works error")); // it can be invoke successfully } } \ No newline at end of file diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvokerTest.java index d8dd653f34e..41dc79ca2ee 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvokerTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvokerTest.java @@ -25,20 +25,22 @@ import org.apache.dubbo.rpc.RpcResult; import org.apache.dubbo.rpc.cluster.Directory; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import java.util.ArrayList; import java.util.List; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.fail; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; /** * FailfastClusterInvokerTest + * */ @SuppressWarnings("unchecked") public class FailfastClusterInvokerTest { @@ -53,7 +55,7 @@ public class FailfastClusterInvokerTest { * @throws java.lang.Exception */ - @BeforeEach + @Before public void setUp() throws Exception { dic = mock(Directory.class); @@ -79,14 +81,12 @@ private void resetInvoker1ToNoException() { given(invoker1.getInterface()).willReturn(FailfastClusterInvokerTest.class); } - @Test + @Test(expected = RpcException.class) public void testInvokeExceptoin() { - Assertions.assertThrows(RpcException.class, () -> { - resetInvoker1ToException(); - FailfastClusterInvoker invoker = new FailfastClusterInvoker(dic); - invoker.invoke(invocation); - Assertions.assertSame(invoker1, RpcContext.getContext().getInvoker()); - }); + resetInvoker1ToException(); + FailfastClusterInvoker invoker = new FailfastClusterInvoker(dic); + invoker.invoke(invocation); + Assert.assertSame(invoker1, RpcContext.getContext().getInvoker()); } @Test() @@ -96,7 +96,7 @@ public void testInvokeNoExceptoin() { FailfastClusterInvoker invoker = new FailfastClusterInvoker(dic); Result ret = invoker.invoke(invocation); - Assertions.assertSame(result, ret); + Assert.assertSame(result, ret); } @Test() diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailoverClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailoverClusterInvokerTest.java index ff29a6183a9..136ba2a349c 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailoverClusterInvokerTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailoverClusterInvokerTest.java @@ -27,18 +27,18 @@ import org.apache.dubbo.rpc.cluster.directory.StaticDirectory; import org.apache.dubbo.rpc.protocol.AbstractInvoker; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Before; +import org.junit.Test; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -61,7 +61,7 @@ public class FailoverClusterInvokerTest { * @throws java.lang.Exception */ - @BeforeEach + @Before public void setUp() throws Exception { dic = mock(Directory.class); diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvokerTest.java index b3d343a3bbe..5b44c838c1b 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvokerTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvokerTest.java @@ -25,15 +25,15 @@ import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.rpc.cluster.Directory; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import java.util.ArrayList; import java.util.List; import java.util.Map; -import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.Assert.assertFalse; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -52,7 +52,7 @@ public class ForkingClusterInvokerTest { private Directory dic; private Result result = new RpcResult(); - @BeforeEach + @Before public void setUp() throws Exception { dic = mock(Directory.class); @@ -111,9 +111,9 @@ public void testInvokeException() { try { invoker.invoke(invocation); - Assertions.fail(); + Assert.fail(); } catch (RpcException expected) { - Assertions.assertTrue(expected.getMessage().contains("Failed to forking invoke provider")); + Assert.assertTrue(expected.getMessage().contains("Failed to forking invoke provider")); assertFalse(expected.getCause() instanceof RpcException); } } @@ -130,16 +130,16 @@ public void testClearRpcContext() { RpcContext.getContext().setAttachment(attachKey, attachValue); Map attachments = RpcContext.getContext().getAttachments(); - Assertions.assertTrue(attachments != null && attachments.size() == 1, "set attachment failed!"); + Assert.assertTrue("set attachment failed!", attachments != null && attachments.size() == 1); try { invoker.invoke(invocation); - Assertions.fail(); + Assert.fail(); } catch (RpcException expected) { - Assertions.assertTrue(expected.getMessage().contains("Failed to forking invoke provider"), "Succeeded to forking invoke provider !"); + Assert.assertTrue("Succeeded to forking invoke provider !", expected.getMessage().contains("Failed to forking invoke provider")); assertFalse(expected.getCause() instanceof RpcException); } Map afterInvoke = RpcContext.getContext().getAttachments(); - Assertions.assertTrue(afterInvoke != null && afterInvoke.size() == 0, "clear attachment failed!"); + Assert.assertTrue("clear attachment failed!", afterInvoke != null && afterInvoke.size() == 0); } @Test() @@ -150,7 +150,7 @@ public void testInvokeNoException() { ForkingClusterInvoker invoker = new ForkingClusterInvoker( dic); Result ret = invoker.invoke(invocation); - Assertions.assertSame(result, ret); + Assert.assertSame(result, ret); } } \ No newline at end of file diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvokerTest.java index 8a4089c9049..ba0b617b88c 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvokerTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvokerTest.java @@ -24,9 +24,9 @@ import org.apache.dubbo.rpc.RpcResult; import org.apache.dubbo.rpc.cluster.Directory; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; @@ -38,7 +38,7 @@ import java.util.List; import java.util.Map; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -86,7 +86,7 @@ static void merge(Map> first, Map> sec } } - @BeforeEach + @Before public void setUp() throws Exception { directory = mock(Directory.class); @@ -155,7 +155,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl // invoke Result result = mergeableClusterInvoker.invoke(invocation); - Assertions.assertTrue(result.getValue() instanceof Menu); + Assert.assertTrue(result.getValue() instanceof Menu); Menu menu = (Menu) result.getValue(); Map> expected = new HashMap>(); merge(expected, firstMenuMap); @@ -219,7 +219,7 @@ public void testAddMenu() throws Exception { mergeableClusterInvoker = new MergeableClusterInvoker(directory); Result result = mergeableClusterInvoker.invoke(invocation); - Assertions.assertNull(result.getValue()); + Assert.assertNull(result.getValue()); } diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockClusterInvokerTest.java index 618e9705d65..f305f919933 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockClusterInvokerTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockClusterInvokerTest.java @@ -31,9 +31,9 @@ import org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker; import org.apache.dubbo.rpc.support.MockProtocol; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; @@ -43,7 +43,7 @@ public class MockClusterInvokerTest { List> invokers = new ArrayList>(); - @BeforeEach + @Before public void beforeMethod() { invokers.clear(); } @@ -67,13 +67,13 @@ public void testMockInvokerInvoke_normal() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); - Assertions.assertEquals("something", ret.getValue()); + Assert.assertEquals("something", ret.getValue()); // If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); - Assertions.assertEquals(null, ret.getValue()); + Assert.assertEquals(null, ret.getValue()); } /** @@ -95,19 +95,19 @@ public void testMockInvokerInvoke_failmock() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); - Assertions.assertEquals("aa", ret.getValue()); + Assert.assertEquals("aa", ret.getValue()); // If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); - Assertions.assertEquals(null, ret.getValue()); + Assert.assertEquals(null, ret.getValue()); // If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); - Assertions.assertEquals(null, ret.getValue()); + Assert.assertEquals(null, ret.getValue()); } @@ -131,19 +131,19 @@ public void testMockInvokerInvoke_forcemock() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); - Assertions.assertEquals("aa", ret.getValue()); + Assert.assertEquals("aa", ret.getValue()); // If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); - Assertions.assertEquals(null, ret.getValue()); + Assert.assertEquals(null, ret.getValue()); // If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); - Assertions.assertEquals(null, ret.getValue()); + Assert.assertEquals(null, ret.getValue()); } @Test @@ -162,7 +162,7 @@ public void testMockInvokerInvoke_forcemock_defaultreturn() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); Result ret = cluster.invoke(invocation); - Assertions.assertEquals(null, ret.getValue()); + Assert.assertEquals(null, ret.getValue()); } /** @@ -178,25 +178,25 @@ public void testMockInvokerFromOverride_Invoke_Fock_someMethods() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); - Assertions.assertEquals("something", ret.getValue()); + Assert.assertEquals("something", ret.getValue()); // If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); - Assertions.assertEquals("y", ret.getValue()); + Assert.assertEquals("y", ret.getValue()); // If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("getSomething3"); ret = cluster.invoke(invocation); - Assertions.assertEquals("something3", ret.getValue()); + Assert.assertEquals("something3", ret.getValue()); // If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); - Assertions.assertEquals(null, ret.getValue()); + Assert.assertEquals(null, ret.getValue()); } /** @@ -213,20 +213,20 @@ public void testMockInvokerFromOverride_Invoke_Fock_WithOutDefault() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); - Assertions.assertEquals("x", ret.getValue()); + Assert.assertEquals("x", ret.getValue()); // If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); - Assertions.assertEquals("y", ret.getValue()); + Assert.assertEquals("y", ret.getValue()); // If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("getSomething3"); try { ret = cluster.invoke(invocation); - Assertions.fail(); + Assert.fail(); } catch (RpcException e) { } @@ -247,25 +247,25 @@ public void testMockInvokerFromOverride_Invoke_Fock_WithDefault() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); - Assertions.assertEquals("x", ret.getValue()); + Assert.assertEquals("x", ret.getValue()); // If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); - Assertions.assertEquals("y", ret.getValue()); + Assert.assertEquals("y", ret.getValue()); // If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("getSomething3"); ret = cluster.invoke(invocation); - Assertions.assertEquals(null, ret.getValue()); + Assert.assertEquals(null, ret.getValue()); // If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); - Assertions.assertEquals(null, ret.getValue()); + Assert.assertEquals(null, ret.getValue()); } /** @@ -283,25 +283,25 @@ public void testMockInvokerFromOverride_Invoke_Fock_WithFailDefault() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); - Assertions.assertEquals("x", ret.getValue()); + Assert.assertEquals("x", ret.getValue()); // If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); - Assertions.assertEquals("y", ret.getValue()); + Assert.assertEquals("y", ret.getValue()); // If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("getSomething3"); ret = cluster.invoke(invocation); - Assertions.assertEquals("z", ret.getValue()); + Assert.assertEquals("z", ret.getValue()); //If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); - Assertions.assertEquals("z", ret.getValue()); + Assert.assertEquals("z", ret.getValue()); } /** @@ -319,25 +319,25 @@ public void testMockInvokerFromOverride_Invoke_Fock_WithForceDefault() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); - Assertions.assertEquals("x", ret.getValue()); + Assert.assertEquals("x", ret.getValue()); //If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); - Assertions.assertEquals("y", ret.getValue()); + Assert.assertEquals("y", ret.getValue()); //If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("getSomething3"); ret = cluster.invoke(invocation); - Assertions.assertEquals("z", ret.getValue()); + Assert.assertEquals("z", ret.getValue()); //If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); - Assertions.assertEquals("z", ret.getValue()); + Assert.assertEquals("z", ret.getValue()); } /** @@ -353,19 +353,19 @@ public void testMockInvokerFromOverride_Invoke_Fock_Default() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); - Assertions.assertEquals("x", ret.getValue()); + Assert.assertEquals("x", ret.getValue()); //If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); - Assertions.assertEquals("x", ret.getValue()); + Assert.assertEquals("x", ret.getValue()); //If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); - Assertions.assertEquals("x", ret.getValue()); + Assert.assertEquals("x", ret.getValue()); } /** @@ -381,14 +381,14 @@ public void testMockInvokerFromOverride_Invoke_checkCompatible_return() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); - Assertions.assertEquals("x", ret.getValue()); + Assert.assertEquals("x", ret.getValue()); //If no mock was configured, return null directly invocation = new RpcInvocation(); invocation.setMethodName("getSomething3"); try { ret = cluster.invoke(invocation); - Assertions.fail("fail invoke"); + Assert.fail("fail invoke"); } catch (RpcException e) { } @@ -407,7 +407,7 @@ public void testMockInvokerFromOverride_Invoke_checkCompatible_ImplMock() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); - Assertions.assertEquals("somethingmock", ret.getValue()); + Assert.assertEquals("somethingmock", ret.getValue()); } /** @@ -423,7 +423,7 @@ public void testMockInvokerFromOverride_Invoke_checkCompatible_ImplMock2() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); - Assertions.assertEquals("somethingmock", ret.getValue()); + Assert.assertEquals("somethingmock", ret.getValue()); } /** @@ -438,7 +438,7 @@ public void testMockInvokerFromOverride_Invoke_checkCompatible_ImplMock3() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); - Assertions.assertEquals("somethingmock", ret.getValue()); + Assert.assertEquals("somethingmock", ret.getValue()); } @Test @@ -451,8 +451,8 @@ public void testMockInvokerFromOverride_Invoke_check_String() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); - Assertions.assertTrue(ret.getValue() instanceof String, "result type must be String but was : " + ret.getValue().getClass()); - Assertions.assertEquals("1688", (String) ret.getValue()); + Assert.assertTrue("result type must be String but was : " + ret.getValue().getClass(), ret.getValue() instanceof String); + Assert.assertEquals("1688", (String) ret.getValue()); } @Test @@ -465,8 +465,8 @@ public void testMockInvokerFromOverride_Invoke_check_int() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getInt1"); Result ret = cluster.invoke(invocation); - Assertions.assertTrue(ret.getValue() instanceof Integer, "result type must be integer but was : " + ret.getValue().getClass()); - Assertions.assertEquals(new Integer(1688), (Integer) ret.getValue()); + Assert.assertTrue("result type must be integer but was : " + ret.getValue().getClass(), ret.getValue() instanceof Integer); + Assert.assertEquals(new Integer(1688), (Integer) ret.getValue()); } @Test @@ -479,8 +479,8 @@ public void testMockInvokerFromOverride_Invoke_check_boolean() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getBoolean1"); Result ret = cluster.invoke(invocation); - Assertions.assertTrue(ret.getValue() instanceof Boolean, "result type must be Boolean but was : " + ret.getValue().getClass()); - Assertions.assertEquals(true, Boolean.parseBoolean(ret.getValue().toString())); + Assert.assertTrue("result type must be Boolean but was : " + ret.getValue().getClass(), ret.getValue() instanceof Boolean); + Assert.assertEquals(true, Boolean.parseBoolean(ret.getValue().toString())); } @Test @@ -493,7 +493,7 @@ public void testMockInvokerFromOverride_Invoke_check_Boolean() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getBoolean2"); Result ret = cluster.invoke(invocation); - Assertions.assertEquals(true, Boolean.parseBoolean(ret.getValue().toString())); + Assert.assertEquals(true, Boolean.parseBoolean(ret.getValue().toString())); } @SuppressWarnings("unchecked") @@ -507,7 +507,7 @@ public void testMockInvokerFromOverride_Invoke_check_ListString_empty() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getListString"); Result ret = cluster.invoke(invocation); - Assertions.assertEquals(0, ((List) ret.getValue()).size()); + Assert.assertEquals(0, ((List) ret.getValue()).size()); } @SuppressWarnings("unchecked") @@ -522,8 +522,8 @@ public void testMockInvokerFromOverride_Invoke_check_ListString() { invocation.setMethodName("getListString"); Result ret = cluster.invoke(invocation); List rl = (List) ret.getValue(); - Assertions.assertEquals(2, rl.size()); - Assertions.assertEquals("hi", rl.get(0)); + Assert.assertEquals(2, rl.size()); + Assert.assertEquals("hi", rl.get(0)); } @SuppressWarnings("unchecked") @@ -537,7 +537,7 @@ public void testMockInvokerFromOverride_Invoke_check_ListPojo_empty() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getUsers"); Result ret = cluster.invoke(invocation); - Assertions.assertEquals(0, ((List) ret.getValue()).size()); + Assert.assertEquals(0, ((List) ret.getValue()).size()); } @SuppressWarnings("unchecked") @@ -553,8 +553,8 @@ public void testMockInvokerFromOverride_Invoke_check_ListPojo() { Result ret = cluster.invoke(invocation); List rl = (List) ret.getValue(); System.out.println(rl); - Assertions.assertEquals(2, rl.size()); - Assertions.assertEquals("hi1", ((User) rl.get(0)).getName()); + Assert.assertEquals(2, rl.size()); + Assert.assertEquals("hi1", ((User) rl.get(0)).getName()); } @Test @@ -583,9 +583,9 @@ public void testMockInvokerFromOverride_Invoke_force_throw() { invocation.setMethodName("getBoolean2"); try { cluster.invoke(invocation); - Assertions.fail(); + Assert.fail(); } catch (RpcException e) { - Assertions.assertFalse(e.isBiz(), "not custem exception"); + Assert.assertFalse("not custem exception", e.isBiz()); } } @@ -600,7 +600,7 @@ public void testMockInvokerFromOverride_Invoke_force_throwCustemException() thro invocation.setMethodName("getBoolean2"); try { cluster.invoke(invocation).recreate(); - Assertions.fail(); + Assert.fail(); } catch (MyMockException e) { } @@ -617,9 +617,9 @@ public void testMockInvokerFromOverride_Invoke_force_throwCustemExceptionNotFoun invocation.setMethodName("getBoolean2"); try { cluster.invoke(invocation); - Assertions.fail(); + Assert.fail(); } catch (Exception e) { - Assertions.assertTrue(e.getCause() instanceof IllegalStateException); + Assert.assertTrue(e.getCause() instanceof IllegalStateException); } } @@ -634,9 +634,9 @@ public void testMockInvokerFromOverride_Invoke_mock_false() { invocation.setMethodName("getBoolean2"); try { cluster.invoke(invocation); - Assertions.fail(); + Assert.fail(); } catch (RpcException e) { - Assertions.assertTrue(e.isTimeout()); + Assert.assertTrue(e.isTimeout()); } } diff --git a/dubbo-cluster/src/test/resources/log4j.xml b/dubbo-cluster/src/test/resources/log4j.xml index eb0c9f105de..df9d68a0fab 100644 --- a/dubbo-cluster/src/test/resources/log4j.xml +++ b/dubbo-cluster/src/test/resources/log4j.xml @@ -18,7 +18,6 @@ - diff --git a/dubbo-common/pom.xml b/dubbo-common/pom.xml index bd3347f5240..22a13222fe2 100644 --- a/dubbo-common/pom.xml +++ b/dubbo-common/pom.xml @@ -20,7 +20,7 @@ org.apache.dubbo dubbo-parent - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-common jar diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/Constants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/Constants.java index c6724b84e74..9b4e25926e7 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/Constants.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/Constants.java @@ -97,10 +97,7 @@ public class Constants { public static final String DEFAULT_PROXY = "javassist"; - /** - * 8M - */ - public static final int DEFAULT_PAYLOAD = 8 * 1024 * 1024; + public static final int DEFAULT_PAYLOAD = 8 * 1024 * 1024; // 8M public static final String DEFAULT_CLUSTER = "failover"; @@ -146,13 +143,7 @@ public class Constants { public static final int DEFAULT_ALIVE = 60 * 1000; - /** - * By default, a consumer JVM instance and a provider JVM instance share a long TCP connection (except when connections are set), - * which can set the number of long TCP connections shared to avoid the bottleneck of sharing a single long TCP connection. - */ - public static final String DEFAULT_SHARE_CONNECTIONS = "1"; - - public static final String SHARE_CONNECTIONS_KEY = "shareconnections"; + public static final int DEFAULT_CONNECTIONS = 0; public static final int DEFAULT_ACCEPTS = 0; @@ -164,20 +155,15 @@ public class Constants { public static final int DEFAULT_CONNECT_TIMEOUT = 3000; - /** - * public static final int DEFAULT_REGISTRY_CONNECT_TIMEOUT = 5000; - */ +// public static final int DEFAULT_REGISTRY_CONNECT_TIMEOUT = 5000; + public static final int DEFAULT_RETRIES = 2; public static final int DEFAULT_FAILBACK_TASKS = 100; public static final int DEFAULT_FAILBACK_TIMES = 3; - public static final int MAX_PROXY_COUNT = 65535; - - /** - * default buffer size is 8k. - */ + // default buffer size is 8k. public static final int DEFAULT_BUFFER_SIZE = 8 * 1024; public static final Integer DEFAULT_METADATA_REPORT_RETRY_TIMES = 100; @@ -200,9 +186,7 @@ public class Constants { public static final String LOADBALANCE_KEY = "loadbalance"; - /** - * key for router type, for e.g., "script"/"file", corresponding to ScriptRouterFactory.NAME, FileRouterFactory.NAME - */ + // key for router type, for e.g., "script"/"file", corresponding to ScriptRouterFactory.NAME, FileRouterFactory.NAME public static final String ROUTER_KEY = "router"; public static final String CLUSTER_KEY = "cluster"; @@ -247,7 +231,7 @@ public class Constants { public static final String PROTOCOL_KEY = "protocol"; - public static final String DUBBO_PROTOCOL = DUBBO; + public static final String DOBBO_PROTOCOL = DUBBO; public static final String ZOOKEEPER_PROTOCOL = "zookeeper"; @@ -314,9 +298,9 @@ public class Constants { public static final long LEAST_HEARTBEAT_DURATION = 1000; /** - * ticks per wheel. + * ticks per wheel. Currently only contains two tasks, so 16 locations are enough */ - public static final int TICKS_PER_WHEEL = 128; + public static final int TICKS_PER_WHEEL = 16; public static final String HEARTBEAT_TIMEOUT_KEY = "heartbeat.timeout"; @@ -753,12 +737,6 @@ public class Constants { public static final String HOST_KEY = "host"; - public static final String PORT_KEY = "port"; - - public static final String USERNAME_KEY = "username"; - - public static final String PASSWORD_KEY = "password"; - public static final String ADDRESS_KEY = "address"; public static final String RETRY_TIMES_KEY = "retry.times"; @@ -772,9 +750,7 @@ public class Constants { public static final String CONFIG_VERSION_KEY = "configVersion"; public static final String COMPATIBLE_CONFIG_KEY = "compatible_config"; - /** - * package version in the manifest - */ + // package version in the manifest public static final String RELEASE_KEY = "release"; public static final String OVERRIDE_PROVIDERS_KEY = "providerAddresses"; @@ -837,25 +813,8 @@ public class Constants { */ public static final String PROXY_CLASS_REF = "refClass"; - public static final String ETCD3_NOTIFY_MAXTHREADS_KEYS = "etcd3.notify.maxthreads"; - - public static final int DEFAULT_ETCD3_NOTIFY_THREADS = DEFAULT_IO_THREADS; - - public static final String DEFAULT_ETCD3_NOTIFY_QUEUES_KEY = "etcd3.notify.queues"; - - public static final int DEFAULT_GRPC_QUEUES = 300_0000; - - /** - * metrics + /* + * private Constants(){ } */ - public static final String DUBBO_PROVIDER = "dubbo.provider"; - public static final String DUBBO_CONSUMER = "dubbo.consumer"; - public static final String DUBBO_PROVIDER_METHOD = "dubbo.provider.method"; - public static final String DUBBO_CONSUMER_METHOD = "dubbo.consumer.method"; - public static final String SERVICE = "service"; - public static final String METHOD = "method"; - public static final String DUBBO_GROUP = "dubbo"; - public static final String METRICS_KEY = "metrics"; - } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/Parameters.java b/dubbo-common/src/main/java/org/apache/dubbo/common/Parameters.java index 333270c9de3..529d3feb910 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/Parameters.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/Parameters.java @@ -96,13 +96,13 @@ public String getDecodedParameter(String key, String defaultValue) { public String getParameter(String key) { String value = parameters.get(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { value = parameters.get(Constants.HIDE_KEY_PREFIX + key); } - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { value = parameters.get(Constants.DEFAULT_KEY_PREFIX + key); } - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { value = parameters.get(Constants.HIDE_KEY_PREFIX + Constants.DEFAULT_KEY_PREFIX + key); } return value; @@ -110,7 +110,7 @@ public String getParameter(String key) { public String getParameter(String key, String defaultValue) { String value = getParameter(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } return value; @@ -118,7 +118,7 @@ public String getParameter(String key, String defaultValue) { public int getIntParameter(String key) { String value = getParameter(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return 0; } return Integer.parseInt(value); @@ -126,7 +126,7 @@ public int getIntParameter(String key) { public int getIntParameter(String key, int defaultValue) { String value = getParameter(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } return Integer.parseInt(value); @@ -137,7 +137,7 @@ public int getPositiveIntParameter(String key, int defaultValue) { throw new IllegalArgumentException("defaultValue <= 0"); } String value = getParameter(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } int i = Integer.parseInt(value); @@ -149,7 +149,7 @@ public int getPositiveIntParameter(String key, int defaultValue) { public boolean getBooleanParameter(String key) { String value = getParameter(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return false; } return Boolean.parseBoolean(value); @@ -157,23 +157,23 @@ public boolean getBooleanParameter(String key) { public boolean getBooleanParameter(String key, boolean defaultValue) { String value = getParameter(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } return Boolean.parseBoolean(value); } - public boolean hasParameter(String key) { + public boolean hasParamter(String key) { String value = getParameter(key); return value != null && value.length() > 0; } public String getMethodParameter(String method, String key) { String value = parameters.get(method + "." + key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { value = parameters.get(Constants.HIDE_KEY_PREFIX + method + "." + key); } - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return getParameter(key); } return value; @@ -181,7 +181,7 @@ public String getMethodParameter(String method, String key) { public String getMethodParameter(String method, String key, String defaultValue) { String value = getMethodParameter(method, key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } return value; @@ -189,7 +189,7 @@ public String getMethodParameter(String method, String key, String defaultValue) public int getMethodIntParameter(String method, String key) { String value = getMethodParameter(method, key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return 0; } return Integer.parseInt(value); @@ -197,7 +197,7 @@ public int getMethodIntParameter(String method, String key) { public int getMethodIntParameter(String method, String key, int defaultValue) { String value = getMethodParameter(method, key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } return Integer.parseInt(value); @@ -208,7 +208,7 @@ public int getMethodPositiveIntParameter(String method, String key, int defaultV throw new IllegalArgumentException("defaultValue <= 0"); } String value = getMethodParameter(method, key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } int i = Integer.parseInt(value); @@ -220,7 +220,7 @@ public int getMethodPositiveIntParameter(String method, String key, int defaultV public boolean getMethodBooleanParameter(String method, String key) { String value = getMethodParameter(method, key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return false; } return Boolean.parseBoolean(value); @@ -228,13 +228,13 @@ public boolean getMethodBooleanParameter(String method, String key) { public boolean getMethodBooleanParameter(String method, String key, boolean defaultValue) { String value = getMethodParameter(method, key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } return Boolean.parseBoolean(value); } - public boolean hasMethodParameter(String method, String key) { + public boolean hasMethodParamter(String method, String key) { String value = getMethodParameter(method, key); return value != null && value.length() > 0; } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/Resetable.java b/dubbo-common/src/main/java/org/apache/dubbo/common/Resetable.java index 021cfbf257a..f2eb8da4ed3 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/Resetable.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/Resetable.java @@ -1,31 +1,31 @@ -/* - * 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 org.apache.dubbo.common; - -/** - * Resetable. - */ -public interface Resetable { - - /** - * reset. - * - * @param url - */ - void reset(URL url); - +/* + * 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 org.apache.dubbo.common; + +/** + * Resetable. + */ +public interface Resetable { + + /** + * reset. + * + * @param url + */ + void reset(URL url); + } \ No newline at end of file diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java index 35a3b7bd1f9..eb096f7e713 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java @@ -18,7 +18,6 @@ import org.apache.dubbo.common.config.Configuration; import org.apache.dubbo.common.config.InmemoryConfiguration; -import org.apache.dubbo.common.utils.ArrayUtils; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.common.utils.StringUtils; @@ -71,8 +70,7 @@ * @see java.net.URL * @see java.net.URI */ -public /*final**/ -class URL implements Serializable { +public /**final**/ class URL implements Serializable { private static final long serialVersionUID = -1985165475234910535L; @@ -151,8 +149,8 @@ public URL(String protocol, String username, String password, String host, int p } public URL(String protocol, String username, String password, String host, int port, String path, Map parameters) { - if (StringUtils.isEmpty(username) - && StringUtils.isNotEmpty(password)) { + if ((username == null || username.length() == 0) + && password != null && password.length() > 0) { throw new IllegalArgumentException("Invalid url, password without username!"); } this.protocol = protocol; @@ -166,9 +164,9 @@ public URL(String protocol, String username, String password, String host, int p } this.path = path; if (parameters == null) { - parameters = new HashMap<>(); + parameters = new HashMap(); } else { - parameters = new HashMap<>(parameters); + parameters = new HashMap(parameters); } this.parameters = Collections.unmodifiableMap(parameters); } @@ -191,10 +189,10 @@ public static URL valueOf(String url) { int port = 0; String path = null; Map parameters = null; - int i = url.indexOf("?"); // separator between body and parameters + int i = url.indexOf("?"); // seperator between body and parameters if (i >= 0) { - String[] parts = url.substring(i + 1).split("&"); - parameters = new HashMap<>(); + String[] parts = url.substring(i + 1).split("\\&"); + parameters = new HashMap(); for (String part : parts) { part = part.trim(); if (part.length() > 0) { @@ -265,7 +263,7 @@ public static URL valueOf(String url, String... reserveParams) { if (reserveParams == null || reserveParams.length == 0) { return result; } - Map newMap = new HashMap<>(reserveParams.length); + Map newMap = new HashMap(reserveParams.length); Map oldMap = result.getParameters(); for (String reserveParam : reserveParams) { String tmp = oldMap.get(reserveParam); @@ -277,7 +275,7 @@ public static URL valueOf(String url, String... reserveParams) { } public static URL valueOf(URL url, String[] reserveParams, String[] reserveParamPrefixs) { - Map newMap = new HashMap<>(); + Map newMap = new HashMap(); Map oldMap = url.getParameters(); if (reserveParamPrefixs != null && reserveParamPrefixs.length != 0) { for (Map.Entry entry : oldMap.entrySet()) { @@ -302,7 +300,7 @@ public static URL valueOf(URL url, String[] reserveParams, String[] reserveParam } public static String encode(String value) { - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return ""; } try { @@ -313,7 +311,7 @@ public static String encode(String value) { } public static String decode(String value) { - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return ""; } try { @@ -348,8 +346,8 @@ public URL setPassword(String password) { } public String getAuthority() { - if (StringUtils.isEmpty(username) - && StringUtils.isEmpty(password)) { + if ((username == null || username.length() == 0) + && (password == null || password.length() == 0)) { return null; } return (username == null ? "" : username) @@ -415,7 +413,7 @@ public String getBackupAddress() { public String getBackupAddress(int defaultPort) { StringBuilder address = new StringBuilder(appendDefaultPort(getAddress(), defaultPort)); String[] backups = getParameter(Constants.BACKUP_KEY, new String[0]); - if (ArrayUtils.isNotEmpty(backups)) { + if (backups != null && backups.length > 0) { for (String backup : backups) { address.append(","); address.append(appendDefaultPort(backup, defaultPort)); @@ -425,7 +423,7 @@ public String getBackupAddress(int defaultPort) { } public List getBackupUrls() { - List urls = new ArrayList<>(); + List urls = new ArrayList(); urls.add(this); String[] backups = getParameter(Constants.BACKUP_KEY, new String[0]); if (backups != null && backups.length > 0) { @@ -436,8 +434,9 @@ public List getBackupUrls() { return urls; } - static String appendDefaultPort(String address, int defaultPort) { - if (address != null && address.length() > 0 && defaultPort > 0) { + private String appendDefaultPort(String address, int defaultPort) { + if (address != null && address.length() > 0 + && defaultPort > 0) { int i = address.indexOf(':'); if (i < 0) { return address + ":" + defaultPort; @@ -477,7 +476,7 @@ public String getParameterAndDecoded(String key, String defaultValue) { public String getParameter(String key) { String value = parameters.get(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { value = parameters.get(Constants.DEFAULT_KEY_PREFIX + key); } return value; @@ -485,7 +484,7 @@ public String getParameter(String key) { public String getParameter(String key, String defaultValue) { String value = getParameter(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } return value; @@ -493,7 +492,7 @@ public String getParameter(String key, String defaultValue) { public String[] getParameter(String key, String[] defaultValue) { String value = getParameter(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } return Constants.COMMA_SPLIT_PATTERN.split(value); @@ -510,14 +509,14 @@ public List getParameter(String key, List defaultValue) { private Map getNumbers() { if (numbers == null) { // concurrent initialization is tolerant - numbers = new ConcurrentHashMap<>(); + numbers = new ConcurrentHashMap(); } return numbers; } private Map getUrls() { if (urls == null) { // concurrent initialization is tolerant - urls = new ConcurrentHashMap<>(); + urls = new ConcurrentHashMap(); } return urls; } @@ -528,7 +527,7 @@ public URL getUrlParameter(String key) { return u; } String value = getParameterAndDecoded(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return null; } u = URL.valueOf(value); @@ -542,7 +541,7 @@ public double getParameter(String key, double defaultValue) { return n.doubleValue(); } String value = getParameter(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } double d = Double.parseDouble(value); @@ -556,7 +555,7 @@ public float getParameter(String key, float defaultValue) { return n.floatValue(); } String value = getParameter(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } float f = Float.parseFloat(value); @@ -570,7 +569,7 @@ public long getParameter(String key, long defaultValue) { return n.longValue(); } String value = getParameter(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } long l = Long.parseLong(value); @@ -584,7 +583,7 @@ public int getParameter(String key, int defaultValue) { return n.intValue(); } String value = getParameter(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } int i = Integer.parseInt(value); @@ -598,7 +597,7 @@ public short getParameter(String key, short defaultValue) { return n.shortValue(); } String value = getParameter(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } short s = Short.parseShort(value); @@ -612,7 +611,7 @@ public byte getParameter(String key, byte defaultValue) { return n.byteValue(); } String value = getParameter(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } byte b = Byte.parseByte(value); @@ -688,7 +687,7 @@ public byte getPositiveParameter(String key, byte defaultValue) { public char getParameter(String key, char defaultValue) { String value = getParameter(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } return value.charAt(0); @@ -696,7 +695,7 @@ public char getParameter(String key, char defaultValue) { public boolean getParameter(String key, boolean defaultValue) { String value = getParameter(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } return Boolean.parseBoolean(value); @@ -717,7 +716,7 @@ public String getMethodParameterAndDecoded(String method, String key, String def public String getMethodParameter(String method, String key) { String value = parameters.get(method + "." + key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return getParameter(key); } return value; @@ -725,7 +724,7 @@ public String getMethodParameter(String method, String key) { public String getMethodParameter(String method, String key, String defaultValue) { String value = getMethodParameter(method, key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } return value; @@ -735,10 +734,10 @@ public double getMethodParameter(String method, String key, double defaultValue) String methodKey = method + "." + key; Number n = getNumbers().get(methodKey); if (n != null) { - return n.doubleValue(); + return n.intValue(); } String value = getMethodParameter(method, key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } double d = Double.parseDouble(value); @@ -750,10 +749,10 @@ public float getMethodParameter(String method, String key, float defaultValue) { String methodKey = method + "." + key; Number n = getNumbers().get(methodKey); if (n != null) { - return n.floatValue(); + return n.intValue(); } String value = getMethodParameter(method, key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } float f = Float.parseFloat(value); @@ -765,10 +764,10 @@ public long getMethodParameter(String method, String key, long defaultValue) { String methodKey = method + "." + key; Number n = getNumbers().get(methodKey); if (n != null) { - return n.longValue(); + return n.intValue(); } String value = getMethodParameter(method, key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } long l = Long.parseLong(value); @@ -783,7 +782,7 @@ public int getMethodParameter(String method, String key, int defaultValue) { return n.intValue(); } String value = getMethodParameter(method, key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } int i = Integer.parseInt(value); @@ -798,7 +797,7 @@ public short getMethodParameter(String method, String key, short defaultValue) { return n.shortValue(); } String value = getMethodParameter(method, key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } short s = Short.parseShort(value); @@ -813,7 +812,7 @@ public byte getMethodParameter(String method, String key, byte defaultValue) { return n.byteValue(); } String value = getMethodParameter(method, key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } byte b = Byte.parseByte(value); @@ -889,7 +888,7 @@ public byte getMethodPositiveParameter(String method, String key, byte defaultVa public char getMethodParameter(String method, String key, char defaultValue) { String value = getMethodParameter(method, key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } return value.charAt(0); @@ -897,7 +896,7 @@ public char getMethodParameter(String method, String key, char defaultValue) { public boolean getMethodParameter(String method, String key, boolean defaultValue) { String value = getMethodParameter(method, key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } return Boolean.parseBoolean(value); @@ -935,7 +934,7 @@ public boolean isAnyHost() { } public URL addParameterAndEncoded(String key, String value) { - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return this; } return addParameter(key, encode(value)); @@ -995,8 +994,8 @@ public URL addParameter(String key, CharSequence value) { } public URL addParameter(String key, String value) { - if (StringUtils.isEmpty(key) - || StringUtils.isEmpty(value)) { + if (key == null || key.length() == 0 + || value == null || value.length() == 0) { return this; } // if value doesn't change, return immediately @@ -1004,20 +1003,20 @@ public URL addParameter(String key, String value) { return this; } - Map map = new HashMap<>(getParameters()); + Map map = new HashMap(getParameters()); map.put(key, value); return new URL(protocol, username, password, host, port, path, map); } public URL addParameterIfAbsent(String key, String value) { - if (StringUtils.isEmpty(key) - || StringUtils.isEmpty(value)) { + if (key == null || key.length() == 0 + || value == null || value.length() == 0) { return this; } if (hasParameter(key)) { return this; } - Map map = new HashMap<>(getParameters()); + Map map = new HashMap(getParameters()); map.put(key, value); return new URL(protocol, username, password, host, port, path, map); } @@ -1029,7 +1028,7 @@ public URL addParameterIfAbsent(String key, String value) { * @return A new URL */ public URL addParameters(Map parameters) { - if (CollectionUtils.isEmptyMap(parameters)) { + if (parameters == null || parameters.size() == 0) { return this; } @@ -1053,16 +1052,16 @@ public URL addParameters(Map parameters) { return this; } - Map map = new HashMap<>(getParameters()); + Map map = new HashMap(getParameters()); map.putAll(parameters); return new URL(protocol, username, password, host, port, path, map); } public URL addParametersIfAbsent(Map parameters) { - if (CollectionUtils.isEmptyMap(parameters)) { + if (parameters == null || parameters.size() == 0) { return this; } - Map map = new HashMap<>(parameters); + Map map = new HashMap(parameters); map.putAll(getParameters()); return new URL(protocol, username, password, host, port, path, map); } @@ -1074,7 +1073,7 @@ public URL addParameters(String... pairs) { if (pairs.length % 2 != 0) { throw new IllegalArgumentException("Map pairs can not be odd number."); } - Map map = new HashMap<>(); + Map map = new HashMap(); int len = pairs.length / 2; for (int i = 0; i < len; i++) { map.put(pairs[2 * i], pairs[2 * i + 1]); @@ -1083,21 +1082,21 @@ public URL addParameters(String... pairs) { } public URL addParameterString(String query) { - if (StringUtils.isEmpty(query)) { + if (query == null || query.length() == 0) { return this; } return addParameters(StringUtils.parseQueryString(query)); } public URL removeParameter(String key) { - if (StringUtils.isEmpty(key)) { + if (key == null || key.length() == 0) { return this; } return removeParameters(key); } public URL removeParameters(Collection keys) { - if (CollectionUtils.isEmpty(keys)) { + if (keys == null || keys.isEmpty()) { return this; } return removeParameters(keys.toArray(new String[0])); @@ -1107,7 +1106,7 @@ public URL removeParameters(String... keys) { if (keys == null || keys.length == 0) { return this; } - Map map = new HashMap<>(getParameters()); + Map map = new HashMap(getParameters()); for (String key : keys) { map.remove(key); } @@ -1118,50 +1117,50 @@ public URL removeParameters(String... keys) { } public URL clearParameters() { - return new URL(protocol, username, password, host, port, path, new HashMap<>()); + return new URL(protocol, username, password, host, port, path, new HashMap()); } public String getRawParameter(String key) { - if (Constants.PROTOCOL_KEY.equals(key)) { + if ("protocol".equals(key)) { return protocol; } - if (Constants.USERNAME_KEY.equals(key)) { + if ("username".equals(key)) { return username; } - if (Constants.PASSWORD_KEY.equals(key)) { + if ("password".equals(key)) { return password; } - if (Constants.HOST_KEY.equals(key)) { + if ("host".equals(key)) { return host; } - if (Constants.PORT_KEY.equals(key)) { + if ("port".equals(key)) { return String.valueOf(port); } - if (Constants.PATH_KEY.equals(key)) { + if ("path".equals(key)) { return path; } return getParameter(key); } public Map toMap() { - Map map = new HashMap<>(parameters); + Map map = new HashMap(parameters); if (protocol != null) { - map.put(Constants.PROTOCOL_KEY, protocol); + map.put("protocol", protocol); } if (username != null) { - map.put(Constants.USERNAME_KEY, username); + map.put("username", username); } if (password != null) { - map.put(Constants.USERNAME_KEY, password); + map.put("password", password); } if (host != null) { - map.put(Constants.HOST_KEY, host); + map.put("host", host); } if (port > 0) { - map.put(Constants.PORT_KEY, String.valueOf(port)); + map.put("port", String.valueOf(port)); } if (path != null) { - map.put(Constants.PATH_KEY, path); + map.put("path", path); } return map; } @@ -1214,10 +1213,10 @@ public String toParameterString(String... parameters) { } private void buildParameters(StringBuilder buf, boolean concat, String[] parameters) { - if (CollectionUtils.isNotEmptyMap(getParameters())) { - List includes = (ArrayUtils.isEmpty(parameters) ? null : Arrays.asList(parameters)); + if (getParameters() != null && getParameters().size() > 0) { + List includes = (parameters == null || parameters.length == 0 ? null : Arrays.asList(parameters)); boolean first = true; - for (Map.Entry entry : new TreeMap<>(getParameters()).entrySet()) { + for (Map.Entry entry : new TreeMap(getParameters()).entrySet()) { if (entry.getKey() != null && entry.getKey().length() > 0 && (includes == null || includes.contains(entry.getKey()))) { if (first) { @@ -1242,11 +1241,11 @@ private String buildString(boolean appendUser, boolean appendParameter, String.. private String buildString(boolean appendUser, boolean appendParameter, boolean useIP, boolean useService, String... parameters) { StringBuilder buf = new StringBuilder(); - if (StringUtils.isNotEmpty(protocol)) { + if (protocol != null && protocol.length() > 0) { buf.append(protocol); buf.append("://"); } - if (appendUser && StringUtils.isNotEmpty(username)) { + if (appendUser && username != null && username.length() > 0) { buf.append(username); if (password != null && password.length() > 0) { buf.append(":"); @@ -1297,7 +1296,7 @@ public InetSocketAddress toInetSocketAddress() { } /** - * The format is '{group}*{interfaceName}:{version}' + * The format is '{group}/{interfaceName/path}*{version}' * * @return */ @@ -1307,36 +1306,18 @@ public String getEncodedServiceKey() { return serviceKey; } - /** - * The format of return value is '{group}/{interfaceName}:{version}' - * @return - */ public String getServiceKey() { String inf = getServiceInterface(); if (inf == null) { return null; } - return buildKey(inf, getParameter(Constants.GROUP_KEY), getParameter(Constants.VERSION_KEY)); - } - - /** - * The format of return value is '{group}/{path/interfaceName}:{version}' - * @return - */ - public String getPathKey() { - String inf = StringUtils.isNotEmpty(path) ? path : getServiceInterface(); - if (inf == null) { - return null; - } - return buildKey(inf, getParameter(Constants.GROUP_KEY), getParameter(Constants.VERSION_KEY)); - } - - public static String buildKey(String path, String group, String version) { StringBuilder buf = new StringBuilder(); + String group = getParameter(Constants.GROUP_KEY); if (group != null && group.length() > 0) { buf.append(group).append("/"); } - buf.append(path); + buf.append(inf); + String version = getParameter(Constants.VERSION_KEY); if (version != null && version.length() > 0) { buf.append(":").append(version); } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java deleted file mode 100644 index c938737811d..00000000000 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java +++ /dev/null @@ -1,350 +0,0 @@ -/* - * 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 org.apache.dubbo.common; - -import org.apache.dubbo.common.utils.CollectionUtils; -import org.apache.dubbo.common.utils.StringUtils; - -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; - -public final class URLBuilder { - private String protocol; - - private String username; - - private String password; - - // by default, host to registry - private String host; - - // by default, port to registry - private int port; - - private String path; - - private Map parameters; - - public URLBuilder() { - protocol = null; - username = null; - password = null; - host = null; - port = 0; - path = null; - parameters = new HashMap<>(); - } - - public URLBuilder(String protocol, String host, int port) { - this(protocol, null, null, host, port, null, null); - } - - public URLBuilder(String protocol, String host, int port, String[] pairs) { - this(protocol, null, null, host, port, null, CollectionUtils.toStringMap(pairs)); - } - - public URLBuilder(String protocol, String host, int port, Map parameters) { - this(protocol, null, null, host, port, null, parameters); - } - - public URLBuilder(String protocol, String host, int port, String path) { - this(protocol, null, null, host, port, path, null); - } - - public URLBuilder(String protocol, String host, int port, String path, String... pairs) { - this(protocol, null, null, host, port, path, CollectionUtils.toStringMap(pairs)); - } - - public URLBuilder(String protocol, String host, int port, String path, Map parameters) { - this(protocol, null, null, host, port, path, parameters); - } - - public URLBuilder(String protocol, String username, String password, String host, int port, String path, Map parameters) { - this.protocol = protocol; - this.username = username; - this.password = password; - this.host = host; - this.port = port; - this.path = path; - this.parameters = parameters != null ? parameters : new HashMap<>(); - } - - public static URLBuilder from(URL url) { - String protocol = url.getProtocol(); - String username = url.getUsername(); - String password = url.getPassword(); - String host = url.getHost(); - int port = url.getPort(); - String path = url.getPath(); - Map parameters = new HashMap<>(url.getParameters()); - return new URLBuilder( - protocol, - username, - password, - host, - port, - path, - parameters); - } - - public URL build() { - if (StringUtils.isEmpty(username) && StringUtils.isNotEmpty(password)) { - throw new IllegalArgumentException("Invalid url, password without username!"); - } - port = port < 0 ? 0 : port; - // trim the leading "/" - int firstNonSlash = 0; - if (path != null) { - while (firstNonSlash < path.length() && path.charAt(firstNonSlash) == '/') { - firstNonSlash++; - } - if (firstNonSlash >= path.length()) { - path = ""; - } else if (firstNonSlash > 0) { - path = path.substring(firstNonSlash); - } - } - return new URL(protocol, username, password, host, port, path, parameters); - } - - - public URLBuilder setProtocol(String protocol) { - this.protocol = protocol; - return this; - } - - public URLBuilder setUsername(String username) { - this.username = username; - return this; - } - - public URLBuilder setPassword(String password) { - this.password = password; - return this; - } - - public URLBuilder setHost(String host) { - this.host = host; - return this; - } - - public URLBuilder setPort(int port) { - this.port = port; - return this; - } - - public URLBuilder setAddress(String address) { - int i = address.lastIndexOf(':'); - String host; - int port = this.port; - if (i >= 0) { - host = address.substring(0, i); - port = Integer.parseInt(address.substring(i + 1)); - } else { - host = address; - } - this.host = host; - this.port = port; - return this; - } - - public URLBuilder setPath(String path) { - this.path = path; - return this; - } - - public URLBuilder addParameterAndEncoded(String key, String value) { - if (StringUtils.isEmpty(value)) { - return this; - } - return addParameter(key, URL.encode(value)); - } - - public URLBuilder addParameter(String key, boolean value) { - return addParameter(key, String.valueOf(value)); - } - - public URLBuilder addParameter(String key, char value) { - return addParameter(key, String.valueOf(value)); - } - - public URLBuilder addParameter(String key, byte value) { - return addParameter(key, String.valueOf(value)); - } - - public URLBuilder addParameter(String key, short value) { - return addParameter(key, String.valueOf(value)); - } - - public URLBuilder addParameter(String key, int value) { - return addParameter(key, String.valueOf(value)); - } - - public URLBuilder addParameter(String key, long value) { - return addParameter(key, String.valueOf(value)); - } - - public URLBuilder addParameter(String key, float value) { - return addParameter(key, String.valueOf(value)); - } - - public URLBuilder addParameter(String key, double value) { - return addParameter(key, String.valueOf(value)); - } - - public URLBuilder addParameter(String key, Enum value) { - if (value == null) { - return this; - } - return addParameter(key, String.valueOf(value)); - } - - public URLBuilder addParameter(String key, Number value) { - if (value == null) { - return this; - } - return addParameter(key, String.valueOf(value)); - } - - public URLBuilder addParameter(String key, CharSequence value) { - if (value == null || value.length() == 0) { - return this; - } - return addParameter(key, String.valueOf(value)); - } - - public URLBuilder addParameter(String key, String value) { - if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) { - return this; - } - // if value doesn't change, return immediately - if (value.equals(parameters.get(key))) { // value != null - return this; - } - - parameters.put(key, value); - return this; - } - - public URLBuilder addParameterIfAbsent(String key, String value) { - if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) { - return this; - } - if (hasParameter(key)) { - return this; - } - parameters.put(key, value); - return this; - } - - public URLBuilder addParameters(Map parameters) { - if (CollectionUtils.isEmptyMap(parameters)) { - return this; - } - - boolean hasAndEqual = true; - for (Map.Entry entry : parameters.entrySet()) { - String oldValue = this.parameters.get(entry.getKey()); - String newValue = entry.getValue(); - if (!Objects.equals(oldValue, newValue)) { - hasAndEqual = false; - break; - } - } - // return immediately if there's no change - if (hasAndEqual) { - return this; - } - - this.parameters.putAll(parameters); - return this; - } - - public URLBuilder addParametersIfAbsent(Map parameters) { - if (CollectionUtils.isEmptyMap(parameters)) { - return this; - } - this.parameters.putAll(parameters); - return this; - } - - public URLBuilder addParameters(String... pairs) { - if (pairs == null || pairs.length == 0) { - return this; - } - if (pairs.length % 2 != 0) { - throw new IllegalArgumentException("Map pairs can not be odd number."); - } - Map map = new HashMap<>(); - int len = pairs.length / 2; - for (int i = 0; i < len; i++) { - map.put(pairs[2 * i], pairs[2 * i + 1]); - } - return addParameters(map); - } - - public URLBuilder addParameterString(String query) { - if (StringUtils.isEmpty(query)) { - return this; - } - return addParameters(StringUtils.parseQueryString(query)); - } - - public URLBuilder removeParameter(String key) { - if (StringUtils.isEmpty(key)) { - return this; - } - return removeParameters(key); - } - - public URLBuilder removeParameters(Collection keys) { - if (CollectionUtils.isEmpty(keys)) { - return this; - } - return removeParameters(keys.toArray(new String[0])); - } - - public URLBuilder removeParameters(String... keys) { - if (keys == null || keys.length == 0) { - return this; - } - for (String key : keys) { - parameters.remove(key); - } - return this; - } - - public URLBuilder clearParameters() { - parameters.clear(); - return this; - } - - public boolean hasParameter(String key) { - String value = getParameter(key); - return value != null && value.length() > 0; - } - - public String getParameter(String key) { - String value = parameters.get(key); - if (StringUtils.isEmpty(value)) { - value = parameters.get(Constants.DEFAULT_KEY_PREFIX + key); - } - return value; - } -} diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/Version.java b/dubbo-common/src/main/java/org/apache/dubbo/common/Version.java index 0ab48a6c4fc..9f69ae81cb0 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/Version.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/Version.java @@ -21,7 +21,6 @@ import org.apache.dubbo.common.utils.ClassHelper; import org.apache.dubbo.common.utils.StringUtils; -import java.io.IOException; import java.net.URL; import java.security.CodeSource; import java.util.Enumeration; @@ -29,16 +28,12 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * Version */ public final class Version { private static final Logger logger = LoggerFactory.getLogger(Version.class); - - private static final Pattern PREFIX_DIGITS_PATTERN = Pattern.compile("^([0-9]*).*"); // Dubbo RPC protocol version, for compatibility, it must not be between 2.0.10 ~ 2.6.2 public static final String DEFAULT_DUBBO_PROTOCOL_VERSION = "2.0.2"; @@ -94,7 +89,7 @@ public static boolean isRelease263OrHigher(String version) { } public static boolean isSupportResponseAttachment(String version) { - if (StringUtils.isEmpty(version)) { + if (version == null || version.length() == 0) { return false; } // for previous dubbo version(2.0.10/020010~2.6.2/020602), this version is the jar's version, so they need to @@ -125,53 +120,75 @@ private static int parseInt(String version) { String[] vArr = version.split("\\."); int len = vArr.length; for (int i = 0; i < len; i++) { - v += Integer.parseInt(getPrefixDigits(vArr[i])) * Math.pow(10, (len - i - 1) * 2); + v += Integer.parseInt(getDigital(vArr[i])) * Math.pow(10, (len - i - 1) * 2); } return v; } - /** - * get prefix digits from given version string - */ - private static String getPrefixDigits(String v) { - Matcher matcher = PREFIX_DIGITS_PATTERN.matcher(v); - if (matcher.find()) { - return matcher.group(1); + private static String getDigital(String v) { + int index = 0; + for (int i = 0; i < v.length(); i++) { + char c = v.charAt(i); + if (Character.isDigit(c)) { + if (i == v.length() - 1) { + index = i + 1; + } else { + index = i; + } + } else { + index = i; + break; + } + } + return v.substring(0, index); + } + + private static boolean hasResource(String path) { + try { + return Version.class.getClassLoader().getResource(path) != null; + } catch (Throwable t) { + return false; } - return ""; } public static String getVersion(Class cls, String defaultVersion) { try { // find version info from MANIFEST.MF first - Package pkg = cls.getPackage(); - String version = null; - if (pkg != null) { - version = pkg.getImplementationVersion(); - if (!StringUtils.isEmpty(version)) { - return version; - } - - version = pkg.getSpecificationVersion(); - if (!StringUtils.isEmpty(version)) { - return version; - } + String version = cls.getPackage().getImplementationVersion(); + if (version == null || version.length() == 0) { + version = cls.getPackage().getSpecificationVersion(); } - - // guess version fro jar file name if nothing's found from MANIFEST.MF - CodeSource codeSource = cls.getProtectionDomain().getCodeSource(); - if (codeSource == null) { - logger.info("No codeSource for class " + cls.getName() + " when getVersion, use default version " + defaultVersion); - return defaultVersion; - } - - String file = codeSource.getLocation().getFile(); - if (!StringUtils.isEmpty(file) && file.endsWith(".jar")) { - version = getFromFile(file); + if (version == null || version.length() == 0) { + // guess version fro jar file name if nothing's found from MANIFEST.MF + CodeSource codeSource = cls.getProtectionDomain().getCodeSource(); + if (codeSource == null) { + logger.info("No codeSource for class " + cls.getName() + " when getVersion, use default version " + defaultVersion); + } else { + String file = codeSource.getLocation().getFile(); + if (file != null && file.length() > 0 && file.endsWith(".jar")) { + file = file.substring(0, file.length() - 4); + int i = file.lastIndexOf('/'); + if (i >= 0) { + file = file.substring(i + 1); + } + i = file.indexOf("-"); + if (i >= 0) { + file = file.substring(i + 1); + } + while (file.length() > 0 && !Character.isDigit(file.charAt(0))) { + i = file.indexOf("-"); + if (i >= 0) { + file = file.substring(i + 1); + } else { + break; + } + } + version = file; + } + } } - // return default version if no version info is found - return StringUtils.isEmpty(version) ? defaultVersion : version; + return version == null || version.length() == 0 ? defaultVersion : version; } catch (Throwable e) { // return default version when any exception is thrown logger.error("return default version, ignore exception " + e.getMessage(), e); @@ -179,37 +196,6 @@ public static String getVersion(Class cls, String defaultVersion) { } } - /** - * get version from file: path/to/group-module-x.y.z.jar, returns x.y.z - */ - private static String getFromFile(String file) { - // remove suffix ".jar": "path/to/group-module-x.y.z" - file = file.substring(0, file.length() - 4); - - // remove path: "group-module-x.y.z" - int i = file.lastIndexOf('/'); - if (i >= 0) { - file = file.substring(i + 1); - } - - // remove group: "module-x.y.z" - i = file.indexOf("-"); - if (i >= 0) { - file = file.substring(i + 1); - } - - // remove module: "x.y.z" - while (file.length() > 0 && !Character.isDigit(file.charAt(0))) { - i = file.indexOf("-"); - if (i >= 0) { - file = file.substring(i + 1); - } else { - break; - } - } - return file; - } - public static void checkDuplicate(Class cls, boolean failOnError) { checkDuplicate(cls.getName().replace('.', '/') + ".class", failOnError); } @@ -221,7 +207,17 @@ public static void checkDuplicate(Class cls) { public static void checkDuplicate(String path, boolean failOnError) { try { // search in caller's classloader - Set files = getResources(path); + Enumeration urls = ClassHelper.getCallerClassLoader(Version.class).getResources(path); + Set files = new HashSet(); + while (urls.hasMoreElements()) { + URL url = urls.nextElement(); + if (url != null) { + String file = url.getFile(); + if (file != null && file.length() > 0) { + files.add(file); + } + } + } // duplicated jar is found if (files.size() > 1) { String error = "Duplicate class " + path + " in " + files.size() + " jar " + files; @@ -236,22 +232,4 @@ public static void checkDuplicate(String path, boolean failOnError) { } } - /** - * search resources in caller's classloader - */ - private static Set getResources(String path) throws IOException { - Enumeration urls = ClassHelper.getCallerClassLoader(Version.class).getResources(path); - Set files = new HashSet(); - while (urls.hasMoreElements()) { - URL url = urls.nextElement(); - if (url != null) { - String file = url.getFile(); - if (file != null && file.length() > 0) { - files.add(file); - } - } - } - return files; - } - } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/ClassGenerator.java b/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/ClassGenerator.java index 40ae8856cfe..c044c7b297a 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/ClassGenerator.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/ClassGenerator.java @@ -16,7 +16,6 @@ */ package org.apache.dubbo.common.bytecode; -import org.apache.dubbo.common.utils.ArrayUtils; import org.apache.dubbo.common.utils.ClassHelper; import org.apache.dubbo.common.utils.ReflectUtils; @@ -30,7 +29,6 @@ import javassist.CtNewMethod; import javassist.LoaderClassPath; import javassist.NotFoundException; -import org.apache.dubbo.common.utils.StringUtils; import java.lang.reflect.Constructor; import java.lang.reflect.Method; @@ -167,7 +165,7 @@ public ClassGenerator addField(String name, int mod, Class type, String def) StringBuilder sb = new StringBuilder(); sb.append(modifier(mod)).append(' ').append(ReflectUtils.getName(type)).append(' '); sb.append(name); - if (StringUtils.isNotEmpty(def)) { + if (def != null && def.length() > 0) { sb.append('='); sb.append(def); } @@ -200,7 +198,7 @@ public ClassGenerator addMethod(String name, int mod, Class rt, Class[] pt sb.append(" arg").append(i); } sb.append(')'); - if (ArrayUtils.isNotEmpty(ets)) { + if (ets != null && ets.length > 0) { sb.append(" throws "); for (int i = 0; i < ets.length; i++) { if (i > 0) { @@ -252,7 +250,7 @@ public ClassGenerator addConstructor(int mod, Class[] pts, Class[] ets, St sb.append(" arg").append(i); } sb.append(')'); - if (ArrayUtils.isNotEmpty(ets)) { + if (ets != null && ets.length > 0) { sb.append(" throws "); for (int i = 0; i < ets.length; i++) { if (i > 0) { diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/Proxy.java b/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/Proxy.java index e72faae2cb1..150e946b690 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/Proxy.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/Proxy.java @@ -16,7 +16,6 @@ */ package org.apache.dubbo.common.bytecode; -import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.utils.ClassHelper; import org.apache.dubbo.common.utils.ReflectUtils; @@ -39,7 +38,12 @@ */ public abstract class Proxy { - public static final InvocationHandler RETURN_NULL_INVOKER = (proxy, method, args) -> null; + public static final InvocationHandler RETURN_NULL_INVOKER = new InvocationHandler() { + @Override + public Object invoke(Object proxy, Method method, Object[] args) { + return null; + } + }; public static final InvocationHandler THROW_UNSUPPORTED_INVOKER = new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) { @@ -73,7 +77,7 @@ public static Proxy getProxy(Class... ics) { * @return Proxy instance. */ public static Proxy getProxy(ClassLoader cl, Class... ics) { - if (ics.length > Constants.MAX_PROXY_COUNT) { + if (ics.length > 65535) { throw new IllegalArgumentException("interface limit exceeded"); } @@ -103,7 +107,11 @@ public static Proxy getProxy(ClassLoader cl, Class... ics) { // get cache by class loader. Map cache; synchronized (ProxyCacheMap) { - cache = ProxyCacheMap.computeIfAbsent(cl, k -> new HashMap<>()); + cache = ProxyCacheMap.get(cl); + if (cache == null) { + cache = new HashMap(); + ProxyCacheMap.put(cl, cache); + } } Proxy proxy = null; @@ -136,8 +144,8 @@ public static Proxy getProxy(ClassLoader cl, Class... ics) { try { ccp = ClassGenerator.newInstance(cl); - Set worked = new HashSet<>(); - List methods = new ArrayList<>(); + Set worked = new HashSet(); + List methods = new ArrayList(); for (int i = 0; i < ics.length; i++) { if (!Modifier.isPublic(ics[i].getModifiers())) { @@ -167,7 +175,7 @@ public static Proxy getProxy(ClassLoader cl, Class... ics) { for (int j = 0; j < pts.length; j++) { code.append(" args[").append(j).append("] = ($w)$").append(j + 1).append(";"); } - code.append(" Object ret = handler.invoke(this, methods[").append(ix).append("], args);"); + code.append(" Object ret = handler.invoke(this, methods[" + ix + "], args);"); if (!Void.TYPE.equals(rt)) { code.append(" return ").append(asArgument(rt, "ret")).append(";"); } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/Wrapper.java b/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/Wrapper.java index 6d3f42bd8fe..76113ba2e7b 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/Wrapper.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/Wrapper.java @@ -137,10 +137,10 @@ private static Wrapper makeWrapper(Class c) { c2.append(name).append(" w; try{ w = ((").append(name).append(")$1); }catch(Throwable e){ throw new IllegalArgumentException(e); }"); c3.append(name).append(" w; try{ w = ((").append(name).append(")$1); }catch(Throwable e){ throw new IllegalArgumentException(e); }"); - Map> pts = new HashMap<>(); // - Map ms = new LinkedHashMap<>(); // - List mns = new ArrayList<>(); // method names. - List dmns = new ArrayList<>(); // declaring method names. + Map> pts = new HashMap>(); // + Map ms = new LinkedHashMap(); // + List mns = new ArrayList(); // method names. + List dmns = new ArrayList(); // declaring method names. // get all public field. for (Field f : c.getFields()) { @@ -160,49 +160,51 @@ private static Wrapper makeWrapper(Class c) { boolean hasMethod = hasMethods(methods); if (hasMethod) { c3.append(" try{"); - for (Method m : methods) { - //ignore Object's method. - if (m.getDeclaringClass() == Object.class) { - continue; - } + } + for (Method m : methods) { + if (m.getDeclaringClass() == Object.class) //ignore Object's method. + { + continue; + } - String mn = m.getName(); - c3.append(" if( \"").append(mn).append("\".equals( $2 ) "); - int len = m.getParameterTypes().length; - c3.append(" && ").append(" $3.length == ").append(len); + String mn = m.getName(); + c3.append(" if( \"").append(mn).append("\".equals( $2 ) "); + int len = m.getParameterTypes().length; + c3.append(" && ").append(" $3.length == ").append(len); - boolean override = false; - for (Method m2 : methods) { - if (m != m2 && m.getName().equals(m2.getName())) { - override = true; - break; - } + boolean override = false; + for (Method m2 : methods) { + if (m != m2 && m.getName().equals(m2.getName())) { + override = true; + break; } - if (override) { - if (len > 0) { - for (int l = 0; l < len; l++) { - c3.append(" && ").append(" $3[").append(l).append("].getName().equals(\"") - .append(m.getParameterTypes()[l].getName()).append("\")"); - } + } + if (override) { + if (len > 0) { + for (int l = 0; l < len; l++) { + c3.append(" && ").append(" $3[").append(l).append("].getName().equals(\"") + .append(m.getParameterTypes()[l].getName()).append("\")"); } } + } - c3.append(" ) { "); + c3.append(" ) { "); - if (m.getReturnType() == Void.TYPE) { - c3.append(" w.").append(mn).append('(').append(args(m.getParameterTypes(), "$4")).append(");").append(" return null;"); - } else { - c3.append(" return ($w)w.").append(mn).append('(').append(args(m.getParameterTypes(), "$4")).append(");"); - } + if (m.getReturnType() == Void.TYPE) { + c3.append(" w.").append(mn).append('(').append(args(m.getParameterTypes(), "$4")).append(");").append(" return null;"); + } else { + c3.append(" return ($w)w.").append(mn).append('(').append(args(m.getParameterTypes(), "$4")).append(");"); + } - c3.append(" }"); + c3.append(" }"); - mns.add(mn); - if (m.getDeclaringClass() == c) { - dmns.add(mn); - } - ms.put(ReflectUtils.getDesc(m), m); + mns.add(mn); + if (m.getDeclaringClass() == c) { + dmns.add(mn); } + ms.put(ReflectUtils.getDesc(m), m); + } + if (hasMethod) { c3.append(" } catch(Throwable e) { "); c3.append(" throw new java.lang.reflect.InvocationTargetException(e); "); c3.append(" }"); @@ -214,7 +216,7 @@ private static Wrapper makeWrapper(Class c) { Matcher matcher; for (Map.Entry entry : ms.entrySet()) { String md = entry.getKey(); - Method method = entry.getValue(); + Method method = (Method) entry.getValue(); if ((matcher = ReflectUtils.GETTER_METHOD_DESC_PATTERN.matcher(md)).matches()) { String pn = propertyName(matcher.group(1)); c2.append(" if( $2.equals(\"").append(pn).append("\") ){ return ($w)w.").append(method.getName()).append("(); }"); diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/compiler/support/ClassUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/compiler/support/ClassUtils.java index 365cb7f36db..e4db9a8cb5c 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/compiler/support/ClassUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/compiler/support/ClassUtils.java @@ -16,8 +16,6 @@ */ package org.apache.dubbo.common.compiler.support; -import org.apache.dubbo.common.utils.StringUtils; - import java.io.PrintWriter; import java.io.StringWriter; import java.lang.reflect.Array; @@ -58,12 +56,12 @@ public static Object newInstance(String name) { public static Class forName(String[] packages, String className) { try { - return classForName(className); + return _forName(className); } catch (ClassNotFoundException e) { if (packages != null && packages.length > 0) { for (String pkg : packages) { try { - return classForName(pkg + "." + className); + return _forName(pkg + "." + className); } catch (ClassNotFoundException e2) { } } @@ -74,14 +72,14 @@ public static Class forName(String[] packages, String className) { public static Class forName(String className) { try { - return classForName(className); + return _forName(className); } catch (ClassNotFoundException e) { throw new IllegalStateException(e.getMessage(), e); } } - public static Class classForName(String className) throws ClassNotFoundException { - switch (className) { + public static Class _forName(String className) throws ClassNotFoundException { + switch(className){ case "boolean": return boolean.class; case "byte": @@ -114,7 +112,6 @@ public static Class classForName(String className) throws ClassNotFoundExcept return float[].class; case "double[]": return double[].class; - default: } try { return arrayForName(className); @@ -293,7 +290,7 @@ public static Class getGenericClass(Class cls, int i) { } public static boolean isBeforeJava5(String javaVersion) { - return (StringUtils.isEmpty(javaVersion) || "1.0".equals(javaVersion) + return (javaVersion == null || javaVersion.length() == 0 || "1.0".equals(javaVersion) || "1.1".equals(javaVersion) || "1.2".equals(javaVersion) || "1.3".equals(javaVersion) || "1.4".equals(javaVersion)); } @@ -431,17 +428,5 @@ public static Map toMap(Map.Entry[] entries) { } return map; } - - /** - * get simple class name from qualified class name - */ - public static String getSimpleClassName(String qualifiedName) { - if (null == qualifiedName) { - return null; - } - - int i = qualifiedName.lastIndexOf('.'); - return i < 0 ? qualifiedName : qualifiedName.substring(i + 1); - } } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/compiler/support/CtClassBuilder.java b/dubbo-common/src/main/java/org/apache/dubbo/common/compiler/support/CtClassBuilder.java deleted file mode 100644 index 0dc8f881385..00000000000 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/compiler/support/CtClassBuilder.java +++ /dev/null @@ -1,174 +0,0 @@ -/* - * 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 org.apache.dubbo.common.compiler.support; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javassist.CannotCompileException; -import javassist.ClassPool; -import javassist.CtClass; -import javassist.CtField; -import javassist.CtNewConstructor; -import javassist.CtNewMethod; -import javassist.LoaderClassPath; -import javassist.NotFoundException; - -/** - * CtClassBuilder is builder for CtClass - *

- * contains all the information, including: - *

- * class name, imported packages, super class name, implemented interfaces, constructors, fields, methods. - */ -public class CtClassBuilder { - - private String className; - - private String superClassName = "java.lang.Object"; - - private List imports = new ArrayList<>(); - - private Map fullNames = new HashMap<>(); - - private List ifaces = new ArrayList<>(); - - private List constructors = new ArrayList<>(); - - private List fields = new ArrayList<>(); - - private List methods = new ArrayList<>(); - - public String getClassName() { - return className; - } - - public void setClassName(String className) { - this.className = className; - } - - public String getSuperClassName() { - return superClassName; - } - - public void setSuperClassName(String superClassName) { - this.superClassName = getQualifiedClassName(superClassName); - } - - public List getImports() { - return imports; - } - - public void addImports(String pkg) { - int pi = pkg.lastIndexOf('.'); - if (pi > 0) { - String pkgName = pkg.substring(0, pi); - this.imports.add(pkgName); - if (!pkg.endsWith(".*")) { - fullNames.put(pkg.substring(pi + 1), pkg); - } - } - } - - public List getInterfaces() { - return ifaces; - } - - public void addInterface(String iface) { - this.ifaces.add(getQualifiedClassName(iface)); - } - - public List getConstructors() { - return constructors; - } - - public void addConstructor(String constructor) { - this.constructors.add(constructor); - } - - public List getFields() { - return fields; - } - - public void addField(String field) { - this.fields.add(field); - } - - public List getMethods() { - return methods; - } - - public void addMethod(String method) { - this.methods.add(method); - } - - /** - * get full qualified class name - * - * @param className super class name, maybe qualified or not - */ - protected String getQualifiedClassName(String className) { - if (className.contains(".")) { - return className; - } - - if (fullNames.containsKey(className)) { - return fullNames.get(className); - } - - return ClassUtils.forName(imports.toArray(new String[0]), className).getName(); - } - - /** - * build CtClass object - */ - public CtClass build(ClassLoader classLoader) throws NotFoundException, CannotCompileException { - ClassPool pool = new ClassPool(true); - pool.appendClassPath(new LoaderClassPath(classLoader)); - - // create class - CtClass ctClass = pool.makeClass(className, pool.get(superClassName)); - - // add imported packages - imports.stream().forEach(pool::importPackage); - - // add implemented interfaces - for (String iface : ifaces) { - ctClass.addInterface(pool.get(iface)); - } - - // add constructors - for (String constructor : constructors) { - ctClass.addConstructor(CtNewConstructor.make(constructor, ctClass)); - } - - // add fields - for (String field : fields) { - ctClass.addField(CtField.make(field, ctClass)); - } - - // add methods - for (String method : methods) { - ctClass.addMethod(CtNewMethod.make(method, ctClass)); - } - - return ctClass; - } - -} diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/compiler/support/JavassistCompiler.java b/dubbo-common/src/main/java/org/apache/dubbo/common/compiler/support/JavassistCompiler.java index 11f76b1e6bd..5beb8e4ad9d 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/compiler/support/JavassistCompiler.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/compiler/support/JavassistCompiler.java @@ -18,9 +18,17 @@ import org.apache.dubbo.common.utils.ClassHelper; +import javassist.ClassPool; import javassist.CtClass; +import javassist.CtField; +import javassist.CtNewConstructor; +import javassist.CtNewMethod; +import javassist.LoaderClassPath; -import java.util.Arrays; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -41,46 +49,77 @@ public class JavassistCompiler extends AbstractCompiler { @Override public Class doCompile(String name, String source) throws Throwable { - CtClassBuilder builder = new CtClassBuilder(); - builder.setClassName(name); - - // process imported classes + int i = name.lastIndexOf('.'); + String className = i < 0 ? name : name.substring(i + 1); + ClassPool pool = new ClassPool(true); + pool.appendClassPath(new LoaderClassPath(ClassHelper.getCallerClassLoader(getClass()))); Matcher matcher = IMPORT_PATTERN.matcher(source); + List importPackages = new ArrayList(); + Map fullNames = new HashMap(); while (matcher.find()) { - builder.addImports(matcher.group(1).trim()); + String pkg = matcher.group(1); + if (pkg.endsWith(".*")) { + String pkgName = pkg.substring(0, pkg.length() - 2); + pool.importPackage(pkgName); + importPackages.add(pkgName); + } else { + int pi = pkg.lastIndexOf('.'); + if (pi > 0) { + String pkgName = pkg.substring(0, pi); + pool.importPackage(pkgName); + importPackages.add(pkgName); + fullNames.put(pkg.substring(pi + 1), pkg); + } + } } - - // process extended super class + String[] packages = importPackages.toArray(new String[0]); matcher = EXTENDS_PATTERN.matcher(source); + CtClass cls; if (matcher.find()) { - builder.setSuperClassName(matcher.group(1).trim()); + String extend = matcher.group(1).trim(); + String extendClass; + if (extend.contains(".")) { + extendClass = extend; + } else if (fullNames.containsKey(extend)) { + extendClass = fullNames.get(extend); + } else { + extendClass = ClassUtils.forName(packages, extend).getName(); + } + cls = pool.makeClass(name, pool.get(extendClass)); + } else { + cls = pool.makeClass(name); } - - // process implemented interfaces matcher = IMPLEMENTS_PATTERN.matcher(source); if (matcher.find()) { String[] ifaces = matcher.group(1).trim().split("\\,"); - Arrays.stream(ifaces).forEach(i -> builder.addInterface(i.trim())); + for (String iface : ifaces) { + iface = iface.trim(); + String ifaceClass; + if (iface.contains(".")) { + ifaceClass = iface; + } else if (fullNames.containsKey(iface)) { + ifaceClass = fullNames.get(iface); + } else { + ifaceClass = ClassUtils.forName(packages, iface).getName(); + } + cls.addInterface(pool.get(ifaceClass)); + } } - - // process constructors, fields, methods - String body = source.substring(source.indexOf('{') + 1, source.length() - 1); + String body = source.substring(source.indexOf("{") + 1, source.length() - 1); String[] methods = METHODS_PATTERN.split(body); - String className = ClassUtils.getSimpleClassName(name); - Arrays.stream(methods).map(String::trim).filter(m -> !m.isEmpty()).forEach(method-> { - if (method.startsWith(className)) { - builder.addConstructor("public " + method); - } else if (FIELD_PATTERN.matcher(method).matches()) { - builder.addField("private " + method); - } else { - builder.addMethod("public " + method); + for (String method : methods) { + method = method.trim(); + if (method.length() > 0) { + if (method.startsWith(className)) { + cls.addConstructor(CtNewConstructor.make("public " + method, cls)); + } else if (FIELD_PATTERN.matcher(method).matches()) { + cls.addField(CtField.make("private " + method, cls)); + } else { + cls.addMethod(CtNewMethod.make("public " + method, cls)); + } } - }); - - // compile - ClassLoader classLoader = ClassHelper.getCallerClassLoader(getClass()); - CtClass cls = builder.build(classLoader); - return cls.toClass(classLoader, JavassistCompiler.class.getProtectionDomain()); + } + return cls.toClass(ClassHelper.getCallerClassLoader(getClass()), JavassistCompiler.class.getProtectionDomain()); } } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/concurrent/ExecutionList.java b/dubbo-common/src/main/java/org/apache/dubbo/common/concurrent/ExecutionList.java new file mode 100644 index 00000000000..c88626cbe6a --- /dev/null +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/concurrent/ExecutionList.java @@ -0,0 +1,180 @@ +/* + * 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 org.apache.dubbo.common.concurrent; + +import org.apache.dubbo.common.logger.Logger; +import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.utils.NamedThreadFactory; + +import java.util.concurrent.Executor; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + *

A list of listeners, each with an associated {@code Executor}, that + * guarantees that every {@code Runnable} that is {@linkplain #add added} will + * be executed after {@link #execute()} is called. Any {@code Runnable} added + * after the call to {@code execute} is still guaranteed to execute. There is no + * guarantee, however, that listeners will be executed in the order that they + * are added. + *

+ *

Exceptions thrown by a listener will be propagated up to the executor. + * Any exception thrown during {@code Executor.execute} (e.g., a {@code + * RejectedExecutionException} or an exception thrown by {@linkplain + * MoreExecutors#sameThreadExecutor inline execution}) will be caught and + * logged. + */ +public final class ExecutionList { + // Logger to log exceptions caught when running runnables. + static final Logger logger = LoggerFactory.getLogger(ExecutionList.class.getName()); + + /** + * The runnable, executor pairs to execute. This acts as a stack threaded through the + * {@link RunnableExecutorPair#next} field. + */ + private RunnableExecutorPair runnables; + + private boolean executed; + + private static final Executor DEFAULT_EXECUTOR = new ThreadPoolExecutor(1, 10, 60000L, TimeUnit.MILLISECONDS, new SynchronousQueue(), new NamedThreadFactory("DubboFutureCallbackDefault", true)); + + /** + * Creates a new, empty {@link ExecutionList}. + */ + public ExecutionList() { + } + + /** + * Adds the {@code Runnable} and accompanying {@code Executor} to the list of + * listeners to execute. If execution has already begun, the listener is + * executed immediately. + *

+ *

Note: For fast, lightweight listeners that would be safe to execute in + * any thread, consider {@link MoreExecutors#sameThreadExecutor}. For heavier + * listeners, {@code sameThreadExecutor()} carries some caveats: First, the + * thread that the listener runs in depends on whether the {@code + * ExecutionList} has been executed at the time it is added. In particular, + * listeners may run in the thread that calls {@code add}. Second, the thread + * that calls {@link #execute} may be an internal implementation thread, such + * as an RPC network thread, and {@code sameThreadExecutor()} listeners may + * run in this thread. Finally, during the execution of a {@code + * sameThreadExecutor} listener, all other registered but unexecuted + * listeners are prevented from running, even if those listeners are to run + * in other executors. + */ + public void add(Runnable runnable, Executor executor) { + // Fail fast on a null. We throw NPE here because the contract of + // Executor states that it throws NPE on null listener, so we propagate + // that contract up into the add method as well. + if (runnable == null) { + throw new NullPointerException("Runnable can not be null!"); + } + if (executor == null) { + logger.info("Executor for listenablefuture is null, will use default executor!"); + executor = DEFAULT_EXECUTOR; + } + // Lock while we check state. We must maintain the lock while adding the + // new pair so that another thread can't run the list out from under us. + // We only add to the list if we have not yet started execution. + synchronized (this) { + if (!executed) { + runnables = new RunnableExecutorPair(runnable, executor, runnables); + return; + } + } + // Execute the runnable immediately. Because of scheduling this may end up + // getting called before some of the previously added runnables, but we're + // OK with that. If we want to change the contract to guarantee ordering + // among runnables we'd have to modify the logic here to allow it. + executeListener(runnable, executor); + } + + /** + * Runs this execution list, executing all existing pairs in the order they + * were added. However, note that listeners added after this point may be + * executed before those previously added, and note that the execution order + * of all listeners is ultimately chosen by the implementations of the + * supplied executors. + *

+ *

This method is idempotent. Calling it several times in parallel is + * semantically equivalent to calling it exactly once. + * + * @since 10.0 (present in 1.0 as {@code run}) + */ + public void execute() { + // Lock while we update our state so the add method above will finish adding + // any listeners before we start to run them. + RunnableExecutorPair list; + synchronized (this) { + if (executed) { + return; + } + executed = true; + list = runnables; + runnables = null; // allow GC to free listeners even if this stays around for a while. + } + // If we succeeded then list holds all the runnables we to execute. The pairs in the stack are + // in the opposite order from how they were added so we need to reverse the list to fulfill our + // contract. + // This is somewhat annoying, but turns out to be very fast in practice. Alternatively, we + // could drop the contract on the method that enforces this queue like behavior since depending + // on it is likely to be a bug anyway. + + // N.B. All writes to the list and the next pointers must have happened before the above + // synchronized block, so we can iterate the list without the lock held here. + RunnableExecutorPair reversedList = null; + while (list != null) { + RunnableExecutorPair tmp = list; + list = list.next; + tmp.next = reversedList; + reversedList = tmp; + } + while (reversedList != null) { + executeListener(reversedList.runnable, reversedList.executor); + reversedList = reversedList.next; + } + } + + /** + * Submits the given runnable to the given {@link Executor} catching and logging all + * {@linkplain RuntimeException runtime exceptions} thrown by the executor. + */ + private static void executeListener(Runnable runnable, Executor executor) { + try { + executor.execute(runnable); + } catch (RuntimeException e) { + // Log it and keep going, bad runnable and/or executor. Don't + // punish the other runnables if we're given a bad one. We only + // catch RuntimeException because we want Errors to propagate up. + logger.error("RuntimeException while executing runnable " + + runnable + " with executor " + executor, e); + } + } + + private static final class RunnableExecutorPair { + final Runnable runnable; + final Executor executor; + RunnableExecutorPair next; + + RunnableExecutorPair(Runnable runnable, Executor executor, RunnableExecutorPair next) { + this.runnable = runnable; + this.executor = executor; + this.next = next; + } + } +} diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/config/ConfigurationUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/config/ConfigurationUtils.java index e2431f20d24..572296c18d0 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/config/ConfigurationUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/config/ConfigurationUtils.java @@ -38,7 +38,7 @@ public class ConfigurationUtils { public static int getServerShutdownTimeout() { int timeout = Constants.DEFAULT_SERVER_SHUTDOWN_TIMEOUT; Configuration configuration = Environment.getInstance().getConfiguration(); - String value = StringUtils.trim(configuration.getString(Constants.SHUTDOWN_WAIT_KEY)); + String value = configuration.getString(Constants.SHUTDOWN_WAIT_KEY); if (value != null && value.length() > 0) { try { @@ -47,7 +47,7 @@ public static int getServerShutdownTimeout() { // ignore } } else { - value = StringUtils.trim(configuration.getString(Constants.SHUTDOWN_WAIT_SECONDS_KEY)); + value = configuration.getString(Constants.SHUTDOWN_WAIT_SECONDS_KEY); if (value != null && value.length() > 0) { try { timeout = Integer.parseInt(value) * 1000; @@ -64,7 +64,7 @@ public static String getProperty(String property) { } public static String getProperty(String property, String defaultValue) { - return StringUtils.trim(Environment.getInstance().getConfiguration().getString(property, defaultValue)); + return Environment.getInstance().getConfiguration().getString(property, defaultValue); } public static Map parseProperties(String content) throws IOException { diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/AdaptiveClassCodeGenerator.java b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/AdaptiveClassCodeGenerator.java deleted file mode 100644 index fcce47feaf1..00000000000 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/AdaptiveClassCodeGenerator.java +++ /dev/null @@ -1,381 +0,0 @@ -/* - * 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 org.apache.dubbo.common.extension; - -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.lang.reflect.Parameter; -import java.util.Arrays; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.StringUtils; - -/** - * Code generator for Adaptive class - */ -public class AdaptiveClassCodeGenerator { - - private static final Logger logger = LoggerFactory.getLogger(AdaptiveClassCodeGenerator.class); - - private static final String CLASSNAME_INVOCATION = "org.apache.dubbo.rpc.Invocation"; - - private static final String CODE_PACKAGE = "package %s;\n"; - - private static final String CODE_IMPORTS = "import %s;\n"; - - private static final String CODE_CLASS_DECLARATION = "public class %s$Adaptive implements %s {\n"; - - private static final String CODE_METHOD_DECLARATION = "public %s %s(%s) %s {\n%s}\n"; - - private static final String CODE_METHOD_ARGUMENT = "%s arg%d"; - - private static final String CODE_METHOD_THROWS = "throws %s"; - - private static final String CODE_UNSUPPORTED = "throw new UnsupportedOperationException(\"The method %s of interface %s is not adaptive method!\");\n"; - - private static final String CODE_URL_NULL_CHECK = "if (arg%d == null) throw new IllegalArgumentException(\"url == null\");\n%s url = arg%d;\n"; - - private static final String CODE_EXT_NAME_ASSIGNMENT = "String extName = %s;\n"; - - private static final String CODE_EXT_NAME_NULL_CHECK = "if(extName == null) " - + "throw new IllegalStateException(\"Failed to get extension (%s) name from url (\" + url.toString() + \") use keys(%s)\");\n"; - - private static final String CODE_INVOCATION_ARGUMENT_NULL_CHECK = "if (arg%d == null) throw new IllegalArgumentException(\"invocation == null\"); " - + "String methodName = arg%d.getMethodName();\n"; - - - private static final String CODE_EXTENSION_ASSIGNMENT = "%s extension = (% type; - - private String defaultExtName; - - public AdaptiveClassCodeGenerator(Class type, String defaultExtName) { - this.type = type; - this.defaultExtName = defaultExtName; - } - - /** - * test if given type has at least one method annotated with SPI - */ - private boolean hasAdaptiveMethod() { - return Arrays.stream(type.getMethods()).anyMatch(m -> m.isAnnotationPresent(Adaptive.class)); - } - - /** - * generate and return class code - */ - public String generate() { - // no need to generate adaptive class since there's no adaptive method found. - if (!hasAdaptiveMethod()) { - throw new IllegalStateException("No adaptive method exist on extension " + type.getName() + ", refuse to create the adaptive class!"); - } - - StringBuilder code = new StringBuilder(); - code.append(generatePackageInfo()); - code.append(generateImports()); - code.append(generateClassDeclaration()); - - Method[] methods = type.getMethods(); - for (Method method : methods) { - code.append(generateMethod(method)); - } - code.append("}"); - - if (logger.isDebugEnabled()) { - logger.debug(code.toString()); - } - return code.toString(); - } - - /** - * generate package info - */ - private String generatePackageInfo() { - return String.format(CODE_PACKAGE, type.getPackage().getName()); - } - - /** - * generate imports - */ - private String generateImports() { - return String.format(CODE_IMPORTS, ExtensionLoader.class.getName()); - } - - /** - * generate class declaration - */ - private String generateClassDeclaration() { - return String.format(CODE_CLASS_DECLARATION, type.getSimpleName(), type.getCanonicalName()); - } - - /** - * generate method not annotated with Adaptive with throwing unsupported exception - */ - private String generateUnsupported(Method method) { - return String.format(CODE_UNSUPPORTED, method, type.getName()); - } - - /** - * get index of parameter with type URL - */ - private int getUrlTypeIndex(Method method) { - int urlTypeIndex = -1; - Class[] pts = method.getParameterTypes(); - for (int i = 0; i < pts.length; ++i) { - if (pts[i].equals(URL.class)) { - urlTypeIndex = i; - break; - } - } - return urlTypeIndex; - } - - /** - * generate method declaration - */ - private String generateMethod(Method method) { - String methodReturnType = method.getReturnType().getCanonicalName(); - String methodName = method.getName(); - String methodContent = generateMethodContent(method); - String methodArgs = generateMethodArguments(method); - String methodThrows = generateMethodThrows(method); - return String.format(CODE_METHOD_DECLARATION, methodReturnType, methodName, methodArgs, methodThrows, methodContent); - } - - /** - * generate method arguments - */ - private String generateMethodArguments(Method method) { - Class[] pts = method.getParameterTypes(); - return IntStream.range(0, pts.length) - .mapToObj(i -> String.format(CODE_METHOD_ARGUMENT, pts[i].getCanonicalName(), i)) - .collect(Collectors.joining(", ")); - } - - /** - * generate method throws - */ - private String generateMethodThrows(Method method) { - Class[] ets = method.getExceptionTypes(); - if (ets.length > 0) { - String list = Arrays.stream(ets).map(Class::getCanonicalName).collect(Collectors.joining(", ")); - return String.format(CODE_METHOD_THROWS, list); - } else { - return ""; - } - } - - /** - * generate method URL argument null check - */ - private String generateUrlNullCheck(int index) { - return String.format(CODE_URL_NULL_CHECK, index, URL.class.getName(), index); - } - - /** - * generate method content - */ - private String generateMethodContent(Method method) { - Adaptive adaptiveAnnotation = method.getAnnotation(Adaptive.class); - StringBuilder code = new StringBuilder(512); - if (adaptiveAnnotation == null) { - return generateUnsupported(method); - } else { - int urlTypeIndex = getUrlTypeIndex(method); - - // found parameter in URL type - if (urlTypeIndex != -1) { - // Null Point check - code.append(generateUrlNullCheck(urlTypeIndex)); - } else { - // did not find parameter in URL type - code.append(generateUrlAssignmentIndirectly(method)); - } - - String[] value = getMethodAdaptiveValue(adaptiveAnnotation); - - boolean hasInvocation = hasInvocationArgument(method); - - code.append(generateInvocationArgumentNullCheck(method)); - - code.append(generateExtNameAssignment(value, hasInvocation)); - // check extName == null? - code.append(generateExtNameNullCheck(value)); - - code.append(generateExtensionAssignment()); - - // return statement - code.append(generateReturnAndInovation(method)); - } - - return code.toString(); - } - - /** - * generate code for variable extName null check - */ - private String generateExtNameNullCheck(String[] value) { - return String.format(CODE_EXT_NAME_NULL_CHECK, type.getName(), Arrays.toString(value)); - } - - /** - * generate extName assigment code - */ - private String generateExtNameAssignment(String[] value, boolean hasInvocation) { - // TODO: refactor it - String getNameCode = null; - for (int i = value.length - 1; i >= 0; --i) { - if (i == value.length - 1) { - if (null != defaultExtName) { - if (!"protocol".equals(value[i])) { - if (hasInvocation) { - getNameCode = String.format("url.getMethodParameter(methodName, \"%s\", \"%s\")", value[i], defaultExtName); - } else { - getNameCode = String.format("url.getParameter(\"%s\", \"%s\")", value[i], defaultExtName); - } - } else { - getNameCode = String.format("( url.getProtocol() == null ? \"%s\" : url.getProtocol() )", defaultExtName); - } - } else { - if (!"protocol".equals(value[i])) { - if (hasInvocation) { - getNameCode = String.format("url.getMethodParameter(methodName, \"%s\", \"%s\")", value[i], defaultExtName); - } else { - getNameCode = String.format("url.getParameter(\"%s\")", value[i]); - } - } else { - getNameCode = "url.getProtocol()"; - } - } - } else { - if (!"protocol".equals(value[i])) { - if (hasInvocation) { - getNameCode = String.format("url.getMethodParameter(methodName, \"%s\", \"%s\")", value[i], defaultExtName); - } else { - getNameCode = String.format("url.getParameter(\"%s\", %s)", value[i], getNameCode); - } - } else { - getNameCode = String.format("url.getProtocol() == null ? (%s) : url.getProtocol()", getNameCode); - } - } - } - - return String.format(CODE_EXT_NAME_ASSIGNMENT, getNameCode); - } - - /** - * @return - */ - private String generateExtensionAssignment() { - return String.format(CODE_EXTENSION_ASSIGNMENT, type.getName(), ExtensionLoader.class.getSimpleName(), type.getName()); - } - - /** - * generate method invocation statement and return it if necessary - */ - private String generateReturnAndInovation(Method method) { - String returnStatement = method.getReturnType().equals(void.class) ? "" : "return "; - - String args = Arrays.stream(method.getParameters()).map(Parameter::getName).collect(Collectors.joining(", ")); - - return returnStatement + String.format("extension.%s(%s);\n", method.getName(), args); - } - - /** - * test if method has argument of type Invocation - */ - private boolean hasInvocationArgument(Method method) { - Class[] pts = method.getParameterTypes(); - return Arrays.stream(pts).anyMatch(p -> CLASSNAME_INVOCATION.equals(p.getName())); - } - - /** - * generate code to test argument of type Invocation is null - */ - private String generateInvocationArgumentNullCheck(Method method) { - Class[] pts = method.getParameterTypes(); - return IntStream.range(0, pts.length).filter(i -> CLASSNAME_INVOCATION.equals(pts[i].getName())) - .mapToObj(i -> String.format(CODE_INVOCATION_ARGUMENT_NULL_CHECK, i, i)) - .findFirst().orElse(""); - } - - /** - * get value of adaptive annotation or if empty return splitted simple name - */ - private String[] getMethodAdaptiveValue(Adaptive adaptiveAnnotation) { - String[] value = adaptiveAnnotation.value(); - // value is not set, use the value generated from class name as the key - if (value.length == 0) { - String splitName = StringUtils.camelToSplitName(type.getSimpleName(), "."); - value = new String[]{splitName}; - } - return value; - } - - /** - * get parameter with type URL from method parameter: - *

- * test if parameter has method which returns type URL - *

- * if not found, throws IllegalStateException - */ - private String generateUrlAssignmentIndirectly(Method method) { - Class[] pts = method.getParameterTypes(); - - // find URL getter method - for (int i = 0; i < pts.length; ++i) { - for (Method m : pts[i].getMethods()) { - String name = m.getName(); - if ((name.startsWith("get") || name.length() > 3) - && Modifier.isPublic(m.getModifiers()) - && !Modifier.isStatic(m.getModifiers()) - && m.getParameterTypes().length == 0 - && m.getReturnType() == URL.class) { - return generateGetUrlNullCheck(i, pts[i], name); - } - } - } - - // getter method not found, throw - throw new IllegalStateException("Failed to create adaptive class for interface " + type.getName() - + ": not found url parameter or url attribute in parameters of method " + method.getName()); - - } - - /** - * 1, test if argi is null - * 2, test if argi.getXX() returns null - * 3, assign url with argi.getXX() - */ - private String generateGetUrlNullCheck(int index, Class type, String method) { - // Null point check - StringBuilder code = new StringBuilder(); - code.append(String.format("if (arg%d == null) throw new IllegalArgumentException(\"%s argument == null\");\n", - index, type.getName())); - code.append(String.format("if (arg%d.%s() == null) throw new IllegalArgumentException(\"%s argument %s() == null\");\n", - index, method, type.getName(), method)); - - code.append(String.format("%s url = arg%d.%s();\n", URL.class.getName(), index, method)); - return code.toString(); - } - -} diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java index 3f2709e8621..578ff65ddf5 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java @@ -21,19 +21,17 @@ import org.apache.dubbo.common.extension.support.ActivateComparator; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.ArrayUtils; import org.apache.dubbo.common.utils.ClassHelper; import org.apache.dubbo.common.utils.ConcurrentHashSet; import org.apache.dubbo.common.utils.ConfigUtils; import org.apache.dubbo.common.utils.Holder; import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.common.utils.CollectionUtils; + import java.io.BufferedReader; import java.io.InputStreamReader; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -72,9 +70,9 @@ public class ExtensionLoader { private static final Pattern NAME_SEPARATOR = Pattern.compile("\\s*[,]+\\s*"); - private static final ConcurrentMap, ExtensionLoader> EXTENSION_LOADERS = new ConcurrentHashMap<>(); + private static final ConcurrentMap, ExtensionLoader> EXTENSION_LOADERS = new ConcurrentHashMap, ExtensionLoader>(); - private static final ConcurrentMap, Object> EXTENSION_INSTANCES = new ConcurrentHashMap<>(); + private static final ConcurrentMap, Object> EXTENSION_INSTANCES = new ConcurrentHashMap, Object>(); // ============================== @@ -82,20 +80,20 @@ public class ExtensionLoader { private final ExtensionFactory objectFactory; - private final ConcurrentMap, String> cachedNames = new ConcurrentHashMap<>(); + private final ConcurrentMap, String> cachedNames = new ConcurrentHashMap, String>(); - private final Holder>> cachedClasses = new Holder<>(); + private final Holder>> cachedClasses = new Holder>>(); - private final Map cachedActivates = new ConcurrentHashMap<>(); - private final ConcurrentMap> cachedInstances = new ConcurrentHashMap<>(); - private final Holder cachedAdaptiveInstance = new Holder<>(); + private final Map cachedActivates = new ConcurrentHashMap(); + private final ConcurrentMap> cachedInstances = new ConcurrentHashMap>(); + private final Holder cachedAdaptiveInstance = new Holder(); private volatile Class cachedAdaptiveClass = null; private String cachedDefaultName; private volatile Throwable createAdaptiveInstanceError; private Set> cachedWrapperClasses; - private Map exceptions = new ConcurrentHashMap<>(); + private Map exceptions = new ConcurrentHashMap(); private ExtensionLoader(Class type) { this.type = type; @@ -112,11 +110,11 @@ public static ExtensionLoader getExtensionLoader(Class type) { throw new IllegalArgumentException("Extension type == null"); } if (!type.isInterface()) { - throw new IllegalArgumentException("Extension type (" + type + ") is not an interface!"); + throw new IllegalArgumentException("Extension type(" + type + ") is not interface!"); } if (!withExtensionAnnotation(type)) { - throw new IllegalArgumentException("Extension type (" + type + - ") is not an extension, because it is NOT annotated with @" + SPI.class.getSimpleName() + "!"); + throw new IllegalArgumentException("Extension type(" + type + + ") is not extension, because WITHOUT @" + SPI.class.getSimpleName() + " Annotation!"); } ExtensionLoader loader = (ExtensionLoader) EXTENSION_LOADERS.get(type); @@ -189,7 +187,7 @@ public List getActivateExtension(URL url, String[] values) { */ public List getActivateExtension(URL url, String key, String group) { String value = url.getParameter(key); - return getActivateExtension(url, StringUtils.isEmpty(value) ? null : Constants.COMMA_SPLIT_PATTERN.split(value), group); + return getActivateExtension(url, value == null || value.length() == 0 ? null : Constants.COMMA_SPLIT_PATTERN.split(value), group); } /** @@ -202,8 +200,8 @@ public List getActivateExtension(URL url, String key, String group) { * @see org.apache.dubbo.common.extension.Activate */ public List getActivateExtension(URL url, String[] values, String group) { - List exts = new ArrayList<>(); - List names = values == null ? new ArrayList<>(0) : Arrays.asList(values); + List exts = new ArrayList(); + List names = values == null ? new ArrayList(0) : Arrays.asList(values); if (!names.contains(Constants.REMOVE_VALUE_PREFIX + Constants.DEFAULT_KEY)) { getExtensionClasses(); for (Map.Entry entry : cachedActivates.entrySet()) { @@ -230,9 +228,9 @@ && isActive(activateValue, url)) { } } } - exts.sort(ActivateComparator.COMPARATOR); + Collections.sort(exts, ActivateComparator.COMPARATOR); } - List usrs = new ArrayList<>(); + List usrs = new ArrayList(); for (int i = 0; i < names.size(); i++) { String name = names.get(i); if (!name.startsWith(Constants.REMOVE_VALUE_PREFIX) @@ -255,7 +253,7 @@ && isActive(activateValue, url)) { } private boolean isMatchGroup(String group, String[] groups) { - if (StringUtils.isEmpty(group)) { + if (group == null || group.length() == 0) { return true; } if (groups != null && groups.length > 0) { @@ -295,20 +293,15 @@ private boolean isActive(String[] keys, URL url) { */ @SuppressWarnings("unchecked") public T getLoadedExtension(String name) { - if (StringUtils.isEmpty(name)) { + if (name == null || name.length() == 0) { throw new IllegalArgumentException("Extension name == null"); } - Holder holder = getOrCreateHolder(name); - return (T) holder.get(); - } - - private Holder getOrCreateHolder(String name) { Holder holder = cachedInstances.get(name); if (holder == null) { - cachedInstances.putIfAbsent(name, new Holder<>()); + cachedInstances.putIfAbsent(name, new Holder()); holder = cachedInstances.get(name); } - return holder; + return (T) holder.get(); } /** @@ -319,7 +312,7 @@ private Holder getOrCreateHolder(String name) { * @see #getSupportedExtensions() */ public Set getLoadedExtensions() { - return Collections.unmodifiableSet(new TreeSet<>(cachedInstances.keySet())); + return Collections.unmodifiableSet(new TreeSet(cachedInstances.keySet())); } public Object getLoadedAdaptiveExtensionInstances() { @@ -332,13 +325,17 @@ public Object getLoadedAdaptiveExtensionInstances() { */ @SuppressWarnings("unchecked") public T getExtension(String name) { - if (StringUtils.isEmpty(name)) { + if (name == null || name.length() == 0) { throw new IllegalArgumentException("Extension name == null"); } if ("true".equals(name)) { return getDefaultExtension(); } - Holder holder = getOrCreateHolder(name); + Holder holder = cachedInstances.get(name); + if (holder == null) { + cachedInstances.putIfAbsent(name, new Holder()); + holder = cachedInstances.get(name); + } Object instance = holder.get(); if (instance == null) { synchronized (holder) { @@ -357,14 +354,15 @@ public T getExtension(String name) { */ public T getDefaultExtension() { getExtensionClasses(); - if (StringUtils.isBlank(cachedDefaultName) || "true".equals(cachedDefaultName)) { + if (null == cachedDefaultName || cachedDefaultName.length() == 0 + || "true".equals(cachedDefaultName)) { return null; } return getExtension(cachedDefaultName); } public boolean hasExtension(String name) { - if (StringUtils.isEmpty(name)) { + if (name == null || name.length() == 0) { throw new IllegalArgumentException("Extension name == null"); } Class c = this.getExtensionClass(name); @@ -373,7 +371,7 @@ public boolean hasExtension(String name) { public Set getSupportedExtensions() { Map> clazzes = getExtensionClasses(); - return Collections.unmodifiableSet(new TreeSet<>(clazzes.keySet())); + return Collections.unmodifiableSet(new TreeSet(clazzes.keySet())); } /** @@ -396,11 +394,11 @@ public void addExtension(String name, Class clazz) { if (!type.isAssignableFrom(clazz)) { throw new IllegalStateException("Input type " + - clazz + " doesn't implement the Extension " + type); + clazz + "not implement Extension " + type); } if (clazz.isInterface()) { throw new IllegalStateException("Input type " + - clazz + " can't be interface!"); + clazz + "can not be interface!"); } if (!clazz.isAnnotationPresent(Adaptive.class)) { @@ -409,14 +407,14 @@ public void addExtension(String name, Class clazz) { } if (cachedClasses.get().containsKey(name)) { throw new IllegalStateException("Extension name " + - name + " already exists (Extension " + type + ")!"); + name + " already existed(Extension " + type + ")!"); } cachedNames.put(clazz, name); cachedClasses.get().put(name, clazz); } else { if (cachedAdaptiveClass != null) { - throw new IllegalStateException("Adaptive Extension already exists (Extension " + type + ")!"); + throw new IllegalStateException("Adaptive Extension already existed(Extension " + type + ")!"); } cachedAdaptiveClass = clazz; @@ -437,11 +435,11 @@ public void replaceExtension(String name, Class clazz) { if (!type.isAssignableFrom(clazz)) { throw new IllegalStateException("Input type " + - clazz + " doesn't implement Extension " + type); + clazz + "not implement Extension " + type); } if (clazz.isInterface()) { throw new IllegalStateException("Input type " + - clazz + " can't be interface!"); + clazz + "can not be interface!"); } if (!clazz.isAnnotationPresent(Adaptive.class)) { @@ -450,7 +448,7 @@ public void replaceExtension(String name, Class clazz) { } if (!cachedClasses.get().containsKey(name)) { throw new IllegalStateException("Extension name " + - name + " doesn't exist (Extension " + type + ")!"); + name + " not existed(Extension " + type + ")!"); } cachedNames.put(clazz, name); @@ -458,7 +456,7 @@ public void replaceExtension(String name, Class clazz) { cachedInstances.remove(name); } else { if (cachedAdaptiveClass == null) { - throw new IllegalStateException("Adaptive Extension doesn't exist (Extension " + type + ")!"); + throw new IllegalStateException("Adaptive Extension not existed(Extension " + type + ")!"); } cachedAdaptiveClass = clazz; @@ -479,12 +477,12 @@ public T getAdaptiveExtension() { cachedAdaptiveInstance.set(instance); } catch (Throwable t) { createAdaptiveInstanceError = t; - throw new IllegalStateException("Failed to create adaptive instance: " + t.toString(), t); + throw new IllegalStateException("fail to create adaptive instance: " + t.toString(), t); } } } } else { - throw new IllegalStateException("Failed to create adaptive instance: " + createAdaptiveInstanceError.toString(), createAdaptiveInstanceError); + throw new IllegalStateException("fail to create adaptive instance: " + createAdaptiveInstanceError.toString(), createAdaptiveInstanceError); } } @@ -530,15 +528,15 @@ private T createExtension(String name) { } injectExtension(instance); Set> wrapperClasses = cachedWrapperClasses; - if (CollectionUtils.isNotEmpty(wrapperClasses)) { + if (wrapperClasses != null && !wrapperClasses.isEmpty()) { for (Class wrapperClass : wrapperClasses) { instance = injectExtension((T) wrapperClass.getConstructor(type).newInstance(instance)); } } return instance; } catch (Throwable t) { - throw new IllegalStateException("Extension instance (name: " + name + ", class: " + - type + ") couldn't be instantiated: " + t.getMessage(), t); + throw new IllegalStateException("Extension instance(name: " + name + ", class: " + + type + ") could not be instantiated: " + t.getMessage(), t); } } @@ -546,7 +544,9 @@ private T injectExtension(T instance) { try { if (objectFactory != null) { for (Method method : instance.getClass().getMethods()) { - if (isSetter(method)) { + if (method.getName().startsWith("set") + && method.getParameterTypes().length == 1 + && Modifier.isPublic(method.getModifiers())) { /** * Check {@link DisableInject} to see if we need auto injection for this property */ @@ -558,13 +558,13 @@ private T injectExtension(T instance) { continue; } try { - String property = getSetterProperty(method); + String property = method.getName().length() > 3 ? method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4) : ""; Object object = objectFactory.getExtension(pt, property); if (object != null) { method.invoke(instance, object); } } catch (Exception e) { - logger.error("Failed to inject via method " + method.getName() + logger.error("fail to inject via method " + method.getName() + " of interface " + type.getName() + ": " + e.getMessage(), e); } } @@ -576,30 +576,6 @@ private T injectExtension(T instance) { return instance; } - /** - * get properties name for setter, for instance: setVersion, return "version" - *

- * return "", if setter name with length less than 3 - */ - private String getSetterProperty(Method method) { - return method.getName().length() > 3 ? method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4) : ""; - } - - /** - * return true if and only if: - *

- * 1, public - *

- * 2, name starts with "set" - *

- * 3, only has one parameter - */ - private boolean isSetter(Method method) { - return method.getName().startsWith("set") - && method.getParameterTypes().length == 1 - && Modifier.isPublic(method.getModifiers()); - } - private Class getExtensionClass(String name) { if (type == null) { throw new IllegalArgumentException("Extension type == null"); @@ -626,29 +602,13 @@ private Map> getExtensionClasses() { // synchronized in getExtensionClasses private Map> loadExtensionClasses() { - cacheDefaultExtensionName(); - - Map> extensionClasses = new HashMap<>(); - loadDirectory(extensionClasses, DUBBO_INTERNAL_DIRECTORY, type.getName()); - loadDirectory(extensionClasses, DUBBO_INTERNAL_DIRECTORY, type.getName().replace("org.apache", "com.alibaba")); - loadDirectory(extensionClasses, DUBBO_DIRECTORY, type.getName()); - loadDirectory(extensionClasses, DUBBO_DIRECTORY, type.getName().replace("org.apache", "com.alibaba")); - loadDirectory(extensionClasses, SERVICES_DIRECTORY, type.getName()); - loadDirectory(extensionClasses, SERVICES_DIRECTORY, type.getName().replace("org.apache", "com.alibaba")); - return extensionClasses; - } - - /** - * extract and cache default extension name if exists - */ - private void cacheDefaultExtensionName() { final SPI defaultAnnotation = type.getAnnotation(SPI.class); if (defaultAnnotation != null) { String value = defaultAnnotation.value(); if ((value = value.trim()).length() > 0) { String[] names = NAME_SEPARATOR.split(value); if (names.length > 1) { - throw new IllegalStateException("More than 1 default extension name on extension " + type.getName() + throw new IllegalStateException("more than 1 default extension name on extension " + type.getName() + ": " + Arrays.toString(names)); } if (names.length == 1) { @@ -656,6 +616,15 @@ private void cacheDefaultExtensionName() { } } } + + Map> extensionClasses = new HashMap>(); + loadDirectory(extensionClasses, DUBBO_INTERNAL_DIRECTORY, type.getName()); + loadDirectory(extensionClasses, DUBBO_INTERNAL_DIRECTORY, type.getName().replace("org.apache", "com.alibaba")); + loadDirectory(extensionClasses, DUBBO_DIRECTORY, type.getName()); + loadDirectory(extensionClasses, DUBBO_DIRECTORY, type.getName().replace("org.apache", "com.alibaba")); + loadDirectory(extensionClasses, SERVICES_DIRECTORY, type.getName()); + loadDirectory(extensionClasses, SERVICES_DIRECTORY, type.getName().replace("org.apache", "com.alibaba")); + return extensionClasses; } private void loadDirectory(Map> extensionClasses, String dir, String type) { @@ -675,14 +644,15 @@ private void loadDirectory(Map> extensionClasses, String dir, S } } } catch (Throwable t) { - logger.error("Exception occurred when loading extension class (interface: " + + logger.error("Exception when load extension class(interface: " + type + ", description file: " + fileName + ").", t); } } private void loadResource(Map> extensionClasses, ClassLoader classLoader, java.net.URL resourceURL) { try { - try (BufferedReader reader = new BufferedReader(new InputStreamReader(resourceURL.openStream(), StandardCharsets.UTF_8))) { + BufferedReader reader = new BufferedReader(new InputStreamReader(resourceURL.openStream(), "utf-8")); + try { String line; while ((line = reader.readLine()) != null) { final int ci = line.indexOf('#'); @@ -702,117 +672,76 @@ private void loadResource(Map> extensionClasses, ClassLoader cl loadClass(extensionClasses, resourceURL, Class.forName(line, true, classLoader), name); } } catch (Throwable t) { - IllegalStateException e = new IllegalStateException("Failed to load extension class (interface: " + type + ", class line: " + line + ") in " + resourceURL + ", cause: " + t.getMessage(), t); + IllegalStateException e = new IllegalStateException("Failed to load extension class(interface: " + type + ", class line: " + line + ") in " + resourceURL + ", cause: " + t.getMessage(), t); exceptions.put(line, e); } } } + } finally { + reader.close(); } } catch (Throwable t) { - logger.error("Exception occurred when loading extension class (interface: " + + logger.error("Exception when load extension class(interface: " + type + ", class file: " + resourceURL + ") in " + resourceURL, t); } } private void loadClass(Map> extensionClasses, java.net.URL resourceURL, Class clazz, String name) throws NoSuchMethodException { if (!type.isAssignableFrom(clazz)) { - throw new IllegalStateException("Error occurred when loading extension class (interface: " + + throw new IllegalStateException("Error when load extension class(interface: " + type + ", class line: " + clazz.getName() + "), class " - + clazz.getName() + " is not subtype of interface."); + + clazz.getName() + "is not subtype of interface."); } if (clazz.isAnnotationPresent(Adaptive.class)) { - cacheAdaptiveClass(clazz); + if (cachedAdaptiveClass == null) { + cachedAdaptiveClass = clazz; + } else if (!cachedAdaptiveClass.equals(clazz)) { + throw new IllegalStateException("More than 1 adaptive class found: " + + cachedAdaptiveClass.getClass().getName() + + ", " + clazz.getClass().getName()); + } } else if (isWrapperClass(clazz)) { - cacheWrapperClass(clazz); + Set> wrappers = cachedWrapperClasses; + if (wrappers == null) { + cachedWrapperClasses = new ConcurrentHashSet>(); + wrappers = cachedWrapperClasses; + } + wrappers.add(clazz); } else { clazz.getConstructor(); - if (StringUtils.isEmpty(name)) { + if (name == null || name.length() == 0) { name = findAnnotationName(clazz); if (name.length() == 0) { throw new IllegalStateException("No such extension name for the class " + clazz.getName() + " in the config " + resourceURL); } } - String[] names = NAME_SEPARATOR.split(name); - if (ArrayUtils.isNotEmpty(names)) { - cacheActivateClass(clazz, names[0]); + if (names != null && names.length > 0) { + Activate activate = clazz.getAnnotation(Activate.class); + if (activate != null) { + cachedActivates.put(names[0], activate); + } else { + // support com.alibaba.dubbo.common.extension.Activate + com.alibaba.dubbo.common.extension.Activate oldActivate = clazz.getAnnotation(com.alibaba.dubbo.common.extension.Activate.class); + if (oldActivate != null) { + cachedActivates.put(names[0], oldActivate); + } + } for (String n : names) { - cacheName(clazz, n); - saveInExtensionClass(extensionClasses, clazz, name); + if (!cachedNames.containsKey(clazz)) { + cachedNames.put(clazz, n); + } + Class c = extensionClasses.get(n); + if (c == null) { + extensionClasses.put(n, clazz); + } else if (c != clazz) { + throw new IllegalStateException("Duplicate extension " + type.getName() + " name " + n + " on " + c.getName() + " and " + clazz.getName()); + } } } } } - /** - * cache name - */ - private void cacheName(Class clazz, String name) { - if (!cachedNames.containsKey(clazz)) { - cachedNames.put(clazz, name); - } - } - - /** - * put clazz in extensionClasses - */ - private void saveInExtensionClass(Map> extensionClasses, Class clazz, String name) { - Class c = extensionClasses.get(name); - if (c == null) { - extensionClasses.put(name, clazz); - } else if (c != clazz) { - throw new IllegalStateException("Duplicate extension " + type.getName() + " name " + name + " on " + c.getName() + " and " + clazz.getName()); - } - } - - /** - * cache Activate class which is annotated with Activate - *

- * for compatibility, also cache class with old alibaba Activate annotation - */ - private void cacheActivateClass(Class clazz, String name) { - Activate activate = clazz.getAnnotation(Activate.class); - if (activate != null) { - cachedActivates.put(name, activate); - } else { - // support com.alibaba.dubbo.common.extension.Activate - com.alibaba.dubbo.common.extension.Activate oldActivate = clazz.getAnnotation(com.alibaba.dubbo.common.extension.Activate.class); - if (oldActivate != null) { - cachedActivates.put(name, oldActivate); - } - } - } - - /** - * cache Adaptive class which is annotated with Adaptive - */ - private void cacheAdaptiveClass(Class clazz) { - if (cachedAdaptiveClass == null) { - cachedAdaptiveClass = clazz; - } else if (!cachedAdaptiveClass.equals(clazz)) { - throw new IllegalStateException("More than 1 adaptive class found: " - + cachedAdaptiveClass.getClass().getName() - + ", " + clazz.getClass().getName()); - } - } - - /** - * cache wrapper class - *

- * like: ProtocolFilterWrapper, ProtocolListenerWrapper - */ - private void cacheWrapperClass(Class clazz) { - if (cachedWrapperClasses == null) { - cachedWrapperClasses = new ConcurrentHashSet<>(); - } - cachedWrapperClasses.add(clazz); - } - - /** - * test if clazz is a wrapper class - *

- * which has Constructor with given class type as its only argument - */ private boolean isWrapperClass(Class clazz) { try { clazz.getConstructor(type); @@ -840,7 +769,7 @@ private T createAdaptiveExtension() { try { return injectExtension((T) getAdaptiveExtensionClass().newInstance()); } catch (Exception e) { - throw new IllegalStateException("Can't create adaptive extension " + type + ", cause: " + e.getMessage(), e); + throw new IllegalStateException("Can not create adaptive extension " + type + ", cause: " + e.getMessage(), e); } } @@ -853,12 +782,212 @@ private Class getAdaptiveExtensionClass() { } private Class createAdaptiveExtensionClass() { - String code = new AdaptiveClassCodeGenerator(type, cachedDefaultName).generate(); + String code = createAdaptiveExtensionClassCode(); ClassLoader classLoader = findClassLoader(); org.apache.dubbo.common.compiler.Compiler compiler = ExtensionLoader.getExtensionLoader(org.apache.dubbo.common.compiler.Compiler.class).getAdaptiveExtension(); return compiler.compile(code, classLoader); } + private String createAdaptiveExtensionClassCode() { + StringBuilder codeBuilder = new StringBuilder(); + Method[] methods = type.getMethods(); + boolean hasAdaptiveAnnotation = false; + for (Method m : methods) { + if (m.isAnnotationPresent(Adaptive.class)) { + hasAdaptiveAnnotation = true; + break; + } + } + // no need to generate adaptive class since there's no adaptive method found. + if (!hasAdaptiveAnnotation) { + throw new IllegalStateException("No adaptive method on extension " + type.getName() + ", refuse to create the adaptive class!"); + } + + codeBuilder.append("package ").append(type.getPackage().getName()).append(";"); + codeBuilder.append("\nimport ").append(ExtensionLoader.class.getName()).append(";"); + codeBuilder.append("\npublic class ").append(type.getSimpleName()).append("$Adaptive").append(" implements ").append(type.getCanonicalName()).append(" {"); + + for (Method method : methods) { + Class rt = method.getReturnType(); + Class[] pts = method.getParameterTypes(); + Class[] ets = method.getExceptionTypes(); + + Adaptive adaptiveAnnotation = method.getAnnotation(Adaptive.class); + StringBuilder code = new StringBuilder(512); + if (adaptiveAnnotation == null) { + code.append("throw new UnsupportedOperationException(\"method ") + .append(method.toString()).append(" of interface ") + .append(type.getName()).append(" is not adaptive method!\");"); + } else { + int urlTypeIndex = -1; + for (int i = 0; i < pts.length; ++i) { + if (pts[i].equals(URL.class)) { + urlTypeIndex = i; + break; + } + } + // found parameter in URL type + if (urlTypeIndex != -1) { + // Null Point check + String s = String.format("\nif (arg%d == null) throw new IllegalArgumentException(\"url == null\");", + urlTypeIndex); + code.append(s); + + s = String.format("\n%s url = arg%d;", URL.class.getName(), urlTypeIndex); + code.append(s); + } + // did not find parameter in URL type + else { + String attribMethod = null; + + // find URL getter method + LBL_PTS: + for (int i = 0; i < pts.length; ++i) { + Method[] ms = pts[i].getMethods(); + for (Method m : ms) { + String name = m.getName(); + if ((name.startsWith("get") || name.length() > 3) + && Modifier.isPublic(m.getModifiers()) + && !Modifier.isStatic(m.getModifiers()) + && m.getParameterTypes().length == 0 + && m.getReturnType() == URL.class) { + urlTypeIndex = i; + attribMethod = name; + break LBL_PTS; + } + } + } + if (attribMethod == null) { + throw new IllegalStateException("fail to create adaptive class for interface " + type.getName() + + ": not found url parameter or url attribute in parameters of method " + method.getName()); + } + + // Null point check + String s = String.format("\nif (arg%d == null) throw new IllegalArgumentException(\"%s argument == null\");", + urlTypeIndex, pts[urlTypeIndex].getName()); + code.append(s); + s = String.format("\nif (arg%d.%s() == null) throw new IllegalArgumentException(\"%s argument %s() == null\");", + urlTypeIndex, attribMethod, pts[urlTypeIndex].getName(), attribMethod); + code.append(s); + + s = String.format("%s url = arg%d.%s();", URL.class.getName(), urlTypeIndex, attribMethod); + code.append(s); + } + + String[] value = adaptiveAnnotation.value(); + // value is not set, use the value generated from class name as the key + if (value.length == 0) { + String splitName = StringUtils.camelToSplitName(type.getSimpleName(), "."); + value = new String[]{splitName}; + } + + boolean hasInvocation = false; + for (int i = 0; i < pts.length; ++i) { + if (("org.apache.dubbo.rpc.Invocation").equals(pts[i].getName())) { + // Null Point check + String s = String.format("\nif (arg%d == null) throw new IllegalArgumentException(\"invocation == null\");", i); + code.append(s); + s = String.format("\nString methodName = arg%d.getMethodName();", i); + code.append(s); + hasInvocation = true; + break; + } + } + + String defaultExtName = cachedDefaultName; + String getNameCode = null; + for (int i = value.length - 1; i >= 0; --i) { + if (i == value.length - 1) { + if (null != defaultExtName) { + if (!"protocol".equals(value[i])) { + if (hasInvocation) { + getNameCode = String.format("url.getMethodParameter(methodName, \"%s\", \"%s\")", value[i], defaultExtName); + } else { + getNameCode = String.format("url.getParameter(\"%s\", \"%s\")", value[i], defaultExtName); + } + } else { + getNameCode = String.format("( url.getProtocol() == null ? \"%s\" : url.getProtocol() )", defaultExtName); + } + } else { + if (!"protocol".equals(value[i])) { + if (hasInvocation) { + getNameCode = String.format("url.getMethodParameter(methodName, \"%s\", \"%s\")", value[i], defaultExtName); + } else { + getNameCode = String.format("url.getParameter(\"%s\")", value[i]); + } + } else { + getNameCode = "url.getProtocol()"; + } + } + } else { + if (!"protocol".equals(value[i])) { + if (hasInvocation) { + getNameCode = String.format("url.getMethodParameter(methodName, \"%s\", \"%s\")", value[i], defaultExtName); + } else { + getNameCode = String.format("url.getParameter(\"%s\", %s)", value[i], getNameCode); + } + } else { + getNameCode = String.format("url.getProtocol() == null ? (%s) : url.getProtocol()", getNameCode); + } + } + } + code.append("\nString extName = ").append(getNameCode).append(";"); + // check extName == null? + String s = String.format("\nif(extName == null) " + + "throw new IllegalStateException(\"Fail to get extension(%s) name from url(\" + url.toString() + \") use keys(%s)\");", + type.getName(), Arrays.toString(value)); + code.append(s); + + s = String.format("\n%s extension = (% 0) { + codeBuilder.append(", "); + } + codeBuilder.append(pts[i].getCanonicalName()); + codeBuilder.append(" "); + codeBuilder.append("arg").append(i); + } + codeBuilder.append(")"); + if (ets.length > 0) { + codeBuilder.append(" throws "); + for (int i = 0; i < ets.length; i++) { + if (i > 0) { + codeBuilder.append(", "); + } + codeBuilder.append(ets[i].getCanonicalName()); + } + } + codeBuilder.append(" {"); + codeBuilder.append(code.toString()); + codeBuilder.append("\n}"); + } + codeBuilder.append("\n}"); + if (logger.isDebugEnabled()) { + logger.debug(codeBuilder.toString()); + } + return codeBuilder.toString(); + } + @Override public String toString() { return this.getClass().getName() + "[" + type.getName() + "]"; diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/support/ActivateComparator.java b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/support/ActivateComparator.java index 5dcedc38cc4..766099fafa7 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/support/ActivateComparator.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/support/ActivateComparator.java @@ -19,9 +19,7 @@ import org.apache.dubbo.common.extension.Activate; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.extension.SPI; -import org.apache.dubbo.common.utils.ArrayUtils; -import java.util.Arrays; import java.util.Comparator; /** @@ -46,92 +44,75 @@ public int compare(Object o1, Object o2) { return 0; } - Class inf = findSpi(o1.getClass()); + // to support com.alibab.dubbo.common.extension.Activate + String[] a1Before, a2Before, a1After, a2After; + int a1Order, a2Order; + Class inf = null; + if (o1.getClass().getInterfaces().length > 0) { + inf = o1.getClass().getInterfaces()[0]; - ActivateInfo a1 = parseActivate(o1.getClass()); - ActivateInfo a2 = parseActivate(o2.getClass()); + if (inf.getInterfaces().length > 0) { + inf = inf.getInterfaces()[0]; + } + } - if ((a1.applicableToCompare() || a2.applicableToCompare()) && inf != null) { + Activate a1 = o1.getClass().getAnnotation(Activate.class); + if (a1 != null) { + a1Before = a1.before(); + a1After = a1.after(); + a1Order = a1.order(); + } else { + com.alibaba.dubbo.common.extension.Activate oa1 = o1.getClass().getAnnotation(com.alibaba.dubbo.common.extension.Activate.class); + a1Before = oa1.before(); + a1After = oa1.after(); + a1Order = oa1.order(); + } + Activate a2 = o2.getClass().getAnnotation(Activate.class); + if (a2 != null) { + a2Before = a2.before(); + a2After = a2.after(); + a2Order = a2.order(); + } else { + com.alibaba.dubbo.common.extension.Activate oa2 = o2.getClass().getAnnotation(com.alibaba.dubbo.common.extension.Activate.class); + a2Before = oa2.before(); + a2After = oa2.after(); + a2Order = oa2.order(); + } + if ((a1Before.length > 0 || a1After.length > 0 + || a2Before.length > 0 || a2After.length > 0) + && inf != null && inf.isAnnotationPresent(SPI.class)) { ExtensionLoader extensionLoader = ExtensionLoader.getExtensionLoader(inf); - if (a1.applicableToCompare()) { + if (a1Before.length > 0 || a1After.length > 0) { String n2 = extensionLoader.getExtensionName(o2.getClass()); - if (a1.isLess(n2)) { - return -1; + for (String before : a1Before) { + if (before.equals(n2)) { + return -1; + } } - - if (a1.isMore(n2)) { - return 1; + for (String after : a1After) { + if (after.equals(n2)) { + return 1; + } } } - - if (a2.applicableToCompare()) { + if (a2Before.length > 0 || a2After.length > 0) { String n1 = extensionLoader.getExtensionName(o1.getClass()); - if (a2.isLess(n1)) { - return 1; + for (String before : a2Before) { + if (before.equals(n1)) { + return 1; + } } - - if (a2.isMore(n1)) { - return -1; + for (String after : a2After) { + if (after.equals(n1)) { + return -1; + } } } } - int n1 = a1 == null ? 0 : a1.order; - int n2 = a2 == null ? 0 : a2.order; + int n1 = a1 == null ? 0 : a1Order; + int n2 = a2 == null ? 0 : a2Order; // never return 0 even if n1 equals n2, otherwise, o1 and o2 will override each other in collection like HashSet return n1 > n2 ? 1 : -1; } - private Class findSpi(Class clazz) { - if (clazz.getInterfaces().length <= 0) { - return null; - } - - for (Class intf : clazz.getInterfaces()) { - if (intf.isAnnotationPresent(SPI.class)) { - return intf; - } else { - Class result = findSpi(intf); - if (result != null) { - return result; - } - } - } - - return null; - } - - private ActivateInfo parseActivate(Class clazz) { - ActivateInfo info = new ActivateInfo(); - if (clazz.isAnnotationPresent(Activate.class)) { - Activate activate = clazz.getAnnotation(Activate.class); - info.before = activate.before(); - info.after = activate.after(); - info.order = activate.order(); - } else { - com.alibaba.dubbo.common.extension.Activate activate = clazz.getAnnotation( - com.alibaba.dubbo.common.extension.Activate.class); - info.before = activate.before(); - info.after = activate.after(); - info.order = activate.order(); - } - return info; - } - - private static class ActivateInfo { - private String[] before; - private String[] after; - private int order; - - private boolean applicableToCompare() { - return ArrayUtils.isNotEmpty(before) || ArrayUtils.isNotEmpty(after); - } - - private boolean isLess(String name) { - return Arrays.asList(before).contains(name); - } - - private boolean isMore(String name) { - return Arrays.asList(after).contains(name); - } - } } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/json/JSON.java b/dubbo-common/src/main/java/org/apache/dubbo/common/json/JSON.java index 0903cfbc869..1d3aa8891d2 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/json/JSON.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/json/JSON.java @@ -579,7 +579,6 @@ private static Object parse(Reader reader, JSONVisitor handler, int expect) thro handler.objectItemValue(value, false); break; } - default: } } break; @@ -630,7 +629,6 @@ private static Object parse(Reader reader, JSONVisitor handler, int expect) thro handler.objectItemValue(value, false); break; } - default: } } break; diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/json/JSONWriter.java b/dubbo-common/src/main/java/org/apache/dubbo/common/json/JSONWriter.java index 6c6bf09c0fe..094832134c0 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/json/JSONWriter.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/json/JSONWriter.java @@ -275,7 +275,6 @@ private void beforeValue() throws IOException { case OBJECT_VALUE: mState.type = OBJECT; return; - default: } } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/json/Yylex.java b/dubbo-common/src/main/java/org/apache/dubbo/common/json/Yylex.java index 822175e6031..8abb42d1ab2 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/json/Yylex.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/json/Yylex.java @@ -837,7 +837,6 @@ public JSONToken yylex() throws java.io.IOException, ParseException { return null; } else { zzScanError(ZZ_NO_MATCH); - break; } } } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/logger/Logger.java b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/Logger.java index c505326d0c3..3b78d04542c 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/logger/Logger.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/Logger.java @@ -28,14 +28,14 @@ public interface Logger { * * @param msg log this message */ - void trace(String msg); + public void trace(String msg); /** * Logs an error with trace log level. * * @param e log this cause */ - void trace(Throwable e); + public void trace(Throwable e); /** * Logs an error with trace log level. @@ -43,21 +43,21 @@ public interface Logger { * @param msg log this message * @param e log this cause */ - void trace(String msg, Throwable e); + public void trace(String msg, Throwable e); /** * Logs a message with debug log level. * * @param msg log this message */ - void debug(String msg); + public void debug(String msg); /** * Logs an error with debug log level. * * @param e log this cause */ - void debug(Throwable e); + public void debug(Throwable e); /** * Logs an error with debug log level. @@ -65,21 +65,21 @@ public interface Logger { * @param msg log this message * @param e log this cause */ - void debug(String msg, Throwable e); + public void debug(String msg, Throwable e); /** * Logs a message with info log level. * * @param msg log this message */ - void info(String msg); + public void info(String msg); /** * Logs an error with info log level. * * @param e log this cause */ - void info(Throwable e); + public void info(Throwable e); /** * Logs an error with info log level. @@ -87,21 +87,21 @@ public interface Logger { * @param msg log this message * @param e log this cause */ - void info(String msg, Throwable e); + public void info(String msg, Throwable e); /** * Logs a message with warn log level. * * @param msg log this message */ - void warn(String msg); + public void warn(String msg); /** * Logs a message with warn log level. * * @param e log this message */ - void warn(Throwable e); + public void warn(Throwable e); /** * Logs a message with warn log level. @@ -109,21 +109,21 @@ public interface Logger { * @param msg log this message * @param e log this cause */ - void warn(String msg, Throwable e); + public void warn(String msg, Throwable e); /** * Logs a message with error log level. * * @param msg log this message */ - void error(String msg); + public void error(String msg); /** * Logs an error with error log level. * * @param e log this cause */ - void error(Throwable e); + public void error(Throwable e); /** * Logs an error with error log level. @@ -131,41 +131,41 @@ public interface Logger { * @param msg log this message * @param e log this cause */ - void error(String msg, Throwable e); + public void error(String msg, Throwable e); /** * Is trace logging currently enabled? * * @return true if trace is enabled */ - boolean isTraceEnabled(); + public boolean isTraceEnabled(); /** * Is debug logging currently enabled? * * @return true if debug is enabled */ - boolean isDebugEnabled(); + public boolean isDebugEnabled(); /** * Is info logging currently enabled? * * @return true if info is enabled */ - boolean isInfoEnabled(); + public boolean isInfoEnabled(); /** * Is warn logging currently enabled? * * @return true if warn is enabled */ - boolean isWarnEnabled(); + public boolean isWarnEnabled(); /** * Is error logging currently enabled? * * @return true if error is enabled */ - boolean isErrorEnabled(); + public boolean isErrorEnabled(); } \ No newline at end of file diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/logger/LoggerFactory.java b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/LoggerFactory.java index 41d1632eacc..ec98755000b 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/logger/LoggerFactory.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/LoggerFactory.java @@ -25,8 +25,6 @@ import org.apache.dubbo.common.logger.support.FailsafeLogger; import java.io.File; -import java.util.Arrays; -import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -36,43 +34,40 @@ */ public class LoggerFactory { - private static final ConcurrentMap LOGGERS = new ConcurrentHashMap<>(); + private static final ConcurrentMap LOGGERS = new ConcurrentHashMap(); private static volatile LoggerAdapter LOGGER_ADAPTER; // search common-used logging frameworks static { - String logger = System.getProperty("dubbo.application.logger", ""); - switch (logger) { - case "slf4j": - setLoggerAdapter(new Slf4jLoggerAdapter()); - break; - case "jcl": - setLoggerAdapter(new JclLoggerAdapter()); - break; - case "log4j": + String logger = System.getProperty("dubbo.application.logger"); + if ("slf4j".equals(logger)) { + setLoggerAdapter(new Slf4jLoggerAdapter()); + } else if ("jcl".equals(logger)) { + setLoggerAdapter(new JclLoggerAdapter()); + } else if ("log4j".equals(logger)) { + setLoggerAdapter(new Log4jLoggerAdapter()); + } else if ("jdk".equals(logger)) { + setLoggerAdapter(new JdkLoggerAdapter()); + } else if ("log4j2".equals(logger)) { + setLoggerAdapter(new Log4j2LoggerAdapter()); + } else { + try { setLoggerAdapter(new Log4jLoggerAdapter()); - break; - case "jdk": - setLoggerAdapter(new JdkLoggerAdapter()); - break; - case "log4j2": - setLoggerAdapter(new Log4j2LoggerAdapter()); - break; - default: - List> candidates = Arrays.asList( - Log4jLoggerAdapter.class, - Slf4jLoggerAdapter.class, - Log4j2LoggerAdapter.class, - JclLoggerAdapter.class, - JdkLoggerAdapter.class - ); - for (Class clazz : candidates) { + } catch (Throwable e1) { + try { + setLoggerAdapter(new Slf4jLoggerAdapter()); + } catch (Throwable e2) { try { - setLoggerAdapter(clazz.newInstance()); - break; - } catch (Throwable ignored) { + setLoggerAdapter(new Log4j2LoggerAdapter()); + } catch (Throwable e3) { + try { + setLoggerAdapter(new JclLoggerAdapter()); + } catch (Throwable e4) { + setLoggerAdapter(new JdkLoggerAdapter()); + } } } + } } } @@ -108,7 +103,12 @@ public static void setLoggerAdapter(LoggerAdapter loggerAdapter) { * @return logger */ public static Logger getLogger(Class key) { - return LOGGERS.computeIfAbsent(key.getName(), name -> new FailsafeLogger(LOGGER_ADAPTER.getLogger(name))); + FailsafeLogger logger = LOGGERS.get(key.getName()); + if (logger == null) { + LOGGERS.putIfAbsent(key.getName(), new FailsafeLogger(LOGGER_ADAPTER.getLogger(key))); + logger = LOGGERS.get(key.getName()); + } + return logger; } /** @@ -118,7 +118,12 @@ public static Logger getLogger(Class key) { * @return logger provider */ public static Logger getLogger(String key) { - return LOGGERS.computeIfAbsent(key, k -> new FailsafeLogger(LOGGER_ADAPTER.getLogger(k))); + FailsafeLogger logger = LOGGERS.get(key); + if (logger == null) { + LOGGERS.putIfAbsent(key, new FailsafeLogger(LOGGER_ADAPTER.getLogger(key))); + logger = LOGGERS.get(key); + } + return logger; } /** @@ -148,4 +153,4 @@ public static File getFile() { return LOGGER_ADAPTER.getFile(); } -} +} \ No newline at end of file diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java index 8a137613f17..7aef31ca94f 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java @@ -24,13 +24,13 @@ import java.io.File; import java.io.FileOutputStream; +import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.Executors; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.Semaphore; import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.ExecutorService; /** * Abort Policy. @@ -78,34 +78,43 @@ private void dumpJStack() { return; } - ExecutorService pool = Executors.newSingleThreadExecutor(); - pool.execute(() -> { - String dumpPath = url.getParameter(Constants.DUMP_DIRECTORY, System.getProperty("user.home")); - - SimpleDateFormat sdf; - - String os = System.getProperty("os.name").toLowerCase(); - - // window system don't support ":" in file name - if (os.contains("win")) { - sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); - } else { - sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss"); - } - - String dateStr = sdf.format(new Date()); - //try-with-resources - try (FileOutputStream jStackStream = new FileOutputStream(new File(dumpPath, "Dubbo_JStack.log" + "." + dateStr))) { - JVMUtil.jstack(jStackStream); - } catch (Throwable t) { - logger.error("dump jStack error", t); - } finally { - guard.release(); + Executors.newSingleThreadExecutor().execute(new Runnable() { + @Override + public void run() { + String dumpPath = url.getParameter(Constants.DUMP_DIRECTORY, System.getProperty("user.home")); + + SimpleDateFormat sdf; + + String OS = System.getProperty("os.name").toLowerCase(); + + // window system don't support ":" in file name + if(OS.contains("win")){ + sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); + }else { + sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss"); + } + + String dateStr = sdf.format(new Date()); + FileOutputStream jstackStream = null; + try { + jstackStream = new FileOutputStream(new File(dumpPath, "Dubbo_JStack.log" + "." + dateStr)); + JVMUtil.jstack(jstackStream); + } catch (Throwable t) { + logger.error("dump jstack error", t); + } finally { + guard.release(); + if (jstackStream != null) { + try { + jstackStream.flush(); + jstackStream.close(); + } catch (IOException e) { + } + } + } + + lastPrintTime = System.currentTimeMillis(); } - lastPrintTime = System.currentTimeMillis(); }); - //must shutdown thread pool ,if not will lead to OOM - pool.shutdown(); } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/timer/HashedWheelTimer.java b/dubbo-common/src/main/java/org/apache/dubbo/common/timer/HashedWheelTimer.java index 3abeb660a64..f786caeb8cc 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/timer/HashedWheelTimer.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/timer/HashedWheelTimer.java @@ -25,9 +25,9 @@ import java.util.Locale; import java.util.Queue; import java.util.Set; +import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executors; -import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; @@ -109,8 +109,8 @@ public class HashedWheelTimer implements Timer { private final HashedWheelBucket[] wheel; private final int mask; private final CountDownLatch startTimeInitialized = new CountDownLatch(1); - private final Queue timeouts = new LinkedBlockingQueue<>(); - private final Queue cancelledTimeouts = new LinkedBlockingQueue<>(); + private final Queue timeouts = new ArrayBlockingQueue(1024); + private final Queue cancelledTimeouts = new ArrayBlockingQueue(1024); private final AtomicLong pendingTimeouts = new AtomicLong(0); private final long maxPendingTimeouts; @@ -290,13 +290,11 @@ private static HashedWheelBucket[] createWheel(int ticksPerWheel) { } private static int normalizeTicksPerWheel(int ticksPerWheel) { - int normalizedTicksPerWheel = ticksPerWheel - 1; - normalizedTicksPerWheel |= normalizedTicksPerWheel >>> 1; - normalizedTicksPerWheel |= normalizedTicksPerWheel >>> 2; - normalizedTicksPerWheel |= normalizedTicksPerWheel >>> 4; - normalizedTicksPerWheel |= normalizedTicksPerWheel >>> 8; - normalizedTicksPerWheel |= normalizedTicksPerWheel >>> 16; - return normalizedTicksPerWheel + 1; + int normalizedTicksPerWheel = 1; + while (normalizedTicksPerWheel < ticksPerWheel) { + normalizedTicksPerWheel <<= 1; + } + return normalizedTicksPerWheel; } /** diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CIDRUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CIDRUtils.java deleted file mode 100644 index 5d9dc8bcbdf..00000000000 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CIDRUtils.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * The MIT License - * - * Copyright (c) 2013 Edin Dazdarevic (edin.dazdarevic@gmail.com) - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - **/ -package org.apache.dubbo.common.utils; - -import java.math.BigInteger; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.List; - -/** - * A class that enables to get an IP range from CIDR specification. It supports - * both IPv4 and IPv6. - *

- * From https://github.com/edazdarevic/CIDRUtils/blob/master/CIDRUtils.java - */ -public class CIDRUtils { - private final String cidr; - - private InetAddress inetAddress; - private InetAddress startAddress; - private InetAddress endAddress; - private final int prefixLength; - - - public CIDRUtils(String cidr) throws UnknownHostException { - - this.cidr = cidr; - - /* split CIDR to address and prefix part */ - if (this.cidr.contains("/")) { - int index = this.cidr.indexOf("/"); - String addressPart = this.cidr.substring(0, index); - String networkPart = this.cidr.substring(index + 1); - - inetAddress = InetAddress.getByName(addressPart); - prefixLength = Integer.parseInt(networkPart); - - calculate(); - } else { - throw new IllegalArgumentException("not an valid CIDR format!"); - } - } - - - private void calculate() throws UnknownHostException { - - ByteBuffer maskBuffer; - int targetSize; - if (inetAddress.getAddress().length == 4) { - maskBuffer = - ByteBuffer - .allocate(4) - .putInt(-1); - targetSize = 4; - } else { - maskBuffer = ByteBuffer.allocate(16) - .putLong(-1L) - .putLong(-1L); - targetSize = 16; - } - - BigInteger mask = (new BigInteger(1, maskBuffer.array())).not().shiftRight(prefixLength); - - ByteBuffer buffer = ByteBuffer.wrap(inetAddress.getAddress()); - BigInteger ipVal = new BigInteger(1, buffer.array()); - - BigInteger startIp = ipVal.and(mask); - BigInteger endIp = startIp.add(mask.not()); - - byte[] startIpArr = toBytes(startIp.toByteArray(), targetSize); - byte[] endIpArr = toBytes(endIp.toByteArray(), targetSize); - - this.startAddress = InetAddress.getByAddress(startIpArr); - this.endAddress = InetAddress.getByAddress(endIpArr); - - } - - private byte[] toBytes(byte[] array, int targetSize) { - int counter = 0; - List newArr = new ArrayList(); - while (counter < targetSize && (array.length - 1 - counter >= 0)) { - newArr.add(0, array[array.length - 1 - counter]); - counter++; - } - - int size = newArr.size(); - for (int i = 0; i < (targetSize - size); i++) { - - newArr.add(0, (byte) 0); - } - - byte[] ret = new byte[newArr.size()]; - for (int i = 0; i < newArr.size(); i++) { - ret[i] = newArr.get(i); - } - return ret; - } - - public String getNetworkAddress() { - - return this.startAddress.getHostAddress(); - } - - public String getBroadcastAddress() { - return this.endAddress.getHostAddress(); - } - - public boolean isInRange(String ipAddress) throws UnknownHostException { - InetAddress address = InetAddress.getByName(ipAddress); - BigInteger start = new BigInteger(1, this.startAddress.getAddress()); - BigInteger end = new BigInteger(1, this.endAddress.getAddress()); - BigInteger target = new BigInteger(1, address.getAddress()); - - int st = start.compareTo(target); - int te = target.compareTo(end); - - return (st == -1 || st == 0) && (te == -1 || te == 0); - } -} diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ClassHelper.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ClassHelper.java index d249645c2bf..b5aa79f2fcc 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ClassHelper.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ClassHelper.java @@ -16,7 +16,6 @@ */ package org.apache.dubbo.common.utils; - import java.lang.reflect.Array; import java.lang.reflect.Method; import java.lang.reflect.Modifier; @@ -105,7 +104,8 @@ public static ClassLoader getClassLoader(Class clazz) { // getClassLoader() returning null indicates the bootstrap ClassLoader try { cl = ClassLoader.getSystemClassLoader(); - } catch (Throwable ex) { + } + catch (Throwable ex) { // Cannot access system ClassLoader - oh well, maybe the caller can live with null... } } @@ -265,34 +265,26 @@ public static boolean isPrimitive(Class type) { } public static Object convertPrimitive(Class type, String value) { - if (value == null) { - return null; - } else if (type == char.class || type == Character.class) { + if (type == char.class || type == Character.class) { return value.length() > 0 ? value.charAt(0) : '\0'; } else if (type == boolean.class || type == Boolean.class) { return Boolean.valueOf(value); - } - try { - if (type == byte.class || type == Byte.class) { - return Byte.valueOf(value); - } else if (type == short.class || type == Short.class) { - return Short.valueOf(value); - } else if (type == int.class || type == Integer.class) { - return Integer.valueOf(value); - } else if (type == long.class || type == Long.class) { - return Long.valueOf(value); - } else if (type == float.class || type == Float.class) { - return Float.valueOf(value); - } else if (type == double.class || type == Double.class) { - return Double.valueOf(value); - } - } catch (NumberFormatException e) { - return null; + } else if (type == byte.class || type == Byte.class) { + return Byte.valueOf(value); + } else if (type == short.class || type == Short.class) { + return Short.valueOf(value); + } else if (type == int.class || type == Integer.class) { + return Integer.valueOf(value); + } else if (type == long.class || type == Long.class) { + return Long.valueOf(value); + } else if (type == float.class || type == Float.class) { + return Float.valueOf(value); + } else if (type == double.class || type == Double.class) { + return Double.valueOf(value); } return value; } - /** * We only check boolean value at this moment. * diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CollectionUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CollectionUtils.java index 40baecdd2db..37271087c99 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CollectionUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CollectionUtils.java @@ -55,7 +55,7 @@ private CollectionUtils() { @SuppressWarnings({"unchecked", "rawtypes"}) public static List sort(List list) { - if (isNotEmpty(list)) { + if (list != null && !list.isEmpty()) { Collections.sort((List) list); } return list; @@ -120,7 +120,7 @@ public static List join(Map map, String separator) { for (Map.Entry entry : map.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { list.add(key); } else { list.add(key + separator + value); @@ -206,15 +206,7 @@ public static boolean isEmpty(Collection collection) { } public static boolean isNotEmpty(Collection collection) { - return !isEmpty(collection); - } - - public static boolean isEmptyMap(Map map) { - return map == null || map.size() == 0; - } - - public static boolean isNotEmptyMap(Map map) { - return !isEmptyMap(map); + return collection != null && !collection.isEmpty(); } } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CompatibleTypeUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CompatibleTypeUtils.java index efc570fe925..de96eebda18 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CompatibleTypeUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CompatibleTypeUtils.java @@ -104,10 +104,15 @@ public static Object compatibleTypeConvert(Object value, Class type) { // Process string to char array for generic invoke // See // - https://github.com/apache/incubator-dubbo/issues/2003 - int len = string.length(); - char[] chars = new char[len]; - string.getChars(0, len, chars, 0); - return chars; + if (string == null) { + return null; + } + else { + int len = string.length(); + char[] chars = new char[len]; + string.getChars(0, len, chars, 0); + return chars; + } } } else if (value instanceof Number) { Number number = (Number) value; diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ConfigUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ConfigUtils.java index 0c68512c20e..93013569c45 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ConfigUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ConfigUtils.java @@ -50,7 +50,7 @@ public static boolean isNotEmpty(String value) { } public static boolean isEmpty(String value) { - return StringUtils.isEmpty(value) + return value == null || value.length() == 0 || "false".equalsIgnoreCase(value) || "0".equalsIgnoreCase(value) || "null".equalsIgnoreCase(value) @@ -190,7 +190,7 @@ public static String getProperty(String key, String defaultValue) { */ public static String getSystemProperty(String key) { String value = System.getenv(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { value = System.getProperty(key); } return value; diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/DubboAppender.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/DubboAppender.java index e2b1940a7e4..da3968e9f6e 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/DubboAppender.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/DubboAppender.java @@ -16,24 +16,17 @@ */ package org.apache.dubbo.common.utils; -import org.apache.log4j.FileAppender; +import org.apache.log4j.ConsoleAppender; import org.apache.log4j.spi.LoggingEvent; import java.util.ArrayList; import java.util.List; -public class DubboAppender extends FileAppender { - - private static final String DEFAULT_FILE_NAME = "dubbo.log"; - - public DubboAppender() { - super(); - setFile(DEFAULT_FILE_NAME); - } +public class DubboAppender extends ConsoleAppender { public static boolean available = false; - public static List logList = new ArrayList<>(); + public static List logList = new ArrayList(); public static void doStart() { available = true; diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ExecutorUtil.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ExecutorUtil.java index 14408dacf64..d33af364247 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ExecutorUtil.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ExecutorUtil.java @@ -26,7 +26,6 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import java.util.concurrent.ScheduledFuture; public class ExecutorUtil { private static final Logger logger = LoggerFactory.getLogger(ExecutorUtil.class); @@ -46,10 +45,9 @@ public static boolean isTerminated(Executor executor) { /** * Use the shutdown pattern from: - * https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html - * + * https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html * @param executor the Executor to shutdown - * @param timeout the timeout in milliseconds before termination + * @param timeout the timeout in milliseconds before termination */ public static void gracefulShutdown(Executor executor, int timeout) { if (!(executor instanceof ExecutorService) || isTerminated(executor)) { @@ -133,11 +131,4 @@ public static URL setThreadName(URL url, String defaultName) { url = url.addParameter(Constants.THREAD_NAME_KEY, name); return url; } - - public static void cancelScheduledFuture(ScheduledFuture scheduledFuture) { - ScheduledFuture future = scheduledFuture; - if (future != null && !future.isCancelled()) { - future.cancel(true); - } - } } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java index 91f04b8a90d..29d79a30f0f 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java @@ -16,60 +16,62 @@ */ package org.apache.dubbo.common.utils; -import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import java.io.IOException; -import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; import java.net.InetSocketAddress; -import java.net.MulticastSocket; import java.net.NetworkInterface; import java.net.ServerSocket; import java.net.UnknownHostException; import java.util.Enumeration; import java.util.Map; -import java.util.Optional; -import java.util.concurrent.ThreadLocalRandom; +import java.util.Random; import java.util.regex.Pattern; /** * IP and Port Helper for RPC */ public class NetUtils { - private static final Logger logger = LoggerFactory.getLogger(NetUtils.class); - // returned port range is [30000, 39999] + public static final String LOCALHOST = "127.0.0.1"; + public static final String ANYHOST = "0.0.0.0"; + private static final Logger logger = LoggerFactory.getLogger(NetUtils.class); private static final int RND_PORT_START = 30000; + private static final int RND_PORT_RANGE = 10000; - // valid port range is (0, 65535] + private static final Random RANDOM = new Random(System.currentTimeMillis()); private static final int MIN_PORT = 0; private static final int MAX_PORT = 65535; - private static final Pattern ADDRESS_PATTERN = Pattern.compile("^\\d{1,3}(\\.\\d{1,3}){3}\\:\\d{1,5}$"); private static final Pattern LOCAL_IP_PATTERN = Pattern.compile("127(\\.\\d{1,3}){3}$"); private static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$"); - - private static final Map hostNameCache = new LRUCache<>(1000); + private static final Map hostNameCache = new LRUCache(1000); private static volatile InetAddress LOCAL_ADDRESS = null; - private static final String SPLIT_IPV4_CHARECTER = "\\."; - private static final String SPLIT_IPV6_CHARECTER = ":"; - public static int getRandomPort() { - return RND_PORT_START + ThreadLocalRandom.current().nextInt(RND_PORT_RANGE); + return RND_PORT_START + RANDOM.nextInt(RND_PORT_RANGE); } public static int getAvailablePort() { - try (ServerSocket ss = new ServerSocket()) { + ServerSocket ss = null; + try { + ss = new ServerSocket(); ss.bind(null); return ss.getLocalPort(); } catch (IOException e) { return getRandomPort(); + } finally { + if (ss != null) { + try { + ss.close(); + } catch (IOException e) { + } + } } } @@ -78,10 +80,19 @@ public static int getAvailablePort(int port) { return getAvailablePort(); } for (int i = port; i < MAX_PORT; i++) { - try (ServerSocket ss = new ServerSocket(i)) { + ServerSocket ss = null; + try { + ss = new ServerSocket(i); return i; } catch (IOException e) { // continue + } finally { + if (ss != null) { + try { + ss.close(); + } catch (IOException e) { + } + } } } return port; @@ -98,23 +109,21 @@ public static boolean isValidAddress(String address) { public static boolean isLocalHost(String host) { return host != null && (LOCAL_IP_PATTERN.matcher(host).matches() - || host.equalsIgnoreCase(Constants.LOCALHOST_KEY)); + || host.equalsIgnoreCase("localhost")); } public static boolean isAnyHost(String host) { - return Constants.ANYHOST_VALUE.equals(host); + return "0.0.0.0".equals(host); } - // FIXME: should remove this method completely public static boolean isInvalidLocalHost(String host) { return host == null || host.length() == 0 - || host.equalsIgnoreCase(Constants.LOCALHOST_KEY) - || host.equals(Constants.ANYHOST_VALUE) + || host.equalsIgnoreCase("localhost") + || host.equals("0.0.0.0") || (LOCAL_IP_PATTERN.matcher(host).matches()); } - // FIXME: should remove this method completely public static boolean isValidLocalHost(String host) { return !isInvalidLocalHost(host); } @@ -124,17 +133,19 @@ public static InetSocketAddress getLocalSocketAddress(String host, int port) { new InetSocketAddress(port) : new InetSocketAddress(host, port); } - static boolean isValidV4Address(InetAddress address) { + static boolean isValidAddress(InetAddress address) { + if (address == null || address.isLoopbackAddress()) { + return false; + } String name = address.getHostAddress(); return (name != null - && IP_PATTERN.matcher(name).matches() - && !Constants.ANYHOST_VALUE.equals(name) - && !Constants.LOCALHOST_VALUE.equals(name)); + && !ANYHOST.equals(name) + && !LOCALHOST.equals(name) + && IP_PATTERN.matcher(name).matches()); } /** * Check if an ipv6 address is reachable. - * * @param address the given address * @return true if it is reachable */ @@ -151,21 +162,16 @@ static boolean isValidV6Address(Inet6Address address) { return false; } - static boolean isValidPublicAddress(InetAddress address) { - return !address.isSiteLocalAddress() && !address.isLoopbackAddress(); - } - /** * normalize the ipv6 Address, convert scope name to scope id. * e.g. * convert - * fe80:0:0:0:894:aeec:f37d:23e1%en0 + * fe80:0:0:0:894:aeec:f37d:23e1%en0 * to - * fe80:0:0:0:894:aeec:f37d:23e1%5 - *

+ * fe80:0:0:0:894:aeec:f37d:23e1%5 + * * The %5 after ipv6 address is called scope id. * see java doc of {@link Inet6Address} for more details. - * * @param address the input address * @return the normalized address, with scope id converted to int */ @@ -185,7 +191,7 @@ static InetAddress normalizeV6Address(Inet6Address address) { public static String getLocalHost() { InetAddress address = getLocalAddress(); - return address == null ? Constants.LOCALHOST_VALUE : address.getHostAddress(); + return address == null ? LOCALHOST : address.getHostAddress(); } public static String filterLocalHost(String host) { @@ -224,33 +230,21 @@ public static InetAddress getLocalAddress() { return localAddress; } - private static Optional toValidAddress(InetAddress address) { - if (isValidPublicAddress(address)) { - if (address instanceof Inet6Address) { - Inet6Address v6Address = (Inet6Address) address; - if (isValidV6Address(v6Address)) { - return Optional.ofNullable(normalizeV6Address(v6Address)); - } - } - if (isValidV4Address(address)) { - return Optional.of(address); - } - } - return Optional.empty(); - } - private static InetAddress getLocalAddress0() { InetAddress localAddress = null; try { localAddress = InetAddress.getLocalHost(); - Optional addressOp = toValidAddress(localAddress); - if (addressOp.isPresent()) { - return addressOp.get(); + if (localAddress instanceof Inet6Address) { + Inet6Address address = (Inet6Address) localAddress; + if (isValidV6Address(address)){ + return normalizeV6Address(address); + } + } else if (isValidAddress(localAddress)) { + return localAddress; } } catch (Throwable e) { logger.warn(e); } - try { Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); if (null == interfaces) { @@ -262,9 +256,14 @@ private static InetAddress getLocalAddress0() { Enumeration addresses = network.getInetAddresses(); while (addresses.hasMoreElements()) { try { - Optional addressOp = toValidAddress(addresses.nextElement()); - if (addressOp.isPresent()) { - return addressOp.get(); + InetAddress address = addresses.nextElement(); + if (address instanceof Inet6Address) { + Inet6Address v6Address = (Inet6Address) address; + if (isValidV6Address(v6Address)){ + return normalizeV6Address(v6Address); + } + } else if (isValidAddress(address)) { + return address; } } catch (Throwable e) { logger.warn(e); @@ -343,164 +342,4 @@ public static String toURL(String protocol, String host, int port, String path) return sb.toString(); } - public static void joinMulticastGroup(MulticastSocket multicastSocket, InetAddress multicastAddress) throws IOException { - setInterface(multicastSocket, multicastAddress instanceof Inet6Address); - multicastSocket.setLoopbackMode(false); - multicastSocket.joinGroup(multicastAddress); - } - - public static void setInterface(MulticastSocket multicastSocket, boolean preferIpv6) throws IOException { - boolean interfaceSet = false; - Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); - while (interfaces.hasMoreElements()) { - NetworkInterface i = (NetworkInterface) interfaces.nextElement(); - Enumeration addresses = i.getInetAddresses(); - while (addresses.hasMoreElements()) { - InetAddress address = (InetAddress) addresses.nextElement(); - if (preferIpv6 && address instanceof Inet6Address) { - multicastSocket.setInterface(address); - interfaceSet = true; - break; - } else if (!preferIpv6 && address instanceof Inet4Address) { - multicastSocket.setInterface(address); - interfaceSet = true; - break; - } - } - if (interfaceSet) { - break; - } - } - } - - public static boolean matchIpExpression(String pattern, String host, int port) throws UnknownHostException { - - // if the pattern is subnet format, it will not be allowed to config port param in pattern. - if (pattern.contains("/")) { - CIDRUtils utils = new CIDRUtils(pattern); - return utils.isInRange(host); - } - - - return matchIpRange(pattern, host, port); - } - - /** - * @param pattern - * @param host - * @param port - * @return - * @throws UnknownHostException - */ - public static boolean matchIpRange(String pattern, String host, int port) throws UnknownHostException { - if (pattern == null || host == null) { - throw new IllegalArgumentException("Illegal Argument pattern or hostName. Pattern:" + pattern + ", Host:" + host); - } - pattern = pattern.trim(); - if (pattern.equals("*.*.*.*") || pattern.equals("*")) { - return true; - } - - InetAddress inetAddress = InetAddress.getByName(host); - boolean isIpv4 = isValidV4Address(inetAddress) ? true : false; - String[] hostAndPort = getPatternHostAndPort(pattern, isIpv4); - if (hostAndPort[1] != null && !hostAndPort[1].equals(String.valueOf(port))) { - return false; - } - pattern = hostAndPort[0]; - - String splitCharacter = SPLIT_IPV4_CHARECTER; - if (!isIpv4) { - splitCharacter = SPLIT_IPV6_CHARECTER; - } - String[] mask = pattern.split(splitCharacter); - //check format of pattern - checkHostPattern(pattern, mask, isIpv4); - - host = inetAddress.getHostAddress(); - - String[] ip_address = host.split(splitCharacter); - if (pattern.equals(host)) { - return true; - } - // short name condition - if (!ipPatternContainExpression(pattern)) { - InetAddress patternAddress = InetAddress.getByName(pattern); - if (patternAddress.getHostAddress().equals(host)) { - return true; - } else { - return false; - } - } - for (int i = 0; i < mask.length; i++) { - if (mask[i].equals("*") || mask[i].equals(ip_address[i])) { - continue; - } else if (mask[i].contains("-")) { - String[] rangeNumStrs = mask[i].split("-"); - if (rangeNumStrs.length != 2) { - throw new IllegalArgumentException("There is wrong format of ip Address: " + mask[i]); - } - Integer min = getNumOfIpSegment(rangeNumStrs[0], isIpv4); - Integer max = getNumOfIpSegment(rangeNumStrs[1], isIpv4); - Integer ip = getNumOfIpSegment(ip_address[i], isIpv4); - if (ip < min || ip > max) { - return false; - } - } else if ("0".equals(ip_address[i]) && ("0".equals(mask[i]) || "00".equals(mask[i]) || "000".equals(mask[i]) || "0000".equals(mask[i]))) { - continue; - } else if (!mask[i].equals(ip_address[i])) { - return false; - } - } - return true; - } - - private static boolean ipPatternContainExpression(String pattern) { - return pattern.contains("*") || pattern.contains("-"); - } - - private static void checkHostPattern(String pattern, String[] mask, boolean isIpv4) { - if (!isIpv4) { - if (mask.length != 8 && ipPatternContainExpression(pattern)) { - throw new IllegalArgumentException("If you config ip expression that contains '*' or '-', please fill qulified ip pattern like 234e:0:4567:0:0:0:3d:*. "); - } - if (mask.length != 8 && !pattern.contains("::")) { - throw new IllegalArgumentException("The host is ipv6, but the pattern is not ipv6 pattern : " + pattern); - } - } else { - if (mask.length != 4) { - throw new IllegalArgumentException("The host is ipv4, but the pattern is not ipv4 pattern : " + pattern); - } - } - } - - private static String[] getPatternHostAndPort(String pattern, boolean isIpv4) { - String[] result = new String[2]; - if (pattern.startsWith("[") && pattern.contains("]:")) { - int end = pattern.indexOf("]:"); - result[0] = pattern.substring(1, end); - result[1] = pattern.substring(end + 2); - return result; - } else if (pattern.startsWith("[") && pattern.endsWith("]")) { - result[0] = pattern.substring(1, pattern.length() - 1); - result[1] = null; - return result; - } else if (isIpv4 && pattern.contains(":")) { - int end = pattern.indexOf(":"); - result[0] = pattern.substring(0, end); - result[1] = pattern.substring(end + 1); - return result; - } else { - result[0] = pattern; - return result; - } - } - - private static Integer getNumOfIpSegment(String ipSegment, boolean isIpv4) { - if (isIpv4) { - return Integer.parseInt(ipSegment); - } - return Integer.parseInt(ipSegment, 16); - } - -} +} \ No newline at end of file diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java index 18998dc6239..e9a9c63201a 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java @@ -16,9 +16,6 @@ */ package org.apache.dubbo.common.utils; -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; - import java.lang.reflect.Array; import java.lang.reflect.Constructor; import java.lang.reflect.Field; @@ -60,7 +57,6 @@ */ public class PojoUtils { - private static final Logger logger = LoggerFactory.getLogger(PojoUtils.class); private static final ConcurrentMap NAME_METHODS_CACHE = new ConcurrentHashMap(); private static final ConcurrentMap, ConcurrentMap> CLASS_FIELD_CACHE = new ConcurrentHashMap, ConcurrentMap>(); @@ -461,10 +457,9 @@ private static Object realize0(Object pojo, Class type, Type genericType, fin try { method.invoke(dest, value); } catch (Exception e) { - String exceptionDescription = "Failed to set pojo " + dest.getClass().getSimpleName() + " property " + name - + " value " + value + "(" + value.getClass() + "), cause: " + e.getMessage(); - logger.error(exceptionDescription, e); - throw new RuntimeException(exceptionDescription, e); + e.printStackTrace(); + throw new RuntimeException("Failed to set pojo " + dest.getClass().getSimpleName() + " property " + name + + " value " + value + "(" + value.getClass() + "), cause: " + e.getMessage(), e); } } else if (field != null) { value = realize0(value, field.getType(), field.getGenericType(), history); @@ -520,14 +515,7 @@ private static Object newInstance(Class cls) { } catch (Throwable t) { try { Constructor[] constructors = cls.getDeclaredConstructors(); - /** - * From Javadoc java.lang.Class#getDeclaredConstructors - * This method returns an array of Constructor objects reflecting all the constructors - * declared by the class represented by this Class object. - * This method returns an array of length 0, - * if this Class object represents an interface, a primitive type, an array class, or void. - */ - if (constructors.length == 0) { + if (constructors != null && constructors.length == 0) { throw new RuntimeException("Illegal constructor: " + cls.getName()); } Constructor constructor = constructors[0]; diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/StringUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/StringUtils.java index a351346ef1a..f599218f410 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/StringUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/StringUtils.java @@ -55,9 +55,10 @@ private StringUtils() { * Gets a CharSequence length or {@code 0} if the CharSequence is * {@code null}. * - * @param cs a CharSequence or {@code null} + * @param cs + * a CharSequence or {@code null} * @return CharSequence length or {@code 0} if the CharSequence is - * {@code null}. + * {@code null}. */ public static int length(final CharSequence cs) { return cs == null ? 0 : cs.length(); @@ -76,10 +77,10 @@ public static int length(final CharSequence cs) { * StringUtils.repeat("a", -2) = "" * * - * @param str the String to repeat, may be null - * @param repeat number of times to repeat str, negative treated as zero + * @param str the String to repeat, may be null + * @param repeat number of times to repeat str, negative treated as zero * @return a new String consisting of the original String repeated, - * {@code null} if null String input + * {@code null} if null String input */ public static String repeat(final String str, final int repeat) { // Performance tuned for 2.0 (JDK1.4) @@ -100,9 +101,9 @@ public static String repeat(final String str, final int repeat) { final int outputLength = inputLength * repeat; switch (inputLength) { - case 1: + case 1 : return repeat(str.charAt(0), repeat); - case 2: + case 2 : final char ch0 = str.charAt(0); final char ch1 = str.charAt(1); final char[] output2 = new char[outputLength]; @@ -111,7 +112,7 @@ public static String repeat(final String str, final int repeat) { output2[i + 1] = ch1; } return new String(output2); - default: + default : final StringBuilder buf = new StringBuilder(outputLength); for (int i = 0; i < repeat; i++) { buf.append(str); @@ -133,15 +134,15 @@ public static String repeat(final String str, final int repeat) { * StringUtils.repeat("?", ", ", 3) = "?, ?, ?" * * - * @param str the String to repeat, may be null - * @param separator the String to inject, may be null - * @param repeat number of times to repeat str, negative treated as zero + * @param str the String to repeat, may be null + * @param separator the String to inject, may be null + * @param repeat number of times to repeat str, negative treated as zero * @return a new String consisting of the original String repeated, - * {@code null} if null String input + * {@code null} if null String input * @since 2.5 */ public static String repeat(final String str, final String separator, final int repeat) { - if (str == null || separator == null) { + if(str == null || separator == null) { return repeat(str, repeat); } // given that repeat(String, int) is quite optimized, better to rely on it than try and splice this into it @@ -167,10 +168,10 @@ public static String repeat(final String str, final String separator, final int * StringUtils.removeEnd("abc", "") = "abc" * * - * @param str the source String to search, may be null - * @param remove the String to search for and remove, may be null + * @param str the source String to search, may be null + * @param remove the String to search for and remove, may be null * @return the substring with the string removed if found, - * {@code null} if null String input + * {@code null} if null String input */ public static String removeEnd(final String str, final String remove) { if (isAnyEmpty(str, remove)) { @@ -199,8 +200,8 @@ public static String removeEnd(final String str, final String remove) { * consider using {@link #repeat(String, int)} instead. *

* - * @param ch character to repeat - * @param repeat number of times to repeat char, negative treated as zero + * @param ch character to repeat + * @param repeat number of times to repeat char, negative treated as zero * @return String with repeated character * @see #repeat(String, int) */ @@ -233,8 +234,8 @@ public static String repeat(final char ch, final int repeat) { * StringUtils.stripEnd("120.00", ".0") = "12" * * - * @param str the String to remove characters from, may be null - * @param stripChars the set of characters to remove, null treated as whitespace + * @param str the String to remove characters from, may be null + * @param stripChars the set of characters to remove, null treated as whitespace * @return the stripped String, {@code null} if null String input */ public static String stripEnd(final String str, final String stripChars) { @@ -273,12 +274,12 @@ public static String stripEnd(final String str, final String stripChars) { * StringUtils.replace("aba", "a", "z") = "zbz" * * - * @param text text to search and replace in, may be null - * @param searchString the String to search for, may be null + * @see #replace(String text, String searchString, String replacement, int max) + * @param text text to search and replace in, may be null + * @param searchString the String to search for, may be null * @param replacement the String to replace it with, may be null * @return the text with any replacements processed, - * {@code null} if null String input - * @see #replace(String text, String searchString, String replacement, int max) + * {@code null} if null String input */ public static String replace(final String text, final String searchString, final String replacement) { return replace(text, searchString, replacement, -1); @@ -305,12 +306,12 @@ public static String replace(final String text, final String searchString, final * StringUtils.replace("abaa", "a", "z", -1) = "zbzz" * * - * @param text text to search and replace in, may be null - * @param searchString the String to search for, may be null + * @param text text to search and replace in, may be null + * @param searchString the String to search for, may be null * @param replacement the String to replace it with, may be null - * @param max maximum number of values to replace, or {@code -1} if no maximum + * @param max maximum number of values to replace, or {@code -1} if no maximum * @return the text with any replacements processed, - * {@code null} if null String input + * {@code null} if null String input */ public static String replace(final String text, final String searchString, final String replacement, int max) { if (isAnyEmpty(text, searchString) || replacement == null || max == 0) { @@ -373,7 +374,7 @@ public static boolean isNoneEmpty(final String... ss) { if (ArrayUtils.isEmpty(ss)) { return false; } - for (final String s : ss) { + for (final String s : ss){ if (isEmpty(s)) { return false; } @@ -477,20 +478,12 @@ public static boolean isContains(String[] values, String value) { return false; } - public static boolean isNumeric(String str, boolean allowDot) { - if (str == null || str.isEmpty()) { + public static boolean isNumeric(String str) { + if (str == null) { return false; } - boolean hasDot = false; int sz = str.length(); for (int i = 0; i < sz; i++) { - if (str.charAt(i) == '.') { - if (hasDot || !allowDot) { - return false; - } - hasDot = true; - continue; - } if (!Character.isDigit(str.charAt(i))) { return false; } @@ -498,7 +491,6 @@ public static boolean isNumeric(String str, boolean allowDot) { return true; } - /** * @param e * @return string @@ -789,8 +781,4 @@ public static String toArgumentString(Object[] args) { } return buf.toString(); } - - public static String trim(String str) { - return str == null ? null : str.trim(); - } } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/UrlUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/UrlUtils.java index f7ca5c007b9..f93bc5a5f34 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/UrlUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/UrlUtils.java @@ -63,22 +63,22 @@ public static URL parseURL(String address, Map defaults) { url += URL_PARAM_STARTING_SYMBOL + Constants.BACKUP_KEY + "=" + backup.toString(); } } - String defaultProtocol = defaults == null ? null : defaults.get(Constants.PROTOCOL_KEY); + String defaultProtocol = defaults == null ? null : defaults.get("protocol"); if (defaultProtocol == null || defaultProtocol.length() == 0) { - defaultProtocol = Constants.DUBBO_PROTOCOL; + defaultProtocol = "dubbo"; } - String defaultUsername = defaults == null ? null : defaults.get(Constants.USERNAME_KEY); - String defaultPassword = defaults == null ? null : defaults.get(Constants.PASSWORD_KEY); - int defaultPort = StringUtils.parseInteger(defaults == null ? null : defaults.get(Constants.PORT_KEY)); - String defaultPath = defaults == null ? null : defaults.get(Constants.PATH_KEY); + String defaultUsername = defaults == null ? null : defaults.get("username"); + String defaultPassword = defaults == null ? null : defaults.get("password"); + int defaultPort = StringUtils.parseInteger(defaults == null ? null : defaults.get("port")); + String defaultPath = defaults == null ? null : defaults.get("path"); Map defaultParameters = defaults == null ? null : new HashMap(defaults); if (defaultParameters != null) { - defaultParameters.remove(Constants.PROTOCOL_KEY); - defaultParameters.remove(Constants.USERNAME_KEY); - defaultParameters.remove(Constants.PASSWORD_KEY); - defaultParameters.remove(Constants.HOST_KEY); - defaultParameters.remove(Constants.PORT_KEY); - defaultParameters.remove(Constants.PATH_KEY); + defaultParameters.remove("protocol"); + defaultParameters.remove("username"); + defaultParameters.remove("password"); + defaultParameters.remove("host"); + defaultParameters.remove("port"); + defaultParameters.remove("path"); } URL u = URL.valueOf(url); boolean changed = false; @@ -126,7 +126,7 @@ public static URL parseURL(String address, Map defaults) { String defaultValue = entry.getValue(); if (defaultValue != null && defaultValue.length() > 0) { String value = parameters.get(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { changed = true; parameters.put(key, defaultValue); } @@ -318,7 +318,7 @@ public static Map> revertNotify(Map revertForbid(List forbid, Set subscribed) { - if (CollectionUtils.isNotEmpty(forbid)) { + if (forbid != null && !forbid.isEmpty()) { List newForbid = new ArrayList(); for (String serviceName : forbid) { if (!serviceName.contains(":") && !serviceName.contains("/")) { @@ -410,10 +410,12 @@ public static boolean isMatchGlobPattern(String pattern, String value) { if ("*".equals(pattern)) { return true; } - if (StringUtils.isEmpty(pattern) && StringUtils.isEmpty(value)) { + if ((pattern == null || pattern.length() == 0) + && (value == null || value.length() == 0)) { return true; } - if (StringUtils.isEmpty(pattern) || StringUtils.isEmpty(value)) { + if ((pattern == null || pattern.length() == 0) + || (value == null || value.length() == 0)) { return false; } @@ -467,19 +469,6 @@ public static boolean isProvider(URL url) { PROVIDERS_CATEGORY.equals(url.getParameter(CATEGORY_KEY, PROVIDERS_CATEGORY)); } - public static int getHeartbeat(URL url) { - return url.getParameter(Constants.HEARTBEAT_KEY, Constants.DEFAULT_HEARTBEAT); - } - - public static int getIdleTimeout(URL url) { - int heartBeat = getHeartbeat(url); - int idleTimeout = url.getParameter(Constants.HEARTBEAT_TIMEOUT_KEY, heartBeat * 3); - if (idleTimeout < heartBeat * 2) { - throw new IllegalStateException("idleTimeout < heartbeatInterval * 2"); - } - return idleTimeout; - } - /** * Check if the given value matches the given pattern. The pattern supports wildcard "*". * diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/URLBuilderTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/URLBuilderTest.java deleted file mode 100644 index c607bd385c6..00000000000 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/URLBuilderTest.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * 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 org.apache.dubbo.common; - -import org.junit.jupiter.api.Test; - -import java.util.Arrays; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; - -public class URLBuilderTest { - @Test - public void testNoArgConstructor() { - URL url = new URLBuilder().build(); - assertThat(url.toString(), equalTo("")); - } - - @Test - public void shouldAddParameter() { - URL url1 = URL.valueOf("dubbo://admin:hello1234@10.20.130.230:20880/context/path?version=1.0.0&application=morgan"); - URL url2 = URLBuilder.from(url1) - .addParameter("newKey1", "newValue1") // string - .addParameter("newKey2", 2) // int - .addParameter("version", 1) // override - .build(); - assertThat(url2.getParameter("newKey1"), equalTo("newValue1")); - assertThat(url2.getParameter("newKey2"), equalTo("2")); - assertThat(url2.getParameter("version"), equalTo("1")); - } - - @Test - public void shouldSet() { - URL url1 = URL.valueOf("dubbo://admin:hello1234@10.20.130.230:20880/context/path?version=1.0.0&application=morgan"); - URL url2 = URLBuilder.from(url1) - .setProtocol("rest") - .setUsername("newUsername") - .setPassword("newPassword") - .setHost("newHost") - .setPath("newContext") - .setPort(1234) - .build(); - assertThat(url2.getProtocol(), equalTo("rest")); - assertThat(url2.getUsername(), equalTo("newUsername")); - assertThat(url2.getPassword(), equalTo("newPassword")); - assertThat(url2.getHost(), equalTo("newHost")); - assertThat(url2.getPort(), equalTo(1234)); - assertThat(url2.getPath(), equalTo("newContext")); - - url2 = URLBuilder.from(url1) - .setAddress("newHost2:2345") - .build(); - assertThat(url2.getHost(), equalTo("newHost2")); - assertThat(url2.getPort(), equalTo(2345)); - } - - @Test - public void shouldClearParameters() { - URL url1 = URL.valueOf("dubbo://admin:hello1234@10.20.130.230:20880/context/path?version=1.0.0&application=morgan"); - URL url2 = URLBuilder.from(url1) - .clearParameters() - .build(); - assertThat(url2.getParameters().size(), equalTo(0)); - } - - @Test - public void shouldRemoveParameters() { - URL url1 = URL.valueOf("dubbo://admin:hello1234@10.20.130.230:20880/context/path?version=1.0.0&application=morgan&key2=v2"); - URL url2 = URLBuilder.from(url1) - .removeParameters(Arrays.asList("key2", "application")) - .build(); - assertThat(url2.getParameters().size(), equalTo(1)); - assertThat(url2.getParameter("version"), equalTo("1.0.0")); - } - - @Test - public void shouldAddIfAbsent() { - URL url1 = URL.valueOf("dubbo://admin:hello1234@10.20.130.230:20880/context/path?version=1.0.0&application=morgan&key2=v2"); - URL url2 = URLBuilder.from(url1) - .addParameterIfAbsent("absentKey", "absentValue") - .addParameterIfAbsent("version", "2.0.0") // should not override - .build(); - assertThat(url2.getParameter("version"), equalTo("1.0.0")); - assertThat(url2.getParameter("absentKey"), equalTo("absentValue")); - } -} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/URLTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/URLTest.java index a74f7baa974..cc2a5dbcbbf 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/URLTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/URLTest.java @@ -17,9 +17,7 @@ package org.apache.dubbo.common; import org.apache.dubbo.common.utils.CollectionUtils; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.io.File; import java.util.Arrays; @@ -28,12 +26,12 @@ import static org.hamcrest.CoreMatchers.anyOf; import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; public class URLTest { @@ -681,28 +679,4 @@ public void testIpV6AddressWithScopeId(){ assertEquals("1.0.0", url.getParameter("version")); assertEquals("morgan", url.getParameter("application")); } - - @Test - public void testDefaultPort() { - Assertions.assertEquals("10.20.153.10:2181", URL.appendDefaultPort("10.20.153.10:0", 2181)); - Assertions.assertEquals("10.20.153.10:2181", URL.appendDefaultPort("10.20.153.10", 2181)); - } - - @Test - public void testGetServiceKey () { - URL url1 = URL.valueOf("10.20.130.230:20880/context/path?interface=org.apache.dubbo.test.interfaceName"); - Assertions.assertEquals("org.apache.dubbo.test.interfaceName", url1.getServiceKey()); - - URL url2 = URL.valueOf("10.20.130.230:20880/org.apache.dubbo.test.interfaceName?interface=org.apache.dubbo.test.interfaceName"); - Assertions.assertEquals("org.apache.dubbo.test.interfaceName", url2.getServiceKey()); - - URL url3 = URL.valueOf("10.20.130.230:20880/org.apache.dubbo.test.interfaceName?interface=org.apache.dubbo.test.interfaceName&group=group1&version=1.0.0"); - Assertions.assertEquals("group1/org.apache.dubbo.test.interfaceName:1.0.0", url3.getServiceKey()); - - URL url4 = URL.valueOf("10.20.130.230:20880/context/path?interface=org.apache.dubbo.test.interfaceName"); - Assertions.assertEquals("context/path", url4.getPathKey()); - - URL url5 = URL.valueOf("10.20.130.230:20880/context/path?interface=org.apache.dubbo.test.interfaceName&group=group1&version=1.0.0"); - Assertions.assertEquals("group1/context/path:1.0.0", url5.getPathKey()); - } } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/beanutil/JavaBeanAccessorTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/beanutil/JavaBeanAccessorTest.java index 55bbc598ec5..4c4e1523501 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/beanutil/JavaBeanAccessorTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/beanutil/JavaBeanAccessorTest.java @@ -16,22 +16,22 @@ */ package org.apache.dubbo.common.beanutil; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; public class JavaBeanAccessorTest { @Test public void testIsAccessByMethod(){ - Assertions.assertTrue(JavaBeanAccessor.isAccessByMethod(JavaBeanAccessor.METHOD)); - Assertions.assertTrue(JavaBeanAccessor.isAccessByMethod(JavaBeanAccessor.ALL)); - Assertions.assertFalse(JavaBeanAccessor.isAccessByMethod(JavaBeanAccessor.FIELD)); + Assert.assertTrue(JavaBeanAccessor.isAccessByMethod(JavaBeanAccessor.METHOD)); + Assert.assertTrue(JavaBeanAccessor.isAccessByMethod(JavaBeanAccessor.ALL)); + Assert.assertFalse(JavaBeanAccessor.isAccessByMethod(JavaBeanAccessor.FIELD)); } @Test public void testIsAccessByField(){ - Assertions.assertTrue(JavaBeanAccessor.isAccessByField(JavaBeanAccessor.FIELD)); - Assertions.assertTrue(JavaBeanAccessor.isAccessByField(JavaBeanAccessor.ALL)); - Assertions.assertFalse(JavaBeanAccessor.isAccessByField(JavaBeanAccessor.METHOD)); + Assert.assertTrue(JavaBeanAccessor.isAccessByField(JavaBeanAccessor.FIELD)); + Assert.assertTrue(JavaBeanAccessor.isAccessByField(JavaBeanAccessor.ALL)); + Assert.assertFalse(JavaBeanAccessor.isAccessByField(JavaBeanAccessor.METHOD)); } } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/beanutil/JavaBeanSerializeUtilTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/beanutil/JavaBeanSerializeUtilTest.java index 5c72a14d3d0..3cf314cd949 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/beanutil/JavaBeanSerializeUtilTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/beanutil/JavaBeanSerializeUtilTest.java @@ -22,9 +22,8 @@ import org.apache.dubbo.common.model.person.PersonStatus; import org.apache.dubbo.common.model.person.Phone; import org.apache.dubbo.common.utils.PojoUtilsTest; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.lang.reflect.Array; import java.math.BigDecimal; @@ -38,160 +37,142 @@ public class JavaBeanSerializeUtilTest { @Test - public void testSerialize_Primitive() { + public void testSerialize_Primitive() throws Exception { JavaBeanDescriptor descriptor; descriptor = JavaBeanSerializeUtil.serialize(Integer.MAX_VALUE); - Assertions.assertTrue(descriptor.isPrimitiveType()); - Assertions.assertEquals(Integer.MAX_VALUE, descriptor.getPrimitiveProperty()); + Assert.assertTrue(descriptor.isPrimitiveType()); + Assert.assertEquals(Integer.MAX_VALUE, descriptor.getPrimitiveProperty()); Date now = new Date(); descriptor = JavaBeanSerializeUtil.serialize(now); - Assertions.assertTrue(descriptor.isPrimitiveType()); - Assertions.assertEquals(now, descriptor.getPrimitiveProperty()); + Assert.assertTrue(descriptor.isPrimitiveType()); + Assert.assertEquals(now, descriptor.getPrimitiveProperty()); } @Test - public void testSerialize_Primitive_NUll() { + public void testSerialize_Primitive_NUll() throws Exception { JavaBeanDescriptor descriptor; descriptor = JavaBeanSerializeUtil.serialize(null); - Assertions.assertTrue(descriptor == null); + Assert.assertTrue(descriptor == null); } @Test - public void testDeserialize_Primitive() { + public void testDeserialize_Primitive() throws Exception { JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_PRIMITIVE); descriptor.setPrimitiveProperty(Long.MAX_VALUE); - Assertions.assertEquals(Long.MAX_VALUE, JavaBeanSerializeUtil.deserialize(descriptor)); + Assert.assertEquals(Long.MAX_VALUE, JavaBeanSerializeUtil.deserialize(descriptor)); BigDecimal decimal = BigDecimal.TEN; - Assertions.assertEquals(Long.MAX_VALUE, descriptor.setPrimitiveProperty(decimal)); - Assertions.assertEquals(decimal, JavaBeanSerializeUtil.deserialize(descriptor)); + Assert.assertEquals(Long.MAX_VALUE, descriptor.setPrimitiveProperty(decimal)); + Assert.assertEquals(decimal, JavaBeanSerializeUtil.deserialize(descriptor)); String string = UUID.randomUUID().toString(); - Assertions.assertEquals(decimal, descriptor.setPrimitiveProperty(string)); - Assertions.assertEquals(string, JavaBeanSerializeUtil.deserialize(descriptor)); + Assert.assertEquals(decimal, descriptor.setPrimitiveProperty(string)); + Assert.assertEquals(string, JavaBeanSerializeUtil.deserialize(descriptor)); } - @Test - public void testDeserialize_Primitive0() { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_BEAN + 1); - }); + @Test(expected = IllegalArgumentException.class) + public void testDeserialize_Primitive0() throws Exception { + JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_BEAN + 1); } - @Test - public void testDeserialize_Null() { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - JavaBeanDescriptor descriptor = new JavaBeanDescriptor(null, JavaBeanDescriptor.TYPE_BEAN); - }); + @Test(expected = IllegalArgumentException.class) + public void testDeserialize_Null() throws Exception { + JavaBeanDescriptor descriptor = new JavaBeanDescriptor(null, JavaBeanDescriptor.TYPE_BEAN); } - @Test - public void testDeserialize_containsProperty() { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_PRIMITIVE); - descriptor.containsProperty(null); - }); + @Test(expected = IllegalArgumentException.class) + public void testDeserialize_containsProperty() throws Exception { + JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_PRIMITIVE); + descriptor.containsProperty(null); } - @Test - public void testSetEnumNameProperty() { - Assertions.assertThrows(IllegalStateException.class, () -> { - JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_PRIMITIVE); - descriptor.setEnumNameProperty(JavaBeanDescriptor.class.getName()); - }); + @Test(expected = IllegalStateException.class) + public void testSetEnumNameProperty() throws Exception { + JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_PRIMITIVE); + descriptor.setEnumNameProperty(JavaBeanDescriptor.class.getName()); } - @Test - public void testGetEnumNameProperty() { - Assertions.assertThrows(IllegalStateException.class, () -> { - JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_PRIMITIVE); - descriptor.getEnumPropertyName(); - }); + @Test(expected = IllegalStateException.class) + public void testGetEnumNameProperty() throws Exception { + JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_PRIMITIVE); + descriptor.getEnumPropertyName(); } - @Test - public void testSetClassNameProperty() { - Assertions.assertThrows(IllegalStateException.class, () -> { - JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_PRIMITIVE); - descriptor.setClassNameProperty(JavaBeanDescriptor.class.getName()); - }); + @Test(expected = IllegalStateException.class) + public void testSetClassNameProperty() throws Exception { + JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_PRIMITIVE); + descriptor.setClassNameProperty(JavaBeanDescriptor.class.getName()); } - @Test - public void testGetClassNameProperty() { - Assertions.assertThrows(IllegalStateException.class, () -> { - JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_PRIMITIVE); - descriptor.getClassNameProperty(); - }); + @Test(expected = IllegalStateException.class) + public void testGetClassNameProperty() throws Exception { + JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_PRIMITIVE); + descriptor.getClassNameProperty(); } - @Test - public void testSetPrimitiveProperty() { - Assertions.assertThrows(IllegalStateException.class, () -> { - JavaBeanDescriptor descriptor = new JavaBeanDescriptor(JavaBeanDescriptor.class.getName(), JavaBeanDescriptor.TYPE_BEAN); - descriptor.setPrimitiveProperty(JavaBeanDescriptor.class.getName()); - }); + @Test(expected = IllegalStateException.class) + public void testSetPrimitiveProperty() throws Exception { + JavaBeanDescriptor descriptor = new JavaBeanDescriptor(JavaBeanDescriptor.class.getName(), JavaBeanDescriptor.TYPE_BEAN); + descriptor.setPrimitiveProperty(JavaBeanDescriptor.class.getName()); } - @Test - public void testGetPrimitiveProperty() { - Assertions.assertThrows(IllegalStateException.class, () -> { - JavaBeanDescriptor descriptor = new JavaBeanDescriptor(JavaBeanDescriptor.class.getName(), JavaBeanDescriptor.TYPE_BEAN); - descriptor.getPrimitiveProperty(); - }); + @Test(expected = IllegalStateException.class) + public void testGetPrimitiveProperty() throws Exception { + JavaBeanDescriptor descriptor = new JavaBeanDescriptor(JavaBeanDescriptor.class.getName(), JavaBeanDescriptor.TYPE_BEAN); + descriptor.getPrimitiveProperty(); } @Test - public void testDeserialize_get_and_set() { + public void testDeserialize_get_and_set() throws Exception { JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_BEAN); descriptor.setType(JavaBeanDescriptor.TYPE_PRIMITIVE); - Assertions.assertTrue(descriptor.getType() == JavaBeanDescriptor.TYPE_PRIMITIVE); + Assert.assertTrue(descriptor.getType() == JavaBeanDescriptor.TYPE_PRIMITIVE); descriptor.setClassName(JavaBeanDescriptor.class.getName()); - Assertions.assertEquals(JavaBeanDescriptor.class.getName(), descriptor.getClassName()); + Assert.assertEquals(JavaBeanDescriptor.class.getName(), descriptor.getClassName()); } @Test - public void testSerialize_Array() { + public void testSerialize_Array() throws Exception { int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9}; JavaBeanDescriptor descriptor = JavaBeanSerializeUtil.serialize(array, JavaBeanAccessor.METHOD); - Assertions.assertTrue(descriptor.isArrayType()); - Assertions.assertEquals(int.class.getName(), descriptor.getClassName()); + Assert.assertTrue(descriptor.isArrayType()); + Assert.assertEquals(int.class.getName(), descriptor.getClassName()); for (int i = 0; i < array.length; i++) { - Assertions.assertEquals(array[i], + Assert.assertEquals(array[i], ((JavaBeanDescriptor) descriptor.getProperty(i)).getPrimitiveProperty()); } Integer[] integers = new Integer[]{1, 2, 3, 4, null, null, null}; descriptor = JavaBeanSerializeUtil.serialize(integers, JavaBeanAccessor.METHOD); - Assertions.assertTrue(descriptor.isArrayType()); - Assertions.assertEquals(Integer.class.getName(), descriptor.getClassName()); - Assertions.assertEquals(integers.length, descriptor.propertySize()); + Assert.assertTrue(descriptor.isArrayType()); + Assert.assertEquals(Integer.class.getName(), descriptor.getClassName()); + Assert.assertEquals(integers.length, descriptor.propertySize()); for (int i = 0; i < integers.length; i++) { if (integers[i] == null) { - Assertions.assertTrue(integers[i] == descriptor.getProperty(i)); + Assert.assertTrue(integers[i] == descriptor.getProperty(i)); } else { - Assertions.assertEquals(integers[i], ((JavaBeanDescriptor) descriptor.getProperty(i)).getPrimitiveProperty()); + Assert.assertEquals(integers[i], ((JavaBeanDescriptor) descriptor.getProperty(i)).getPrimitiveProperty()); } } int[][] second = {{1, 2}, {3, 4}}; descriptor = JavaBeanSerializeUtil.serialize(second, JavaBeanAccessor.METHOD); - Assertions.assertTrue(descriptor.isArrayType()); - Assertions.assertEquals(int[].class.getName(), descriptor.getClassName()); + Assert.assertTrue(descriptor.isArrayType()); + Assert.assertEquals(int[].class.getName(), descriptor.getClassName()); for (int i = 0; i < second.length; i++) { for (int j = 0; j < second[i].length; j++) { JavaBeanDescriptor item = (((JavaBeanDescriptor) descriptor.getProperty(i))); - Assertions.assertTrue(item.isArrayType()); - Assertions.assertEquals(int.class.getName(), item.getClassName()); - Assertions.assertEquals(second[i][j], ((JavaBeanDescriptor) item.getProperty(j)).getPrimitiveProperty()); + Assert.assertTrue(item.isArrayType()); + Assert.assertEquals(int.class.getName(), item.getClassName()); + Assert.assertEquals(second[i][j], ((JavaBeanDescriptor) item.getProperty(j)).getPrimitiveProperty()); } } BigPerson[] persons = new BigPerson[]{createBigPerson(), createBigPerson()}; descriptor = JavaBeanSerializeUtil.serialize(persons); - Assertions.assertTrue(descriptor.isArrayType()); - Assertions.assertEquals(BigPerson.class.getName(), descriptor.getClassName()); + Assert.assertTrue(descriptor.isArrayType()); + Assert.assertEquals(BigPerson.class.getName(), descriptor.getClassName()); for (int i = 0; i < persons.length; i++) { assertEqualsBigPerson(persons[i], descriptor.getProperty(i)); } @@ -199,27 +180,27 @@ public void testSerialize_Array() { @Test public void testConstructorArg() { - Assertions.assertFalse((boolean) JavaBeanSerializeUtil.getConstructorArg(boolean.class)); - Assertions.assertFalse((boolean) JavaBeanSerializeUtil.getConstructorArg(Boolean.class)); - Assertions.assertEquals((byte) 0, JavaBeanSerializeUtil.getConstructorArg(byte.class)); - Assertions.assertEquals((byte) 0, JavaBeanSerializeUtil.getConstructorArg(Byte.class)); - Assertions.assertEquals((short) 0, JavaBeanSerializeUtil.getConstructorArg(short.class)); - Assertions.assertEquals((short) 0, JavaBeanSerializeUtil.getConstructorArg(Short.class)); - Assertions.assertEquals(0, JavaBeanSerializeUtil.getConstructorArg(int.class)); - Assertions.assertEquals(0, JavaBeanSerializeUtil.getConstructorArg(Integer.class)); - Assertions.assertEquals((long) 0, JavaBeanSerializeUtil.getConstructorArg(long.class)); - Assertions.assertEquals((long) 0, JavaBeanSerializeUtil.getConstructorArg(Long.class)); - Assertions.assertEquals((float) 0, JavaBeanSerializeUtil.getConstructorArg(float.class)); - Assertions.assertEquals((float) 0, JavaBeanSerializeUtil.getConstructorArg(Float.class)); - Assertions.assertEquals((double) 0, JavaBeanSerializeUtil.getConstructorArg(double.class)); - Assertions.assertEquals((double) 0, JavaBeanSerializeUtil.getConstructorArg(Double.class)); - Assertions.assertEquals((char) 0, JavaBeanSerializeUtil.getConstructorArg(char.class)); - Assertions.assertEquals(new Character((char) 0), JavaBeanSerializeUtil.getConstructorArg(Character.class)); - Assertions.assertEquals(null, JavaBeanSerializeUtil.getConstructorArg(JavaBeanSerializeUtil.class)); + Assert.assertFalse((boolean) JavaBeanSerializeUtil.getConstructorArg(boolean.class)); + Assert.assertFalse((boolean) JavaBeanSerializeUtil.getConstructorArg(Boolean.class)); + Assert.assertEquals((byte)0, JavaBeanSerializeUtil.getConstructorArg(byte.class)); + Assert.assertEquals((byte)0, JavaBeanSerializeUtil.getConstructorArg(Byte.class)); + Assert.assertEquals((short)0, JavaBeanSerializeUtil.getConstructorArg(short.class)); + Assert.assertEquals((short)0, JavaBeanSerializeUtil.getConstructorArg(Short.class)); + Assert.assertEquals(0, JavaBeanSerializeUtil.getConstructorArg(int.class)); + Assert.assertEquals(0, JavaBeanSerializeUtil.getConstructorArg(Integer.class)); + Assert.assertEquals((long)0, JavaBeanSerializeUtil.getConstructorArg(long.class)); + Assert.assertEquals((long)0, JavaBeanSerializeUtil.getConstructorArg(Long.class)); + Assert.assertEquals((float) 0, JavaBeanSerializeUtil.getConstructorArg(float.class)); + Assert.assertEquals((float) 0, JavaBeanSerializeUtil.getConstructorArg(Float.class)); + Assert.assertEquals((double) 0, JavaBeanSerializeUtil.getConstructorArg(double.class)); + Assert.assertEquals((double) 0, JavaBeanSerializeUtil.getConstructorArg(Double.class)); + Assert.assertEquals((char)0, JavaBeanSerializeUtil.getConstructorArg(char.class)); + Assert.assertEquals(new Character((char)0), JavaBeanSerializeUtil.getConstructorArg(Character.class)); + Assert.assertEquals(null, JavaBeanSerializeUtil.getConstructorArg(JavaBeanSerializeUtil.class)); } @Test - public void testDeserialize_Array() { + public void testDeserialize_Array() throws Exception { final int len = 10; JavaBeanDescriptor descriptor = new JavaBeanDescriptor(int.class.getName(), JavaBeanDescriptor.TYPE_ARRAY); for (int i = 0; i < len; i++) { @@ -227,10 +208,10 @@ public void testDeserialize_Array() { } Object obj = JavaBeanSerializeUtil.deserialize(descriptor); - Assertions.assertTrue(obj.getClass().isArray()); - Assertions.assertTrue(int.class == obj.getClass().getComponentType()); + Assert.assertTrue(obj.getClass().isArray()); + Assert.assertTrue(int.class == obj.getClass().getComponentType()); for (int i = 0; i < len; i++) { - Assertions.assertEquals(i, Array.get(obj, i)); + Assert.assertEquals(i, Array.get(obj, i)); } descriptor = new JavaBeanDescriptor(int[].class.getName(), JavaBeanDescriptor.TYPE_ARRAY); @@ -242,14 +223,14 @@ public void testDeserialize_Array() { descriptor.setProperty(i, innerItem); } obj = JavaBeanSerializeUtil.deserialize(descriptor); - Assertions.assertTrue(obj.getClass().isArray()); - Assertions.assertEquals(int[].class, obj.getClass().getComponentType()); + Assert.assertTrue(obj.getClass().isArray()); + Assert.assertEquals(int[].class, obj.getClass().getComponentType()); for (int i = 0; i < len; i++) { Object innerItem = Array.get(obj, i); - Assertions.assertTrue(innerItem.getClass().isArray()); - Assertions.assertEquals(int.class, innerItem.getClass().getComponentType()); + Assert.assertTrue(innerItem.getClass().isArray()); + Assert.assertEquals(int.class, innerItem.getClass().getComponentType()); for (int j = 0; j < len; j++) { - Assertions.assertEquals(j, Array.get(innerItem, j)); + Assert.assertEquals(j, Array.get(innerItem, j)); } } @@ -259,18 +240,18 @@ public void testDeserialize_Array() { descriptor.setProperty(0, innerDescriptor); obj = JavaBeanSerializeUtil.deserialize(descriptor); - Assertions.assertTrue(obj.getClass().isArray()); - Assertions.assertEquals(BigPerson[].class, obj.getClass().getComponentType()); - Assertions.assertEquals(1, Array.getLength(obj)); + Assert.assertTrue(obj.getClass().isArray()); + Assert.assertEquals(BigPerson[].class, obj.getClass().getComponentType()); + Assert.assertEquals(1, Array.getLength(obj)); obj = Array.get(obj, 0); - Assertions.assertTrue(obj.getClass().isArray()); - Assertions.assertEquals(BigPerson.class, obj.getClass().getComponentType()); - Assertions.assertEquals(1, Array.getLength(obj)); - Assertions.assertEquals(createBigPerson(), Array.get(obj, 0)); + Assert.assertTrue(obj.getClass().isArray()); + Assert.assertEquals(BigPerson.class, obj.getClass().getComponentType()); + Assert.assertEquals(1, Array.getLength(obj)); + Assert.assertEquals(createBigPerson(), Array.get(obj, 0)); } @Test - public void test_Circular_Reference() { + public void test_Circular_Reference() throws Exception { PojoUtilsTest.Parent parent = new PojoUtilsTest.Parent(); parent.setAge(Integer.MAX_VALUE); parent.setEmail("a@b"); @@ -284,19 +265,19 @@ public void test_Circular_Reference() { parent.setChild(child); JavaBeanDescriptor descriptor = JavaBeanSerializeUtil.serialize(parent, JavaBeanAccessor.METHOD); - Assertions.assertTrue(descriptor.isBeanType()); + Assert.assertTrue(descriptor.isBeanType()); assertEqualsPrimitive(parent.getAge(), descriptor.getProperty("age")); assertEqualsPrimitive(parent.getName(), descriptor.getProperty("name")); assertEqualsPrimitive(parent.getEmail(), descriptor.getProperty("email")); JavaBeanDescriptor childDescriptor = (JavaBeanDescriptor) descriptor.getProperty("child"); - Assertions.assertTrue(descriptor == childDescriptor.getProperty("parent")); + Assert.assertTrue(descriptor == childDescriptor.getProperty("parent")); assertEqualsPrimitive(child.getName(), childDescriptor.getProperty("name")); assertEqualsPrimitive(child.getAge(), childDescriptor.getProperty("age")); } @Test - public void testBeanSerialize() { + public void testBeanSerialize() throws Exception { Bean bean = new Bean(); bean.setDate(new Date()); bean.setStatus(PersonStatus.ENABLED); @@ -314,39 +295,39 @@ public void testBeanSerialize() { bean.setAddresses(map); JavaBeanDescriptor descriptor = JavaBeanSerializeUtil.serialize(bean, JavaBeanAccessor.METHOD); - Assertions.assertTrue(descriptor.isBeanType()); + Assert.assertTrue(descriptor.isBeanType()); assertEqualsPrimitive(bean.getDate(), descriptor.getProperty("date")); assertEqualsEnum(bean.getStatus(), descriptor.getProperty("status")); - Assertions.assertTrue(((JavaBeanDescriptor) descriptor.getProperty("type")).isClassType()); - Assertions.assertEquals(Bean.class.getName(), ((JavaBeanDescriptor) descriptor.getProperty("type")).getClassNameProperty()); - Assertions.assertTrue(((JavaBeanDescriptor) descriptor.getProperty("array")).isArrayType()); - Assertions.assertEquals(0, ((JavaBeanDescriptor) descriptor.getProperty("array")).propertySize()); + Assert.assertTrue(((JavaBeanDescriptor) descriptor.getProperty("type")).isClassType()); + Assert.assertEquals(Bean.class.getName(), ((JavaBeanDescriptor) descriptor.getProperty("type")).getClassNameProperty()); + Assert.assertTrue(((JavaBeanDescriptor) descriptor.getProperty("array")).isArrayType()); + Assert.assertEquals(0, ((JavaBeanDescriptor) descriptor.getProperty("array")).propertySize()); JavaBeanDescriptor property = (JavaBeanDescriptor) descriptor.getProperty("collection"); - Assertions.assertTrue(property.isCollectionType()); - Assertions.assertEquals(1, property.propertySize()); + Assert.assertTrue(property.isCollectionType()); + Assert.assertEquals(1, property.propertySize()); property = (JavaBeanDescriptor) property.getProperty(0); - Assertions.assertTrue(property.isBeanType()); - Assertions.assertEquals(Phone.class.getName(), property.getClassName()); - Assertions.assertEquals(0, property.propertySize()); + Assert.assertTrue(property.isBeanType()); + Assert.assertEquals(Phone.class.getName(), property.getClassName()); + Assert.assertEquals(0, property.propertySize()); property = (JavaBeanDescriptor) descriptor.getProperty("addresses"); - Assertions.assertTrue(property.isMapType()); - Assertions.assertEquals(bean.getAddresses().getClass().getName(), property.getClassName()); - Assertions.assertEquals(1, property.propertySize()); + Assert.assertTrue(property.isMapType()); + Assert.assertEquals(bean.getAddresses().getClass().getName(), property.getClassName()); + Assert.assertEquals(1, property.propertySize()); Map.Entry entry = property.iterator().next(); - Assertions.assertTrue(((JavaBeanDescriptor) entry.getKey()).isPrimitiveType()); - Assertions.assertEquals("first", ((JavaBeanDescriptor) entry.getKey()).getPrimitiveProperty()); + Assert.assertTrue(((JavaBeanDescriptor) entry.getKey()).isPrimitiveType()); + Assert.assertEquals("first", ((JavaBeanDescriptor) entry.getKey()).getPrimitiveProperty()); - Assertions.assertTrue(((JavaBeanDescriptor) entry.getValue()).isBeanType()); - Assertions.assertEquals(FullAddress.class.getName(), ((JavaBeanDescriptor) entry.getValue()).getClassName()); - Assertions.assertEquals(0, ((JavaBeanDescriptor) entry.getValue()).propertySize()); + Assert.assertTrue(((JavaBeanDescriptor) entry.getValue()).isBeanType()); + Assert.assertEquals(FullAddress.class.getName(), ((JavaBeanDescriptor) entry.getValue()).getClassName()); + Assert.assertEquals(0, ((JavaBeanDescriptor) entry.getValue()).propertySize()); } @Test - public void testDeserializeBean() { + public void testDeserializeBean() throws Exception { Bean bean = new Bean(); bean.setDate(new Date()); bean.setStatus(PersonStatus.ENABLED); @@ -365,45 +346,45 @@ public void testDeserializeBean() { JavaBeanDescriptor beanDescriptor = JavaBeanSerializeUtil.serialize(bean, JavaBeanAccessor.METHOD); Object deser = JavaBeanSerializeUtil.deserialize(beanDescriptor); - Assertions.assertTrue(deser instanceof Bean); + Assert.assertTrue(deser instanceof Bean); Bean deserBean = (Bean) deser; - Assertions.assertEquals(bean.getDate(), deserBean.getDate()); - Assertions.assertEquals(bean.getStatus(), deserBean.getStatus()); - Assertions.assertEquals(bean.getType(), deserBean.getType()); - Assertions.assertEquals(bean.getCollection().size(), deserBean.getCollection().size()); - Assertions.assertEquals(bean.getCollection().iterator().next().getClass(), + Assert.assertEquals(bean.getDate(), deserBean.getDate()); + Assert.assertEquals(bean.getStatus(), deserBean.getStatus()); + Assert.assertEquals(bean.getType(), deserBean.getType()); + Assert.assertEquals(bean.getCollection().size(), deserBean.getCollection().size()); + Assert.assertEquals(bean.getCollection().iterator().next().getClass(), deserBean.getCollection().iterator().next().getClass()); - Assertions.assertEquals(bean.getAddresses().size(), deserBean.getAddresses().size()); - Assertions.assertEquals(bean.getAddresses().entrySet().iterator().next().getKey(), + Assert.assertEquals(bean.getAddresses().size(), deserBean.getAddresses().size()); + Assert.assertEquals(bean.getAddresses().entrySet().iterator().next().getKey(), deserBean.getAddresses().entrySet().iterator().next().getKey()); - Assertions.assertEquals(bean.getAddresses().entrySet().iterator().next().getValue().getClass(), + Assert.assertEquals(bean.getAddresses().entrySet().iterator().next().getValue().getClass(), deserBean.getAddresses().entrySet().iterator().next().getValue().getClass()); } @Test @SuppressWarnings("unchecked") - public void testSerializeJavaBeanDescriptor() { + public void testSerializeJavaBeanDescriptor() throws Exception { JavaBeanDescriptor descriptor = new JavaBeanDescriptor(); JavaBeanDescriptor result = JavaBeanSerializeUtil.serialize(descriptor); - Assertions.assertTrue(descriptor == result); + Assert.assertTrue(descriptor == result); Map map = new HashMap(); map.put("first", descriptor); result = JavaBeanSerializeUtil.serialize(map); - Assertions.assertTrue(result.isMapType()); - Assertions.assertEquals(HashMap.class.getName(), result.getClassName()); - Assertions.assertEquals(map.size(), result.propertySize()); + Assert.assertTrue(result.isMapType()); + Assert.assertEquals(HashMap.class.getName(), result.getClassName()); + Assert.assertEquals(map.size(), result.propertySize()); Object object = result.iterator().next().getValue(); - Assertions.assertTrue(object instanceof JavaBeanDescriptor); + Assert.assertTrue(object instanceof JavaBeanDescriptor); JavaBeanDescriptor actual = (JavaBeanDescriptor) object; - Assertions.assertEquals(map.get("first"), actual); + Assert.assertEquals(map.get("first"), actual); } static void assertEqualsEnum(Enum expected, Object obj) { JavaBeanDescriptor descriptor = (JavaBeanDescriptor) obj; - Assertions.assertTrue(descriptor.isEnumType()); - Assertions.assertEquals(expected.getClass().getName(), descriptor.getClassName()); - Assertions.assertEquals(expected.name(), descriptor.getEnumPropertyName()); + Assert.assertTrue(descriptor.isEnumType()); + Assert.assertEquals(expected.getClass().getName(), descriptor.getClassName()); + Assert.assertEquals(expected.name(), descriptor.getEnumPropertyName()); } static void assertEqualsPrimitive(Object expected, Object obj) { @@ -411,13 +392,13 @@ static void assertEqualsPrimitive(Object expected, Object obj) { return; } JavaBeanDescriptor descriptor = (JavaBeanDescriptor) obj; - Assertions.assertTrue(descriptor.isPrimitiveType()); - Assertions.assertEquals(expected, descriptor.getPrimitiveProperty()); + Assert.assertTrue(descriptor.isPrimitiveType()); + Assert.assertEquals(expected, descriptor.getPrimitiveProperty()); } static void assertEqualsBigPerson(BigPerson person, Object obj) { JavaBeanDescriptor descriptor = (JavaBeanDescriptor) obj; - Assertions.assertTrue(descriptor.isBeanType()); + Assert.assertTrue(descriptor.isBeanType()); assertEqualsPrimitive(person.getPersonId(), descriptor.getProperty("personId")); assertEqualsPrimitive(person.getLoginName(), descriptor.getProperty("loginName")); assertEqualsEnum(person.getStatus(), descriptor.getProperty("status")); @@ -425,9 +406,9 @@ static void assertEqualsBigPerson(BigPerson person, Object obj) { assertEqualsPrimitive(person.getPenName(), descriptor.getProperty("penName")); JavaBeanDescriptor infoProfile = (JavaBeanDescriptor) descriptor.getProperty("infoProfile"); - Assertions.assertTrue(infoProfile.isBeanType()); + Assert.assertTrue(infoProfile.isBeanType()); JavaBeanDescriptor phones = (JavaBeanDescriptor) infoProfile.getProperty("phones"); - Assertions.assertTrue(phones.isCollectionType()); + Assert.assertTrue(phones.isCollectionType()); assertEqualsPhone(person.getInfoProfile().getPhones().get(0), phones.getProperty(0)); assertEqualsPhone(person.getInfoProfile().getPhones().get(1), phones.getProperty(1)); assertEqualsPhone(person.getInfoProfile().getFax(), infoProfile.getProperty("fax")); @@ -443,7 +424,7 @@ static void assertEqualsBigPerson(BigPerson person, Object obj) { static void assertEqualsPhone(Phone excpected, Object obj) { JavaBeanDescriptor descriptor = (JavaBeanDescriptor) obj; - Assertions.assertTrue(descriptor.isBeanType()); + Assert.assertTrue(descriptor.isBeanType()); if (excpected.getArea() != null) { assertEqualsPrimitive(excpected.getArea(), descriptor.getProperty("area")); } @@ -460,7 +441,7 @@ static void assertEqualsPhone(Phone excpected, Object obj) { static void assertEqualsFullAddress(FullAddress expected, Object obj) { JavaBeanDescriptor descriptor = (JavaBeanDescriptor) obj; - Assertions.assertTrue(descriptor.isBeanType()); + Assert.assertTrue(descriptor.isBeanType()); if (expected.getCityId() != null) { assertEqualsPrimitive(expected.getCityId(), descriptor.getProperty("cityId")); } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/ClassGeneratorTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/ClassGeneratorTest.java index c49554623ec..9ae67f3b8c9 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/ClassGeneratorTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/ClassGeneratorTest.java @@ -16,7 +16,8 @@ */ package org.apache.dubbo.common.bytecode; -import org.junit.jupiter.api.Test; +import junit.framework.TestCase; +import org.junit.Test; import java.lang.reflect.Field; import java.lang.reflect.Modifier; @@ -27,7 +28,7 @@ interface Builder { void setName(Bean bean, T name); } -public class ClassGeneratorTest { +public class ClassGeneratorTest extends TestCase { @SuppressWarnings("unchecked") @Test diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/MixinTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/MixinTest.java index 5ed7a6eff22..556b7759c0f 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/MixinTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/MixinTest.java @@ -16,11 +16,10 @@ */ package org.apache.dubbo.common.bytecode; -import org.junit.jupiter.api.Test; +import junit.framework.TestCase; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class MixinTest { +public class MixinTest extends TestCase { @Test public void testMain() throws Exception { diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/ProxyTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/ProxyTest.java index ac626e05aab..1f4abca57a7 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/ProxyTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/ProxyTest.java @@ -16,28 +16,32 @@ */ package org.apache.dubbo.common.bytecode; +import junit.framework.TestCase; import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import net.sf.cglib.proxy.MethodProxy; +import org.junit.Assert; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; -public class ProxyTest { +public class ProxyTest extends TestCase { @Test public void testMain() throws Exception { Proxy proxy = Proxy.getProxy(ITest.class, ITest.class); - ITest instance = (ITest) proxy.newInstance((proxy1, method, args) -> { - if ("getName".equals(method.getName())) { - assertEquals(args.length, 0); - } else if ("setName".equals(method.getName())) { - assertEquals(args.length, 2); - assertEquals(args[0], "qianlei"); - assertEquals(args[1], "hello"); + ITest instance = (ITest) proxy.newInstance(new InvocationHandler() { + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + if ("getName".equals(method.getName())) { + assertEquals(args.length, 0); + } else if ("setName".equals(method.getName())) { + assertEquals(args.length, 2); + assertEquals(args[0], "qianlei"); + assertEquals(args[1], "hello"); + } + return null; } - return null; }); assertNull(instance.getName()); @@ -46,19 +50,27 @@ public void testMain() throws Exception { @Test public void testCglibProxy() throws Exception { - ITest test = (ITest) Proxy.getProxy(ITest.class).newInstance((proxy, method, args) -> { - System.out.println(method.getName()); - return null; + ITest test = (ITest) Proxy.getProxy(ITest.class).newInstance(new InvocationHandler() { + + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + System.out.println(method.getName()); + return null; + } }); Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(test.getClass()); - enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> null); + enhancer.setCallback(new MethodInterceptor() { + + public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { + return null; + } + }); try { enhancer.create(); } catch (IllegalArgumentException e) { e.printStackTrace(); - Assertions.fail(); + Assert.fail(); } } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/WrapperTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/WrapperTest.java index ab5dff43393..fe4b5df10f5 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/WrapperTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/WrapperTest.java @@ -16,12 +16,12 @@ */ package org.apache.dubbo.common.bytecode; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; public class WrapperTest { @Test @@ -63,54 +63,48 @@ public void test_makeEmptyClass() throws Exception { @Test public void testHasMethod() throws Exception { Wrapper w = Wrapper.getWrapper(I1.class); - Assertions.assertTrue(w.hasMethod("setName")); - Assertions.assertTrue(w.hasMethod("hello")); - Assertions.assertTrue(w.hasMethod("showInt")); - Assertions.assertTrue(w.hasMethod("getFloat")); - Assertions.assertTrue(w.hasMethod("setFloat")); - Assertions.assertFalse(w.hasMethod("setFloatXXX")); + Assert.assertTrue(w.hasMethod("setName")); + Assert.assertTrue(w.hasMethod("hello")); + Assert.assertTrue(w.hasMethod("showInt")); + Assert.assertTrue(w.hasMethod("getFloat")); + Assert.assertTrue(w.hasMethod("setFloat")); + Assert.assertFalse(w.hasMethod("setFloatXXX")); } @Test public void testWrapperObject() throws Exception { Wrapper w = Wrapper.getWrapper(Object.class); - Assertions.assertTrue(w.getMethodNames().length == 4); - Assertions.assertTrue(w.getPropertyNames().length == 0); - Assertions.assertEquals(null, w.getPropertyType(null)); + Assert.assertTrue(w.getMethodNames().length == 4); + Assert.assertTrue(w.getPropertyNames().length == 0); + Assert.assertEquals(null, w.getPropertyType(null)); } - @Test + @Test(expected = NoSuchPropertyException.class) public void testGetPropertyValue() throws Exception { - Assertions.assertThrows(NoSuchPropertyException.class, () -> { - Wrapper w = Wrapper.getWrapper(Object.class); - w.getPropertyValue(null, null); - }); + Wrapper w = Wrapper.getWrapper(Object.class); + w.getPropertyValue(null, null); } - @Test + @Test(expected = NoSuchPropertyException.class) public void testSetPropertyValue() throws Exception { - Assertions.assertThrows(NoSuchPropertyException.class, () -> { - Wrapper w = Wrapper.getWrapper(Object.class); - w.setPropertyValue(null, null, null); - }); + Wrapper w = Wrapper.getWrapper(Object.class); + w.setPropertyValue(null, null, null); } @Test public void testInvokeWrapperObject() throws Exception { Wrapper w = Wrapper.getWrapper(Object.class); Object instance = new Object(); - Assertions.assertEquals(instance.getClass(), (Class) w.invokeMethod(instance, "getClass", null, null)); - Assertions.assertEquals(instance.hashCode(), (int) w.invokeMethod(instance, "hashCode", null, null)); - Assertions.assertEquals(instance.toString(), (String) w.invokeMethod(instance, "toString", null, null)); - Assertions.assertEquals(true, (boolean) w.invokeMethod(instance, "equals", null, new Object[]{instance})); + Assert.assertEquals(instance.getClass(), (Class) w.invokeMethod(instance, "getClass", null, null)); + Assert.assertEquals(instance.hashCode(), (int) w.invokeMethod(instance, "hashCode", null, null)); + Assert.assertEquals(instance.toString(), (String) w.invokeMethod(instance, "toString", null, null)); + Assert.assertEquals(true, (boolean) w.invokeMethod(instance, "equals", null, new Object[]{instance})); } - @Test + @Test(expected = NoSuchMethodException.class) public void testNoSuchMethod() throws Exception { - Assertions.assertThrows(NoSuchMethodException.class, () -> { - Wrapper w = Wrapper.getWrapper(Object.class); - w.invokeMethod(new Object(), "__XX__", null, null); - }); + Wrapper w = Wrapper.getWrapper(Object.class); + w.invokeMethod(new Object(), "__XX__", null, null); } /** diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/compiler/support/AdaptiveCompilerTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/compiler/support/AdaptiveCompilerTest.java index 6e6e5848aa8..136d43400a6 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/compiler/support/AdaptiveCompilerTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/compiler/support/AdaptiveCompilerTest.java @@ -16,8 +16,8 @@ */ package org.apache.dubbo.common.compiler.support; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; public class AdaptiveCompilerTest extends JavaCodeTest { @@ -27,7 +27,7 @@ public void testAvailableCompiler() throws Exception { AdaptiveCompiler compiler = new AdaptiveCompiler(); Class clazz = compiler.compile(getSimpleCode(), AdaptiveCompiler.class.getClassLoader()); HelloService helloService = (HelloService) clazz.newInstance(); - Assertions.assertEquals("Hello world!", helloService.sayHello()); + Assert.assertEquals("Hello world!", helloService.sayHello()); } } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/compiler/support/ClassUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/compiler/support/ClassUtilsTest.java index 8acf00bfeef..4f93cc4e95e 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/compiler/support/ClassUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/compiler/support/ClassUtilsTest.java @@ -16,10 +16,11 @@ */ package org.apache.dubbo.common.compiler.support; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.lang.reflect.GenericArrayType; +import java.lang.reflect.ParameterizedType; import java.lang.reflect.TypeVariable; import java.util.ArrayList; import java.util.Collection; @@ -32,22 +33,22 @@ public class ClassUtilsTest { @Test public void testNewInstance() { HelloServiceImpl0 instance = (HelloServiceImpl0) ClassUtils.newInstance(HelloServiceImpl0.class.getName()); - Assertions.assertEquals("Hello world!", instance.sayHello()); + Assert.assertEquals("Hello world!", instance.sayHello()); } - @Test + @Test(expected = IllegalStateException.class) public void testNewInstance0() { - Assertions.assertThrows(IllegalStateException.class, () -> ClassUtils.newInstance(PrivateHelloServiceImpl.class.getName())); + ClassUtils.newInstance(PrivateHelloServiceImpl.class.getName()); } - @Test + @Test(expected = IllegalStateException.class) public void testNewInstance1() { - Assertions.assertThrows(IllegalStateException.class, () -> ClassUtils.newInstance("org.apache.dubbo.common.compiler.support.internal.HelloServiceInternalImpl")); + ClassUtils.newInstance("org.apache.dubbo.common.compiler.support.internal.HelloServiceInternalImpl"); } - @Test + @Test(expected = IllegalStateException.class) public void testNewInstance2() { - Assertions.assertThrows(IllegalStateException.class, () -> ClassUtils.newInstance("org.apache.dubbo.common.compiler.support.internal.NotExistsImpl")); + ClassUtils.newInstance("org.apache.dubbo.common.compiler.support.internal.NotExistsImpl"); } @Test @@ -55,9 +56,9 @@ public void testForName() { ClassUtils.forName(new String[]{"org.apache.dubbo.common.compiler.support"}, "HelloServiceImpl0"); } - @Test + @Test(expected = IllegalStateException.class) public void testForName1() { - Assertions.assertThrows(IllegalStateException.class, () -> ClassUtils.forName(new String[]{"org.apache.dubbo.common.compiler.support"}, "HelloServiceImplXX")); + ClassUtils.forName(new String[]{"org.apache.dubbo.common.compiler.support"}, "HelloServiceImplXX"); } @Test @@ -82,95 +83,86 @@ public void testForName2() { @Test public void testGetBoxedClass() { - Assertions.assertEquals(Boolean.class, ClassUtils.getBoxedClass(boolean.class)); - Assertions.assertEquals(Character.class, ClassUtils.getBoxedClass(char.class)); - Assertions.assertEquals(Byte.class, ClassUtils.getBoxedClass(byte.class)); - Assertions.assertEquals(Short.class, ClassUtils.getBoxedClass(short.class)); - Assertions.assertEquals(Integer.class, ClassUtils.getBoxedClass(int.class)); - Assertions.assertEquals(Long.class, ClassUtils.getBoxedClass(long.class)); - Assertions.assertEquals(Float.class, ClassUtils.getBoxedClass(float.class)); - Assertions.assertEquals(Double.class, ClassUtils.getBoxedClass(double.class)); - Assertions.assertEquals(ClassUtilsTest.class, ClassUtils.getBoxedClass(ClassUtilsTest.class)); + Assert.assertEquals(Boolean.class, ClassUtils.getBoxedClass(boolean.class)); + Assert.assertEquals(Character.class, ClassUtils.getBoxedClass(char.class)); + Assert.assertEquals(Byte.class, ClassUtils.getBoxedClass(byte.class)); + Assert.assertEquals(Short.class, ClassUtils.getBoxedClass(short.class)); + Assert.assertEquals(Integer.class, ClassUtils.getBoxedClass(int.class)); + Assert.assertEquals(Long.class, ClassUtils.getBoxedClass(long.class)); + Assert.assertEquals(Float.class, ClassUtils.getBoxedClass(float.class)); + Assert.assertEquals(Double.class, ClassUtils.getBoxedClass(double.class)); + Assert.assertEquals(ClassUtilsTest.class, ClassUtils.getBoxedClass(ClassUtilsTest.class)); } @Test public void testBoxedAndUnboxed() { - Assertions.assertEquals(Boolean.valueOf(true), ClassUtils.boxed(true)); - Assertions.assertEquals(Character.valueOf('0'), ClassUtils.boxed('0')); - Assertions.assertEquals(Byte.valueOf((byte) 0), ClassUtils.boxed((byte) 0)); - Assertions.assertEquals(Short.valueOf((short) 0), ClassUtils.boxed((short) 0)); - Assertions.assertEquals(Integer.valueOf((int) 0), ClassUtils.boxed((int) 0)); - Assertions.assertEquals(Long.valueOf((long) 0), ClassUtils.boxed((long) 0)); - Assertions.assertEquals(Float.valueOf((float) 0), ClassUtils.boxed((float) 0)); - Assertions.assertEquals(Double.valueOf((double) 0), ClassUtils.boxed((double) 0)); - - Assertions.assertEquals(true, ClassUtils.unboxed(Boolean.valueOf(true))); - Assertions.assertEquals('0', ClassUtils.unboxed(Character.valueOf('0'))); - Assertions.assertEquals((byte) 0, ClassUtils.unboxed(Byte.valueOf((byte) 0))); - Assertions.assertEquals((short) 0, ClassUtils.unboxed(Short.valueOf((short) 0))); - Assertions.assertEquals(0, ClassUtils.unboxed(Integer.valueOf((int) 0))); - Assertions.assertEquals((long) 0, ClassUtils.unboxed(Long.valueOf((long) 0))); -// Assertions.assertEquals((float) 0, ClassUtils.unboxed(Float.valueOf((float) 0)), ((float) 0)); -// Assertions.assertEquals((double) 0, ClassUtils.unboxed(Double.valueOf((double) 0)), ((double) 0)); + Assert.assertEquals(Boolean.valueOf(true), ClassUtils.boxed(true)); + Assert.assertEquals(Character.valueOf('0'), ClassUtils.boxed('0')); + Assert.assertEquals(Byte.valueOf((byte) 0), ClassUtils.boxed((byte) 0)); + Assert.assertEquals(Short.valueOf((short) 0), ClassUtils.boxed((short) 0)); + Assert.assertEquals(Integer.valueOf((int) 0), ClassUtils.boxed((int) 0)); + Assert.assertEquals(Long.valueOf((long) 0), ClassUtils.boxed((long) 0)); + Assert.assertEquals(Float.valueOf((float) 0), ClassUtils.boxed((float) 0)); + Assert.assertEquals(Double.valueOf((double) 0), ClassUtils.boxed((double) 0)); + + Assert.assertEquals(true, ClassUtils.unboxed(Boolean.valueOf(true))); + Assert.assertEquals('0', ClassUtils.unboxed(Character.valueOf('0'))); + Assert.assertEquals((byte) 0, ClassUtils.unboxed(Byte.valueOf((byte) 0))); + Assert.assertEquals((short) 0, ClassUtils.unboxed(Short.valueOf((short) 0))); + Assert.assertEquals(0, ClassUtils.unboxed(Integer.valueOf((int) 0))); + Assert.assertEquals((long) 0, ClassUtils.unboxed(Long.valueOf((long) 0))); + Assert.assertEquals((float) 0, ClassUtils.unboxed(Float.valueOf((float) 0)), ((float)0)); + Assert.assertEquals((double) 0, ClassUtils.unboxed(Double.valueOf((double) 0)), ((double)0)); } @Test - public void testGetSize() { - Assertions.assertEquals(0, ClassUtils.getSize(null)); - List list = new ArrayList<>(); - list.add(1); - Assertions.assertEquals(1, ClassUtils.getSize(list)); - Map map = new HashMap(); - map.put(1, 1); - Assertions.assertEquals(1, ClassUtils.getSize(map)); + public void testGetSize(){ + Assert.assertEquals(0, ClassUtils.getSize(null)); + List list = new ArrayList<>();list.add(1); + Assert.assertEquals(1, ClassUtils.getSize(list)); + Map map = new HashMap(); map.put(1, 1); + Assert.assertEquals(1, ClassUtils.getSize(map)); int[] array = new int[1]; - Assertions.assertEquals(1, ClassUtils.getSize(array)); - Assertions.assertEquals(-1, ClassUtils.getSize(new Object())); + Assert.assertEquals(1, ClassUtils.getSize(array)); + Assert.assertEquals(-1, ClassUtils.getSize(new Object())); } - @Test - public void testToUri() { - Assertions.assertThrows(RuntimeException.class, () -> ClassUtils.toURI("#xx_abc#hello")); + @Test(expected = RuntimeException.class) + public void testToUri(){ + ClassUtils.toURI("#xx_abc#hello"); } @Test - public void testGetGenericClass() { - Assertions.assertTrue(TypeVariable.class.isAssignableFrom(ClassUtils.getGenericClass(GenericClass.class))); - Assertions.assertTrue(String.class.isAssignableFrom(ClassUtils.getGenericClass(GenericClass0.class))); - Assertions.assertTrue(Collection.class.isAssignableFrom(ClassUtils.getGenericClass(GenericClass1.class))); - Assertions.assertTrue(TypeVariable.class.isAssignableFrom(ClassUtils.getGenericClass(GenericClass2.class))); - Assertions.assertTrue(GenericArrayType.class.isAssignableFrom(ClassUtils.getGenericClass(GenericClass3.class))); + public void testGetGenericClass(){ + Assert.assertTrue(TypeVariable.class.isAssignableFrom(ClassUtils.getGenericClass(GenericClass.class))); + Assert.assertTrue(String.class.isAssignableFrom(ClassUtils.getGenericClass(GenericClass0.class))); + Assert.assertTrue(Collection.class.isAssignableFrom(ClassUtils.getGenericClass(GenericClass1.class))); + Assert.assertTrue(TypeVariable.class.isAssignableFrom(ClassUtils.getGenericClass(GenericClass2.class))); + Assert.assertTrue(GenericArrayType.class.isAssignableFrom(ClassUtils.getGenericClass(GenericClass3.class))); } @Test - public void testGetSizeMethod() { - Assertions.assertEquals("getLength()", ClassUtils.getSizeMethod(GenericClass3.class)); - } - - @Test - public void testGetSimpleClassName() { - Assertions.assertNull(ClassUtils.getSimpleClassName(null)); - Assertions.assertEquals("Map", ClassUtils.getSimpleClassName(Map.class.getName())); - Assertions.assertEquals("Map", ClassUtils.getSimpleClassName(Map.class.getSimpleName())); + public void testGetSizeMethod(){ + Assert.assertEquals("getLength()", ClassUtils.getSizeMethod(GenericClass3.class)); } - private interface GenericInterface { + private interface GenericInterface{ } - private class GenericClass implements GenericInterface { + private class GenericClass implements GenericInterface{ } - private class GenericClass0 implements GenericInterface { + private class GenericClass0 implements GenericInterface{ } - private class GenericClass1 implements GenericInterface> { + private class GenericClass1 implements GenericInterface>{ } - private class GenericClass2 implements GenericInterface { + private class GenericClass2 implements GenericInterface{ } - private class GenericClass3 implements GenericInterface { - public int getLength() { + private class GenericClass3 implements GenericInterface{ + public int getLength(){ return -1; } } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/compiler/support/JavassistCompilerTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/compiler/support/JavassistCompilerTest.java index adbfbb878d0..ac626ba3eb8 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/compiler/support/JavassistCompilerTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/compiler/support/JavassistCompilerTest.java @@ -16,8 +16,8 @@ */ package org.apache.dubbo.common.compiler.support; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.lang.reflect.Method; @@ -30,7 +30,7 @@ public void testCompileJavaClass() throws Exception { // Because javassist compiles using the caller class loader, we should't use HelloService directly Object instance = clazz.newInstance(); Method sayHello = instance.getClass().getMethod("sayHello"); - Assertions.assertEquals("Hello world!", sayHello.invoke(instance)); + Assert.assertEquals("Hello world!", sayHello.invoke(instance)); } /** @@ -42,18 +42,16 @@ public void testCompileJavaClass0() throws Exception { Class clazz = compiler.compile(getSimpleCodeWithoutPackage(), JavassistCompiler.class.getClassLoader()); Object instance = clazz.newInstance(); Method sayHello = instance.getClass().getMethod("sayHello"); - Assertions.assertEquals("Hello world!", sayHello.invoke(instance)); + Assert.assertEquals("Hello world!", sayHello.invoke(instance)); } - @Test + @Test(expected = IllegalStateException.class) public void testCompileJavaClass1() throws Exception { - Assertions.assertThrows(IllegalStateException.class, () -> { - JavassistCompiler compiler = new JavassistCompiler(); - Class clazz = compiler.compile(getSimpleCodeWithSyntax0(), JavassistCompiler.class.getClassLoader()); - Object instance = clazz.newInstance(); - Method sayHello = instance.getClass().getMethod("sayHello"); - Assertions.assertEquals("Hello world!", sayHello.invoke(instance)); - }); + JavassistCompiler compiler = new JavassistCompiler(); + Class clazz = compiler.compile(getSimpleCodeWithSyntax0(), JavassistCompiler.class.getClassLoader()); + Object instance = clazz.newInstance(); + Method sayHello = instance.getClass().getMethod("sayHello"); + Assert.assertEquals("Hello world!", sayHello.invoke(instance)); } @Test @@ -62,7 +60,7 @@ public void testCompileJavaClassWithImport() throws Exception { Class clazz = compiler.compile(getSimpleCodeWithImports(), JavassistCompiler.class.getClassLoader()); Object instance = clazz.newInstance(); Method sayHello = instance.getClass().getMethod("sayHello"); - Assertions.assertEquals("Hello world!", sayHello.invoke(instance)); + Assert.assertEquals("Hello world!", sayHello.invoke(instance)); } @Test @@ -71,6 +69,6 @@ public void testCompileJavaClassWithExtends() throws Exception { Class clazz = compiler.compile(getSimpleCodeWithWithExtends(), JavassistCompiler.class.getClassLoader()); Object instance = clazz.newInstance(); Method sayHello = instance.getClass().getMethod("sayHello"); - Assertions.assertEquals("Hello world3!", sayHello.invoke(instance)); + Assert.assertEquals("Hello world3!", sayHello.invoke(instance)); } } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/compiler/support/JdkCompilerTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/compiler/support/JdkCompilerTest.java index b95e645680f..dd0c3994b17 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/compiler/support/JdkCompilerTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/compiler/support/JdkCompilerTest.java @@ -16,12 +16,12 @@ */ package org.apache.dubbo.common.compiler.support; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.lang.reflect.Method; -public class JdkCompilerTest extends JavaCodeTest { +public class JdkCompilerTest extends JavaCodeTest{ @Test public void test_compileJavaClass() throws Exception { @@ -29,28 +29,24 @@ public void test_compileJavaClass() throws Exception { Class clazz = compiler.compile(getSimpleCode(), JdkCompiler.class.getClassLoader()); Object instance = clazz.newInstance(); Method sayHello = instance.getClass().getMethod("sayHello"); - Assertions.assertEquals("Hello world!", sayHello.invoke(instance)); + Assert.assertEquals("Hello world!", sayHello.invoke(instance)); } - @Test + @Test(expected = IllegalStateException.class) public void test_compileJavaClass0() throws Exception { - Assertions.assertThrows(IllegalStateException.class, () -> { - JdkCompiler compiler = new JdkCompiler(); - Class clazz = compiler.compile(getSimpleCodeWithoutPackage(), JdkCompiler.class.getClassLoader()); - Object instance = clazz.newInstance(); - Method sayHello = instance.getClass().getMethod("sayHello"); - Assertions.assertEquals("Hello world!", sayHello.invoke(instance)); - }); + JdkCompiler compiler = new JdkCompiler(); + Class clazz = compiler.compile(getSimpleCodeWithoutPackage(), JdkCompiler.class.getClassLoader()); + Object instance = clazz.newInstance(); + Method sayHello = instance.getClass().getMethod("sayHello"); + Assert.assertEquals("Hello world!", sayHello.invoke(instance)); } - @Test + @Test(expected = IllegalStateException.class) public void test_compileJavaClass1() throws Exception { - Assertions.assertThrows(IllegalStateException.class, () -> { - JdkCompiler compiler = new JdkCompiler(); - Class clazz = compiler.compile(getSimpleCodeWithSyntax(), JdkCompiler.class.getClassLoader()); - Object instance = clazz.newInstance(); - Method sayHello = instance.getClass().getMethod("sayHello"); - Assertions.assertEquals("Hello world!", sayHello.invoke(instance)); - }); + JdkCompiler compiler = new JdkCompiler(); + Class clazz = compiler.compile(getSimpleCodeWithSyntax(), JdkCompiler.class.getClassLoader()); + Object instance = clazz.newInstance(); + Method sayHello = instance.getClass().getMethod("sayHello"); + Assert.assertEquals("Hello world!", sayHello.invoke(instance)); } } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/concurrent/CompletableFutureTaskTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/concurrent/CompletableFutureTaskTest.java index 2d0cb48fef6..4513721366a 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/concurrent/CompletableFutureTaskTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/concurrent/CompletableFutureTaskTest.java @@ -17,7 +17,7 @@ package org.apache.dubbo.common.concurrent; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.ArgumentMatchers.any; @@ -33,7 +33,7 @@ import java.util.concurrent.TimeUnit; import org.apache.dubbo.common.utils.NamedThreadFactory; -import org.junit.jupiter.api.Test; +import org.junit.Test; public class CompletableFutureTaskTest { diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/concurrent/ExecutionListTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/concurrent/ExecutionListTest.java new file mode 100644 index 00000000000..417b4a49dab --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/concurrent/ExecutionListTest.java @@ -0,0 +1,94 @@ +/* + * 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 org.apache.dubbo.common.concurrent; + +import org.junit.Before; +import org.junit.Test; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executor; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +public class ExecutionListTest { + private ExecutionList executionList; + + @Before + public void setUp() throws Exception { + this.executionList = new ExecutionList(); + } + + @Test(expected = NullPointerException.class) + public void testAddNullRunnable() { + this.executionList.add(null, mock(Executor.class)); + } + + @Test + public void testAddRunnableToExecutor() { + Executor mockedExecutor = mock(Executor.class); + + this.executionList.add(mock(Runnable.class), mockedExecutor); + this.executionList.execute(); + + verify(mockedExecutor).execute(any(Runnable.class)); + } + + @Test + public void testExecuteRunnableWithDefaultExecutor() throws InterruptedException { + final CountDownLatch countDownLatch = new CountDownLatch(1); + this.executionList.add(countDownLatch::countDown, null); + + this.executionList.execute(); + countDownLatch.await(); + } + + @Test + public void testExceptionForExecutor() { + Executor mockedExecutor = mock(Executor.class); + doThrow(new RuntimeException()).when(mockedExecutor).execute(any(Runnable.class)); + + this.executionList.add(mock(Runnable.class), mockedExecutor); + this.executionList.execute(); + } + + @Test + public void testNotRunSameRunnableTwice() { + Executor mockedExecutor = mock(Executor.class); + + this.executionList.add(mock(Runnable.class), mockedExecutor); + + this.executionList.execute(); + this.executionList.execute(); + + verify(mockedExecutor).execute(any(Runnable.class)); + } + + @Test + public void testRunImmediatelyAfterExecuted() { + Executor mockedExecutor = mock(Executor.class); + + this.executionList.add(mock(Runnable.class), mockedExecutor); + this.executionList.execute(); + this.executionList.add(mock(Runnable.class), mockedExecutor); + + verify(mockedExecutor, times(2)).execute(any(Runnable.class)); + } +} \ No newline at end of file diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/config/ConfigurationUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/config/ConfigurationUtilsTest.java deleted file mode 100644 index a16a3748310..00000000000 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/config/ConfigurationUtilsTest.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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 org.apache.dubbo.common.config; - -import org.apache.dubbo.common.Constants; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.Map; - -/** - * - */ -public class ConfigurationUtilsTest { - - @Test - public void testGetServerShutdownTimeout () { - System.setProperty(Constants.SHUTDOWN_WAIT_KEY, " 10000"); - Assertions.assertEquals(10000, ConfigurationUtils.getServerShutdownTimeout()); - System.clearProperty(Constants.SHUTDOWN_WAIT_KEY); - } - - @Test - public void testGetProperty () { - System.setProperty(Constants.SHUTDOWN_WAIT_KEY, " 10000"); - Assertions.assertEquals("10000", ConfigurationUtils.getProperty(Constants.SHUTDOWN_WAIT_KEY)); - System.clearProperty(Constants.SHUTDOWN_WAIT_KEY); - } - - @Test - public void testParseSingleProperties() throws Exception { - String p1 = "aaa=bbb"; - Map result = ConfigurationUtils.parseProperties(p1); - Assertions.assertEquals(1, result.size()); - Assertions.assertEquals("bbb", result.get("aaa")); - } - - @Test - public void testParseMultipleProperties() throws Exception { - String p1 = "aaa=bbb\nccc=ddd"; - Map result = ConfigurationUtils.parseProperties(p1); - Assertions.assertEquals(2, result.size()); - Assertions.assertEquals("bbb", result.get("aaa")); - Assertions.assertEquals("ddd", result.get("ccc")); - } - - @Test - public void testEscapedNewLine() throws Exception { - String p1 = "dubbo.registry.address=zookeeper://127.0.0.1:2181\\\\ndubbo.protocol.port=20880"; - Map result = ConfigurationUtils.parseProperties(p1); - Assertions.assertEquals(1, result.size()); - Assertions.assertEquals("zookeeper://127.0.0.1:2181\\ndubbo.protocol.port=20880", result.get("dubbo.registry.address")); - } -} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/config/PropertiesConfigurationTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/config/PropertiesConfigurationTest.java index 8a23ed2faa6..5c5c4077596 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/config/PropertiesConfigurationTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/config/PropertiesConfigurationTest.java @@ -16,15 +16,15 @@ */ package org.apache.dubbo.common.config; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; public class PropertiesConfigurationTest { @Test public void testOrderPropertiesProviders() { PropertiesConfiguration configuration = new PropertiesConfiguration("test", null); - Assertions.assertTrue(configuration.getInternalProperty("testKey").equals("999")); + Assert.assertTrue(configuration.getInternalProperty("testKey").equals("999")); } } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoaderTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoaderTest.java index 12d8aa92af7..185296fa16f 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoaderTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoaderTest.java @@ -49,9 +49,8 @@ import org.apache.dubbo.common.extension.ext9_empty.impl.Ext9EmptyImpl; import org.apache.dubbo.common.extension.injection.InjectExt; import org.apache.dubbo.common.extension.injection.impl.InjectExtImpl; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.util.HashSet; import java.util.List; @@ -60,13 +59,13 @@ import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.anyOf; import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsString; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.junit.matchers.JUnitMatchers.containsString; public class ExtensionLoaderTest { @Test @@ -87,7 +86,7 @@ public void test_getExtensionLoader_NotInterface() throws Exception { fail(); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage(), - containsString("Extension type (class org.apache.dubbo.common.extension.ExtensionLoaderTest) is not an interface")); + containsString("Extension type(class org.apache.dubbo.common.extension.ExtensionLoaderTest) is not interface")); } } @@ -99,8 +98,8 @@ public void test_getExtensionLoader_NotSpiAnnotation() throws Exception { } catch (IllegalArgumentException expected) { assertThat(expected.getMessage(), allOf(containsString("org.apache.dubbo.common.extension.NoSpiExt"), - containsString("is not an extension"), - containsString("NOT annotated with @SPI"))); + containsString("is not extension"), + containsString("WITHOUT @SPI Annotation"))); } } @@ -263,7 +262,7 @@ public void test_AddExtension_ExceptionWhenExistedExtension() throws Exception { ExtensionLoader.getExtensionLoader(AddExt1.class).addExtension("impl1", AddExt1_ManualAdd1.class); fail(); } catch (IllegalStateException expected) { - assertThat(expected.getMessage(), containsString("Extension name impl1 already exists (Extension interface org.apache.dubbo.common.extension.ext8_add.AddExt1)!")); + assertThat(expected.getMessage(), containsString("Extension name impl1 already existed(Extension interface org.apache.dubbo.common.extension.ext8_add.AddExt1)!")); } } @@ -286,7 +285,7 @@ public void test_AddExtension_Adaptive_ExceptionWhenExistedAdaptive() throws Exc loader.addExtension(null, AddExt1_ManualAdaptive.class); fail(); } catch (IllegalStateException expected) { - assertThat(expected.getMessage(), containsString("Adaptive Extension already exists (Extension interface org.apache.dubbo.common.extension.ext8_add.AddExt1)!")); + assertThat(expected.getMessage(), containsString("Adaptive Extension already existed(Extension interface org.apache.dubbo.common.extension.ext8_add.AddExt1)!")); } } @@ -335,7 +334,7 @@ public void test_replaceExtension_ExceptionWhenNotExistedExtension() throws Exce ExtensionLoader.getExtensionLoader(AddExt1.class).replaceExtension("NotExistedExtension", AddExt1_ManualAdd1.class); fail(); } catch (IllegalStateException expected) { - assertThat(expected.getMessage(), containsString("Extension name NotExistedExtension doesn't exist (Extension interface org.apache.dubbo.common.extension.ext8_add.AddExt1)")); + assertThat(expected.getMessage(), containsString("Extension name NotExistedExtension not existed(Extension interface org.apache.dubbo.common.extension.ext8_add.AddExt1)")); } } @@ -347,7 +346,7 @@ public void test_replaceExtension_Adaptive_ExceptionWhenNotExistedExtension() th loader.replaceExtension(null, AddExt4_ManualAdaptive.class); fail(); } catch (IllegalStateException expected) { - assertThat(expected.getMessage(), containsString("Adaptive Extension doesn't exist (Extension interface org.apache.dubbo.common.extension.ext8_add.AddExt4)")); + assertThat(expected.getMessage(), containsString("Adaptive Extension not existed(Extension interface org.apache.dubbo.common.extension.ext8_add.AddExt4)")); } } @@ -361,7 +360,7 @@ public void test_InitError() throws Exception { loader.getExtension("error"); fail(); } catch (IllegalStateException expected) { - assertThat(expected.getMessage(), containsString("Failed to load extension class (interface: interface org.apache.dubbo.common.extension.ext7.InitErrorExt")); + assertThat(expected.getMessage(), containsString("Failed to load extension class(interface: interface org.apache.dubbo.common.extension.ext7.InitErrorExt")); assertThat(expected.getCause(), instanceOf(ExceptionInInitializerError.class)); } } @@ -372,22 +371,22 @@ public void testLoadActivateExtension() throws Exception { URL url = URL.valueOf("test://localhost/test"); List list = ExtensionLoader.getExtensionLoader(ActivateExt1.class) .getActivateExtension(url, new String[]{}, "default_group"); - Assertions.assertEquals(1, list.size()); - Assertions.assertTrue(list.get(0).getClass() == ActivateExt1Impl1.class); + Assert.assertEquals(1, list.size()); + Assert.assertTrue(list.get(0).getClass() == ActivateExt1Impl1.class); // test group url = url.addParameter(Constants.GROUP_KEY, "group1"); list = ExtensionLoader.getExtensionLoader(ActivateExt1.class) .getActivateExtension(url, new String[]{}, "group1"); - Assertions.assertEquals(1, list.size()); - Assertions.assertTrue(list.get(0).getClass() == GroupActivateExtImpl.class); + Assert.assertEquals(1, list.size()); + Assert.assertTrue(list.get(0).getClass() == GroupActivateExtImpl.class); // test old @Activate group url = url.addParameter(Constants.GROUP_KEY, "old_group"); list = ExtensionLoader.getExtensionLoader(ActivateExt1.class) .getActivateExtension(url, new String[]{}, "old_group"); - Assertions.assertEquals(2, list.size()); - Assertions.assertTrue(list.get(0).getClass() == OldActivateExt1Impl2.class + Assert.assertEquals(2, list.size()); + Assert.assertTrue(list.get(0).getClass() == OldActivateExt1Impl2.class || list.get(0).getClass() == OldActivateExt1Impl3.class); // test value @@ -396,17 +395,17 @@ public void testLoadActivateExtension() throws Exception { url = url.addParameter("value", "value"); list = ExtensionLoader.getExtensionLoader(ActivateExt1.class) .getActivateExtension(url, new String[]{}, "value"); - Assertions.assertEquals(1, list.size()); - Assertions.assertTrue(list.get(0).getClass() == ValueActivateExtImpl.class); + Assert.assertEquals(1, list.size()); + Assert.assertTrue(list.get(0).getClass() == ValueActivateExtImpl.class); // test order url = URL.valueOf("test://localhost/test"); url = url.addParameter(Constants.GROUP_KEY, "order"); list = ExtensionLoader.getExtensionLoader(ActivateExt1.class) .getActivateExtension(url, new String[]{}, "order"); - Assertions.assertEquals(2, list.size()); - Assertions.assertTrue(list.get(0).getClass() == OrderActivateExtImpl1.class); - Assertions.assertTrue(list.get(1).getClass() == OrderActivateExtImpl2.class); + Assert.assertEquals(2, list.size()); + Assert.assertTrue(list.get(0).getClass() == OrderActivateExtImpl1.class); + Assert.assertTrue(list.get(1).getClass() == OrderActivateExtImpl2.class); } @Test @@ -415,16 +414,16 @@ public void testLoadDefaultActivateExtension() throws Exception { URL url = URL.valueOf("test://localhost/test?ext=order1,default"); List list = ExtensionLoader.getExtensionLoader(ActivateExt1.class) .getActivateExtension(url, "ext", "default_group"); - Assertions.assertEquals(2, list.size()); - Assertions.assertTrue(list.get(0).getClass() == OrderActivateExtImpl1.class); - Assertions.assertTrue(list.get(1).getClass() == ActivateExt1Impl1.class); + Assert.assertEquals(2, list.size()); + Assert.assertTrue(list.get(0).getClass() == OrderActivateExtImpl1.class); + Assert.assertTrue(list.get(1).getClass() == ActivateExt1Impl1.class); url = URL.valueOf("test://localhost/test?ext=default,order1"); list = ExtensionLoader.getExtensionLoader(ActivateExt1.class) .getActivateExtension(url, "ext", "default_group"); - Assertions.assertEquals(2, list.size()); - Assertions.assertTrue(list.get(0).getClass() == ActivateExt1Impl1.class); - Assertions.assertTrue(list.get(1).getClass() == OrderActivateExtImpl1.class); + Assert.assertEquals(2, list.size()); + Assert.assertTrue(list.get(0).getClass() == ActivateExt1Impl1.class); + Assert.assertTrue(list.get(1).getClass() == OrderActivateExtImpl1.class); } @Test @@ -432,9 +431,9 @@ public void testInjectExtension() { // test default InjectExt injectExt = ExtensionLoader.getExtensionLoader(InjectExt.class).getExtension("injection"); InjectExtImpl injectExtImpl = (InjectExtImpl) injectExt; - Assertions.assertNotNull(injectExtImpl.getSimpleExt()); - Assertions.assertNull(injectExtImpl.getSimpleExt1()); - Assertions.assertNull(injectExtImpl.getGenericType()); + Assert.assertNotNull(injectExtImpl.getSimpleExt()); + Assert.assertNull(injectExtImpl.getSimpleExt1()); + Assert.assertNull(injectExtImpl.getGenericType()); } -} +} \ No newline at end of file diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoader_Adaptive_Test.java b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoader_Adaptive_Test.java index fe64a63b6ed..47942030a12 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoader_Adaptive_Test.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoader_Adaptive_Test.java @@ -29,19 +29,19 @@ import org.apache.dubbo.common.extension.ext6_inject.impl.Ext6Impl2; import org.apache.dubbo.common.utils.LogUtil; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.util.HashMap; import java.util.Map; import static org.hamcrest.CoreMatchers.allOf; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.junit.matchers.JUnitMatchers.containsString; public class ExtensionLoader_Adaptive_Test { @@ -147,8 +147,8 @@ public void test_getAdaptiveExtension_ExceptionWhenNoAdaptiveMethodOnInterface() fail(); } catch (IllegalStateException expected) { assertThat(expected.getMessage(), - allOf(containsString("Can't create adaptive extension interface org.apache.dubbo.common.extension.ext5.NoAdaptiveMethodExt"), - containsString("No adaptive method exist on extension org.apache.dubbo.common.extension.ext5.NoAdaptiveMethodExt, refuse to create the adaptive class"))); + allOf(containsString("Can not create adaptive extension interface org.apache.dubbo.common.extension.ext5.NoAdaptiveMethodExt"), + containsString("No adaptive method on extension org.apache.dubbo.common.extension.ext5.NoAdaptiveMethodExt, refuse to create the adaptive class"))); } // report same error when get is invoked for multiple times try { @@ -156,8 +156,8 @@ public void test_getAdaptiveExtension_ExceptionWhenNoAdaptiveMethodOnInterface() fail(); } catch (IllegalStateException expected) { assertThat(expected.getMessage(), - allOf(containsString("Can't create adaptive extension interface org.apache.dubbo.common.extension.ext5.NoAdaptiveMethodExt"), - containsString("No adaptive method exist on extension org.apache.dubbo.common.extension.ext5.NoAdaptiveMethodExt, refuse to create the adaptive class"))); + allOf(containsString("Can not create adaptive extension interface org.apache.dubbo.common.extension.ext5.NoAdaptiveMethodExt"), + containsString("No adaptive method on extension org.apache.dubbo.common.extension.ext5.NoAdaptiveMethodExt, refuse to create the adaptive class"))); } } @@ -185,7 +185,7 @@ public void test_getAdaptiveExtension_ExceptionWhenNoUrlAttribute() throws Excep ExtensionLoader.getExtensionLoader(NoUrlParamExt.class).getAdaptiveExtension(); fail(); } catch (Exception expected) { - assertThat(expected.getMessage(), containsString("Failed to create adaptive class for interface ")); + assertThat(expected.getMessage(), containsString("fail to create adaptive class for interface ")); assertThat(expected.getMessage(), containsString(": not found url parameter or url attribute in parameters of method ")); } } @@ -218,7 +218,7 @@ public void test_urlHolder_getAdaptiveExtension_noExtension() throws Exception { ext.echo(holder, "haha"); fail(); } catch (IllegalStateException expected) { - assertThat(expected.getMessage(), containsString("Failed to get extension")); + assertThat(expected.getMessage(), containsString("Fail to get extension(")); } url = url.addParameter("ext2", "XXX"); @@ -281,7 +281,7 @@ public void test_urlHolder_getAdaptiveExtension_ExceptionWhenNameNotProvided() t ext.echo(holder, "impl1"); fail(); } catch (IllegalStateException expected) { - assertThat(expected.getMessage(), containsString("Failed to get extension")); + assertThat(expected.getMessage(), containsString("Fail to get extension(")); } url = url.addParameter("key1", "impl1"); @@ -290,7 +290,7 @@ public void test_urlHolder_getAdaptiveExtension_ExceptionWhenNameNotProvided() t ext.echo(holder, "haha"); fail(); } catch (IllegalStateException expected) { - assertThat(expected.getMessage(), containsString("Failed to get extension (org.apache.dubbo.common.extension.ext2.Ext2) name from url")); + assertThat(expected.getMessage(), containsString("Fail to get extension(org.apache.dubbo.common.extension.ext2.Ext2) name from url")); } } @@ -304,7 +304,7 @@ public void test_getAdaptiveExtension_inject() throws Exception { assertEquals("Ext6Impl1-echo-Ext1Impl1-echo", ext.echo(url, "ha")); - Assertions.assertTrue(LogUtil.checkNoError(), "can not find error."); + Assert.assertTrue("can not find error.", LogUtil.checkNoError()); LogUtil.stop(); url = url.addParameters("simple.ext", "impl2"); @@ -319,4 +319,4 @@ public void test_getAdaptiveExtension_InjectNotExtFail() throws Exception { Ext6Impl2 impl = (Ext6Impl2) ext; assertNull(impl.getList()); } -} +} \ No newline at end of file diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoader_Adaptive_UseJdkCompiler_Test.java b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoader_Adaptive_UseJdkCompiler_Test.java index 371f8589741..51ac8f765d9 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoader_Adaptive_UseJdkCompiler_Test.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoader_Adaptive_UseJdkCompiler_Test.java @@ -18,16 +18,16 @@ import org.apache.dubbo.common.compiler.support.AdaptiveCompiler; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; +import org.junit.AfterClass; +import org.junit.BeforeClass; public class ExtensionLoader_Adaptive_UseJdkCompiler_Test extends ExtensionLoader_Adaptive_Test { - @BeforeAll + @BeforeClass public static void setUp() throws Exception { AdaptiveCompiler.setDefaultCompiler("jdk"); } - @AfterAll + @AfterClass public static void tearDown() throws Exception { AdaptiveCompiler.setDefaultCompiler("javassist"); } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoader_Compatible_Test.java b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoader_Compatible_Test.java index d9df1367071..f11aff73b6f 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoader_Compatible_Test.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoader_Compatible_Test.java @@ -20,9 +20,9 @@ import org.apache.dubbo.common.extension.compatible.impl.CompatibleExtImpl1; import org.apache.dubbo.common.extension.compatible.impl.CompatibleExtImpl2; -import org.junit.jupiter.api.Test; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertTrue; public class ExtensionLoader_Compatible_Test { diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ext6_inject/impl/Ext6Impl1.java b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ext6_inject/impl/Ext6Impl1.java index 4f746dba8ef..90d2c141ab0 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ext6_inject/impl/Ext6Impl1.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ext6_inject/impl/Ext6Impl1.java @@ -21,15 +21,15 @@ import org.apache.dubbo.common.extension.ext6_inject.Dao; import org.apache.dubbo.common.extension.ext6_inject.Ext6; -import org.junit.jupiter.api.Assertions; +import org.junit.Assert; public class Ext6Impl1 implements Ext6 { public Dao obj; SimpleExt ext1; public void setDao(Dao obj) { - Assertions.assertNotNull(obj, "inject extension instance can not be null"); - Assertions.fail(); + Assert.assertNotNull("inject extension instance can not be null", obj); + Assert.fail(); } public void setExt1(SimpleExt ext1) { diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/support/ActivateComparatorTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/support/ActivateComparatorTest.java index 9181766fc7f..dcc43d170b3 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/support/ActivateComparatorTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/support/ActivateComparatorTest.java @@ -17,8 +17,8 @@ package org.apache.dubbo.common.extension.support; import org.apache.dubbo.common.extension.Activate; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.util.ArrayList; import java.util.Collections; @@ -42,10 +42,10 @@ public void testActivateComparator(){ Collections.sort(filters, ActivateComparator.COMPARATOR); - Assertions.assertEquals(f4, filters.get(0)); - Assertions.assertEquals(f5, filters.get(1)); - Assertions.assertEquals(f3, filters.get(2)); - Assertions.assertEquals(f2, filters.get(3)); - Assertions.assertEquals(f1, filters.get(4)); + Assert.assertEquals(f4, filters.get(0)); + Assert.assertEquals(f5, filters.get(1)); + Assert.assertEquals(f3, filters.get(2)); + Assert.assertEquals(f2, filters.get(3)); + Assert.assertEquals(f1, filters.get(4)); } } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/io/BytesTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/io/BytesTest.java index 71b915c1e40..a00f599f0eb 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/io/BytesTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/io/BytesTest.java @@ -16,14 +16,13 @@ */ package org.apache.dubbo.common.io; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.io.File; import java.io.IOException; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class BytesTest { private final byte[] b1 = "adpfioha;eoh;aldfadl;kfadslkfdajfio123431241235123davas;odvwe;lmzcoqpwoewqogineopwqihwqetup\n\tejqf;lajsfd中文字符0da0gsaofdsf==adfasdfs".getBytes(); @@ -59,24 +58,24 @@ public void testMain() throws Exception { assertThat(bytesWithC64, is(bytes)); } - @Test + @Test(expected = IllegalArgumentException.class) public void testWrongBase64Code() { - Assertions.assertThrows(IllegalArgumentException.class, () -> Bytes.bytes2base64("dubbo".getBytes(), 0, 1, new char[]{'a'})); + Bytes.bytes2base64("dubbo".getBytes(), 0, 1, new char[]{'a'}); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void testWrongOffSet() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> Bytes.bytes2base64("dubbo".getBytes(), -1, 1)); + Bytes.bytes2base64("dubbo".getBytes(), -1, 1); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void testLargeLength() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> Bytes.bytes2base64("dubbo".getBytes(), 0, 100000)); + Bytes.bytes2base64("dubbo".getBytes(), 0, 100000); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void testSmallLength() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> Bytes.bytes2base64("dubbo".getBytes(), 0, -1)); + Bytes.bytes2base64("dubbo".getBytes(), 0, -1); } @Test @@ -116,14 +115,14 @@ public void testZip() throws IOException { assertThat(unzip, is(s.getBytes())); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void testBytes2HexWithWrongOffset() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> Bytes.bytes2hex("hello".getBytes(), -1, 1)); + Bytes.bytes2hex("hello".getBytes(), -1, 1); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void testBytes2HexWithWrongLength() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> Bytes.bytes2hex("hello".getBytes(), 0, 6)); + Bytes.bytes2hex("hello".getBytes(), 0, 6); } private byte[] int2bytes(int x) { diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/io/StreamUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/io/StreamUtilsTest.java index 7c2e6b9e21b..2f8595fef87 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/io/StreamUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/io/StreamUtilsTest.java @@ -16,17 +16,16 @@ */ package org.apache.dubbo.common.io; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.io.PushbackInputStream; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; public class StreamUtilsTest { @@ -118,25 +117,23 @@ public void testLimitedInputStream() throws Exception { is.close(); } - @Test - public void testMarkInputSupport() { - Assertions.assertThrows(IOException.class, () -> { - InputStream is = StreamUtilsTest.class.getResourceAsStream("/StreamUtilsTest.txt"); - try { - is = StreamUtils.markSupportedInputStream(new PushbackInputStream(is), 1); - - is.mark(1); - int read = is.read(); - assertThat(read, is((int) '0')); - - is.skip(1); - is.read(); - } finally { - if (is != null) { - is.close(); - } + @Test(expected = IOException.class) + public void testMarkInputSupport() throws IOException { + InputStream is = StreamUtilsTest.class.getResourceAsStream("/StreamUtilsTest.txt"); + try { + is = StreamUtils.markSupportedInputStream(new PushbackInputStream(is), 1); + + is.mark(1); + int read = is.read(); + assertThat(read, is((int) '0')); + + is.skip(1); + is.read(); + } finally { + if (is != null) { + is.close(); } - }); + } } @Test @@ -148,33 +145,29 @@ public void testSkipForOriginMarkSupportInput() throws IOException { is.close(); } - @Test - public void testReadEmptyByteArray() { - Assertions.assertThrows(NullPointerException.class, () -> { - InputStream is = StreamUtilsTest.class.getResourceAsStream("/StreamUtilsTest.txt"); - try { - is = StreamUtils.limitedInputStream(is, 2); - is.read(null, 0, 1); - } finally { - if (is != null) { - is.close(); - } + @Test(expected = NullPointerException.class) + public void testReadEmptyByteArray() throws IOException { + InputStream is = StreamUtilsTest.class.getResourceAsStream("/StreamUtilsTest.txt"); + try { + is = StreamUtils.limitedInputStream(is, 2); + is.read(null, 0, 1); + } finally { + if (is != null) { + is.close(); } - }); + } } - @Test - public void testReadWithWrongOffset() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { - InputStream is = StreamUtilsTest.class.getResourceAsStream("/StreamUtilsTest.txt"); - try { - is = StreamUtils.limitedInputStream(is, 2); - is.read(new byte[1], -1, 1); - } finally { - if (is != null) { - is.close(); - } + @Test(expected = IndexOutOfBoundsException.class) + public void testReadWithWrongOffset() throws IOException { + InputStream is = StreamUtilsTest.class.getResourceAsStream("/StreamUtilsTest.txt"); + try { + is = StreamUtils.limitedInputStream(is, 2); + is.read(new byte[1], -1, 1); + } finally { + if (is != null) { + is.close(); } - }); + } } } \ No newline at end of file diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/io/UnsafeByteArrayInputStreamTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/io/UnsafeByteArrayInputStreamTest.java index bc792fa3f25..39ab9955e28 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/io/UnsafeByteArrayInputStreamTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/io/UnsafeByteArrayInputStreamTest.java @@ -16,13 +16,12 @@ */ package org.apache.dubbo.common.io; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.io.IOException; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class UnsafeByteArrayInputStreamTest { @Test @@ -59,28 +58,22 @@ public void testRead() throws IOException { assertThat(stream.read(), is((int) 'b')); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void testWrongLength() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { - UnsafeByteArrayInputStream stream = new UnsafeByteArrayInputStream("abc".getBytes()); - stream.read(new byte[1], 0, 100); - }); + UnsafeByteArrayInputStream stream = new UnsafeByteArrayInputStream("abc".getBytes()); + stream.read(new byte[1], 0, 100); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void testWrongOffset() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { - UnsafeByteArrayInputStream stream = new UnsafeByteArrayInputStream("abc".getBytes()); - stream.read(new byte[1], -1, 1); - }); + UnsafeByteArrayInputStream stream = new UnsafeByteArrayInputStream("abc".getBytes()); + stream.read(new byte[1], -1, 1); } - @Test + @Test(expected = NullPointerException.class) public void testReadEmptyByteArray() { - Assertions.assertThrows(NullPointerException.class, () -> { - UnsafeByteArrayInputStream stream = new UnsafeByteArrayInputStream("abc".getBytes()); - stream.read(null, 0, 1); - }); + UnsafeByteArrayInputStream stream = new UnsafeByteArrayInputStream("abc".getBytes()); + stream.read(null, 0, 1); } @Test diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/io/UnsafeByteArrayOutputStreamTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/io/UnsafeByteArrayOutputStreamTest.java index 715c128e00d..f411ae908f9 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/io/UnsafeByteArrayOutputStreamTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/io/UnsafeByteArrayOutputStreamTest.java @@ -16,8 +16,7 @@ */ package org.apache.dubbo.common.io; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; import org.mockito.Mockito; import java.io.IOException; @@ -25,16 +24,16 @@ import java.nio.ByteBuffer; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; public class UnsafeByteArrayOutputStreamTest { - @Test + @Test(expected = IllegalArgumentException.class) public void testWrongSize() { - Assertions.assertThrows(IllegalArgumentException.class, () -> new UnsafeByteArrayOutputStream(-1)); + new UnsafeByteArrayOutputStream(-1); } @Test diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/io/UnsafeStringReaderTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/io/UnsafeStringReaderTest.java index 1ee478f71d6..2cf78d6a521 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/io/UnsafeStringReaderTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/io/UnsafeStringReaderTest.java @@ -16,13 +16,12 @@ */ package org.apache.dubbo.common.io; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.io.IOException; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class UnsafeStringReaderTest { @Test @@ -64,12 +63,10 @@ public void testSkipTooLong() throws IOException { assertThat(skip, is(0L)); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void testWrongLength() throws IOException { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { - UnsafeStringReader reader = new UnsafeStringReader("abc"); - char[] chars = new char[1]; - reader.read(chars, 0, 2); - }); + UnsafeStringReader reader = new UnsafeStringReader("abc"); + char[] chars = new char[1]; + reader.read(chars, 0, 2); } } \ No newline at end of file diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/io/UnsafeStringWriterTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/io/UnsafeStringWriterTest.java index f487441f476..4e423b73bbb 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/io/UnsafeStringWriterTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/io/UnsafeStringWriterTest.java @@ -16,13 +16,12 @@ */ package org.apache.dubbo.common.io; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.io.IOException; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class UnsafeStringWriterTest { @Test @@ -37,11 +36,9 @@ public void testWrite() { assertThat(writer.toString(), is("abc")); } - @Test + @Test(expected = IllegalArgumentException.class) public void testNegativeSize() { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - new UnsafeStringWriter(-1); - }); + new UnsafeStringWriter(-1); } @Test @@ -81,21 +78,17 @@ public void testWriteNull() throws IOException { assertThat(writer.toString(), is("aba")); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void testWriteCharWithWrongLength() throws IOException { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { - UnsafeStringWriter writer = new UnsafeStringWriter(); - char[] chars = new char[0]; - writer.write(chars, 0, 1); - }); + UnsafeStringWriter writer = new UnsafeStringWriter(); + char[] chars = new char[0]; + writer.write(chars, 0, 1); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void testWriteCharWithWrongCombineLength() throws IOException { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { - UnsafeStringWriter writer = new UnsafeStringWriter(); - char[] chars = new char[1]; - writer.write(chars, 1, 1); - }); + UnsafeStringWriter writer = new UnsafeStringWriter(); + char[] chars = new char[1]; + writer.write(chars, 1, 1); } } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/json/JSONReaderTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/json/JSONReaderTest.java index 2688fa26bfe..8607e56e438 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/json/JSONReaderTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/json/JSONReaderTest.java @@ -18,9 +18,9 @@ import org.apache.dubbo.common.io.UnsafeStringReader; -import static org.junit.jupiter.api.Assertions.assertEquals; +import junit.framework.TestCase; -public class JSONReaderTest { +public class JSONReaderTest extends TestCase { public void testMain() throws Exception { String json = "{ name: 'name', friends: [ 1, null, 3.2, ] }"; JSONReader reader = new JSONReader(new UnsafeStringReader(json)); diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/json/JSONTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/json/JSONTest.java index 0a7f9063f33..21050e2a83d 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/json/JSONTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/json/JSONTest.java @@ -16,8 +16,8 @@ */ package org.apache.dubbo.common.json; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.io.StringReader; import java.io.StringWriter; @@ -27,7 +27,7 @@ import java.util.Locale; import java.util.Map; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; @Deprecated public class JSONTest { @@ -42,12 +42,12 @@ public void testException() throws Exception { JSON.json(e, writer); String json = writer.getBuffer().toString(); System.out.println(json); - // Assertions.assertEquals("{\"code\":\"001\",\"message\":\"AAAAAAAA\"}", json); + // Assert.assertEquals("{\"code\":\"001\",\"message\":\"AAAAAAAA\"}", json); StringReader reader = new StringReader(json); MyException result = JSON.parse(reader, MyException.class); - Assertions.assertEquals("001", result.getCode()); - Assertions.assertEquals("AAAAAAAA", result.getMessage()); + Assert.assertEquals("001", result.getCode()); + Assert.assertEquals("AAAAAAAA", result.getMessage()); } @Test @@ -59,11 +59,11 @@ public void testMap() throws Exception { StringWriter writer = new StringWriter(); JSON.json(map, writer); String json = writer.getBuffer().toString(); - Assertions.assertEquals("{\"aaa\":\"bbb\"}", json); + Assert.assertEquals("{\"aaa\":\"bbb\"}", json); StringReader reader = new StringReader(json); Map result = JSON.parse(reader, Map.class); - Assertions.assertEquals("bbb", result.get("aaa")); + Assert.assertEquals("bbb", result.get("aaa")); } @Test @@ -75,12 +75,12 @@ public void testMapArray() throws Exception { StringWriter writer = new StringWriter(); JSON.json(new Object[]{map}, writer); // args String json = writer.getBuffer().toString(); - Assertions.assertEquals("[{\"aaa\":\"bbb\"}]", json); + Assert.assertEquals("[{\"aaa\":\"bbb\"}]", json); StringReader reader = new StringReader(json); Object[] result = JSON.parse(reader, new Class[]{Map.class}); - Assertions.assertEquals(1, result.length); - Assertions.assertEquals("bbb", ((Map) result[0]).get("aaa")); + Assert.assertEquals(1, result.length); + Assert.assertEquals("bbb", ((Map) result[0]).get("aaa")); } @Test @@ -92,11 +92,11 @@ public void testLinkedMap() throws Exception { StringWriter writer = new StringWriter(); JSON.json(map, writer); String json = writer.getBuffer().toString(); - Assertions.assertEquals("{\"aaa\":\"bbb\"}", json); + Assert.assertEquals("{\"aaa\":\"bbb\"}", json); StringReader reader = new StringReader(json); LinkedHashMap result = JSON.parse(reader, LinkedHashMap.class); - Assertions.assertEquals("bbb", result.get("aaa")); + Assert.assertEquals("bbb", result.get("aaa")); } @Test diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/json/JSONWriterTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/json/JSONWriterTest.java index 0d7da710ca9..9066c904ea9 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/json/JSONWriterTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/json/JSONWriterTest.java @@ -16,12 +16,11 @@ */ package org.apache.dubbo.common.json; +import junit.framework.TestCase; import java.io.StringWriter; -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class JSONWriterTest { +public class JSONWriterTest extends TestCase { public void testWriteJson() throws Exception { StringWriter w = new StringWriter(); JSONWriter writer = new JSONWriter(w); diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerAdapterTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerAdapterTest.java index 1d6fbe77e06..9234626b4ee 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerAdapterTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerAdapterTest.java @@ -26,45 +26,51 @@ import org.apache.dubbo.common.logger.log4j2.Log4j2LoggerAdapter; import org.apache.dubbo.common.logger.slf4j.Slf4jLogger; import org.apache.dubbo.common.logger.slf4j.Slf4jLoggerAdapter; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -import java.util.stream.Stream; +import java.util.Arrays; +import java.util.Collection; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; +@RunWith(Parameterized.class) public class LoggerAdapterTest { - static Stream data() { - return Stream.of( - Arguments.of(JclLoggerAdapter.class, JclLogger.class), - Arguments.of(JdkLoggerAdapter.class, JdkLogger.class), - Arguments.of(Log4jLoggerAdapter.class, Log4jLogger.class), - Arguments.of(Slf4jLoggerAdapter.class, Slf4jLogger.class), - Arguments.of(Log4j2LoggerAdapter.class, Log4j2Logger.class) - ); + @Parameterized.Parameters + public static Collection data() { + return Arrays.asList(new Object[][]{ + {JclLoggerAdapter.class, JclLogger.class}, + {JdkLoggerAdapter.class, JdkLogger.class}, + {Log4jLoggerAdapter.class, Log4jLogger.class}, + {Slf4jLoggerAdapter.class, Slf4jLogger.class}, + {Log4j2LoggerAdapter.class, Log4j2Logger.class}, + }); + } + + private Class loggerClass; + private LoggerAdapter loggerAdapter; + + public LoggerAdapterTest(Class loggerAdapterClass, Class loggerClass) throws Exception { + this.loggerClass = loggerClass; + this.loggerAdapter = loggerAdapterClass.newInstance(); } - @ParameterizedTest - @MethodSource("data") - public void testGetLogger(Class loggerAdapterClass, Class loggerClass) throws IllegalAccessException, InstantiationException { - LoggerAdapter loggerAdapter = loggerAdapterClass.newInstance(); + @Test + public void testGetLogger() { Logger logger = loggerAdapter.getLogger(this.getClass()); - assertThat(logger.getClass().isAssignableFrom(loggerClass), is(true)); + assertThat(logger.getClass().isAssignableFrom(this.loggerClass), is(true)); logger = loggerAdapter.getLogger(this.getClass().getSimpleName()); - assertThat(logger.getClass().isAssignableFrom(loggerClass), is(true)); - + assertThat(logger.getClass().isAssignableFrom(this.loggerClass), is(true)); } - @ParameterizedTest - @MethodSource("data") - public void testLevel(Class loggerAdapterClass) throws IllegalAccessException, InstantiationException { - LoggerAdapter loggerAdapter = loggerAdapterClass.newInstance(); + @Test + public void testLevel() { for (Level targetLevel : Level.values()) { loggerAdapter.setLevel(targetLevel); + assertThat(loggerAdapter.getLevel(), is(targetLevel)); } } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerFactoryTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerFactoryTest.java index 25fce0ebdfd..1e718dd6d0e 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerFactoryTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerFactoryTest.java @@ -16,13 +16,13 @@ */ package org.apache.dubbo.common.logger; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.io.File; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class LoggerFactoryTest { @Test @@ -58,12 +58,4 @@ public void testGetLogger() { assertThat(logger1, is(logger2)); } - - @Test - public void shouldReturnSameLogger() { - Logger logger1 = LoggerFactory.getLogger(this.getClass().getName()); - Logger logger2 = LoggerFactory.getLogger(this.getClass().getName()); - - assertThat(logger1, is(logger2)); - } -} +} \ No newline at end of file diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerTest.java index 81987f782da..ed87b152a07 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/logger/LoggerTest.java @@ -19,38 +19,41 @@ import org.apache.dubbo.common.logger.jcl.JclLoggerAdapter; import org.apache.dubbo.common.logger.jdk.JdkLoggerAdapter; import org.apache.dubbo.common.logger.log4j.Log4jLoggerAdapter; -import org.apache.dubbo.common.logger.log4j2.Log4j2LoggerAdapter; import org.apache.dubbo.common.logger.slf4j.Slf4jLoggerAdapter; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -import java.util.stream.Stream; +import java.util.Arrays; +import java.util.Collection; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; +@RunWith(Parameterized.class) public class LoggerTest { - - static Stream data() { - return Stream.of( - Arguments.of(JclLoggerAdapter.class), - Arguments.of(JdkLoggerAdapter.class), - Arguments.of(Log4jLoggerAdapter.class), - Arguments.of(Slf4jLoggerAdapter.class), - Arguments.of(Log4j2LoggerAdapter.class) - ); + @Parameterized.Parameters + public static Collection data() { + return Arrays.asList(new Object[][]{ + {JclLoggerAdapter.class}, + {JdkLoggerAdapter.class}, + {Log4jLoggerAdapter.class}, + {Slf4jLoggerAdapter.class} + }); } - @ParameterizedTest - @MethodSource("data") - public void testAllLogMethod(Class loggerAdapter) throws Exception { + private Logger logger; + + public LoggerTest(Class loggerAdapter) throws Exception { LoggerAdapter adapter = loggerAdapter.newInstance(); adapter.setLevel(Level.ALL); - Logger logger = adapter.getLogger(this.getClass()); + this.logger = adapter.getLogger(this.getClass()); + } + + @Test + public void testAllLogMethod() { logger.error("error"); logger.warn("warn"); logger.info("info"); @@ -70,12 +73,8 @@ public void testAllLogMethod(Class loggerAdapter) throw logger.trace("trace", new Exception("trace")); } - @ParameterizedTest - @MethodSource("data") - public void testLevelEnable(Class loggerAdapter) throws IllegalAccessException, InstantiationException { - LoggerAdapter adapter = loggerAdapter.newInstance(); - adapter.setLevel(Level.ALL); - Logger logger = adapter.getLogger(this.getClass()); + @Test + public void testLevelEnable() { assertThat(logger.isWarnEnabled(), not(nullValue())); assertThat(logger.isTraceEnabled(), not(nullValue())); assertThat(logger.isErrorEnabled(), not(nullValue())); diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/logger/slf4j/Slf4jLoggerTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/logger/slf4j/Slf4jLoggerTest.java index d53ce2e83bf..a8cb0c44e66 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/logger/slf4j/Slf4jLoggerTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/logger/slf4j/Slf4jLoggerTest.java @@ -16,7 +16,7 @@ */ package org.apache.dubbo.common.logger.slf4j; -import org.junit.jupiter.api.Test; +import org.junit.Test; import org.slf4j.Marker; import org.slf4j.spi.LocationAwareLogger; diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/logger/support/FailsafeLoggerTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/logger/support/FailsafeLoggerTest.java index 530c2a1d61c..61a72155bf7 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/logger/support/FailsafeLoggerTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/logger/support/FailsafeLoggerTest.java @@ -17,9 +17,7 @@ package org.apache.dubbo.common.logger.support; import org.apache.dubbo.common.logger.Logger; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; @@ -93,14 +91,12 @@ public void testSuccessLogger() { failsafeLogger.trace("trace", new Exception("trace")); } - @Test + @Test(expected = RuntimeException.class) public void testGetLogger() { - Assertions.assertThrows(RuntimeException.class, () -> { - Logger failLogger = mock(Logger.class); - FailsafeLogger failsafeLogger = new FailsafeLogger(failLogger); + Logger failLogger = mock(Logger.class); + FailsafeLogger failsafeLogger = new FailsafeLogger(failLogger); - doThrow(new RuntimeException()).when(failLogger).error(anyString()); - failsafeLogger.getLogger().error("should get error"); - }); + doThrow(new RuntimeException()).when(failLogger).error(anyString()); + failsafeLogger.getLogger().error("should get error"); } } \ No newline at end of file diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/status/StatusTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/status/StatusTest.java index 41cad69367b..02b07f7de95 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/status/StatusTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/status/StatusTest.java @@ -17,13 +17,13 @@ package org.apache.dubbo.common.status; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.apache.dubbo.common.status.Status.Level.OK; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.isEmptyOrNullString; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class StatusTest { @Test diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/status/support/LoadStatusCheckerTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/status/support/LoadStatusCheckerTest.java index baafdca9c0b..7d30291dc66 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/status/support/LoadStatusCheckerTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/status/support/LoadStatusCheckerTest.java @@ -20,10 +20,10 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.status.Status; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.hamcrest.Matchers.notNullValue; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class LoadStatusCheckerTest { private static Logger logger = LoggerFactory.getLogger(LoadStatusCheckerTest.class); diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/status/support/MemoryStatusCheckerTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/status/support/MemoryStatusCheckerTest.java index 94e3b1de681..249837cc592 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/status/support/MemoryStatusCheckerTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/status/support/MemoryStatusCheckerTest.java @@ -20,13 +20,13 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.status.Status; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.apache.dubbo.common.status.Status.Level.OK; import static org.apache.dubbo.common.status.Status.Level.WARN; import static org.hamcrest.CoreMatchers.anyOf; import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class MemoryStatusCheckerTest { private static final Logger logger = LoggerFactory.getLogger(MemoryStatusCheckerTest.class); diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/status/support/StatusUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/status/support/StatusUtilsTest.java index 90b9400c6b0..43347238abf 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/status/support/StatusUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/status/support/StatusUtilsTest.java @@ -18,7 +18,7 @@ package org.apache.dubbo.common.status.support; import org.apache.dubbo.common.status.Status; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.HashMap; import java.util.Map; @@ -27,7 +27,7 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.isEmptyOrNullString; import static org.hamcrest.Matchers.not; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class StatusUtilsTest { @Test diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/store/support/SimpleDataStoreTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/store/support/SimpleDataStoreTest.java index fc99859d477..ab2b308d677 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/store/support/SimpleDataStoreTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/store/support/SimpleDataStoreTest.java @@ -16,14 +16,14 @@ */ package org.apache.dubbo.common.store.support; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.Map; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; public class SimpleDataStoreTest { private SimpleDataStore dataStore = new SimpleDataStore(); diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/threadlocal/InternalThreadLocalTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/threadlocal/InternalThreadLocalTest.java index 60067a3b737..caeda65ea8c 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/threadlocal/InternalThreadLocalTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/threadlocal/InternalThreadLocalTest.java @@ -17,8 +17,8 @@ package org.apache.dubbo.common.threadlocal; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.util.Objects; import java.util.concurrent.CountDownLatch; @@ -60,26 +60,26 @@ protected Integer initialValue() throws Exception { public void testRemoveAll() throws InterruptedException { final InternalThreadLocal internalThreadLocal = new InternalThreadLocal(); internalThreadLocal.set(1); - Assertions.assertTrue(internalThreadLocal.get() == 1, "set failed"); + Assert.assertTrue("set failed", internalThreadLocal.get() == 1); final InternalThreadLocal internalThreadLocalString = new InternalThreadLocal(); internalThreadLocalString.set("value"); - Assertions.assertTrue("value".equals(internalThreadLocalString.get()), "set failed"); + Assert.assertTrue("set failed", "value".equals(internalThreadLocalString.get())); InternalThreadLocal.removeAll(); - Assertions.assertTrue(internalThreadLocal.get() == null, "removeAll failed!"); - Assertions.assertTrue(internalThreadLocalString.get() == null, "removeAll failed!"); + Assert.assertTrue("removeAll failed!", internalThreadLocal.get() == null); + Assert.assertTrue("removeAll failed!", internalThreadLocalString.get() == null); } @Test public void testSize() throws InterruptedException { final InternalThreadLocal internalThreadLocal = new InternalThreadLocal(); internalThreadLocal.set(1); - Assertions.assertTrue(InternalThreadLocal.size() == 1, "size method is wrong!"); + Assert.assertTrue("size method is wrong!", InternalThreadLocal.size() == 1); final InternalThreadLocal internalThreadLocalString = new InternalThreadLocal(); internalThreadLocalString.set("value"); - Assertions.assertTrue(InternalThreadLocal.size() == 2, "size method is wrong!"); + Assert.assertTrue("size method is wrong!", InternalThreadLocal.size() == 2); } @Test @@ -87,18 +87,18 @@ public void testSetAndGet() { final Integer testVal = 10; final InternalThreadLocal internalThreadLocal = new InternalThreadLocal(); internalThreadLocal.set(testVal); - Assertions.assertTrue( - Objects.equals(testVal, internalThreadLocal.get()), "set is not equals get"); + Assert.assertTrue("set is not equals get", + Objects.equals(testVal, internalThreadLocal.get())); } @Test public void testRemove() { final InternalThreadLocal internalThreadLocal = new InternalThreadLocal(); internalThreadLocal.set(1); - Assertions.assertTrue(internalThreadLocal.get() == 1, "get method false!"); + Assert.assertTrue("get method false!", internalThreadLocal.get() == 1); internalThreadLocal.remove(); - Assertions.assertTrue(internalThreadLocal.get() == null, "remove failed!"); + Assert.assertTrue("remove failed!", internalThreadLocal.get() == null); } @Test @@ -112,10 +112,10 @@ protected void onRemoval(Integer value) throws Exception { } }; internalThreadLocal.set(1); - Assertions.assertTrue(internalThreadLocal.get() == 1, "get method false!"); + Assert.assertTrue("get method false!", internalThreadLocal.get() == 1); internalThreadLocal.remove(); - Assertions.assertTrue(valueToRemove[0] == 2, "onRemove method failed!"); + Assert.assertTrue("onRemove method failed!", valueToRemove[0] == 2); } @Test @@ -129,8 +129,8 @@ public void testMultiThreadSetAndGet() throws InterruptedException { public void run() { internalThreadLocal.set(testVal1); - Assertions.assertTrue( - Objects.equals(testVal1, internalThreadLocal.get()), "set is not equals get"); + Assert.assertTrue("set is not equals get", + Objects.equals(testVal1, internalThreadLocal.get())); countDownLatch.countDown(); } }); @@ -140,8 +140,8 @@ public void run() { @Override public void run() { internalThreadLocal.set(testVal2); - Assertions.assertTrue( - Objects.equals(testVal2, internalThreadLocal.get()), "set is not equals get"); + Assert.assertTrue("set is not equals get", + Objects.equals(testVal2, internalThreadLocal.get())); countDownLatch.countDown(); } }); diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/threadlocal/NamedInternalThreadFactoryTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/threadlocal/NamedInternalThreadFactoryTest.java index 7b9619f12b2..f99b985c306 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/threadlocal/NamedInternalThreadFactoryTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/threadlocal/NamedInternalThreadFactoryTest.java @@ -17,8 +17,8 @@ package org.apache.dubbo.common.threadlocal; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; public class NamedInternalThreadFactoryTest { @@ -31,6 +31,6 @@ public void run() { } }); - Assertions.assertTrue(t.getClass().equals(InternalThread.class), "thread is not InternalThread"); + Assert.assertTrue("thread is not InternalThread", t.getClass().equals(InternalThread.class)); } } \ No newline at end of file diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReportTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReportTest.java index ed737efa11f..54800156553 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReportTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReportTest.java @@ -18,7 +18,7 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.threadpool.support.AbortPolicyWithReport; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.concurrent.Executors; import java.util.concurrent.RejectedExecutionException; diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/cached/CachedThreadPoolTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/cached/CachedThreadPoolTest.java index 763cd0fdfe9..1bc883e5045 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/cached/CachedThreadPoolTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/cached/CachedThreadPoolTest.java @@ -23,7 +23,7 @@ import org.apache.dubbo.common.threadpool.ThreadPool; import org.apache.dubbo.common.threadpool.support.AbortPolicyWithReport; import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; @@ -36,7 +36,7 @@ import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.startsWith; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class CachedThreadPoolTest { @Test diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/eager/EagerThreadPoolExecutorTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/eager/EagerThreadPoolExecutorTest.java index ddbe8a0580c..87ef727f81e 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/eager/EagerThreadPoolExecutorTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/eager/EagerThreadPoolExecutorTest.java @@ -22,8 +22,8 @@ import org.apache.dubbo.common.threadpool.support.AbortPolicyWithReport; import org.apache.dubbo.common.utils.NamedThreadFactory; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; @@ -90,7 +90,7 @@ public void run() { } Thread.sleep(5000); // cores theads are all alive. - Assertions.assertTrue(executor.getPoolSize() == cores, "more than cores threads alive!"); + Assert.assertTrue("more than cores threads alive!", executor.getPoolSize() == cores); } @Test @@ -98,9 +98,9 @@ public void testSPI() { ExecutorService executorService = (ExecutorService) ExtensionLoader.getExtensionLoader(ThreadPool.class) .getExtension("eager") .getExecutor(URL); - Assertions.assertTrue( + Assert.assertTrue("test spi fail!", executorService.getClass() .getSimpleName() - .equals("EagerThreadPoolExecutor"), "test spi fail!"); + .equals("EagerThreadPoolExecutor")); } } \ No newline at end of file diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/eager/EagerThreadPoolTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/eager/EagerThreadPoolTest.java index 2884c73e8cd..2861841fcc4 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/eager/EagerThreadPoolTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/eager/EagerThreadPoolTest.java @@ -23,7 +23,7 @@ import org.apache.dubbo.common.threadpool.ThreadPool; import org.apache.dubbo.common.threadpool.support.AbortPolicyWithReport; import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; @@ -34,7 +34,7 @@ import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.startsWith; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class EagerThreadPoolTest { @Test diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/eager/TaskQueueTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/eager/TaskQueueTest.java index dc4a8bfa775..d20fb44c8cb 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/eager/TaskQueueTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/eager/TaskQueueTest.java @@ -17,25 +17,22 @@ package org.apache.dubbo.common.threadpool.support.eager; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; import org.mockito.Mockito; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.TimeUnit; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; public class TaskQueueTest { - @Test + @Test(expected = RejectedExecutionException.class) public void testOffer1() throws Exception { - Assertions.assertThrows(RejectedExecutionException.class, () -> { - TaskQueue queue = new TaskQueue(1); - queue.offer(mock(Runnable.class)); - }); + TaskQueue queue = new TaskQueue(1); + queue.offer(mock(Runnable.class)); } @Test @@ -70,15 +67,13 @@ public void testOffer4() throws Exception { assertThat(queue.offer(mock(Runnable.class)), is(true)); } - @Test + @Test(expected = RejectedExecutionException.class) public void testRetryOffer1() throws Exception { - Assertions.assertThrows(RejectedExecutionException.class, () -> { - TaskQueue queue = new TaskQueue(1); - EagerThreadPoolExecutor executor = mock(EagerThreadPoolExecutor.class); - Mockito.when(executor.isShutdown()).thenReturn(true); - queue.setExecutor(executor); - queue.retryOffer(mock(Runnable.class), 1000, TimeUnit.MILLISECONDS); - }); + TaskQueue queue = new TaskQueue(1); + EagerThreadPoolExecutor executor = mock(EagerThreadPoolExecutor.class); + Mockito.when(executor.isShutdown()).thenReturn(true); + queue.setExecutor(executor); + queue.retryOffer(mock(Runnable.class), 1000, TimeUnit.MILLISECONDS); } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/fixed/FixedThreadPoolTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/fixed/FixedThreadPoolTest.java index f1324b18d89..f416300a1cf 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/fixed/FixedThreadPoolTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/fixed/FixedThreadPoolTest.java @@ -24,7 +24,7 @@ import org.apache.dubbo.common.threadpool.support.AbortPolicyWithReport; import org.apache.dubbo.common.threadpool.support.limited.LimitedThreadPool; import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; @@ -37,7 +37,7 @@ import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.startsWith; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class FixedThreadPoolTest { @Test diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/limited/LimitedThreadPoolTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/limited/LimitedThreadPoolTest.java index bb1465773b7..9f27f33cfe8 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/limited/LimitedThreadPoolTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/limited/LimitedThreadPoolTest.java @@ -23,7 +23,7 @@ import org.apache.dubbo.common.threadpool.ThreadPool; import org.apache.dubbo.common.threadpool.support.AbortPolicyWithReport; import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; @@ -36,7 +36,7 @@ import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.startsWith; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class LimitedThreadPoolTest { @Test diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/timer/HashedWheelTimerTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/timer/HashedWheelTimerTest.java index 022351462b7..15bd5c52a75 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/timer/HashedWheelTimerTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/timer/HashedWheelTimerTest.java @@ -18,7 +18,7 @@ package org.apache.dubbo.common.timer; import org.apache.dubbo.common.utils.NamedThreadFactory; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ArrayUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ArrayUtilsTest.java index 9d19f18b1d6..76fb7884bd1 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ArrayUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ArrayUtilsTest.java @@ -17,10 +17,10 @@ package org.apache.dubbo.common.utils; -import org.junit.jupiter.api.Test; +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertTrue; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.Test; public class ArrayUtilsTest { diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/AssertTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/AssertTest.java index bea94885222..b4a2cc1b980 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/AssertTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/AssertTest.java @@ -17,45 +17,43 @@ package org.apache.dubbo.common.utils; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; -import static org.apache.dubbo.common.utils.Assert.notEmptyString; import static org.apache.dubbo.common.utils.Assert.notNull; +import static org.apache.dubbo.common.utils.Assert.notEmptyString; public class AssertTest { - @Test - public void testNotNull1() { - Assertions.assertThrows(IllegalArgumentException.class, () -> notNull(null, "null object")); + @Test(expected = IllegalArgumentException.class) + public void testNotNull1() throws Exception { + notNull(null, "null object"); } - @Test - public void testNotNull2() { - Assertions.assertThrows(IllegalStateException.class, () -> notNull(null, new IllegalStateException("null object"))); + @Test(expected = IllegalStateException.class) + public void testNotNull2() throws Exception { + notNull(null, new IllegalStateException("null object")); } @Test public void testNotNullWhenInputNotNull1() { - notNull(new Object(), "null object"); + notNull(new Object(),"null object"); } @Test public void testNotNullWhenInputNotNull2() { - notNull(new Object(), new IllegalStateException("null object")); + notNull(new Object(),new IllegalStateException("null object")); } - - @Test + @Test(expected = IllegalArgumentException.class) public void testNotNullString() { - Assertions.assertThrows(IllegalArgumentException.class, () -> notEmptyString(null, "Message can't be null")); + notEmptyString(null,"Message can't be null"); } - @Test + @Test(expected = IllegalArgumentException.class) public void testNotEmptyString() { - Assertions.assertThrows(IllegalArgumentException.class, () -> notEmptyString("", "Message can't be null or empty")); + notEmptyString("","Message can't be null or empty"); } @Test public void testNotNullNotEmptyString() { - notEmptyString("abcd", "Message can'be null or empty"); + notEmptyString("abcd","Message can'be null or empty"); } } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/AtomicPositiveIntegerTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/AtomicPositiveIntegerTest.java index f57b943a658..c7086006cef 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/AtomicPositiveIntegerTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/AtomicPositiveIntegerTest.java @@ -16,16 +16,15 @@ */ package org.apache.dubbo.common.utils; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; public class AtomicPositiveIntegerTest { private AtomicPositiveInteger i1 = new AtomicPositiveInteger(); @@ -160,28 +159,24 @@ public void testAddAndGet() throws Exception { assertEquals(2, i3.get()); } - @Test - public void testCompareAndSet1() { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - i1.compareAndSet(i1.get(), -1); - }); + @Test(expected = IllegalArgumentException.class) + public void testCompareAndSet1() throws Exception { + i1.compareAndSet(i1.get(), -1); } @Test - public void testCompareAndSet2() { + public void testCompareAndSet2() throws Exception { assertThat(i1.compareAndSet(i1.get(), 2), is(true)); assertThat(i1.get(), is(2)); } - @Test - public void testWeakCompareAndSet1() { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - i1.weakCompareAndSet(i1.get(), -1); - }); + @Test(expected = IllegalArgumentException.class) + public void testWeakCompareAndSet1() throws Exception { + i1.weakCompareAndSet(i1.get(), -1); } @Test - public void testWeakCompareAndSet2() { + public void testWeakCompareAndSet2() throws Exception { assertThat(i1.weakCompareAndSet(i1.get(), 2), is(true)); assertThat(i1.get(), is(2)); } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CIDRUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CIDRUtilsTest.java deleted file mode 100644 index d0c627d174b..00000000000 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CIDRUtilsTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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 org.apache.dubbo.common.utils; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.net.UnknownHostException; - -/** - * @author cvictory ON 2019-02-28 - */ -public class CIDRUtilsTest { - - @Test - public void testIpv4() throws UnknownHostException { - CIDRUtils cidrUtils = new CIDRUtils("192.168.1.0/26"); - Assertions.assertTrue(cidrUtils.isInRange("192.168.1.63")); - Assertions.assertFalse(cidrUtils.isInRange("192.168.1.65")); - - cidrUtils = new CIDRUtils("192.168.1.192/26"); - Assertions.assertTrue(cidrUtils.isInRange("192.168.1.199")); - Assertions.assertFalse(cidrUtils.isInRange("192.168.1.190")); - } - - @Test - public void testIpv6() throws UnknownHostException { - CIDRUtils cidrUtils = new CIDRUtils("234e:0:4567::3d/64"); - Assertions.assertTrue(cidrUtils.isInRange("234e:0:4567::3e")); - Assertions.assertTrue(cidrUtils.isInRange("234e:0:4567::ffff:3e")); - Assertions.assertFalse(cidrUtils.isInRange("234e:1:4567::3d")); - Assertions.assertFalse(cidrUtils.isInRange("234e:0:4567:1::3d")); - - cidrUtils = new CIDRUtils("3FFE:FFFF:0:CC00::/54"); - Assertions.assertTrue(cidrUtils.isInRange("3FFE:FFFF:0:CC00::dd")); - Assertions.assertTrue(cidrUtils.isInRange("3FFE:FFFF:0:CC00:0000:eeee:0909:dd")); - Assertions.assertTrue(cidrUtils.isInRange("3FFE:FFFF:0:CC0F:0000:eeee:0909:dd")); - - Assertions.assertFalse(cidrUtils.isInRange("3EFE:FFFE:0:C107::dd")); - Assertions.assertFalse(cidrUtils.isInRange("1FFE:FFFE:0:CC00::dd")); - } -} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ClassHelperTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ClassHelperTest.java index 470e58709a1..1ccd15339b7 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ClassHelperTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ClassHelperTest.java @@ -17,7 +17,7 @@ package org.apache.dubbo.common.utils; -import org.junit.jupiter.api.Test; +import org.junit.Test; import org.mockito.Mockito; import static org.apache.dubbo.common.utils.ClassHelper.forName; @@ -25,12 +25,11 @@ import static org.apache.dubbo.common.utils.ClassHelper.getClassLoader; import static org.apache.dubbo.common.utils.ClassHelper.resolvePrimitiveClassName; import static org.apache.dubbo.common.utils.ClassHelper.toShortString; -import static org.apache.dubbo.common.utils.ClassHelper.convertPrimitive; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.sameInstance; import static org.hamcrest.Matchers.startsWith; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.verify; public class ClassHelperTest { @@ -119,43 +118,4 @@ public void testToShortString() throws Exception { assertThat(toShortString(null), equalTo("null")); assertThat(toShortString(new ClassHelperTest()), startsWith("ClassHelperTest@")); } - - @Test - public void testConvertPrimitive() throws Exception { - - assertThat(convertPrimitive(char.class, ""), equalTo('\0')); - assertThat(convertPrimitive(char.class, null), equalTo(null)); - assertThat(convertPrimitive(char.class, "6"), equalTo('6')); - - assertThat(convertPrimitive(boolean.class, ""), equalTo(Boolean.FALSE)); - assertThat(convertPrimitive(boolean.class, null), equalTo(null)); - assertThat(convertPrimitive(boolean.class, "true"), equalTo(Boolean.TRUE)); - - - assertThat(convertPrimitive(byte.class, ""), equalTo(null)); - assertThat(convertPrimitive(byte.class, null), equalTo(null)); - assertThat(convertPrimitive(byte.class, "127"), equalTo(Byte.MAX_VALUE)); - - - assertThat(convertPrimitive(short.class, ""), equalTo(null)); - assertThat(convertPrimitive(short.class, null), equalTo(null)); - assertThat(convertPrimitive(short.class, "32767"), equalTo(Short.MAX_VALUE)); - - assertThat(convertPrimitive(int.class, ""), equalTo(null)); - assertThat(convertPrimitive(int.class, null), equalTo(null)); - assertThat(convertPrimitive(int.class, "6"), equalTo(6)); - - assertThat(convertPrimitive(long.class, ""), equalTo(null)); - assertThat(convertPrimitive(long.class, null), equalTo(null)); - assertThat(convertPrimitive(long.class, "6"), equalTo(new Long(6))); - - assertThat(convertPrimitive(float.class, ""), equalTo(null)); - assertThat(convertPrimitive(float.class, null), equalTo(null)); - assertThat(convertPrimitive(float.class, "1.1"), equalTo(new Float(1.1))); - - assertThat(convertPrimitive(double.class, ""), equalTo(null)); - assertThat(convertPrimitive(double.class, null), equalTo(null)); - assertThat(convertPrimitive(double.class, "10.1"), equalTo(new Double(10.1))); - } - } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CollectionUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CollectionUtilsTest.java index 22d8d923bb1..58a3212a8d5 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CollectionUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CollectionUtilsTest.java @@ -16,8 +16,7 @@ */ package org.apache.dubbo.common.utils; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; @@ -35,11 +34,11 @@ import static java.util.Collections.singleton; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; public class CollectionUtilsTest { @Test @@ -162,9 +161,9 @@ public void testStringMap1() throws Exception { assertThat(toStringMap("key", "value"), equalTo(Collections.singletonMap("key", "value"))); } - @Test + @Test(expected = IllegalArgumentException.class) public void testStringMap2() throws Exception { - Assertions.assertThrows(IllegalArgumentException.class, () -> toStringMap("key", "value", "odd")); + toStringMap("key", "value", "odd"); } @Test @@ -179,9 +178,9 @@ public void testToMap1() throws Exception { assertEquals(expected, CollectionUtils.toMap("a", 1, "b", 2, "c", 3)); } - @Test + @Test(expected = IllegalArgumentException.class) public void testToMap2() throws Exception { - Assertions.assertThrows(IllegalArgumentException.class, () -> toMap("a", "b", "c")); + toMap("a", "b", "c"); } @Test diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CompatibleTypeUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CompatibleTypeUtilsTest.java index ff1ebd58cd7..ff9a144830c 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CompatibleTypeUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CompatibleTypeUtilsTest.java @@ -16,7 +16,7 @@ */ package org.apache.dubbo.common.utils; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.math.BigDecimal; import java.math.BigInteger; @@ -28,10 +28,10 @@ import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; public class CompatibleTypeUtilsTest { diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ConfigUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ConfigUtilsTest.java index 7e9a4904778..97398a0ea48 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ConfigUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ConfigUtilsTest.java @@ -18,11 +18,11 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.threadpool.ThreadPool; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; import java.util.Collections; import java.util.List; @@ -33,16 +33,16 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.is; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; public class ConfigUtilsTest { - @BeforeEach + @Before public void setUp() throws Exception { ConfigUtils.setProperties(null); } - @AfterEach + @After public void tearDown() throws Exception { ConfigUtils.setProperties(null); } @@ -228,12 +228,12 @@ public void testLoadPropertiesOneFileNotRootPath() throws Exception { } - @Disabled("see http://code.alibabatech.com/jira/browse/DUBBO-133") + @Ignore("see http://code.alibabatech.com/jira/browse/DUBBO-133") @Test public void testLoadPropertiesMultiFileNotRootPathException() throws Exception { try { ConfigUtils.loadProperties("META-INF/services/org.apache.dubbo.common.status.StatusChecker", false); - Assertions.fail(); + Assert.fail(); } catch (IllegalStateException expected) { assertThat(expected.getMessage(), containsString("only 1 META-INF/services/org.apache.dubbo.common.status.StatusChecker file is expected, but 2 dubbo.properties files found on class path:")); } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/DubboAppenderTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/DubboAppenderTest.java index 018e3b7c676..ee01c42383f 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/DubboAppenderTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/DubboAppenderTest.java @@ -20,20 +20,20 @@ import org.apache.log4j.Category; import org.apache.log4j.Level; import org.apache.log4j.spi.LoggingEvent; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import org.mockito.Mockito; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class DubboAppenderTest { private LoggingEvent event; - @BeforeEach + @Before public void setUp() throws Exception { Level level = Mockito.mock(Level.class); Category category = Mockito.mock(Category.class); @@ -44,7 +44,7 @@ public void setUp() throws Exception { Mockito.when(event.getMessage()).thenReturn("message"); } - @AfterEach + @After public void tearDown() throws Exception { DubboAppender.clear(); DubboAppender.doStop(); diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ExecutorUtilTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ExecutorUtilTest.java index 2ef0f4a2a40..9ffa1c35ab7 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ExecutorUtilTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ExecutorUtilTest.java @@ -19,7 +19,7 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.junit.jupiter.api.Test; +import org.junit.Test; import org.mockito.Mockito; import java.util.concurrent.Executor; @@ -28,7 +28,7 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.atLeast; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/HolderTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/HolderTest.java index 0534712d98c..1332654de8c 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/HolderTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/HolderTest.java @@ -17,10 +17,10 @@ package org.apache.dubbo.common.utils; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class HolderTest { @Test diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/IOUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/IOUtilsTest.java index 3b5743d2b40..e964b3443d4 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/IOUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/IOUtilsTest.java @@ -17,11 +17,11 @@ package org.apache.dubbo.common.utils; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -32,12 +32,13 @@ import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; -import java.nio.file.Path; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; public class IOUtilsTest { + @Rule + public TemporaryFolder tmpDir = new TemporaryFolder(); private static String TEXT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"; private InputStream is; @@ -45,7 +46,7 @@ public class IOUtilsTest { private Reader reader; private Writer writer; - @BeforeEach + @Before public void setUp() throws Exception { is = new ByteArrayInputStream(TEXT.getBytes("UTF-8")); os = new ByteArrayOutputStream(); @@ -53,7 +54,7 @@ public void setUp() throws Exception { writer = new StringWriter(); } - @AfterEach + @After public void tearDown() throws Exception { is.close(); os.close(); @@ -87,13 +88,12 @@ public void testWrite5() throws Exception { } @Test - public void testLines(@TempDir Path tmpDir) throws Exception { - File file = tmpDir.getFileName().toAbsolutePath().toFile(); + public void testLines() throws Exception { + File file = tmpDir.newFile(); IOUtils.writeLines(file, new String[]{TEXT}); String[] lines = IOUtils.readLines(file); assertThat(lines.length, equalTo(1)); assertThat(lines[0], equalTo(TEXT)); - tmpDir.getFileName().toAbsolutePath().toFile().delete(); } @Test @@ -116,14 +116,13 @@ public void testRead() throws Exception { } @Test - public void testAppendLines(@TempDir Path tmpDir) throws Exception { - File file = tmpDir.getFileName().toAbsolutePath().toFile(); + public void testAppendLines() throws Exception { + File file = tmpDir.newFile(); IOUtils.appendLines(file, new String[]{"a", "b", "c"}); String[] lines = IOUtils.readLines(file); assertThat(lines.length, equalTo(3)); assertThat(lines[0], equalTo("a")); assertThat(lines[1], equalTo("b")); assertThat(lines[2], equalTo("c")); - tmpDir.getFileName().toAbsolutePath().toFile().delete(); } } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/LRUCacheTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/LRUCacheTest.java index a52685d08dc..d088e0c1368 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/LRUCacheTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/LRUCacheTest.java @@ -17,12 +17,12 @@ package org.apache.dubbo.common.utils; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.hamcrest.Matchers.equalTo; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; public class LRUCacheTest { @Test diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/LogHelperTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/LogHelperTest.java index 78de5f812f5..6a513c8d4d3 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/LogHelperTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/LogHelperTest.java @@ -18,7 +18,7 @@ package org.apache.dubbo.common.utils; import org.apache.dubbo.common.logger.Logger; -import org.junit.jupiter.api.Test; +import org.junit.Test; import org.mockito.Mockito; import static org.mockito.Mockito.verify; diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/LogTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/LogTest.java index 203cadb547f..3c77ca77fc7 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/LogTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/LogTest.java @@ -18,11 +18,11 @@ package org.apache.dubbo.common.utils; import org.apache.log4j.Level; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class LogTest { @Test diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/LogUtilTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/LogUtilTest.java index aec7cc7f95b..c1c36a7172a 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/LogUtilTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/LogUtilTest.java @@ -18,17 +18,17 @@ package org.apache.dubbo.common.utils; import org.apache.log4j.Level; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Test; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class LogUtilTest { - @AfterEach + @After public void tearDown() throws Exception { DubboAppender.logList.clear(); } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NamedThreadFactoryTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NamedThreadFactoryTest.java index ece611194bd..f0253543d34 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NamedThreadFactoryTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NamedThreadFactoryTest.java @@ -17,15 +17,15 @@ package org.apache.dubbo.common.utils; -import org.junit.jupiter.api.Test; +import org.junit.Test; import org.mockito.Mockito; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.containsString; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; public class NamedThreadFactoryTest { @Test diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java index 2fe0a636815..caa216f9301 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java @@ -17,24 +17,22 @@ package org.apache.dubbo.common.utils; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; +import org.junit.Ignore; +import org.junit.Test; import java.net.Inet6Address; import java.net.InetAddress; import java.net.InetSocketAddress; -import java.net.UnknownHostException; -import static org.hamcrest.MatcherAssert.assertThat; +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertTrue; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.greaterThanOrEqualTo; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -106,21 +104,22 @@ public void testGetLocalSocketAddress() throws Exception { @Test public void testIsValidAddress() throws Exception { + assertFalse(NetUtils.isValidAddress((InetAddress) null)); InetAddress address = mock(InetAddress.class); when(address.isLoopbackAddress()).thenReturn(true); - assertFalse(NetUtils.isValidV4Address(address)); + assertFalse(NetUtils.isValidAddress(address)); address = mock(InetAddress.class); when(address.getHostAddress()).thenReturn("localhost"); - assertFalse(NetUtils.isValidV4Address(address)); + assertFalse(NetUtils.isValidAddress(address)); address = mock(InetAddress.class); when(address.getHostAddress()).thenReturn("0.0.0.0"); - assertFalse(NetUtils.isValidV4Address(address)); + assertFalse(NetUtils.isValidAddress(address)); address = mock(InetAddress.class); when(address.getHostAddress()).thenReturn("127.0.0.1"); - assertFalse(NetUtils.isValidV4Address(address)); + assertFalse(NetUtils.isValidAddress(address)); address = mock(InetAddress.class); when(address.getHostAddress()).thenReturn("1.2.3.4"); - assertTrue(NetUtils.isValidV4Address(address)); + assertTrue(NetUtils.isValidAddress(address)); } @Test @@ -132,6 +131,7 @@ public void testGetLocalHost() throws Exception { public void testGetLocalAddress() throws Exception { InetAddress address = NetUtils.getLocalAddress(); assertNotNull(address); + assertTrue(NetUtils.isValidLocalHost(address.getHostAddress())); } @Test @@ -200,7 +200,7 @@ public void testIsValidV6Address() { * But enable it will cause other UT to fail. * Therefore currently disabling this UT. */ - @Disabled + @Ignore @Test public void testNormalizeV6Address() { Inet6Address address = mock(Inet6Address.class); @@ -209,93 +209,4 @@ public void testNormalizeV6Address() { InetAddress normalized = NetUtils.normalizeV6Address(address); assertThat(normalized.getHostAddress(), equalTo("fe80:0:0:0:894:aeec:f37d:23e1%5")); } - - @Test - public void testMatchIpRangeMatchWhenIpv4() throws UnknownHostException { - assertTrue(NetUtils.matchIpRange("*.*.*.*", "192.168.1.63", 90)); - assertTrue(NetUtils.matchIpRange("192.168.1.*", "192.168.1.63", 90)); - assertTrue(NetUtils.matchIpRange("192.168.1.63", "192.168.1.63", 90)); - assertTrue(NetUtils.matchIpRange("192.168.1.1-65", "192.168.1.63", 90)); - assertFalse(NetUtils.matchIpRange("192.168.1.1-61", "192.168.1.63", 90)); - assertFalse(NetUtils.matchIpRange("192.168.1.62", "192.168.1.63", 90)); - } - - @Test - public void testMatchIpRangeMatchWhenIpv6() throws UnknownHostException { - assertTrue(NetUtils.matchIpRange("*.*.*.*", "192.168.1.63", 90)); - assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:*", "234e:0:4567::3d:ff", 90)); - assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:ee", "234e:0:4567::3d:ee", 90)); - assertTrue(NetUtils.matchIpRange("234e:0:4567::3d:ee", "234e:0:4567::3d:ee", 90)); - assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ff", "234e:0:4567::3d:ee", 90)); - assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ee", "234e:0:4567::3d:ee", 90)); - - assertFalse(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:ff", "234e:0:4567::3d:ee", 90)); - assertFalse(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ea", "234e:0:4567::3d:ee", 90)); - } - - @Test - public void testMatchIpRangeMatchWhenIpv6Exception() throws UnknownHostException { - IllegalArgumentException thrown = - assertThrows(IllegalArgumentException.class, () -> - NetUtils.matchIpRange("234e:0:4567::3d:*", "234e:0:4567::3d:ff", 90)); - assertTrue(thrown.getMessage().contains("If you config ip expression that contains '*'")); - - thrown = assertThrows(IllegalArgumentException.class, () -> - NetUtils.matchIpRange("234e:0:4567:3d", "234e:0:4567::3d:ff", 90)); - assertTrue(thrown.getMessage().contains("The host is ipv6, but the pattern is not ipv6 pattern")); - - thrown = - assertThrows(IllegalArgumentException.class, () -> - NetUtils.matchIpRange("192.168.1.1-65-3", "192.168.1.63", 90)); - assertTrue(thrown.getMessage().contains("There is wrong format of ip Address")); - } - - @Test - public void testMatchIpRangeMatchWhenIpWrongException() throws UnknownHostException { - UnknownHostException thrown = - assertThrows(UnknownHostException.class, () -> - NetUtils.matchIpRange("192.168.1.63", "192.168.1.ff", 90)); - assertTrue(thrown.getMessage().contains("192.168.1.ff")); - } - - @Test - public void testMatchIpMatch() throws UnknownHostException { - assertTrue(NetUtils.matchIpExpression("192.168.1.*", "192.168.1.63", 90)); - assertTrue(NetUtils.matchIpExpression("192.168.1.192/26", "192.168.1.199", 90)); - } - - @Test - public void testMatchIpv6WithIpPort() throws UnknownHostException { - assertTrue(NetUtils.matchIpRange("[234e:0:4567::3d:ee]", "234e:0:4567::3d:ee", 8090)); - assertTrue(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:ee]", "234e:0:4567::3d:ee", 8090)); - assertTrue(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:ee]:8090", "234e:0:4567::3d:ee", 8090)); - assertTrue(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:0-ee]:8090", "234e:0:4567::3d:ee", 8090)); - assertTrue(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:ee-ff]:8090", "234e:0:4567::3d:ee", 8090)); - assertTrue(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:*]:90", "234e:0:4567::3d:ff", 90)); - - assertFalse(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:ee]:7289", "234e:0:4567::3d:ee", 8090)); - assertFalse(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:ee-ff]:8090", "234e:0:4567::3d:ee", 9090)); - } - - @Test - public void testMatchIpv4WithIpPort() throws UnknownHostException { - NumberFormatException thrown = - assertThrows(NumberFormatException.class, () ->NetUtils.matchIpExpression("192.168.1.192/26:90", "192.168.1.199", 90)); - assertTrue(thrown instanceof NumberFormatException); - - assertTrue(NetUtils.matchIpRange("*.*.*.*:90", "192.168.1.63", 90)); - assertTrue(NetUtils.matchIpRange("192.168.1.*:90", "192.168.1.63", 90)); - assertTrue(NetUtils.matchIpRange("192.168.1.63:90", "192.168.1.63", 90)); - assertTrue(NetUtils.matchIpRange("192.168.1.63-65:90", "192.168.1.63", 90)); - assertTrue(NetUtils.matchIpRange("192.168.1.1-63:90", "192.168.1.63", 90)); - - assertFalse(NetUtils.matchIpRange("*.*.*.*:80", "192.168.1.63", 90)); - assertFalse(NetUtils.matchIpRange("192.168.1.*:80", "192.168.1.63", 90)); - assertFalse(NetUtils.matchIpRange("192.168.1.63:80", "192.168.1.63", 90)); - assertFalse(NetUtils.matchIpRange("192.168.1.63-65:80", "192.168.1.63", 90)); - assertFalse(NetUtils.matchIpRange("192.168.1.1-63:80", "192.168.1.63", 90)); - - assertFalse(NetUtils.matchIpRange("192.168.1.1-61:90", "192.168.1.62", 90)); - assertFalse(NetUtils.matchIpRange("192.168.1.62:90", "192.168.1.63", 90)); - } -} +} \ No newline at end of file diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ParametersTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ParametersTest.java index 1b86110a75a..9d5fc055335 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ParametersTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ParametersTest.java @@ -16,12 +16,12 @@ */ package org.apache.dubbo.common.utils; +import junit.framework.TestCase; + import java.util.HashMap; import java.util.Map; -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class ParametersTest { +public class ParametersTest extends TestCase { final String ServiceName = "org.apache.dubbo.rpc.service.GenericService"; final String ServiceVersion = "1.0.15"; final String LoadBalance = "lcr"; diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java index ecc3f46ee38..fa93d7b341d 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java @@ -23,8 +23,8 @@ import org.apache.dubbo.common.model.person.PersonInfo; import org.apache.dubbo.common.model.person.PersonStatus; import org.apache.dubbo.common.model.person.Phone; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.lang.reflect.Method; import java.lang.reflect.Type; @@ -40,13 +40,13 @@ import java.util.UUID; import static org.hamcrest.Matchers.equalTo; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; public class PojoUtilsTest { @@ -533,11 +533,11 @@ public void testPublicField() throws Exception { child.setParent(parent); Object obj = PojoUtils.generalize(parent); Parent realizedParent = (Parent) PojoUtils.realize(obj, Parent.class); - Assertions.assertEquals(parent.gender, realizedParent.gender); - Assertions.assertEquals(child.gender, parent.getChild().gender); - Assertions.assertEquals(child.age, realizedParent.getChild().getAge()); - Assertions.assertEquals(parent.getEmail(), realizedParent.getEmail()); - Assertions.assertNull(realizedParent.email); + Assert.assertEquals(parent.gender, realizedParent.gender); + Assert.assertEquals(child.gender, parent.getChild().gender); + Assert.assertEquals(child.age, realizedParent.getChild().getAge()); + Assert.assertEquals(parent.getEmail(), realizedParent.getEmail()); + Assert.assertNull(realizedParent.email); } @Test @@ -553,24 +553,24 @@ public void testMapField() throws Exception { data.setList(Arrays.asList(newChild("forth", 4))); Object obj = PojoUtils.generalize(data); - Assertions.assertEquals(3, data.getChildren().size()); + Assert.assertEquals(3, data.getChildren().size()); assertTrue(data.getChildren().get("first").getClass() == Child.class); - Assertions.assertEquals(1, data.getList().size()); + Assert.assertEquals(1, data.getList().size()); assertTrue(data.getList().get(0).getClass() == Child.class); TestData realizadData = (TestData) PojoUtils.realize(obj, TestData.class); - Assertions.assertEquals(data.getChildren().size(), realizadData.getChildren().size()); - Assertions.assertEquals(data.getChildren().keySet(), realizadData.getChildren().keySet()); + Assert.assertEquals(data.getChildren().size(), realizadData.getChildren().size()); + Assert.assertEquals(data.getChildren().keySet(), realizadData.getChildren().keySet()); for (Map.Entry entry : data.getChildren().entrySet()) { Child c = realizadData.getChildren().get(entry.getKey()); - Assertions.assertNotNull(c); - Assertions.assertEquals(entry.getValue().getName(), c.getName()); - Assertions.assertEquals(entry.getValue().getAge(), c.getAge()); + Assert.assertNotNull(c); + Assert.assertEquals(entry.getValue().getName(), c.getName()); + Assert.assertEquals(entry.getValue().getAge(), c.getAge()); } - Assertions.assertEquals(1, realizadData.getList().size()); - Assertions.assertEquals(data.getList().get(0).getName(), realizadData.getList().get(0).getName()); - Assertions.assertEquals(data.getList().get(0).getAge(), realizadData.getList().get(0).getAge()); + Assert.assertEquals(1, realizadData.getList().size()); + Assert.assertEquals(data.getList().get(0).getName(), realizadData.getList().get(0).getName()); + Assert.assertEquals(data.getList().get(0).getAge(), realizadData.getList().get(0).getAge()); } @Test @@ -617,8 +617,8 @@ public void testPojoList() throws Exception { assertTrue(l.size() == 1); assertTrue(l.get(0) instanceof Parent); Parent realizeParent = (Parent) l.get(0); - Assertions.assertEquals(parent.getName(), realizeParent.getName()); - Assertions.assertEquals(parent.getAge(), realizeParent.getAge()); + Assert.assertEquals(parent.getName(), realizeParent.getName()); + Assert.assertEquals(parent.getAge(), realizeParent.getAge()); } @Test @@ -638,14 +638,14 @@ public void testListPojoListPojo() throws Exception { assertTrue(realizeObject instanceof ListResult); ListResult realizeList = (ListResult) realizeObject; List realizeInnerList = realizeList.getResult(); - Assertions.assertEquals(1, realizeInnerList.size()); + Assert.assertEquals(1, realizeInnerList.size()); assertTrue(realizeInnerList.get(0) instanceof InnerPojo); InnerPojo realizeParentList = (InnerPojo) realizeInnerList.get(0); - Assertions.assertEquals(1, realizeParentList.getList().size()); + Assert.assertEquals(1, realizeParentList.getList().size()); assertTrue(realizeParentList.getList().get(0) instanceof Parent); Parent realizeParent = (Parent) realizeParentList.getList().get(0); - Assertions.assertEquals(parent.getName(), realizeParent.getName()); - Assertions.assertEquals(parent.getAge(), realizeParent.getAge()); + Assert.assertEquals(parent.getName(), realizeParent.getName()); + Assert.assertEquals(parent.getAge(), realizeParent.getAge()); } @Test @@ -774,7 +774,7 @@ public List getList() { } public void setList(List list) { - if (CollectionUtils.isNotEmpty(list)) { + if (list != null && !list.isEmpty()) { this.list.addAll(list); } } @@ -784,7 +784,7 @@ public Map getChildren() { } public void setChildren(Map children) { - if (CollectionUtils.isNotEmptyMap(children)) { + if (children != null && !children.isEmpty()) { this.children.putAll(children); } } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ReflectUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ReflectUtilsTest.java index f46235daaa1..ddca7ac6a80 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ReflectUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ReflectUtilsTest.java @@ -16,8 +16,7 @@ */ package org.apache.dubbo.common.utils; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.lang.reflect.Constructor; import java.lang.reflect.Field; @@ -29,18 +28,18 @@ import java.util.Map; import java.util.Set; -import static org.hamcrest.MatcherAssert.assertThat; +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertNotNull; +import static junit.framework.TestCase.assertNull; +import static junit.framework.TestCase.assertTrue; +import static junit.framework.TestCase.fail; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.sameInstance; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; public class ReflectUtilsTest { @Test @@ -190,12 +189,9 @@ public void testFindMethodByMethodName1() throws Exception { assertNotNull(ReflectUtils.findMethodByMethodName(Foo.class, "hello")); } - @Test - public void testFindMethodByMethodName2() { - Assertions.assertThrows(IllegalStateException.class, () -> { - ReflectUtils.findMethodByMethodName(Foo2.class, "hello"); - }); - + @Test(expected = IllegalStateException.class) + public void testFindMethodByMethodName2() throws Exception { + ReflectUtils.findMethodByMethodName(Foo2.class, "hello"); } @Test @@ -394,11 +390,9 @@ public void testForName1() throws Exception { assertThat(ReflectUtils.forName(ReflectUtils.class.getName()), sameInstance(ReflectUtils.class)); } - @Test - public void testForName2() { - Assertions.assertThrows(IllegalStateException.class, () -> { - ReflectUtils.forName("a.c.d.e.F"); - }); + @Test(expected = IllegalStateException.class) + public void testForName2() throws Exception { + ReflectUtils.forName("a.c.d.e.F"); } public static class EmptyClass { diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/StackTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/StackTest.java index 4b113e8303b..921d217572e 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/StackTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/StackTest.java @@ -17,14 +17,13 @@ package org.apache.dubbo.common.utils; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.EmptyStackException; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class StackTest { @Test @@ -58,43 +57,33 @@ public void testClear() throws Exception { assertThat(stack.isEmpty(), is(true)); } - @Test + @Test(expected = EmptyStackException.class) public void testIllegalPop() throws Exception { - Assertions.assertThrows(EmptyStackException.class, () -> { - Stack stack = new Stack(); - stack.pop(); - }); + Stack stack = new Stack(); + stack.pop(); } - @Test + @Test(expected = EmptyStackException.class) public void testIllegalPeek() throws Exception { - Assertions.assertThrows(EmptyStackException.class, () -> { - Stack stack = new Stack(); - stack.peek(); - }); + Stack stack = new Stack(); + stack.peek(); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void testIllegalGet() throws Exception { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { - Stack stack = new Stack(); - stack.get(1); - }); + Stack stack = new Stack(); + stack.get(1); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void testIllegalSet() throws Exception { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { - Stack stack = new Stack(); - stack.set(1, "illegal"); - }); + Stack stack = new Stack(); + stack.set(1, "illegal"); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void testIllegalRemove() throws Exception { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { - Stack stack = new Stack(); - stack.remove(1); - }); + Stack stack = new Stack(); + stack.remove(1); } } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/StringUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/StringUtilsTest.java index 384c957bb23..111544155b9 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/StringUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/StringUtilsTest.java @@ -17,8 +17,8 @@ package org.apache.dubbo.common.utils; import org.apache.dubbo.common.Constants; - -import org.junit.jupiter.api.Test; +import org.hamcrest.Matchers; +import org.junit.Test; import java.util.ArrayList; import java.util.Collections; @@ -26,15 +26,15 @@ import java.util.List; import java.util.Map; -import static org.hamcrest.MatcherAssert.assertThat; +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertTrue; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.isEmptyOrNullString; import static org.hamcrest.Matchers.nullValue; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertThat; public class StringUtilsTest { @Test @@ -240,22 +240,9 @@ public void testIsContains() throws Exception { @Test public void testIsNumeric() throws Exception { - assertThat(StringUtils.isNumeric("123", false), is(true)); - assertThat(StringUtils.isNumeric("1a3", false), is(false)); - assertThat(StringUtils.isNumeric(null, false), is(false)); - - assertThat(StringUtils.isNumeric("0", true), is(true)); - assertThat(StringUtils.isNumeric("0.1", true), is(true)); - assertThat(StringUtils.isNumeric("DUBBO", true), is(false)); - assertThat(StringUtils.isNumeric("", true), is(false)); - assertThat(StringUtils.isNumeric(" ", true), is(false)); - assertThat(StringUtils.isNumeric(" ", true), is(false)); - - assertThat(StringUtils.isNumeric("123.3.3", true), is(false)); - assertThat(StringUtils.isNumeric("123.", true), is(true)); - assertThat(StringUtils.isNumeric(".123", true), is(true)); - assertThat(StringUtils.isNumeric("..123", true), is(false)); - + assertThat(StringUtils.isNumeric("123"), is(true)); + assertThat(StringUtils.isNumeric("1a3"), is(false)); + assertThat(StringUtils.isNumeric(null), is(false)); } @Test @@ -287,12 +274,4 @@ public void testToArgumentString() throws Exception { assertThat(s, containsString("0,")); assertThat(s, containsString("{\"enabled\":true}")); } - - @Test - public void testTrim() { - assertEquals("left blank", StringUtils.trim(" left blank")); - assertEquals("right blank", StringUtils.trim("right blank ")); - assertEquals("bi-side blank", StringUtils.trim(" bi-side blank ")); - - } } \ No newline at end of file diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/UrlUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/UrlUtilsTest.java index 2da27fea760..3fc14c3a594 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/UrlUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/UrlUtilsTest.java @@ -19,7 +19,7 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.ArrayList; import java.util.HashMap; @@ -29,11 +29,11 @@ import java.util.Set; import static org.hamcrest.Matchers.equalTo; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; public class UrlUtilsTest { diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/version/VersionTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/version/VersionTest.java index 10da6f047ad..fd00216a5e9 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/version/VersionTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/version/VersionTest.java @@ -19,48 +19,48 @@ import org.apache.dubbo.common.Version; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; public class VersionTest { @Test public void testGetProtocolVersion() { - Assertions.assertEquals(Version.getProtocolVersion(), Version.DEFAULT_DUBBO_PROTOCOL_VERSION); + Assert.assertEquals(Version.getProtocolVersion(), Version.DEFAULT_DUBBO_PROTOCOL_VERSION); } @Test public void testSupportResponseAttachment() { - Assertions.assertTrue(Version.isSupportResponseAttachment("2.0.2")); - Assertions.assertTrue(Version.isSupportResponseAttachment("2.0.3")); - Assertions.assertFalse(Version.isSupportResponseAttachment("2.0.0")); + Assert.assertTrue(Version.isSupportResponseAttachment("2.0.2")); + Assert.assertTrue(Version.isSupportResponseAttachment("2.0.3")); + Assert.assertFalse(Version.isSupportResponseAttachment("2.0.0")); } @Test public void testGetIntVersion() { - Assertions.assertEquals(2060100, Version.getIntVersion("2.6.1")); - Assertions.assertEquals(2060101, Version.getIntVersion("2.6.1.1")); - Assertions.assertEquals(2070001, Version.getIntVersion("2.7.0.1")); - Assertions.assertEquals(2070000, Version.getIntVersion("2.7.0")); + Assert.assertEquals(2060100, Version.getIntVersion("2.6.1")); + Assert.assertEquals(2060101, Version.getIntVersion("2.6.1.1")); + Assert.assertEquals(2070001, Version.getIntVersion("2.7.0.1")); + Assert.assertEquals(2070000, Version.getIntVersion("2.7.0")); } @Test public void testIsFramework270OrHigher() { - Assertions.assertTrue(Version.isRelease270OrHigher("2.7.0")); - Assertions.assertTrue(Version.isRelease270OrHigher("2.7.0.1")); - Assertions.assertTrue(Version.isRelease270OrHigher("2.7.0.2")); - Assertions.assertTrue(Version.isRelease270OrHigher("2.8.0")); - Assertions.assertFalse(Version.isRelease270OrHigher("2.6.3")); - Assertions.assertFalse(Version.isRelease270OrHigher("2.6.3.1")); + Assert.assertTrue(Version.isRelease270OrHigher("2.7.0")); + Assert.assertTrue(Version.isRelease270OrHigher("2.7.0.1")); + Assert.assertTrue(Version.isRelease270OrHigher("2.7.0.2")); + Assert.assertTrue(Version.isRelease270OrHigher("2.8.0")); + Assert.assertFalse(Version.isRelease270OrHigher("2.6.3")); + Assert.assertFalse(Version.isRelease270OrHigher("2.6.3.1")); } @Test public void testIsFramework263OrHigher() { - Assertions.assertTrue(Version.isRelease263OrHigher("2.7.0")); - Assertions.assertTrue(Version.isRelease263OrHigher("2.7.0.1")); - Assertions.assertTrue(Version.isRelease263OrHigher("2.6.4")); - Assertions.assertFalse(Version.isRelease263OrHigher("2.6.2")); - Assertions.assertFalse(Version.isRelease263OrHigher("2.6.1.1")); - Assertions.assertTrue(Version.isRelease263OrHigher("2.6.3")); + Assert.assertTrue(Version.isRelease263OrHigher("2.7.0")); + Assert.assertTrue(Version.isRelease263OrHigher("2.7.0.1")); + Assert.assertTrue(Version.isRelease263OrHigher("2.6.4")); + Assert.assertFalse(Version.isRelease263OrHigher("2.6.2")); + Assert.assertFalse(Version.isRelease263OrHigher("2.6.1.1")); + Assert.assertTrue(Version.isRelease263OrHigher("2.6.3")); } } diff --git a/dubbo-common/src/test/resources/log4j.xml b/dubbo-common/src/test/resources/log4j.xml index bfb523c0cc3..f5da181b5e3 100644 --- a/dubbo-common/src/test/resources/log4j.xml +++ b/dubbo-common/src/test/resources/log4j.xml @@ -1,19 +1,18 @@ @@ -21,7 +20,6 @@ - diff --git a/dubbo-compatible/pom.xml b/dubbo-compatible/pom.xml index 43d14f95104..4648309f69d 100644 --- a/dubbo-compatible/pom.xml +++ b/dubbo-compatible/pom.xml @@ -20,7 +20,7 @@ org.apache.dubbo dubbo-parent - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-compatible jar diff --git a/dubbo-compatible/src/main/java/com/alibaba/dubbo/common/URL.java b/dubbo-compatible/src/main/java/com/alibaba/dubbo/common/URL.java index 42bab293018..76b09f43db9 100644 --- a/dubbo-compatible/src/main/java/com/alibaba/dubbo/common/URL.java +++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/common/URL.java @@ -18,7 +18,6 @@ package com.alibaba.dubbo.common; import org.apache.dubbo.common.utils.CollectionUtils; -import org.apache.dubbo.common.utils.StringUtils; import java.net.InetSocketAddress; import java.util.Collection; @@ -27,10 +26,6 @@ @Deprecated public class URL extends org.apache.dubbo.common.URL { - protected URL() { - super(); - } - public URL(org.apache.dubbo.common.URL url) { super(url.getProtocol(), url.getUsername(), url.getPassword(), url.getHost(), url.getPort(), url.getPath(), url.getParameters()); } @@ -408,7 +403,7 @@ public boolean isAnyHost() { @Override public URL addParameterAndEncoded(String key, String value) { - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return this; } return addParameter(key, encode(value)); diff --git a/dubbo-compatible/src/main/java/com/alibaba/dubbo/config/annotation/Reference.java b/dubbo-compatible/src/main/java/com/alibaba/dubbo/config/annotation/Reference.java index c8f25b3b423..4c1ca979a17 100644 --- a/dubbo-compatible/src/main/java/com/alibaba/dubbo/config/annotation/Reference.java +++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/config/annotation/Reference.java @@ -43,7 +43,7 @@ boolean generic() default false; - boolean injvm() default true; + boolean injvm() default false; boolean check() default true; diff --git a/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Invoker.java b/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Invoker.java index 8811f59f797..55a948083f0 100644 --- a/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Invoker.java +++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Invoker.java @@ -17,25 +17,18 @@ package com.alibaba.dubbo.rpc; -import com.alibaba.dubbo.common.URL; +import org.apache.dubbo.common.URL; @Deprecated public interface Invoker extends org.apache.dubbo.rpc.Invoker { - Result invoke(Invocation invocation) throws RpcException; - - URL getUrl(); + @Override + Result invoke(org.apache.dubbo.rpc.Invocation invocation) throws RpcException; default org.apache.dubbo.rpc.Invoker getOriginal() { return null; } - // This method will never be called for a legacy invoker. - @Override - default org.apache.dubbo.rpc.Result invoke(org.apache.dubbo.rpc.Invocation invocation) throws org.apache.dubbo.rpc.RpcException { - return null; - } - class CompatibleInvoker implements Invoker { private org.apache.dubbo.rpc.Invoker invoker; @@ -50,13 +43,13 @@ public Class getInterface() { } @Override - public Result invoke(Invocation invocation) throws RpcException { - return new Result.CompatibleResult(invoker.invoke(invocation.getOriginal())); + public Result invoke(org.apache.dubbo.rpc.Invocation invocation) throws RpcException { + return new Result.CompatibleResult(invoker.invoke(((Invocation) invocation).getOriginal())); } @Override public URL getUrl() { - return new URL(invoker.getUrl()); + return invoker.getUrl(); } @Override diff --git a/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Protocol.java b/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Protocol.java index 18035416da2..34d56a8e743 100644 --- a/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Protocol.java +++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Protocol.java @@ -17,22 +17,6 @@ package com.alibaba.dubbo.rpc; -import com.alibaba.dubbo.common.URL; - @Deprecated public interface Protocol extends org.apache.dubbo.rpc.Protocol { - - Exporter export(Invoker invoker) throws RpcException; - - Invoker refer(Class aClass, URL url) throws RpcException; - - @Override - default org.apache.dubbo.rpc.Exporter export(org.apache.dubbo.rpc.Invoker invoker) throws RpcException { - return this.export(new Invoker.CompatibleInvoker<>(invoker)); - } - - @Override - default org.apache.dubbo.rpc.Invoker refer(Class aClass, org.apache.dubbo.common.URL url) throws RpcException { - return this.refer(aClass, new URL(url)); - } } diff --git a/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/cluster/Router.java b/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/cluster/Router.java index d8822bcd86e..2fb30d1bb35 100644 --- a/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/cluster/Router.java +++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/cluster/Router.java @@ -26,7 +26,7 @@ import java.util.stream.Collectors; @Deprecated -public interface Router extends org.apache.dubbo.rpc.cluster.Router{ +public interface Router extends org.apache.dubbo.rpc.cluster.Router { @Override com.alibaba.dubbo.common.URL getUrl(); @@ -36,7 +36,7 @@ List> route(List { - // Ignore those fields - static final String[] IGNORE_FIELD_NAMES = of("application", "module", "consumer", "monitor", "registry"); - private CompatibleReferenceBeanBuilder(Reference annotation, ClassLoader classLoader, ApplicationContext applicationContext) { super(annotation, classLoader, applicationContext); } @@ -99,30 +96,20 @@ protected ReferenceBean doBuild() { protected void preConfigureBean(Reference reference, ReferenceBean referenceBean) { Assert.notNull(interfaceClass, "The interface class must set first!"); DataBinder dataBinder = new DataBinder(referenceBean); - // Register CustomEditors for special fields - dataBinder.registerCustomEditor(String.class, "filter", new StringTrimmerEditor(true)); - dataBinder.registerCustomEditor(String.class, "listener", new StringTrimmerEditor(true)); - dataBinder.registerCustomEditor(Map.class, "parameters", new PropertyEditorSupport() { - @Override - public void setAsText(String text) throws java.lang.IllegalArgumentException { - // Trim all whitespace - String content = StringUtils.trimAllWhitespace(text); - if (!StringUtils.hasText(content)) { // No content , ignore directly - return; - } - // replace "=" to "," - content = StringUtils.replace(content, "=", ","); - // replace ":" to "," - content = StringUtils.replace(content, ":", ","); - // String[] to Map - Map parameters = CollectionUtils.toStringMap(commaDelimitedListToStringArray(content)); - setValue(parameters); - } - }); - + // Set ConversionService + dataBinder.setConversionService(getConversionService()); + // Ignore those fields + String[] ignoreAttributeNames = of("application", "module", "consumer", "monitor", "registry"); +// dataBinder.setDisallowedFields(ignoreAttributeNames); // Bind annotation attributes - dataBinder.bind(new AnnotationPropertyValuesAdapter(reference, applicationContext.getEnvironment(), IGNORE_FIELD_NAMES)); + dataBinder.bind(new AnnotationPropertyValuesAdapter(reference, applicationContext.getEnvironment(), ignoreAttributeNames)); + } + private ConversionService getConversionService() { + DefaultConversionService conversionService = new DefaultConversionService(); + conversionService.addConverter(new StringArrayToStringConverter()); + conversionService.addConverter(new StringArrayToMapConverter()); + return conversionService; } diff --git a/dubbo-compatible/src/main/java/org/apache/dubbo/config/spring/schema/CompatibleAnnotationBeanDefinitionParser.java b/dubbo-compatible/src/main/java/org/apache/dubbo/config/spring/schema/CompatibleAnnotationBeanDefinitionParser.java index 1172591190a..66d6ac2dcc3 100644 --- a/dubbo-compatible/src/main/java/org/apache/dubbo/config/spring/schema/CompatibleAnnotationBeanDefinitionParser.java +++ b/dubbo-compatible/src/main/java/org/apache/dubbo/config/spring/schema/CompatibleAnnotationBeanDefinitionParser.java @@ -16,9 +16,11 @@ */ package org.apache.dubbo.config.spring.schema; +import org.apache.dubbo.config.spring.AnnotationBean; import org.apache.dubbo.config.spring.beans.factory.annotation.CompatibleReferenceAnnotationBeanPostProcessor; import org.apache.dubbo.config.spring.beans.factory.annotation.CompatibleServiceAnnotationBeanPostProcessor; import org.apache.dubbo.config.spring.util.BeanRegistrar; + import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; @@ -32,7 +34,7 @@ import static org.springframework.util.StringUtils.trimArrayElements; /** - * {@link BeanDefinitionParser} + * {@link AnnotationBean} {@link BeanDefinitionParser} * * @see CompatibleServiceAnnotationBeanPostProcessor * @see CompatibleReferenceAnnotationBeanPostProcessor diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/cache/CacheTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/cache/CacheTest.java index b4305ced025..08d7d8d210e 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/cache/CacheTest.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/cache/CacheTest.java @@ -25,8 +25,8 @@ import com.alibaba.dubbo.rpc.Invocation; import com.alibaba.dubbo.rpc.Invoker; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.util.Map; @@ -45,7 +45,7 @@ public void testCacheFactory() { org.apache.dubbo.rpc.Invocation inv = new RpcInvocation(); org.apache.dubbo.cache.Cache c = factory.getCache(u, inv); String v = (String) c.get("testKey"); - Assertions.assertEquals("testValue", v); + Assert.assertEquals("testValue", v); } static class NullInvocation implements Invocation { diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/common/extension/ExtensionTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/common/extension/ExtensionTest.java index 2b7a5c86be8..124382711ec 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/common/extension/ExtensionTest.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/common/extension/ExtensionTest.java @@ -17,10 +17,10 @@ package org.apache.dubbo.common.extension; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.fail; public class ExtensionTest { @@ -28,13 +28,13 @@ public class ExtensionTest { public void testExtensionFactory() { try { ExtensionFactory factory = ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getExtension("myfactory"); - Assertions.assertTrue(factory instanceof ExtensionFactory); - Assertions.assertTrue(factory instanceof com.alibaba.dubbo.common.extension.ExtensionFactory); - Assertions.assertTrue(factory instanceof MyExtensionFactory); + Assert.assertTrue(factory instanceof ExtensionFactory); + Assert.assertTrue(factory instanceof com.alibaba.dubbo.common.extension.ExtensionFactory); + Assert.assertTrue(factory instanceof MyExtensionFactory); ExtensionFactory spring = ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getExtension("spring"); - Assertions.assertTrue(spring instanceof ExtensionFactory); - Assertions.assertFalse(spring instanceof com.alibaba.dubbo.common.extension.ExtensionFactory); + Assert.assertTrue(spring instanceof ExtensionFactory); + Assert.assertFalse(spring instanceof com.alibaba.dubbo.common.extension.ExtensionFactory); } catch (IllegalArgumentException expected) { fail(); } diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/common/extension/MockDispatcher.java b/dubbo-compatible/src/test/java/org/apache/dubbo/common/extension/MockDispatcher.java deleted file mode 100644 index 732f333c082..00000000000 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/common/extension/MockDispatcher.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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 org.apache.dubbo.common.extension; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.remoting.ChannelHandler; -import org.apache.dubbo.remoting.Dispatcher; - -public class MockDispatcher implements Dispatcher { - @Override - public ChannelHandler dispatch(ChannelHandler handler, URL url) { - return null; - } -} diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/config/ApplicationConfigTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/config/ApplicationConfigTest.java index 49966db9e05..ef4cd390c00 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/config/ApplicationConfigTest.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/config/ApplicationConfigTest.java @@ -22,8 +22,7 @@ import com.alibaba.dubbo.config.MonitorConfig; import com.alibaba.dubbo.config.RegistryConfig; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.Collections; import java.util.HashMap; @@ -35,7 +34,7 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.sameInstance; import static org.hamcrest.collection.IsCollectionWithSize.hasSize; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class ApplicationConfigTest { @Test @@ -92,12 +91,10 @@ public void testEnvironment1() throws Exception { assertThat(application.getEnvironment(), equalTo("product")); } - @Test + @Test(expected = IllegalStateException.class) public void testEnvironment2() throws Exception { - Assertions.assertThrows(IllegalStateException.class, () -> { - ApplicationConfig application = new ApplicationConfig("app"); - application.setEnvironment("illegal-env"); - }); + ApplicationConfig application = new ApplicationConfig("app"); + application.setEnvironment("illegal-env"); } @Test diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/config/ArgumentConfigTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/config/ArgumentConfigTest.java index e23b27639c9..18e149b4858 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/config/ArgumentConfigTest.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/config/ArgumentConfigTest.java @@ -19,7 +19,7 @@ import com.alibaba.dubbo.config.ArgumentConfig; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.HashMap; import java.util.Map; @@ -27,7 +27,7 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class ArgumentConfigTest { @Test diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/config/ConfigTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/config/ConfigTest.java index 4aadd60cf12..26e0bddad4a 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/config/ConfigTest.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/config/ConfigTest.java @@ -24,21 +24,21 @@ import com.alibaba.dubbo.config.ReferenceConfig; import com.alibaba.dubbo.config.ServiceConfig; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; public class ConfigTest { private com.alibaba.dubbo.config.ApplicationConfig applicationConfig = new com.alibaba.dubbo.config.ApplicationConfig("first-dubbo-test"); private com.alibaba.dubbo.config.RegistryConfig registryConfig = new com.alibaba.dubbo.config.RegistryConfig("multicast://224.5.6.7:1234"); - @AfterEach + @After public void tearDown() { ConfigManager.getInstance().clear(); } - @BeforeEach + @Before public void setup() { // In IDE env, make sure adding the following argument to VM options System.setProperty("java.net.preferIPv4Stack", "true"); @@ -60,6 +60,6 @@ public void testConfig() { reference.setInterface(DemoService.class); DemoService demoService = reference.get(); String message = demoService.sayHello("dubbo"); - Assertions.assertEquals("hello dubbo", message); + Assert.assertEquals("hello dubbo", message); } } diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/config/ConsumerConfigTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/config/ConsumerConfigTest.java index 5a3569f2308..386f218378f 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/config/ConsumerConfigTest.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/config/ConsumerConfigTest.java @@ -19,11 +19,11 @@ import com.alibaba.dubbo.config.ConsumerConfig; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class ConsumerConfigTest { @Test diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java index 075c5548085..033688612b4 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java @@ -25,7 +25,7 @@ import com.alibaba.dubbo.config.MethodConfig; import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.Collections; import java.util.HashMap; @@ -37,8 +37,8 @@ import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; public class MethodConfigTest { @Test diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/config/ModuleConfigTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/config/ModuleConfigTest.java index 4dc72dad90f..5dc9701a068 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/config/ModuleConfigTest.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/config/ModuleConfigTest.java @@ -21,8 +21,7 @@ import com.alibaba.dubbo.config.RegistryConfig; import org.hamcrest.Matchers; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.Collections; import java.util.HashMap; @@ -33,16 +32,14 @@ import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.sameInstance; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class ModuleConfigTest { - @Test + @Test(expected = IllegalStateException.class) public void testName1() throws Exception { - Assertions.assertThrows(IllegalStateException.class, () -> { - ModuleConfig module = new ModuleConfig(); - Map parameters = new HashMap(); - ModuleConfig.appendParameters(parameters, module); - }); + ModuleConfig module = new ModuleConfig(); + Map parameters = new HashMap(); + ModuleConfig.appendParameters(parameters, module); } @Test diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/config/ProtocolConfigTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/config/ProtocolConfigTest.java index 5cadde7ee42..f9e9ecd32f6 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/config/ProtocolConfigTest.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/config/ProtocolConfigTest.java @@ -19,7 +19,7 @@ import com.alibaba.dubbo.config.ProtocolConfig; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.Collections; import java.util.HashMap; @@ -28,7 +28,7 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class ProtocolConfigTest { diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/config/ProviderConfigTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/config/ProviderConfigTest.java index b26e26e31e9..10bdeb79561 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/config/ProviderConfigTest.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/config/ProviderConfigTest.java @@ -19,7 +19,7 @@ import com.alibaba.dubbo.config.ProviderConfig; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.HashMap; import java.util.Map; @@ -29,7 +29,7 @@ import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class ProviderConfigTest { @Test diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java index a7139ed61b9..3acd2c282a7 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java @@ -27,21 +27,21 @@ import com.alibaba.dubbo.config.RegistryConfig; import com.alibaba.dubbo.config.ServiceConfig; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; public class ReferenceConfigTest { private ApplicationConfig application = new ApplicationConfig(); private RegistryConfig registry = new RegistryConfig(); private ProtocolConfig protocol = new ProtocolConfig(); - @BeforeEach + @Before public void setUp() { ConfigManager.getInstance().clear(); } - @AfterEach + @After public void tearDown() { ConfigManager.getInstance().clear(); } diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/config/RegistryConfigTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/config/RegistryConfigTest.java index 3b66191b17b..00efb783615 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/config/RegistryConfigTest.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/config/RegistryConfigTest.java @@ -20,7 +20,7 @@ import com.alibaba.dubbo.common.Constants; import com.alibaba.dubbo.config.RegistryConfig; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.Collections; import java.util.HashMap; @@ -31,7 +31,7 @@ import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.not; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class RegistryConfigTest { @Test diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/echo/EchoServiceTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/echo/EchoServiceTest.java index 24cbfa291a4..2cba721f216 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/echo/EchoServiceTest.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/echo/EchoServiceTest.java @@ -28,8 +28,8 @@ import com.alibaba.dubbo.rpc.service.EchoService; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; public class EchoServiceTest { @@ -43,11 +43,11 @@ public void testEcho() { Invoker invoker = protocol.refer(DemoService.class, url); EchoService client = (EchoService) proxyFactory.getProxy(invoker); Object result = client.$echo("haha"); - Assertions.assertEquals("haha", result); + Assert.assertEquals("haha", result); org.apache.dubbo.rpc.service.EchoService newClient = (org.apache.dubbo.rpc.service.EchoService) proxyFactory.getProxy(invoker); Object res = newClient.$echo("hehe"); - Assertions.assertEquals("hehe", res); + Assert.assertEquals("hehe", res); invoker.destroy(); exporter.unexport(); } diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/filter/FilterTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/filter/FilterTest.java index 5463b58c8c1..14c20cf46c5 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/filter/FilterTest.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/filter/FilterTest.java @@ -17,17 +17,18 @@ package org.apache.dubbo.filter; +import org.apache.dubbo.rpc.Filter; +import org.apache.dubbo.rpc.Invocation; +import org.apache.dubbo.rpc.Invoker; +import org.apache.dubbo.rpc.Result; import org.apache.dubbo.rpc.RpcException; +import org.apache.dubbo.service.MockInvocation; -import com.alibaba.dubbo.rpc.Filter; -import com.alibaba.dubbo.rpc.Invocation; -import com.alibaba.dubbo.rpc.Invoker; -import com.alibaba.dubbo.rpc.Result; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.fail; public class FilterTest { @@ -36,25 +37,25 @@ public class FilterTest { @Test public void testInvokeException() { try { - Invoker invoker = new LegacyInvoker(null); - Invocation invocation = new LegacyInvocation("aa"); + Invoker invoker = new MyInvoker(null); + Invocation invocation = new MockInvocation("aa"); myFilter.invoke(invoker, invocation); fail(); } catch (RpcException e) { - Assertions.assertTrue(e.getMessage().contains("arg0 illegal")); + Assert.assertTrue(e.getMessage().contains("arg0 illegal")); } } @Test public void testDefault() { - Invoker invoker = new LegacyInvoker(null); - Invocation invocation = new LegacyInvocation("bbb"); + Invoker invoker = new MyInvoker(null); + Invocation invocation = new MockInvocation("bbb"); Result res = myFilter.invoke(invoker, invocation); System.out.println(res); } - @AfterAll + @AfterClass public static void tear() { - Assertions.assertEquals(2, MyFilter.count); + Assert.assertEquals(2, MyFilter.count); } } diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/filter/LegacyInvocation.java b/dubbo-compatible/src/test/java/org/apache/dubbo/filter/LegacyInvocation.java deleted file mode 100644 index c1e87923928..00000000000 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/filter/LegacyInvocation.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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 org.apache.dubbo.filter; - -import org.apache.dubbo.common.Constants; - -import com.alibaba.dubbo.rpc.Invocation; -import com.alibaba.dubbo.rpc.Invoker; - -import java.util.HashMap; -import java.util.Map; - -/** - * MockInvocation.java - */ -public class LegacyInvocation implements Invocation { - - private String arg0; - - public LegacyInvocation(String arg0) { - this.arg0 = arg0; - } - - public String getMethodName() { - return "echo"; - } - - public Class[] getParameterTypes() { - return new Class[]{String.class}; - } - - public Object[] getArguments() { - return new Object[]{arg0}; - } - - public Map getAttachments() { - Map attachments = new HashMap(); - attachments.put(Constants.PATH_KEY, "dubbo"); - attachments.put(Constants.GROUP_KEY, "dubbo"); - attachments.put(Constants.VERSION_KEY, "1.0.0"); - attachments.put(Constants.DUBBO_VERSION_KEY, "1.0.0"); - attachments.put(Constants.TOKEN_KEY, "sfag"); - attachments.put(Constants.TIMEOUT_KEY, "1000"); - return attachments; - } - - public Invoker getInvoker() { - return null; - } - - @Override - public Object put(Object key, Object value) { - return null; - } - - @Override - public Object get(Object key) { - return null; - } - - @Override - public Map getAttributes() { - return null; - } - - public String getAttachment(String key) { - return getAttachments().get(key); - } - - public String getAttachment(String key, String defaultValue) { - return getAttachments().get(key); - } - -} \ No newline at end of file diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/filter/LegacyInvoker.java b/dubbo-compatible/src/test/java/org/apache/dubbo/filter/MyInvoker.java similarity index 79% rename from dubbo-compatible/src/test/java/org/apache/dubbo/filter/LegacyInvoker.java rename to dubbo-compatible/src/test/java/org/apache/dubbo/filter/MyInvoker.java index ee1288affff..7ba9995f6dc 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/filter/LegacyInvoker.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/filter/MyInvoker.java @@ -1,74 +1,74 @@ -/* - * 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 org.apache.dubbo.filter; - - -import org.apache.dubbo.rpc.RpcResult; -import org.apache.dubbo.service.DemoService; - -import com.alibaba.dubbo.common.URL; -import com.alibaba.dubbo.rpc.Invocation; -import com.alibaba.dubbo.rpc.Invoker; -import com.alibaba.dubbo.rpc.Result; -import com.alibaba.dubbo.rpc.RpcException; - -public class LegacyInvoker implements Invoker { - - URL url; - Class type; - boolean hasException = false; - - public LegacyInvoker(URL url) { - this.url = url; - type = (Class) DemoService.class; - } - - public LegacyInvoker(URL url, boolean hasException) { - this.url = url; - type = (Class) DemoService.class; - this.hasException = hasException; - } - - @Override - public Class getInterface() { - return type; - } - - public URL getUrl() { - return url; - } - - @Override - public boolean isAvailable() { - return false; - } - - public Result invoke(Invocation invocation) throws RpcException { - RpcResult result = new RpcResult(); - if (hasException == false) { - result.setValue("alibaba"); - } else { - result.setException(new RuntimeException("mocked exception")); - } - return new Result.CompatibleResult(result); - } - - @Override - public void destroy() { - } - -} +/* + * 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 org.apache.dubbo.filter; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.rpc.Invocation; +import org.apache.dubbo.rpc.Invoker; +import org.apache.dubbo.rpc.Result; +import org.apache.dubbo.rpc.RpcException; +import org.apache.dubbo.rpc.RpcResult; +import org.apache.dubbo.service.DemoService; + +public class MyInvoker implements Invoker { + + URL url; + Class type; + boolean hasException = false; + + public MyInvoker(URL url) { + this.url = url; + type = (Class) DemoService.class; + } + + public MyInvoker(URL url, boolean hasException) { + this.url = url; + type = (Class) DemoService.class; + this.hasException = hasException; + } + + @Override + public Class getInterface() { + return type; + } + + public URL getUrl() { + return url; + } + + @Override + public boolean isAvailable() { + return false; + } + + public Result invoke(Invocation invocation) throws RpcException { + RpcResult result = new RpcResult(); + if (hasException == false) { + result.setValue("alibaba"); + return result; + } else { + result.setException(new RuntimeException("mocked exception")); + return result; + } + + } + + @Override + public void destroy() { + } + +} diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/generic/GenericServiceTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/generic/GenericServiceTest.java index 09d62cc067c..3d753b58b8b 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/generic/GenericServiceTest.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/generic/GenericServiceTest.java @@ -35,14 +35,15 @@ import com.alibaba.fastjson.JSON; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; -import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; public class GenericServiceTest { @@ -57,11 +58,11 @@ public void testGeneric() { GenericService client = (GenericService) proxyFactory.getProxy(invoker, true); Object result = client.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{"haha"}); - Assertions.assertEquals("hello haha", result); + Assert.assertEquals("hello haha", result); org.apache.dubbo.rpc.service.GenericService newClient = (org.apache.dubbo.rpc.service.GenericService) proxyFactory.getProxy(invoker, true); Object res = newClient.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{"hehe"}); - Assertions.assertEquals("hello hehe", res); + Assert.assertEquals("hello hehe", res); invoker.destroy(); exporter.unexport(); } @@ -77,13 +78,13 @@ public void testGeneric2() { GenericService client = proxyFactory.getProxy(invoker, true); Object result = client.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{"haha"}); - Assertions.assertEquals("hello haha", result); + Assert.assertEquals("hello haha", result); Invoker invoker2 = protocol.refer(DemoService.class, url); GenericService client2 = (GenericService) proxyFactory.getProxy(invoker2, true); Object result2 = client2.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{"haha"}); - Assertions.assertEquals("hello haha", result2); + Assert.assertEquals("hello haha", result2); invoker.destroy(); exporter.unexport(); @@ -107,21 +108,21 @@ public void testGenericComplexCompute4FullServiceMetadata() { FullServiceDefinition fullServiceDefinition = ServiceDefinitionBuilder.buildFullDefinition(DemoService.class); MethodDefinition methodDefinition = getMethod("complexCompute", fullServiceDefinition.getMethods()); - Map mapObject = createComplexObject(fullServiceDefinition,var1, var2, l, var3, var4, testEnum); - ComplexObject complexObject = map2bean(mapObject); + Map parm2= createComplextObject(fullServiceDefinition,var1, var2, l, var3, var4, testEnum); + ComplexObject complexObject = map2bean(parm2); Invoker invoker = protocol.refer(GenericService.class, url); GenericService client = proxyFactory.getProxy(invoker, true); - Object result = client.$invoke(methodDefinition.getName(), methodDefinition.getParameterTypes(), new Object[]{"haha", mapObject}); - Assertions.assertEquals("haha###" + complexObject.toString(), result); + Object result = client.$invoke(methodDefinition.getName(), methodDefinition.getParameterTypes(), new Object[]{"haha", parm2}); + Assert.assertEquals("haha###" + complexObject.toString(), result); Invoker invoker2 = protocol.refer(DemoService.class, url); GenericService client2 = (GenericService) proxyFactory.getProxy(invoker2, true); - Object result2 = client2.$invoke("complexCompute", methodDefinition.getParameterTypes(), new Object[]{"haha2", mapObject}); - Assertions.assertEquals("haha2###" + complexObject.toString(), result2); + Object result2 = client2.$invoke("complexCompute", methodDefinition.getParameterTypes(), new Object[]{"haha2", parm2}); + Assert.assertEquals("haha2###" + complexObject.toString(), result2); invoker.destroy(); exporter.unexport(); @@ -149,9 +150,9 @@ public void testGenericFindComplexObject4FullServiceMetadata() { GenericService client = proxyFactory.getProxy(invoker, true); Object result = client.$invoke("findComplexObject", new String[]{"java.lang.String", "int", "long", "java.lang.String[]", "java.util.List", "org.apache.dubbo.service.ComplexObject$TestEnum"}, new Object[]{var1, var2, l, var3, var4, testEnum}); - Assertions.assertNotNull(result); + Assert.assertNotNull(result); ComplexObject r = map2bean((Map) result); - Assertions.assertEquals(r, createComplexObject(var1, var2, l, var3, var4, testEnum)); + Assert.assertEquals(r, createComplexObject(var1, var2, l, var3, var4, testEnum)); invoker.destroy(); exporter.unexport(); @@ -166,7 +167,7 @@ MethodDefinition getMethod(String methodName, List list) { return null; } - Map createComplexObject(FullServiceDefinition fullServiceDefinition, String var1, int var2, long l, String[] var3, List var4, ComplexObject.TestEnum testEnum) { + Map createComplextObject(FullServiceDefinition fullServiceDefinition, String var1, int var2, long l, String[] var3, List var4, ComplexObject.TestEnum testEnum) { List typeDefinitions = fullServiceDefinition.getTypes(); TypeDefinition topTypeDefinition = null; TypeDefinition innerTypeDefinition = null; @@ -183,22 +184,22 @@ Map createComplexObject(FullServiceDefinition fullServiceDefinit inner3TypeDefinition = typeDefinition; } } - Assertions.assertEquals(topTypeDefinition.getProperties().get("v").getType(), "long"); - Assertions.assertEquals(topTypeDefinition.getProperties().get("maps").getType(), "java.util.Map"); - Assertions.assertEquals(topTypeDefinition.getProperties().get("innerObject").getType(), "org.apache.dubbo.service.ComplexObject$InnerObject"); - Assertions.assertEquals(topTypeDefinition.getProperties().get("intList").getType(), "java.util.List"); - Assertions.assertEquals(topTypeDefinition.getProperties().get("strArrays").getType(), "java.lang.String[]"); - Assertions.assertEquals(topTypeDefinition.getProperties().get("innerObject3").getType(), "org.apache.dubbo.service.ComplexObject.InnerObject3[]"); - Assertions.assertEquals(topTypeDefinition.getProperties().get("testEnum").getType(), "org.apache.dubbo.service.ComplexObject.TestEnum"); - Assertions.assertEquals(topTypeDefinition.getProperties().get("innerObject2").getType(), "java.util.List"); + Assert.assertEquals(topTypeDefinition.getProperties().get("v").getType(), "long"); + Assert.assertEquals(topTypeDefinition.getProperties().get("maps").getType(), "java.util.Map"); + Assert.assertEquals(topTypeDefinition.getProperties().get("innerObject").getType(), "org.apache.dubbo.service.ComplexObject$InnerObject"); + Assert.assertEquals(topTypeDefinition.getProperties().get("intList").getType(), "java.util.List"); + Assert.assertEquals(topTypeDefinition.getProperties().get("strArrays").getType(), "java.lang.String[]"); + Assert.assertEquals(topTypeDefinition.getProperties().get("innerObject3").getType(), "org.apache.dubbo.service.ComplexObject.InnerObject3[]"); + Assert.assertEquals(topTypeDefinition.getProperties().get("testEnum").getType(), "org.apache.dubbo.service.ComplexObject.TestEnum"); + Assert.assertEquals(topTypeDefinition.getProperties().get("innerObject2").getType(), "java.util.Set"); - Assertions.assertSame(innerTypeDefinition.getProperties().get("innerA").getType(), "java.lang.String"); - Assertions.assertSame(innerTypeDefinition.getProperties().get("innerB").getType(), "int"); + Assert.assertSame(innerTypeDefinition.getProperties().get("innerA").getType(), "java.lang.String"); + Assert.assertSame(innerTypeDefinition.getProperties().get("innerB").getType(), "int"); - Assertions.assertSame(inner2TypeDefinition.getProperties().get("innerA2").getType(), "java.lang.String"); - Assertions.assertSame(inner2TypeDefinition.getProperties().get("innerB2").getType(), "int"); + Assert.assertSame(inner2TypeDefinition.getProperties().get("innerA2").getType(), "java.lang.String"); + Assert.assertSame(inner2TypeDefinition.getProperties().get("innerB2").getType(), "int"); - Assertions.assertSame(inner3TypeDefinition.getProperties().get("innerA3").getType(), "java.lang.String"); + Assert.assertSame(inner3TypeDefinition.getProperties().get("innerA3").getType(), "java.lang.String"); Map result = new HashMap<>(); result.put("v", l); @@ -215,16 +216,16 @@ Map createComplexObject(FullServiceDefinition fullServiceDefinit innerObjectMap.put("innerA", var1); innerObjectMap.put("innerB", var2); - List innerObject2List = new ArrayList<>(); - result.put("innerObject2", innerObject2List); + Set innerObject2Set = new HashSet<>(4); + result.put("innerObject2", innerObject2Set); Map innerObject2Tmp1 = new HashMap<>(4); innerObject2Tmp1.put("innerA2", var1 + "_21"); innerObject2Tmp1.put("innerB2", var2 + 100000); Map innerObject2Tmp2 = new HashMap<>(4); innerObject2Tmp2.put("innerA2", var1 + "_22"); innerObject2Tmp2.put("innerB2", var2 + 200000); - innerObject2List.add(innerObject2Tmp1); - innerObject2List.add(innerObject2Tmp2); + innerObject2Set.add(innerObject2Tmp1); + innerObject2Set.add(innerObject2Tmp2); Map innerObject3Tmp1 = new HashMap<>(4); innerObject3Tmp1.put("innerA3", var1 + "_31"); diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/CompatibleRouter.java b/dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/CompatibleRouter.java index 2351b214409..14f5a0e8ed6 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/CompatibleRouter.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/CompatibleRouter.java @@ -28,7 +28,6 @@ * */ public class CompatibleRouter implements Router { - @Override public URL getUrl() { return null; diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/RouterTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/RouterTest.java deleted file mode 100644 index 5288c53a231..00000000000 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/RouterTest.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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 org.apache.dubbo.rpc.cluster; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * - */ -public class RouterTest { - - private static List routers = new ArrayList<>(); - - @BeforeAll - public static void setUp () { - CompatibleRouter compatibleRouter = new CompatibleRouter(); - routers.add(compatibleRouter); - CompatibleRouter2 compatibleRouter2 = new CompatibleRouter2(); - routers.add(compatibleRouter2); - NewRouter newRouter = new NewRouter(); - routers.add(newRouter); - } - - @Test - public void testCompareTo () { - try { - Collections.sort(routers); - Assertions.assertTrue(true); - } catch (Exception e) { - Assertions.assertFalse(false); - } - } -} diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/serialization/SerializationTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/serialization/SerializationTest.java index 8957950a92e..10706370e37 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/serialization/SerializationTest.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/serialization/SerializationTest.java @@ -22,8 +22,8 @@ import org.hamcrest.CoreMatchers; import org.hamcrest.Matchers; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Before; +import org.junit.Test; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -32,7 +32,7 @@ import java.io.OutputStream; import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; public class SerializationTest { @@ -44,7 +44,7 @@ public class SerializationTest { private ByteArrayOutputStream byteArrayOutputStream; private ByteArrayInputStream byteArrayInputStream; - @BeforeEach + @Before public void setUp() throws Exception { this.mySerialization = new MySerialization(); diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/service/ComplexObject.java b/dubbo-compatible/src/test/java/org/apache/dubbo/service/ComplexObject.java index 3657a03a289..5c895380302 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/service/ComplexObject.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/service/ComplexObject.java @@ -16,12 +16,13 @@ */ package org.apache.dubbo.service; -import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Set; /** * ON 2018/11/5 @@ -45,7 +46,7 @@ public ComplexObject(String var1, int var2, long l, String[] var3, List InnerObject2 io22 = new InnerObject2(); io22.setInnerA2(var1 + "_22"); io22.setInnerB2(var2 + 200000); - this.setInnerObject2(new ArrayList<>(Arrays.asList(io21, io22))); + this.setInnerObject2(new HashSet(Arrays.asList(io21, io22))); InnerObject3 io31 = new InnerObject3(); io31.setInnerA3(var1 + "_31"); @@ -60,7 +61,7 @@ public ComplexObject(String var1, int var2, long l, String[] var3, List } private InnerObject innerObject; - private List innerObject2; + private Set innerObject2; private InnerObject3[] innerObject3; private String[] strArrays; private List intList; @@ -108,11 +109,11 @@ public void setTestEnum(TestEnum testEnum) { this.testEnum = testEnum; } - public List getInnerObject2() { + public Set getInnerObject2() { return innerObject2; } - public void setInnerObject2(List innerObject2) { + public void setInnerObject2(Set innerObject2) { this.innerObject2 = innerObject2; } @@ -224,7 +225,7 @@ public String getInnerA2() { return innerA2; } - public void setInnerA2(String innerA2) { + public void setInnerA2(String innerA) { this.innerA2 = innerA2; } @@ -232,7 +233,7 @@ public int getInnerB2() { return innerB2; } - public void setInnerB2(int innerB2) { + public void setInnerB2(int innerB) { this.innerB2 = innerB2; } diff --git a/dubbo-compatible/src/test/resources/META-INF/services/org.apache.dubbo.remoting.Dispatcher b/dubbo-compatible/src/test/resources/META-INF/services/org.apache.dubbo.remoting.Dispatcher deleted file mode 100644 index eba5810fcc9..00000000000 --- a/dubbo-compatible/src/test/resources/META-INF/services/org.apache.dubbo.remoting.Dispatcher +++ /dev/null @@ -1,18 +0,0 @@ -# -# 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. -# - -mockdispatcher=org.apache.dubbo.common.extension.MockDispatcher \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/pom.xml b/dubbo-config/dubbo-config-api/pom.xml index 19c7aca7bb7..1b2d1ff61c9 100644 --- a/dubbo-config/dubbo-config-api/pom.xml +++ b/dubbo-config/dubbo-config-api/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-config - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-config-api jar diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java index a40bfc3a2e5..6b5fd0bca09 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java @@ -304,9 +304,9 @@ protected static void checkExtension(Class type, String property, String valu * Check whether there is a Extension who's name (property) is value (special treatment is * required) * - * @param type The Extension type + * @param type The Extension type * @param property The extension key - * @param value The Extension name + * @param value The Extension name */ protected static void checkMultiExtension(Class type, String property, String value) { checkMultiName(property, value); @@ -359,7 +359,7 @@ protected static void checkMethodName(String property, String value) { } protected static void checkParameterName(Map parameters) { - if (CollectionUtils.isEmptyMap(parameters)) { + if (parameters == null || parameters.size() == 0) { return; } for (Map.Entry entry : parameters.entrySet()) { @@ -485,7 +485,12 @@ public Map getMetaData() { for (Method method : methods) { try { String name = method.getName(); - if (isMetaMethod(method)) { + if ((name.startsWith("get") || name.startsWith("is")) + && !name.equals("get") + && !"getClass".equals(name) + && Modifier.isPublic(method.getModifiers()) + && method.getParameterTypes().length == 0 + && ClassHelper.isPrimitive(method.getReturnType())) { String prop = calculateAttributeFromGetter(name); String key; Parameter parameter = method.getAnnotation(Parameter.class); @@ -557,7 +562,7 @@ public void refresh() { for (Method method : methods) { if (ClassHelper.isSetter(method)) { try { - String value = StringUtils.trim(compositeConfiguration.getString(extractPropertyName(getClass(), method))); + String value = compositeConfiguration.getString(extractPropertyName(getClass(), method)); // isTypeMatch() is called to avoid duplicate and incorrect update, for example, we have two 'setGeneric' methods in ReferenceConfig. if (StringUtils.isNotEmpty(value) && ClassHelper.isTypeMatch(method.getParameterTypes()[0], value)) { method.invoke(this, ClassHelper.convertPrimitive(method.getParameterTypes()[0], value)); @@ -615,29 +620,6 @@ public boolean isValid() { return true; } - private boolean isMetaMethod(Method method) { - String name = method.getName(); - if (!(name.startsWith("get") || name.startsWith("is"))) { - return false; - } - if ("get".equals(name)) { - return false; - } - if ("getClass".equals(name)) { - return false; - } - if (!Modifier.isPublic(method.getModifiers())) { - return false; - } - if (method.getParameterTypes().length != 0) { - return false; - } - if (!ClassHelper.isPrimitive(method.getReturnType())) { - return false; - } - return true; - } - @Override public boolean equals(Object obj) { if (obj == null || !(obj.getClass().getName().equals(this.getClass().getName()))) { diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java index 4269924c609..cc311a1db4f 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java @@ -18,7 +18,6 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.Version; import org.apache.dubbo.common.config.Environment; import org.apache.dubbo.common.extension.ExtensionLoader; @@ -91,7 +90,7 @@ public abstract class AbstractInterfaceConfig extends AbstractMethodConfig { protected String cluster; /** - * The {@link Filter} when the provider side exposed a service or the customer side references a remote service used, + * The {@link Filter} when the provicer side exposed a service or the customer side references a remote service used, * if there are more than one, you can use commas to separate them */ protected String filter; @@ -103,7 +102,7 @@ public abstract class AbstractInterfaceConfig extends AbstractMethodConfig { protected String listener; /** - * The owner of the service providers + * The owner of zhe service providers */ protected String owner; @@ -287,7 +286,7 @@ private DynamicConfiguration getDynamicConfiguration(URL url) { protected List loadRegistries(boolean provider) { // check && override if necessary List registryList = new ArrayList(); - if (CollectionUtils.isNotEmpty(registries)) { + if (registries != null && !registries.isEmpty()) { for (RegistryConfig config : registries) { String address = config.getAddress(); if (StringUtils.isEmpty(address)) { @@ -297,18 +296,16 @@ protected List loadRegistries(boolean provider) { Map map = new HashMap(); appendParameters(map, application); appendParameters(map, config); - map.put(Constants.PATH_KEY, RegistryService.class.getName()); + map.put("path", RegistryService.class.getName()); appendRuntimeParameters(map); - if (!map.containsKey(Constants.PROTOCOL_KEY)) { - map.put(Constants.PROTOCOL_KEY, Constants.DUBBO_PROTOCOL); + if (!map.containsKey("protocol")) { + map.put("protocol", "dubbo"); } List urls = UrlUtils.parseURLs(address, map); for (URL url : urls) { - url = URLBuilder.from(url) - .addParameter(Constants.REGISTRY_KEY, url.getProtocol()) - .setProtocol(Constants.REGISTRY_PROTOCOL) - .build(); + url = url.addParameter(Constants.REGISTRY_KEY, url.getProtocol()); + url = url.setProtocol(Constants.REGISTRY_PROTOCOL); if ((provider && url.getParameter(Constants.REGISTER_KEY, true)) || (!provider && url.getParameter(Constants.SUBSCRIBE_KEY, true))) { registryList.add(url); @@ -334,8 +331,11 @@ protected URL loadMonitor(URL registryURL) { appendRuntimeParameters(map); //set ip String hostToRegistry = ConfigUtils.getSystemProperty(Constants.DUBBO_IP_TO_REGISTRY); - if (StringUtils.isEmpty(hostToRegistry)) { + if (hostToRegistry == null || hostToRegistry.length() == 0) { hostToRegistry = NetUtils.getLocalHost(); + } else if (NetUtils.isInvalidLocalHost(hostToRegistry)) { + throw new IllegalArgumentException("Specified invalid registry ip from property:" + + Constants.DUBBO_IP_TO_REGISTRY + ", value:" + hostToRegistry); } map.put(Constants.REGISTER_IP_KEY, hostToRegistry); appendParameters(map, monitor); @@ -350,16 +350,12 @@ protected URL loadMonitor(URL registryURL) { if (getExtensionLoader(MonitorFactory.class).hasExtension("logstat")) { map.put(Constants.PROTOCOL_KEY, "logstat"); } else { - map.put(Constants.PROTOCOL_KEY, Constants.DUBBO_PROTOCOL); + map.put(Constants.PROTOCOL_KEY, Constants.DOBBO_PROTOCOL); } } return UrlUtils.parseURL(address, map); } else if (Constants.REGISTRY_PROTOCOL.equals(monitor.getProtocol()) && registryURL != null) { - return URLBuilder.from(registryURL) - .setProtocol(Constants.DUBBO_PROTOCOL) - .addParameter(Constants.PROTOCOL_KEY, Constants.REGISTRY_PROTOCOL) - .addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map)) - .build(); + return registryURL.setProtocol(Constants.DOBBO_PROTOCOL).addParameter(Constants.PROTOCOL_KEY, Constants.REGISTRY_PROTOCOL).addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map)); } return null; } @@ -375,7 +371,7 @@ static void appendRuntimeParameters(Map map) { private URL loadMetadataReporterURL() { String address = metadataReportConfig.getAddress(); - if (StringUtils.isEmpty(address)) { + if (address == null || address.length() == 0) { return null; } Map map = new HashMap(); @@ -407,13 +403,13 @@ protected void checkInterfaceAndMethods(Class interfaceClass, List name attribute is required! Please check: " + "" + ""); @@ -493,7 +489,7 @@ private void verify(Class interfaceClass, Class localClass) { } try { - //Check if the localClass a constructor with parameter who's type is interfaceClass + //Check if the localClass a contructor with parameter who's type is interfaceClass ReflectUtils.findConstructor(localClass, interfaceClass); } catch (NoSuchMethodException e) { throw new IllegalStateException("No such constructor \"public " + localClass.getSimpleName() + @@ -605,7 +601,7 @@ public void setLocal(Boolean local) { if (local == null) { setLocal((String) null); } else { - setLocal(local.toString()); + setLocal(String.valueOf(local)); } } @@ -627,7 +623,7 @@ public void setStub(Boolean stub) { if (stub == null) { setStub((String) null); } else { - setStub(stub.toString()); + setStub(String.valueOf(stub)); } } @@ -726,7 +722,7 @@ public void setModule(ModuleConfig module) { } public RegistryConfig getRegistry() { - return CollectionUtils.isEmpty(registries) ? null : registries.get(0); + return registries == null || registries.isEmpty() ? null : registries.get(0); } public void setRegistry(RegistryConfig registry) { diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractMethodConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractMethodConfig.java index fd751d7515c..0248b9a8ddf 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractMethodConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractMethodConfig.java @@ -178,7 +178,7 @@ public void setMock(Boolean mock) { if (mock == null) { setMock((String) null); } else { - setMock(mock.toString()); + setMock(String.valueOf(mock)); } } diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractReferenceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractReferenceConfig.java index cf490ed4079..325b1f1790e 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractReferenceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractReferenceConfig.java @@ -56,11 +56,11 @@ public abstract class AbstractReferenceConfig extends AbstractInterfaceConfig { /** * Lazy create connection */ - protected Boolean lazy = false; + protected Boolean lazy; protected String reconnect; - protected Boolean sticky = false; + protected Boolean sticky; /** * Whether to support event in stub. diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractServiceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractServiceConfig.java index ba7f129355f..069cd9d787b 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractServiceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractServiceConfig.java @@ -18,7 +18,6 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.config.context.ConfigManager; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.config.support.Parameter; import org.apache.dubbo.rpc.ExporterListener; @@ -47,7 +46,7 @@ public abstract class AbstractServiceConfig extends AbstractInterfaceConfig { /** * whether the service is deprecated */ - protected Boolean deprecated = false; + protected Boolean deprecated; /** * The time delay register service (milliseconds) @@ -74,7 +73,7 @@ public abstract class AbstractServiceConfig extends AbstractInterfaceConfig { * after the service registered,and it needs to be enabled manually; if you want to disable the service, you also need * manual processing */ - protected Boolean dynamic = false; + protected Boolean dynamic; /** * Whether to use token @@ -99,7 +98,7 @@ public abstract class AbstractServiceConfig extends AbstractInterfaceConfig { /** * Whether to register */ - private Boolean register = true; + private Boolean register; /** * Warm up period @@ -206,7 +205,7 @@ public void setProtocols(List protocols) { } public ProtocolConfig getProtocol() { - return CollectionUtils.isEmpty(protocols) ? null : protocols.get(0); + return protocols == null || protocols.isEmpty() ? null : protocols.get(0); } public void setProtocol(ProtocolConfig protocol) { diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ApplicationConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ApplicationConfig.java index d73e3e209e1..fa184c8348f 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ApplicationConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ApplicationConfig.java @@ -19,7 +19,6 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.compiler.support.AdaptiveCompiler; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.config.support.Parameter; @@ -203,7 +202,7 @@ public void setEnvironment(String environment) { } public RegistryConfig getRegistry() { - return CollectionUtils.isEmpty(registries) ? null : registries.get(0); + return registries == null || registries.isEmpty() ? null : registries.get(0); } public void setRegistry(RegistryConfig registry) { diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ArgumentConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ArgumentConfig.java index 74d906cf2ec..61db01f28fd 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ArgumentConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ArgumentConfig.java @@ -16,7 +16,6 @@ */ package org.apache.dubbo.config; -import org.apache.dubbo.config.annotation.Argument; import org.apache.dubbo.config.support.Parameter; import java.io.Serializable; @@ -45,15 +44,6 @@ public class ArgumentConfig implements Serializable { */ private Boolean callback; - public ArgumentConfig() { - } - - public ArgumentConfig(Argument argument) { - this.index = argument.index(); - this.type = argument.type(); - this.callback = argument.callback(); - } - @Parameter(excluded = true) public Integer getIndex() { return index; @@ -80,4 +70,4 @@ public Boolean isCallback() { return callback; } -} +} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ConfigCenterConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ConfigCenterConfig.java index 4b88702018e..58c613834ae 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ConfigCenterConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ConfigCenterConfig.java @@ -27,7 +27,7 @@ import java.util.concurrent.atomic.AtomicBoolean; /** - * ConfigCenterConfig + * */ public class ConfigCenterConfig extends AbstractConfig { private AtomicBoolean inited = new AtomicBoolean(false); @@ -95,7 +95,7 @@ public void setAddress(String address) { this.address = address; } - @Parameter(key = Constants.CONFIG_CLUSTER_KEY) + @Parameter(key = Constants.CONFIG_CLUSTER_KEY, useKeyAsProperty = false) public String getCluster() { return cluster; } @@ -104,7 +104,7 @@ public void setCluster(String cluster) { this.cluster = cluster; } - @Parameter(key = Constants.CONFIG_NAMESPACE_KEY) + @Parameter(key = Constants.CONFIG_NAMESPACE_KEY, useKeyAsProperty = false) public String getNamespace() { return namespace; } @@ -113,7 +113,7 @@ public void setNamespace(String namespace) { this.namespace = namespace; } - @Parameter(key = Constants.CONFIG_GROUP_KEY) + @Parameter(key = Constants.CONFIG_GROUP_KEY, useKeyAsProperty = false) public String getGroup() { return group; } @@ -122,7 +122,7 @@ public void setGroup(String group) { this.group = group; } - @Parameter(key = Constants.CONFIG_CHECK_KEY) + @Parameter(key = Constants.CONFIG_CHECK_KEY, useKeyAsProperty = false) public Boolean isCheck() { return check; } @@ -131,7 +131,7 @@ public void setCheck(Boolean check) { this.check = check; } - @Parameter(key = Constants.CONFIG_ENABLE_KEY) + @Parameter(key = Constants.CONFIG_ENABLE_KEY, useKeyAsProperty = false) public Boolean isHighestPriority() { return highestPriority; } @@ -156,7 +156,7 @@ public void setPassword(String password) { this.password = password; } - @Parameter(key = Constants.CONFIG_TIMEOUT_KEY) + @Parameter(key = Constants.CONFIG_TIMEOUT_KEY, useKeyAsProperty = false) public Long getTimeout() { return timeout; } @@ -165,7 +165,7 @@ public void setTimeout(Long timeout) { this.timeout = timeout; } - @Parameter(key = Constants.CONFIG_CONFIGFILE_KEY) + @Parameter(key = Constants.CONFIG_CONFIGFILE_KEY, useKeyAsProperty = false) public String getConfigFile() { return configFile; } @@ -183,7 +183,7 @@ public void setAppConfigFile(String appConfigFile) { this.appConfigFile = appConfigFile; } - @Parameter(key = Constants.CONFIG_APPNAME_KEY) + @Parameter(key = Constants.CONFIG_APPNAME_KEY, useKeyAsProperty = false) public String getAppName() { return appName; } diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ConsumerConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ConsumerConfig.java index afbd4c485af..ce106550601 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ConsumerConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ConsumerConfig.java @@ -19,7 +19,7 @@ import org.apache.dubbo.common.utils.StringUtils; /** - * The service consumer default configuration + * The service consumer default confuguration * * @export */ @@ -57,12 +57,6 @@ public class ConsumerConfig extends AbstractReferenceConfig { */ private Integer queues; - /** - * By default, a TCP long-connection communication is shared between the consumer process and the provider process. - * This property can be set to share multiple TCP long-connection communications. Note that only the dubbo protocol takes effect. - */ - private Integer shareconnections; - @Override public void setTimeout(Integer timeout) { super.setTimeout(timeout); @@ -124,12 +118,4 @@ public Integer getQueues() { public void setQueues(Integer queues) { this.queues = queues; } - - public Integer getShareconnections() { - return shareconnections; - } - - public void setShareconnections(Integer shareconnections) { - this.shareconnections = shareconnections; - } } diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/MetadataReportConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/MetadataReportConfig.java index e7add0a0e81..a4ace7a266d 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/MetadataReportConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/MetadataReportConfig.java @@ -25,7 +25,7 @@ import static org.apache.dubbo.common.Constants.PROPERTIES_CHAR_SEPERATOR; /** - * MetadataReportConfig + * RegistryConfig * * @export */ diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/MethodConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/MethodConfig.java index c9478d45cae..d65c6659a41 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/MethodConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/MethodConfig.java @@ -18,11 +18,8 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.config.annotation.Method; import org.apache.dubbo.config.support.Parameter; -import java.util.ArrayList; -import java.util.Collections; import java.util.List; /** @@ -120,46 +117,6 @@ public String getName() { return name; } - public MethodConfig() { - } - - public MethodConfig(Method method) { - appendAnnotation(Method.class, method); - - this.setReturn(method.isReturn()); - - if(!"".equals(method.oninvoke())){ - this.setOninvoke(method.oninvoke()); - } - if(!"".equals(method.onreturn())){ - this.setOnreturn(method.onreturn()); - } - if(!"".equals(method.onthrow())){ - this.setOnthrow(method.onthrow()); - } - - if (method.arguments() != null && method.arguments().length != 0) { - List argumentConfigs = new ArrayList(method.arguments().length); - this.setArguments(argumentConfigs); - for (int i = 0; i < method.arguments().length; i++) { - ArgumentConfig argumentConfig = new ArgumentConfig(method.arguments()[i]); - argumentConfigs.add(argumentConfig); - } - } - } - - public static List constructMethodConfig(Method[] methods) { - if (methods != null && methods.length != 0) { - List methodConfigs = new ArrayList(methods.length); - for (int i = 0; i < methods.length; i++) { - MethodConfig methodConfig = new MethodConfig(methods[i]); - methodConfigs.add(methodConfig); - } - return methodConfigs; - } - return Collections.emptyList(); - } - public void setName(String name) { checkMethodName("name", name); this.name = name; @@ -322,4 +279,4 @@ public String getPrefix() { + (StringUtils.isEmpty(serviceId) ? "" : ("." + serviceId)) + "." + getName(); } -} +} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ModuleConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ModuleConfig.java index b47c35c479f..36168096cf4 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ModuleConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ModuleConfig.java @@ -17,8 +17,6 @@ package org.apache.dubbo.config; import org.apache.dubbo.common.Constants; -import org.apache.dubbo.common.utils.CollectionUtils; -import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.config.support.Parameter; import java.util.ArrayList; @@ -83,7 +81,7 @@ public String getName() { public void setName(String name) { checkName(Constants.NAME, name); this.name = name; - if (StringUtils.isEmpty(id)) { + if (id == null || id.length() == 0) { id = name; } } @@ -116,7 +114,7 @@ public void setOrganization(String organization) { } public RegistryConfig getRegistry() { - return CollectionUtils.isEmpty(registries) ? null : registries.get(0); + return registries == null || registries.isEmpty() ? null : registries.get(0); } public void setRegistry(RegistryConfig registry) { diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ProtocolConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ProtocolConfig.java index 008f7bf03fb..5f60b4f736e 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ProtocolConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ProtocolConfig.java @@ -317,7 +317,7 @@ public String getCodec() { } public void setCodec(String codec) { - if (Constants.DUBBO_PROTOCOL.equals(name)) { + if (Constants.DOBBO_PROTOCOL.equals(name)) { checkMultiExtension(Codec.class, Constants.CODEC_KEY, codec); } this.codec = codec; @@ -328,7 +328,7 @@ public String getSerialization() { } public void setSerialization(String serialization) { - if (Constants.DUBBO_PROTOCOL.equals(name)) { + if (Constants.DOBBO_PROTOCOL.equals(name)) { checkMultiExtension(Serialization.class, Constants.SERIALIZATION_KEY, serialization); } this.serialization = serialization; @@ -371,7 +371,7 @@ public String getServer() { } public void setServer(String server) { - if (Constants.DUBBO_PROTOCOL.equals(name)) { + if (Constants.DOBBO_PROTOCOL.equals(name)) { checkMultiExtension(Transporter.class, Constants.SERVER_KEY, server); } this.server = server; @@ -382,7 +382,7 @@ public String getClient() { } public void setClient(String client) { - if (Constants.DUBBO_PROTOCOL.equals(name)) { + if (Constants.DOBBO_PROTOCOL.equals(name)) { checkMultiExtension(Transporter.class, Constants.CLIENT_KEY, client); } this.client = client; diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ProviderConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ProviderConfig.java index e6540c083e8..ddfba1f2c3d 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ProviderConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ProviderConfig.java @@ -405,8 +405,8 @@ public String getDispatcher() { } public void setDispatcher(String dispatcher) { - checkExtension(Dispatcher.class, Constants.DISPATCHER_KEY, dispatcher); - checkExtension(Dispatcher.class, "dispather", dispatcher); + checkExtension(Dispatcher.class, Constants.DISPATCHER_KEY, exchanger); + checkExtension(Dispatcher.class, "dispather", exchanger); this.dispatcher = dispatcher; } diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java index d5cc678d3f6..4e1d46722fa 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java @@ -21,8 +21,6 @@ import org.apache.dubbo.common.Version; import org.apache.dubbo.common.bytecode.Wrapper; import org.apache.dubbo.common.extension.ExtensionLoader; -import org.apache.dubbo.common.utils.ClassHelper; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.ConfigUtils; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.common.utils.StringUtils; @@ -55,6 +53,7 @@ import java.util.Map; import java.util.Properties; +import static org.apache.dubbo.common.utils.NetUtils.isInvalidLocalHost; /** * ReferenceConfig @@ -75,7 +74,7 @@ public class ReferenceConfig extends AbstractReferenceConfig { * *
  • when the url is dubbo://224.5.6.7:1234/org.apache.dubbo.config.api.DemoService?application=dubbo-sample, then * the protocol is DubboProtocol
  • - * + *

    * Actually,when the {@link ExtensionLoader} init the {@link Protocol} instants,it will automatically wraps two * layers, and eventually will get a ProtocolFilterWrapper or ProtocolListenerWrapper */ @@ -107,10 +106,7 @@ public class ReferenceConfig extends AbstractReferenceConfig { * The interface class of the reference service */ private Class interfaceClass; - - /** - * client type - */ + // client type private String client; /** @@ -178,7 +174,6 @@ public ReferenceConfig() { public ReferenceConfig(Reference reference) { appendAnnotation(Reference.class, reference); - setMethods(MethodConfig.constructMethodConfig(reference.methods())); } public URL toUrl() { @@ -194,7 +189,7 @@ public List toUrls() { * Check each config modules are created properly and override their properties if necessary. */ public void checkAndUpdateSubConfigs() { - if (StringUtils.isEmpty(interfaceName)) { + if (interfaceName == null || interfaceName.length() == 0) { throw new IllegalStateException(" interface not allow null!"); } completeCompoundConfigs(); @@ -232,7 +227,7 @@ public synchronized T get() { checkAndUpdateSubConfigs(); if (destroyed) { - throw new IllegalStateException("The invoker of ReferenceConfig(" + url + ") has already destroyed!"); + throw new IllegalStateException("Already destroyed!"); } if (ref == null) { init(); @@ -251,7 +246,7 @@ public synchronized void destroy() { try { invoker.destroy(); } catch (Throwable t) { - logger.warn("Unexpected error occured when destroy invoker of ReferenceConfig(" + url + ").", t); + logger.warn("Unexpected err when destroy invoker of ReferenceConfig(" + url + ").", t); } invoker = null; ref = null; @@ -266,7 +261,7 @@ private void init() { checkMock(interfaceClass); ConsumerModel consumerModel = new ConsumerModel(interfaceName, group, version, interfaceClass); - ApplicationModel.initConsumerModel(URL.buildKey(interfaceName, group, version), consumerModel); + ApplicationModel.initConsumerModel(getUniqueServiceName(), consumerModel); Map map = new HashMap(); @@ -280,7 +275,7 @@ private void init() { String[] methods = Wrapper.getWrapper(interfaceClass).getMethodNames(); if (methods.length == 0) { - logger.warn("No method found in service interface " + interfaceClass.getName()); + logger.warn("NO method found in service interface " + interfaceClass.getName()); map.put("methods", Constants.ANY_VALUE); } else { map.put("methods", StringUtils.join(new HashSet(Arrays.asList(methods)), ",")); @@ -292,7 +287,7 @@ private void init() { appendParameters(map, consumer, Constants.DEFAULT_KEY); appendParameters(map, this); Map attributes = null; - if (CollectionUtils.isNotEmpty(methods)) { + if (methods != null && !methods.isEmpty()) { attributes = new HashMap(); for (MethodConfig methodConfig : methods) { appendParameters(map, methodConfig, methodConfig.getName()); @@ -311,8 +306,10 @@ private void init() { } String hostToRegistry = ConfigUtils.getSystemProperty(Constants.DUBBO_IP_TO_REGISTRY); - if (StringUtils.isEmpty(hostToRegistry)) { + if (hostToRegistry == null || hostToRegistry.length() == 0) { hostToRegistry = NetUtils.getLocalHost(); + } else if (isInvalidLocalHost(hostToRegistry)) { + throw new IllegalArgumentException("Specified invalid registry ip from property:" + Constants.DUBBO_IP_TO_REGISTRY + ", value:" + hostToRegistry); } map.put(Constants.REGISTER_IP_KEY, hostToRegistry); @@ -323,8 +320,21 @@ private void init() { @SuppressWarnings({"unchecked", "rawtypes", "deprecation"}) private T createProxy(Map map) { - if (shouldJvmRefer(map)) { - URL url = new URL(Constants.LOCAL_PROTOCOL, Constants.LOCALHOST_VALUE, 0, interfaceClass.getName()).addParameters(map); + URL tmpUrl = new URL("temp", "localhost", 0, map); + final boolean isJvmRefer; + if (isInjvm() == null) { + if (url != null && url.length() > 0) { // if a url is specified, don't do local reference + isJvmRefer = false; + } else { + // by default, reference local service if there is + isJvmRefer = InjvmProtocol.getInjvmProtocol().isInjvmRefer(tmpUrl); + } + } else { + isJvmRefer = isInjvm(); + } + + if (isJvmRefer) { + URL url = new URL(Constants.LOCAL_PROTOCOL, NetUtils.LOCALHOST, 0, interfaceClass.getName()).addParameters(map); invoker = refprotocol.refer(interfaceClass, url); if (logger.isInfoEnabled()) { logger.info("Using injvm service " + interfaceClass.getName()); @@ -335,7 +345,7 @@ private T createProxy(Map map) { if (us != null && us.length > 0) { for (String u : us) { URL url = URL.valueOf(u); - if (StringUtils.isEmpty(url.getPath())) { + if (url.getPath() == null || url.getPath().length() == 0) { url = url.setPath(interfaceName); } if (Constants.REGISTRY_PROTOCOL.equals(url.getProtocol())) { @@ -348,7 +358,7 @@ private T createProxy(Map map) { } else { // assemble URL from register center's configuration checkRegistry(); List us = loadRegistries(false); - if (CollectionUtils.isNotEmpty(us)) { + if (us != null && !us.isEmpty()) { for (URL u : us) { URL monitorUrl = loadMonitor(u); if (monitorUrl != null) { @@ -384,7 +394,14 @@ private T createProxy(Map map) { } } - if (shouldCheck() && !invoker.isAvailable()) { + Boolean c = check; + if (c == null && consumer != null) { + c = consumer.isCheck(); + } + if (c == null) { + c = true; // default true + } + if (c && !invoker.isAvailable()) { // make it possible for consumer to retry later if provider is temporarily unavailable initialized = false; throw new IllegalStateException("Failed to check the status of the service " + interfaceName + ". No provider available for the service " + (group == null ? "" : group + "/") + interfaceName + (version == null ? "" : ":" + version) + " from the url " + invoker.getUrl() + " to the consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion()); @@ -405,55 +422,6 @@ private T createProxy(Map map) { return (T) proxyFactory.getProxy(invoker); } - /** - * Figure out should refer the service in the same JVM from configurations. The default behavior is true - * 1. if injvm is specified, then use it - * 2. then if a url is specified, then assume it's a remote call - * 3. otherwise, check scope parameter - * 4. if scope is not specified but the target service is provided in the same JVM, then prefer to make the local - * call, which is the default behavior - */ - protected boolean shouldJvmRefer(Map map) { - URL tmpUrl = new URL("temp", "localhost", 0, map); - boolean isJvmRefer; - if (isInjvm() == null) { - // if a url is specified, don't do local reference - if (url != null && url.length() > 0) { - isJvmRefer = false; - } else { - // by default, reference local service if there is - isJvmRefer = InjvmProtocol.getInjvmProtocol().isInjvmRefer(tmpUrl); - } - } else { - isJvmRefer = isInjvm(); - } - return isJvmRefer; - } - - protected boolean shouldCheck() { - Boolean shouldCheck = isCheck(); - if (shouldCheck == null && getConsumer()!= null) { - shouldCheck = getConsumer().isCheck(); - } - if (shouldCheck == null) { - // default true - shouldCheck = true; - } - return shouldCheck; - } - - protected boolean shouldInit() { - Boolean shouldInit = isInit(); - if (shouldInit == null && getConsumer() != null) { - shouldInit = getConsumer().isInit(); - } - if (shouldInit == null) { - // default is false - return false; - } - return shouldInit; - } - private void checkDefault() { createConsumerIfAbsent(); } @@ -516,7 +484,8 @@ public Class getInterfaceClass() { } try { if (interfaceName != null && interfaceName.length() > 0) { - this.interfaceClass = Class.forName(interfaceName, true, ClassHelper.getClassLoader()); + this.interfaceClass = Class.forName(interfaceName, true, Thread.currentThread() + .getContextClassLoader()); } } catch (ClassNotFoundException t) { throw new IllegalStateException(t.getMessage(), t); @@ -540,7 +509,7 @@ public String getInterface() { public void setInterface(String interfaceName) { this.interfaceName = interfaceName; - if (StringUtils.isEmpty(id)) { + if (id == null || id.length() == 0) { id = interfaceName; } } @@ -602,6 +571,19 @@ Invoker getInvoker() { return invoker; } + @Parameter(excluded = true) + public String getUniqueServiceName() { + StringBuilder buf = new StringBuilder(); + if (group != null && group.length() > 0) { + buf.append(group).append("/"); + } + buf.append(interfaceName); + if (version != null && version.length() > 0) { + buf.append(":").append(version); + } + return buf.toString(); + } + @Override @Parameter(excluded = true) public String getPrefix() { @@ -611,9 +593,9 @@ public String getPrefix() { private void resolveFile() { String resolve = System.getProperty(interfaceName); String resolveFile = null; - if (StringUtils.isEmpty(resolve)) { + if (resolve == null || resolve.length() == 0) { resolveFile = System.getProperty("dubbo.resolve.file"); - if (StringUtils.isEmpty(resolveFile)) { + if (resolveFile == null || resolveFile.length() == 0) { File userResolveFile = new File(new File(System.getProperty("user.home")), "dubbo-resolve.properties"); if (userResolveFile.exists()) { resolveFile = userResolveFile.getAbsolutePath(); @@ -621,12 +603,21 @@ private void resolveFile() { } if (resolveFile != null && resolveFile.length() > 0) { Properties properties = new Properties(); - try (FileInputStream fis = new FileInputStream(new File(resolveFile))) { + FileInputStream fis = null; + try { + fis = new FileInputStream(new File(resolveFile)); properties.load(fis); } catch (IOException e) { - throw new IllegalStateException("Failed to load " + resolveFile + ", cause: " + e.getMessage(), e); + throw new IllegalStateException("Unload " + resolveFile + ", cause: " + e.getMessage(), e); + } finally { + try { + if (null != fis) { + fis.close(); + } + } catch (IOException e) { + logger.warn(e.getMessage(), e); + } } - resolve = properties.getProperty(interfaceName); } } diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java index 3513c6d8cf3..c2bd4307c75 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java @@ -18,7 +18,6 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.Version; import org.apache.dubbo.common.bytecode.Wrapper; import org.apache.dubbo.common.config.Environment; @@ -44,24 +43,26 @@ import org.apache.dubbo.rpc.support.ProtocolUtils; import java.lang.reflect.Method; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; +import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.UUID; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import static org.apache.dubbo.common.Constants.LOCALHOST_VALUE; +import static org.apache.dubbo.common.utils.NetUtils.LOCALHOST; import static org.apache.dubbo.common.utils.NetUtils.getAvailablePort; import static org.apache.dubbo.common.utils.NetUtils.getLocalHost; +import static org.apache.dubbo.common.utils.NetUtils.isInvalidLocalHost; import static org.apache.dubbo.common.utils.NetUtils.isInvalidPort; /** @@ -83,7 +84,7 @@ public class ServiceConfig extends AbstractServiceConfig { * *

  • when the url is dubbo://224.5.6.7:1234/org.apache.dubbo.config.api.DemoService?application=dubbo-sample, then * the protocol is DubboProtocol
  • - * + *

    * Actually,when the {@link ExtensionLoader} init the {@link Protocol} instants,it will automatically wraps two * layers, and eventually will get a ProtocolFilterWrapper or ProtocolListenerWrapper */ @@ -170,12 +171,11 @@ public ServiceConfig() { public ServiceConfig(Service service) { appendAnnotation(Service.class, service); - setMethods(MethodConfig.constructMethodConfig(service.methods())); } @Deprecated private static List convertProviderToProtocol(List providers) { - if (CollectionUtils.isEmpty(providers)) { + if (providers == null || providers.isEmpty()) { return null; } List protocols = new ArrayList(providers.size()); @@ -187,7 +187,7 @@ private static List convertProviderToProtocol(List convertProtocolToProvider(List protocols) { - if (CollectionUtils.isEmpty(protocols)) { + if (protocols == null || protocols.isEmpty()) { return null; } List providers = new ArrayList(protocols.size()); @@ -272,7 +272,7 @@ public void checkAndUpdateSubConfigs() { this.refresh(); checkMetadataReport(); - if (StringUtils.isEmpty(interfaceName)) { + if (interfaceName == null || interfaceName.length() == 0) { throw new IllegalStateException(" interface not allow null!"); } @@ -334,53 +334,39 @@ private void appendParameters() { public synchronized void export() { checkAndUpdateSubConfigs(); - if (!shouldExport()) { + if (provider != null) { + if (export == null) { + export = provider.getExport(); + } + if (delay == null) { + delay = provider.getDelay(); + } + } + if (export != null && !export) { return; } - if (shouldDelay()) { + if (delay != null && delay > 0) { delayExportExecutor.schedule(this::doExport, delay, TimeUnit.MILLISECONDS); } else { doExport(); } } - private boolean shouldExport() { - Boolean shouldExport = getExport(); - if (shouldExport == null && provider != null) { - shouldExport = provider.getExport(); - } - - // default value is true - if (shouldExport == null) { - return true; - } - - return shouldExport; - } - - private boolean shouldDelay() { - Integer delay = getDelay(); - if (delay == null && provider != null) { - delay = provider.getDelay(); - } - return delay != null && delay > 0; - } - protected synchronized void doExport() { if (unexported) { - throw new IllegalStateException("The service " + interfaceClass.getName() + " has already unexported!"); + throw new IllegalStateException("Already unexported!"); } if (exported) { return; } exported = true; - if (StringUtils.isEmpty(path)) { + if (path == null || path.length() == 0) { path = interfaceName; } ProviderModel providerModel = new ProviderModel(interfaceName, group, version, ref, interfaceClass); - ApplicationModel.initProviderModel(URL.buildKey(interfaceName, group, version), providerModel); + ApplicationModel.initProviderModel(getUniqueServiceName(), providerModel); doExportUrls(); } @@ -408,7 +394,7 @@ public synchronized void unexport() { try { exporter.unexport(); } catch (Throwable t) { - logger.warn("Unexpected error occured when unexport " + exporter, t); + logger.warn("unexpected err when unexport" + exporter, t); } } exporters.clear(); @@ -426,7 +412,7 @@ private void doExportUrls() { private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List registryURLs) { String name = protocolConfig.getName(); - if (StringUtils.isEmpty(name)) { + if (name == null || name.length() == 0) { name = Constants.DUBBO; } @@ -438,7 +424,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r appendParameters(map, provider, Constants.DEFAULT_KEY); appendParameters(map, protocolConfig); appendParameters(map, this); - if (CollectionUtils.isNotEmpty(methods)) { + if (methods != null && !methods.isEmpty()) { for (MethodConfig method : methods) { appendParameters(map, method, method.getName()); String retryKey = method.getName() + ".retry"; @@ -449,7 +435,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r } } List arguments = method.getArguments(); - if (CollectionUtils.isNotEmpty(arguments)) { + if (arguments != null && !arguments.isEmpty()) { for (ArgumentConfig argument : arguments) { // convert argument type if (argument.getType() != null && argument.getType().length() > 0) { @@ -466,7 +452,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r if (argtypes[argument.getIndex()].getName().equals(argument.getType())) { appendParameters(map, argument, method.getName() + "." + argument.getIndex()); } else { - throw new IllegalArgumentException("Argument config error : the index attribute and type attribute not match :index :" + argument.getIndex() + ", type:" + argument.getType()); + throw new IllegalArgumentException("argument config error : the index attribute and type attribute not match :index :" + argument.getIndex() + ", type:" + argument.getType()); } } else { // multiple callbacks in the method @@ -475,7 +461,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r if (argclazz.getName().equals(argument.getType())) { appendParameters(map, argument, method.getName() + "." + j); if (argument.getIndex() != -1 && argument.getIndex() != j) { - throw new IllegalArgumentException("Argument config error : the index attribute and type attribute not match :index :" + argument.getIndex() + ", type:" + argument.getType()); + throw new IllegalArgumentException("argument config error : the index attribute and type attribute not match :index :" + argument.getIndex() + ", type:" + argument.getType()); } } } @@ -486,7 +472,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r } else if (argument.getIndex() != -1) { appendParameters(map, argument, method.getName() + "." + argument.getIndex()); } else { - throw new IllegalArgumentException("Argument config must set index or type attribute.eg: or "); + throw new IllegalArgumentException("argument config must set index or type attribute.eg: or "); } } @@ -505,7 +491,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r String[] methods = Wrapper.getWrapper(interfaceClass).getMethodNames(); if (methods.length == 0) { - logger.warn("No method found in service interface " + interfaceClass.getName()); + logger.warn("NO method found in service interface " + interfaceClass.getName()); map.put(Constants.METHODS_KEY, Constants.ANY_VALUE); } else { map.put(Constants.METHODS_KEY, StringUtils.join(new HashSet(Arrays.asList(methods)), ",")); @@ -518,10 +504,19 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r map.put(Constants.TOKEN_KEY, token); } } + if (Constants.LOCAL_PROTOCOL.equals(protocolConfig.getName())) { + protocolConfig.setRegister(false); + map.put("notify", "false"); + } // export service + String contextPath = protocolConfig.getContextpath(); + if ((contextPath == null || contextPath.length() == 0) && provider != null) { + contextPath = provider.getContextpath(); + } + String host = this.findConfigedHosts(protocolConfig, registryURLs, map); Integer port = this.findConfigedPorts(protocolConfig, name, map); - URL url = new URL(name, host, port, getContextPath(protocolConfig).map(p -> p + "/" + path).orElse(path), map); + URL url = new URL(name, host, port, (contextPath == null || contextPath.length() == 0 ? "" : contextPath + "/") + path, map); if (ExtensionLoader.getExtensionLoader(ConfiguratorFactory.class) .hasExtension(url.getProtocol())) { @@ -542,7 +537,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r if (logger.isInfoEnabled()) { logger.info("Export dubbo service " + interfaceClass.getName() + " to url " + url); } - if (CollectionUtils.isNotEmpty(registryURLs)) { + if (registryURLs != null && !registryURLs.isEmpty()) { for (URL registryURL : registryURLs) { url = url.addParameterIfAbsent(Constants.DYNAMIC_KEY, registryURL.getParameter(Constants.DYNAMIC_KEY)); URL monitorUrl = loadMonitor(registryURL); @@ -588,11 +583,10 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r @SuppressWarnings({"unchecked", "rawtypes"}) private void exportLocal(URL url) { if (!Constants.LOCAL_PROTOCOL.equalsIgnoreCase(url.getProtocol())) { - URL local = URLBuilder.from(url) + URL local = URL.valueOf(url.toFullString()) .setProtocol(Constants.LOCAL_PROTOCOL) - .setHost(LOCALHOST_VALUE) - .setPort(0) - .build(); + .setHost(LOCALHOST) + .setPort(0); Exporter exporter = protocol.export( proxyFactory.getInvoker(ref, (Class) interfaceClass, local)); exporters.add(exporter); @@ -600,14 +594,6 @@ private void exportLocal(URL url) { } } - private Optional getContextPath(ProtocolConfig protocolConfig) { - String contextPath = protocolConfig.getContextpath(); - if (StringUtils.isEmpty(contextPath) && provider != null) { - contextPath = provider.getContextpath(); - } - return Optional.ofNullable(contextPath); - } - protected Class getServiceClass(T ref) { return ref.getClass(); } @@ -626,20 +612,51 @@ private String findConfigedHosts(ProtocolConfig protocolConfig, List regist boolean anyhost = false; String hostToBind = getValueFromConfig(protocolConfig, Constants.DUBBO_IP_TO_BIND); + if (hostToBind != null && hostToBind.length() > 0 && isInvalidLocalHost(hostToBind)) { + throw new IllegalArgumentException("Specified invalid bind ip from property:" + Constants.DUBBO_IP_TO_BIND + ", value:" + hostToBind); + } // if bind ip is not found in environment, keep looking up - if (StringUtils.isEmpty(hostToBind)) { + if (hostToBind == null || hostToBind.length() == 0) { hostToBind = protocolConfig.getHost(); - if (provider != null && StringUtils.isEmpty(hostToBind)) { + if (provider != null && (hostToBind == null || hostToBind.length() == 0)) { hostToBind = provider.getHost(); } - - if (StringUtils.isEmpty(hostToBind)) { + if (isInvalidLocalHost(hostToBind)) { anyhost = true; - hostToBind = getLocalHost(); - - if (StringUtils.isEmpty(hostToBind)) { - hostToBind = findHostToBindByConnectRegistries(registryURLs); + try { + hostToBind = InetAddress.getLocalHost().getHostAddress(); + } catch (UnknownHostException e) { + logger.warn(e.getMessage(), e); + } + if (isInvalidLocalHost(hostToBind)) { + if (registryURLs != null && !registryURLs.isEmpty()) { + for (URL registryURL : registryURLs) { + if (Constants.MULTICAST.equalsIgnoreCase(registryURL.getParameter("registry"))) { + // skip multicast registry since we cannot connect to it via Socket + continue; + } + try { + Socket socket = new Socket(); + try { + SocketAddress addr = new InetSocketAddress(registryURL.getHost(), registryURL.getPort()); + socket.connect(addr, 1000); + hostToBind = socket.getLocalAddress().getHostAddress(); + break; + } finally { + try { + socket.close(); + } catch (Throwable e) { + } + } + } catch (Exception e) { + logger.warn(e.getMessage(), e); + } + } + } + if (isInvalidLocalHost(hostToBind)) { + hostToBind = getLocalHost(); + } } } } @@ -648,7 +665,9 @@ private String findConfigedHosts(ProtocolConfig protocolConfig, List regist // registry ip is not used for bind ip by default String hostToRegistry = getValueFromConfig(protocolConfig, Constants.DUBBO_IP_TO_REGISTRY); - if (StringUtils.isEmpty(hostToRegistry)) { + if (hostToRegistry != null && hostToRegistry.length() > 0 && isInvalidLocalHost(hostToRegistry)) { + throw new IllegalArgumentException("Specified invalid registry ip from property:" + Constants.DUBBO_IP_TO_REGISTRY + ", value:" + hostToRegistry); + } else if (hostToRegistry == null || hostToRegistry.length() == 0) { // bind ip is used as registry ip by default hostToRegistry = hostToBind; } @@ -658,25 +677,6 @@ private String findConfigedHosts(ProtocolConfig protocolConfig, List regist return hostToRegistry; } - private String findHostToBindByConnectRegistries(List registryURLs) { - if (CollectionUtils.isNotEmpty(registryURLs)) { - for (URL registryURL : registryURLs) { - if (Constants.MULTICAST.equalsIgnoreCase(registryURL.getParameter("registry"))) { - // skip multicast registry since we cannot connect to it via Socket - continue; - } - try (Socket socket = new Socket()) { - SocketAddress addr = new InetSocketAddress(registryURL.getHost(), registryURL.getPort()); - socket.connect(addr, 1000); - return socket.getLocalAddress().getHostAddress(); - } catch (Exception e) { - logger.warn(e.getMessage(), e); - } - } - } - return null; - } - /** * Register port and bind port for the provider, can be configured separately * Configuration priority: environment variable -> java system properties -> port property in protocol config file @@ -744,7 +744,7 @@ private Integer parsePort(String configPort) { private String getValueFromConfig(ProtocolConfig protocolConfig, String key) { String protocolPrefix = protocolConfig.getName().toUpperCase() + "_"; String port = ConfigUtils.getSystemProperty(protocolPrefix + key); - if (StringUtils.isEmpty(port)) { + if (port == null || port.length() == 0) { port = ConfigUtils.getSystemProperty(key); } return port; @@ -809,7 +809,7 @@ private void createProviderIfAbsent() { } private void checkProtocol() { - if (CollectionUtils.isEmpty(protocols) && provider != null) { + if ((protocols == null || protocols.isEmpty()) && provider != null) { setProtocols(provider.getProtocols()); } convertProtocolIdsToProtocols(); @@ -900,7 +900,7 @@ public void setInterface(Class interfaceClass) { public void setInterface(String interfaceName) { this.interfaceName = interfaceName; - if (StringUtils.isEmpty(id)) { + if (id == null || id.length() == 0) { id = interfaceName; } } @@ -997,6 +997,19 @@ public void setProviders(List providers) { this.protocols = convertProviderToProtocol(providers); } + @Parameter(excluded = true) + public String getUniqueServiceName() { + StringBuilder buf = new StringBuilder(); + if (group != null && group.length() > 0) { + buf.append(group).append("/"); + } + buf.append(StringUtils.isNotEmpty(path) ? path : interfaceName); + if (version != null && version.length() > 0) { + buf.append(":").append(version); + } + return buf.toString(); + } + @Override @Parameter(excluded = true) public String getPrefix() { diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Argument.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Argument.java deleted file mode 100644 index 6a0da8ea8d9..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Argument.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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 org.apache.dubbo.config.annotation; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * @since 2.6.5 - * - * 2018/9/29 - */ -@Documented -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.ANNOTATION_TYPE}) -@Inherited -public @interface Argument { - //argument: index -1 represents not set - int index() default -1; - - //argument type - String type() default ""; - - //callback interface - boolean callback() default false; -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Method.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Method.java deleted file mode 100644 index 0cdc97cf814..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Method.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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 org.apache.dubbo.config.annotation; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * @since 2.6.5 - * * - * * 2018/9/29 - */ -@Documented -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.ANNOTATION_TYPE}) -@Inherited -public @interface Method { - String name(); - - int timeout() default -1; - - int retries() default -1; - - String loadbalance() default ""; - - boolean async() default false; - - boolean sent() default true; - - int actives() default 0; - - int executes() default 0; - - boolean deprecated() default false; - - boolean sticky() default false; - - boolean isReturn() default true; - - String oninvoke() default ""; - - String onreturn() default ""; - - String onthrow() default ""; - - String cache() default ""; - - String validation() default ""; - - Argument[] arguments() default {}; -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Reference.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Reference.java index bdbbd356cb4..63e2cd5f1d1 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Reference.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Reference.java @@ -71,9 +71,9 @@ boolean generic() default false; /** - * When enable, prefer to call local service in the same JVM if it's present, default value is true + * When enable, prefer to call local service in the same JVM if it's present, default value is false */ - boolean injvm() default true; + boolean injvm() default false; /** * Check if service provider is available during boot up, default value is true @@ -256,10 +256,4 @@ * Protocol spring bean names */ String protocol() default ""; - - /** - * methods support - * @return - */ - Method[] methods() default {}; } diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Service.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Service.java index 2e6f9b684a7..6f956f50c05 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Service.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Service.java @@ -268,10 +268,4 @@ * Service tag name */ String tag() default ""; - - /** - * methods support - * @return - */ - Method[] methods() default {}; } diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/AbstractBuilder.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/AbstractBuilder.java deleted file mode 100644 index 404483e9afa..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/AbstractBuilder.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.config.AbstractConfig; - -import java.util.HashMap; -import java.util.Map; - -/** - * AbstractBuilder - * - * @since 2.7 - */ -public abstract class AbstractBuilder { - /** - * The config id - */ - protected String id; - protected String prefix; - - protected B id(String id) { - this.id = id; - return getThis(); - } - - protected B prefix(String prefix) { - this.prefix = prefix; - return getThis(); - } - - protected abstract B getThis(); - - protected static Map appendParameter(Map parameters, String key, String value) { - if (parameters == null) { - parameters = new HashMap<>(); - } - parameters.put(key, value); - return parameters; - } - - protected static Map appendParameters(Map parameters, Map appendParameters) { - if (parameters == null) { - parameters = new HashMap<>(); - } - parameters.putAll(appendParameters); - return parameters; - } - - protected void build(T instance) { - if (!StringUtils.isEmpty(id)) instance.setId(id); - if (!StringUtils.isEmpty(prefix)) instance.setPrefix(prefix); - } -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/AbstractInterfaceBuilder.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/AbstractInterfaceBuilder.java deleted file mode 100644 index af97b5b795b..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/AbstractInterfaceBuilder.java +++ /dev/null @@ -1,294 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.config.AbstractInterfaceConfig; -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.config.ConfigCenterConfig; -import org.apache.dubbo.config.MetadataReportConfig; -import org.apache.dubbo.config.ModuleConfig; -import org.apache.dubbo.config.MonitorConfig; -import org.apache.dubbo.config.RegistryConfig; -import org.apache.dubbo.rpc.Filter; - -import java.util.ArrayList; -import java.util.List; - -/** - * AbstractBuilder - * - * @since 2.7 - */ -public abstract class AbstractInterfaceBuilder> - extends AbstractMethodBuilder { - /** - * Local impl class name for the service interface - */ - protected String local; - - /** - * Local stub class name for the service interface - */ - protected String stub; - - /** - * Service monitor - */ - protected MonitorConfig monitor; - - /** - * Strategies for generating dynamic agents,there are two strategies can be choosed: jdk and javassist - */ - protected String proxy; - - /** - * Cluster type - */ - protected String cluster; - - /** - * The {@link Filter} when the provider side exposed a service or the customer side references a remote service used, - * if there are more than one, you can use commas to separate them - */ - protected String filter; - - /** - * The Listener when the provider side exposes a service or the customer side references a remote service used - * if there are more than one, you can use commas to separate them - */ - protected String listener; - - /** - * The owner of the service providers - */ - protected String owner; - - /** - * Connection limits, 0 means shared connection, otherwise it defines the connections delegated to the current service - */ - protected Integer connections; - - /** - * The layer of service providers - */ - protected String layer; - - /** - * The application info - */ - protected ApplicationConfig application; - - /** - * The module info - */ - protected ModuleConfig module; - - /** - * Registry centers - */ - protected List registries; - - protected String registryIds; - - // connection events - protected String onconnect; - - /** - * Disconnection events - */ - protected String ondisconnect; - protected MetadataReportConfig metadataReportConfig; - - protected ConfigCenterConfig configCenter; - - // callback limits - private Integer callbacks; - // the scope for referring/exporting a service, if it's local, it means searching in current JVM only. - private String scope; - - /** - * @param local - * @see org.apache.dubbo.config.builders.AbstractInterfaceBuilder#stub(String) - * @deprecated Replace to stub(String) - */ - @Deprecated - public B local(String local) { - this.local = local; - return getThis(); - } - - /** - * @param local - * @see org.apache.dubbo.config.builders.AbstractInterfaceBuilder#stub(Boolean) - * @deprecated Replace to stub(Boolean) - */ - @Deprecated - public B local(Boolean local) { - if (local != null) { - this.local = local.toString(); - } else { - this.local = null; - } - return getThis(); - } - - public B stub(String stub) { - this.stub = stub; - return getThis(); - } - - public B stub(Boolean stub) { - if (stub != null) { - this.stub = stub.toString(); - } else { - this.stub = null; - } - return getThis(); - } - - public B monitor(MonitorConfig monitor) { - this.monitor = monitor; - return getThis(); - } - - public B monitor(String monitor) { - this.monitor = new MonitorConfig(monitor); - return getThis(); - } - - public B proxy(String proxy) { - this.proxy = proxy; - return getThis(); - } - - public B cluster(String cluster) { - this.cluster = cluster; - return getThis(); - } - - public B filter(String filter) { - this.filter = filter; - return getThis(); - } - - public B listener(String listener) { - this.listener = listener; - return getThis(); - } - - public B owner(String owner) { - this.owner = owner; - return getThis(); - } - - public B connections(Integer connections) { - this.connections = connections; - return getThis(); - } - - public B layer(String layer) { - this.layer = layer; - return getThis(); - } - - public B application(ApplicationConfig application) { - this.application = application; - return getThis(); - } - - public B module(ModuleConfig module) { - this.module = module; - return getThis(); - } - - public B addRegistries(List registries) { - if (this.registries == null) { - this.registries = new ArrayList<>(); - } - this.registries.addAll(registries); - return getThis(); - } - - public B addRegistry(RegistryConfig registry) { - if (this.registries == null) { - this.registries = new ArrayList<>(); - } - this.registries.add(registry); - return getThis(); - } - - public B registryIds(String registryIds) { - this.registryIds = registryIds; - return getThis(); - } - - public B onconnect(String onconnect) { - this.onconnect = onconnect; - return getThis(); - } - - public B ondisconnect(String ondisconnect) { - this.ondisconnect = ondisconnect; - return getThis(); - } - - public B metadataReportConfig(MetadataReportConfig metadataReportConfig) { - this.metadataReportConfig = metadataReportConfig; - return getThis(); - } - - public B configCenter(ConfigCenterConfig configCenter) { - this.configCenter = configCenter; - return getThis(); - } - - public B callbacks(Integer callbacks) { - this.callbacks = callbacks; - return getThis(); - } - - public B scope(String scope) { - this.scope = scope; - return getThis(); - } - - public void build(T instance) { - super.build(instance); - - if (!StringUtils.isEmpty(local)) instance.setLocal(local); - if (!StringUtils.isEmpty(stub)) instance.setStub(stub); - if (monitor != null) instance.setMonitor(monitor); - if (!StringUtils.isEmpty(proxy)) instance.setProxy(proxy); - if (!StringUtils.isEmpty(cluster)) instance.setCluster(cluster); - if (!StringUtils.isEmpty(filter)) instance.setFilter(filter); - if (!StringUtils.isEmpty(listener)) instance.setListener(listener); - if (!StringUtils.isEmpty(owner)) instance.setOwner(owner); - if (connections != null) instance.setConnections(connections); - if (!StringUtils.isEmpty(layer)) instance.setLayer(layer); - if (application != null) instance.setApplication(application); - if (module != null) instance.setModule(module); - if (registries != null) instance.setRegistries(registries); - if (!StringUtils.isEmpty(registryIds)) instance.setRegistryIds(registryIds); - if (!StringUtils.isEmpty(onconnect)) instance.setOnconnect(onconnect); - if (!StringUtils.isEmpty(ondisconnect)) instance.setOndisconnect(ondisconnect); - if (metadataReportConfig != null) instance.setMetadataReportConfig(metadataReportConfig); - if (configCenter != null) instance.setConfigCenter(configCenter); - if (callbacks != null) instance.setCallbacks(callbacks); - if (!StringUtils.isEmpty(scope)) instance.setScope(scope); - } -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/AbstractMethodBuilder.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/AbstractMethodBuilder.java deleted file mode 100644 index bdd0933d9df..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/AbstractMethodBuilder.java +++ /dev/null @@ -1,188 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.config.AbstractMethodConfig; - -import java.util.Map; - -/** - * AbstractBuilder - * - * @since 2.7 - */ -public abstract class AbstractMethodBuilder> - extends AbstractBuilder{ - /** - * The timeout for remote invocation in milliseconds - */ - protected Integer timeout; - - /** - * The retry times - */ - protected Integer retries; - - /** - * max concurrent invocations - */ - protected Integer actives; - - /** - * The load balance - */ - protected String loadbalance; - - /** - * Whether to async - * note that: it is an unreliable asynchronism that ignores return values and does not block threads. - */ - protected Boolean async; - - /** - * Whether to ack async-sent - */ - protected Boolean sent; - - /** - * The name of mock class which gets called when a service fails to execute - * - * note that: the mock doesn't support on the provider side,and the mock is executed when a non-business exception - * occurs after a remote service call - */ - protected String mock; - - /** - * Merger - */ - protected String merger; - - /** - * Cache the return result with the call parameter as key, the following options are available: lru, threadlocal, - * jcache, etc. - */ - protected String cache; - - /** - * Whether JSR303 standard annotation validation is enabled or not, if enabled, annotations on method parameters will - * be validated - */ - protected String validation; - - /** - * The customized parameters - */ - protected Map parameters; - - /** - * Forks for forking cluster - */ - protected Integer forks; - - public B timeout(Integer timeout) { - this.timeout = timeout; - return getThis(); - } - - public B retries(Integer retries) { - this.retries = retries; - return getThis(); - } - - public B actives(Integer actives) { - this.actives = actives; - return getThis(); - } - - public B loadbalance(String loadbalance) { - this.loadbalance = loadbalance; - return getThis(); - } - - public B async(Boolean async) { - this.async = async; - return getThis(); - } - - public B sent(Boolean sent) { - this.sent = sent; - return getThis(); - } - - public B mock(String mock) { - this.mock = mock; - return getThis(); - } - - public B mock(Boolean mock) { - if (mock != null) { - this.mock = mock.toString(); - } else { - this.mock = null; - } - return getThis(); - } - - public B merger(String merger) { - this.merger = merger; - return getThis(); - } - - public B cache(String cache) { - this.cache = cache; - return getThis(); - } - - public B validation(String validation) { - this.validation = validation; - return getThis(); - } - - public B appendParameters(Map appendParameters) { - this.parameters = appendParameters(parameters, appendParameters); - return getThis(); - } - - public B appendParameter(String key, String value) { - this.parameters = appendParameter(parameters, key, value); - return getThis(); - } - - public B forks(Integer forks) { - this.forks = forks; - return getThis(); - } - - @SuppressWarnings("unchecked") - public void build(T instance) { - super.build(instance); - - if (actives != null) instance.setActives(actives); - if (async != null) instance.setAsync(async); - if (!StringUtils.isEmpty(cache)) instance.setCache(cache); - if (forks != null) instance.setForks(forks); - if (!StringUtils.isEmpty(loadbalance)) instance.setLoadbalance(loadbalance); - if (!StringUtils.isEmpty(merger)) instance.setMerger(merger); - if(!StringUtils.isEmpty(mock)) instance.setMock(mock); - if (retries != null) instance.setRetries(retries); - if (sent != null) instance.setSent(sent); - if (timeout != null) instance.setTimeout(timeout); - if (!StringUtils.isEmpty(validation)) instance.setValidation(validation); - if (parameters != null) instance.setParameters(parameters); - } -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/AbstractReferenceBuilder.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/AbstractReferenceBuilder.java deleted file mode 100644 index 7748317a74a..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/AbstractReferenceBuilder.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.config.AbstractReferenceConfig; - -/** - * AbstractBuilder - * - * @since 2.7 - */ -public abstract class AbstractReferenceBuilder> - extends AbstractInterfaceBuilder { - - /** - * Check if service provider exists, if not exists, it will be fast fail - */ - protected Boolean check; - - /** - * Whether to eagle-init - */ - protected Boolean init; - - /** - * Whether to use generic interface - */ - protected String generic; - - /** - * Whether to find reference's instance from the current JVM - */ - protected Boolean injvm; - - /** - * Lazy create connection - */ - protected Boolean lazy; - - protected String reconnect; - - protected Boolean sticky; - - /** - * The remote service version the customer side will reference - */ - protected String version; - - /** - * The remote service group the customer side will reference - */ - protected String group; - - public B check(Boolean check) { - this.check = check; - return getThis(); - } - - public B init(Boolean init) { - this.init = init; - return getThis(); - } - - public B generic(String generic) { - this.generic = generic; - return getThis(); - } - - public B generic(Boolean generic) { - if (generic != null) { - this.generic = generic.toString(); - } else { - this.generic = null; - } - return getThis(); - } - - /** - * @param injvm - * @see org.apache.dubbo.config.builders.AbstractInterfaceBuilder#scope(String) - * @deprecated instead, use the parameter scope to judge if it's in jvm, scope=local - */ - @Deprecated - public B injvm(Boolean injvm) { - this.injvm = injvm; - return getThis(); - } - - public B lazy(Boolean lazy) { - this.lazy = lazy; - return getThis(); - } - - public B reconnect(String reconnect) { - this.reconnect = reconnect; - return getThis(); - } - - public B sticky(Boolean sticky) { - this.sticky = sticky; - return getThis(); - } - - public B version(String version) { - this.version = version; - return getThis(); - } - - public B group(String group) { - this.group = group; - return getThis(); - } - - public void build(T instance) { - super.build(instance); - - if (check != null) instance.setCheck(check); - if (init != null) instance.setInit(init); - if (!StringUtils.isEmpty(generic)) instance.setGeneric(generic); - if (injvm != null) instance.setInjvm(injvm); - if (lazy != null) instance.setLazy(lazy); - if (!StringUtils.isEmpty(reconnect)) instance.setReconnect(reconnect); - if (sticky != null) instance.setSticky(sticky); - if (!StringUtils.isEmpty(version)) instance.setVersion(version); - if (!StringUtils.isEmpty(group)) instance.setGroup(group); - } -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/AbstractServiceBuilder.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/AbstractServiceBuilder.java deleted file mode 100644 index fce73884f69..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/AbstractServiceBuilder.java +++ /dev/null @@ -1,246 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.config.AbstractServiceConfig; -import org.apache.dubbo.config.ProtocolConfig; - -import java.util.ArrayList; -import java.util.List; - -/** - * AbstractBuilder - * - * @since 2.7 - */ -public abstract class AbstractServiceBuilder> - extends AbstractInterfaceBuilder { - - /** - * The service version - */ - protected String version; - - /** - * The service group - */ - protected String group; - - /** - * whether the service is deprecated - */ - protected Boolean deprecated; - - /** - * The time delay register service (milliseconds) - */ - protected Integer delay; - - /** - * Whether to export the service - */ - protected Boolean export; - - /** - * The service weight - */ - protected Integer weight; - - /** - * Document center - */ - protected String document; - - /** - * Whether to register as a dynamic service or not on register center, it the value is false, the status will be disabled - * after the service registered,and it needs to be enabled manually; if you want to disable the service, you also need - * manual processing - */ - protected Boolean dynamic; - - /** - * Whether to use token - */ - protected String token; - - /** - * Whether to export access logs to logs - */ - protected String accesslog; - - /** - * The protocol list the service will export with - */ - protected List protocols; - protected String protocolIds; - // provider tag - protected String tag; - // max allowed execute times - private Integer executes; - - /** - * Whether to register - */ - private Boolean register; - - /** - * Warm up period - */ - private Integer warmup; - - /** - * The serialization type - */ - private String serialization; - - public B version(String version) { - this.version = version; - return getThis(); - } - - public B group(String group) { - this.group = group; - return getThis(); - } - - public B deprecated(Boolean deprecated) { - this.deprecated = deprecated; - return getThis(); - } - - public B delay(Integer delay) { - this.delay = delay; - return getThis(); - } - - public B export(Boolean export) { - this.export = export; - return getThis(); - } - - public B weight(Integer weight) { - this.weight = weight; - return getThis(); - } - - public B document(String document) { - this.document = document; - return getThis(); - } - - public B dynamic(Boolean dynamic) { - this.dynamic = dynamic; - return getThis(); - } - - public B token(String token) { - this.token = token; - return getThis(); - } - - public B token(Boolean token) { - if (token != null) { - this.token = token.toString(); - } else { - this.token = null; - } - return getThis(); - } - - public B accesslog(String accesslog) { - this.accesslog = accesslog; - return getThis(); - } - - public B accesslog(Boolean accesslog) { - if (accesslog != null) { - this.accesslog = accesslog.toString(); - } else { - this.accesslog = null; - } - return getThis(); - } - - public B addProtocols(List protocols) { - if (this.protocols == null) { - this.protocols = new ArrayList<>(); - } - this.protocols.addAll(protocols); - return getThis(); - } - - public B addProtocol(ProtocolConfig protocol) { - if (this.protocols == null) { - this.protocols = new ArrayList<>(); - } - this.protocols.add(protocol); - return getThis(); - } - - public B protocolIds(String protocolIds) { - this.protocolIds = protocolIds; - return getThis(); - } - - public B tag(String tag) { - this.tag = tag; - return getThis(); - } - - public B executes(Integer executes) { - this.executes = executes; - return getThis(); - } - - public B register(Boolean register) { - this.register = register; - return getThis(); - } - - public B warmup(Integer warmup) { - this.warmup = warmup; - return getThis(); - } - - public B serialization(String serialization) { - this.serialization = serialization; - return getThis(); - } - - public void build(T instance) { - super.build(instance); - - if (!StringUtils.isEmpty(version)) instance.setVersion(version); - if (!StringUtils.isEmpty(group)) instance.setGroup(group); - if (deprecated != null) instance.setDeprecated(deprecated); - if (delay != null) instance.setDelay(delay); - if (export != null) instance.setExport(export); - if (weight != null) instance.setWeight(weight); - if (!StringUtils.isEmpty(document)) instance.setDocument(document); - if (dynamic != null) instance.setDynamic(dynamic); - if (!StringUtils.isEmpty(token)) instance.setToken(token); - if (!StringUtils.isEmpty(accesslog)) instance.setAccesslog(accesslog); - if (protocols != null) instance.setProtocols(protocols); - if (!StringUtils.isEmpty(protocolIds)) instance.setProtocolIds(protocolIds); - if (tag != null) instance.setTag(tag); - if (executes != null) instance.setExecutes(executes); - if (register != null) instance.setRegister(register); - if (warmup != null) instance.setWarmup(warmup); - if (!StringUtils.isEmpty(serialization)) instance.setSerialization(serialization); - } -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ApplicationBuilder.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ApplicationBuilder.java deleted file mode 100644 index b843a4d5dd5..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ApplicationBuilder.java +++ /dev/null @@ -1,193 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import org.apache.dubbo.common.Constants; -import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.config.MonitorConfig; -import org.apache.dubbo.config.RegistryConfig; - -/** - * This is a builder for build {@link ApplicationConfig}. - * @since 2.7 - */ -public class ApplicationBuilder extends AbstractBuilder { - private String name; - private String version; - private String owner; - private String organization; - private String architecture; - private String environment = Constants.PRODUCTION_ENVIRONMENT; - private String compiler; - private String logger; - private List registries; - private String registryIds; - private MonitorConfig monitor; - private Boolean isDefault; - private String dumpDirectory; - private Boolean qosEnable; - private Integer qosPort; - private Boolean qosAcceptForeignIp; - private Map parameters; - private String shutwait; - - public ApplicationBuilder name(String name) { - this.name = name; - return getThis(); - } - - public ApplicationBuilder version(String version) { - this.version = version; - return getThis(); - } - - public ApplicationBuilder owner(String owner) { - this.owner = owner; - return getThis(); - } - - public ApplicationBuilder organization(String organization) { - this.organization = organization; - return getThis(); - } - - public ApplicationBuilder architecture(String architecture) { - this.architecture = architecture; - return getThis(); - } - - public ApplicationBuilder environment(String environment) { - this.environment = environment; - return getThis(); - } - - public ApplicationBuilder compiler(String compiler) { - this.compiler = compiler; - return getThis(); - } - - public ApplicationBuilder logger(String logger) { - this.logger = logger; - return getThis(); - } - - public ApplicationBuilder addRegistry(RegistryConfig registry) { - if (this.registries == null) { - this.registries = new ArrayList<>(); - } - this.registries.add(registry); - return getThis(); - } - - public ApplicationBuilder addRegistries(List registries) { - if (this.registries == null) { - this.registries = new ArrayList<>(); - } - this.registries.addAll(registries); - return getThis(); - } - - public ApplicationBuilder registryIds(String registryIds) { - this.registryIds = registryIds; - return getThis(); - } - - public ApplicationBuilder monitor(MonitorConfig monitor) { - this.monitor = monitor; - return getThis(); - } - - public ApplicationBuilder monitor(String monitor) { - this.monitor = new MonitorConfig(monitor); - return getThis(); - } - - public ApplicationBuilder isDefault(Boolean isDefault) { - this.isDefault = isDefault; - return getThis(); - } - - public ApplicationBuilder dumpDirectory(String dumpDirectory) { - this.dumpDirectory = dumpDirectory; - return getThis(); - } - - public ApplicationBuilder qosEnable(Boolean qosEnable) { - this.qosEnable = qosEnable; - return getThis(); - } - - public ApplicationBuilder qosPort(Integer qosPort) { - this.qosPort = qosPort; - return getThis(); - } - - public ApplicationBuilder qosAcceptForeignIp(Boolean qosAcceptForeignIp) { - this.qosAcceptForeignIp = qosAcceptForeignIp; - return getThis(); - } - - public ApplicationBuilder shutwait(String shutwait) { - this.shutwait = shutwait; - return getThis(); - } - - public ApplicationBuilder appendParameter(String key, String value) { - this.parameters = appendParameter(parameters, key, value); - return getThis(); - } - - public ApplicationBuilder appendParameters(Map appendParameters) { - this.parameters = appendParameters(parameters, appendParameters); - return getThis(); - } - - public ApplicationConfig build() { - ApplicationConfig config = new ApplicationConfig(); - super.build(config); - - config.setName(name); - config.setVersion(this.version); - config.setOwner(this.owner); - config.setOrganization(this.organization); - config.setArchitecture(this.architecture); - config.setEnvironment(this.environment); - config.setCompiler(this.compiler); - config.setLogger(this.logger); - config.setRegistries(this.registries); - config.setRegistryIds(this.registryIds); - config.setMonitor(this.monitor); - config.setDefault(this.isDefault); - config.setDumpDirectory(this.dumpDirectory); - config.setQosEnable(this.qosEnable); - config.setQosPort(this.qosPort); - config.setQosAcceptForeignIp(this.qosAcceptForeignIp); - config.setParameters(this.parameters); - if (!StringUtils.isEmpty(shutwait)) config.setShutwait(shutwait); - return config; - } - - @Override - protected ApplicationBuilder getThis() { - return this; - } -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ArgumentBuilder.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ArgumentBuilder.java deleted file mode 100644 index 03b1df25c09..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ArgumentBuilder.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.ArgumentConfig; - -/** - * This is a builder for build {@link ArgumentConfig}. - * @since 2.7 - */ -public class ArgumentBuilder { - /** - * The argument index: index -1 represents not set - */ - private Integer index = -1; - - /** - * Argument type - */ - private String type; - - /** - * Whether the argument is the callback interface - */ - private Boolean callback; - - public ArgumentBuilder index(Integer index) { - this.index = index; - return this; - } - - public ArgumentBuilder type(String type) { - this.type = type; - return this; - } - - public ArgumentBuilder callback(Boolean callback) { - this.callback = callback; - return this; - } - - public ArgumentConfig build() { - ArgumentConfig argumentConfig = new ArgumentConfig(); - argumentConfig.setIndex(index); - argumentConfig.setType(type); - argumentConfig.setCallback(callback); - return argumentConfig; - } -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ConfigCenterBuilder.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ConfigCenterBuilder.java deleted file mode 100644 index 44110e1797d..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ConfigCenterBuilder.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.ConfigCenterConfig; - -import java.util.Map; - -/** - * This is a builder for build {@link ConfigCenterConfig}. - * - * @since 2.7 - */ -public class ConfigCenterBuilder extends AbstractBuilder { - - private String protocol; - private String address; - private String cluster; - private String namespace = "dubbo"; - private String group = "dubbo"; - private String username; - private String password; - private Long timeout = 3000L; - private Boolean highestPriority = true; - private Boolean check = true; - - private String appName; - private String configFile = "dubbo.properties"; - private String appConfigFile; - - private Map parameters; - - public ConfigCenterBuilder protocol(String protocol) { - this.protocol = protocol; - return getThis(); - } - - public ConfigCenterBuilder address(String address) { - this.address = address; - return getThis(); - } - - public ConfigCenterBuilder cluster(String cluster) { - this.cluster = cluster; - return getThis(); - } - - public ConfigCenterBuilder namespace(String namespace) { - this.namespace = namespace; - return getThis(); - } - - public ConfigCenterBuilder group(String group) { - this.group = group; - return getThis(); - } - - public ConfigCenterBuilder username(String username) { - this.username = username; - return getThis(); - } - - public ConfigCenterBuilder password(String password) { - this.password = password; - return getThis(); - } - - public ConfigCenterBuilder timeout(Long timeout) { - this.timeout = timeout; - return getThis(); - } - - public ConfigCenterBuilder highestPriority(Boolean highestPriority) { - this.highestPriority = highestPriority; - return getThis(); - } - - public ConfigCenterBuilder check(Boolean check) { - this.check = check; - return getThis(); - } - - public ConfigCenterBuilder appName(String appName) { - this.appName = appName; - return getThis(); - } - - public ConfigCenterBuilder configFile(String configFile) { - this.configFile = configFile; - return getThis(); - } - - public ConfigCenterBuilder appConfigFile(String appConfigFile) { - this.appConfigFile = appConfigFile; - return getThis(); - } - - public ConfigCenterBuilder appendParameters(Map appendParameters) { - this.parameters = appendParameters(this.parameters, appendParameters); - return getThis(); - } - - public ConfigCenterBuilder appendParameter(String key, String value) { - this.parameters = appendParameter(this.parameters, key, value); - return getThis(); - } - - public ConfigCenterConfig build() { - ConfigCenterConfig configCenter = new ConfigCenterConfig(); - super.build(configCenter); - - configCenter.setProtocol(protocol); - configCenter.setAddress(address); - configCenter.setCluster(cluster); - configCenter.setNamespace(namespace); - configCenter.setGroup(group); - configCenter.setUsername(username); - configCenter.setPassword(password); - configCenter.setTimeout(timeout); - configCenter.setHighestPriority(highestPriority); - configCenter.setCheck(check); - configCenter.setAppName(appName); - configCenter.setConfigFile(configFile); - configCenter.setAppConfigFile(appConfigFile); - configCenter.setParameters(parameters); - - return configCenter; - } - - @Override - protected ConfigCenterBuilder getThis() { - return this; - } -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ConsumerBuilder.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ConsumerBuilder.java deleted file mode 100644 index 992d0eaa250..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ConsumerBuilder.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.ConsumerConfig; - -/** - * This is a builder for build {@link ConsumerConfig}. - * - * @since 2.7 - */ -public class ConsumerBuilder extends AbstractReferenceBuilder { - - /** - * Whether to use the default protocol - */ - private Boolean isDefault; - - /** - * Networking framework client uses: netty, mina, etc. - */ - private String client; - - /** - * Consumer thread pool type: cached, fixed, limit, eager - */ - private String threadpool; - - /** - * Consumer threadpool core thread size - */ - private Integer corethreads; - - /** - * Consumer threadpool thread size - */ - private Integer threads; - - /** - * Consumer threadpool queue size - */ - private Integer queues; - - /** - * By default, a TCP long-connection communication is shared between the consumer process and the provider process. - * This property can be set to share multiple TCP long-connection communications. Note that only the dubbo protocol takes effect. - */ - private Integer shareconnections; - - public ConsumerBuilder isDefault(Boolean isDefault) { - this.isDefault = isDefault; - return getThis(); - } - - public ConsumerBuilder client(String client) { - this.client = client; - return getThis(); - } - - public ConsumerBuilder threadPool(String threadPool) { - this.threadpool = threadPool; - return getThis(); - } - - public ConsumerBuilder coreThreads(Integer coreThreads) { - this.corethreads = coreThreads; - return getThis(); - } - - public ConsumerBuilder threads(Integer threads) { - this.threads = threads; - return getThis(); - } - - public ConsumerBuilder queues(Integer queues) { - this.queues = queues; - return getThis(); - } - - public ConsumerBuilder shareConnections(Integer shareConnections) { - this.shareconnections = shareConnections; - return getThis(); - } - - public ConsumerConfig build() { - ConsumerConfig consumer = new ConsumerConfig(); - super.build(consumer); - - consumer.setDefault(isDefault); - consumer.setClient(client); - consumer.setThreadpool(threadpool); - consumer.setCorethreads(corethreads); - consumer.setThreads(threads); - consumer.setQueues(queues); - consumer.setShareconnections(shareconnections); - - return consumer; - } - - @Override - protected ConsumerBuilder getThis() { - return this; - } -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/DubboBuilders.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/DubboBuilders.java deleted file mode 100644 index 145e0ce50a0..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/DubboBuilders.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -/** - * The tools for creat builder - * - * @since 2.7 - */ -public class DubboBuilders { - public static ServiceBuilder serviceBuilder() { - return new ServiceBuilder<>(); - } - - public static ApplicationBuilder applicationBuilder() { - return new ApplicationBuilder(); - } - - public static ConfigCenterBuilder configCenterBuilder() { - return new ConfigCenterBuilder(); - } - - public static ConsumerBuilder consumerBuilder() { - return new ConsumerBuilder(); - } - - public static MetadataReportBuilder metadataReportBuilder() { - return new MetadataReportBuilder(); - } - - public static MethodBuilder methodBuilder() { - return new MethodBuilder(); - } - - public static MonitorBuilder monitorBuilder() { - return new MonitorBuilder(); - } - - public static ProviderBuilder providerBuilder() { - return new ProviderBuilder(); - } - - public static ProtocolBuilder protocolBuilder() { - return new ProtocolBuilder(); - } - - public static ReferenceBuilder referenceBuilder() { - return new ReferenceBuilder(); - } - - public static RegistryBuilder registryBuilder() { - return new RegistryBuilder(); - } - - public static ArgumentBuilder argumentBuilder() { - return new ArgumentBuilder(); - } -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/MetadataReportBuilder.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/MetadataReportBuilder.java deleted file mode 100644 index 140ea137907..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/MetadataReportBuilder.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.MetadataReportConfig; - -import java.util.Map; - -/** - * This is a builder for build {@link MetadataReportConfig}. - * - * @since 2.7 - */ -public class MetadataReportBuilder extends AbstractBuilder { - - // Register center address - private String address; - - // Username to login register center - private String username; - - // Password to login register center - private String password; - - // Request timeout in milliseconds for register center - private Integer timeout; - - /** - * The group the metadata in . It is the same as registry - */ - private String group; - - // Customized parameters - private Map parameters; - - private Integer retryTimes; - - private Integer retryPeriod; - /** - * By default the metadatastore will store full metadata repeatly every day . - */ - private Boolean cycleReport; - - /** - * Sync report, default async - */ - private Boolean syncReport; - - public MetadataReportBuilder address(String address) { - this.address = address; - return getThis(); - } - - public MetadataReportBuilder username(String username) { - this.username = username; - return getThis(); - } - - public MetadataReportBuilder password(String password) { - this.password = password; - return getThis(); - } - - public MetadataReportBuilder timeout(Integer timeout) { - this.timeout = timeout; - return getThis(); - } - - public MetadataReportBuilder group(String group) { - this.group = group; - return getThis(); - } - - public MetadataReportBuilder appendParameters(Map appendParameters) { - this.parameters = appendParameters(this.parameters, appendParameters); - return getThis(); - } - - public MetadataReportBuilder appendParameter(String key, String value) { - this.parameters = appendParameter(this.parameters, key, value); - return getThis(); - } - - public MetadataReportBuilder retryTimes(Integer retryTimes) { - this.retryTimes = retryTimes; - return getThis(); - } - - public MetadataReportBuilder retryPeriod(Integer retryPeriod) { - this.retryPeriod = retryPeriod; - return getThis(); - } - - public MetadataReportBuilder cycleReport(Boolean cycleReport) { - this.cycleReport = cycleReport; - return getThis(); - } - - public MetadataReportBuilder syncReport(Boolean syncReport) { - this.syncReport = syncReport; - return getThis(); - } - - public MetadataReportConfig build() { - MetadataReportConfig metadataReport = new MetadataReportConfig(); - super.build(metadataReport); - - metadataReport.setAddress(address); - metadataReport.setUsername(username); - metadataReport.setPassword(password); - metadataReport.setTimeout(timeout); - metadataReport.setGroup(group); - metadataReport.setParameters(parameters); - metadataReport.setRetryTimes(retryTimes); - metadataReport.setRetryPeriod(retryPeriod); - metadataReport.setCycleReport(cycleReport); - metadataReport.setSyncReport(syncReport); - - return metadataReport; - } - - @Override - protected MetadataReportBuilder getThis() { - return this; - } -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/MethodBuilder.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/MethodBuilder.java deleted file mode 100644 index 41bcd629ac5..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/MethodBuilder.java +++ /dev/null @@ -1,237 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.ArgumentConfig; -import org.apache.dubbo.config.MethodConfig; - -import java.util.ArrayList; -import java.util.List; - -/** - * This is a builder for build {@link MethodConfig}. - * - * @since 2.7 - */ -public class MethodBuilder extends AbstractMethodBuilder { - /** - * The method name - */ - private String name; - - /** - * Stat - */ - private Integer stat; - - /** - * Whether to retry - */ - private Boolean retry; - - /** - * If it's reliable - */ - private Boolean reliable; - - /** - * Thread limits for method invocations - */ - private Integer executes; - - /** - * If it's deprecated - */ - private Boolean deprecated; - - /** - * Whether to enable sticky - */ - private Boolean sticky; - - /** - * Whether need to return - */ - private Boolean isReturn; - - /** - * Callback instance when async-call is invoked - */ - private Object oninvoke; - - /** - * Callback method when async-call is invoked - */ - private String oninvokeMethod; - - /** - * Callback instance when async-call is returned - */ - private Object onreturn; - - /** - * Callback method when async-call is returned - */ - private String onreturnMethod; - - /** - * Callback instance when async-call has exception thrown - */ - private Object onthrow; - - /** - * Callback method when async-call has exception thrown - */ - private String onthrowMethod; - - /** - * The method arguments - */ - private List arguments; - - /** - * These properties come from MethodConfig's parent Config module, they will neither be collected directly from xml or API nor be delivered to url - */ - private String service; - private String serviceId; - - public MethodBuilder name(String name) { - this.name = name; - return getThis(); - } - - public MethodBuilder stat(Integer stat) { - this.stat = stat; - return getThis(); - } - - public MethodBuilder retry(Boolean retry) { - this.retry = retry; - return getThis(); - } - - public MethodBuilder reliable(Boolean reliable) { - this.reliable = reliable; - return getThis(); - } - - public MethodBuilder executes(Integer executes) { - this.executes = executes; - return getThis(); - } - - public MethodBuilder deprecated(Boolean deprecated) { - this.deprecated = deprecated; - return getThis(); - } - - public MethodBuilder sticky(Boolean sticky) { - this.sticky = sticky; - return getThis(); - } - - public MethodBuilder isReturn(Boolean isReturn) { - this.isReturn = isReturn; - return getThis(); - } - - public MethodBuilder oninvoke(Object oninvoke) { - this.oninvoke = oninvoke; - return getThis(); - } - - public MethodBuilder oninvokeMethod(String oninvokeMethod) { - this.oninvokeMethod = oninvokeMethod; - return getThis(); - } - - public MethodBuilder onreturn(Object onreturn) { - this.onreturn = onreturn; - return getThis(); - } - - public MethodBuilder onreturnMethod(String onreturnMethod) { - this.onreturnMethod = onreturnMethod; - return getThis(); - } - - public MethodBuilder onthrow(Object onthrow) { - this.onthrow = onthrow; - return getThis(); - } - - public MethodBuilder onthrowMethod(String onthrowMethod) { - this.onthrowMethod = onthrowMethod; - return getThis(); - } - - public MethodBuilder addArguments(List arguments) { - if (this.arguments == null) { - this.arguments = new ArrayList<>(); - } - this.arguments.addAll(arguments); - return getThis(); - } - - public MethodBuilder addArgument(ArgumentConfig argument) { - if (this.arguments == null) { - this.arguments = new ArrayList<>(); - } - this.arguments.add(argument); - return getThis(); - } - - public MethodBuilder service(String service) { - this.service = service; - return getThis(); - } - - public MethodBuilder serviceId(String serviceId) { - this.serviceId = serviceId; - return getThis(); - } - - public MethodConfig build() { - MethodConfig methodConfig = new MethodConfig(); - super.build(methodConfig); - - methodConfig.setArguments(arguments); - methodConfig.setDeprecated(deprecated); - methodConfig.setExecutes(executes); - methodConfig.setName(name); - methodConfig.setOninvoke(oninvoke); - methodConfig.setOninvokeMethod(oninvokeMethod); - methodConfig.setOnreturn(onreturn); - methodConfig.setOnreturnMethod(onreturnMethod); - methodConfig.setOnthrow(onthrow); - methodConfig.setOnthrowMethod(onthrowMethod); - methodConfig.setReturn(isReturn); - methodConfig.setService(service); - methodConfig.setServiceId(serviceId); - methodConfig.setSticky(sticky); - methodConfig.setReliable(reliable); - methodConfig.setStat(stat); - methodConfig.setRetry(retry); - - return methodConfig; - } - - @Override - protected MethodBuilder getThis() { - return this; - } -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ModuleBuilder.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ModuleBuilder.java deleted file mode 100644 index 3e4653683bb..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ModuleBuilder.java +++ /dev/null @@ -1,132 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.ModuleConfig; -import org.apache.dubbo.config.MonitorConfig; -import org.apache.dubbo.config.RegistryConfig; - -import java.util.ArrayList; -import java.util.List; - -/** - * This is a builder for build {@link ModuleConfig}. - * - * @since 2.7 - */ -public class ModuleBuilder extends AbstractBuilder { - /** - * Module name - */ - private String name; - - /** - * Module version - */ - private String version; - - /** - * Module owner - */ - private String owner; - - /** - * Module's organization - */ - private String organization; - - /** - * Registry centers - */ - private List registries; - - /** - * Monitor center - */ - private MonitorConfig monitor; - - /** - * If it's default - */ - private Boolean isDefault; - - public ModuleBuilder name(String name) { - this.name = name; - return getThis(); - } - - public ModuleBuilder version(String version) { - this.version = version; - return getThis(); - } - - public ModuleBuilder owner(String owner) { - this.owner = owner; - return getThis(); - } - - public ModuleBuilder organization(String organization) { - this.organization = organization; - return getThis(); - } - - public ModuleBuilder addRegistries(List registries) { - if (this.registries == null) { - this.registries = new ArrayList<>(); - } - this.registries.addAll(registries); - return getThis(); - } - - public ModuleBuilder addRegistry(RegistryConfig registry) { - if (this.registries == null) { - this.registries = new ArrayList<>(); - } - this.registries.add(registry); - return getThis(); - } - - public ModuleBuilder monitor(MonitorConfig monitor) { - this.monitor = monitor; - return getThis(); - } - - public ModuleBuilder isDefault(Boolean isDefault) { - this.isDefault = isDefault; - return getThis(); - } - - public ModuleConfig build() { - ModuleConfig moduleConfig = new ModuleConfig(); - super.build(moduleConfig); - - moduleConfig.setDefault(isDefault); - moduleConfig.setMonitor(monitor); - moduleConfig.setName(name); - moduleConfig.setOrganization(organization); - moduleConfig.setOwner(owner); - moduleConfig.setRegistries(registries); - moduleConfig.setVersion(version); - - return moduleConfig; - } - - @Override - protected ModuleBuilder getThis() { - return this; - } -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/MonitorBuilder.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/MonitorBuilder.java deleted file mode 100644 index cfe48eacdd2..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/MonitorBuilder.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import java.util.Map; - -import org.apache.dubbo.config.MonitorConfig; - -/** - * This is a builder for build {@link MonitorConfig}. - * - * @since 2.7 - */ -public class MonitorBuilder extends AbstractBuilder { - /** - * The protocol of the monitor, if the value is registry, it will search the monitor address from the registry center, - * otherwise, it will directly connect to the monitor center - */ - private String protocol; - - /** - * The monitor address - */ - private String address; - - /** - * The monitor user name - */ - private String username; - - /** - * The password - */ - private String password; - - private String group; - - private String version; - - private String interval; - - /** - * customized parameters - */ - private Map parameters; - - /** - * If it's default - */ - private Boolean isDefault; - - public MonitorBuilder protocol(String protocol) { - this.protocol = protocol; - return getThis(); - } - - public MonitorBuilder address(String address) { - this.address = address; - return getThis(); - } - - public MonitorBuilder username(String username) { - this.username = username; - return getThis(); - } - - public MonitorBuilder password(String password) { - this.password = password; - return getThis(); - } - - public MonitorBuilder group(String group) { - this.group = group; - return getThis(); - } - - public MonitorBuilder version(String version) { - this.version = version; - return getThis(); - } - - public MonitorBuilder interval(String interval) { - this.interval = interval; - return getThis(); - } - - public MonitorBuilder isDefault(Boolean isDefault) { - this.isDefault = isDefault; - return getThis(); - } - - public MonitorBuilder appendParameter(String key, String value) { - this.parameters = appendParameter(parameters, key, value); - return getThis(); - } - - public MonitorBuilder appendParameters(Map appendParameters) { - this.parameters = appendParameters(parameters, appendParameters); - return getThis(); - } - - public MonitorConfig build() { - MonitorConfig monitorConfig = new MonitorConfig(); - super.build(monitorConfig); - - monitorConfig.setProtocol(protocol); - monitorConfig.setAddress(address); - monitorConfig.setUsername(username); - monitorConfig.setPassword(password); - monitorConfig.setGroup(group); - monitorConfig.setVersion(version); - monitorConfig.setInterval(interval); - monitorConfig.setParameters(parameters); - monitorConfig.setDefault(isDefault); - - return monitorConfig; - } - - @Override - protected MonitorBuilder getThis() { - return this; - } -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ProtocolBuilder.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ProtocolBuilder.java deleted file mode 100644 index 42e55503ea7..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ProtocolBuilder.java +++ /dev/null @@ -1,419 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.ProtocolConfig; - -import java.util.Map; - -/** - * This is a builder for build {@link ProtocolConfig}. - * - * @since 2.7 - */ -public class ProtocolBuilder extends AbstractBuilder { - /** - * Protocol name - */ - private String name; - - /** - * Service ip address (when there are multiple network cards available) - */ - private String host; - - /** - * Service port - */ - private Integer port; - - /** - * Context path - */ - private String contextpath; - - /** - * Thread pool - */ - private String threadpool; - - /** - * Thread pool core thread size - */ - private Integer corethreads; - - /** - * Thread pool size (fixed size) - */ - private Integer threads; - - /** - * IO thread pool size (fixed size) - */ - private Integer iothreads; - - /** - * Thread pool's queue length - */ - private Integer queues; - - /** - * Max acceptable connections - */ - private Integer accepts; - - /** - * Protocol codec - */ - private String codec; - - /** - * Serialization - */ - private String serialization; - - /** - * Charset - */ - private String charset; - - /** - * Payload max length - */ - private Integer payload; - - /** - * Buffer size - */ - private Integer buffer; - - /** - * Heartbeat interval - */ - private Integer heartbeat; - - /** - * Access log - */ - private String accesslog; - - /** - * Transfort - */ - private String transporter; - - /** - * How information is exchanged - */ - private String exchanger; - - /** - * Thread dispatch mode - */ - private String dispatcher; - - /** - * Networker - */ - private String networker; - - /** - * Sever impl - */ - private String server; - - /** - * Client impl - */ - private String client; - - /** - * Supported telnet commands, separated with comma. - */ - private String telnet; - - /** - * Command line prompt - */ - private String prompt; - - /** - * Status check - */ - private String status; - - /** - * Whether to register - */ - private Boolean register; - - /** - * whether it is a persistent connection - */ - //TODO add this to provider config - private Boolean keepAlive; - - // TODO add this to provider config - private String optimizer; - - /** - * The extension - */ - private String extension; - - /** - * The customized parameters - */ - private Map parameters; - - /** - * If it's default - */ - private Boolean isDefault; - - public ProtocolBuilder name(String name) { - this.name = name; - return getThis(); - } - - public ProtocolBuilder host(String host) { - this.host = host; - return getThis(); - } - - public ProtocolBuilder port(Integer port) { - this.port = port; - return getThis(); - } - - public ProtocolBuilder contextpath(String contextpath) { - this.contextpath = contextpath; - return getThis(); - } - - /** - * @see org.apache.dubbo.config.builders.ProtocolBuilder#contextpath(String) - * @param path - * @return ProtocolBuilder - */ - @Deprecated - public ProtocolBuilder path(String path) { - this.contextpath = path; - return getThis(); - } - - public ProtocolBuilder threadpool(String threadpool) { - this.threadpool = threadpool; - return getThis(); - } - - public ProtocolBuilder corethreads(Integer corethreads) { - this.corethreads = corethreads; - return getThis(); - } - - public ProtocolBuilder threads(Integer threads) { - this.threads = threads; - return getThis(); - } - - public ProtocolBuilder iothreads(Integer iothreads) { - this.iothreads = iothreads; - return getThis(); - } - - public ProtocolBuilder queues(Integer queues) { - this.queues = queues; - return getThis(); - } - - public ProtocolBuilder accepts(Integer accepts) { - this.accepts = accepts; - return getThis(); - } - - public ProtocolBuilder codec(String codec) { - this.codec = codec; - return getThis(); - } - - public ProtocolBuilder serialization(String serialization) { - this.serialization = serialization; - return getThis(); - } - - public ProtocolBuilder charset(String charset) { - this.charset = charset; - return getThis(); - } - - public ProtocolBuilder payload(Integer payload) { - this.payload = payload; - return getThis(); - } - - public ProtocolBuilder buffer(Integer buffer) { - this.buffer = buffer; - return getThis(); - } - - public ProtocolBuilder heartbeat(Integer heartbeat) { - this.heartbeat = heartbeat; - return getThis(); - } - - public ProtocolBuilder accesslog(String accesslog) { - this.accesslog = accesslog; - return getThis(); - } - - public ProtocolBuilder transporter(String transporter) { - this.transporter = transporter; - return getThis(); - } - - public ProtocolBuilder exchanger(String exchanger) { - this.exchanger = exchanger; - return getThis(); - } - - public ProtocolBuilder dispatcher(String dispatcher) { - this.dispatcher = dispatcher; - return getThis(); - } - - /** - * @see org.apache.dubbo.config.builders.ProtocolBuilder#dispatcher(String) - * @param dispather - * @return ProtocolBuilder - */ - @Deprecated - public ProtocolBuilder dispather(String dispather) { - this.dispatcher = dispather; - return getThis(); - } - - public ProtocolBuilder networker(String networker) { - this.networker = networker; - return getThis(); - } - - public ProtocolBuilder server(String server) { - this.server = server; - return getThis(); - } - - public ProtocolBuilder client(String client) { - this.client = client; - return getThis(); - } - - public ProtocolBuilder telnet(String telnet) { - this.telnet = telnet; - return getThis(); - } - - public ProtocolBuilder prompt(String prompt) { - this.prompt = prompt; - return getThis(); - } - - public ProtocolBuilder status(String status) { - this.status = status; - return getThis(); - } - - public ProtocolBuilder register(Boolean register) { - this.register = register; - return getThis(); - } - - public ProtocolBuilder keepAlive(Boolean keepAlive) { - this.keepAlive = keepAlive; - return getThis(); - } - - public ProtocolBuilder optimizer(String optimizer) { - this.optimizer = optimizer; - return getThis(); - } - - public ProtocolBuilder extension(String extension) { - this.extension = extension; - return getThis(); - } - - public ProtocolBuilder appendParameter(String key, String value) { - this.parameters = appendParameter(parameters, key, value); - return getThis(); - } - - public ProtocolBuilder appendParameters(Map appendParameters) { - this.parameters = appendParameters(parameters, appendParameters); - return getThis(); - } - - public ProtocolBuilder isDefault(Boolean isDefault) { - this.isDefault = isDefault; - return getThis(); - } - - public ProtocolConfig build() { - ProtocolConfig protocolConfig = new ProtocolConfig(); - super.build(protocolConfig); - - protocolConfig.setAccepts(accepts); - protocolConfig.setAccesslog(accesslog); - protocolConfig.setBuffer(buffer); - protocolConfig.setCharset(charset); - protocolConfig.setClient(client); - protocolConfig.setCodec(codec); - protocolConfig.setContextpath(contextpath); - protocolConfig.setCorethreads(corethreads); - protocolConfig.setDefault(isDefault); - protocolConfig.setDispatcher(dispatcher); - protocolConfig.setExchanger(exchanger); - protocolConfig.setExtension(extension); - protocolConfig.setHeartbeat(heartbeat); - protocolConfig.setHost(host); - protocolConfig.setIothreads(iothreads); - protocolConfig.setKeepAlive(keepAlive); - protocolConfig.setName(name); - protocolConfig.setNetworker(networker); - protocolConfig.setOptimizer(optimizer); - protocolConfig.setParameters(parameters); - protocolConfig.setPayload(payload); - protocolConfig.setPort(port); - protocolConfig.setPrompt(prompt); - protocolConfig.setQueues(queues); - protocolConfig.setRegister(register); - protocolConfig.setSerialization(serialization); - protocolConfig.setServer(server); - protocolConfig.setStatus(status); - protocolConfig.setTelnet(telnet); - protocolConfig.setThreadpool(threadpool); - protocolConfig.setThreads(threads); - protocolConfig.setTransporter(transporter); - - return protocolConfig; - } - - @Override - protected ProtocolBuilder getThis() { - return this; - } -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ProviderBuilder.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ProviderBuilder.java deleted file mode 100644 index 8a6e2213dc4..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ProviderBuilder.java +++ /dev/null @@ -1,293 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.ProviderConfig; - -/** - * This is a builder for build {@link ProviderConfig}. - * - * @since 2.7 - */ -public class ProviderBuilder extends AbstractServiceBuilder { - - /** - * Service ip addresses (used when there are multiple network cards available) - */ - private String host; - - /** - * Service port - */ - private Integer port; - - /** - * Context path - */ - private String contextpath; - - /** - * Thread pool - */ - private String threadpool; - - /** - * Thread pool size (fixed size) - */ - private Integer threads; - - /** - * IO thread pool size (fixed size) - */ - private Integer iothreads; - - /** - * Thread pool queue length - */ - private Integer queues; - - /** - * Max acceptable connections - */ - private Integer accepts; - - /** - * Protocol codec - */ - private String codec; - - /** - * The serialization charset - */ - private String charset; - - /** - * Payload max length - */ - private Integer payload; - - /** - * The network io buffer size - */ - private Integer buffer; - - /** - * Transporter - */ - private String transporter; - - /** - * How information gets exchanged - */ - private String exchanger; - - /** - * Thread dispatching mode - */ - private String dispatcher; - - /** - * Networker - */ - private String networker; - - /** - * The server-side implementation model of the protocol - */ - private String server; - - /** - * The client-side implementation model of the protocol - */ - private String client; - - /** - * Supported telnet commands, separated with comma. - */ - private String telnet; - - /** - * Command line prompt - */ - private String prompt; - - /** - * Status check - */ - private String status; - - /** - * Wait time when stop - */ - private Integer wait; - - /** - * Whether to use the default protocol - */ - private Boolean isDefault; - - public ProviderBuilder host(String host) { - this.host = host; - return getThis(); - } - - public ProviderBuilder port(Integer port) { - this.port = port; - return getThis(); - } - - public ProviderBuilder contextPath(String contextPath) { - this.contextpath = contextPath; - return getThis(); - } - - public ProviderBuilder threadPool(String threadPool) { - this.threadpool = threadPool; - return getThis(); - } - - public ProviderBuilder threads(Integer threads) { - this.threads = threads; - return getThis(); - } - - public ProviderBuilder ioThreads(Integer ioThreads) { - this.iothreads = ioThreads; - return getThis(); - } - - public ProviderBuilder queues(Integer queues) { - this.queues = queues; - return getThis(); - } - - public ProviderBuilder accepts(Integer accepts) { - this.accepts = accepts; - return getThis(); - } - - public ProviderBuilder codec(String codec) { - this.codec = codec; - return getThis(); - } - - public ProviderBuilder charset(String charset) { - this.charset = charset; - return getThis(); - } - - public ProviderBuilder payload(Integer payload) { - this.payload = payload; - return getThis(); - } - - public ProviderBuilder buffer(Integer buffer) { - this.buffer = buffer; - return getThis(); - } - - public ProviderBuilder transporter(String transporter) { - this.transporter = transporter; - return getThis(); - } - - public ProviderBuilder exchanger(String exchanger) { - this.exchanger = exchanger; - return getThis(); - } - - public ProviderBuilder dispatcher(String dispatcher) { - this.dispatcher = dispatcher; - return getThis(); - } - - public ProviderBuilder networker(String networker) { - this.networker = networker; - return getThis(); - } - - public ProviderBuilder server(String server) { - this.server = server; - return getThis(); - } - - public ProviderBuilder client(String client) { - this.client = client; - return getThis(); - } - - public ProviderBuilder telnet(String telnet) { - this.telnet = telnet; - return getThis(); - } - - public ProviderBuilder prompt(String prompt) { - this.prompt = prompt; - return getThis(); - } - - public ProviderBuilder status(String status) { - this.status = status; - return getThis(); - } - - public ProviderBuilder wait(Integer wait) { - this.wait = wait; - return getThis(); - } - - public ProviderBuilder isDefault(Boolean isDefault) { - this.isDefault = isDefault; - return getThis(); - } - - public ProviderConfig build() { - ProviderConfig provider = new ProviderConfig(); - super.build(provider); - - provider.setHost(host); - provider.setPort(port); - provider.setContextpath(contextpath); - provider.setThreadpool(threadpool); - provider.setThreads(threads); - provider.setIothreads(iothreads); - provider.setQueues(queues); - provider.setAccepts(accepts); - provider.setCodec(codec); - provider.setPayload(payload); - provider.setCharset(charset); - provider.setBuffer(buffer); - provider.setTransporter(transporter); - provider.setExchanger(exchanger); - provider.setDispatcher(dispatcher); - provider.setNetworker(networker); - provider.setServer(server); - provider.setClient(client); - provider.setTelnet(telnet); - provider.setPrompt(prompt); - provider.setStatus(status); - provider.setWait(wait); - provider.setDefault(isDefault); - - return provider; - } - - @Override - protected ProviderBuilder getThis() { - return this; - } -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ReferenceBuilder.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ReferenceBuilder.java deleted file mode 100644 index d0df60208e5..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ReferenceBuilder.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.ConsumerConfig; -import org.apache.dubbo.config.MethodConfig; -import org.apache.dubbo.config.ReferenceConfig; - -import java.util.ArrayList; -import java.util.List; - -/** - * This is a builder for build {@link ReferenceConfig}. - * - * @since 2.7 - */ -public class ReferenceBuilder extends AbstractReferenceBuilder> { - /** - * The interface name of the reference service - */ - private String interfaceName; - - /** - * The interface class of the reference service - */ - private Class interfaceClass; - - /** - * client type - */ - private String client; - - /** - * The url for peer-to-peer invocation - */ - private String url; - - /** - * The method configs - */ - private List methods; - - /** - * The consumer config (default) - */ - private ConsumerConfig consumer; - - /** - * Only the service provider of the specified protocol is invoked, and other protocols are ignored. - */ - private String protocol; - - public ReferenceBuilder interfaceName(String interfaceName) { - this.interfaceName = interfaceName; - return getThis(); - } - - public ReferenceBuilder interfaceClass(Class interfaceClass) { - this.interfaceClass = interfaceClass; - return getThis(); - } - - public ReferenceBuilder client(String client) { - this.client = client; - return getThis(); - } - - public ReferenceBuilder url(String url) { - this.url = url; - return getThis(); - } - - public ReferenceBuilder addMethods(List methods) { - if (this.methods == null) { - this.methods = new ArrayList<>(); - } - this.methods.addAll(methods); - return getThis(); - } - - public ReferenceBuilder addMethod(MethodConfig method) { - if (this.methods == null) { - this.methods = new ArrayList<>(); - } - this.methods.add(method); - return getThis(); - } - - public ReferenceBuilder consumer(ConsumerConfig consumer) { - this.consumer = consumer; - return getThis(); - } - - public ReferenceBuilder protocol(String protocol) { - this.protocol = protocol; - return getThis(); - } - - public ReferenceConfig build() { - ReferenceConfig reference = new ReferenceConfig<>(); - super.build(reference); - - reference.setInterface(interfaceName); - if (interfaceClass != null) { - reference.setInterface(interfaceClass); - } - reference.setClient(client); - reference.setUrl(url); - reference.setMethods(methods); - reference.setConsumer(consumer); - reference.setProtocol(protocol); - - return reference; - } - - @Override - protected ReferenceBuilder getThis() { - return this; - } -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/RegistryBuilder.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/RegistryBuilder.java deleted file mode 100644 index f64e595f37d..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/RegistryBuilder.java +++ /dev/null @@ -1,309 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import java.util.Map; - -import org.apache.dubbo.config.RegistryConfig; - -/** - * This is a builder for build {@link RegistryConfig}. - * - * @since 2.7 - */ -public class RegistryBuilder extends AbstractBuilder { - /** - * Register center address - */ - private String address; - - /** - * Username to login register center - */ - private String username; - - /** - * Password to login register center - */ - private String password; - - /** - * Default port for register center - */ - private Integer port; - - /** - * Protocol for register center - */ - private String protocol; - - /** - * Network transmission type - */ - private String transporter; - - private String server; - - private String client; - - private String cluster; - - /** - * The group the services registry in - */ - private String group; - - private String version; - - /** - * Request timeout in milliseconds for register center - */ - private Integer timeout; - - /** - * Session timeout in milliseconds for register center - */ - private Integer session; - - /** - * File for saving register center dynamic list - */ - private String file; - - /** - * Wait time before stop - */ - private Integer wait; - - /** - * Whether to check if register center is available when boot up - */ - private Boolean check; - - /** - * Whether to allow dynamic service to register on the register center - */ - private Boolean dynamic; - - /** - * Whether to export service on the register center - */ - private Boolean register; - - /** - * Whether allow to subscribe service on the register center - */ - private Boolean subscribe; - - /** - * The customized parameters - */ - private Map parameters; - - /** - * Whether it's default - */ - private Boolean isDefault; - - /** - * Simple the registry. both useful for provider and consumer - * - * @since 2.7.0 - */ - private Boolean simplified; - /** - * After simplify the registry, should add some paramter individually. just for provider. - *

    - * such as: extra-keys = A,b,c,d - * - * @since 2.7.0 - */ - private String extraKeys; - - public RegistryBuilder address(String address) { - this.address = address; - return getThis(); - } - - public RegistryBuilder username(String username) { - this.username = username; - return getThis(); - } - - public RegistryBuilder password(String password) { - this.password = password; - return getThis(); - } - - public RegistryBuilder port(Integer port) { - this.port = port; - return getThis(); - } - - public RegistryBuilder protocol(String protocol) { - this.protocol = protocol; - return getThis(); - } - - public RegistryBuilder transporter(String transporter) { - this.transporter = transporter; - return getThis(); - } - - /** - * @param transport - * @see #transporter(String) - * @deprecated - */ - @Deprecated - public RegistryBuilder transport(String transport) { - this.transporter = transport; - return getThis(); - } - - public RegistryBuilder server(String server) { - this.server = server; - return getThis(); - } - - public RegistryBuilder client(String client) { - this.client = client; - return getThis(); - } - - public RegistryBuilder cluster(String cluster) { - this.cluster = cluster; - return getThis(); - } - - public RegistryBuilder group(String group) { - this.group = group; - return getThis(); - } - - public RegistryBuilder version(String version) { - this.version = version; - return getThis(); - } - - public RegistryBuilder timeout(Integer timeout) { - this.timeout = timeout; - return getThis(); - } - - public RegistryBuilder session(Integer session) { - this.session = session; - return getThis(); - } - - public RegistryBuilder file(String file) { - this.file = file; - return getThis(); - } - - /** - * @param wait - * @see org.apache.dubbo.config.builders.ProviderBuilder#wait(Integer) - * @deprecated - */ - @Deprecated - public RegistryBuilder wait(Integer wait) { - this.wait = wait; - return getThis(); - } - - public RegistryBuilder isCheck(Boolean check) { - this.check = check; - return getThis(); - } - - public RegistryBuilder isDynamic(Boolean dynamic) { - this.dynamic = dynamic; - return getThis(); - } - - public RegistryBuilder register(Boolean register) { - this.register = register; - return getThis(); - } - - public RegistryBuilder subscribe(Boolean subscribe) { - this.subscribe = subscribe; - return getThis(); - } - - public RegistryBuilder appendParameter(String key, String value) { - this.parameters = appendParameter(parameters, key, value); - return getThis(); - } - - public RegistryBuilder appendParameters(Map appendParameters) { - this.parameters = appendParameters(parameters, appendParameters); - return getThis(); - } - - public RegistryBuilder isDefault(Boolean isDefault) { - this.isDefault = isDefault; - return getThis(); - } - - public RegistryBuilder simplified(Boolean simplified) { - this.simplified = simplified; - return getThis(); - } - - public RegistryBuilder extraKeys(String extraKeys) { - this.extraKeys = extraKeys; - return getThis(); - } - - public RegistryConfig build() { - RegistryConfig registry = new RegistryConfig(); - super.build(registry); - - registry.setAddress(address); - registry.setCheck(check); - registry.setClient(client); - registry.setCluster(cluster); - registry.setDefault(isDefault); - registry.setDynamic(dynamic); - registry.setExtraKeys(extraKeys); - registry.setFile(file); - registry.setGroup(group); - registry.setParameters(parameters); - registry.setPassword(password); - registry.setPort(port); - registry.setProtocol(protocol); - registry.setRegister(register); - registry.setServer(server); - registry.setSession(session); - registry.setSimplified(simplified); - registry.setSubscribe(subscribe); - registry.setTimeout(timeout); - registry.setTransporter(transporter); - registry.setUsername(username); - registry.setVersion(version); - registry.setWait(wait); - - return registry; - } - - @Override - protected RegistryBuilder getThis() { - return this; - } -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ServiceBuilder.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ServiceBuilder.java deleted file mode 100644 index 6628d3aba59..00000000000 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/builders/ServiceBuilder.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import java.util.ArrayList; -import java.util.List; - -import org.apache.dubbo.config.MethodConfig; -import org.apache.dubbo.config.ProviderConfig; -import org.apache.dubbo.config.ServiceConfig; - -/** - * This is a builder for build {@link ServiceConfig}. - * - * @since 2.7 - */ -public class ServiceBuilder extends AbstractServiceBuilder> { - /** - * The interface name of the exported service - */ - private String interfaceName; - - /** - * The interface class of the exported service - */ - private Class interfaceClass; - - /** - * The reference of the interface implementation - */ - private U ref; - - /** - * The service name - */ - private String path; - - /** - * The method configuration - */ - private List methods; - - /** - * The provider configuration - */ - private ProviderConfig provider; - - /** - * The providerIds - */ - private String providerIds; - /** - * whether it is a GenericService - */ - private String generic; - - public ServiceBuilder interfaceName(String interfaceName) { - this.interfaceName = interfaceName; - return getThis(); - } - - public ServiceBuilder interfaceClass(Class interfaceClass) { - this.interfaceClass = interfaceClass; - return getThis(); - } - - public ServiceBuilder ref(U ref) { - this.ref = ref; - return getThis(); - } - - public ServiceBuilder path(String path) { - this.path = path; - return getThis(); - } - - public ServiceBuilder addMethod(MethodConfig method) { - if (this.methods == null) { - this.methods = new ArrayList<>(); - } - this.methods.add(method); - return getThis(); - } - - public ServiceBuilder addMethods(List methods) { - if (this.methods == null) { - this.methods = new ArrayList<>(); - } - this.methods.addAll(methods); - return getThis(); - } - - public ServiceBuilder provider(ProviderConfig provider) { - this.provider = provider; - return getThis(); - } - - public ServiceBuilder providerIds(String providerIds) { - this.providerIds = providerIds; - return getThis(); - } - - public ServiceBuilder generic(String generic) { - this.generic = generic; - return getThis(); - } - - @Override - public ServiceBuilder mock(String mock) { - throw new IllegalArgumentException("mock doesn't support on provider side"); - } - - @Override - public ServiceBuilder mock(Boolean mock) { - throw new IllegalArgumentException("mock doesn't support on provider side"); - } - - public ServiceConfig build() { - ServiceConfig serviceConfig = new ServiceConfig<>(); - super.build(serviceConfig); - - serviceConfig.setInterface(interfaceName); - serviceConfig.setInterface(interfaceClass); - serviceConfig.setRef(ref); - serviceConfig.setPath(path); - serviceConfig.setMethods(methods); - serviceConfig.setProvider(provider); - serviceConfig.setProviderIds(providerIds); - serviceConfig.setGeneric(generic); - - return serviceConfig; - } - - @Override - protected ServiceBuilder getThis() { - return this; - } -} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/utils/ReferenceConfigCache.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/utils/ReferenceConfigCache.java index 09f12dadeca..5ca96987242 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/utils/ReferenceConfigCache.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/utils/ReferenceConfigCache.java @@ -39,25 +39,28 @@ public class ReferenceConfigCache { *

    * key example: group1/org.apache.dubbo.foo.FooService:1.0.0. */ - public static final KeyGenerator DEFAULT_KEY_GENERATOR = referenceConfig -> { - String iName = referenceConfig.getInterface(); - if (StringUtils.isBlank(iName)) { - Class clazz = referenceConfig.getInterfaceClass(); - iName = clazz.getName(); + public static final KeyGenerator DEFAULT_KEY_GENERATOR = new KeyGenerator() { + @Override + public String generateKey(ReferenceConfig referenceConfig) { + String iName = referenceConfig.getInterface(); + if (StringUtils.isBlank(iName)) { + Class clazz = referenceConfig.getInterfaceClass(); + iName = clazz.getName(); + } + if (StringUtils.isBlank(iName)) { + throw new IllegalArgumentException("No interface info in ReferenceConfig" + referenceConfig); + } + + StringBuilder ret = new StringBuilder(); + if (!StringUtils.isBlank(referenceConfig.getGroup())) { + ret.append(referenceConfig.getGroup()).append("/"); + } + ret.append(iName); + if (!StringUtils.isBlank(referenceConfig.getVersion())) { + ret.append(":").append(referenceConfig.getVersion()); + } + return ret.toString(); } - if (StringUtils.isBlank(iName)) { - throw new IllegalArgumentException("No interface info in ReferenceConfig" + referenceConfig); - } - - StringBuilder ret = new StringBuilder(); - if (!StringUtils.isBlank(referenceConfig.getGroup())) { - ret.append(referenceConfig.getGroup()).append("/"); - } - ret.append(iName); - if (!StringUtils.isBlank(referenceConfig.getVersion())) { - ret.append(":").append(referenceConfig.getVersion()); - } - return ret.toString(); }; static final ConcurrentMap cacheHolder = new ConcurrentHashMap(); private final String name; @@ -112,22 +115,6 @@ public T get(ReferenceConfig referenceConfig) { return (T) config.get(); } - /** - * Fetch cache with the specified key. The key is decided by KeyGenerator passed-in. If the default KeyGenerator is - * used, then the key is in the format of group/interfaceClass:version - * - * @param key cache key - * @param type object class - * @param object type - * @return object from the cached ReferenceConfig - * @see KeyGenerator#generateKey(ReferenceConfig) - */ - @SuppressWarnings("unchecked") - public T get(String key, Class type) { - ReferenceConfig config = cache.get(key); - return (config != null) ? (T) config.get() : null; - } - void destroyKey(String key) { ReferenceConfig config = cache.remove(key); if (config == null) { diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractConfigTest.java index 2d40cc369fd..0310f04569c 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractConfigTest.java @@ -21,9 +21,10 @@ import org.apache.dubbo.config.api.Greeting; import org.apache.dubbo.config.support.Parameter; +import junit.framework.TestCase; import org.hamcrest.Matchers; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; @@ -34,9 +35,7 @@ import java.util.Map; import java.util.Properties; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.assertThat; public class AbstractConfigTest { @@ -55,15 +54,15 @@ public void testAppendProperties1() throws Exception { System.setProperty("dubbo.properties.bool", "true"); PropertiesConfig config = new PropertiesConfig(); AbstractConfig.appendProperties(config); - Assertions.assertEquals(1, config.getI()); - Assertions.assertEquals('c', config.getC()); - Assertions.assertEquals((byte) 0x02, config.getB()); - Assertions.assertEquals(3d, config.getD()); - Assertions.assertEquals(4f, config.getF()); - Assertions.assertEquals(5L, config.getL()); - Assertions.assertEquals(6, config.getS()); - Assertions.assertEquals("dubbo", config.getStr()); - Assertions.assertTrue(config.isBool()); + TestCase.assertEquals(1, config.getI()); + TestCase.assertEquals('c', config.getC()); + TestCase.assertEquals((byte) 0x02, config.getB()); + TestCase.assertEquals(3d, config.getD()); + TestCase.assertEquals(4f, config.getF()); + TestCase.assertEquals(5L, config.getL()); + TestCase.assertEquals(6, config.getS()); + TestCase.assertEquals("dubbo", config.getStr()); + TestCase.assertTrue(config.isBool()); } finally { System.clearProperty("dubbo.properties.i"); System.clearProperty("dubbo.properties.c"); @@ -83,7 +82,7 @@ public void testAppendProperties2() throws Exception { System.setProperty("dubbo.properties.two.i", "2"); PropertiesConfig config = new PropertiesConfig("two"); AbstractConfig.appendProperties(config); - Assertions.assertEquals(2, config.getI()); + TestCase.assertEquals(2, config.getI()); } finally { System.clearProperty("dubbo.properties.two.i"); } @@ -97,7 +96,7 @@ public void testAppendProperties3() throws Exception { ConfigUtils.setProperties(p); PropertiesConfig config = new PropertiesConfig(); AbstractConfig.appendProperties(config); - Assertions.assertEquals("dubbo", config.getStr()); + TestCase.assertEquals("dubbo", config.getStr()); } finally { System.clearProperty(Constants.DUBBO_PROPERTIES_KEY); ConfigUtils.setProperties(null); @@ -110,99 +109,93 @@ public void testAppendParameters1() throws Exception { parameters.put("default.num", "one"); parameters.put("num", "ONE"); AbstractConfig.appendParameters(parameters, new ParameterConfig(1, "hello/world", 30, "password"), "prefix"); - Assertions.assertEquals("one", parameters.get("prefix.key.1")); - Assertions.assertEquals("two", parameters.get("prefix.key.2")); - Assertions.assertEquals("ONE,one,1", parameters.get("prefix.num")); - Assertions.assertEquals("hello%2Fworld", parameters.get("prefix.naming")); - Assertions.assertEquals("30", parameters.get("prefix.age")); - Assertions.assertFalse(parameters.containsKey("prefix.key-2")); - Assertions.assertFalse(parameters.containsKey("prefix.secret")); + TestCase.assertEquals("one", parameters.get("prefix.key.1")); + TestCase.assertEquals("two", parameters.get("prefix.key.2")); + TestCase.assertEquals("ONE,one,1", parameters.get("prefix.num")); + TestCase.assertEquals("hello%2Fworld", parameters.get("prefix.naming")); + TestCase.assertEquals("30", parameters.get("prefix.age")); + TestCase.assertFalse(parameters.containsKey("prefix.key-2")); + TestCase.assertFalse(parameters.containsKey("prefix.secret")); } - @Test + @Test(expected = IllegalStateException.class) public void testAppendParameters2() throws Exception { - Assertions.assertThrows(IllegalStateException.class, () -> { - Map parameters = new HashMap(); - AbstractConfig.appendParameters(parameters, new ParameterConfig()); - }); + Map parameters = new HashMap(); + AbstractConfig.appendParameters(parameters, new ParameterConfig()); } @Test public void testAppendParameters3() throws Exception { Map parameters = new HashMap(); AbstractConfig.appendParameters(parameters, null); - assertTrue(parameters.isEmpty()); + TestCase.assertTrue(parameters.isEmpty()); } @Test public void testAppendParameters4() throws Exception { Map parameters = new HashMap(); AbstractConfig.appendParameters(parameters, new ParameterConfig(1, "hello/world", 30, "password")); - Assertions.assertEquals("one", parameters.get("key.1")); - Assertions.assertEquals("two", parameters.get("key.2")); - Assertions.assertEquals("1", parameters.get("num")); - Assertions.assertEquals("hello%2Fworld", parameters.get("naming")); - Assertions.assertEquals("30", parameters.get("age")); + TestCase.assertEquals("one", parameters.get("key.1")); + TestCase.assertEquals("two", parameters.get("key.2")); + TestCase.assertEquals("1", parameters.get("num")); + TestCase.assertEquals("hello%2Fworld", parameters.get("naming")); + TestCase.assertEquals("30", parameters.get("age")); } @Test public void testAppendAttributes1() throws Exception { Map parameters = new HashMap(); AbstractConfig.appendAttributes(parameters, new AttributeConfig('l', true, (byte) 0x01), "prefix"); - Assertions.assertEquals('l', parameters.get("prefix.let")); - Assertions.assertEquals(true, parameters.get("prefix.activate")); - Assertions.assertFalse(parameters.containsKey("prefix.flag")); + TestCase.assertEquals('l', parameters.get("prefix.let")); + TestCase.assertEquals(true, parameters.get("prefix.activate")); + TestCase.assertFalse(parameters.containsKey("prefix.flag")); } @Test public void testAppendAttributes2() throws Exception { Map parameters = new HashMap(); AbstractConfig.appendAttributes(parameters, new AttributeConfig('l', true, (byte) 0x01)); - Assertions.assertEquals('l', parameters.get("let")); - Assertions.assertEquals(true, parameters.get("activate")); - Assertions.assertFalse(parameters.containsKey("flag")); + TestCase.assertEquals('l', parameters.get("let")); + TestCase.assertEquals(true, parameters.get("activate")); + TestCase.assertFalse(parameters.containsKey("flag")); } - @Test + @Test(expected = IllegalStateException.class) public void checkExtension() throws Exception { - Assertions.assertThrows(IllegalStateException.class, () -> AbstractConfig.checkExtension(Greeting.class, "hello", "world")); + AbstractConfig.checkExtension(Greeting.class, "hello", "world"); } - @Test + @Test(expected = IllegalStateException.class) public void checkMultiExtension1() throws Exception { - Assertions.assertThrows(IllegalStateException.class, () -> AbstractConfig.checkMultiExtension(Greeting.class, "hello", "default,world")); + AbstractConfig.checkMultiExtension(Greeting.class, "hello", "default,world"); } - @Test + @Test(expected = IllegalStateException.class) public void checkMultiExtension2() throws Exception { - Assertions.assertThrows(IllegalStateException.class, () -> AbstractConfig.checkMultiExtension(Greeting.class, "hello", "default,-world")); + AbstractConfig.checkMultiExtension(Greeting.class, "hello", "default,-world"); } - @Test + @Test(expected = IllegalStateException.class) public void checkLength() throws Exception { - Assertions.assertThrows(IllegalStateException.class, () -> { - StringBuilder builder = new StringBuilder(); - for (int i = 0; i <= 200; i++) { - builder.append("a"); - } - AbstractConfig.checkLength("hello", builder.toString()); - }); + StringBuilder builder = new StringBuilder(); + for (int i = 0; i <= 200; i++) { + builder.append("a"); + } + AbstractConfig.checkLength("hello", builder.toString()); } - @Test + @Test(expected = IllegalStateException.class) public void checkPathLength() throws Exception { - Assertions.assertThrows(IllegalStateException.class, () -> { - StringBuilder builder = new StringBuilder(); - for (int i = 0; i <= 200; i++) { - builder.append("a"); - } - AbstractConfig.checkPathLength("hello", builder.toString()); - }); + StringBuilder builder = new StringBuilder(); + for (int i = 0; i <= 200; i++) { + builder.append("a"); + } + AbstractConfig.checkPathLength("hello", builder.toString()); } - @Test + @Test(expected = IllegalStateException.class) public void checkName() throws Exception { - Assertions.assertThrows(IllegalStateException.class, () -> AbstractConfig.checkName("hello", "world%")); + AbstractConfig.checkName("hello", "world%"); } @Test @@ -211,7 +204,7 @@ public void checkNameHasSymbol() throws Exception { AbstractConfig.checkNameHasSymbol("hello", ":*,/ -0123\tabcdABCD"); AbstractConfig.checkNameHasSymbol("mock", "force:return world"); } catch (Exception e) { - fail("the value should be legal."); + TestCase.fail("the value should be legal."); } } @@ -220,7 +213,7 @@ public void checkKey() throws Exception { try { AbstractConfig.checkKey("hello", "*,-0123abcdABCD"); } catch (Exception e) { - fail("the value should be legal."); + TestCase.fail("the value should be legal."); } } @@ -229,7 +222,7 @@ public void checkMultiName() throws Exception { try { AbstractConfig.checkMultiName("hello", ",-._0123abcdABCD"); } catch (Exception e) { - fail("the value should be legal."); + TestCase.fail("the value should be legal."); } } @@ -238,7 +231,7 @@ public void checkPathName() throws Exception { try { AbstractConfig.checkPathName("hello", "/-$._0123abcdABCD"); } catch (Exception e) { - fail("the value should be legal."); + TestCase.fail("the value should be legal."); } } @@ -247,12 +240,12 @@ public void checkMethodName() throws Exception { try { AbstractConfig.checkMethodName("hello", "abcdABCD0123abcd"); } catch (Exception e) { - fail("the value should be legal."); + TestCase.fail("the value should be legal."); } try { AbstractConfig.checkMethodName("hello", "0a"); - fail("the value should be illegal."); + TestCase.fail("the value should be illegal."); } catch (Exception e) { // ignore } @@ -264,7 +257,7 @@ public void checkParameterName() throws Exception { try { AbstractConfig.checkParameterName(parameters); } catch (Exception e) { - fail("the value should be legal."); + TestCase.fail("the value should be legal."); } } @@ -275,12 +268,12 @@ public void appendAnnotation() throws Exception { Config config = getClass().getMethod("appendAnnotation").getAnnotation(Config.class); AnnotationConfig annotationConfig = new AnnotationConfig(); annotationConfig.appendAnnotation(Config.class, config); - Assertions.assertSame(Greeting.class, annotationConfig.getInterface()); - Assertions.assertEquals("f1, f2", annotationConfig.getFilter()); - Assertions.assertEquals("l1, l2", annotationConfig.getListener()); - Assertions.assertEquals(2, annotationConfig.getParameters().size()); - Assertions.assertEquals("v1", annotationConfig.getParameters().get("k1")); - Assertions.assertEquals("v2", annotationConfig.getParameters().get("k2")); + TestCase.assertSame(Greeting.class, annotationConfig.getInterface()); + TestCase.assertEquals("f1, f2", annotationConfig.getFilter()); + TestCase.assertEquals("l1, l2", annotationConfig.getListener()); + TestCase.assertEquals(2, annotationConfig.getParameters().size()); + TestCase.assertEquals("v1", annotationConfig.getParameters().get("k1")); + TestCase.assertEquals("v2", annotationConfig.getParameters().get("k2")); assertThat(annotationConfig.toString(), Matchers.containsString("filter=\"f1, f2\" ")); assertThat(annotationConfig.toString(), Matchers.containsString("listener=\"l1, l2\" ")); } @@ -313,11 +306,11 @@ public void testRefreshAll() { // Load configuration from system properties -> externalConfiguration -> RegistryConfig -> dubbo.properties overrideConfig.refresh(); - Assertions.assertEquals("system://127.0.0.1:2181", overrideConfig.getAddress()); - Assertions.assertEquals("system", overrideConfig.getProtocol()); - Assertions.assertEquals("override-config://", overrideConfig.getEscape()); - Assertions.assertEquals("external", overrideConfig.getKey()); - Assertions.assertEquals("system", overrideConfig.getUseKeyAsProperty()); + Assert.assertEquals("system://127.0.0.1:2181", overrideConfig.getAddress()); + Assert.assertEquals("system", overrideConfig.getProtocol()); + Assert.assertEquals("override-config://", overrideConfig.getEscape()); + Assert.assertEquals("external", overrideConfig.getKey()); + Assert.assertEquals("system", overrideConfig.getUseKeyAsProperty()); } finally { System.clearProperty("dubbo.override.address"); System.clearProperty("dubbo.override.protocol"); @@ -342,10 +335,10 @@ public void testRefreshSystem() { overrideConfig.refresh(); - Assertions.assertEquals("system://127.0.0.1:2181", overrideConfig.getAddress()); - Assertions.assertEquals("system", overrideConfig.getProtocol()); - Assertions.assertEquals("override-config://", overrideConfig.getEscape()); - Assertions.assertEquals("system", overrideConfig.getKey()); + Assert.assertEquals("system://127.0.0.1:2181", overrideConfig.getAddress()); + Assert.assertEquals("system", overrideConfig.getProtocol()); + Assert.assertEquals("override-config://", overrideConfig.getEscape()); + Assert.assertEquals("system", overrideConfig.getKey()); } finally { System.clearProperty("dubbo.override.address"); System.clearProperty("dubbo.override.protocol"); @@ -369,10 +362,10 @@ public void testRefreshProperties() throws Exception{ overrideConfig.refresh(); - Assertions.assertEquals("override-config://127.0.0.1:2181", overrideConfig.getAddress()); - Assertions.assertEquals("override-config", overrideConfig.getProtocol()); - Assertions.assertEquals("override-config://", overrideConfig.getEscape()); - //Assertions.assertEquals("properties", overrideConfig.getUseKeyAsProperty()); + Assert.assertEquals("override-config://127.0.0.1:2181", overrideConfig.getAddress()); + Assert.assertEquals("override-config", overrideConfig.getProtocol()); + Assert.assertEquals("override-config://", overrideConfig.getEscape()); + Assert.assertEquals("properties", overrideConfig.getUseKeyAsProperty()); } finally { Environment.getInstance().clearExternalConfigs(); ConfigUtils.setProperties(null); @@ -402,12 +395,12 @@ public void testRefreshExternal() { overrideConfig.refresh(); - Assertions.assertEquals("external://127.0.0.1:2181", overrideConfig.getAddress()); - Assertions.assertEquals("external", overrideConfig.getProtocol()); - Assertions.assertEquals("external://", overrideConfig.getEscape()); - Assertions.assertEquals("external", overrideConfig.getExclude()); - Assertions.assertEquals("external", overrideConfig.getKey()); - Assertions.assertEquals("external", overrideConfig.getUseKeyAsProperty()); + Assert.assertEquals("external://127.0.0.1:2181", overrideConfig.getAddress()); + Assert.assertEquals("external", overrideConfig.getProtocol()); + Assert.assertEquals("external://", overrideConfig.getEscape()); + Assert.assertEquals("external", overrideConfig.getExclude()); + Assert.assertEquals("external", overrideConfig.getKey()); + Assert.assertEquals("external", overrideConfig.getUseKeyAsProperty()); } finally { Environment.getInstance().clearExternalConfigs(); } @@ -439,11 +432,11 @@ public void testRefreshById() { // Load configuration from system properties -> externalConfiguration -> RegistryConfig -> dubbo.properties overrideConfig.refresh(); - Assertions.assertEquals("external-override-id://127.0.0.1:2181", overrideConfig.getAddress()); - Assertions.assertEquals("override-config", overrideConfig.getProtocol()); - Assertions.assertEquals("override-config://", overrideConfig.getEscape()); - Assertions.assertEquals("external", overrideConfig.getKey()); - Assertions.assertEquals("external", overrideConfig.getUseKeyAsProperty()); + Assert.assertEquals("external-override-id://127.0.0.1:2181", overrideConfig.getAddress()); + Assert.assertEquals("override-config", overrideConfig.getProtocol()); + Assert.assertEquals("override-config://", overrideConfig.getEscape()); + Assert.assertEquals("external", overrideConfig.getKey()); + Assert.assertEquals("external", overrideConfig.getUseKeyAsProperty()); } finally { Environment.getInstance().clearExternalConfigs(); } @@ -459,18 +452,12 @@ public void tetMetaData() { overrideConfig.setExclude("override-config"); Map metaData = overrideConfig.getMetaData(); - Assertions.assertEquals("override-config://127.0.0.1:2181", metaData.get("address")); - Assertions.assertEquals("override-config", metaData.get("protocol")); - Assertions.assertEquals("override-config://", metaData.get("escape")); - Assertions.assertEquals("override-config", metaData.get("exclude")); - Assertions.assertNull(metaData.get("key")); - Assertions.assertNull(metaData.get("key2")); - } - - @Retention(RetentionPolicy.RUNTIME) - @Target({ElementType.ANNOTATION_TYPE}) - public @interface ConfigField { - String value() default ""; + Assert.assertEquals("override-config://127.0.0.1:2181", metaData.get("address")); + Assert.assertEquals("override-config", metaData.get("protocol")); + Assert.assertEquals("override-config://", metaData.get("escape")); + Assert.assertEquals("override-config", metaData.get("exclude")); + Assert.assertNull(metaData.get("key")); + Assert.assertNull(metaData.get("key2")); } @Retention(RetentionPolicy.RUNTIME) @@ -485,10 +472,6 @@ public void tetMetaData() { String[] listener() default {}; String[] parameters() default {}; - - ConfigField[] configFields() default {}; - - ConfigField configField() default @ConfigField; } private static class OverrideConfig extends AbstractInterfaceConfig { @@ -745,7 +728,6 @@ private static class AnnotationConfig extends AbstractConfig { private String filter; private String listener; private Map parameters; - private String[] configFields; public Class getInterface() { return interfaceClass; @@ -778,13 +760,5 @@ public Map getParameters() { public void setParameters(Map parameters) { this.parameters = parameters; } - - public String[] getConfigFields() { - return configFields; - } - - public void setConfigFields(String[] configFields) { - this.configFields = configFields; - } } } diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractInterfaceConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractInterfaceConfigTest.java index 6f30581b875..1a89fddc0b6 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractInterfaceConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractInterfaceConfigTest.java @@ -29,64 +29,64 @@ import org.apache.dubbo.monitor.MonitorService; import org.apache.dubbo.registry.RegistryService; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; +import junit.framework.TestCase; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.nio.file.Path; import java.util.Collections; import java.util.List; import java.util.Properties; public class AbstractInterfaceConfigTest { + @ClassRule + public static TemporaryFolder tempDir = new TemporaryFolder(); private static File dubboProperties; - @BeforeAll - public static void setUp(@TempDir Path folder) { - dubboProperties = folder.resolve(Constants.DUBBO_PROPERTIES_KEY).toFile(); + @BeforeClass + public static void setUp() throws Exception { + dubboProperties = tempDir.newFile(Constants.DUBBO_PROPERTIES_KEY); System.setProperty(Constants.DUBBO_PROPERTIES_KEY, dubboProperties.getAbsolutePath()); } - @AfterAll - public static void tearDown() { + @AfterClass + public static void tearDown() throws Exception { System.clearProperty(Constants.DUBBO_PROPERTIES_KEY); } - @AfterEach + @After public void tearMethodAfterEachUT() { ConfigManager.getInstance().clear(); } @Test - public void testCheckRegistry1() { + public void testCheckRegistry1() throws Exception { System.setProperty("dubbo.registry.address", "addr1|addr2"); try { InterfaceConfig interfaceConfig = new InterfaceConfig(); interfaceConfig.checkRegistry(); - Assertions.assertEquals(2, interfaceConfig.getRegistries().size()); + TestCase.assertEquals(2, interfaceConfig.getRegistries().size()); } finally { System.clearProperty("dubbo.registry.address"); } } - @Test - public void testCheckRegistry2() { - Assertions.assertThrows(IllegalStateException.class, () -> { - InterfaceConfig interfaceConfig = new InterfaceConfig(); - interfaceConfig.checkRegistry(); - }); + @Test(expected = IllegalStateException.class) + public void testCheckRegistry2() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.checkRegistry(); } @Test - public void checkApplication1() { + public void checkApplication1() throws Exception { try { ConfigUtils.setProperties(null); System.clearProperty(Constants.SHUTDOWN_WAIT_KEY); @@ -97,8 +97,8 @@ public void checkApplication1() { InterfaceConfig interfaceConfig = new InterfaceConfig(); interfaceConfig.checkApplication(); ApplicationConfig appConfig = interfaceConfig.getApplication(); - Assertions.assertEquals("demo", appConfig.getName()); - Assertions.assertEquals("100", System.getProperty(Constants.SHUTDOWN_WAIT_KEY)); + TestCase.assertEquals("demo", appConfig.getName()); + TestCase.assertEquals("100", System.getProperty(Constants.SHUTDOWN_WAIT_KEY)); System.clearProperty(Constants.SHUTDOWN_WAIT_KEY); ConfigUtils.setProperties(null); @@ -106,7 +106,7 @@ public void checkApplication1() { System.setProperty("dubbo.application.name", "demo"); interfaceConfig = new InterfaceConfig(); interfaceConfig.checkApplication(); - Assertions.assertEquals("1000", System.getProperty(Constants.SHUTDOWN_WAIT_SECONDS_KEY)); + TestCase.assertEquals("1000", System.getProperty(Constants.SHUTDOWN_WAIT_SECONDS_KEY)); } finally { ConfigUtils.setProperties(null); System.clearProperty("dubbo.application.name"); @@ -115,314 +115,290 @@ public void checkApplication1() { } } - @Test - public void checkApplication2() { - Assertions.assertThrows(IllegalStateException.class, () -> { - InterfaceConfig interfaceConfig = new InterfaceConfig(); - interfaceConfig.checkApplication(); - }); + @Test(expected = IllegalStateException.class) + public void checkApplication2() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.checkApplication(); } @Test - public void testLoadRegistries() { + public void testLoadRegistries() throws Exception { System.setProperty("dubbo.registry.address", "addr1"); InterfaceConfig interfaceConfig = new InterfaceConfig(); // FIXME: now we need to check first, then load interfaceConfig.checkRegistry(); List urls = interfaceConfig.loadRegistries(true); - Assertions.assertEquals(1, urls.size()); + TestCase.assertEquals(1, urls.size()); URL url = urls.get(0); - Assertions.assertEquals("registry", url.getProtocol()); - Assertions.assertEquals("addr1:9090", url.getAddress()); - Assertions.assertEquals(RegistryService.class.getName(), url.getPath()); - Assertions.assertTrue(url.getParameters().containsKey("timestamp")); - Assertions.assertTrue(url.getParameters().containsKey("pid")); - Assertions.assertTrue(url.getParameters().containsKey("registry")); - Assertions.assertTrue(url.getParameters().containsKey("dubbo")); + TestCase.assertEquals("registry", url.getProtocol()); + TestCase.assertEquals("addr1:9090", url.getAddress()); + TestCase.assertEquals(RegistryService.class.getName(), url.getPath()); + TestCase.assertTrue(url.getParameters().containsKey("timestamp")); + TestCase.assertTrue(url.getParameters().containsKey("pid")); + TestCase.assertTrue(url.getParameters().containsKey("registry")); + TestCase.assertTrue(url.getParameters().containsKey("dubbo")); } @Test - public void testLoadMonitor() { + public void testLoadMonitor() throws Exception { System.setProperty("dubbo.monitor.address", "monitor-addr:12080"); System.setProperty("dubbo.monitor.protocol", "monitor"); InterfaceConfig interfaceConfig = new InterfaceConfig(); URL url = interfaceConfig.loadMonitor(new URL("dubbo", "addr1", 9090)); - Assertions.assertEquals("monitor-addr:12080", url.getAddress()); - Assertions.assertEquals(MonitorService.class.getName(), url.getParameter("interface")); - Assertions.assertNotNull(url.getParameter("dubbo")); - Assertions.assertNotNull(url.getParameter("pid")); - Assertions.assertNotNull(url.getParameter("timestamp")); + TestCase.assertEquals("monitor-addr:12080", url.getAddress()); + TestCase.assertEquals(MonitorService.class.getName(), url.getParameter("interface")); + TestCase.assertNotNull(url.getParameter("dubbo")); + TestCase.assertNotNull(url.getParameter("pid")); + TestCase.assertNotNull(url.getParameter("timestamp")); } - @Test - public void checkInterfaceAndMethods1() { - Assertions.assertThrows(IllegalStateException.class, () -> { - InterfaceConfig interfaceConfig = new InterfaceConfig(); - interfaceConfig.checkInterfaceAndMethods(null, null); - }); + @Test(expected = IllegalStateException.class) + public void checkInterfaceAndMethods1() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.checkInterfaceAndMethods(null, null); } - @Test - public void checkInterfaceAndMethods2() { - Assertions.assertThrows(IllegalStateException.class, () -> { - InterfaceConfig interfaceConfig = new InterfaceConfig(); - interfaceConfig.checkInterfaceAndMethods(AbstractInterfaceConfigTest.class, null); - }); + @Test(expected = IllegalStateException.class) + public void checkInterfaceAndMethods2() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.checkInterfaceAndMethods(AbstractInterfaceConfigTest.class, null); } - @Test - public void checkInterfaceAndMethod3() { - Assertions.assertThrows(IllegalStateException.class, () -> { - MethodConfig methodConfig = new MethodConfig(); - InterfaceConfig interfaceConfig = new InterfaceConfig(); - interfaceConfig.checkInterfaceAndMethods(Greeting.class, Collections.singletonList(methodConfig)); - }); + @Test(expected = IllegalStateException.class) + public void checkInterfaceAndMethod3() throws Exception { + MethodConfig methodConfig = new MethodConfig(); + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.checkInterfaceAndMethods(Greeting.class, Collections.singletonList(methodConfig)); } - @Test - public void checkInterfaceAndMethod4() { - Assertions.assertThrows(IllegalStateException.class, () -> { - MethodConfig methodConfig = new MethodConfig(); - methodConfig.setName("nihao"); - InterfaceConfig interfaceConfig = new InterfaceConfig(); - interfaceConfig.checkInterfaceAndMethods(Greeting.class, Collections.singletonList(methodConfig)); - }); + @Test(expected = IllegalStateException.class) + public void checkInterfaceAndMethod4() throws Exception { + MethodConfig methodConfig = new MethodConfig(); + methodConfig.setName("nihao"); + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.checkInterfaceAndMethods(Greeting.class, Collections.singletonList(methodConfig)); } @Test - public void checkInterfaceAndMethod5() { + public void checkInterfaceAndMethod5() throws Exception { MethodConfig methodConfig = new MethodConfig(); methodConfig.setName("hello"); InterfaceConfig interfaceConfig = new InterfaceConfig(); interfaceConfig.checkInterfaceAndMethods(Greeting.class, Collections.singletonList(methodConfig)); } - @Test - public void checkStubAndMock1() { - Assertions.assertThrows(IllegalStateException.class, () -> { - InterfaceConfig interfaceConfig = new InterfaceConfig(); - interfaceConfig.setLocal(GreetingLocal1.class.getName()); - interfaceConfig.checkStubAndLocal(Greeting.class); - interfaceConfig.checkMock(Greeting.class); - }); + @Test(expected = IllegalStateException.class) + public void checkStubAndMock1() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setLocal(GreetingLocal1.class.getName()); + interfaceConfig.checkStubAndLocal(Greeting.class); + interfaceConfig.checkMock(Greeting.class); } - @Test - public void checkStubAndMock2() { - Assertions.assertThrows(IllegalStateException.class, () -> { - InterfaceConfig interfaceConfig = new InterfaceConfig(); - interfaceConfig.setLocal(GreetingLocal2.class.getName()); - interfaceConfig.checkStubAndLocal(Greeting.class); - interfaceConfig.checkMock(Greeting.class); - }); + @Test(expected = IllegalStateException.class) + public void checkStubAndMock2() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setLocal(GreetingLocal2.class.getName()); + interfaceConfig.checkStubAndLocal(Greeting.class); + interfaceConfig.checkMock(Greeting.class); } @Test - public void checkStubAndMock3() { + public void checkStubAndMock3() throws Exception { InterfaceConfig interfaceConfig = new InterfaceConfig(); interfaceConfig.setLocal(GreetingLocal3.class.getName()); interfaceConfig.checkStubAndLocal(Greeting.class); interfaceConfig.checkMock(Greeting.class); } - @Test - public void checkStubAndMock4() { - Assertions.assertThrows(IllegalStateException.class, () -> { - InterfaceConfig interfaceConfig = new InterfaceConfig(); - interfaceConfig.setStub(GreetingLocal1.class.getName()); - interfaceConfig.checkStubAndLocal(Greeting.class); - interfaceConfig.checkMock(Greeting.class); - }); + @Test(expected = IllegalStateException.class) + public void checkStubAndMock4() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setStub(GreetingLocal1.class.getName()); + interfaceConfig.checkStubAndLocal(Greeting.class); + interfaceConfig.checkMock(Greeting.class); } - @Test - public void checkStubAndMock5() { - Assertions.assertThrows(IllegalStateException.class, () -> { - InterfaceConfig interfaceConfig = new InterfaceConfig(); - interfaceConfig.setStub(GreetingLocal2.class.getName()); - interfaceConfig.checkStubAndLocal(Greeting.class); - interfaceConfig.checkMock(Greeting.class); - }); + @Test(expected = IllegalStateException.class) + public void checkStubAndMock5() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setStub(GreetingLocal2.class.getName()); + interfaceConfig.checkStubAndLocal(Greeting.class); + interfaceConfig.checkMock(Greeting.class); } @Test - public void checkStubAndMock6() { + public void checkStubAndMock6() throws Exception { InterfaceConfig interfaceConfig = new InterfaceConfig(); interfaceConfig.setStub(GreetingLocal3.class.getName()); interfaceConfig.checkStubAndLocal(Greeting.class); interfaceConfig.checkMock(Greeting.class); } - @Test - public void checkStubAndMock7() { - Assertions.assertThrows(IllegalStateException.class, () -> { - InterfaceConfig interfaceConfig = new InterfaceConfig(); - interfaceConfig.setMock("return {a, b}"); - interfaceConfig.checkStubAndLocal(Greeting.class); - interfaceConfig.checkMock(Greeting.class); - }); + @Test(expected = IllegalStateException.class) + public void checkStubAndMock7() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setMock("return {a, b}"); + interfaceConfig.checkStubAndLocal(Greeting.class); + interfaceConfig.checkMock(Greeting.class); } - @Test - public void checkStubAndMock8() { - Assertions.assertThrows(IllegalStateException.class, () -> { - InterfaceConfig interfaceConfig = new InterfaceConfig(); - interfaceConfig.setMock(GreetingMock1.class.getName()); - interfaceConfig.checkStubAndLocal(Greeting.class); - interfaceConfig.checkMock(Greeting.class); - }); + @Test(expected = IllegalStateException.class) + public void checkStubAndMock8() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setMock(GreetingMock1.class.getName()); + interfaceConfig.checkStubAndLocal(Greeting.class); + interfaceConfig.checkMock(Greeting.class); } - @Test - public void checkStubAndMock9() { - Assertions.assertThrows(IllegalStateException.class, () -> { - InterfaceConfig interfaceConfig = new InterfaceConfig(); - interfaceConfig.setMock(GreetingMock2.class.getName()); - interfaceConfig.checkStubAndLocal(Greeting.class); - interfaceConfig.checkMock(Greeting.class); - }); + @Test(expected = IllegalStateException.class) + public void checkStubAndMock9() throws Exception { + InterfaceConfig interfaceConfig = new InterfaceConfig(); + interfaceConfig.setMock(GreetingMock2.class.getName()); + interfaceConfig.checkStubAndLocal(Greeting.class); + interfaceConfig.checkMock(Greeting.class); } @Test - public void testLocal() { + public void testLocal() throws Exception { InterfaceConfig interfaceConfig = new InterfaceConfig(); interfaceConfig.setLocal((Boolean) null); - Assertions.assertNull(interfaceConfig.getLocal()); + TestCase.assertNull(interfaceConfig.getLocal()); interfaceConfig.setLocal(true); - Assertions.assertEquals("true", interfaceConfig.getLocal()); + TestCase.assertEquals("true", interfaceConfig.getLocal()); interfaceConfig.setLocal("GreetingMock"); - Assertions.assertEquals("GreetingMock", interfaceConfig.getLocal()); + TestCase.assertEquals("GreetingMock", interfaceConfig.getLocal()); } @Test - public void testStub() { + public void testStub() throws Exception { InterfaceConfig interfaceConfig = new InterfaceConfig(); interfaceConfig.setStub((Boolean) null); - Assertions.assertNull(interfaceConfig.getStub()); + TestCase.assertNull(interfaceConfig.getStub()); interfaceConfig.setStub(true); - Assertions.assertEquals("true", interfaceConfig.getStub()); + TestCase.assertEquals("true", interfaceConfig.getStub()); interfaceConfig.setStub("GreetingMock"); - Assertions.assertEquals("GreetingMock", interfaceConfig.getStub()); + TestCase.assertEquals("GreetingMock", interfaceConfig.getStub()); } @Test - public void testCluster() { + public void testCluster() throws Exception { InterfaceConfig interfaceConfig = new InterfaceConfig(); interfaceConfig.setCluster("mockcluster"); - Assertions.assertEquals("mockcluster", interfaceConfig.getCluster()); + TestCase.assertEquals("mockcluster", interfaceConfig.getCluster()); } @Test - public void testProxy() { + public void testProxy() throws Exception { InterfaceConfig interfaceConfig = new InterfaceConfig(); interfaceConfig.setProxy("mockproxyfactory"); - Assertions.assertEquals("mockproxyfactory", interfaceConfig.getProxy()); + TestCase.assertEquals("mockproxyfactory", interfaceConfig.getProxy()); } @Test - public void testConnections() { + public void testConnections() throws Exception { InterfaceConfig interfaceConfig = new InterfaceConfig(); interfaceConfig.setConnections(1); - Assertions.assertEquals(1, interfaceConfig.getConnections().intValue()); + TestCase.assertEquals(1, interfaceConfig.getConnections().intValue()); } @Test - public void testFilter() { + public void testFilter() throws Exception { InterfaceConfig interfaceConfig = new InterfaceConfig(); interfaceConfig.setFilter("mockfilter"); - Assertions.assertEquals("mockfilter", interfaceConfig.getFilter()); + TestCase.assertEquals("mockfilter", interfaceConfig.getFilter()); } @Test - public void testListener() { + public void testListener() throws Exception { InterfaceConfig interfaceConfig = new InterfaceConfig(); interfaceConfig.setListener("mockinvokerlistener"); - Assertions.assertEquals("mockinvokerlistener", interfaceConfig.getListener()); + TestCase.assertEquals("mockinvokerlistener", interfaceConfig.getListener()); } @Test - public void testLayer() { + public void testLayer() throws Exception { InterfaceConfig interfaceConfig = new InterfaceConfig(); interfaceConfig.setLayer("layer"); - Assertions.assertEquals("layer", interfaceConfig.getLayer()); + TestCase.assertEquals("layer", interfaceConfig.getLayer()); } @Test - public void testApplication() { + public void testApplication() throws Exception { InterfaceConfig interfaceConfig = new InterfaceConfig(); ApplicationConfig applicationConfig = new ApplicationConfig(); interfaceConfig.setApplication(applicationConfig); - Assertions.assertSame(applicationConfig, interfaceConfig.getApplication()); + TestCase.assertSame(applicationConfig, interfaceConfig.getApplication()); } @Test - public void testModule() { + public void testModule() throws Exception { InterfaceConfig interfaceConfig = new InterfaceConfig(); ModuleConfig moduleConfig = new ModuleConfig(); interfaceConfig.setModule(moduleConfig); - Assertions.assertSame(moduleConfig, interfaceConfig.getModule()); + TestCase.assertSame(moduleConfig, interfaceConfig.getModule()); } @Test - public void testRegistry() { + public void testRegistry() throws Exception { InterfaceConfig interfaceConfig = new InterfaceConfig(); RegistryConfig registryConfig = new RegistryConfig(); interfaceConfig.setRegistry(registryConfig); - Assertions.assertSame(registryConfig, interfaceConfig.getRegistry()); + TestCase.assertSame(registryConfig, interfaceConfig.getRegistry()); } @Test - public void testRegistries() { + public void testRegistries() throws Exception { InterfaceConfig interfaceConfig = new InterfaceConfig(); RegistryConfig registryConfig = new RegistryConfig(); interfaceConfig.setRegistries(Collections.singletonList(registryConfig)); - Assertions.assertEquals(1, interfaceConfig.getRegistries().size()); - Assertions.assertSame(registryConfig, interfaceConfig.getRegistries().get(0)); + TestCase.assertEquals(1, interfaceConfig.getRegistries().size()); + TestCase.assertSame(registryConfig, interfaceConfig.getRegistries().get(0)); } @Test - public void testMonitor() { + public void testMonitor() throws Exception { InterfaceConfig interfaceConfig = new InterfaceConfig(); interfaceConfig.setMonitor("monitor-addr"); - Assertions.assertEquals("monitor-addr", interfaceConfig.getMonitor().getAddress()); + TestCase.assertEquals("monitor-addr", interfaceConfig.getMonitor().getAddress()); MonitorConfig monitorConfig = new MonitorConfig(); interfaceConfig.setMonitor(monitorConfig); - Assertions.assertSame(monitorConfig, interfaceConfig.getMonitor()); + TestCase.assertSame(monitorConfig, interfaceConfig.getMonitor()); } @Test - public void testOwner() { + public void testOwner() throws Exception { InterfaceConfig interfaceConfig = new InterfaceConfig(); interfaceConfig.setOwner("owner"); - Assertions.assertEquals("owner", interfaceConfig.getOwner()); + TestCase.assertEquals("owner", interfaceConfig.getOwner()); } @Test - public void testCallbacks() { + public void testCallbacks() throws Exception { InterfaceConfig interfaceConfig = new InterfaceConfig(); interfaceConfig.setCallbacks(2); - Assertions.assertEquals(2, interfaceConfig.getCallbacks().intValue()); + TestCase.assertEquals(2, interfaceConfig.getCallbacks().intValue()); } @Test - public void testOnconnect() { + public void testOnconnect() throws Exception { InterfaceConfig interfaceConfig = new InterfaceConfig(); interfaceConfig.setOnconnect("onConnect"); - Assertions.assertEquals("onConnect", interfaceConfig.getOnconnect()); + TestCase.assertEquals("onConnect", interfaceConfig.getOnconnect()); } @Test - public void testOndisconnect() { + public void testOndisconnect() throws Exception { InterfaceConfig interfaceConfig = new InterfaceConfig(); interfaceConfig.setOndisconnect("onDisconnect"); - Assertions.assertEquals("onDisconnect", interfaceConfig.getOndisconnect()); + TestCase.assertEquals("onDisconnect", interfaceConfig.getOndisconnect()); } @Test - public void testScope() { + public void testScope() throws Exception { InterfaceConfig interfaceConfig = new InterfaceConfig(); interfaceConfig.setScope("scope"); - Assertions.assertEquals("scope", interfaceConfig.getScope()); + TestCase.assertEquals("scope", interfaceConfig.getScope()); } private void writeDubboProperties(String key, String value) { diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractMethodConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractMethodConfigTest.java index dde506a8093..063a2e3408a 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractMethodConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractMethodConfigTest.java @@ -16,7 +16,7 @@ */ package org.apache.dubbo.config; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.HashMap; import java.util.Map; @@ -25,7 +25,7 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.isEmptyOrNullString; import static org.hamcrest.Matchers.sameInstance; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class AbstractMethodConfigTest { @Test diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractReferenceConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractReferenceConfigTest.java index c77d3fde823..c4629f230f2 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractReferenceConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractReferenceConfigTest.java @@ -19,7 +19,7 @@ import org.apache.dubbo.common.Constants; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.HashMap; import java.util.Map; @@ -28,7 +28,7 @@ import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.hasValue; import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class AbstractReferenceConfigTest { diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractServiceConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractServiceConfigTest.java index f015975eab0..8e0ed83f717 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractServiceConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractServiceConfigTest.java @@ -19,7 +19,7 @@ import org.apache.dubbo.common.Constants; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.Collections; import java.util.HashMap; @@ -31,7 +31,7 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class AbstractServiceConfigTest { @Test diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ApplicationConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ApplicationConfigTest.java index d560921de4b..a49e57cc334 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ApplicationConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ApplicationConfigTest.java @@ -19,8 +19,7 @@ import org.apache.dubbo.common.Constants; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.Collections; import java.util.HashMap; @@ -32,7 +31,7 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.sameInstance; import static org.hamcrest.collection.IsCollectionWithSize.hasSize; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class ApplicationConfigTest { @Test @@ -89,12 +88,10 @@ public void testEnvironment1() throws Exception { assertThat(application.getEnvironment(), equalTo("product")); } - @Test + @Test(expected = IllegalStateException.class) public void testEnvironment2() throws Exception { - Assertions.assertThrows(IllegalStateException.class, () -> { - ApplicationConfig application = new ApplicationConfig("app"); - application.setEnvironment("illegal-env"); - }); + ApplicationConfig application = new ApplicationConfig("app"); + application.setEnvironment("illegal-env"); } @Test diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ArgumentConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ArgumentConfigTest.java index 1d9b08fe794..26051f92fe7 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ArgumentConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ArgumentConfigTest.java @@ -17,7 +17,7 @@ package org.apache.dubbo.config; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.HashMap; import java.util.Map; @@ -25,7 +25,7 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class ArgumentConfigTest { @Test diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ConfigCenterConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ConfigCenterConfigTest.java index 96e55825d74..f5ace5cb136 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ConfigCenterConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ConfigCenterConfigTest.java @@ -19,14 +19,13 @@ package org.apache.dubbo.config; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import junit.framework.TestCase; +import org.junit.Test; public class ConfigCenterConfigTest { @Test public void testPrefix() { ConfigCenterConfig config = new ConfigCenterConfig(); - Assertions.assertEquals("dubbo.config-center", config.getPrefix()); + TestCase.assertEquals("dubbo.config-center", config.getPrefix()); } } diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ConsumerConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ConsumerConfigTest.java index 503349210d6..b9a1a9a8827 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ConsumerConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ConsumerConfigTest.java @@ -17,11 +17,11 @@ package org.apache.dubbo.config; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class ConsumerConfigTest { @Test diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/MethodConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/MethodConfigTest.java index 1d976d7e4ee..a51ff76d976 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/MethodConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/MethodConfigTest.java @@ -18,75 +18,23 @@ package org.apache.dubbo.config; import org.apache.dubbo.common.Constants; -import org.apache.dubbo.config.annotation.Argument; -import org.apache.dubbo.config.annotation.Method; -import org.apache.dubbo.config.annotation.Reference; import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.Collections; import java.util.HashMap; -import java.util.List; import java.util.Map; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertThat; public class MethodConfigTest { - private static final String METHOD_NAME = "sayHello"; - private static final int TIMEOUT = 1300; - private static final int RETRIES = 4; - private static final String LOADBALANCE = "random"; - private static final boolean ASYNC = true; - private static final int ACTIVES = 3; - private static final int EXECUTES = 5; - private static final boolean DEPERECATED = true; - private static final boolean STICKY = true; - private static final String ONINVOKE = "i"; - private static final String ONTHROW = "t"; - private static final String ONRETURN = "r"; - private static final String CACHE = "c"; - private static final String VALIDATION = "v"; - private static final int ARGUMENTS_INDEX = 24; - private static final boolean ARGUMENTS_CALLBACK = true; - private static final String ARGUMENTS_TYPE = "sss"; - - @Reference(methods = {@Method(name = METHOD_NAME, timeout = TIMEOUT, retries = RETRIES, loadbalance = LOADBALANCE, async = ASYNC, - actives = ACTIVES, executes = EXECUTES, deprecated = DEPERECATED, sticky = STICKY, oninvoke = ONINVOKE, onthrow = ONTHROW, onreturn = ONRETURN, cache = CACHE, validation = VALIDATION, - arguments = {@Argument(index = ARGUMENTS_INDEX, callback = ARGUMENTS_CALLBACK, type = ARGUMENTS_TYPE)})}) - private String testField; - - @Test - public void testStaticConstructor() throws NoSuchFieldException { - Method[] methods = this.getClass().getDeclaredField("testField").getAnnotation(Reference.class).methods(); - List methodConfigs = MethodConfig.constructMethodConfig(methods); - MethodConfig methodConfig = methodConfigs.get(0); - - assertThat(METHOD_NAME, equalTo(methodConfig.getName())); - assertThat(TIMEOUT, equalTo(methodConfig.getTimeout().intValue())); - assertThat(RETRIES, equalTo(methodConfig.getRetries().intValue())); - assertThat(LOADBALANCE, equalTo(methodConfig.getLoadbalance())); - assertThat(ASYNC, equalTo(methodConfig.isAsync())); - assertThat(ACTIVES, equalTo(methodConfig.getActives().intValue())); - assertThat(EXECUTES, equalTo(methodConfig.getExecutes().intValue())); - assertThat(DEPERECATED, equalTo(methodConfig.getDeprecated())); - assertThat(STICKY, equalTo(methodConfig.getSticky())); - assertThat(ONINVOKE, equalTo(methodConfig.getOninvoke())); - assertThat(ONTHROW, equalTo(methodConfig.getOnthrow())); - assertThat(ONRETURN, equalTo(methodConfig.getOnreturn())); - assertThat(CACHE, equalTo(methodConfig.getCache())); - assertThat(VALIDATION, equalTo(methodConfig.getValidation())); - assertThat(ARGUMENTS_INDEX, equalTo(methodConfig.getArguments().get(0).getIndex().intValue())); - assertThat(ARGUMENTS_CALLBACK, equalTo(methodConfig.getArguments().get(0).isCallback())); - assertThat(ARGUMENTS_TYPE, equalTo(methodConfig.getArguments().get(0).getType())); - } - @Test public void testName() throws Exception { MethodConfig method = new MethodConfig(); diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ModuleConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ModuleConfigTest.java index 176d6fca183..c535e997c35 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ModuleConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ModuleConfigTest.java @@ -18,29 +18,25 @@ package org.apache.dubbo.config; import org.hamcrest.Matchers; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.Collections; import java.util.HashMap; import java.util.Map; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.sameInstance; +import static org.junit.Assert.assertThat; public class ModuleConfigTest { - @Test - public void testName1() { - Assertions.assertThrows(IllegalStateException.class, () -> { - ModuleConfig module = new ModuleConfig(); - Map parameters = new HashMap(); - ModuleConfig.appendParameters(parameters, module); - }); - + @Test(expected = IllegalStateException.class) + public void testName1() throws Exception { + ModuleConfig module = new ModuleConfig(); + Map parameters = new HashMap(); + ModuleConfig.appendParameters(parameters, module); } @Test diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/MonitorConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/MonitorConfigTest.java index 7a43de16b5a..c666072e9db 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/MonitorConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/MonitorConfigTest.java @@ -17,7 +17,7 @@ package org.apache.dubbo.config; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.Collections; import java.util.HashMap; @@ -26,7 +26,7 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class MonitorConfigTest { @Test diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ProtocolConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ProtocolConfigTest.java index ecbd153c4d0..0cd5a01fd83 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ProtocolConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ProtocolConfigTest.java @@ -20,7 +20,7 @@ import org.apache.dubbo.config.mock.MockProtocol2; import org.apache.dubbo.rpc.Protocol; -import org.junit.jupiter.api.Test; +import org.junit.Test; import org.mockito.Mockito; import java.util.Collections; @@ -30,7 +30,7 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class ProtocolConfigTest { diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ProviderConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ProviderConfigTest.java index 6dfe92646a4..18c630ace41 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ProviderConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ProviderConfigTest.java @@ -17,7 +17,7 @@ package org.apache.dubbo.config; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.HashMap; import java.util.Map; @@ -27,7 +27,7 @@ import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class ProviderConfigTest { @Test diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java index 8a5cf100ea6..758cd858f85 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java @@ -17,26 +17,23 @@ package org.apache.dubbo.config; import org.apache.dubbo.common.Constants; -import org.apache.dubbo.config.annotation.Argument; -import org.apache.dubbo.config.annotation.Method; -import org.apache.dubbo.config.annotation.Reference; import org.apache.dubbo.config.api.DemoService; import org.apache.dubbo.config.context.ConfigManager; import org.apache.dubbo.config.provider.impl.DemoServiceImpl; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; public class ReferenceConfigTest { - @BeforeEach + @Before public void setUp() { ConfigManager.getInstance().clear(); } - @AfterEach + @After public void tearDown() { ConfigManager.getInstance().clear(); } @@ -70,7 +67,7 @@ public void testInjvm() throws Exception { System.setProperty("java.net.preferIPv4Stack", "true"); demoService.export(); rc.get(); - Assertions.assertTrue(!Constants.LOCAL_PROTOCOL.equalsIgnoreCase( + Assert.assertTrue(!Constants.LOCAL_PROTOCOL.equalsIgnoreCase( rc.getInvoker().getUrl().getProtocol())); } finally { System.clearProperty("java.net.preferIPv4Stack"); @@ -103,8 +100,8 @@ public void testReferenceRetry() { } catch (Exception e) { e.printStackTrace(); } - Assertions.assertFalse(success); - Assertions.assertNull(demoService); + Assert.assertFalse(success); + Assert.assertNull(demoService); ServiceConfig sc = new ServiceConfig(); sc.setInterface(DemoService.class); @@ -123,36 +120,8 @@ public void testReferenceRetry() { } finally { System.clearProperty("java.net.preferIPv4Stack"); } - Assertions.assertTrue(success); - Assertions.assertNotNull(demoService); - - } - - @Test - public void testConstructWithReferenceAnnotation() throws NoSuchFieldException { - Reference reference = getClass().getDeclaredField("innerTest").getAnnotation(Reference.class); - ReferenceConfig referenceConfig = new ReferenceConfig(reference); - Assertions.assertTrue(referenceConfig.getMethods().size() == 1); - Assertions.assertEquals(((MethodConfig) referenceConfig.getMethods().get(0)).getName(), "sayHello"); - Assertions.assertTrue(((MethodConfig) referenceConfig.getMethods().get(0)).getTimeout() == 1300); - Assertions.assertTrue(((MethodConfig) referenceConfig.getMethods().get(0)).getRetries() == 4); - Assertions.assertEquals(((MethodConfig) referenceConfig.getMethods().get(0)).getLoadbalance(), "random"); - Assertions.assertTrue(((MethodConfig) referenceConfig.getMethods().get(0)).getActives() == 3); - Assertions.assertTrue(((MethodConfig) referenceConfig.getMethods().get(0)).getExecutes() == 5); - Assertions.assertTrue(((MethodConfig) referenceConfig.getMethods().get(0)).isAsync()); - Assertions.assertEquals(((MethodConfig) referenceConfig.getMethods().get(0)).getOninvoke(), "i"); - Assertions.assertEquals(((MethodConfig) referenceConfig.getMethods().get(0)).getOnreturn(), "r"); - Assertions.assertEquals(((MethodConfig) referenceConfig.getMethods().get(0)).getOnthrow(), "t"); - Assertions.assertEquals(((MethodConfig) referenceConfig.getMethods().get(0)).getCache(), "c"); - } - - - @Reference(methods = {@Method(name = "sayHello", timeout = 1300, retries = 4, loadbalance = "random", async = true, - actives = 3, executes = 5, deprecated = true, sticky = true, oninvoke = "i", onthrow = "t", onreturn = "r", cache = "c", validation = "v", - arguments = {@Argument(index = 24, callback = true, type = "sss")})}) - private InnerTest innerTest; - - private class InnerTest { + Assert.assertTrue(success); + Assert.assertNotNull(demoService); } } diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/RegistryConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/RegistryConfigTest.java index bb52242bf10..1494848f993 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/RegistryConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/RegistryConfigTest.java @@ -19,7 +19,7 @@ import org.apache.dubbo.common.Constants; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.Collections; import java.util.HashMap; @@ -30,7 +30,7 @@ import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.not; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class RegistryConfigTest { @Test diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ServiceConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ServiceConfigTest.java index 296538cbd94..0379045cc9c 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ServiceConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ServiceConfigTest.java @@ -32,15 +32,13 @@ import org.apache.dubbo.rpc.Protocol; import org.apache.dubbo.rpc.service.GenericService; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; import org.mockito.Mockito; import java.util.Collections; -import java.util.concurrent.TimeUnit; import static org.apache.dubbo.common.Constants.GENERIC_SERIALIZATION_BEAN; import static org.apache.dubbo.common.Constants.GENERIC_SERIALIZATION_DEFAULT; @@ -48,12 +46,11 @@ import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.hasSize; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.withSettings; public class ServiceConfigTest { @@ -62,9 +59,9 @@ public class ServiceConfigTest { private Exporter exporter = Mockito.mock(Exporter.class); private ServiceConfig service = new ServiceConfig(); private ServiceConfig service2 = new ServiceConfig(); - private ServiceConfig delayService = new ServiceConfig(); - @BeforeEach + + @Before public void setUp() throws Exception { MockProtocol2.delegate = protocolDelegate; MockRegistryFactory2.registry = registryDelegate; @@ -106,18 +103,10 @@ public void setUp() throws Exception { service2.setMethods(Collections.singletonList(method)); service2.setProxy("testproxyfactory"); - delayService.setProvider(provider); - delayService.setApplication(app); - delayService.setRegistry(registry); - delayService.setInterface(DemoService.class); - delayService.setRef(new DemoServiceImpl()); - delayService.setMethods(Collections.singletonList(method)); - delayService.setDelay(100); - ConfigManager.getInstance().clear(); } - @AfterEach + @After public void tearDown() { ConfigManager.getInstance().clear(); } @@ -135,6 +124,7 @@ public void testExport() throws Exception { assertThat(url.getParameters(), hasKey(Constants.BIND_IP_KEY)); assertThat(url.getParameters(), hasKey(Constants.BIND_PORT_KEY)); assertThat(url.getParameters(), hasEntry(Constants.DEFAULT_KEY + "." + Constants.EXPORT_KEY, "true")); + assertThat(url.getParameters(), hasEntry(Constants.EXPORT_KEY, "true")); assertThat(url.getParameters(), hasEntry("echo.0.callback", "false")); assertThat(url.getParameters(), hasEntry(Constants.GENERIC_KEY, "false")); assertThat(url.getParameters(), hasEntry(Constants.INTERFACE_KEY, DemoService.class.getName())); @@ -152,18 +142,8 @@ public void testProxy() throws Exception { assertEquals(2, TestProxyFactory.count); // local injvm and registry protocol, so expected is 2 } - @Test - public void testDelayExport() throws Exception { - delayService.export(); - assertTrue(delayService.getExportedUrls().isEmpty()); - //add 300ms to ensure that the delayService has been exported - TimeUnit.MILLISECONDS.sleep(delayService.getDelay() + 300); - assertThat(delayService.getExportedUrls(), hasSize(1)); - } - - @Test - @Disabled("cannot pass in travis") + @Ignore("cannot pass in travis") public void testUnexport() throws Exception { System.setProperty(Constants.SHUTDOWN_WAIT_KEY, "0"); try { @@ -187,12 +167,10 @@ public void testInterfaceClass() throws Exception { assertThat(service.getInterfaceClass() == GenericService.class, is(true)); } - @Test + @Test(expected = IllegalStateException.class) public void testInterface1() throws Exception { - Assertions.assertThrows(IllegalStateException.class, () -> { - ServiceConfig service = new ServiceConfig(); - service.setInterface(DemoServiceImpl.class); - }); + ServiceConfig service = new ServiceConfig(); + service.setInterface(DemoServiceImpl.class); } @Test @@ -221,27 +199,30 @@ public void testGeneric1() throws Exception { assertThat(service.getGeneric(), equalTo(GENERIC_SERIALIZATION_BEAN)); } - @Test + @Test(expected = IllegalArgumentException.class) public void testGeneric2() throws Exception { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - ServiceConfig service = new ServiceConfig(); - service.setGeneric("illegal"); - }); + ServiceConfig service = new ServiceConfig(); + service.setGeneric("illegal"); } - @Test + @Test(expected = IllegalArgumentException.class) public void testMock() throws Exception { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - ServiceConfig service = new ServiceConfig(); - service.setMock("true"); - }); + ServiceConfig service = new ServiceConfig(); + service.setMock("true"); } - @Test + @Test(expected = IllegalArgumentException.class) public void testMock2() throws Exception { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - ServiceConfig service = new ServiceConfig(); - service.setMock(true); - }); + ServiceConfig service = new ServiceConfig(); + service.setMock(true); + } + + @Test + public void testUniqueServiceName() throws Exception { + ServiceConfig service = new ServiceConfig(); + service.setGroup("dubbo"); + service.setInterface(Greeting.class); + service.setVersion("1.0.0"); + assertThat(service.getUniqueServiceName(), equalTo("dubbo/" + Greeting.class.getName() + ":1.0.0")); } } diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/AbstractBuilderTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/AbstractBuilderTest.java deleted file mode 100644 index dfbd74a29e5..00000000000 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/AbstractBuilderTest.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.AbstractConfig; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.HashMap; -import java.util.Map; - -class AbstractBuilderTest { - - @Test - void id() { - Builder builder = new Builder(); - builder.id("id"); - Assertions.assertEquals("id", builder.build().getId()); - } - - @Test - void prefix() { - Builder builder = new Builder(); - builder.prefix("prefix"); - Assertions.assertEquals("prefix", builder.build().getPrefix()); - } - - @Test - void appendParameter() { - Map source = null; - - Map parameters = new HashMap<>(); - parameters.put("default.num", "one"); - parameters.put("num", "ONE"); - source = AbstractBuilder.appendParameters(source, parameters); - - Assertions.assertTrue(source.containsKey("default.num")); - Assertions.assertEquals("ONE", source.get("num")); - } - - @Test - void appendParameter2() { - Map source = new HashMap<>(); - source.put("default.num", "one1"); - source.put("num", "ONE1"); - - Map parameters = new HashMap<>(); - parameters.put("default.num", "one"); - parameters.put("num", "ONE"); - source = AbstractBuilder.appendParameters(source, parameters); - - Assertions.assertTrue(source.containsKey("default.num")); - Assertions.assertEquals("ONE", source.get("num")); - } - - @Test - void appendParameters() { - Map source = null; - - source = AbstractBuilder.appendParameter(source, "default.num", "one"); - source = AbstractBuilder.appendParameter(source, "num", "ONE"); - - Assertions.assertTrue(source.containsKey("default.num")); - Assertions.assertEquals("ONE", source.get("num")); - } - - @Test - void appendParameters2() { - Map source = new HashMap<>(); - source.put("default.num", "one1"); - source.put("num", "ONE1"); - - source = AbstractBuilder.appendParameter(source, "default.num", "one"); - source = AbstractBuilder.appendParameter(source, "num", "ONE"); - - Assertions.assertTrue(source.containsKey("default.num")); - Assertions.assertEquals("ONE", source.get("num")); - } - - @Test - void build() { - Builder builder = new Builder(); - builder.id("id"); - builder.prefix("prefix"); - - Config config = builder.build(); - Config config2 = builder.build(); - - Assertions.assertEquals("id", config.getId()); - Assertions.assertEquals("prefix", config.getPrefix()); - - Assertions.assertNotSame(config, config2); - } - - private static class Builder extends AbstractBuilder { - public Config build() { - Config parameterConfig = new Config(); - super.build(parameterConfig); - - return parameterConfig; - } - - @Override - protected Builder getThis() { - return this; - } - } - - private static class Config extends AbstractConfig { } -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/AbstractInterfaceBuilderTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/AbstractInterfaceBuilderTest.java deleted file mode 100644 index 149d0304709..00000000000 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/AbstractInterfaceBuilderTest.java +++ /dev/null @@ -1,310 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.AbstractInterfaceConfig; -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.config.ConfigCenterConfig; -import org.apache.dubbo.config.MetadataReportConfig; -import org.apache.dubbo.config.ModuleConfig; -import org.apache.dubbo.config.MonitorConfig; -import org.apache.dubbo.config.RegistryConfig; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.Collections; - -class AbstractInterfaceBuilderTest { - - @Test - void local() { - InterfaceBuilder builder = new InterfaceBuilder(); - builder.local("GreetingMock"); - Assertions.assertEquals("GreetingMock", builder.build().getLocal()); - } - - @Test - void local1() { - InterfaceBuilder builder = new InterfaceBuilder(); - builder.local((Boolean) null); - Assertions.assertNull(builder.build().getLocal()); - builder.local(false); - Assertions.assertEquals("false", builder.build().getLocal()); - builder.local(true); - Assertions.assertEquals("true", builder.build().getLocal()); - } - - @Test - void stub() { - InterfaceBuilder builder = new InterfaceBuilder(); - builder.stub("GreetingMock"); - Assertions.assertEquals("GreetingMock", builder.build().getStub()); - } - - @Test - void stub1() { - InterfaceBuilder builder = new InterfaceBuilder(); - builder.stub((Boolean) null); - Assertions.assertNull(builder.build().getLocal()); - builder.stub(false); - Assertions.assertEquals("false", builder.build().getStub()); - builder.stub(true); - Assertions.assertEquals("true", builder.build().getStub()); - } - - @Test - void monitor() { - InterfaceBuilder builder = new InterfaceBuilder(); - builder.monitor("123"); - - MonitorConfig monitorConfig = new MonitorConfig("123"); - Assertions.assertEquals(monitorConfig, builder.build().getMonitor()); - } - - @Test - void monitor1() { - MonitorConfig monitorConfig = new MonitorConfig("123"); - InterfaceBuilder builder = new InterfaceBuilder(); - builder.monitor(monitorConfig); - - Assertions.assertEquals(monitorConfig, builder.build().getMonitor()); - } - - @Test - void proxy() { - InterfaceBuilder builder = new InterfaceBuilder(); - builder.proxy("mockproxyfactory"); - - Assertions.assertEquals("mockproxyfactory", builder.build().getProxy()); - } - - @Test - void cluster() { - InterfaceBuilder builder = new InterfaceBuilder(); - builder.cluster("mockcluster"); - - Assertions.assertEquals("mockcluster", builder.build().getCluster()); - } - - @Test - void filter() { - InterfaceBuilder builder = new InterfaceBuilder(); - builder.filter("mockfilter"); - - Assertions.assertEquals("mockfilter", builder.build().getFilter()); - } - - @Test - void listener() { - InterfaceBuilder builder = new InterfaceBuilder(); - builder.listener("mockinvokerlistener"); - - Assertions.assertEquals("mockinvokerlistener", builder.build().getListener()); - } - - @Test - void owner() { - InterfaceBuilder builder = new InterfaceBuilder(); - builder.owner("owner"); - - Assertions.assertEquals("owner", builder.build().getOwner()); - } - - @Test - void connections() { - InterfaceBuilder builder = new InterfaceBuilder(); - builder.connections(1); - - Assertions.assertEquals(1, builder.build().getConnections().intValue()); - } - - @Test - void layer() { - InterfaceBuilder builder = new InterfaceBuilder(); - builder.layer("layer"); - - Assertions.assertEquals("layer", builder.build().getLayer()); - } - - @Test - void application() { - ApplicationConfig applicationConfig = new ApplicationConfig(); - - InterfaceBuilder builder = new InterfaceBuilder(); - builder.application(applicationConfig); - - Assertions.assertEquals(applicationConfig, builder.build().getApplication()); - } - - @Test - void module() { - ModuleConfig moduleConfig = new ModuleConfig(); - InterfaceBuilder builder = new InterfaceBuilder(); - builder.module(moduleConfig); - - Assertions.assertEquals(moduleConfig, builder.build().getModule()); - } - - @Test - void addRegistries() { - RegistryConfig registryConfig = new RegistryConfig(); - - InterfaceBuilder builder = new InterfaceBuilder(); - builder.addRegistries(Collections.singletonList(registryConfig)); - - Assertions.assertEquals(1, builder.build().getRegistries().size()); - Assertions.assertSame(registryConfig, builder.build().getRegistries().get(0)); - Assertions.assertSame(registryConfig, builder.build().getRegistry()); - } - - @Test - void addRegistry() { - RegistryConfig registryConfig = new RegistryConfig(); - - InterfaceBuilder builder = new InterfaceBuilder(); - builder.addRegistry(registryConfig); - - Assertions.assertEquals(1, builder.build().getRegistries().size()); - Assertions.assertSame(registryConfig, builder.build().getRegistries().get(0)); - Assertions.assertSame(registryConfig, builder.build().getRegistry()); - } - - @Test - void registryIds() { - InterfaceBuilder builder = new InterfaceBuilder(); - builder.registryIds("registryIds"); - - Assertions.assertEquals("registryIds", builder.build().getRegistryIds()); - } - - @Test - void onconnect() { - InterfaceBuilder builder = new InterfaceBuilder(); - builder.onconnect("onconnect"); - - Assertions.assertEquals("onconnect", builder.build().getOnconnect()); - } - - @Test - void ondisconnect() { - InterfaceBuilder builder = new InterfaceBuilder(); - builder.ondisconnect("ondisconnect"); - - Assertions.assertEquals("ondisconnect", builder.build().getOndisconnect()); - } - - @Test - void metadataReportConfig() { - MetadataReportConfig metadataReportConfig = new MetadataReportConfig(); - - InterfaceBuilder builder = new InterfaceBuilder(); - builder.metadataReportConfig(metadataReportConfig); - - Assertions.assertEquals(metadataReportConfig, builder.build().getMetadataReportConfig()); - } - - @Test - void configCenter() { - ConfigCenterConfig configCenterConfig = new ConfigCenterConfig(); - - InterfaceBuilder builder = new InterfaceBuilder(); - builder.configCenter(configCenterConfig); - - Assertions.assertEquals(configCenterConfig, builder.build().getConfigCenter()); - } - - @Test - void callbacks() { - InterfaceBuilder builder = new InterfaceBuilder(); - builder.callbacks(2); - Assertions.assertEquals(2, builder.build().getCallbacks().intValue()); - } - - @Test - void scope() { - InterfaceBuilder builder = new InterfaceBuilder(); - builder.scope("scope"); - - Assertions.assertEquals("scope", builder.build().getScope()); - } - - @Test - void build() { - MonitorConfig monitorConfig = new MonitorConfig("123"); - ApplicationConfig applicationConfig = new ApplicationConfig(); - ModuleConfig moduleConfig = new ModuleConfig(); - RegistryConfig registryConfig = new RegistryConfig(); - MetadataReportConfig metadataReportConfig = new MetadataReportConfig(); - ConfigCenterConfig configCenterConfig = new ConfigCenterConfig(); - - InterfaceBuilder builder = new InterfaceBuilder(); - builder.id("id").prefix("prefix").local(true).stub(false).monitor("123").proxy("mockproxyfactory").cluster("mockcluster") - .filter("mockfilter").listener("mockinvokerlistener").owner("owner").connections(1) - .layer("layer").application(applicationConfig).module(moduleConfig) - .addRegistry(registryConfig).registryIds("registryIds") - .onconnect("onconnet").ondisconnect("ondisconnect") - .metadataReportConfig(metadataReportConfig) - .configCenter(configCenterConfig) - .callbacks(2).scope("scope"); - - InterfaceConfig config = builder.build(); - InterfaceConfig config2 = builder.build(); - - Assertions.assertEquals("id", config.getId()); - Assertions.assertEquals("prefix", config.getPrefix()); - Assertions.assertEquals("true", config.getLocal()); - Assertions.assertEquals("false", config.getStub()); - Assertions.assertEquals(monitorConfig, config.getMonitor()); - Assertions.assertEquals("mockproxyfactory", config.getProxy()); - Assertions.assertEquals("mockcluster", config.getCluster()); - Assertions.assertEquals("mockfilter", config.getFilter()); - Assertions.assertEquals("mockinvokerlistener", config.getListener()); - Assertions.assertEquals("owner", config.getOwner()); - Assertions.assertEquals(1, config.getConnections().intValue()); - Assertions.assertEquals("layer", config.getLayer()); - Assertions.assertEquals(applicationConfig, config.getApplication()); - Assertions.assertEquals(moduleConfig, config.getModule()); - Assertions.assertEquals(registryConfig, config.getRegistry()); - Assertions.assertEquals("registryIds", config.getRegistryIds()); - Assertions.assertEquals("onconnet", config.getOnconnect()); - Assertions.assertEquals("ondisconnect", config.getOndisconnect()); - Assertions.assertEquals(metadataReportConfig, config.getMetadataReportConfig()); - Assertions.assertEquals(configCenterConfig, config.getConfigCenter()); - Assertions.assertEquals(2, config.getCallbacks().intValue()); - Assertions.assertEquals("scope", config.getScope()); - - Assertions.assertNotSame(config, config2); - } - - private static class InterfaceBuilder extends AbstractInterfaceBuilder { - - public InterfaceConfig build() { - InterfaceConfig config = new InterfaceConfig(); - super.build(config); - - return config; - } - - @Override - protected InterfaceBuilder getThis() { - return this; - } - } - - private static class InterfaceConfig extends AbstractInterfaceConfig { } -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/AbstractMethodBuilderTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/AbstractMethodBuilderTest.java deleted file mode 100644 index 0d1fd08da23..00000000000 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/AbstractMethodBuilderTest.java +++ /dev/null @@ -1,195 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.AbstractMethodConfig; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.HashMap; -import java.util.Map; - -class AbstractMethodBuilderTest { - - @Test - void timeout() { - MethodBuilder builder = new MethodBuilder(); - builder.timeout(10); - - Assertions.assertEquals(10, builder.build().getTimeout()); - } - - @Test - void retries() { - MethodBuilder builder = new MethodBuilder(); - builder.retries(3); - - Assertions.assertEquals(3, builder.build().getRetries()); - } - - @Test - void actives() { - MethodBuilder builder = new MethodBuilder(); - builder.actives(3); - - Assertions.assertEquals(3, builder.build().getActives()); - } - - @Test - void loadbalance() { - MethodBuilder builder = new MethodBuilder(); - builder.loadbalance("mockloadbalance"); - - Assertions.assertEquals("mockloadbalance", builder.build().getLoadbalance()); - } - - @Test - void async() { - MethodBuilder builder = new MethodBuilder(); - builder.async(true); - - Assertions.assertTrue(builder.build().isAsync()); - } - - @Test - void sent() { - MethodBuilder builder = new MethodBuilder(); - builder.sent(true); - - Assertions.assertTrue(builder.build().getSent()); - } - - @Test - void mock() { - MethodBuilder builder = new MethodBuilder(); - builder.mock("mock"); - Assertions.assertEquals("mock", builder.build().getMock()); - builder.mock("return null"); - Assertions.assertEquals("return null", builder.build().getMock()); - } - - @Test - void mock1() { - MethodBuilder builder = new MethodBuilder(); - builder.mock(true); - Assertions.assertEquals("true", builder.build().getMock()); - builder.mock(false); - Assertions.assertEquals("false", builder.build().getMock()); - } - - @Test - void merger() { - MethodBuilder builder = new MethodBuilder(); - builder.merger("merger"); - Assertions.assertEquals("merger", builder.build().getMerger()); - } - - @Test - void cache() { - MethodBuilder builder = new MethodBuilder(); - builder.cache("cache"); - Assertions.assertEquals("cache", builder.build().getCache()); - } - - @Test - void validation() { - MethodBuilder builder = new MethodBuilder(); - builder.validation("validation"); - Assertions.assertEquals("validation", builder.build().getValidation()); - } - - @Test - void appendParameter() { - MethodBuilder builder = new MethodBuilder(); - builder.appendParameter("default.num", "one").appendParameter("num", "ONE"); - - Map parameters = builder.build().getParameters(); - - Assertions.assertTrue(parameters.containsKey("default.num")); - Assertions.assertEquals("ONE", parameters.get("num")); - } - - @Test - void appendParameters() { - Map source = new HashMap<>(); - source.put("default.num", "one"); - source.put("num", "ONE"); - - MethodBuilder builder = new MethodBuilder(); - builder.appendParameters(source); - - Map parameters = builder.build().getParameters(); - - Assertions.assertTrue(parameters.containsKey("default.num")); - Assertions.assertEquals("ONE", parameters.get("num")); - } - - @Test - void forks() { - MethodBuilder builder = new MethodBuilder(); - builder.forks(5); - - Assertions.assertEquals(5, builder.build().getForks()); - } - - @Test - void build() { - MethodBuilder builder = new MethodBuilder(); - builder.id("id").prefix("prefix").timeout(1).retries(2).actives(3).loadbalance("mockloadbalance").async(true) - .sent(false).mock("mock").merger("merger").cache("cache").validation("validation") - .appendParameter("default.num", "one"); - - MethodConfig config = builder.build(); - MethodConfig config2 = builder.build(); - - Assertions.assertEquals("id", config.getId()); - Assertions.assertEquals("prefix", config.getPrefix()); - Assertions.assertEquals(1, config.getTimeout()); - Assertions.assertEquals(2, config.getRetries()); - Assertions.assertEquals(3, config.getActives()); - Assertions.assertEquals("mockloadbalance", config.getLoadbalance()); - Assertions.assertTrue(config.isAsync()); - Assertions.assertFalse(config.getSent()); - Assertions.assertEquals("mock", config.getMock()); - Assertions.assertEquals("merger", config.getMerger()); - Assertions.assertEquals("cache", config.getCache()); - Assertions.assertEquals("validation", config.getValidation()); - Assertions.assertTrue(config.getParameters().containsKey("default.num")); - Assertions.assertEquals("one", config.getParameters().get("default.num")); - - Assertions.assertNotSame(config, config2); - - } - - private static class MethodBuilder extends AbstractMethodBuilder { - - public MethodConfig build() { - MethodConfig parameterConfig = new MethodConfig(); - super.build(parameterConfig); - - return parameterConfig; - } - - @Override - protected MethodBuilder getThis() { - return this; - } - } - - private static class MethodConfig extends AbstractMethodConfig { } -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/AbstractReferenceBuilderTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/AbstractReferenceBuilderTest.java deleted file mode 100644 index 2a5f697684d..00000000000 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/AbstractReferenceBuilderTest.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.AbstractReferenceConfig; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -class AbstractReferenceBuilderTest { - - @Test - void check() { - ReferenceBuilder builder = new ReferenceBuilder(); - builder.check(true); - Assertions.assertTrue(builder.build().isCheck()); - builder.check(false); - Assertions.assertFalse(builder.build().isCheck()); - } - - @Test - void init() { - ReferenceBuilder builder = new ReferenceBuilder(); - builder.init(true); - Assertions.assertTrue(builder.build().isInit()); - builder.init(false); - Assertions.assertFalse(builder.build().isInit()); - } - - @Test - void generic() { - ReferenceBuilder builder = new ReferenceBuilder(); - builder.generic(true); - Assertions.assertTrue(builder.build().isGeneric()); - builder.generic(false); - Assertions.assertFalse(builder.build().isGeneric()); - } - - @Test - void generic1() { - ReferenceBuilder builder = new ReferenceBuilder(); - builder.generic("generic"); - Assertions.assertEquals("generic", builder.build().getGeneric()); - } - - @Test - void injvm() { - ReferenceBuilder builder = new ReferenceBuilder(); - builder.injvm(true); - Assertions.assertTrue(builder.build().isInjvm()); - builder.injvm(false); - Assertions.assertFalse(builder.build().isInjvm()); - } - - @Test - void lazy() { - ReferenceBuilder builder = new ReferenceBuilder(); - builder.lazy(true); - Assertions.assertTrue(builder.build().getLazy()); - builder.lazy(false); - Assertions.assertFalse(builder.build().getLazy()); - } - - @Test - void reconnect() { - ReferenceBuilder builder = new ReferenceBuilder(); - builder.reconnect("reconnect"); - Assertions.assertEquals("reconnect", builder.build().getReconnect()); - } - - @Test - void sticky() { - ReferenceBuilder builder = new ReferenceBuilder(); - builder.sticky(true); - Assertions.assertTrue(builder.build().getSticky()); - builder.sticky(false); - Assertions.assertFalse(builder.build().getSticky()); - } - - @Test - void version() { - ReferenceBuilder builder = new ReferenceBuilder(); - builder.version("version"); - Assertions.assertEquals("version", builder.build().getVersion()); - } - - @Test - void group() { - ReferenceBuilder builder = new ReferenceBuilder(); - builder.group("group"); - Assertions.assertEquals("group", builder.build().getGroup()); - } - - @Test - void build() { - ReferenceBuilder builder = new ReferenceBuilder(); - builder.check(true).init(false).generic(true).injvm(false).lazy(true).reconnect("reconnect").sticky(false) - .version("version").group("group").id("id").prefix("prefix"); - - ReferenceConfig config = builder.build(); - ReferenceConfig config2 = builder.build(); - - Assertions.assertEquals("id", config.getId()); - Assertions.assertEquals("prefix", config.getPrefix()); - Assertions.assertTrue(config.isCheck()); - Assertions.assertFalse(config.isInit()); - Assertions.assertTrue(config.isGeneric()); - Assertions.assertFalse(config.isInjvm()); - Assertions.assertTrue(config.getLazy()); - Assertions.assertFalse(config.getSticky()); - Assertions.assertEquals("reconnect", config.getReconnect()); - Assertions.assertEquals("version", config.getVersion()); - Assertions.assertEquals("group", config.getGroup()); - - Assertions.assertNotSame(config, config2); - } - - private static class ReferenceBuilder extends AbstractReferenceBuilder { - - public ReferenceConfig build() { - ReferenceConfig parameterConfig = new ReferenceConfig(); - super.build(parameterConfig); - - return parameterConfig; - } - - @Override - protected ReferenceBuilder getThis() { - return this; - } - } - - private static class ReferenceConfig extends AbstractReferenceConfig { } -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/AbstractServiceBuilderTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/AbstractServiceBuilderTest.java deleted file mode 100644 index 59dee6e6525..00000000000 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/AbstractServiceBuilderTest.java +++ /dev/null @@ -1,245 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.AbstractServiceConfig; -import org.apache.dubbo.config.ProtocolConfig; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.Collections; - -class AbstractServiceBuilderTest { - - @Test - void version() { - ServiceBuilder builder = new ServiceBuilder(); - builder.version("version"); - Assertions.assertEquals("version", builder.build().getVersion()); - } - - @Test - void group() { - ServiceBuilder builder = new ServiceBuilder(); - builder.group("group"); - Assertions.assertEquals("group", builder.build().getGroup()); - } - - @Test - void deprecated() { - ServiceBuilder builder = new ServiceBuilder(); - builder.deprecated(true); - Assertions.assertTrue(builder.build().isDeprecated()); - builder.deprecated(false); - Assertions.assertFalse(builder.build().isDeprecated()); - } - - @Test - void delay() { - ServiceBuilder builder = new ServiceBuilder(); - builder.delay(1000); - Assertions.assertEquals(1000, builder.build().getDelay()); - } - - @Test - void export() { - ServiceBuilder builder = new ServiceBuilder(); - builder.export(true); - Assertions.assertTrue(builder.build().getExport()); - builder.export(false); - Assertions.assertFalse(builder.build().getExport()); - } - - @Test - void weight() { - ServiceBuilder builder = new ServiceBuilder(); - builder.weight(500); - Assertions.assertEquals(500, builder.build().getWeight()); - } - - @Test - void document() { - ServiceBuilder builder = new ServiceBuilder(); - builder.document("http://dubbo.io"); - Assertions.assertEquals("http://dubbo.io", builder.build().getDocument()); - } - - @Test - void dynamic() { - ServiceBuilder builder = new ServiceBuilder(); - builder.dynamic(true); - Assertions.assertTrue(builder.build().isDynamic()); - builder.dynamic(false); - Assertions.assertFalse(builder.build().isDynamic()); - } - - @Test - void token() { - ServiceBuilder builder = new ServiceBuilder(); - builder.token("token"); - Assertions.assertEquals("token", builder.build().getToken()); - } - - @Test - void token1() { - ServiceBuilder builder = new ServiceBuilder(); - builder.token(true); - Assertions.assertEquals("true", builder.build().getToken()); - builder.token(false); - Assertions.assertEquals("false",builder.build().getToken()); - builder.token((Boolean) null); - Assertions.assertNull(builder.build().getToken()); - } - - @Test - void accesslog() { - ServiceBuilder builder = new ServiceBuilder(); - builder.accesslog("accesslog"); - Assertions.assertEquals("accesslog", builder.build().getAccesslog()); - } - - @Test - void accesslog1() { - ServiceBuilder builder = new ServiceBuilder(); - builder.accesslog(true); - Assertions.assertEquals("true", builder.build().getAccesslog()); - builder.accesslog(false); - Assertions.assertEquals("false",builder.build().getAccesslog()); - builder.accesslog((Boolean) null); - Assertions.assertNull(builder.build().getAccesslog()); - } - - @Test - void addProtocols() { - ProtocolConfig protocol = new ProtocolConfig(); - ServiceBuilder builder = new ServiceBuilder(); - Assertions.assertNull(builder.build().getProtocols()); - builder.addProtocols(Collections.singletonList(protocol)); - Assertions.assertNotNull(builder.build().getProtocols()); - Assertions.assertEquals(1, builder.build().getProtocols().size()); - } - - @Test - void addProtocol() { - ProtocolConfig protocol = new ProtocolConfig(); - ServiceBuilder builder = new ServiceBuilder(); - Assertions.assertNull(builder.build().getProtocols()); - builder.addProtocol(protocol); - Assertions.assertNotNull(builder.build().getProtocols()); - Assertions.assertEquals(1, builder.build().getProtocols().size()); - Assertions.assertEquals(protocol, builder.build().getProtocol()); - } - - @Test - void protocolIds() { - ServiceBuilder builder = new ServiceBuilder(); - builder.protocolIds("protocolIds"); - Assertions.assertEquals("protocolIds", builder.build().getProtocolIds()); - } - - @Test - void tag() { - ServiceBuilder builder = new ServiceBuilder(); - builder.tag("tag"); - Assertions.assertEquals("tag", builder.build().getTag()); - } - - @Test - void executes() { - ServiceBuilder builder = new ServiceBuilder(); - builder.executes(10); - Assertions.assertEquals(10, builder.build().getExecutes()); - } - - @Test - void register() { - ServiceBuilder builder = new ServiceBuilder(); - builder.register(true); - Assertions.assertTrue(builder.build().isRegister()); - builder.register(false); - Assertions.assertFalse(builder.build().isRegister()); - } - - @Test - void warmup() { - ServiceBuilder builder = new ServiceBuilder(); - builder.warmup(100); - Assertions.assertEquals(100, builder.build().getWarmup()); - } - - @Test - void serialization() { - ServiceBuilder builder = new ServiceBuilder(); - builder.serialization("serialization"); - Assertions.assertEquals("serialization", builder.build().getSerialization()); - } - - @Test - void build() { - ProtocolConfig protocol = new ProtocolConfig(); - - ServiceBuilder builder = new ServiceBuilder(); - builder.version("version").group("group").deprecated(true).delay(1000).export(false).weight(1) - .document("document").dynamic(true).token("token").accesslog("accesslog") - .addProtocol(protocol).protocolIds("protocolIds").tag("tag").executes(100).register(false) - .warmup(200).serialization("serialization").id("id").prefix("prefix"); - - ServiceConfig config = builder.build(); - ServiceConfig config2 = builder.build(); - - Assertions.assertEquals("id", config.getId()); - Assertions.assertEquals("prefix", config.getPrefix()); - Assertions.assertEquals("version", config.getVersion()); - Assertions.assertEquals("group", config.getGroup()); - Assertions.assertEquals("document", config.getDocument()); - Assertions.assertEquals("token", config.getToken()); - Assertions.assertEquals("accesslog", config.getAccesslog()); - Assertions.assertEquals("protocolIds", config.getProtocolIds()); - Assertions.assertEquals("tag", config.getTag()); - Assertions.assertEquals("serialization", config.getSerialization()); - Assertions.assertTrue(config.isDeprecated()); - Assertions.assertFalse(config.getExport()); - Assertions.assertTrue(config.isDynamic()); - Assertions.assertFalse(config.isRegister()); - Assertions.assertEquals(1000, config.getDelay()); - Assertions.assertEquals(1, config.getWeight()); - Assertions.assertEquals(100, config.getExecutes()); - Assertions.assertEquals(200, config.getWarmup()); - - Assertions.assertNotSame(config, config2); - } - - private static class ServiceBuilder extends AbstractServiceBuilder { - - public ServiceConfig build() { - ServiceConfig parameterConfig = new ServiceConfig(); - super.build(parameterConfig); - - return parameterConfig; - } - - @Override - protected ServiceBuilder getThis() { - return this; - } - } - - private static class ServiceConfig extends AbstractServiceConfig { - - } -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ApplicationBuilderTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ApplicationBuilderTest.java deleted file mode 100644 index 87d5693f222..00000000000 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ApplicationBuilderTest.java +++ /dev/null @@ -1,255 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.config.MonitorConfig; -import org.apache.dubbo.config.RegistryConfig; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -class ApplicationBuilderTest { - - @Test - void name() { - ApplicationBuilder builder = new ApplicationBuilder(); - builder.name("app"); - Assertions.assertEquals("app", builder.build().getName()); - } - - @Test - void version() { - ApplicationBuilder builder = new ApplicationBuilder(); - builder.version("version"); - Assertions.assertEquals("version", builder.build().getVersion()); - } - - @Test - void owner() { - ApplicationBuilder builder = new ApplicationBuilder(); - builder.owner("owner"); - Assertions.assertEquals("owner", builder.build().getOwner()); - } - - @Test - void organization() { - ApplicationBuilder builder = new ApplicationBuilder(); - builder.organization("organization"); - Assertions.assertEquals("organization", builder.build().getOrganization()); - } - - @Test - void architecture() { - ApplicationBuilder builder = new ApplicationBuilder(); - builder.architecture("architecture"); - Assertions.assertEquals("architecture", builder.build().getArchitecture()); - } - - @Test - void environment() { - ApplicationBuilder builder = new ApplicationBuilder(); - Assertions.assertEquals("product", builder.build().getEnvironment()); - builder.environment("develop"); - Assertions.assertEquals("develop", builder.build().getEnvironment()); - builder.environment("test"); - Assertions.assertEquals("test", builder.build().getEnvironment()); - builder.environment("product"); - Assertions.assertEquals("product", builder.build().getEnvironment()); - } - - @Test - void compiler() { - ApplicationBuilder builder = new ApplicationBuilder(); - builder.compiler("compiler"); - Assertions.assertEquals("compiler", builder.build().getCompiler()); - } - - @Test - void logger() { - ApplicationBuilder builder = new ApplicationBuilder(); - builder.logger("log4j"); - Assertions.assertEquals("log4j", builder.build().getLogger()); - } - - @Test - void addRegistry() { - RegistryConfig registry = new RegistryConfig(); - ApplicationBuilder builder = new ApplicationBuilder(); - builder.addRegistry(registry); - Assertions.assertNotNull(builder.build().getRegistry()); - Assertions.assertEquals(1, builder.build().getRegistries().size()); - Assertions.assertSame(registry, builder.build().getRegistry()); - } - - @Test - void addRegistries() { - RegistryConfig registry = new RegistryConfig(); - ApplicationBuilder builder = new ApplicationBuilder(); - builder.addRegistries(Collections.singletonList(registry)); - Assertions.assertNotNull(builder.build().getRegistry()); - Assertions.assertEquals(1, builder.build().getRegistries().size()); - Assertions.assertSame(registry, builder.build().getRegistry()); - } - - @Test - void registryIds() { - ApplicationBuilder builder = new ApplicationBuilder(); - builder.registryIds("registryIds"); - Assertions.assertEquals("registryIds", builder.build().getRegistryIds()); - } - - @Test - void monitor() { - MonitorConfig monitor = new MonitorConfig("monitor-addr"); - ApplicationBuilder builder = new ApplicationBuilder(); - builder.monitor(monitor); - Assertions.assertSame(monitor, builder.build().getMonitor()); - Assertions.assertEquals("monitor-addr", builder.build().getMonitor().getAddress()); - } - - @Test - void monitor1() { - ApplicationBuilder builder = new ApplicationBuilder(); - builder.monitor("monitor-addr"); - Assertions.assertEquals("monitor-addr", builder.build().getMonitor().getAddress()); - } - - @Test - void isDefault() { - ApplicationBuilder builder = new ApplicationBuilder(); - builder.isDefault(true); - Assertions.assertTrue(builder.build().isDefault()); - builder.isDefault(false); - Assertions.assertFalse(builder.build().isDefault()); - builder.isDefault(null); - Assertions.assertNull(builder.build().isDefault()); - } - - @Test - void dumpDirectory() { - ApplicationBuilder builder = new ApplicationBuilder(); - builder.dumpDirectory("dumpDirectory"); - Assertions.assertEquals("dumpDirectory", builder.build().getDumpDirectory()); - } - - @Test - void qosEnable() { - ApplicationBuilder builder = new ApplicationBuilder(); - builder.qosEnable(true); - Assertions.assertTrue(builder.build().getQosEnable()); - builder.qosEnable(false); - Assertions.assertFalse(builder.build().getQosEnable()); - builder.qosEnable(null); - Assertions.assertNull(builder.build().getQosEnable()); - } - - @Test - void qosPort() { - ApplicationBuilder builder = new ApplicationBuilder(); - builder.qosPort(8080); - Assertions.assertEquals(8080, builder.build().getQosPort()); - } - - @Test - void qosAcceptForeignIp() { - ApplicationBuilder builder = new ApplicationBuilder(); - builder.qosAcceptForeignIp(true); - Assertions.assertTrue(builder.build().getQosAcceptForeignIp()); - builder.qosAcceptForeignIp(false); - Assertions.assertFalse(builder.build().getQosAcceptForeignIp()); - builder.qosAcceptForeignIp(null); - Assertions.assertNull(builder.build().getQosAcceptForeignIp()); - } - - @Test - void shutwait() { - ApplicationBuilder builder = new ApplicationBuilder(); - builder.shutwait("shutwait"); - Assertions.assertEquals("shutwait", builder.build().getShutwait()); - } - - @Test - void appendParameter() { - ApplicationBuilder builder = new ApplicationBuilder(); - builder.appendParameter("default.num", "one").appendParameter("num", "ONE"); - - Map parameters = builder.build().getParameters(); - - Assertions.assertTrue(parameters.containsKey("default.num")); - Assertions.assertEquals("ONE", parameters.get("num")); - } - - @Test - void appendParameters() { - Map source = new HashMap<>(); - source.put("default.num", "one"); - source.put("num", "ONE"); - - ApplicationBuilder builder = new ApplicationBuilder(); - builder.appendParameters(source); - - Map parameters = builder.build().getParameters(); - - Assertions.assertTrue(parameters.containsKey("default.num")); - Assertions.assertEquals("ONE", parameters.get("num")); - } - - @Test - void build() { - MonitorConfig monitor = new MonitorConfig("monitor-addr"); - RegistryConfig registry = new RegistryConfig(); - - ApplicationBuilder builder = new ApplicationBuilder(); - builder.id("id").prefix("prefix").name("name").version("version").owner("owner").organization("organization").architecture("architecture") - .environment("develop").compiler("compiler").logger("log4j").monitor(monitor).isDefault(false) - .dumpDirectory("dumpDirectory").qosEnable(true).qosPort(8080).qosAcceptForeignIp(false) - .shutwait("shutwait").registryIds("registryIds").addRegistry(registry) - .appendParameter("default.num", "one"); - - ApplicationConfig config = builder.build(); - ApplicationConfig config2 = builder.build(); - - Assertions.assertEquals("id", config.getId()); - Assertions.assertEquals("prefix", config.getPrefix()); - Assertions.assertEquals("name", config.getName()); - Assertions.assertEquals("version", config.getVersion()); - Assertions.assertEquals("owner", config.getOwner()); - Assertions.assertEquals("organization", config.getOrganization()); - Assertions.assertEquals("architecture", config.getArchitecture()); - Assertions.assertEquals("develop", config.getEnvironment()); - Assertions.assertEquals("compiler", config.getCompiler()); - Assertions.assertEquals("log4j", config.getLogger()); - Assertions.assertSame(monitor, config.getMonitor()); - Assertions.assertFalse(config.isDefault()); - Assertions.assertEquals("dumpDirectory", config.getDumpDirectory()); - Assertions.assertTrue(config.getQosEnable()); - Assertions.assertEquals(8080, config.getQosPort()); - Assertions.assertFalse(config.getQosAcceptForeignIp()); - Assertions.assertEquals("shutwait", config.getShutwait()); - Assertions.assertEquals("registryIds", config.getRegistryIds()); - Assertions.assertSame(registry, config.getRegistry()); - Assertions.assertTrue(config.getParameters().containsKey("default.num")); - Assertions.assertEquals("one", config.getParameters().get("default.num")); - - Assertions.assertNotSame(config, config2); - } -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ArgumentBuilderTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ArgumentBuilderTest.java deleted file mode 100644 index bfaff92a9f0..00000000000 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ArgumentBuilderTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.ArgumentConfig; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -class ArgumentBuilderTest { - - @Test - void index() { - ArgumentBuilder builder = new ArgumentBuilder(); - builder.index(1); - Assertions.assertEquals(1, builder.build().getIndex()); - } - - @Test - void type() { - ArgumentBuilder builder = new ArgumentBuilder(); - builder.type("int"); - Assertions.assertEquals("int", builder.build().getType()); - } - - @Test - void callback() { - ArgumentBuilder builder = new ArgumentBuilder(); - builder.callback(true); - Assertions.assertTrue(builder.build().isCallback()); - builder.callback(false); - Assertions.assertFalse(builder.build().isCallback()); - } - - @Test - void build() { - ArgumentBuilder builder = new ArgumentBuilder(); - builder.index(1).type("int").callback(true); - - ArgumentConfig argument1 = builder.build(); - ArgumentConfig argument2 = builder.build(); - - Assertions.assertTrue(argument1.isCallback()); - Assertions.assertEquals("int", argument1.getType()); - Assertions.assertEquals(1, argument1.getIndex()); - - Assertions.assertNotSame(argument1, argument2); - } -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ConfigCenterBuilderTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ConfigCenterBuilderTest.java deleted file mode 100644 index daf1fa817ee..00000000000 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ConfigCenterBuilderTest.java +++ /dev/null @@ -1,177 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.ConfigCenterConfig; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.HashMap; -import java.util.Map; - -class ConfigCenterBuilderTest { - - @Test - void protocol() { - ConfigCenterBuilder builder = new ConfigCenterBuilder(); - builder.protocol("protocol"); - Assertions.assertEquals("protocol", builder.build().getProtocol()); - } - - @Test - void address() { - ConfigCenterBuilder builder = new ConfigCenterBuilder(); - builder.address("address"); - Assertions.assertEquals("address", builder.build().getAddress()); - } - - @Test - void cluster() { - ConfigCenterBuilder builder = new ConfigCenterBuilder(); - builder.cluster("cluster"); - Assertions.assertEquals("cluster", builder.build().getCluster()); - } - - @Test - void namespace() { - ConfigCenterBuilder builder = new ConfigCenterBuilder(); - builder.namespace("namespace"); - Assertions.assertEquals("namespace", builder.build().getNamespace()); - } - - @Test - void group() { - ConfigCenterBuilder builder = new ConfigCenterBuilder(); - builder.group("group"); - Assertions.assertEquals("group", builder.build().getGroup()); - } - - @Test - void username() { - ConfigCenterBuilder builder = new ConfigCenterBuilder(); - builder.username("username"); - Assertions.assertEquals("username", builder.build().getUsername()); - } - - @Test - void password() { - ConfigCenterBuilder builder = new ConfigCenterBuilder(); - builder.password("password"); - Assertions.assertEquals("password", builder.build().getPassword()); - } - - @Test - void timeout() { - ConfigCenterBuilder builder = new ConfigCenterBuilder(); - builder.timeout(1000L); - Assertions.assertEquals(1000L, builder.build().getTimeout()); - } - - @Test - void highestPriority() { - ConfigCenterBuilder builder = new ConfigCenterBuilder(); - builder.highestPriority(true); - Assertions.assertTrue(builder.build().isHighestPriority()); - } - - @Test - void check() { - ConfigCenterBuilder builder = new ConfigCenterBuilder(); - builder.check(true); - Assertions.assertTrue(builder.build().isCheck()); - } - - @Test - void appName() { - ConfigCenterBuilder builder = new ConfigCenterBuilder(); - builder.appName("appName"); - Assertions.assertEquals("appName", builder.build().getAppName()); - } - - @Test - void configFile() { - ConfigCenterBuilder builder = new ConfigCenterBuilder(); - builder.configFile("configFile"); - Assertions.assertEquals("configFile", builder.build().getConfigFile()); - } - - @Test - void appConfigFile() { - ConfigCenterBuilder builder = new ConfigCenterBuilder(); - builder.appConfigFile("appConfigFile"); - Assertions.assertEquals("appConfigFile", builder.build().getAppConfigFile()); - } - - @Test - void appendParameter() { - ConfigCenterBuilder builder = new ConfigCenterBuilder(); - builder.appendParameter("default.num", "one").appendParameter("num", "ONE"); - - Map parameters = builder.build().getParameters(); - - Assertions.assertTrue(parameters.containsKey("default.num")); - Assertions.assertEquals("ONE", parameters.get("num")); - } - - @Test - void appendParameters() { - Map source = new HashMap<>(); - source.put("default.num", "one"); - source.put("num", "ONE"); - - ConfigCenterBuilder builder = new ConfigCenterBuilder(); - builder.appendParameters(source); - - Map parameters = builder.build().getParameters(); - - Assertions.assertTrue(parameters.containsKey("default.num")); - Assertions.assertEquals("ONE", parameters.get("num")); - } - - @Test - void build() { - ConfigCenterBuilder builder = new ConfigCenterBuilder(); - builder.check(true).protocol("protocol").address("address").appConfigFile("appConfigFile").appName("appName") - .cluster("cluster").configFile("configFile").group("group").highestPriority(false) - .namespace("namespace").password("password").timeout(1000L).username("usernama") - .appendParameter("default.num", "one").id("id").prefix("prefix"); - - ConfigCenterConfig config = builder.build(); - ConfigCenterConfig config2 = builder.build(); - - Assertions.assertTrue(config.isCheck()); - Assertions.assertFalse(config.isHighestPriority()); - Assertions.assertEquals(1000L, config.getTimeout()); - Assertions.assertEquals("protocol", config.getProtocol()); - Assertions.assertEquals("address", config.getAddress()); - Assertions.assertEquals("appConfigFile", config.getAppConfigFile()); - Assertions.assertEquals("appName", config.getAppName()); - Assertions.assertEquals("cluster", config.getCluster()); - Assertions.assertEquals("configFile", config.getConfigFile()); - Assertions.assertEquals("group", config.getGroup()); - Assertions.assertEquals("namespace", config.getNamespace()); - Assertions.assertEquals("password", config.getPassword()); - Assertions.assertEquals("usernama", config.getUsername()); - Assertions.assertTrue(config.getParameters().containsKey("default.num")); - Assertions.assertEquals("one", config.getParameters().get("default.num")); - Assertions.assertEquals("id", config.getId()); - Assertions.assertEquals("prefix", config.getPrefix()); - - Assertions.assertNotSame(config, config2); - } -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ConsumerBuilderTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ConsumerBuilderTest.java deleted file mode 100644 index e054bedc63d..00000000000 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ConsumerBuilderTest.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.ConsumerConfig; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -class ConsumerBuilderTest { - - @Test - void isDefault() { - ConsumerBuilder builder = new ConsumerBuilder(); - builder.isDefault(false); - Assertions.assertFalse(builder.build().isDefault()); - } - - @Test - void client() { - ConsumerBuilder builder = new ConsumerBuilder(); - builder.client("client"); - Assertions.assertEquals("client", builder.build().getClient()); - } - - @Test - void threadPool() { - ConsumerBuilder builder = new ConsumerBuilder(); - builder.threadPool("threadPool"); - Assertions.assertEquals("threadPool", builder.build().getThreadpool()); - } - - @Test - void coreThreads() { - ConsumerBuilder builder = new ConsumerBuilder(); - builder.coreThreads(10); - Assertions.assertEquals(10, builder.build().getCorethreads()); - } - - @Test - void threads() { - ConsumerBuilder builder = new ConsumerBuilder(); - builder.threads(100); - Assertions.assertEquals(100, builder.build().getThreads()); - } - - @Test - void queues() { - ConsumerBuilder builder = new ConsumerBuilder(); - builder.queues(200); - Assertions.assertEquals(200, builder.build().getQueues()); - } - - @Test - void shareConnections() { - ConsumerBuilder builder = new ConsumerBuilder(); - builder.shareConnections(300); - Assertions.assertEquals(300, builder.build().getShareconnections()); - } - - @Test - void build() { - ConsumerBuilder builder = new ConsumerBuilder(); - builder.isDefault(true).client("client").threadPool("threadPool").coreThreads(10).threads(100).queues(200) - .shareConnections(300).id("id").prefix("prefix"); - - ConsumerConfig config = builder.build(); - ConsumerConfig config2 = builder.build(); - - Assertions.assertTrue(config.isDefault()); - Assertions.assertEquals("client", config.getClient()); - Assertions.assertEquals("threadPool", config.getThreadpool()); - Assertions.assertEquals("id", config.getId()); - Assertions.assertEquals("prefix", config.getPrefix()); - Assertions.assertEquals(10, config.getCorethreads()); - Assertions.assertEquals(100, config.getThreads()); - Assertions.assertEquals(200, config.getQueues()); - Assertions.assertEquals(300, config.getShareconnections()); - Assertions.assertNotSame(config, config2); - } -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/MetadataReportBuilderTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/MetadataReportBuilderTest.java deleted file mode 100644 index 2e9ee789ea7..00000000000 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/MetadataReportBuilderTest.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.MetadataReportConfig; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.HashMap; -import java.util.Map; - -class MetadataReportBuilderTest { - - @Test - void address() { - MetadataReportBuilder builder = new MetadataReportBuilder(); - builder.address("address"); - Assertions.assertEquals("address", builder.build().getAddress()); - } - - @Test - void username() { - MetadataReportBuilder builder = new MetadataReportBuilder(); - builder.username("username"); - Assertions.assertEquals("username", builder.build().getUsername()); - } - - @Test - void password() { - MetadataReportBuilder builder = new MetadataReportBuilder(); - builder.password("password"); - Assertions.assertEquals("password", builder.build().getPassword()); - } - - @Test - void timeout() { - MetadataReportBuilder builder = new MetadataReportBuilder(); - builder.timeout(1000); - Assertions.assertEquals(1000, builder.build().getTimeout()); - } - - @Test - void group() { - MetadataReportBuilder builder = new MetadataReportBuilder(); - builder.group("group"); - Assertions.assertEquals("group", builder.build().getGroup()); - } - - @Test - void appendParameter() { - MetadataReportBuilder builder = new MetadataReportBuilder(); - builder.appendParameter("default.num", "one").appendParameter("num", "ONE"); - - Map parameters = builder.build().getParameters(); - - Assertions.assertTrue(parameters.containsKey("default.num")); - Assertions.assertEquals("ONE", parameters.get("num")); - } - - @Test - void appendParameters() { - Map source = new HashMap<>(); - source.put("default.num", "one"); - source.put("num", "ONE"); - - MetadataReportBuilder builder = new MetadataReportBuilder(); - builder.appendParameters(source); - - Map parameters = builder.build().getParameters(); - - Assertions.assertTrue(parameters.containsKey("default.num")); - Assertions.assertEquals("ONE", parameters.get("num")); - } - - @Test - void retryTimes() { - MetadataReportBuilder builder = new MetadataReportBuilder(); - builder.retryTimes(1); - Assertions.assertEquals(1, builder.build().getRetryTimes()); - } - - @Test - void retryPeriod() { - MetadataReportBuilder builder = new MetadataReportBuilder(); - builder.retryPeriod(2); - Assertions.assertEquals(2, builder.build().getRetryPeriod()); - } - - @Test - void cycleReport() { - MetadataReportBuilder builder = new MetadataReportBuilder(); - builder.cycleReport(true); - Assertions.assertTrue(builder.build().getCycleReport()); - builder.cycleReport(false); - Assertions.assertFalse(builder.build().getCycleReport()); - builder.cycleReport(null); - Assertions.assertNull(builder.build().getCycleReport()); - } - - @Test - void syncReport() { - MetadataReportBuilder builder = new MetadataReportBuilder(); - builder.syncReport(true); - Assertions.assertTrue(builder.build().getSyncReport()); - builder.syncReport(false); - Assertions.assertFalse(builder.build().getSyncReport()); - builder.syncReport(null); - Assertions.assertNull(builder.build().getSyncReport()); - } - - @Test - void build() { - MetadataReportBuilder builder = new MetadataReportBuilder(); - builder.address("address").username("username").password("password").timeout(1000).group("group") - .retryTimes(1).retryPeriod(2).cycleReport(true).syncReport(false) - .appendParameter("default.num", "one").id("id").prefix("prefix"); - - MetadataReportConfig config = builder.build(); - MetadataReportConfig config2 = builder.build(); - - Assertions.assertTrue(config.getCycleReport()); - Assertions.assertFalse(config.getSyncReport()); - Assertions.assertEquals(1000, config.getTimeout()); - Assertions.assertEquals(1, config.getRetryTimes()); - Assertions.assertEquals(2, config.getRetryPeriod()); - Assertions.assertEquals("address", config.getAddress()); - Assertions.assertEquals("username", config.getUsername()); - Assertions.assertEquals("password", config.getPassword()); - Assertions.assertEquals("group", config.getGroup()); - Assertions.assertTrue(config.getParameters().containsKey("default.num")); - Assertions.assertEquals("one", config.getParameters().get("default.num")); - Assertions.assertEquals("id", config.getId()); - Assertions.assertEquals("prefix", config.getPrefix()); - Assertions.assertNotSame(config, config2); - } -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/MethodBuilderTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/MethodBuilderTest.java deleted file mode 100644 index 5bf0beeb1f8..00000000000 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/MethodBuilderTest.java +++ /dev/null @@ -1,189 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.ArgumentConfig; -import org.apache.dubbo.config.MethodConfig; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.Collections; - -class MethodBuilderTest { - - @Test - void name() { - MethodBuilder builder = new MethodBuilder(); - builder.name("name"); - Assertions.assertEquals("name", builder.build().getName()); - } - - @Test - void stat() { - MethodBuilder builder = new MethodBuilder(); - builder.stat(1); - Assertions.assertEquals(1, builder.build().getStat()); - } - - @Test - void retry() { - MethodBuilder builder = new MethodBuilder(); - builder.retry(true); - Assertions.assertTrue(builder.build().isRetry()); - } - - @Test - void reliable() { - MethodBuilder builder = new MethodBuilder(); - builder.reliable(true); - Assertions.assertTrue(builder.build().isReliable()); - } - - @Test - void executes() { - MethodBuilder builder = new MethodBuilder(); - builder.executes(1); - Assertions.assertEquals(1, builder.build().getExecutes()); - } - - @Test - void deprecated() { - MethodBuilder builder = new MethodBuilder(); - builder.deprecated(true); - Assertions.assertTrue(builder.build().getDeprecated()); - } - - @Test - void sticky() { - MethodBuilder builder = new MethodBuilder(); - builder.sticky(true); - Assertions.assertTrue(builder.build().getSticky()); - } - - @Test - void isReturn() { - MethodBuilder builder = new MethodBuilder(); - builder.isReturn(true); - Assertions.assertTrue(builder.build().isReturn()); - } - - @Test - void oninvoke() { - MethodBuilder builder = new MethodBuilder(); - builder.oninvoke("on-invoke-object"); - Assertions.assertEquals("on-invoke-object", builder.build().getOninvoke()); - } - - @Test - void oninvokeMethod() { - MethodBuilder builder = new MethodBuilder(); - builder.oninvokeMethod("on-invoke-method"); - Assertions.assertEquals("on-invoke-method", builder.build().getOninvokeMethod()); - } - - @Test - void onreturn() { - MethodBuilder builder = new MethodBuilder(); - builder.onreturn("on-return-object"); - Assertions.assertEquals("on-return-object", builder.build().getOnreturn()); - } - - @Test - void onreturnMethod() { - MethodBuilder builder = new MethodBuilder(); - builder.onreturnMethod("on-return-method"); - Assertions.assertEquals("on-return-method", builder.build().getOnreturnMethod()); - } - - @Test - void onthrow() { - MethodBuilder builder = new MethodBuilder(); - builder.onthrow("on-throw-object"); - Assertions.assertEquals("on-throw-object", builder.build().getOnthrow()); - } - - @Test - void onthrowMethod() { - MethodBuilder builder = new MethodBuilder(); - builder.onthrowMethod("on-throw-method"); - Assertions.assertEquals("on-throw-method", builder.build().getOnthrowMethod()); - } - - @Test - void addArguments() { - ArgumentConfig argument = new ArgumentConfig(); - MethodBuilder builder = new MethodBuilder(); - builder.addArguments(Collections.singletonList(argument)); - Assertions.assertTrue(builder.build().getArguments().contains(argument)); - Assertions.assertEquals(1, builder.build().getArguments().size()); - } - - @Test - void addArgument() { - ArgumentConfig argument = new ArgumentConfig(); - MethodBuilder builder = new MethodBuilder(); - builder.addArgument(argument); - Assertions.assertTrue(builder.build().getArguments().contains(argument)); - Assertions.assertEquals(1, builder.build().getArguments().size()); - } - - @Test - void service() { - MethodBuilder builder = new MethodBuilder(); - builder.service("service"); - Assertions.assertEquals("service", builder.build().getService()); - } - - @Test - void serviceId() { - MethodBuilder builder = new MethodBuilder(); - builder.serviceId("serviceId"); - Assertions.assertEquals("serviceId", builder.build().getServiceId()); - } - - @Test - void build() { - ArgumentConfig argument = new ArgumentConfig(); - MethodBuilder builder = new MethodBuilder(); - builder.name("name").stat(1).retry(true).reliable(false).executes(2).deprecated(true).sticky(false) - .isReturn(true).oninvoke("on-invoke-object").oninvokeMethod("on-invoke-method").service("service") - .onreturn("on-return-object").onreturnMethod("on-return-method").serviceId("serviceId") - .onthrow("on-throw-object").onthrowMethod("on-throw-method").addArgument(argument); - - MethodConfig config = builder.build(); - MethodConfig config2 = builder.build(); - - Assertions.assertTrue(config.isRetry()); - Assertions.assertFalse(config.isReliable()); - Assertions.assertTrue(config.getDeprecated()); - Assertions.assertFalse(config.getSticky()); - Assertions.assertTrue(config.isReturn()); - Assertions.assertEquals(1, config.getStat()); - Assertions.assertEquals(2, config.getExecutes()); - Assertions.assertEquals("on-invoke-object", config.getOninvoke()); - Assertions.assertEquals("on-invoke-method", config.getOninvokeMethod()); - Assertions.assertEquals("on-return-object", config.getOnreturn()); - Assertions.assertEquals("on-return-method", config.getOnreturnMethod()); - Assertions.assertEquals("on-throw-object", config.getOnthrow()); - Assertions.assertEquals("on-throw-method", config.getOnthrowMethod()); - Assertions.assertEquals("name", config.getName()); - Assertions.assertEquals("service", config.getService()); - Assertions.assertEquals("serviceId", config.getServiceId()); - Assertions.assertNotSame(config, config2); - } -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ModuleBuilderTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ModuleBuilderTest.java deleted file mode 100644 index 8bad86a9682..00000000000 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ModuleBuilderTest.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.ModuleConfig; -import org.apache.dubbo.config.MonitorConfig; -import org.apache.dubbo.config.RegistryConfig; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.Collections; - -class ModuleBuilderTest { - - @Test - void name() { - ModuleBuilder builder = new ModuleBuilder(); - builder.name("name"); - Assertions.assertEquals("name", builder.build().getName()); - } - - @Test - void version() { - ModuleBuilder builder = new ModuleBuilder(); - builder.version("version"); - Assertions.assertEquals("version", builder.build().getVersion()); - } - - @Test - void owner() { - ModuleBuilder builder = new ModuleBuilder(); - builder.owner("owner"); - Assertions.assertEquals("owner", builder.build().getOwner()); - } - - @Test - void organization() { - ModuleBuilder builder = new ModuleBuilder(); - builder.organization("organization"); - Assertions.assertEquals("organization", builder.build().getOrganization()); - } - - @Test - void addRegistries() { - RegistryConfig registry = new RegistryConfig(); - ModuleBuilder builder = new ModuleBuilder(); - builder.addRegistries(Collections.singletonList(registry)); - Assertions.assertTrue(builder.build().getRegistries().contains(registry)); - Assertions.assertEquals(1, builder.build().getRegistries().size()); - } - - @Test - void addRegistry() { - RegistryConfig registry = new RegistryConfig(); - ModuleBuilder builder = new ModuleBuilder(); - builder.addRegistry(registry); - Assertions.assertTrue(builder.build().getRegistries().contains(registry)); - Assertions.assertEquals(1, builder.build().getRegistries().size()); - } - - @Test - void monitor() { - MonitorConfig monitor = new MonitorConfig(); - ModuleBuilder builder = new ModuleBuilder(); - builder.monitor(monitor); - Assertions.assertSame(monitor, builder.build().getMonitor()); - } - - @Test - void isDefault() { - ModuleBuilder builder = new ModuleBuilder(); - builder.isDefault(true); - Assertions.assertTrue(builder.build().isDefault()); - } - - @Test - void build() { - RegistryConfig registry = new RegistryConfig(); - MonitorConfig monitor = new MonitorConfig(); - - ModuleBuilder builder = new ModuleBuilder(); - builder.name("name").version("version").owner("owner").organization("organization").addRegistry(registry) - .monitor(monitor).isDefault(false); - - ModuleConfig config = builder.build(); - ModuleConfig config2 = builder.build(); - - Assertions.assertEquals("name", config.getName()); - Assertions.assertEquals("version", config.getVersion()); - Assertions.assertEquals("owner", config.getOwner()); - Assertions.assertEquals("organization", config.getOrganization()); - Assertions.assertTrue(builder.build().getRegistries().contains(registry)); - Assertions.assertSame(monitor, builder.build().getMonitor()); - Assertions.assertFalse(config.isDefault()); - Assertions.assertNotSame(config, config2); - } -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/MonitorBuilderTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/MonitorBuilderTest.java deleted file mode 100644 index 71fc43e2a15..00000000000 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/MonitorBuilderTest.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.MonitorConfig; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.HashMap; -import java.util.Map; - -class MonitorBuilderTest { - - @Test - void protocol() { - MonitorBuilder builder = new MonitorBuilder(); - builder.protocol("protocol"); - Assertions.assertEquals("protocol", builder.build().getProtocol()); - } - - @Test - void address() { - MonitorBuilder builder = new MonitorBuilder(); - builder.address("address"); - Assertions.assertEquals("address", builder.build().getAddress()); - } - - @Test - void username() { - MonitorBuilder builder = new MonitorBuilder(); - builder.username("username"); - Assertions.assertEquals("username", builder.build().getUsername()); - } - - @Test - void password() { - MonitorBuilder builder = new MonitorBuilder(); - builder.password("password"); - Assertions.assertEquals("password", builder.build().getPassword()); - } - - @Test - void group() { - MonitorBuilder builder = new MonitorBuilder(); - builder.group("group"); - Assertions.assertEquals("group", builder.build().getGroup()); - } - - @Test - void version() { - MonitorBuilder builder = new MonitorBuilder(); - builder.version("version"); - Assertions.assertEquals("version", builder.build().getVersion()); - } - - @Test - void interval() { - MonitorBuilder builder = new MonitorBuilder(); - builder.interval("interval"); - Assertions.assertEquals("interval", builder.build().getInterval()); - } - - @Test - void isDefault() { - MonitorBuilder builder = new MonitorBuilder(); - builder.isDefault(true); - Assertions.assertTrue(builder.build().isDefault()); - } - - @Test - void appendParameter() { - MonitorBuilder builder = new MonitorBuilder(); - builder.appendParameter("default.num", "one").appendParameter("num", "ONE"); - - Map parameters = builder.build().getParameters(); - - Assertions.assertTrue(parameters.containsKey("default.num")); - Assertions.assertEquals("ONE", parameters.get("num")); - } - - @Test - void appendParameters() { - Map source = new HashMap<>(); - source.put("default.num", "one"); - source.put("num", "ONE"); - - MonitorBuilder builder = new MonitorBuilder(); - builder.appendParameters(source); - - Map parameters = builder.build().getParameters(); - - Assertions.assertTrue(parameters.containsKey("default.num")); - Assertions.assertEquals("ONE", parameters.get("num")); - } - - @Test - void build() { - MonitorBuilder builder = new MonitorBuilder(); - builder.protocol("protocol").address("address").group("group").interval("interval").isDefault(true) - .password("password").username("username").version("version") - .appendParameter("default.num", "one").id("id").prefix("prefix"); - - MonitorConfig config = builder.build(); - MonitorConfig config2 = builder.build(); - - Assertions.assertEquals("protocol", config.getProtocol()); - Assertions.assertEquals("address", config.getAddress()); - Assertions.assertEquals("group", config.getGroup()); - Assertions.assertEquals("interval", config.getInterval()); - Assertions.assertEquals("password", config.getPassword()); - Assertions.assertEquals("username", config.getUsername()); - Assertions.assertEquals("version", config.getVersion()); - Assertions.assertTrue(config.isDefault()); - Assertions.assertTrue(config.getParameters().containsKey("default.num")); - Assertions.assertEquals("one", config.getParameters().get("default.num")); - Assertions.assertEquals("id", config.getId()); - Assertions.assertEquals("prefix", config.getPrefix()); - Assertions.assertNotSame(config, config2); - } -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ProtocolBuilderTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ProtocolBuilderTest.java deleted file mode 100644 index df3c8c7fdbd..00000000000 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ProtocolBuilderTest.java +++ /dev/null @@ -1,338 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.ProtocolConfig; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.HashMap; -import java.util.Map; - -class ProtocolBuilderTest { - - @Test - void name() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.name("name"); - Assertions.assertEquals("name", builder.build().getName()); - } - - @Test - void host() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.host("host"); - Assertions.assertEquals("host", builder.build().getHost()); - } - - @Test - void port() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.port(8080); - Assertions.assertEquals(8080, builder.build().getPort()); - } - - @Test - void contextpath() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.contextpath("contextpath"); - Assertions.assertEquals("contextpath", builder.build().getContextpath()); - } - - @Test - void path() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.path("path"); - Assertions.assertEquals("path", builder.build().getPath()); - } - - @Test - void threadpool() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.threadpool("mockthreadpool"); - Assertions.assertEquals("mockthreadpool", builder.build().getThreadpool()); - } - - @Test - void corethreads() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.corethreads(10); - Assertions.assertEquals(10, builder.build().getCorethreads()); - } - - @Test - void threads() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.threads(20); - Assertions.assertEquals(20, builder.build().getThreads()); - } - - @Test - void iothreads() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.iothreads(25); - Assertions.assertEquals(25, builder.build().getIothreads()); - } - - @Test - void queues() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.queues(30); - Assertions.assertEquals(30, builder.build().getQueues()); - } - - @Test - void accepts() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.accepts(35); - Assertions.assertEquals(35, builder.build().getAccepts()); - } - - @Test - void codec() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.codec("mockcodec"); - Assertions.assertEquals("mockcodec", builder.build().getCodec()); - } - - @Test - void serialization() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.serialization("serialization"); - Assertions.assertEquals("serialization", builder.build().getSerialization()); - } - - @Test - void charset() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.charset("utf-8"); - Assertions.assertEquals("utf-8", builder.build().getCharset()); - } - - @Test - void payload() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.payload(40); - Assertions.assertEquals(40, builder.build().getPayload()); - } - - @Test - void buffer() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.buffer(1024); - Assertions.assertEquals(1024, builder.build().getBuffer()); - } - - @Test - void heartbeat() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.heartbeat(1000); - Assertions.assertEquals(1000, builder.build().getHeartbeat()); - } - - @Test - void accesslog() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.accesslog("accesslog"); - Assertions.assertEquals("accesslog", builder.build().getAccesslog()); - } - - @Test - void transporter() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.transporter("mocktransporter"); - Assertions.assertEquals("mocktransporter", builder.build().getTransporter()); - } - - @Test - void exchanger() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.exchanger("mockexchanger"); - Assertions.assertEquals("mockexchanger", builder.build().getExchanger()); - } - - @Test - void dispatcher() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.dispatcher("mockdispatcher"); - Assertions.assertEquals("mockdispatcher", builder.build().getDispatcher()); - } - - @Test - void dispather() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.dispather("mockdispatcher"); - Assertions.assertEquals("mockdispatcher", builder.build().getDispather()); - } - - @Test - void networker() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.networker("networker"); - Assertions.assertEquals("networker", builder.build().getNetworker()); - } - - @Test - void server() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.server("server"); - Assertions.assertEquals("server", builder.build().getServer()); - } - - @Test - void client() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.client("client"); - Assertions.assertEquals("client", builder.build().getClient()); - } - - @Test - void telnet() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.telnet("mocktelnethandler"); - Assertions.assertEquals("mocktelnethandler", builder.build().getTelnet()); - } - - @Test - void prompt() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.prompt("prompt"); - Assertions.assertEquals("prompt", builder.build().getPrompt()); - } - - @Test - void status() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.status("mockstatuschecker"); - Assertions.assertEquals("mockstatuschecker", builder.build().getStatus()); - } - - @Test - void register() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.register(true); - Assertions.assertTrue(builder.build().isRegister()); - } - - @Test - void keepAlive() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.keepAlive(true); - Assertions.assertTrue(builder.build().getKeepAlive()); - } - - @Test - void optimizer() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.optimizer("optimizer"); - Assertions.assertEquals("optimizer", builder.build().getOptimizer()); - } - - @Test - void extension() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.extension("extension"); - Assertions.assertEquals("extension", builder.build().getExtension()); - } - - @Test - void appendParameter() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.appendParameter("default.num", "one").appendParameter("num", "ONE"); - - Map parameters = builder.build().getParameters(); - - Assertions.assertTrue(parameters.containsKey("default.num")); - Assertions.assertEquals("ONE", parameters.get("num")); - } - - @Test - void appendParameters() { - Map source = new HashMap<>(); - source.put("default.num", "one"); - source.put("num", "ONE"); - - ProtocolBuilder builder = new ProtocolBuilder(); - builder.appendParameters(source); - - Map parameters = builder.build().getParameters(); - - Assertions.assertTrue(parameters.containsKey("default.num")); - Assertions.assertEquals("ONE", parameters.get("num")); - } - - @Test - void isDefault() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.isDefault(true); - Assertions.assertTrue(builder.build().isDefault()); - } - - @Test - void build() { - ProtocolBuilder builder = new ProtocolBuilder(); - builder.name("name").host("host").port(8080).contextpath("contextpath").threadpool("mockthreadpool") - .corethreads(1).threads(2).iothreads(3).queues(4).accepts(5).codec("mockcodec") - .serialization("serialization").charset("utf-8").payload(6).buffer(1024).heartbeat(1000) - .accesslog("accesslog").transporter("mocktransporter").exchanger("mockexchanger") - .dispatcher("mockdispatcher").networker("networker").server("server").client("client") - .telnet("mocktelnethandler").prompt("prompt").status("mockstatuschecker").register(true).keepAlive(false) - .optimizer("optimizer").extension("extension").isDefault(true) - .appendParameter("default.num", "one").id("id").prefix("prefix"); - - ProtocolConfig config = builder.build(); - ProtocolConfig config2 = builder.build(); - - Assertions.assertEquals(8080, config.getPort()); - Assertions.assertEquals(1, config.getCorethreads()); - Assertions.assertEquals(2, config.getThreads()); - Assertions.assertEquals(3, config.getIothreads()); - Assertions.assertEquals(4, config.getQueues()); - Assertions.assertEquals(5, config.getAccepts()); - Assertions.assertEquals(6, config.getPayload()); - Assertions.assertEquals(1024, config.getBuffer()); - Assertions.assertEquals(1000, config.getHeartbeat()); - Assertions.assertEquals("name", config.getName()); - Assertions.assertEquals("host", config.getHost()); - Assertions.assertEquals("contextpath", config.getContextpath()); - Assertions.assertEquals("mockthreadpool", config.getThreadpool()); - Assertions.assertEquals("mockcodec", config.getCodec()); - Assertions.assertEquals("serialization", config.getSerialization()); - Assertions.assertEquals("utf-8", config.getCharset()); - Assertions.assertEquals("accesslog", config.getAccesslog()); - Assertions.assertEquals("mocktransporter", config.getTransporter()); - Assertions.assertEquals("mockexchanger", config.getExchanger()); - Assertions.assertEquals("mockdispatcher", config.getDispatcher()); - Assertions.assertEquals("networker", config.getNetworker()); - Assertions.assertEquals("server", config.getServer()); - Assertions.assertEquals("client", config.getClient()); - Assertions.assertEquals("mocktelnethandler", config.getTelnet()); - Assertions.assertEquals("prompt", config.getPrompt()); - Assertions.assertEquals("mockstatuschecker", config.getStatus()); - Assertions.assertEquals("optimizer", config.getOptimizer()); - Assertions.assertEquals("extension", config.getExtension()); - Assertions.assertTrue(config.isRegister()); - Assertions.assertFalse(config.getKeepAlive()); - Assertions.assertTrue(config.isDefault()); - Assertions.assertTrue(config.getParameters().containsKey("default.num")); - Assertions.assertEquals("one", config.getParameters().get("default.num")); - Assertions.assertEquals("id", config.getId()); - Assertions.assertEquals("prefix", config.getPrefix()); - Assertions.assertNotSame(config, config2); - } -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ProviderBuilderTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ProviderBuilderTest.java deleted file mode 100644 index 9d3dfb152f9..00000000000 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ProviderBuilderTest.java +++ /dev/null @@ -1,227 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.ProviderConfig; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -class ProviderBuilderTest { - - @Test - void setHost() { - ProviderBuilder builder = new ProviderBuilder(); - builder.host("host"); - Assertions.assertEquals("host", builder.build().getHost()); - } - - @Test - void port() { - ProviderBuilder builder = new ProviderBuilder(); - builder.port(8080); - Assertions.assertEquals(8080, builder.build().getPort()); - } - - @Test - void contextPath() { - ProviderBuilder builder = new ProviderBuilder(); - builder.contextPath("contextpath"); - Assertions.assertEquals("contextpath", builder.build().getContextpath()); - } - - @Test - void threadPool() { - ProviderBuilder builder = new ProviderBuilder(); - builder.threadPool("mockthreadpool"); - Assertions.assertEquals("mockthreadpool", builder.build().getThreadpool()); - } - - @Test - void threads() { - ProviderBuilder builder = new ProviderBuilder(); - builder.threads(20); - Assertions.assertEquals(20, builder.build().getThreads()); - } - - @Test - void ioThreads() { - ProviderBuilder builder = new ProviderBuilder(); - builder.ioThreads(25); - Assertions.assertEquals(25, builder.build().getIothreads()); - } - - @Test - void queues() { - ProviderBuilder builder = new ProviderBuilder(); - builder.queues(30); - Assertions.assertEquals(30, builder.build().getQueues()); - } - - @Test - void accepts() { - ProviderBuilder builder = new ProviderBuilder(); - builder.accepts(35); - Assertions.assertEquals(35, builder.build().getAccepts()); - } - - @Test - void codec() { - ProviderBuilder builder = new ProviderBuilder(); - builder.codec("mockcodec"); - Assertions.assertEquals("mockcodec", builder.build().getCodec()); - } - - @Test - void charset() { - ProviderBuilder builder = new ProviderBuilder(); - builder.charset("utf-8"); - Assertions.assertEquals("utf-8", builder.build().getCharset()); - } - - @Test - void payload() { - ProviderBuilder builder = new ProviderBuilder(); - builder.payload(40); - Assertions.assertEquals(40, builder.build().getPayload()); - } - - @Test - void buffer() { - ProviderBuilder builder = new ProviderBuilder(); - builder.buffer(1024); - Assertions.assertEquals(1024, builder.build().getBuffer()); - } - - @Test - void transporter() { - ProviderBuilder builder = new ProviderBuilder(); - builder.transporter("mocktransporter"); - Assertions.assertEquals("mocktransporter", builder.build().getTransporter()); - } - - @Test - void exchanger() { - ProviderBuilder builder = new ProviderBuilder(); - builder.exchanger("mockexchanger"); - Assertions.assertEquals("mockexchanger", builder.build().getExchanger()); - } - - @Test - void dispatcher() { - ProviderBuilder builder = new ProviderBuilder(); - builder.dispatcher("mockdispatcher"); - Assertions.assertEquals("mockdispatcher", builder.build().getDispatcher()); - } - - @Test - void networker() { - ProviderBuilder builder = new ProviderBuilder(); - builder.networker("networker"); - Assertions.assertEquals("networker", builder.build().getNetworker()); - } - - @Test - void server() { - ProviderBuilder builder = new ProviderBuilder(); - builder.server("server"); - Assertions.assertEquals("server", builder.build().getServer()); - } - - @Test - void client() { - ProviderBuilder builder = new ProviderBuilder(); - builder.client("client"); - Assertions.assertEquals("client", builder.build().getClient()); - } - - @Test - void telnet() { - ProviderBuilder builder = new ProviderBuilder(); - builder.telnet("mocktelnethandler"); - Assertions.assertEquals("mocktelnethandler", builder.build().getTelnet()); - } - - @Test - void prompt() { - ProviderBuilder builder = new ProviderBuilder(); - builder.prompt("prompt"); - Assertions.assertEquals("prompt", builder.build().getPrompt()); - } - - @Test - void status() { - ProviderBuilder builder = new ProviderBuilder(); - builder.status("mockstatuschecker"); - Assertions.assertEquals("mockstatuschecker", builder.build().getStatus()); - } - - @Test - void Wait() { - ProviderBuilder builder = new ProviderBuilder(); - builder.wait(Integer.valueOf(1000)); - Assertions.assertEquals(1000, builder.build().getWait()); - } - - @Test - void isDefault() { - ProviderBuilder builder = new ProviderBuilder(); - builder.isDefault(true); - Assertions.assertTrue(builder.build().isDefault()); - } - - @Test - void build() { - ProviderBuilder builder = new ProviderBuilder(); - builder.host("host").port(8080).contextPath("contextpath").threadPool("mockthreadpool") - .threads(2).ioThreads(3).queues(4).accepts(5).codec("mockcodec") - .charset("utf-8").payload(6).buffer(1024).transporter("mocktransporter").exchanger("mockexchanger") - .dispatcher("mockdispatcher").networker("networker").server("server").client("client") - .telnet("mocktelnethandler").prompt("prompt").status("mockstatuschecker").wait(Integer.valueOf(1000)) - .isDefault(true).id("id").prefix("prefix"); - - ProviderConfig config = builder.build(); - ProviderConfig config2 = builder.build(); - - Assertions.assertEquals(8080, config.getPort()); - Assertions.assertEquals(2, config.getThreads()); - Assertions.assertEquals(3, config.getIothreads()); - Assertions.assertEquals(4, config.getQueues()); - Assertions.assertEquals(5, config.getAccepts()); - Assertions.assertEquals(6, config.getPayload()); - Assertions.assertEquals(1024, config.getBuffer()); - Assertions.assertEquals(1000, config.getWait()); - Assertions.assertEquals("host", config.getHost()); - Assertions.assertEquals("contextpath", config.getContextpath()); - Assertions.assertEquals("mockthreadpool", config.getThreadpool()); - Assertions.assertEquals("mockcodec", config.getCodec()); - Assertions.assertEquals("utf-8", config.getCharset()); - Assertions.assertEquals("mocktransporter", config.getTransporter()); - Assertions.assertEquals("mockexchanger", config.getExchanger()); - Assertions.assertEquals("mockdispatcher", config.getDispatcher()); - Assertions.assertEquals("networker", config.getNetworker()); - Assertions.assertEquals("server", config.getServer()); - Assertions.assertEquals("client", config.getClient()); - Assertions.assertEquals("mocktelnethandler", config.getTelnet()); - Assertions.assertEquals("prompt", config.getPrompt()); - Assertions.assertEquals("mockstatuschecker", config.getStatus()); - Assertions.assertTrue(config.isDefault()); - Assertions.assertEquals("id", config.getId()); - Assertions.assertEquals("prefix", config.getPrefix()); - Assertions.assertNotSame(config, config2); - } -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ReferenceBuilderTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ReferenceBuilderTest.java deleted file mode 100644 index 0a8ec039bc1..00000000000 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ReferenceBuilderTest.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.ConsumerConfig; -import org.apache.dubbo.config.MethodConfig; -import org.apache.dubbo.config.ReferenceConfig; -import org.apache.dubbo.config.api.DemoService; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.Collections; - -class ReferenceBuilderTest { - - @Test - void interfaceName() { - ReferenceBuilder builder = new ReferenceBuilder(); - builder.interfaceName(DemoService.class.getName()); - Assertions.assertEquals("org.apache.dubbo.config.api.DemoService", builder.build().getInterface()); - } - - @Test - void interfaceClass() { - ReferenceBuilder builder = new ReferenceBuilder(); - builder.interfaceClass(DemoService.class); - Assertions.assertEquals(DemoService.class, builder.build().getInterfaceClass()); - } - - @Test - void client() { - ReferenceBuilder builder = new ReferenceBuilder(); - builder.client("client"); - Assertions.assertEquals("client", builder.build().getClient()); - } - - @Test - void url() { - ReferenceBuilder builder = new ReferenceBuilder(); - builder.url("url"); - Assertions.assertEquals("url", builder.build().getUrl()); - } - - @Test - void addMethods() { - MethodConfig method = new MethodConfig(); - ReferenceBuilder builder = new ReferenceBuilder(); - builder.addMethods(Collections.singletonList(method)); - Assertions.assertTrue(builder.build().getMethods().contains(method)); - Assertions.assertEquals(1, builder.build().getMethods().size()); - } - - @Test - void addMethod() { - MethodConfig method = new MethodConfig(); - ReferenceBuilder builder = new ReferenceBuilder(); - builder.addMethod(method); - Assertions.assertTrue(builder.build().getMethods().contains(method)); - Assertions.assertEquals(1, builder.build().getMethods().size()); - } - - @Test - void consumer() { - ConsumerConfig consumer = new ConsumerConfig(); - ReferenceBuilder builder = new ReferenceBuilder(); - builder.consumer(consumer); - Assertions.assertSame(consumer, builder.build().getConsumer()); - } - - @Test - void protocol() { - ReferenceBuilder builder = new ReferenceBuilder(); - builder.protocol("protocol"); - Assertions.assertEquals("protocol", builder.build().getProtocol()); - } - - @Test - void build() { - ConsumerConfig consumer = new ConsumerConfig(); - MethodConfig method = new MethodConfig(); - - ReferenceBuilder builder = new ReferenceBuilder<>(); - builder.id("id").interfaceClass(DemoService.class).protocol("protocol").client("client").url("url") - .consumer(consumer).addMethod(method); - - ReferenceConfig config = builder.build(); - ReferenceConfig config2 = builder.build(); - - Assertions.assertEquals("org.apache.dubbo.config.api.DemoService", config.getInterface()); - Assertions.assertEquals(DemoService.class, config.getInterfaceClass()); - Assertions.assertEquals("protocol", config.getProtocol()); - Assertions.assertEquals("client", config.getClient()); - Assertions.assertEquals("url", config.getUrl()); - Assertions.assertEquals(consumer, config.getConsumer()); - Assertions.assertTrue(config.getMethods().contains(method)); - Assertions.assertEquals(1, config.getMethods().size()); - Assertions.assertNotSame(config, config2); - } -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/RegistryBuilderTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/RegistryBuilderTest.java deleted file mode 100644 index c676b1d2f02..00000000000 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/RegistryBuilderTest.java +++ /dev/null @@ -1,256 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.RegistryConfig; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.HashMap; -import java.util.Map; - -class RegistryBuilderTest { - - @Test - void address() { - RegistryBuilder builder = new RegistryBuilder(); - builder.address("address"); - Assertions.assertEquals("address", builder.build().getAddress()); - } - - @Test - void username() { - RegistryBuilder builder = new RegistryBuilder(); - builder.username("username"); - Assertions.assertEquals("username", builder.build().getUsername()); - } - - @Test - void password() { - RegistryBuilder builder = new RegistryBuilder(); - builder.password("password"); - Assertions.assertEquals("password", builder.build().getPassword()); - } - - @Test - void port() { - RegistryBuilder builder = new RegistryBuilder(); - builder.port(8080); - Assertions.assertEquals(8080, builder.build().getPort()); - } - - @Test - void protocol() { - RegistryBuilder builder = new RegistryBuilder(); - builder.protocol("protocol"); - Assertions.assertEquals("protocol", builder.build().getProtocol()); - } - - @Test - void transporter() { - RegistryBuilder builder = new RegistryBuilder(); - builder.transporter("transporter"); - Assertions.assertEquals("transporter", builder.build().getTransporter()); - } - - @Test - void transport() { - RegistryBuilder builder = new RegistryBuilder(); - builder.transport("transport"); - Assertions.assertEquals("transport", builder.build().getTransport()); - } - - @Test - void server() { - RegistryBuilder builder = new RegistryBuilder(); - builder.server("server"); - Assertions.assertEquals("server", builder.build().getServer()); - } - - @Test - void client() { - RegistryBuilder builder = new RegistryBuilder(); - builder.client("client"); - Assertions.assertEquals("client", builder.build().getClient()); - } - - @Test - void cluster() { - RegistryBuilder builder = new RegistryBuilder(); - builder.cluster("cluster"); - Assertions.assertEquals("cluster", builder.build().getCluster()); - } - - @Test - void group() { - RegistryBuilder builder = new RegistryBuilder(); - builder.group("group"); - Assertions.assertEquals("group", builder.build().getGroup()); - } - - @Test - void version() { - RegistryBuilder builder = new RegistryBuilder(); - builder.version("version"); - Assertions.assertEquals("version", builder.build().getVersion()); - } - - @Test - void timeout() { - RegistryBuilder builder = new RegistryBuilder(); - builder.timeout(1000); - Assertions.assertEquals(1000, builder.build().getTimeout()); - } - - @Test - void session() { - RegistryBuilder builder = new RegistryBuilder(); - builder.session(2000); - Assertions.assertEquals(2000, builder.build().getSession()); - } - - @Test - void file() { - RegistryBuilder builder = new RegistryBuilder(); - builder.file("file"); - Assertions.assertEquals("file", builder.build().getFile()); - } - - @Test - void testWait() { - RegistryBuilder builder = new RegistryBuilder(); - builder.wait(Integer.valueOf(1000)); - Assertions.assertEquals(1000, builder.build().getWait()); - } - - @Test - void isCheck() { - RegistryBuilder builder = new RegistryBuilder(); - builder.isCheck(true); - Assertions.assertTrue(builder.build().isCheck()); - } - - @Test - void isDynamic() { - RegistryBuilder builder = new RegistryBuilder(); - builder.isDynamic(true); - Assertions.assertTrue(builder.build().isDynamic()); - } - - @Test - void register() { - RegistryBuilder builder = new RegistryBuilder(); - builder.register(true); - Assertions.assertTrue(builder.build().isRegister()); - } - - @Test - void subscribe() { - RegistryBuilder builder = new RegistryBuilder(); - builder.subscribe(true); - Assertions.assertTrue(builder.build().isSubscribe()); - } - - @Test - void appendParameter() { - RegistryBuilder builder = new RegistryBuilder(); - builder.appendParameter("default.num", "one").appendParameter("num", "ONE"); - - Map parameters = builder.build().getParameters(); - - Assertions.assertTrue(parameters.containsKey("default.num")); - Assertions.assertEquals("ONE", parameters.get("num")); - } - - @Test - void appendParameters() { - Map source = new HashMap<>(); - source.put("default.num", "one"); - source.put("num", "ONE"); - - RegistryBuilder builder = new RegistryBuilder(); - builder.appendParameters(source); - - Map parameters = builder.build().getParameters(); - - Assertions.assertTrue(parameters.containsKey("default.num")); - Assertions.assertEquals("ONE", parameters.get("num")); - } - - @Test - void isDefault() { - RegistryBuilder builder = new RegistryBuilder(); - builder.isDefault(true); - Assertions.assertTrue(builder.build().isDefault()); - } - - @Test - void simplified() { - RegistryBuilder builder = new RegistryBuilder(); - builder.simplified(true); - Assertions.assertTrue(builder.build().getSimplified()); - } - - @Test - void extraKeys() { - RegistryBuilder builder = new RegistryBuilder(); - builder.extraKeys("extraKeys"); - Assertions.assertEquals("extraKeys", builder.build().getExtraKeys()); - } - - @Test - void build() { - RegistryBuilder builder = new RegistryBuilder(); - builder.address("address").username("username").password("password").port(8080).protocol("protocol") - .transporter("transporter").server("server").client("client").cluster("cluster").group("group") - .version("version").timeout(1000).session(2000).file("file").wait(Integer.valueOf(10)).isCheck(true) - .isDynamic(false).register(true).subscribe(false).isDefault(true).simplified(false).extraKeys("A") - .appendParameter("default.num", "one").id("id").prefix("prefix"); - - RegistryConfig config = builder.build(); - RegistryConfig config2 = builder.build(); - - Assertions.assertEquals(8080, config.getPort()); - Assertions.assertEquals(1000, config.getTimeout()); - Assertions.assertEquals(2000, config.getSession()); - Assertions.assertEquals(10, config.getWait()); - Assertions.assertTrue(config.isCheck()); - Assertions.assertFalse(config.isDynamic()); - Assertions.assertTrue(config.isRegister()); - Assertions.assertFalse(config.isSubscribe()); - Assertions.assertTrue(config.isDefault()); - Assertions.assertFalse(config.getSimplified()); - Assertions.assertEquals("address", config.getAddress()); - Assertions.assertEquals("username", config.getUsername()); - Assertions.assertEquals("password", config.getPassword()); - Assertions.assertEquals("protocol", config.getProtocol()); - Assertions.assertEquals("transporter", config.getTransporter()); - Assertions.assertEquals("server", config.getServer()); - Assertions.assertEquals("client", config.getClient()); - Assertions.assertEquals("cluster", config.getCluster()); - Assertions.assertEquals("group", config.getGroup()); - Assertions.assertEquals("version", config.getVersion()); - Assertions.assertEquals("file", config.getFile()); - Assertions.assertEquals("A", config.getExtraKeys()); - Assertions.assertTrue(config.getParameters().containsKey("default.num")); - Assertions.assertEquals("one", config.getParameters().get("default.num")); - Assertions.assertEquals("id", config.getId()); - Assertions.assertEquals("prefix", config.getPrefix()); - Assertions.assertNotSame(config, config2); - } -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ServiceBuilderTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ServiceBuilderTest.java deleted file mode 100644 index fb1a4cdfd39..00000000000 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ServiceBuilderTest.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * 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 org.apache.dubbo.config.builders; - -import org.apache.dubbo.config.MethodConfig; -import org.apache.dubbo.config.ProviderConfig; -import org.apache.dubbo.config.ServiceConfig; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.Collections; - -import static org.apache.dubbo.common.Constants.GENERIC_SERIALIZATION_BEAN; -import static org.apache.dubbo.common.Constants.GENERIC_SERIALIZATION_DEFAULT; -import static org.apache.dubbo.common.Constants.GENERIC_SERIALIZATION_NATIVE_JAVA; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; - -class ServiceBuilderTest { - - @Test - void path() { - ServiceBuilder builder = new ServiceBuilder(); - builder.path("path"); - Assertions.assertEquals("path", builder.build().getPath()); - } - - @Test - void addMethod() { - MethodConfig method = new MethodConfig(); - ServiceBuilder builder = new ServiceBuilder(); - builder.addMethod(method); - Assertions.assertTrue(builder.build().getMethods().contains(method)); - Assertions.assertEquals(1, builder.build().getMethods().size()); - } - - @Test - void addMethods() { - MethodConfig method = new MethodConfig(); - ServiceBuilder builder = new ServiceBuilder(); - builder.addMethods(Collections.singletonList(method)); - Assertions.assertTrue(builder.build().getMethods().contains(method)); - Assertions.assertEquals(1, builder.build().getMethods().size()); - } - - @Test - void provider() { - ProviderConfig provider = new ProviderConfig(); - ServiceBuilder builder = new ServiceBuilder(); - builder.provider(provider); - Assertions.assertSame(provider, builder.build().getProvider()); - } - - @Test - void providerIds() { - ServiceBuilder builder = new ServiceBuilder(); - builder.providerIds("providerIds"); - Assertions.assertEquals("providerIds", builder.build().getProviderIds()); - } - - @Test - public void generic() throws Exception { - ServiceBuilder builder = new ServiceBuilder(); - builder.generic(GENERIC_SERIALIZATION_DEFAULT); - assertThat(builder.build().getGeneric(), equalTo(GENERIC_SERIALIZATION_DEFAULT)); - builder.generic(GENERIC_SERIALIZATION_NATIVE_JAVA); - assertThat(builder.build().getGeneric(), equalTo(GENERIC_SERIALIZATION_NATIVE_JAVA)); - builder.generic(GENERIC_SERIALIZATION_BEAN); - assertThat(builder.build().getGeneric(), equalTo(GENERIC_SERIALIZATION_BEAN)); - } - - @Test - public void generic1() throws Exception { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - ServiceBuilder builder = new ServiceBuilder(); - builder.generic("illegal").build(); - }); - } - - @Test - public void Mock() throws Exception { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - ServiceBuilder builder = new ServiceBuilder(); - builder.mock("true"); - }); - } - - @Test - public void Mock1() throws Exception { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - ServiceBuilder builder = new ServiceBuilder(); - builder.mock(true); - }); - } - - @Test - void build() { - MethodConfig method = new MethodConfig(); - ProviderConfig provider = new ProviderConfig(); - - ServiceBuilder builder = new ServiceBuilder(); - builder.path("path").addMethod(method).provider(provider).providerIds("providerIds") - .generic(GENERIC_SERIALIZATION_DEFAULT); - - ServiceConfig config = builder.build(); - ServiceConfig config2 = builder.build(); - - assertThat(config.getGeneric(), equalTo(GENERIC_SERIALIZATION_DEFAULT)); - Assertions.assertEquals("path", config.getPath()); - Assertions.assertEquals("providerIds", config.getProviderIds()); - Assertions.assertSame(provider, config.getProvider()); - Assertions.assertTrue(config.getMethods().contains(method)); - Assertions.assertEquals(1, config.getMethods().size()); - Assertions.assertNotSame(config, config2); - } -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/cache/CacheTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/cache/CacheTest.java index b9b5c45d435..ec4c57310cc 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/cache/CacheTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/cache/CacheTest.java @@ -31,28 +31,26 @@ import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.RpcInvocation; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import junit.framework.TestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import java.util.Arrays; import java.util.HashMap; import java.util.Map; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - /** * CacheTest */ -public class CacheTest { +public class CacheTest extends TestCase { - @BeforeEach + @Before public void setUp() { ConfigManager.getInstance().clear(); } - @AfterEach + @After public void tearDown() { ConfigManager.getInstance().clear(); } diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/invoker/DelegateProviderMetaDataInvokerTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/invoker/DelegateProviderMetaDataInvokerTest.java index 56cc05486e2..ca514cc810b 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/invoker/DelegateProviderMetaDataInvokerTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/invoker/DelegateProviderMetaDataInvokerTest.java @@ -22,18 +22,18 @@ import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Before; +import org.junit.Test; import org.mockito.Mockito; import static org.hamcrest.Matchers.sameInstance; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class DelegateProviderMetaDataInvokerTest { private ServiceConfig service; private Invoker invoker; - @BeforeEach + @Before public void setUp() throws Exception { service = Mockito.mock(ServiceConfig.class); invoker = Mockito.mock(Invoker.class); diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/url/ExporterSideConfigUrlTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/url/ExporterSideConfigUrlTest.java index 16122908c85..c72f7c8b42a 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/url/ExporterSideConfigUrlTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/url/ExporterSideConfigUrlTest.java @@ -21,10 +21,10 @@ import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.config.context.ConfigManager; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; @@ -36,18 +36,18 @@ public class ExporterSideConfigUrlTest extends UrlTestBase { // ====================================================== // tests start // ====================================================== - @BeforeAll + @BeforeClass public static void start() { } - @BeforeEach + @Before public void setUp() { initServConf(); ConfigManager.getInstance().clear(); } - @AfterEach() + @After() public void teardown() { ConfigManager.getInstance().clear(); } diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/url/InvokerSideConfigUrlTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/url/InvokerSideConfigUrlTest.java index da0f6071776..05c28388f44 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/url/InvokerSideConfigUrlTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/url/InvokerSideConfigUrlTest.java @@ -27,11 +27,11 @@ import org.apache.dubbo.config.context.ConfigManager; import org.apache.dubbo.config.mock.MockRegistry; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; import java.util.Arrays; @@ -133,20 +133,20 @@ public class InvokerSideConfigUrlTest extends UrlTestBase { // test Start // ====================================================== - @BeforeAll + @BeforeClass public static void start() { //RegistryController.startRegistryIfAbsence(1); } - @BeforeEach + @Before public void setUp() { initServConf(); initRefConf(); ConfigManager.getInstance().clear(); } - @AfterEach() + @After() public void teardown() { //RegistryServer.reloadCache(); ConfigManager.getInstance().clear(); @@ -163,7 +163,7 @@ public void refConfUrlTest() { verifyInvokerUrlGeneration(refConf, refConfTable); } - @Disabled("parameter on register center will not be merged any longer with query parameter request from the consumer") + @Ignore("parameter on register center will not be merged any longer with query parameter request from the consumer") @Test public void regConfForConsumerUrlTest() { verifyInvokerUrlGeneration(regConfForConsumer, regConfForConsumerTable); diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/url/UrlTestBase.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/url/UrlTestBase.java index f5a1a73ed22..ee00f897262 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/url/UrlTestBase.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/url/UrlTestBase.java @@ -30,7 +30,7 @@ import java.util.Arrays; -import static org.junit.jupiter.api.Assertions.fail; +import static junit.framework.TestCase.fail; @SuppressWarnings("unused") public class UrlTestBase { diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/utils/ReferenceConfigCacheTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/utils/ReferenceConfigCacheTest.java index 2af6da720ec..8216609b81c 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/utils/ReferenceConfigCacheTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/utils/ReferenceConfigCacheTest.java @@ -16,15 +16,15 @@ */ package org.apache.dubbo.config.utils; -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.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; public class ReferenceConfigCacheTest { - @BeforeEach + @Before public void setUp() throws Exception { MockReferenceConfig.setCounter(0); ReferenceConfigCache.cacheHolder.clear(); @@ -58,14 +58,6 @@ public void testGetCacheDiffReference() throws Exception { assertEquals("1", value); } - @Test - public void testGetCacheWithKey() throws Exception { - ReferenceConfigCache cache = ReferenceConfigCache.getCache(); - MockReferenceConfig config = buildMockReferenceConfig("FooService", "group1", "1.0.0"); - String value = cache.get(config); - assertEquals(value, cache.get("group1/FooService:1.0.0", String.class)); - } - @Test public void testGetCacheDiffName() throws Exception { ReferenceConfigCache cache = ReferenceConfigCache.getCache(); diff --git a/dubbo-config/dubbo-config-spring/pom.xml b/dubbo-config/dubbo-config-spring/pom.xml index 988715baf4f..626556cc324 100644 --- a/dubbo-config/dubbo-config-spring/pom.xml +++ b/dubbo-config/dubbo-config-spring/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-config - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-config-spring jar @@ -117,16 +117,39 @@ spring-test test - - junit - junit - 4.12 - test - org.apache.tomcat.embed tomcat-embed-core test + + + + + + + diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/AnnotationBean.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/AnnotationBean.java new file mode 100644 index 00000000000..9af02a1e16c --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/AnnotationBean.java @@ -0,0 +1,321 @@ +/* + * 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 org.apache.dubbo.config.spring; + +import org.apache.dubbo.common.Constants; +import org.apache.dubbo.common.logger.Logger; +import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.utils.ConcurrentHashSet; +import org.apache.dubbo.common.utils.ReflectUtils; +import org.apache.dubbo.config.AbstractConfig; +import org.apache.dubbo.config.ApplicationConfig; +import org.apache.dubbo.config.ConsumerConfig; +import org.apache.dubbo.config.ModuleConfig; +import org.apache.dubbo.config.MonitorConfig; +import org.apache.dubbo.config.ProtocolConfig; +import org.apache.dubbo.config.ProviderConfig; +import org.apache.dubbo.config.RegistryConfig; +import org.apache.dubbo.config.ServiceConfig; +import org.apache.dubbo.config.annotation.Reference; +import org.apache.dubbo.config.annotation.Service; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.config.BeanFactoryPostProcessor; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +/** + * AnnotationBean + * + * @export + */ +@Deprecated +public class AnnotationBean extends AbstractConfig implements DisposableBean, BeanFactoryPostProcessor, BeanPostProcessor, ApplicationContextAware { + + private static final long serialVersionUID = -7582802454287589552L; + + private static final Logger logger = LoggerFactory.getLogger(Logger.class); + private final Set> serviceConfigs = new ConcurrentHashSet>(); + private final ConcurrentMap> referenceConfigs = new ConcurrentHashMap>(); + private String annotationPackage; + private String[] annotationPackages; + private ApplicationContext applicationContext; + + public String getPackage() { + return annotationPackage; + } + + public void setPackage(String annotationPackage) { + this.annotationPackage = annotationPackage; + this.annotationPackages = (annotationPackage == null || annotationPackage.length() == 0) ? null + : Constants.COMMA_SPLIT_PATTERN.split(annotationPackage); + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } + + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) + throws BeansException { + if (annotationPackage == null || annotationPackage.length() == 0) { + return; + } + if (beanFactory instanceof BeanDefinitionRegistry) { + try { + // init scanner + Class scannerClass = ReflectUtils.forName("org.springframework.context.annotation.ClassPathBeanDefinitionScanner"); + Object scanner = scannerClass.getConstructor(new Class[]{BeanDefinitionRegistry.class, boolean.class}).newInstance((BeanDefinitionRegistry) beanFactory, true); + // add filter + Class filterClass = ReflectUtils.forName("org.springframework.core.type.filter.AnnotationTypeFilter"); + Object filter = filterClass.getConstructor(Class.class).newInstance(Service.class); + Method addIncludeFilter = scannerClass.getMethod("addIncludeFilter", ReflectUtils.forName("org.springframework.core.type.filter.TypeFilter")); + addIncludeFilter.invoke(scanner, filter); + // scan packages + String[] packages = Constants.COMMA_SPLIT_PATTERN.split(annotationPackage); + Method scan = scannerClass.getMethod("scan", String[].class); + scan.invoke(scanner, new Object[]{packages}); + } catch (Throwable e) { + // spring 2.0 + } + } + } + + @Override + public void destroy() { + // no need to destroy here + // see org.apache.dubbo.config.spring.extension.SpringExtensionFactory.ShutdownHookListener + /* + for (ServiceConfig serviceConfig : serviceConfigs) { + try { + serviceConfig.unexport(); + } catch (Throwable e) { + logger.error(e.getMessage(), e); + } + } + + for (ReferenceConfig referenceConfig : referenceConfigs.values()) { + try { + referenceConfig.destroy(); + } catch (Throwable e) { + logger.error(e.getMessage(), e); + } + } + */ + } + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) + throws BeansException { + if (!isMatchPackage(bean)) { + return bean; + } + Service service = bean.getClass().getAnnotation(Service.class); + if (service != null) { + ServiceBean serviceConfig = new ServiceBean(service); + serviceConfig.setRef(bean); + if (void.class.equals(service.interfaceClass()) + && "".equals(service.interfaceName())) { + if (bean.getClass().getInterfaces().length > 0) { + serviceConfig.setInterface(bean.getClass().getInterfaces()[0]); + } else { + throw new IllegalStateException("Failed to export remote service class " + bean.getClass().getName() + ", cause: The @Service undefined interfaceClass or interfaceName, and the service class unimplemented any interfaces."); + } + } + if (applicationContext != null) { + serviceConfig.setApplicationContext(applicationContext); + if (service.registry().length > 0) { + List registryConfigs = new ArrayList(); + for (String registryId : service.registry()) { + if (registryId != null && registryId.length() > 0) { + registryConfigs.add(applicationContext.getBean(registryId, RegistryConfig.class)); + } + } + serviceConfig.setRegistries(registryConfigs); + } + if (service.provider().length() > 0) { + serviceConfig.setProvider(applicationContext.getBean(service.provider(), ProviderConfig.class)); + } + if (service.monitor().length() > 0) { + serviceConfig.setMonitor(applicationContext.getBean(service.monitor(), MonitorConfig.class)); + } + if (service.application().length() > 0) { + serviceConfig.setApplication(applicationContext.getBean(service.application(), ApplicationConfig.class)); + } + if (service.module().length() > 0) { + serviceConfig.setModule(applicationContext.getBean(service.module(), ModuleConfig.class)); + } + if (service.protocol().length > 0) { + List protocolConfigs = new ArrayList(); + for (String protocolId : service.protocol()) { + if (protocolId != null && protocolId.length() > 0) { + protocolConfigs.add(applicationContext.getBean(protocolId, ProtocolConfig.class)); + } + } + serviceConfig.setProtocols(protocolConfigs); + } + if (service.tag().length() > 0) { + serviceConfig.setTag(service.tag()); + } + try { + serviceConfig.afterPropertiesSet(); + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + throw new IllegalStateException(e.getMessage(), e); + } + } + serviceConfigs.add(serviceConfig); + serviceConfig.export(); + } + return bean; + } + + @Override + public Object postProcessBeforeInitialization(Object bean, String beanName) + throws BeansException { + if (!isMatchPackage(bean)) { + return bean; + } + Method[] methods = bean.getClass().getMethods(); + for (Method method : methods) { + String name = method.getName(); + if (name.length() > 3 && name.startsWith("set") + && method.getParameterTypes().length == 1 + && Modifier.isPublic(method.getModifiers()) + && !Modifier.isStatic(method.getModifiers())) { + try { + Reference reference = method.getAnnotation(Reference.class); + if (reference != null) { + Object value = refer(reference, method.getParameterTypes()[0]); + if (value != null) { + method.invoke(bean, value); + } + } + } catch (Throwable e) { + logger.error("Failed to init remote service reference at method " + name + " in class " + bean.getClass().getName() + ", cause: " + e.getMessage(), e); + } + } + } + Field[] fields = bean.getClass().getDeclaredFields(); + for (Field field : fields) { + try { + if (!field.isAccessible()) { + field.setAccessible(true); + } + Reference reference = field.getAnnotation(Reference.class); + if (reference != null) { + Object value = refer(reference, field.getType()); + if (value != null) { + field.set(bean, value); + } + } + } catch (Throwable e) { + logger.error("Failed to init remote service reference at field " + field.getName() + " in class " + bean.getClass().getName() + ", cause: " + e.getMessage(), e); + } + } + return bean; + } + + private Object refer(Reference reference, Class referenceClass) { //method.getParameterTypes()[0] + String interfaceName; + if (!"".equals(reference.interfaceName())) { + interfaceName = reference.interfaceName(); + } else if (!void.class.equals(reference.interfaceClass())) { + interfaceName = reference.interfaceClass().getName(); + } else if (referenceClass.isInterface()) { + interfaceName = referenceClass.getName(); + } else { + throw new IllegalStateException("The @Reference undefined interfaceClass or interfaceName, and the property type " + referenceClass.getName() + " is not a interface."); + } + String key = reference.group() + "/" + interfaceName + ":" + reference.version(); + ReferenceBean referenceConfig = referenceConfigs.get(key); + if (referenceConfig == null) { + referenceConfig = new ReferenceBean(reference); + if (void.class.equals(reference.interfaceClass()) + && "".equals(reference.interfaceName()) + && referenceClass.isInterface()) { + referenceConfig.setInterface(referenceClass); + } + if (applicationContext != null) { + referenceConfig.setApplicationContext(applicationContext); + if (reference.registry().length > 0) { + List registryConfigs = new ArrayList(); + for (String registryId : reference.registry()) { + if (registryId != null && registryId.length() > 0) { + registryConfigs.add(applicationContext.getBean(registryId, RegistryConfig.class)); + } + } + referenceConfig.setRegistries(registryConfigs); + } + if (reference.consumer().length() > 0) { + referenceConfig.setConsumer(applicationContext.getBean(reference.consumer(), ConsumerConfig.class)); + } + if (reference.monitor().length() > 0) { + referenceConfig.setMonitor(applicationContext.getBean(reference.monitor(), MonitorConfig.class)); + } + if (reference.application().length() > 0) { + referenceConfig.setApplication(applicationContext.getBean(reference.application(), ApplicationConfig.class)); + } + if (reference.module().length() > 0) { + referenceConfig.setModule(applicationContext.getBean(reference.module(), ModuleConfig.class)); + } + if (reference.consumer().length() > 0) { + referenceConfig.setConsumer(applicationContext.getBean(reference.consumer(), ConsumerConfig.class)); + } + try { + referenceConfig.afterPropertiesSet(); + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + throw new IllegalStateException(e.getMessage(), e); + } + } + referenceConfigs.putIfAbsent(key, referenceConfig); + referenceConfig = referenceConfigs.get(key); + } + return referenceConfig.get(); + } + + private boolean isMatchPackage(Object bean) { + if (annotationPackages == null || annotationPackages.length == 0) { + return true; + } + String beanClassName = bean.getClass().getName(); + for (String pkg : annotationPackages) { + if (beanClassName.startsWith(pkg)) { + return true; + } + } + return false; + } + +} diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java index 3daec132ac5..5c2a5c898fa 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java @@ -17,7 +17,6 @@ package org.apache.dubbo.config.spring; import org.apache.dubbo.common.Constants; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.config.ConfigCenterConfig; @@ -150,9 +149,9 @@ public void afterPropertiesSet() throws Exception { } } - if (CollectionUtils.isEmpty(getRegistries()) - && (getConsumer() == null || CollectionUtils.isEmpty(getConsumer().getRegistries())) - && (getApplication() == null || CollectionUtils.isEmpty(getApplication().getRegistries()))) { + if ((getRegistries() == null || getRegistries().isEmpty()) + && (getConsumer() == null || getConsumer().getRegistries() == null || getConsumer().getRegistries().isEmpty()) + && (getApplication() == null || getApplication().getRegistries() == null || getApplication().getRegistries().isEmpty())) { Map registryConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, RegistryConfig.class, false, false); if (registryConfigMap != null && registryConfigMap.size() > 0) { List registryConfigs = new ArrayList<>(); @@ -216,7 +215,11 @@ public void afterPropertiesSet() throws Exception { } } - if (shouldInit()) { + Boolean b = isInit(); + if (b == null && getConsumer() != null) { + b = getConsumer().isInit(); + } + if (b != null && b) { getObject(); } } diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ServiceBean.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ServiceBean.java index 7c216a64a97..63bbea99cd4 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ServiceBean.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ServiceBean.java @@ -1,355 +1,309 @@ -/* - * 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 org.apache.dubbo.config.spring; - -import org.apache.dubbo.common.Constants; -import org.apache.dubbo.common.utils.CollectionUtils; -import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.config.ConfigCenterConfig; -import org.apache.dubbo.config.MetadataReportConfig; -import org.apache.dubbo.config.ModuleConfig; -import org.apache.dubbo.config.MonitorConfig; -import org.apache.dubbo.config.ProtocolConfig; -import org.apache.dubbo.config.ProviderConfig; -import org.apache.dubbo.config.RegistryConfig; -import org.apache.dubbo.config.ServiceConfig; -import org.apache.dubbo.config.annotation.Service; -import org.apache.dubbo.config.spring.context.event.ServiceBeanExportedEvent; -import org.apache.dubbo.config.spring.extension.SpringExtensionFactory; - -import org.springframework.aop.support.AopUtils; -import org.springframework.beans.factory.BeanFactoryUtils; -import org.springframework.beans.factory.BeanNameAware; -import org.springframework.beans.factory.DisposableBean; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; -import org.springframework.context.ApplicationEventPublisher; -import org.springframework.context.ApplicationEventPublisherAware; -import org.springframework.context.ApplicationListener; -import org.springframework.context.event.ContextRefreshedEvent; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -import static org.apache.dubbo.config.spring.util.BeanFactoryUtils.addApplicationListener; - -/** - * ServiceFactoryBean - * - * @export - */ -public class ServiceBean extends ServiceConfig implements InitializingBean, DisposableBean, - ApplicationContextAware, ApplicationListener, BeanNameAware, - ApplicationEventPublisherAware { - - - private static final long serialVersionUID = 213195494150089726L; - - private final transient Service service; - - private transient ApplicationContext applicationContext; - - private transient String beanName; - - private transient boolean supportedApplicationListener; - - private ApplicationEventPublisher applicationEventPublisher; - - public ServiceBean() { - super(); - this.service = null; - } - - public ServiceBean(Service service) { - super(service); - this.service = service; - } - - @Override - public void setApplicationContext(ApplicationContext applicationContext) { - this.applicationContext = applicationContext; - SpringExtensionFactory.addApplicationContext(applicationContext); - supportedApplicationListener = addApplicationListener(applicationContext, this); - } - - @Override - public void setBeanName(String name) { - this.beanName = name; - } - - /** - * Gets associated {@link Service} - * - * @return associated {@link Service} - */ - public Service getService() { - return service; - } - - @Override - public void onApplicationEvent(ContextRefreshedEvent event) { - if (!isExported() && !isUnexported()) { - if (logger.isInfoEnabled()) { - logger.info("The service ready on spring started. service: " + getInterface()); - } - export(); - } - } - - @Override - @SuppressWarnings({"unchecked", "deprecation"}) - public void afterPropertiesSet() throws Exception { - if (getProvider() == null) { - Map providerConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ProviderConfig.class, false, false); - if (providerConfigMap != null && providerConfigMap.size() > 0) { - Map protocolConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ProtocolConfig.class, false, false); - if (CollectionUtils.isEmptyMap(protocolConfigMap) - && providerConfigMap.size() > 1) { // backward compatibility - List providerConfigs = new ArrayList(); - for (ProviderConfig config : providerConfigMap.values()) { - if (config.isDefault() != null && config.isDefault()) { - providerConfigs.add(config); - } - } - if (!providerConfigs.isEmpty()) { - setProviders(providerConfigs); - } - } else { - ProviderConfig providerConfig = null; - for (ProviderConfig config : providerConfigMap.values()) { - if (config.isDefault() == null || config.isDefault()) { - if (providerConfig != null) { - throw new IllegalStateException("Duplicate provider configs: " + providerConfig + " and " + config); - } - providerConfig = config; - } - } - if (providerConfig != null) { - setProvider(providerConfig); - } - } - } - } - if (getApplication() == null - && (getProvider() == null || getProvider().getApplication() == null)) { - Map applicationConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ApplicationConfig.class, false, false); - if (applicationConfigMap != null && applicationConfigMap.size() > 0) { - ApplicationConfig applicationConfig = null; - for (ApplicationConfig config : applicationConfigMap.values()) { - if (applicationConfig != null) { - throw new IllegalStateException("Duplicate application configs: " + applicationConfig + " and " + config); - } - applicationConfig = config; - } - if (applicationConfig != null) { - setApplication(applicationConfig); - } - } - } - if (getModule() == null - && (getProvider() == null || getProvider().getModule() == null)) { - Map moduleConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ModuleConfig.class, false, false); - if (moduleConfigMap != null && moduleConfigMap.size() > 0) { - ModuleConfig moduleConfig = null; - for (ModuleConfig config : moduleConfigMap.values()) { - if (config.isDefault() == null || config.isDefault()) { - if (moduleConfig != null) { - throw new IllegalStateException("Duplicate module configs: " + moduleConfig + " and " + config); - } - moduleConfig = config; - } - } - if (moduleConfig != null) { - setModule(moduleConfig); - } - } - } - - if (StringUtils.isEmpty(getRegistryIds())) { - if (getApplication() != null && StringUtils.isNotEmpty(getApplication().getRegistryIds())) { - setRegistryIds(getApplication().getRegistryIds()); - } - if (getProvider() != null && StringUtils.isNotEmpty(getProvider().getRegistryIds())) { - setRegistryIds(getProvider().getRegistryIds()); - } - } - - if ((CollectionUtils.isEmpty(getRegistries())) - && (getProvider() == null || CollectionUtils.isEmpty(getProvider().getRegistries())) - && (getApplication() == null || CollectionUtils.isEmpty(getApplication().getRegistries()))) { - Map registryConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, RegistryConfig.class, false, false); - if (CollectionUtils.isNotEmptyMap(registryConfigMap)) { - List registryConfigs = new ArrayList<>(); - if (StringUtils.isNotEmpty(registryIds)) { - Arrays.stream(Constants.COMMA_SPLIT_PATTERN.split(registryIds)).forEach(id -> { - if (registryConfigMap.containsKey(id)) { - registryConfigs.add(registryConfigMap.get(id)); - } - }); - } - - if (registryConfigs.isEmpty()) { - for (RegistryConfig config : registryConfigMap.values()) { - if (StringUtils.isEmpty(registryIds)) { - registryConfigs.add(config); - } - } - } - if (!registryConfigs.isEmpty()) { - super.setRegistries(registryConfigs); - } - } - } - if (getMetadataReportConfig() == null) { - Map metadataReportConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, MetadataReportConfig.class, false, false); - if (metadataReportConfigMap != null && metadataReportConfigMap.size() == 1) { - super.setMetadataReportConfig(metadataReportConfigMap.values().iterator().next()); - } else if (metadataReportConfigMap != null && metadataReportConfigMap.size() > 1) { - throw new IllegalStateException("Multiple MetadataReport configs: " + metadataReportConfigMap); - } - } - - if (getConfigCenter() == null) { - Map configenterMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ConfigCenterConfig.class, false, false); - if (configenterMap != null && configenterMap.size() == 1) { - super.setConfigCenter(configenterMap.values().iterator().next()); - } else if (configenterMap != null && configenterMap.size() > 1) { - throw new IllegalStateException("Multiple ConfigCenter found:" + configenterMap); - } - } - - if (getMonitor() == null - && (getProvider() == null || getProvider().getMonitor() == null) - && (getApplication() == null || getApplication().getMonitor() == null)) { - Map monitorConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, MonitorConfig.class, false, false); - if (monitorConfigMap != null && monitorConfigMap.size() > 0) { - MonitorConfig monitorConfig = null; - for (MonitorConfig config : monitorConfigMap.values()) { - if (config.isDefault() == null || config.isDefault()) { - if (monitorConfig != null) { - throw new IllegalStateException("Duplicate monitor configs: " + monitorConfig + " and " + config); - } - monitorConfig = config; - } - } - if (monitorConfig != null) { - setMonitor(monitorConfig); - } - } - } - - if (StringUtils.isEmpty(getProtocolIds())) { - if (getProvider() != null && StringUtils.isNotEmpty(getProvider().getProtocolIds())) { - setProtocolIds(getProvider().getProtocolIds()); - } - } - - if (CollectionUtils.isEmpty(getProtocols()) - && (getProvider() == null || CollectionUtils.isEmpty(getProvider().getProtocols()))) { - Map protocolConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ProtocolConfig.class, false, false); - if (protocolConfigMap != null && protocolConfigMap.size() > 0) { - List protocolConfigs = new ArrayList(); - if (StringUtils.isNotEmpty(getProtocolIds())) { - Arrays.stream(Constants.COMMA_SPLIT_PATTERN.split(getProtocolIds())) - .forEach(id -> { - if (protocolConfigMap.containsKey(id)) { - protocolConfigs.add(protocolConfigMap.get(id)); - } - }); - } - - if (protocolConfigs.isEmpty()) { - for (ProtocolConfig config : protocolConfigMap.values()) { - if (StringUtils.isEmpty(protocolIds)) { - protocolConfigs.add(config); - } - } - } - - if (!protocolConfigs.isEmpty()) { - super.setProtocols(protocolConfigs); - } - } - } - if (StringUtils.isEmpty(getPath())) { - if (StringUtils.isNotEmpty(beanName) - && StringUtils.isNotEmpty(getInterface()) - && beanName.startsWith(getInterface())) { - setPath(beanName); - } - } - if (!supportedApplicationListener) { - export(); - } - } - - /** - * Get the name of {@link ServiceBean} - * - * @return {@link ServiceBean}'s name - * @since 2.6.5 - */ - public String getBeanName() { - return this.beanName; - } - - /** - * @since 2.6.5 - */ - @Override - public void export() { - super.export(); - // Publish ServiceBeanExportedEvent - publishExportEvent(); - } - - /** - * @since 2.6.5 - */ - private void publishExportEvent() { - ServiceBeanExportedEvent exportEvent = new ServiceBeanExportedEvent(this); - applicationEventPublisher.publishEvent(exportEvent); - } - - @Override - public void destroy() throws Exception { - // no need to call unexport() here, see - // org.apache.dubbo.config.spring.extension.SpringExtensionFactory.ShutdownHookListener - } - - // merged from dubbox - @Override - protected Class getServiceClass(T ref) { - if (AopUtils.isAopProxy(ref)) { - return AopUtils.getTargetClass(ref); - } - return super.getServiceClass(ref); - } - - /** - * @param applicationEventPublisher - * @since 2.6.5 - */ - @Override - public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { - this.applicationEventPublisher = applicationEventPublisher; - } +/* + * 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 org.apache.dubbo.config.spring; + +import org.apache.dubbo.common.Constants; +import org.apache.dubbo.common.utils.StringUtils; +import org.apache.dubbo.config.ApplicationConfig; +import org.apache.dubbo.config.ConfigCenterConfig; +import org.apache.dubbo.config.MetadataReportConfig; +import org.apache.dubbo.config.ModuleConfig; +import org.apache.dubbo.config.MonitorConfig; +import org.apache.dubbo.config.ProtocolConfig; +import org.apache.dubbo.config.ProviderConfig; +import org.apache.dubbo.config.RegistryConfig; +import org.apache.dubbo.config.ServiceConfig; +import org.apache.dubbo.config.annotation.Service; +import org.apache.dubbo.config.spring.extension.SpringExtensionFactory; + +import org.springframework.aop.support.AopUtils; +import org.springframework.beans.factory.BeanFactoryUtils; +import org.springframework.beans.factory.BeanNameAware; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextRefreshedEvent; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import static org.apache.dubbo.config.spring.util.BeanFactoryUtils.addApplicationListener; + +/** + * ServiceFactoryBean + * + * @export + */ +public class ServiceBean extends ServiceConfig implements InitializingBean, DisposableBean, ApplicationContextAware, ApplicationListener, BeanNameAware { + + private static final long serialVersionUID = 213195494150089726L; + + private final transient Service service; + + private transient ApplicationContext applicationContext; + + private transient String beanName; + + private transient boolean supportedApplicationListener; + + public ServiceBean() { + super(); + this.service = null; + } + + public ServiceBean(Service service) { + super(service); + this.service = service; + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + SpringExtensionFactory.addApplicationContext(applicationContext); + supportedApplicationListener = addApplicationListener(applicationContext, this); + } + + @Override + public void setBeanName(String name) { + this.beanName = name; + } + + /** + * Gets associated {@link Service} + * + * @return associated {@link Service} + */ + public Service getService() { + return service; + } + + @Override + public void onApplicationEvent(ContextRefreshedEvent event) { + if (!isExported() && !isUnexported()) { + if (logger.isInfoEnabled()) { + logger.info("The service ready on spring started. service: " + getInterface()); + } + export(); + } + } + + @Override + @SuppressWarnings({"unchecked", "deprecation"}) + public void afterPropertiesSet() throws Exception { + if (getProvider() == null) { + Map providerConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ProviderConfig.class, false, false); + if (providerConfigMap != null && providerConfigMap.size() > 0) { + Map protocolConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ProtocolConfig.class, false, false); + if ((protocolConfigMap == null || protocolConfigMap.size() == 0) + && providerConfigMap.size() > 1) { // backward compatibility + List providerConfigs = new ArrayList(); + for (ProviderConfig config : providerConfigMap.values()) { + if (config.isDefault() != null && config.isDefault()) { + providerConfigs.add(config); + } + } + if (!providerConfigs.isEmpty()) { + setProviders(providerConfigs); + } + } else { + ProviderConfig providerConfig = null; + for (ProviderConfig config : providerConfigMap.values()) { + if (config.isDefault() == null || config.isDefault()) { + if (providerConfig != null) { + throw new IllegalStateException("Duplicate provider configs: " + providerConfig + " and " + config); + } + providerConfig = config; + } + } + if (providerConfig != null) { + setProvider(providerConfig); + } + } + } + } + if (getApplication() == null + && (getProvider() == null || getProvider().getApplication() == null)) { + Map applicationConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ApplicationConfig.class, false, false); + if (applicationConfigMap != null && applicationConfigMap.size() > 0) { + ApplicationConfig applicationConfig = null; + for (ApplicationConfig config : applicationConfigMap.values()) { + if (applicationConfig != null) { + throw new IllegalStateException("Duplicate application configs: " + applicationConfig + " and " + config); + } + applicationConfig = config; + } + if (applicationConfig != null) { + setApplication(applicationConfig); + } + } + } + if (getModule() == null + && (getProvider() == null || getProvider().getModule() == null)) { + Map moduleConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ModuleConfig.class, false, false); + if (moduleConfigMap != null && moduleConfigMap.size() > 0) { + ModuleConfig moduleConfig = null; + for (ModuleConfig config : moduleConfigMap.values()) { + if (config.isDefault() == null || config.isDefault()) { + if (moduleConfig != null) { + throw new IllegalStateException("Duplicate module configs: " + moduleConfig + " and " + config); + } + moduleConfig = config; + } + } + if (moduleConfig != null) { + setModule(moduleConfig); + } + } + } + + if (StringUtils.isEmpty(getRegistryIds())) { + if (getApplication() != null && StringUtils.isNotEmpty(getApplication().getRegistryIds())) { + setRegistryIds(getApplication().getRegistryIds()); + } + if (getProvider() != null && StringUtils.isNotEmpty(getProvider().getRegistryIds())) { + setRegistryIds(getProvider().getRegistryIds()); + } + } + + if ((getRegistries() == null || getRegistries().isEmpty()) + && (getProvider() == null || getProvider().getRegistries() == null || getProvider().getRegistries().isEmpty()) + && (getApplication() == null || getApplication().getRegistries() == null || getApplication().getRegistries().isEmpty())) { + Map registryConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, RegistryConfig.class, false, false); + if (registryConfigMap != null && registryConfigMap.size() > 0) { + List registryConfigs = new ArrayList<>(); + if (StringUtils.isNotEmpty(registryIds)) { + Arrays.stream(Constants.COMMA_SPLIT_PATTERN.split(registryIds)).forEach(id -> { + if (registryConfigMap.containsKey(id)) { + registryConfigs.add(registryConfigMap.get(id)); + } + }); + } + + if (registryConfigs.isEmpty()) { + for (RegistryConfig config : registryConfigMap.values()) { + if (StringUtils.isEmpty(registryIds)) { + registryConfigs.add(config); + } + } + } + if (!registryConfigs.isEmpty()) { + super.setRegistries(registryConfigs); + } + } + } + if (getMetadataReportConfig() == null) { + Map metadataReportConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, MetadataReportConfig.class, false, false); + if (metadataReportConfigMap != null && metadataReportConfigMap.size() == 1) { + super.setMetadataReportConfig(metadataReportConfigMap.values().iterator().next()); + } else if (metadataReportConfigMap != null && metadataReportConfigMap.size() > 1) { + throw new IllegalStateException("Multiple MetadataReport configs: " + metadataReportConfigMap); + } + } + + if (getConfigCenter() == null) { + Map configenterMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ConfigCenterConfig.class, false, false); + if (configenterMap != null && configenterMap.size() == 1) { + super.setConfigCenter(configenterMap.values().iterator().next()); + } else if (configenterMap != null && configenterMap.size() > 1) { + throw new IllegalStateException("Multiple ConfigCenter found:" + configenterMap); + } + } + + if (getMonitor() == null + && (getProvider() == null || getProvider().getMonitor() == null) + && (getApplication() == null || getApplication().getMonitor() == null)) { + Map monitorConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, MonitorConfig.class, false, false); + if (monitorConfigMap != null && monitorConfigMap.size() > 0) { + MonitorConfig monitorConfig = null; + for (MonitorConfig config : monitorConfigMap.values()) { + if (config.isDefault() == null || config.isDefault()) { + if (monitorConfig != null) { + throw new IllegalStateException("Duplicate monitor configs: " + monitorConfig + " and " + config); + } + monitorConfig = config; + } + } + if (monitorConfig != null) { + setMonitor(monitorConfig); + } + } + } + + if (StringUtils.isEmpty(getProtocolIds())) { + if (getProvider() != null && StringUtils.isNotEmpty(getProvider().getProtocolIds())) { + setProtocolIds(getProvider().getProtocolIds()); + } + } + + if ((getProtocols() == null || getProtocols().isEmpty()) + && (getProvider() == null || getProvider().getProtocols() == null || getProvider().getProtocols().isEmpty())) { + Map protocolConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ProtocolConfig.class, false, false); + if (protocolConfigMap != null && protocolConfigMap.size() > 0) { + List protocolConfigs = new ArrayList(); + if (StringUtils.isNotEmpty(getProtocolIds())) { + Arrays.stream(Constants.COMMA_SPLIT_PATTERN.split(getProtocolIds())) + .forEach(id -> { + if (protocolConfigMap.containsKey(id)) { + protocolConfigs.add(protocolConfigMap.get(id)); + } + }); + } + + if (protocolConfigs.isEmpty()) { + for (ProtocolConfig config : protocolConfigMap.values()) { + if (StringUtils.isEmpty(protocolIds)) { + protocolConfigs.add(config); + } + } + } + + if (!protocolConfigs.isEmpty()) { + super.setProtocols(protocolConfigs); + } + } + } + if (getPath() == null || getPath().length() == 0) { + if (beanName != null && beanName.length() > 0 + && getInterface() != null && getInterface().length() > 0 + && beanName.startsWith(getInterface())) { + setPath(beanName); + } + } + if (!supportedApplicationListener) { + export(); + } + } + + @Override + public void destroy() throws Exception { + // no need to call unexport() here, see + // org.apache.dubbo.config.spring.extension.SpringExtensionFactory.ShutdownHookListener + } + + // merged from dubbox + @Override + protected Class getServiceClass(T ref) { + if (AopUtils.isAopProxy(ref)) { + return AopUtils.getTargetClass(ref); + } + return super.getServiceClass(ref); + } } \ No newline at end of file diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AbstractAnnotationConfigBeanBuilder.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AbstractAnnotationConfigBeanBuilder.java index 7f7afd98d66..eb99855e72b 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AbstractAnnotationConfigBeanBuilder.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AbstractAnnotationConfigBeanBuilder.java @@ -77,7 +77,7 @@ public final B build() throws Exception { configureBean(bean); if (logger.isInfoEnabled()) { - logger.info("The bean[type:" + bean.getClass().getSimpleName() + "] has been built."); + logger.info(bean + " has been built."); } return bean; @@ -175,7 +175,7 @@ private void configureModuleConfig(B bean) { /** - * Resolves the bean ids of {@link RegistryConfig} + * Resolves the bean ids of {@link org.apache.dubbo.config.RegistryConfig} * * @param annotation {@link A} * @return non-empty array diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationInjectedBeanPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationInjectedBeanPostProcessor.java deleted file mode 100644 index 30592a1a7cb..00000000000 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationInjectedBeanPostProcessor.java +++ /dev/null @@ -1,531 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.beans.factory.annotation; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.beans.BeanUtils; -import org.springframework.beans.BeansException; -import org.springframework.beans.PropertyValues; -import org.springframework.beans.factory.BeanClassLoaderAware; -import org.springframework.beans.factory.BeanCreationException; -import org.springframework.beans.factory.BeanFactory; -import org.springframework.beans.factory.BeanFactoryAware; -import org.springframework.beans.factory.DisposableBean; -import org.springframework.beans.factory.annotation.InjectionMetadata; -import org.springframework.beans.factory.config.BeanPostProcessor; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; -import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; -import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor; -import org.springframework.beans.factory.support.RootBeanDefinition; -import org.springframework.context.EnvironmentAware; -import org.springframework.core.Ordered; -import org.springframework.core.PriorityOrdered; -import org.springframework.core.env.Environment; -import org.springframework.util.Assert; -import org.springframework.util.ClassUtils; -import org.springframework.util.ReflectionUtils; -import org.springframework.util.StringUtils; - -import java.beans.PropertyDescriptor; -import java.lang.annotation.Annotation; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - -import static org.apache.dubbo.config.spring.util.ClassUtils.resolveGenericType; -import static org.springframework.core.BridgeMethodResolver.findBridgedMethod; -import static org.springframework.core.BridgeMethodResolver.isVisibilityBridgeMethodPair; -import static org.springframework.core.annotation.AnnotationUtils.findAnnotation; -import static org.springframework.core.annotation.AnnotationUtils.getAnnotation; - -/** - * Abstract generic {@link BeanPostProcessor} implementation for customized annotation that annotated injected-object. - * - * The source code is cloned from https://github.com/alibaba/spring-context-support/blob/1.0.2/src/main/java/com/alibaba/spring/beans/factory/annotation/AnnotationInjectedBeanPostProcessor.java - * - * @since 2.6.6 - */ -public abstract class AnnotationInjectedBeanPostProcessor extends - InstantiationAwareBeanPostProcessorAdapter implements MergedBeanDefinitionPostProcessor, PriorityOrdered, - BeanFactoryAware, BeanClassLoaderAware, EnvironmentAware, DisposableBean { - - private final static int CACHE_SIZE = Integer.getInteger("", 32); - - private final Log logger = LogFactory.getLog(getClass()); - - private final Class annotationType; - - private final ConcurrentMap injectionMetadataCache = - new ConcurrentHashMap(CACHE_SIZE); - - private final ConcurrentMap injectedObjectsCache = new ConcurrentHashMap(CACHE_SIZE); - - private ConfigurableListableBeanFactory beanFactory; - - private Environment environment; - - private ClassLoader classLoader; - - private int order = Ordered.LOWEST_PRECEDENCE; - - public AnnotationInjectedBeanPostProcessor() { - this.annotationType = resolveGenericType(getClass()); - } - - private static Collection combine(Collection... elements) { - List allElements = new ArrayList(); - for (Collection e : elements) { - allElements.addAll(e); - } - return allElements; - } - - /** - * Annotation type - * - * @return non-null - */ - public final Class getAnnotationType() { - return annotationType; - } - - @Override - public void setBeanFactory(BeanFactory beanFactory) throws BeansException { - Assert.isInstanceOf(ConfigurableListableBeanFactory.class, beanFactory, - "AnnotationInjectedBeanPostProcessor requires a ConfigurableListableBeanFactory"); - this.beanFactory = (ConfigurableListableBeanFactory) beanFactory; - } - - @Override - public PropertyValues postProcessPropertyValues( - PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeanCreationException { - - InjectionMetadata metadata = findInjectionMetadata(beanName, bean.getClass(), pvs); - try { - metadata.inject(bean, beanName, pvs); - } catch (BeanCreationException ex) { - throw ex; - } catch (Throwable ex) { - throw new BeanCreationException(beanName, "Injection of @" + getAnnotationType().getName() - + " dependencies is failed", ex); - } - return pvs; - } - - - /** - * Finds {@link InjectionMetadata.InjectedElement} Metadata from annotated {@link A} fields - * - * @param beanClass The {@link Class} of Bean - * @return non-null {@link List} - */ - private List findFieldAnnotationMetadata(final Class beanClass) { - - final List elements = new LinkedList(); - - ReflectionUtils.doWithFields(beanClass, new ReflectionUtils.FieldCallback() { - @Override - public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { - - A annotation = getAnnotation(field, getAnnotationType()); - - if (annotation != null) { - - if (Modifier.isStatic(field.getModifiers())) { - if (logger.isWarnEnabled()) { - logger.warn("@" + getAnnotationType().getName() + " is not supported on static fields: " + field); - } - return; - } - - elements.add(new AnnotationInjectedBeanPostProcessor.AnnotatedFieldElement(field, annotation)); - } - - } - }); - - return elements; - - } - - /** - * Finds {@link InjectionMetadata.InjectedElement} Metadata from annotated {@link A @A} methods - * - * @param beanClass The {@link Class} of Bean - * @return non-null {@link List} - */ - private List findAnnotatedMethodMetadata(final Class beanClass) { - - final List elements = new LinkedList(); - - ReflectionUtils.doWithMethods(beanClass, new ReflectionUtils.MethodCallback() { - @Override - public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { - - Method bridgedMethod = findBridgedMethod(method); - - if (!isVisibilityBridgeMethodPair(method, bridgedMethod)) { - return; - } - - A annotation = findAnnotation(bridgedMethod, getAnnotationType()); - - if (annotation != null && method.equals(ClassUtils.getMostSpecificMethod(method, beanClass))) { - if (Modifier.isStatic(method.getModifiers())) { - if (logger.isWarnEnabled()) { - logger.warn("@" + getAnnotationType().getSimpleName() + " annotation is not supported on static methods: " + method); - } - return; - } - if (method.getParameterTypes().length == 0) { - if (logger.isWarnEnabled()) { - logger.warn("@" + getAnnotationType().getSimpleName() + " annotation should only be used on methods with parameters: " + - method); - } - } - PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, beanClass); - elements.add(new AnnotationInjectedBeanPostProcessor.AnnotatedMethodElement(method, pd, annotation)); - } - } - }); - - return elements; - - } - - - private AnnotationInjectedBeanPostProcessor.AnnotatedInjectionMetadata buildAnnotatedMetadata(final Class beanClass) { - Collection fieldElements = findFieldAnnotationMetadata(beanClass); - Collection methodElements = findAnnotatedMethodMetadata(beanClass); - return new AnnotationInjectedBeanPostProcessor.AnnotatedInjectionMetadata(beanClass, fieldElements, methodElements); - - } - - private InjectionMetadata findInjectionMetadata(String beanName, Class clazz, PropertyValues pvs) { - // Fall back to class name as cache key, for backwards compatibility with custom callers. - String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName()); - // Quick check on the concurrent map first, with minimal locking. - AnnotationInjectedBeanPostProcessor.AnnotatedInjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey); - if (InjectionMetadata.needsRefresh(metadata, clazz)) { - synchronized (this.injectionMetadataCache) { - metadata = this.injectionMetadataCache.get(cacheKey); - if (InjectionMetadata.needsRefresh(metadata, clazz)) { - if (metadata != null) { - metadata.clear(pvs); - } - try { - metadata = buildAnnotatedMetadata(clazz); - this.injectionMetadataCache.put(cacheKey, metadata); - } catch (NoClassDefFoundError err) { - throw new IllegalStateException("Failed to introspect object class [" + clazz.getName() + - "] for annotation metadata: could not find class that it depends on", err); - } - } - } - } - return metadata; - } - - @Override - public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class beanType, String beanName) { - if (beanType != null) { - InjectionMetadata metadata = findInjectionMetadata(beanName, beanType, null); - metadata.checkConfigMembers(beanDefinition); - } - } - - @Override - public int getOrder() { - return order; - } - - public void setOrder(int order) { - this.order = order; - } - - @Override - public void destroy() throws Exception { - - for (Object object : injectedObjectsCache.values()) { - if (logger.isInfoEnabled()) { - logger.info(object + " was destroying!"); - } - - if (object instanceof DisposableBean) { - ((DisposableBean) object).destroy(); - } - } - - injectionMetadataCache.clear(); - injectedObjectsCache.clear(); - - if (logger.isInfoEnabled()) { - logger.info(getClass() + " was destroying!"); - } - - } - - @Override - public void setBeanClassLoader(ClassLoader classLoader) { - this.classLoader = classLoader; - } - - @Override - public void setEnvironment(Environment environment) { - this.environment = environment; - } - - protected Environment getEnvironment() { - return environment; - } - - protected ClassLoader getClassLoader() { - return classLoader; - } - - protected ConfigurableListableBeanFactory getBeanFactory() { - return beanFactory; - } - - /** - * Gets all injected-objects. - * - * @return non-null {@link Collection} - */ - protected Collection getInjectedObjects() { - return this.injectedObjectsCache.values(); - } - - /** - * Get injected-object from specified {@link A annotation} and Bean Class - * - * @param annotation {@link A annotation} - * @param bean Current bean that will be injected - * @param beanName Current bean name that will be injected - * @param injectedType the type of injected-object - * @param injectedElement {@link InjectionMetadata.InjectedElement} - * @return An injected object - * @throws Exception If getting is failed - */ - protected Object getInjectedObject(A annotation, Object bean, String beanName, Class injectedType, - InjectionMetadata.InjectedElement injectedElement) throws Exception { - - String cacheKey = buildInjectedObjectCacheKey(annotation, bean, beanName, injectedType, injectedElement); - - Object injectedObject = injectedObjectsCache.get(cacheKey); - - if (injectedObject == null) { - injectedObject = doGetInjectedBean(annotation, bean, beanName, injectedType, injectedElement); - // Customized inject-object if necessary - injectedObjectsCache.putIfAbsent(cacheKey, injectedObject); - } - - return injectedObject; - - } - - /** - * Subclass must implement this method to get injected-object. The context objects could help this method if - * necessary : - *
      - *
    • {@link #getBeanFactory() BeanFactory}
    • - *
    • {@link #getClassLoader() ClassLoader}
    • - *
    • {@link #getEnvironment() Environment}
    • - *
    - * - * @param annotation {@link A annotation} - * @param bean Current bean that will be injected - * @param beanName Current bean name that will be injected - * @param injectedType the type of injected-object - * @param injectedElement {@link InjectionMetadata.InjectedElement} - * @return The injected object - * @throws Exception If resolving an injected object is failed. - */ - protected abstract Object doGetInjectedBean(A annotation, Object bean, String beanName, Class injectedType, - InjectionMetadata.InjectedElement injectedElement) throws Exception; - - /** - * Build a cache key for injected-object. The context objects could help this method if - * necessary : - *
      - *
    • {@link #getBeanFactory() BeanFactory}
    • - *
    • {@link #getClassLoader() ClassLoader}
    • - *
    • {@link #getEnvironment() Environment}
    • - *
    - * - * @param annotation {@link A annotation} - * @param bean Current bean that will be injected - * @param beanName Current bean name that will be injected - * @param injectedType the type of injected-object - * @param injectedElement {@link InjectionMetadata.InjectedElement} - * @return Bean cache key - */ - protected abstract String buildInjectedObjectCacheKey(A annotation, Object bean, String beanName, - Class injectedType, - InjectionMetadata.InjectedElement injectedElement); - - /** - * Get {@link Map} in injected field. - * - * @return non-null ready-only {@link Map} - */ - protected Map getInjectedFieldObjectsMap() { - - Map injectedElementBeanMap = - new LinkedHashMap(); - - for (AnnotationInjectedBeanPostProcessor.AnnotatedInjectionMetadata metadata : injectionMetadataCache.values()) { - - Collection fieldElements = metadata.getFieldElements(); - - for (AnnotationInjectedBeanPostProcessor.AnnotatedFieldElement fieldElement : fieldElements) { - - injectedElementBeanMap.put(fieldElement, fieldElement.bean); - - } - - } - - return Collections.unmodifiableMap(injectedElementBeanMap); - - } - - /** - * Get {@link Map} in injected method. - * - * @return non-null {@link Map} - */ - protected Map getInjectedMethodObjectsMap() { - - Map injectedElementBeanMap = - new LinkedHashMap(); - - for (AnnotationInjectedBeanPostProcessor.AnnotatedInjectionMetadata metadata : injectionMetadataCache.values()) { - - Collection methodElements = metadata.getMethodElements(); - - for (AnnotationInjectedBeanPostProcessor.AnnotatedMethodElement methodElement : methodElements) { - - injectedElementBeanMap.put(methodElement, methodElement.object); - - } - - } - - return Collections.unmodifiableMap(injectedElementBeanMap); - - } - - /** - * {@link A} {@link InjectionMetadata} implementation - */ - private class AnnotatedInjectionMetadata extends InjectionMetadata { - - private final Collection fieldElements; - - private final Collection methodElements; - - public AnnotatedInjectionMetadata(Class targetClass, Collection fieldElements, - Collection methodElements) { - super(targetClass, combine(fieldElements, methodElements)); - this.fieldElements = fieldElements; - this.methodElements = methodElements; - } - - public Collection getFieldElements() { - return fieldElements; - } - - public Collection getMethodElements() { - return methodElements; - } - } - - /** - * {@link A} {@link Method} {@link InjectionMetadata.InjectedElement} - */ - private class AnnotatedMethodElement extends InjectionMetadata.InjectedElement { - - private final Method method; - - private final A annotation; - - private volatile Object object; - - protected AnnotatedMethodElement(Method method, PropertyDescriptor pd, A annotation) { - super(method, pd); - this.method = method; - this.annotation = annotation; - } - - @Override - protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable { - - Class injectedType = pd.getPropertyType(); - - Object injectedObject = getInjectedObject(annotation, bean, beanName, injectedType, this); - - ReflectionUtils.makeAccessible(method); - - method.invoke(bean, injectedObject); - - } - - } - - /** - * {@link A} {@link Field} {@link InjectionMetadata.InjectedElement} - */ - public class AnnotatedFieldElement extends InjectionMetadata.InjectedElement { - - private final Field field; - - private final A annotation; - - private volatile Object bean; - - protected AnnotatedFieldElement(Field field, A annotation) { - super(field, null); - this.field = field; - this.annotation = annotation; - } - - @Override - protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable { - - Class injectedType = field.getType(); - - Object injectedObject = getInjectedObject(annotation, bean, beanName, injectedType, this); - - ReflectionUtils.makeAccessible(field); - - field.set(bean, injectedObject); - - } - - } -} diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboConfigBindingBeanPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboConfigBindingBeanPostProcessor.java index 96c4d3885b0..65aadeff359 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboConfigBindingBeanPostProcessor.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboConfigBindingBeanPostProcessor.java @@ -76,7 +76,7 @@ public DubboConfigBindingBeanPostProcessor(String prefix, String beanName) { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { - if (beanName.equals(this.beanName) && bean instanceof AbstractConfig) { + if (beanName != null && beanName.equals(this.beanName) && bean instanceof AbstractConfig) { AbstractConfig dubboConfig = (AbstractConfig) bean; diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java index 82fb9b25eb0..4d8f0bdd29e 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java @@ -16,237 +16,582 @@ */ package org.apache.dubbo.config.spring.beans.factory.annotation; +import org.apache.dubbo.common.Constants; +import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.config.annotation.Reference; import org.apache.dubbo.config.spring.ReferenceBean; -import org.apache.dubbo.config.spring.ServiceBean; -import org.apache.dubbo.config.spring.context.event.ServiceBeanExportedEvent; -import org.apache.dubbo.config.spring.util.AnnotationUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.BeanUtils; import org.springframework.beans.BeansException; +import org.springframework.beans.PropertyValues; +import org.springframework.beans.factory.BeanClassLoaderAware; +import org.springframework.beans.factory.BeanCreationException; +import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.annotation.InjectionMetadata; +import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; +import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor; +import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; -import org.springframework.context.ApplicationEvent; -import org.springframework.context.ApplicationListener; -import org.springframework.context.event.ContextRefreshedEvent; - +import org.springframework.core.PriorityOrdered; +import org.springframework.core.env.Environment; +import org.springframework.util.ClassUtils; +import org.springframework.util.ConcurrentReferenceHashMap; +import org.springframework.util.ObjectUtils; +import org.springframework.util.ReflectionUtils; + +import java.beans.PropertyDescriptor; +import java.lang.annotation.Annotation; import java.lang.reflect.Field; -import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; -import java.lang.reflect.Proxy; +import java.lang.reflect.Modifier; +import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import static org.springframework.core.BridgeMethodResolver.findBridgedMethod; +import static org.springframework.core.BridgeMethodResolver.isVisibilityBridgeMethodPair; +import static org.springframework.core.annotation.AnnotationUtils.findAnnotation; +import static org.springframework.core.annotation.AnnotationUtils.getAnnotation; + /** * {@link org.springframework.beans.factory.config.BeanPostProcessor} implementation * that Consumer service {@link Reference} annotated fields * * @since 2.5.7 */ -public class ReferenceAnnotationBeanPostProcessor extends AnnotationInjectedBeanPostProcessor - implements ApplicationContextAware, ApplicationListener { +public class ReferenceAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter + implements MergedBeanDefinitionPostProcessor, PriorityOrdered, ApplicationContextAware, BeanClassLoaderAware, + DisposableBean { /** * The bean name of {@link ReferenceAnnotationBeanPostProcessor} */ public static final String BEAN_NAME = "referenceAnnotationBeanPostProcessor"; - /** - * Cache size - */ - private static final int CACHE_SIZE = Integer.getInteger(BEAN_NAME + ".cache.size", 32); + private final Log logger = LogFactory.getLog(getClass()); - private final ConcurrentMap> referenceBeanCache = - new ConcurrentHashMap>(CACHE_SIZE); + private ApplicationContext applicationContext; - private final ConcurrentHashMap localReferenceBeanInvocationHandlerCache = - new ConcurrentHashMap(CACHE_SIZE); + private ClassLoader classLoader; - private final ConcurrentMap> injectedFieldReferenceBeanCache = - new ConcurrentHashMap>(CACHE_SIZE); + private final ConcurrentMap injectionMetadataCache = + new ConcurrentHashMap(256); - private final ConcurrentMap> injectedMethodReferenceBeanCache = - new ConcurrentHashMap>(CACHE_SIZE); + private final ConcurrentMap> referenceBeansCache = + new ConcurrentHashMap>(); + + private static final Map, List> annotationMethodsCache = + new ConcurrentReferenceHashMap, List>(256); + + @Override + public PropertyValues postProcessPropertyValues( + PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeanCreationException { + + InjectionMetadata metadata = findReferenceMetadata(beanName, bean.getClass(), pvs); + try { + metadata.inject(bean, beanName, pvs); + } catch (BeanCreationException ex) { + throw ex; + } catch (Throwable ex) { + throw new BeanCreationException(beanName, "Injection of @Reference dependencies failed", ex); + } + return pvs; + } - private ApplicationContext applicationContext; /** - * Gets all beans of {@link ReferenceBean} + * Finds {@link InjectionMetadata.InjectedElement} Metadata from annotated {@link Reference @Reference} fields * - * @return non-null read-only {@link Collection} - * @since 2.5.9 + * @param beanClass The {@link Class} of Bean + * @return non-null {@link List} */ - public Collection> getReferenceBeans() { - return referenceBeanCache.values(); + private List findFieldReferenceMetadata(final Class beanClass) { + + final List elements = new LinkedList(); + + ReflectionUtils.doWithFields(beanClass, new ReflectionUtils.FieldCallback() { + @Override + public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { + + Reference reference = getAnnotation(field, Reference.class); + + if (reference != null) { + + if (Modifier.isStatic(field.getModifiers())) { + if (logger.isWarnEnabled()) { + logger.warn("@Reference annotation is not supported on static fields: " + field); + } + return; + } + + elements.add(new ReferenceFieldElement(field, reference)); + } + + } + }); + + return elements; + } /** - * Get {@link ReferenceBean} {@link Map} in injected field. + * Finds {@link InjectionMetadata.InjectedElement} Metadata from annotated {@link Reference @Reference} methods * - * @return non-null {@link Map} - * @since 2.5.11 + * @param beanClass The {@link Class} of Bean + * @return non-null {@link List} */ - public Map> getInjectedFieldReferenceBeanMap() { - return Collections.unmodifiableMap(injectedFieldReferenceBeanCache); + private List findMethodReferenceMetadata(final Class beanClass) { + + final List elements = new LinkedList(); + + ReflectionUtils.doWithMethods(beanClass, new ReflectionUtils.MethodCallback() { + @Override + public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { + + Method bridgedMethod = findBridgedMethod(method); + + if (!isVisibilityBridgeMethodPair(method, bridgedMethod)) { + return; + } + + Reference reference = findAnnotation(bridgedMethod, Reference.class); + + if (reference != null && method.equals(ClassUtils.getMostSpecificMethod(method, beanClass))) { + if (Modifier.isStatic(method.getModifiers())) { + if (logger.isWarnEnabled()) { + logger.warn("@Reference annotation is not supported on static methods: " + method); + } + return; + } + if (method.getParameterTypes().length == 0) { + if (logger.isWarnEnabled()) { + logger.warn("@Reference annotation should only be used on methods with parameters: " + + method); + } + } + PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, beanClass); + elements.add(new ReferenceMethodElement(method, pd, reference)); + } + } + }); + + return elements; + } + /** - * Get {@link ReferenceBean} {@link Map} in injected method. - * - * @return non-null {@link Map} - * @since 2.5.11 + * @param beanClass + * @return */ - public Map> getInjectedMethodReferenceBeanMap() { - return Collections.unmodifiableMap(injectedMethodReferenceBeanCache); + private ReferenceInjectionMetadata buildReferenceMetadata(final Class beanClass) { + Collection fieldElements = findFieldReferenceMetadata(beanClass); + Collection methodElements = findMethodReferenceMetadata(beanClass); + return new ReferenceInjectionMetadata(beanClass, fieldElements, methodElements); + + } + + private InjectionMetadata findReferenceMetadata(String beanName, Class clazz, PropertyValues pvs) { + // Fall back to class name as cache key, for backwards compatibility with custom callers. + String cacheKey = (StringUtils.isNotEmpty(beanName) ? beanName : clazz.getName()); + // Quick check on the concurrent map first, with minimal locking. + ReferenceInjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey); + if (InjectionMetadata.needsRefresh(metadata, clazz)) { + synchronized (this.injectionMetadataCache) { + metadata = this.injectionMetadataCache.get(cacheKey); + if (InjectionMetadata.needsRefresh(metadata, clazz)) { + if (metadata != null) { + metadata.clear(pvs); + } + try { + metadata = buildReferenceMetadata(clazz); + this.injectionMetadataCache.put(cacheKey, metadata); + } catch (NoClassDefFoundError err) { + throw new IllegalStateException("Failed to introspect bean class [" + clazz.getName() + + "] for reference metadata: could not find class that it depends on", err); + } + } + } + } + return metadata; + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } + + @Override + public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class beanType, String beanName) { + if (beanType != null) { + InjectionMetadata metadata = findReferenceMetadata(beanName, beanType, null); + metadata.checkConfigMembers(beanDefinition); + } } @Override - protected Object doGetInjectedBean(Reference reference, Object bean, String beanName, Class injectedType, - InjectionMetadata.InjectedElement injectedElement) throws Exception { + public int getOrder() { + return LOWEST_PRECEDENCE; + } - String referencedBeanName = buildReferencedBeanName(reference, injectedType); + @Override + public void destroy() throws Exception { - ReferenceBean referenceBean = buildReferenceBeanIfAbsent(referencedBeanName, reference, injectedType, getClassLoader()); + for (ReferenceBean referenceBean : referenceBeansCache.values()) { + if (logger.isInfoEnabled()) { + logger.info(referenceBean + " was destroying!"); + } + referenceBean.destroy(); + } - cacheInjectedReferenceBean(referenceBean, injectedElement); + injectionMetadataCache.clear(); + referenceBeansCache.clear(); - Object proxy = buildProxy(referencedBeanName, referenceBean, injectedType); + if (logger.isInfoEnabled()) { + logger.info(getClass() + " was destroying!"); + } - return proxy; } - private Object buildProxy(String referencedBeanName, ReferenceBean referenceBean, Class injectedType) { - InvocationHandler handler = buildInvocationHandler(referencedBeanName, referenceBean); - Object proxy = Proxy.newProxyInstance(getClassLoader(), new Class[]{injectedType}, handler); - return proxy; + @Override + public void setBeanClassLoader(ClassLoader classLoader) { + this.classLoader = classLoader; + } + + + /** + * Gets all beans of {@link ReferenceBean} + * + * @return non-null {@link Collection} + * @since 2.5.9 + */ + public Collection> getReferenceBeans() { + return this.referenceBeansCache.values(); } - private InvocationHandler buildInvocationHandler(String referencedBeanName, ReferenceBean referenceBean) { - ReferenceBeanInvocationHandler handler = localReferenceBeanInvocationHandlerCache.get(referencedBeanName); + /** + * {@link Reference} {@link InjectionMetadata} implementation + * + * @since 2.5.11 + */ + private static class ReferenceInjectionMetadata extends InjectionMetadata { + + private final Collection fieldElements; + + private final Collection methodElements; + - if (handler == null) { - handler = new ReferenceBeanInvocationHandler(referenceBean); + public ReferenceInjectionMetadata(Class targetClass, Collection fieldElements, + Collection methodElements) { + super(targetClass, combine(fieldElements, methodElements)); + this.fieldElements = fieldElements; + this.methodElements = methodElements; } - if (applicationContext.containsBean(referencedBeanName)) { // Is local @Service Bean or not ? - // ReferenceBeanInvocationHandler's initialization has to wait for current local @Service Bean has been exported. - localReferenceBeanInvocationHandlerCache.put(referencedBeanName, handler); - } else { - // Remote Reference Bean should initialize immediately - handler.init(); + private static Collection combine(Collection... elements) { + List allElements = new ArrayList(); + for (Collection e : elements) { + allElements.addAll(e); + } + return allElements; + } + + public Collection getFieldElements() { + return fieldElements; } - return handler; + public Collection getMethodElements() { + return methodElements; + } } - private static class ReferenceBeanInvocationHandler implements InvocationHandler { + /** + * {@link Reference} {@link Method} {@link InjectionMetadata.InjectedElement} + */ + private class ReferenceMethodElement extends InjectionMetadata.InjectedElement { - private final ReferenceBean referenceBean; + private final Method method; - private Object bean; + private final Reference reference; - private ReferenceBeanInvocationHandler(ReferenceBean referenceBean) { - this.referenceBean = referenceBean; + private volatile ReferenceBean referenceBean; + + protected ReferenceMethodElement(Method method, PropertyDescriptor pd, Reference reference) { + super(method, pd); + this.method = method; + this.reference = reference; } @Override - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - return method.invoke(bean, args); - } + protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable { + + Class referenceClass = pd.getPropertyType(); + + referenceBean = buildReferenceBean(reference, referenceClass); + + ReflectionUtils.makeAccessible(method); + + method.invoke(bean, referenceBean.getObject()); - private void init() { - this.bean = referenceBean.get(); } + } - @Override - protected String buildInjectedObjectCacheKey(Reference reference, Object bean, String beanName, - Class injectedType, InjectionMetadata.InjectedElement injectedElement) { + /** + * {@link Reference} {@link Field} {@link InjectionMetadata.InjectedElement} + */ + private class ReferenceFieldElement extends InjectionMetadata.InjectedElement { - String key = buildReferencedBeanName(reference, injectedType) + - "#source=" + (injectedElement.getMember()) + - "#attributes=" + AnnotationUtils.getAttributes(reference,getEnvironment(),true); + private final Field field; - return key; - } + private final Reference reference; + + private volatile ReferenceBean referenceBean; - private String buildReferencedBeanName(Reference reference, Class injectedType) { + protected ReferenceFieldElement(Field field, Reference reference) { + super(field, null); + this.field = field; + this.reference = reference; + } + + @Override + protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable { + + Class referenceClass = field.getType(); + + referenceBean = buildReferenceBean(reference, referenceClass); + + ReflectionUtils.makeAccessible(field); - ServiceBeanNameBuilder builder = ServiceBeanNameBuilder.create(reference, injectedType, getEnvironment()); + field.set(bean, referenceBean.getObject()); + + } - return getEnvironment().resolvePlaceholders(builder.build()); } - private ReferenceBean buildReferenceBeanIfAbsent(String referencedBeanName, Reference reference, - Class referencedType, ClassLoader classLoader) - throws Exception { + private ReferenceBean buildReferenceBean(Reference reference, Class referenceClass) throws Exception { + + String referenceBeanCacheKey = generateReferenceBeanCacheKey(reference, referenceClass); - ReferenceBean referenceBean = referenceBeanCache.get(referencedBeanName); + ReferenceBean referenceBean = referenceBeansCache.get(referenceBeanCacheKey); if (referenceBean == null) { + ReferenceBeanBuilder beanBuilder = ReferenceBeanBuilder .create(reference, classLoader, applicationContext) - .interfaceClass(referencedType); + .interfaceClass(referenceClass); + referenceBean = beanBuilder.build(); - referenceBeanCache.put(referencedBeanName, referenceBean); + + referenceBeansCache.putIfAbsent(referenceBeanCacheKey, referenceBean); + } return referenceBean; + + } + + + /** + * Generate a cache key of {@link ReferenceBean} + * + * @param reference {@link Reference} + * @param beanClass {@link Class} + * @return + */ + private String generateReferenceBeanCacheKey(Reference reference, Class beanClass) { + + String key = resolveReferenceKey(annotationValues(reference)); + + Environment environment = applicationContext.getEnvironment(); + + key = environment.resolvePlaceholders(key); + + return key; + } - private void cacheInjectedReferenceBean(ReferenceBean referenceBean, - InjectionMetadata.InjectedElement injectedElement) { - if (injectedElement.getMember() instanceof Field) { - injectedFieldReferenceBeanCache.put(injectedElement, referenceBean); - } else if (injectedElement.getMember() instanceof Method) { - injectedMethodReferenceBeanCache.put(injectedElement, referenceBean); + private static String resolveInterfaceName(Reference reference, Class beanClass) + throws IllegalStateException { + + String interfaceName; + if (!"".equals(reference.interfaceName())) { + interfaceName = reference.interfaceName(); + } else if (!void.class.equals(reference.interfaceClass())) { + interfaceName = reference.interfaceClass().getName(); + } else if (beanClass.isInterface()) { + interfaceName = beanClass.getName(); + } else { + throw new IllegalStateException( + "The @Reference undefined interfaceClass or interfaceName, and the property type " + + beanClass.getName() + " is not a interface."); } + + return interfaceName; + } - @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - this.applicationContext = applicationContext; + + /** + * Get {@link ReferenceBean} {@link Map} in injected field. + * + * @return non-null {@link Map} + * @since 2.5.11 + */ + public Map> getInjectedFieldReferenceBeanMap() { + + Map> injectedElementReferenceBeanMap = + new LinkedHashMap>(); + + for (ReferenceInjectionMetadata metadata : injectionMetadataCache.values()) { + + Collection fieldElements = metadata.getFieldElements(); + + for (ReferenceFieldElement fieldElement : fieldElements) { + + injectedElementReferenceBeanMap.put(fieldElement, fieldElement.referenceBean); + + } + + } + + return injectedElementReferenceBeanMap; + } - @Override - public void onApplicationEvent(ApplicationEvent event) { - if (event instanceof ServiceBeanExportedEvent) { - onServiceBeanExportEvent((ServiceBeanExportedEvent) event); - } else if (event instanceof ContextRefreshedEvent) { - onContextRefreshedEvent((ContextRefreshedEvent) event); + /** + * Get {@link ReferenceBean} {@link Map} in injected method. + * + * @return non-null {@link Map} + * @since 2.5.11 + */ + public Map> getInjectedMethodReferenceBeanMap() { + + Map> injectedElementReferenceBeanMap = + new LinkedHashMap>(); + + for (ReferenceInjectionMetadata metadata : injectionMetadataCache.values()) { + + Collection methodElements = metadata.getMethodElements(); + + for (ReferenceMethodElement methodElement : methodElements) { + + injectedElementReferenceBeanMap.put(methodElement, methodElement.referenceBean); + + } + } + + return injectedElementReferenceBeanMap; + } - private void onServiceBeanExportEvent(ServiceBeanExportedEvent event) { - ServiceBean serviceBean = event.getServiceBean(); - initReferenceBeanInvocationHandler(serviceBean); + private T getFieldValue(Object object, String fieldName, Class fieldType) { + + Field field = ReflectionUtils.findField(object.getClass(), fieldName, fieldType); + + ReflectionUtils.makeAccessible(field); + + return (T) ReflectionUtils.getField(field, object); + + } + + /** + * Generate a key based on the annotation. + * + * @param annotations annotation value + * @return unique key, never null will be returned. + * @since 2.7.0 + */ + private String resolveReferenceKey(Map annotations) { + Iterator> annotationVisitor = annotations.entrySet().iterator(); + StringBuilder builder = new StringBuilder(); + while (annotationVisitor.hasNext()) { + Map.Entry attribute = annotationVisitor.next(); + String attributeValue = null; + if (attribute.getValue() instanceof String[]) { + attributeValue = toPlainString((String[]) attribute.getValue()); + } else { + attributeValue = attribute.getValue() == null ? "" : attribute.getValue().toString(); + } + + if (StringUtils.isNotEmpty(attributeValue)) { + if (builder.length() > 0) { + builder.append(Constants.PATH_SEPARATOR); + } + builder.append(attributeValue); + } + } + return builder.toString(); } - private void initReferenceBeanInvocationHandler(ServiceBean serviceBean) { - String serviceBeanName = serviceBean.getBeanName(); - // Remove ServiceBean when it's exported - ReferenceBeanInvocationHandler handler = localReferenceBeanInvocationHandlerCache.remove(serviceBeanName); - // Initialize - if (handler != null) { - handler.init(); + private Map annotationValues(Annotation annotation) { + Map annotations = new LinkedHashMap<>(); + + for (Method method : getAnnotationMethods(annotation.annotationType())) { + try { + Object attributeValue = method.invoke(annotation); + Object defaultValue = method.getDefaultValue(); + if (nullSafeEquals(attributeValue, defaultValue)) { + continue; + } + annotations.put(method.getName(), attributeValue); + } catch (Throwable e) { + throw new IllegalStateException("Failed to obtain annotation attribute value for " + method, e); + } } + return annotations; } - private void onContextRefreshedEvent(ContextRefreshedEvent event) { + private static List getAnnotationMethods(Class annotationType) { + List methods = annotationMethodsCache.get(annotationType); + if (methods != null) { + return methods; + } + + methods = new ArrayList(); + for (Method method : annotationType.getDeclaredMethods()) { + if (isAnnotationMethod(method)) { + ReflectionUtils.makeAccessible(method); + methods.add(method); + } + } + annotationMethodsCache.put(annotationType, methods); + return methods; } + private static boolean isAnnotationMethod(Method method) { + return (method != null + && method.getParameterTypes().length == 0 + && method.getReturnType() != void.class); + } - @Override - public void destroy() throws Exception { - super.destroy(); - this.referenceBeanCache.clear(); - this.localReferenceBeanInvocationHandlerCache.clear(); - this.injectedFieldReferenceBeanCache.clear(); - this.injectedMethodReferenceBeanCache.clear(); + private static boolean nullSafeEquals(Object first, Object another) { + return ObjectUtils.nullSafeEquals(first, another); + } + + private String toPlainString(String[] array) { + if (array == null || array.length == 0) { + return ""; + } + StringBuilder buffer = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + if (i > 0) { + buffer.append(Constants.COMMA_SEPARATOR); + } + buffer.append(array[i]); + } + return buffer.toString(); } } diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilder.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilder.java index d4aef6a37ec..617b5f15ff9 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilder.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilder.java @@ -16,27 +16,22 @@ */ package org.apache.dubbo.config.spring.beans.factory.annotation; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.config.ConsumerConfig; -import org.apache.dubbo.config.MethodConfig; -import org.apache.dubbo.config.annotation.Method; import org.apache.dubbo.config.annotation.Reference; import org.apache.dubbo.config.spring.ReferenceBean; +import org.apache.dubbo.config.spring.convert.converter.StringArrayToMapConverter; +import org.apache.dubbo.config.spring.convert.converter.StringArrayToStringConverter; -import org.springframework.beans.propertyeditors.StringTrimmerEditor; import org.springframework.context.ApplicationContext; +import org.springframework.core.convert.ConversionService; +import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; import org.springframework.validation.DataBinder; -import java.beans.PropertyEditorSupport; -import java.util.List; -import java.util.Map; - import static org.apache.dubbo.config.spring.util.BeanFactoryUtils.getOptionalBean; import static org.apache.dubbo.config.spring.util.ObjectUtils.of; -import static org.springframework.util.StringUtils.commaDelimitedListToStringArray; /** * {@link ReferenceBean} Builder @@ -45,8 +40,6 @@ */ class ReferenceBeanBuilder extends AbstractAnnotationConfigBeanBuilder { - // Ignore those fields - static final String[] IGNORE_FIELD_NAMES = of("application", "module", "consumer", "monitor", "registry"); private ReferenceBeanBuilder(Reference annotation, ClassLoader classLoader, ApplicationContext applicationContext) { super(annotation, classLoader, applicationContext); @@ -92,14 +85,6 @@ private void configureConsumerConfig(Reference reference, ReferenceBean refer } - void configureMethodConfig(Reference reference, ReferenceBean referenceBean){ - Method[] methods = reference.methods(); - List methodConfigs = MethodConfig.constructMethodConfig(methods); - if(!methodConfigs.isEmpty()){ - referenceBean.setMethods(methodConfigs); - } - } - @Override protected ReferenceBean doBuild() { return new ReferenceBean(); @@ -109,30 +94,20 @@ protected ReferenceBean doBuild() { protected void preConfigureBean(Reference reference, ReferenceBean referenceBean) { Assert.notNull(interfaceClass, "The interface class must set first!"); DataBinder dataBinder = new DataBinder(referenceBean); - // Register CustomEditors for special fields - dataBinder.registerCustomEditor(String.class, "filter", new StringTrimmerEditor(true)); - dataBinder.registerCustomEditor(String.class, "listener", new StringTrimmerEditor(true)); - dataBinder.registerCustomEditor(Map.class, "parameters", new PropertyEditorSupport() { - @Override - public void setAsText(String text) throws java.lang.IllegalArgumentException { - // Trim all whitespace - String content = StringUtils.trimAllWhitespace(text); - if (!StringUtils.hasText(content)) { // No content , ignore directly - return; - } - // replace "=" to "," - content = StringUtils.replace(content, "=", ","); - // replace ":" to "," - content = StringUtils.replace(content, ":", ","); - // String[] to Map - Map parameters = CollectionUtils.toStringMap(commaDelimitedListToStringArray(content)); - setValue(parameters); - } - }); - + // Set ConversionService + dataBinder.setConversionService(getConversionService()); + // Ignore those fields + String[] ignoreAttributeNames = of("application", "module", "consumer", "monitor", "registry"); +// dataBinder.setDisallowedFields(ignoreAttributeNames); // Bind annotation attributes - dataBinder.bind(new AnnotationPropertyValuesAdapter(reference, applicationContext.getEnvironment(), IGNORE_FIELD_NAMES)); + dataBinder.bind(new AnnotationPropertyValuesAdapter(reference, applicationContext.getEnvironment(), ignoreAttributeNames)); + } + private ConversionService getConversionService() { + DefaultConversionService conversionService = new DefaultConversionService(); + conversionService.addConverter(new StringArrayToStringConverter()); + conversionService.addConverter(new StringArrayToMapConverter()); + return conversionService; } @@ -165,8 +140,6 @@ protected void postConfigureBean(Reference annotation, ReferenceBean bean) throw configureConsumerConfig(annotation, bean); - configureMethodConfig(annotation, bean); - bean.afterPropertiesSet(); } diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java index 22fa70422d5..37fed800348 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java @@ -18,7 +18,6 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.config.MethodConfig; import org.apache.dubbo.config.annotation.Service; import org.apache.dubbo.config.spring.ServiceBean; import org.apache.dubbo.config.spring.context.annotation.DubboClassPathBeanDefinitionScanner; @@ -73,6 +72,7 @@ public class ServiceAnnotationBeanPostProcessor implements BeanDefinitionRegistryPostProcessor, EnvironmentAware, ResourceLoaderAware, BeanClassLoaderAware { + private static final String SEPARATOR = ":"; private final Logger logger = LoggerFactory.getLogger(getClass()); @@ -208,7 +208,7 @@ private BeanNameGenerator resolveBeanNameGenerator(BeanDefinitionRegistry regist * {@link Service} Annotation. * * @param scanner {@link ClassPathBeanDefinitionScanner} - * @param packageToScan pachage to scan + * @param packageToScan package to scan * @param registry {@link BeanDefinitionRegistry} * @return non-null * @since 2.5.8 @@ -262,8 +262,8 @@ private void registerServiceBean(BeanDefinitionHolder beanDefinitionHolder, Bean if (scanner.checkCandidate(beanName, serviceBeanDefinition)) { // check duplicated candidate bean registry.registerBeanDefinition(beanName, serviceBeanDefinition); - if (logger.isInfoEnabled()) { - logger.info("The BeanDefinition[" + serviceBeanDefinition + + if (logger.isWarnEnabled()) { + logger.warn("The BeanDefinition[" + serviceBeanDefinition + "] of ServiceBean has been registered with name : " + beanName); } @@ -290,10 +290,27 @@ private void registerServiceBean(BeanDefinitionHolder beanDefinitionHolder, Bean */ private String generateServiceBeanName(Service service, Class interfaceClass, String annotatedServiceBeanName) { - ServiceBeanNameBuilder builder = ServiceBeanNameBuilder.create(service, interfaceClass, environment); + StringBuilder beanNameBuilder = new StringBuilder(ServiceBean.class.getSimpleName()); + + beanNameBuilder.append(SEPARATOR).append(annotatedServiceBeanName); + + String interfaceClassName = interfaceClass.getName(); + + beanNameBuilder.append(SEPARATOR).append(interfaceClassName); + + String version = service.version(); + if (StringUtils.hasText(version)) { + beanNameBuilder.append(SEPARATOR).append(version); + } + + String group = service.group(); + + if (StringUtils.hasText(group)) { + beanNameBuilder.append(SEPARATOR).append(group); + } - return builder.build(); + return beanNameBuilder.toString(); } @@ -370,8 +387,7 @@ private AbstractBeanDefinition buildServiceBeanDefinition(Service service, Class MutablePropertyValues propertyValues = beanDefinition.getPropertyValues(); - String[] ignoreAttributeNames = of("provider", "monitor", "application", "module", "registry", "protocol", - "interface", "interfaceName"); + String[] ignoreAttributeNames = of("provider", "monitor", "application", "module", "registry", "protocol", "interface", "interfaceName"); propertyValues.addPropertyValues(new AnnotationPropertyValuesAdapter(service, environment, ignoreAttributeNames)); @@ -435,11 +451,6 @@ private AbstractBeanDefinition buildServiceBeanDefinition(Service service, Class builder.addPropertyValue("protocols", protocolRuntimeBeanReferences); } - List methodConfigs = MethodConfig.constructMethodConfig(service.methods()); - if (!methodConfigs.isEmpty()) { - builder.addPropertyValue("methods", methodConfigs); - } - return builder.getBeanDefinition(); } @@ -490,4 +501,4 @@ public void setBeanClassLoader(ClassLoader classLoader) { this.classLoader = classLoader; } -} +} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java deleted file mode 100644 index 6cd712408be..00000000000 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.beans.factory.annotation; - -import org.apache.dubbo.config.annotation.Reference; -import org.apache.dubbo.config.annotation.Service; -import org.apache.dubbo.config.spring.ReferenceBean; -import org.apache.dubbo.config.spring.ServiceBean; - -import org.springframework.core.env.Environment; -import org.springframework.util.StringUtils; - -import static org.apache.dubbo.config.spring.util.AnnotationUtils.resolveInterfaceName; - - -/** - * Dubbo {@link Service @Service} Bean Builder - * - * @see Service - * @see Reference - * @see ServiceBean - * @see ReferenceBean - * @since 2.6.5 - */ -class ServiceBeanNameBuilder { - - private static final String SEPARATOR = ":"; - - private final String interfaceClassName; - - private final Environment environment; - - // Optional - private String version; - - private String group; - - private ServiceBeanNameBuilder(String interfaceClassName, Environment environment) { - this.interfaceClassName = interfaceClassName; - this.environment = environment; - } - - private ServiceBeanNameBuilder(Class interfaceClass, Environment environment) { - this(interfaceClass.getName(), environment); - } - - private ServiceBeanNameBuilder(Service service, Class interfaceClass, Environment environment) { - this(resolveInterfaceName(service, interfaceClass), environment); - this.group(service.group()); - this.version(service.version()); - } - - private ServiceBeanNameBuilder(Reference reference, Class interfaceClass, Environment environment) { - this(resolveInterfaceName(reference, interfaceClass), environment); - this.group(reference.group()); - this.version(reference.version()); - } - - public static ServiceBeanNameBuilder create(Class interfaceClass, Environment environment) { - return new ServiceBeanNameBuilder(interfaceClass, environment); - } - - public static ServiceBeanNameBuilder create(Service service, Class interfaceClass, Environment environment) { - return new ServiceBeanNameBuilder(service, interfaceClass, environment); - } - - public static ServiceBeanNameBuilder create(Reference reference, Class interfaceClass, Environment environment) { - return new ServiceBeanNameBuilder(reference, interfaceClass, environment); - } - - private static void append(StringBuilder builder, String value) { - if (StringUtils.hasText(value)) { - builder.append(SEPARATOR).append(value); - } - } - - public ServiceBeanNameBuilder group(String group) { - this.group = group; - return this; - } - - public ServiceBeanNameBuilder version(String version) { - this.version = version; - return this; - } - - public String build() { - StringBuilder beanNameBuilder = new StringBuilder("ServiceBean"); - // Required - append(beanNameBuilder, interfaceClassName); - // Optional - append(beanNameBuilder, version); - append(beanNameBuilder, group); - // Build - String rawBeanName = beanNameBuilder.toString(); - // Resolve placeholders - return environment.resolvePlaceholders(rawBeanName); - } -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingRegistrar.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingRegistrar.java index e797e5585ec..52ab0e67129 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingRegistrar.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingRegistrar.java @@ -86,7 +86,7 @@ private void registerDubboConfigBeans(String prefix, boolean multiple, BeanDefinitionRegistry registry) { - Map properties = getSubProperties(environment.getPropertySources(), prefix); + Map properties = getSubProperties(environment.getPropertySources(), prefix); if (CollectionUtils.isEmpty(properties)) { if (log.isDebugEnabled()) { @@ -158,7 +158,7 @@ public void setEnvironment(Environment environment) { } - private Set resolveMultipleBeanNames(Map properties) { + private Set resolveMultipleBeanNames(Map properties) { Set beanNames = new LinkedHashSet(); @@ -179,10 +179,10 @@ private Set resolveMultipleBeanNames(Map properties) { } - private String resolveSingleBeanName(Map properties, Class configClass, + private String resolveSingleBeanName(Map properties, Class configClass, BeanDefinitionRegistry registry) { - String beanName = (String) properties.get("id"); + String beanName = properties.get("id"); if (!StringUtils.hasText(beanName)) { BeanDefinitionBuilder builder = rootBeanDefinition(configClass); diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfiguration.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfiguration.java index 3c5a43f0227..de60b5d2cfa 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfiguration.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfiguration.java @@ -26,7 +26,6 @@ import org.apache.dubbo.config.ProviderConfig; import org.apache.dubbo.config.RegistryConfig; import org.apache.dubbo.config.spring.ConfigCenterBean; - import org.springframework.context.annotation.Configuration; /** diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfigurationRegistrar.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfigurationSelector.java similarity index 66% rename from dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfigurationRegistrar.java rename to dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfigurationSelector.java index cc5f4f8c722..639ad971b4d 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfigurationRegistrar.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfigurationSelector.java @@ -18,26 +18,22 @@ import org.apache.dubbo.config.AbstractConfig; -import org.springframework.beans.factory.support.BeanDefinitionRegistry; -import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; +import org.springframework.context.annotation.ImportSelector; import org.springframework.core.Ordered; import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; -import static org.apache.dubbo.config.spring.util.AnnotatedBeanDefinitionRegistryUtils.registerBeans; - /** - * Dubbo {@link AbstractConfig Config} {@link ImportBeanDefinitionRegistrar register}, which order can be configured + * Dubbo {@link AbstractConfig Config} Registrar * * @see EnableDubboConfig * @see DubboConfigConfiguration - * @see Ordered * @since 2.5.8 */ -public class DubboConfigConfigurationRegistrar implements ImportBeanDefinitionRegistrar { +public class DubboConfigConfigurationSelector implements ImportSelector, Ordered { @Override - public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { + public String[] selectImports(AnnotationMetadata importingClassMetadata) { AnnotationAttributes attributes = AnnotationAttributes.fromMap( importingClassMetadata.getAnnotationAttributes(EnableDubboConfig.class.getName())); @@ -45,10 +41,20 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B boolean multiple = attributes.getBoolean("multiple"); if (multiple) { - registerBeans(registry, DubboConfigConfiguration.Multiple.class); + return of(DubboConfigConfiguration.Multiple.class.getName()); } else { - registerBeans(registry, DubboConfigConfiguration.Single.class); + return of(DubboConfigConfiguration.Single.class.getName()); } } + private static T[] of(T... values) { + return values; + } + + @Override + public int getOrder() { + return HIGHEST_PRECEDENCE; + } + + } diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfig.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfig.java index 47bafa53b54..889e39078b0 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfig.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfig.java @@ -66,7 +66,7 @@ @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented -@Import(DubboConfigConfigurationRegistrar.class) +@Import(DubboConfigConfigurationSelector.class) public @interface EnableDubboConfig { /** diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigBinding.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigBinding.java index 54fc3373eac..8b50f367286 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigBinding.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigBinding.java @@ -27,7 +27,6 @@ import java.lang.annotation.Documented; import java.lang.annotation.ElementType; -import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @@ -49,7 +48,6 @@ @Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented -@Repeatable(EnableDubboConfigBindings.class) @Import(DubboConfigBindingRegistrar.class) public @interface EnableDubboConfigBinding { diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/event/ServiceBeanExportedEvent.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/event/ServiceBeanExportedEvent.java deleted file mode 100644 index 93c950dfcde..00000000000 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/event/ServiceBeanExportedEvent.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.context.event; - -import org.apache.dubbo.config.spring.ServiceBean; - -import org.springframework.context.ApplicationEvent; -import org.springframework.context.ApplicationListener; - -/** - * A {@link ApplicationEvent} after {@link ServiceBean} {@link ServiceBean#export() export} invocation - * - * @see ApplicationEvent - * @see ApplicationListener - * @see ServiceBean - * @since 2.6.5 - */ -public class ServiceBeanExportedEvent extends ApplicationEvent { - - /** - * Create a new ApplicationEvent. - * - * @param serviceBean {@link ServiceBean} bean - */ - public ServiceBeanExportedEvent(ServiceBean serviceBean) { - super(serviceBean); - } - - /** - * Get {@link ServiceBean} instance - * - * @return non-null - */ - public ServiceBean getServiceBean() { - return (ServiceBean) super.getSource(); - } -} diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/properties/DefaultDubboConfigBinder.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/properties/DefaultDubboConfigBinder.java index ca10d3eb537..e1d44da8f7f 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/properties/DefaultDubboConfigBinder.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/properties/DefaultDubboConfigBinder.java @@ -37,7 +37,7 @@ public void bind(String prefix, C dubboConfig) { dataBinder.setIgnoreInvalidFields(isIgnoreInvalidFields()); dataBinder.setIgnoreUnknownFields(isIgnoreUnknownFields()); // Get properties under specified prefix from PropertySources - Map properties = getSubProperties(getPropertySources(), prefix); + Map properties = getSubProperties(getPropertySources(), prefix); // Convert Map to MutablePropertyValues MutablePropertyValues propertyValues = new MutablePropertyValues(properties); // Bind @@ -45,4 +45,3 @@ public void bind(String prefix, C dubboConfig) { } } - diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/CompatibleRouter2.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/convert/converter/StringArrayToMapConverter.java similarity index 58% rename from dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/CompatibleRouter2.java rename to dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/convert/converter/StringArrayToMapConverter.java index 13350d4572f..a2e4790919b 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/CompatibleRouter2.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/convert/converter/StringArrayToMapConverter.java @@ -14,32 +14,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.rpc.cluster; +package org.apache.dubbo.config.spring.convert.converter; -import com.alibaba.dubbo.common.URL; -import com.alibaba.dubbo.rpc.Invocation; -import com.alibaba.dubbo.rpc.Invoker; -import com.alibaba.dubbo.rpc.RpcException; -import com.alibaba.dubbo.rpc.cluster.Router; +import org.apache.dubbo.common.utils.CollectionUtils; -import java.util.List; +import org.springframework.core.convert.converter.Converter; +import org.springframework.util.ObjectUtils; + +import java.util.Map; /** + * {@link String}[] to {@link Map} {@link Converter} * + * @see CollectionUtils#toStringMap(String[]) + * @since 2.5.11 */ -public class CompatibleRouter2 implements Router { - @Override - public URL getUrl() { - return null; - } +public class StringArrayToMapConverter implements Converter> { @Override - public List> route(List> invokers, URL url, Invocation invocation) throws RpcException { - return null; + public Map convert(String[] source) { + return ObjectUtils.isEmpty(source) ? null : CollectionUtils.toStringMap(source); } - @Override - public int compareTo(Router o) { - return 0; - } } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/provider/DefaultHelloService.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/convert/converter/StringArrayToStringConverter.java similarity index 59% rename from dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/provider/DefaultHelloService.java rename to dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/convert/converter/StringArrayToStringConverter.java index b484d26a931..7cd6e7124aa 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/provider/DefaultHelloService.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/convert/converter/StringArrayToStringConverter.java @@ -14,25 +14,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.config.spring.context.context.annotation.provider; +package org.apache.dubbo.config.spring.convert.converter; -import org.apache.dubbo.config.spring.api.HelloService; +import org.springframework.core.convert.converter.Converter; +import org.springframework.util.ObjectUtils; +import org.springframework.util.StringUtils; -import org.springframework.stereotype.Service; /** - * Default {@link HelloService} annotation with Spring's {@link Service} - * and Dubbo's {@link org.apache.dubbo.config.annotation.Service} + * String[] to String {@ConditionalGenericConverter} * - * @since TODO + * @see StringUtils#arrayToCommaDelimitedString(Object[]) + * @since 2.5.11 */ -@Service -@org.apache.dubbo.config.annotation.Service -public class DefaultHelloService implements HelloService { +public class StringArrayToStringConverter implements Converter { @Override - public String sayHello(String name) { - return "Greeting, " + name; + public String convert(String[] source) { + return ObjectUtils.isEmpty(source) ? null : StringUtils.arrayToCommaDelimitedString(source); } } diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/AnnotationBeanDefinitionParser.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/AnnotationBeanDefinitionParser.java index 4a8e963c111..918c916f3c8 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/AnnotationBeanDefinitionParser.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/AnnotationBeanDefinitionParser.java @@ -16,6 +16,7 @@ */ package org.apache.dubbo.config.spring.schema; +import org.apache.dubbo.config.spring.AnnotationBean; import org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor; import org.apache.dubbo.config.spring.beans.factory.annotation.ServiceAnnotationBeanPostProcessor; import org.apache.dubbo.config.spring.util.BeanRegistrar; @@ -25,6 +26,7 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; +import org.springframework.beans.factory.xml.BeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; import org.w3c.dom.Element; @@ -32,7 +34,7 @@ import static org.springframework.util.StringUtils.trimArrayElements; /** - * @link BeanDefinitionParser} + * {@link AnnotationBean} {@link BeanDefinitionParser} * * @see ServiceAnnotationBeanPostProcessor * @see ReferenceAnnotationBeanPostProcessor diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/DubboBeanDefinitionParser.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/DubboBeanDefinitionParser.java index 9272b7f5a1b..9defb056e47 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/DubboBeanDefinitionParser.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/DubboBeanDefinitionParser.java @@ -75,16 +75,16 @@ private static BeanDefinition parse(Element element, ParserContext parserContext beanDefinition.setBeanClass(beanClass); beanDefinition.setLazyInit(false); String id = element.getAttribute("id"); - if (StringUtils.isEmpty(id) && required) { + if ((id == null || id.length() == 0) && required) { String generatedBeanName = element.getAttribute("name"); - if (StringUtils.isEmpty(generatedBeanName)) { + if (generatedBeanName == null || generatedBeanName.length() == 0) { if (ProtocolConfig.class.equals(beanClass)) { generatedBeanName = "dubbo"; } else { generatedBeanName = element.getAttribute("interface"); } } - if (StringUtils.isEmpty(generatedBeanName)) { + if (generatedBeanName == null || generatedBeanName.length() == 0) { generatedBeanName = beanClass.getName(); } id = generatedBeanName; @@ -258,7 +258,7 @@ private static void parseNested(Element element, ParserContext parserContext, Cl if (first) { first = false; String isDefault = element.getAttribute("default"); - if (StringUtils.isEmpty(isDefault)) { + if (isDefault == null || isDefault.length() == 0) { beanDefinition.getPropertyValues().addPropertyValue("default", "false"); } } @@ -335,7 +335,7 @@ private static void parseMethods(String id, NodeList nodeList, RootBeanDefinitio Element element = (Element) node; if ("method".equals(node.getNodeName()) || "method".equals(node.getLocalName())) { String methodName = element.getAttribute("name"); - if (StringUtils.isEmpty(methodName)) { + if (methodName == null || methodName.length() == 0) { throw new IllegalStateException(" name attribute == null"); } if (methods == null) { diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/status/DataSourceStatusChecker.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/status/DataSourceStatusChecker.java index 3fba654c9db..5f268065b43 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/status/DataSourceStatusChecker.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/status/DataSourceStatusChecker.java @@ -21,7 +21,6 @@ import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.status.Status; import org.apache.dubbo.common.status.StatusChecker; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.config.spring.extension.SpringExtensionFactory; import org.springframework.context.ApplicationContext; @@ -56,7 +55,7 @@ public Status check() { } Map dataSources = context.getBeansOfType(DataSource.class, false, false); - if (CollectionUtils.isEmptyMap(dataSources)) { + if (dataSources == null || dataSources.size() == 0) { return new Status(Status.Level.UNKNOWN); } Status.Level level = Status.Level.OK; @@ -67,20 +66,27 @@ public Status check() { buf.append(", "); } buf.append(entry.getKey()); - - try (Connection connection = dataSource.getConnection()) { - DatabaseMetaData metaData = connection.getMetaData(); - try (ResultSet resultSet = metaData.getTypeInfo()) { - if (!resultSet.next()) { - level = Status.Level.ERROR; + try { + Connection connection = dataSource.getConnection(); + try { + DatabaseMetaData metaData = connection.getMetaData(); + ResultSet resultSet = metaData.getTypeInfo(); + try { + if (!resultSet.next()) { + level = Status.Level.ERROR; + } + } finally { + resultSet.close(); } + buf.append(metaData.getURL()); + buf.append("("); + buf.append(metaData.getDatabaseProductName()); + buf.append("-"); + buf.append(metaData.getDatabaseProductVersion()); + buf.append(")"); + } finally { + connection.close(); } - buf.append(metaData.getURL()); - buf.append("("); - buf.append(metaData.getDatabaseProductName()); - buf.append("-"); - buf.append(metaData.getDatabaseProductVersion()); - buf.append(")"); } catch (Throwable e) { logger.warn(e.getMessage(), e); return new Status(level, e.getMessage()); diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtils.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtils.java deleted file mode 100644 index bf065f8b085..00000000000 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtils.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.util; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.support.BeanDefinitionRegistry; -import org.springframework.context.annotation.AnnotatedBeanDefinitionReader; -import org.springframework.util.ObjectUtils; - -import java.lang.annotation.Annotation; -import java.util.Arrays; - -/** - * Annotated {@link BeanDefinition} Utilities - *

    - * The source code is cloned from https://github.com/alibaba/spring-context-support/blob/1.0.2/src/main/java/com/alibaba/spring/util/AnnotatedBeanDefinitionRegistryUtils.java - * @since 2.6.6 - */ -public abstract class AnnotatedBeanDefinitionRegistryUtils { - - private static final Log logger = LogFactory.getLog(AnnotatedBeanDefinitionRegistryUtils.class); - - /** - * Register Beans - * - * @param registry {@link BeanDefinitionRegistry} - * @param annotatedClasses {@link Annotation annotation} class - */ - public static void registerBeans(BeanDefinitionRegistry registry, Class... annotatedClasses) { - - if (ObjectUtils.isEmpty(annotatedClasses)) { - return; - } - - boolean debugEnabled = logger.isDebugEnabled(); - - AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(registry); - - if (debugEnabled) { - logger.debug(registry.getClass().getSimpleName() + " will register annotated classes : " + Arrays.asList(annotatedClasses) + " ."); - } - - reader.register(annotatedClasses); - - } -} diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotationUtils.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotationUtils.java index 9832e105624..70c04ccb436 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotationUtils.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotationUtils.java @@ -16,34 +16,20 @@ */ package org.apache.dubbo.config.spring.util; -import org.apache.dubbo.config.annotation.Reference; -import org.apache.dubbo.config.annotation.Service; - -import org.springframework.core.env.Environment; import org.springframework.core.env.PropertyResolver; -import org.springframework.util.StringUtils; import java.lang.annotation.Annotation; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; -import java.lang.reflect.Method; -import java.util.Collections; import java.util.HashSet; import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; import java.util.Map; import java.util.Set; import static java.lang.String.valueOf; -import static org.springframework.core.annotation.AnnotationUtils.findAnnotation; import static org.springframework.core.annotation.AnnotationUtils.getAnnotationAttributes; import static org.springframework.core.annotation.AnnotationUtils.getDefaultValue; import static org.springframework.util.CollectionUtils.arrayToList; import static org.springframework.util.ObjectUtils.nullSafeEquals; -import static org.springframework.util.StringUtils.trimWhitespace; +import static org.springframework.util.StringUtils.trimAllWhitespace; /** * Annotation Utilities Class @@ -53,186 +39,13 @@ */ public class AnnotationUtils { - public static String resolveInterfaceName(Service service, Class defaultInterfaceClass) - throws IllegalStateException { - - String interfaceName; - if (StringUtils.hasText(service.interfaceName())) { - interfaceName = service.interfaceName(); - } else if (!void.class.equals(service.interfaceClass())) { - interfaceName = service.interfaceClass().getName(); - } else if (defaultInterfaceClass.isInterface()) { - interfaceName = defaultInterfaceClass.getName(); - } else { - throw new IllegalStateException( - "The @Service undefined interfaceClass or interfaceName, and the type " - + defaultInterfaceClass.getName() + " is not a interface."); - } - - return interfaceName; - - } - - public static String resolveInterfaceName(Reference reference, Class defaultInterfaceClass) - throws IllegalStateException { - - String interfaceName; - if (!"".equals(reference.interfaceName())) { - interfaceName = reference.interfaceName(); - } else if (!void.class.equals(reference.interfaceClass())) { - interfaceName = reference.interfaceClass().getName(); - } else if (defaultInterfaceClass.isInterface()) { - interfaceName = defaultInterfaceClass.getName(); - } else { - throw new IllegalStateException( - "The @Reference undefined interfaceClass or interfaceName, and the type " - + defaultInterfaceClass.getName() + " is not a interface."); - } - - return interfaceName; - - } - - - // Cloned from https://github.com/alibaba/spring-context-support/blob/1.0.2/src/main/java/com/alibaba/spring/util/AnnotationUtils.java - - /** - * Is specified {@link Annotation} present on {@link Method}'s declaring class or parameters or itself. - * - * @param method {@link Method} - * @param annotationClass {@link Annotation} type - * @param {@link Annotation} type - * @return If present , return true , or false - * @since 2.6.6 - */ - public static boolean isPresent(Method method, Class annotationClass) { - - Map> annotationsMap = findAnnotations(method, annotationClass); - - return !annotationsMap.isEmpty(); - - } - - /** - * Find specified {@link Annotation} type maps from {@link Method} - * - * @param method {@link Method} - * @param annotationClass {@link Annotation} type - * @param {@link Annotation} type - * @return {@link Annotation} type maps , the {@link ElementType} as key , - * the list of {@link Annotation} as value. - * If {@link Annotation} was annotated on {@link Method}'s parameters{@link ElementType#PARAMETER} , - * the associated {@link Annotation} list may contain multiple elements. - * @since 2.6.6 - */ - public static Map> findAnnotations(Method method, - Class annotationClass) { - - Retention retention = annotationClass.getAnnotation(Retention.class); - - RetentionPolicy retentionPolicy = retention.value(); - - if (!RetentionPolicy.RUNTIME.equals(retentionPolicy)) { - return Collections.emptyMap(); - } - - Map> annotationsMap = new LinkedHashMap>(); - - Target target = annotationClass.getAnnotation(Target.class); - - ElementType[] elementTypes = target.value(); - - - for (ElementType elementType : elementTypes) { - - List annotationsList = new LinkedList(); - - switch (elementType) { - - case PARAMETER: - - Annotation[][] parameterAnnotations = method.getParameterAnnotations(); - - for (Annotation[] annotations : parameterAnnotations) { - - for (Annotation annotation : annotations) { - - if (annotationClass.equals(annotation.annotationType())) { - - annotationsList.add((A) annotation); - - } - - } - - } - - break; - - case METHOD: - - A annotation = findAnnotation(method, annotationClass); - - if (annotation != null) { - - annotationsList.add(annotation); - - } - - break; - - case TYPE: - - Class beanType = method.getDeclaringClass(); - - A annotation2 = findAnnotation(beanType, annotationClass); - - if (annotation2 != null) { - - annotationsList.add(annotation2); - - } - - break; - - } - - if (!annotationsList.isEmpty()) { - - annotationsMap.put(elementType, annotationsList); - - } - - - } - - return Collections.unmodifiableMap(annotationsMap); - - } - - /** - * Get the {@link Annotation} attributes - * - * @param annotation specified {@link Annotation} - * @param ignoreDefaultValue whether ignore default value or not - * @param ignoreAttributeNames the attribute names of annotation should be ignored - * @return non-null - * @since 2.6.6 - */ - public static Map getAttributes(Annotation annotation, boolean ignoreDefaultValue, - String... ignoreAttributeNames) { - return getAttributes(annotation, null, ignoreDefaultValue, ignoreAttributeNames); - } - /** - * Get the {@link Annotation} attributes + * Get {@link Annotation} attributes * - * @param annotation specified {@link Annotation} - * @param propertyResolver {@link PropertyResolver} instance, e.g {@link Environment} - * @param ignoreDefaultValue whether ignore default value or not - * @param ignoreAttributeNames the attribute names of annotation should be ignored + * @param annotation + * @param propertyResolver + * @param ignoreDefaultValue * @return non-null - * @since 2.6.6 */ public static Map getAttributes(Annotation annotation, PropertyResolver propertyResolver, boolean ignoreDefaultValue, String... ignoreAttributeNames) { @@ -243,6 +56,8 @@ public static Map getAttributes(Annotation annotation, PropertyR Map actualAttributes = new LinkedHashMap(); + boolean requiredResolve = propertyResolver != null; + for (Map.Entry entry : attributes.entrySet()) { String attributeName = entry.getKey(); @@ -258,38 +73,17 @@ public static Map getAttributes(Annotation annotation, PropertyR continue; } - /** - * @since 2.7.1 - * ignore annotation member - */ - if (attributeValue.getClass().isAnnotation()){ - continue; - } - if (attributeValue.getClass().isArray() && attributeValue.getClass().getComponentType().isAnnotation()){ - continue; + if (requiredResolve && attributeValue instanceof String) { // Resolve Placeholder + String resolvedValue = propertyResolver.resolvePlaceholders(valueOf(attributeValue)); + attributeValue = trimAllWhitespace(resolvedValue); } - if (attributeValue instanceof String) { - attributeValue = resolvePlaceholders(valueOf(attributeValue), propertyResolver); - } else if (attributeValue instanceof String[]) { - String[] values = (String[]) attributeValue; - for (int i = 0; i < values.length; i++) { - values[i] = resolvePlaceholders(values[i], propertyResolver); - } - attributeValue = values; - } actualAttributes.put(attributeName, attributeValue); + } + return actualAttributes; - } - private static String resolvePlaceholders(String attributeValue, PropertyResolver propertyResolver) { - String resolvedValue = attributeValue; - if (propertyResolver != null) { - resolvedValue = propertyResolver.resolvePlaceholders(resolvedValue); - resolvedValue = trimWhitespace(resolvedValue); - } - return resolvedValue; } } diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/BeanFactoryUtils.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/BeanFactoryUtils.java index 71fb7676f88..e519cdee280 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/BeanFactoryUtils.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/BeanFactoryUtils.java @@ -16,6 +16,8 @@ */ package org.apache.dubbo.config.spring.util; +import org.apache.dubbo.common.utils.StringUtils; + import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory; @@ -31,7 +33,6 @@ import static org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors; import static org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors; -import static org.springframework.util.ObjectUtils.containsElement; /** * {@link BeanFactory} Utilities class @@ -43,29 +44,6 @@ */ public class BeanFactoryUtils { - public static boolean addApplicationListener(ApplicationContext applicationContext, ApplicationListener listener) { - try { - // backward compatibility to spring 2.0.1 - Method method = applicationContext.getClass().getMethod("addApplicationListener", ApplicationListener.class); - method.invoke(applicationContext, listener); - return true; - } catch (Throwable t) { - if (applicationContext instanceof AbstractApplicationContext) { - try { - // backward compatibility to spring 2.0.1 - Method method = AbstractApplicationContext.class.getDeclaredMethod("addListener", ApplicationListener.class); - if (!method.isAccessible()) { - method.setAccessible(true); - } - method.invoke(applicationContext, listener); - return true; - } catch (Throwable t2) { - // ignore - } - } - } - return false; - } /** * Get optional Bean @@ -75,13 +53,12 @@ public static boolean addApplicationListener(ApplicationContext applicationConte * @param beanType the {@link Class type} of Bean * @param the {@link Class type} of Bean * @return A bean if present , or null - * @since 2.6.6 */ public static T getOptionalBean(ListableBeanFactory beanFactory, String beanName, Class beanType) { String[] allBeanNames = beanNamesForTypeIncludingAncestors(beanFactory, beanType); - if (!containsElement(allBeanNames, beanName)) { + if (!StringUtils.isContains(allBeanNames, beanName)) { return null; } @@ -99,8 +76,7 @@ public static T getOptionalBean(ListableBeanFactory beanFactory, String bean * @param beanNames the names of Bean * @param beanType the {@link Class type} of Bean * @param the {@link Class type} of Bean - * @return the read-only and non-null {@link List} of Bean names - * @since 2.6.6 + * @return */ public static List getBeans(ListableBeanFactory beanFactory, String[] beanNames, Class beanType) { @@ -109,11 +85,36 @@ public static List getBeans(ListableBeanFactory beanFactory, String[] bea List beans = new ArrayList(beanNames.length); for (String beanName : beanNames) { - if (containsElement(allBeanNames, beanName)) { + if (StringUtils.isContains(allBeanNames, beanName)) { beans.add(beanFactory.getBean(beanName, beanType)); } } return Collections.unmodifiableList(beans); + + } + + public static boolean addApplicationListener(ApplicationContext applicationContext, ApplicationListener listener) { + try { + // backward compatibility to spring 2.0.1 + Method method = applicationContext.getClass().getMethod("addApplicationListener", ApplicationListener.class); + method.invoke(applicationContext, listener); + return true; + } catch (Throwable t) { + if (applicationContext instanceof AbstractApplicationContext) { + try { + // backward compatibility to spring 2.0.1 + Method method = AbstractApplicationContext.class.getDeclaredMethod("addListener", ApplicationListener.class); + if (!method.isAccessible()) { + method.setAccessible(true); + } + method.invoke(applicationContext, listener); + return true; + } catch (Throwable t2) { + // ignore + } + } + } + return false; } } diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/ClassUtils.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/ClassUtils.java deleted file mode 100644 index 29cd6378748..00000000000 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/ClassUtils.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.util; - -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; - -/** - * {@link Class} Utilities - *

    - * The source code is cloned from - * https://github.com/alibaba/spring-context-support/blob/1.0.2/src/main/java/com/alibaba/spring/util/ClassUtils.java - * - * @since 2.6.6 - */ -public abstract class ClassUtils { - - public static Class resolveGenericType(Class declaredClass) { - ParameterizedType parameterizedType = (ParameterizedType) declaredClass.getGenericSuperclass(); - Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); - return (Class) actualTypeArguments[0]; - } -} diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/ObjectUtils.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/ObjectUtils.java index 57832cc926b..0ffd7a77a5d 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/ObjectUtils.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/ObjectUtils.java @@ -17,18 +17,18 @@ package org.apache.dubbo.config.spring.util; /** - * Object Utilities + * Object Utilities Class * - * @since 2.6.6 + * @since 2.5.11 */ -public abstract class ObjectUtils { +public class ObjectUtils { /** - * Convert from variable arguments to array + * of factory method * - * @param values variable arguments - * @param The class - * @return array + * @param values + * @param + * @return */ public static T[] of(T... values) { return values; diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/PropertySourcesUtils.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/PropertySourcesUtils.java index 8a070579b34..28d43fc2179 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/PropertySourcesUtils.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/PropertySourcesUtils.java @@ -16,25 +16,20 @@ */ package org.apache.dubbo.config.spring.util; -import org.springframework.core.env.AbstractEnvironment; -import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.EnumerablePropertySource; -import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySources; +import org.springframework.core.env.PropertySourcesPropertyResolver; -import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; - /** * {@link PropertySources} Utilities - *

    - * The source code is cloned from https://github.com/alibaba/spring-context-support/blob/1.0.2/src/main/java/com/alibaba/spring/util/PropertySourcesUtils.java * - * @since 2.6.6 + * @see PropertySources + * @since 2.5.8 */ public abstract class PropertySourcesUtils { @@ -43,60 +38,30 @@ public abstract class PropertySourcesUtils { * * @param propertySources {@link PropertySource} Iterable * @param prefix the prefix of property name - * @return Map - * @see Properties - */ - public static Map getSubProperties(Iterable> propertySources, String prefix) { - - // Non-Extension AbstractEnvironment - AbstractEnvironment environment = new AbstractEnvironment() { - }; - - MutablePropertySources mutablePropertySources = environment.getPropertySources(); - - for (PropertySource source : propertySources) { - mutablePropertySources.addLast(source); - } - - return getSubProperties(environment, prefix); - - } - - /** - * Get Sub {@link Properties} - * - * @param environment {@link ConfigurableEnvironment} - * @param prefix the prefix of property name - * @return Map + * @return Map * @see Properties */ - public static Map getSubProperties(ConfigurableEnvironment environment, String prefix) { - - Map subProperties = new LinkedHashMap(); + public static Map getSubProperties(Iterable> propertySources, String prefix) { - MutablePropertySources propertySources = environment.getPropertySources(); + Map subProperties = new LinkedHashMap(); String normalizedPrefix = normalizePrefix(prefix); + PropertySourcesPropertyResolver propertyResolver = new PropertySourcesPropertyResolver((PropertySources) propertySources); + for (PropertySource source : propertySources) { if (source instanceof EnumerablePropertySource) { for (String name : ((EnumerablePropertySource) source).getPropertyNames()) { - if (!subProperties.containsKey(name) && name.startsWith(normalizedPrefix)) { + if (name.startsWith(normalizedPrefix)) { String subName = name.substring(normalizedPrefix.length()); - if (!subProperties.containsKey(subName)) { // take first one - Object value = source.getProperty(name); - if (value instanceof String) { - // Resolve placeholder - value = environment.resolvePlaceholders((String) value); - } - subProperties.put(subName, value); - } + String value = propertyResolver.getProperty(name); + subProperties.putIfAbsent(subName, value); } } } } - return Collections.unmodifiableMap(subProperties); + return subProperties; } @@ -109,4 +74,5 @@ public static Map getSubProperties(ConfigurableEnvironment envir public static String normalizePrefix(String prefix) { return prefix.endsWith(".") ? prefix : prefix + "."; } + } diff --git a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/compat/dubbo.xsd b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/compat/dubbo.xsd index 4f0b1bf7a16..30e8f14c927 100644 --- a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/compat/dubbo.xsd +++ b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/compat/dubbo.xsd @@ -1310,6 +1310,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd index 41af239deb5..c5b890c7fc4 100644 --- a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd +++ b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd @@ -891,12 +891,6 @@ - - - - - - diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/AbstractRegistryService.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/AbstractRegistryService.java index 6aabdfacac5..3c273d3bb4d 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/AbstractRegistryService.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/AbstractRegistryService.java @@ -19,8 +19,6 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.CollectionUtils; -import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.registry.NotifyListener; import org.apache.dubbo.registry.RegistryService; @@ -207,8 +205,8 @@ protected final void forbid(String service) { } protected final void notify(String service, List urls) { - if (StringUtils.isEmpty(service) - || CollectionUtils.isEmpty(urls)) { + if (service == null || service.length() == 0 + || urls == null || urls.size() == 0) { return; } doNotify(service, urls); diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/ConfigTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/ConfigTest.java index acd75bf98a1..d68a2952968 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/ConfigTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/ConfigTest.java @@ -1,1022 +1,1021 @@ -/* - * 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 org.apache.dubbo.config.spring; - -import org.apache.dubbo.common.Constants; -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.extension.ExtensionLoader; -import org.apache.dubbo.common.utils.NetUtils; -import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.config.ConsumerConfig; -import org.apache.dubbo.config.ProtocolConfig; -import org.apache.dubbo.config.ProviderConfig; -import org.apache.dubbo.config.ReferenceConfig; -import org.apache.dubbo.config.RegistryConfig; -import org.apache.dubbo.config.ServiceConfig; -import org.apache.dubbo.config.spring.action.DemoActionByAnnotation; -import org.apache.dubbo.config.spring.action.DemoActionBySetter; -import org.apache.dubbo.config.spring.annotation.consumer.AnnotationAction; -import org.apache.dubbo.config.spring.api.DemoService; -import org.apache.dubbo.config.spring.api.HelloService; -import org.apache.dubbo.config.spring.context.annotation.provider.ProviderConfiguration; -import org.apache.dubbo.config.spring.filter.MockFilter; -import org.apache.dubbo.config.spring.impl.DemoServiceImpl; -import org.apache.dubbo.config.spring.impl.HelloServiceImpl; -import org.apache.dubbo.config.spring.registry.MockRegistry; -import org.apache.dubbo.config.spring.registry.MockRegistryFactory; -import org.apache.dubbo.registry.Registry; -import org.apache.dubbo.registry.RegistryService; -import org.apache.dubbo.rpc.Exporter; -import org.apache.dubbo.rpc.Filter; -import org.apache.dubbo.rpc.RpcContext; -import org.apache.dubbo.rpc.RpcException; -import org.apache.dubbo.rpc.service.GenericException; -import org.apache.dubbo.rpc.service.GenericService; - -import org.junit.Assert; -import org.junit.Ignore; -import org.junit.Test; -import org.springframework.beans.factory.BeanCreationException; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import java.util.Collection; -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.junit.matchers.JUnitMatchers.containsString; - - -/** - * ConfigTest - */ -@Ignore -public class ConfigTest { - - private static void unexportService(ServiceConfig config) { - if (config != null) { - config.unexport(); - } - } - - @Test - public void testSpringExtensionInject() { - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/spring-extension-inject.xml"); - ctx.start(); - try { - MockFilter filter = (MockFilter) ExtensionLoader.getExtensionLoader(Filter.class).getExtension("mymock"); - assertNotNull(filter.getMockDao()); - assertNotNull(filter.getProtocol()); - assertNotNull(filter.getLoadBalance()); - } finally { - ctx.stop(); - ctx.close(); - } - } - - @Test - public void testServiceClass() { - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/service-class.xml"); - ctx.start(); - try { - DemoService demoService = refer("dubbo://127.0.0.1:30887"); - String hello = demoService.sayName("hello"); - assertEquals("welcome:hello", hello); - } finally { - ctx.stop(); - ctx.close(); - } - } - - @Test - public void testServiceAnnotation() { - AnnotationConfigApplicationContext providerContext = new AnnotationConfigApplicationContext(); - providerContext.register(ProviderConfiguration.class); - - providerContext.refresh(); - - ReferenceConfig reference = new ReferenceConfig(); - reference.setApplication(new ApplicationConfig("consumer")); - reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE)); - reference.setInterface(HelloService.class); - reference.setUrl("dubbo://127.0.0.1:12345"); - String hello = reference.get().sayHello("hello"); - assertEquals("Hello, hello", hello); - - } - - @Test - @SuppressWarnings("unchecked") - public void testProviderNestedService() { - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/provider-nested-service.xml"); - ctx.start(); - try { - ServiceConfig serviceConfig = (ServiceConfig) ctx.getBean("serviceConfig"); - assertNotNull(serviceConfig.getProvider()); - assertEquals(2000, serviceConfig.getProvider().getTimeout().intValue()); - - ServiceConfig serviceConfig2 = (ServiceConfig) ctx.getBean("serviceConfig2"); - assertNotNull(serviceConfig2.getProvider()); - assertEquals(1000, serviceConfig2.getProvider().getTimeout().intValue()); - } finally { - ctx.stop(); - ctx.close(); - } - } - - private DemoService refer(String url) { - ReferenceConfig reference = new ReferenceConfig(); - reference.setApplication(new ApplicationConfig("consumer")); - reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE)); - reference.setInterface(DemoService.class); - reference.setUrl(url); - return reference.get(); - } - - @Test - public void testToString() { - ReferenceConfig reference = new ReferenceConfig(); - reference.setApplication(new ApplicationConfig("consumer")); - reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE)); - reference.setInterface(DemoService.class); - reference.setUrl("dubbo://127.0.0.1:20881"); - String str = reference.toString(); - assertTrue(str.startsWith("")); - } - - @Test - public void testForks() { - ReferenceConfig reference = new ReferenceConfig(); - reference.setApplication(new ApplicationConfig("consumer")); - reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE)); - reference.setInterface(DemoService.class); - reference.setUrl("dubbo://127.0.0.1:20881"); - - int forks = 10; - reference.setForks(forks); - String str = reference.toString(); - assertTrue(str.contains("forks=\"" + forks + "\"")); - } - - @Test - public void testMultiProtocol() { - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-protocol.xml"); - ctx.start(); - try { - DemoService demoService = refer("dubbo://127.0.0.1:20881"); - String hello = demoService.sayName("hello"); - assertEquals("say:hello", hello); - } finally { - ctx.stop(); - ctx.close(); - } - } - - @Test - public void testMultiProtocolDefault() { - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-protocol-default.xml"); - ctx.start(); - try { - DemoService demoService = refer("rmi://127.0.0.1:10991"); - String hello = demoService.sayName("hello"); - assertEquals("say:hello", hello); - } finally { - ctx.stop(); - ctx.close(); - } - } - - @Test - public void testMultiProtocolError() { - try { - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-protocol-error.xml"); - ctx.start(); - ctx.stop(); - ctx.close(); - } catch (BeanCreationException e) { - assertTrue(e.getMessage().contains("Found multi-protocols")); - } - } - - @Test - public void testMultiProtocolRegister() { - SimpleRegistryService registryService = new SimpleRegistryService(); - Exporter exporter = SimpleRegistryExporter.export(4547, registryService); - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-protocol-register.xml"); - ctx.start(); - try { - List urls = registryService.getRegistered().get("org.apache.dubbo.config.spring.api.DemoService"); - assertNotNull(urls); - assertEquals(1, urls.size()); - assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20824/org.apache.dubbo.config.spring.api.DemoService", urls.get(0).toIdentityString()); - } finally { - ctx.stop(); - ctx.close(); - exporter.unexport(); - } - } - - @Test - public void testMultiRegistry() { - SimpleRegistryService registryService1 = new SimpleRegistryService(); - Exporter exporter1 = SimpleRegistryExporter.export(4545, registryService1); - SimpleRegistryService registryService2 = new SimpleRegistryService(); - Exporter exporter2 = SimpleRegistryExporter.export(4546, registryService2); - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-registry.xml"); - ctx.start(); - try { - List urls1 = registryService1.getRegistered().get("org.apache.dubbo.config.spring.api.DemoService"); - assertNull(urls1); - List urls2 = registryService2.getRegistered().get("org.apache.dubbo.config.spring.api.DemoService"); - assertNotNull(urls2); - assertEquals(1, urls2.size()); - assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20880/org.apache.dubbo.config.spring.api.DemoService", urls2.get(0).toIdentityString()); - } finally { - ctx.stop(); - ctx.close(); - exporter1.unexport(); - exporter2.unexport(); - } - } - - @Test - public void testDelayFixedTime() throws Exception { - SimpleRegistryService registryService = new SimpleRegistryService(); - Exporter exporter = SimpleRegistryExporter.export(4548, registryService); - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/delay-fixed-time.xml"); - ctx.start(); - try { - List urls = registryService.getRegistered().get("org.apache.dubbo.config.spring.api.DemoService"); - assertNull(urls); - int i = 0; - while ((i++) < 60 && urls == null) { - urls = registryService.getRegistered().get("org.apache.dubbo.config.spring.api.DemoService"); - Thread.sleep(10); - } - assertNotNull(urls); - assertEquals(1, urls.size()); - assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20888/org.apache.dubbo.config.spring.api.DemoService", urls.get(0).toIdentityString()); - } finally { - ctx.stop(); - ctx.close(); - exporter.unexport(); - } - } - - @Test - public void testDelayOnInitialized() throws Exception { - SimpleRegistryService registryService = new SimpleRegistryService(); - Exporter exporter = SimpleRegistryExporter.export(4548, registryService); - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/delay-on-initialized.xml"); - //ctx.start(); - try { - List urls = registryService.getRegistered().get("org.apache.dubbo.config.spring.api.DemoService"); - assertNotNull(urls); - assertEquals(1, urls.size()); - assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20888/org.apache.dubbo.config.spring.api.DemoService", urls.get(0).toIdentityString()); - } finally { - ctx.stop(); - ctx.close(); - exporter.unexport(); - } - } - - @Test - public void testRmiTimeout() throws Exception { - if (System.getProperty("sun.rmi.transport.tcp.responseTimeout") != null) { - System.setProperty("sun.rmi.transport.tcp.responseTimeout", ""); - } - ConsumerConfig consumer = new ConsumerConfig(); - consumer.setTimeout(1000); - assertEquals("1000", System.getProperty("sun.rmi.transport.tcp.responseTimeout")); - consumer.setTimeout(2000); - assertEquals("1000", System.getProperty("sun.rmi.transport.tcp.responseTimeout")); - } - - @Test - public void testAutowireAndAOP() throws Exception { - ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/demo-provider.xml"); - providerContext.start(); - try { - ClassPathXmlApplicationContext byNameContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/aop-autowire-byname.xml"); - byNameContext.start(); - try { - DemoActionBySetter demoActionBySetter = (DemoActionBySetter) byNameContext.getBean("demoActionBySetter"); - assertNotNull(demoActionBySetter.getDemoService()); - assertEquals("aop:say:hello", demoActionBySetter.getDemoService().sayName("hello")); - DemoActionByAnnotation demoActionByAnnotation = (DemoActionByAnnotation) byNameContext.getBean("demoActionByAnnotation"); - assertNotNull(demoActionByAnnotation.getDemoService()); - assertEquals("aop:say:hello", demoActionByAnnotation.getDemoService().sayName("hello")); - } finally { - byNameContext.stop(); - byNameContext.close(); - } - ClassPathXmlApplicationContext byTypeContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/aop-autowire-bytype.xml"); - byTypeContext.start(); - try { - DemoActionBySetter demoActionBySetter = (DemoActionBySetter) byTypeContext.getBean("demoActionBySetter"); - assertNotNull(demoActionBySetter.getDemoService()); - assertEquals("aop:say:hello", demoActionBySetter.getDemoService().sayName("hello")); - DemoActionByAnnotation demoActionByAnnotation = (DemoActionByAnnotation) byTypeContext.getBean("demoActionByAnnotation"); - assertNotNull(demoActionByAnnotation.getDemoService()); - assertEquals("aop:say:hello", demoActionByAnnotation.getDemoService().sayName("hello")); - } finally { - byTypeContext.stop(); - byTypeContext.close(); - } - } finally { - providerContext.stop(); - providerContext.close(); - } - } - - @Test - public void testAppendFilter() throws Exception { - ProviderConfig provider = new ProviderConfig(); - provider.setFilter("classloader,monitor"); - ServiceConfig service = new ServiceConfig(); - service.setFilter("accesslog,trace"); - service.setProvider(provider); - service.setProtocol(new ProtocolConfig("dubbo", 20880)); - service.setApplication(new ApplicationConfig("provider")); - service.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE)); - service.setInterface(DemoService.class); - service.setRef(new DemoServiceImpl()); - try { - service.export(); - List urls = service.toUrls(); - assertNotNull(urls); - assertEquals(1, urls.size()); - assertEquals("classloader,monitor,accesslog,trace", urls.get(0).getParameter("service.filter")); - - ConsumerConfig consumer = new ConsumerConfig(); - consumer.setFilter("classloader,monitor"); - ReferenceConfig reference = new ReferenceConfig(); - reference.setFilter("accesslog,trace"); - reference.setConsumer(consumer); - reference.setApplication(new ApplicationConfig("consumer")); - reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE)); - reference.setInterface(DemoService.class); - reference.setUrl("dubbo://" + NetUtils.getLocalHost() + ":20880?" + DemoService.class.getName() + "?check=false"); - try { - reference.get(); - urls = reference.toUrls(); - assertNotNull(urls); - assertEquals(1, urls.size()); - assertEquals("classloader,monitor,accesslog,trace", urls.get(0).getParameter("reference.filter")); - } finally { - reference.destroy(); - } - } finally { - service.unexport(); - } - } - - @Test - public void testInitReference() throws Exception { - ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/demo-provider.xml"); - providerContext.start(); - try { - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/init-reference.xml"); - ctx.start(); - try { - DemoService demoService = (DemoService) ctx.getBean("demoService"); - assertEquals("say:world", demoService.sayName("world")); - } finally { - ctx.stop(); - ctx.close(); - } - } finally { - providerContext.stop(); - providerContext.close(); - } - } - - // DUBBO-571 methods key in provider's URLONE doesn't contain the methods from inherited super interface - @Test - public void test_noMethodInterface_methodsKeyHasValue() throws Exception { - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/demo-provider-no-methods-interface.xml"); - ctx.start(); - try { - ServiceBean bean = (ServiceBean) ctx.getBean("service"); - List urls = bean.getExportedUrls(); - assertEquals(1, urls.size()); - URL url = urls.get(0); - assertEquals("sayName,getBox", url.getParameter("methods")); - } finally { - ctx.stop(); - ctx.close(); - } - } - - // DUBBO-147 find all invoker instances which have been tried from RpcContext - @Test - public void test_RpcContext_getUrls() throws Exception { - ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext( - ConfigTest.class.getPackage().getName().replace('.', '/') + "/demo-provider-long-waiting.xml"); - providerContext.start(); - - try { - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( - ConfigTest.class.getPackage().getName().replace('.', '/') - + "/init-reference-getUrls.xml"); - ctx.start(); - try { - DemoService demoService = (DemoService) ctx.getBean("demoService"); - try { - demoService.sayName("Haha"); - fail(); - } catch (RpcException expected) { - assertThat(expected.getMessage(), containsString("Tried 3 times")); - } - - assertEquals(3, RpcContext.getContext().getUrls().size()); - } finally { - ctx.stop(); - ctx.close(); - } - } finally { - providerContext.stop(); - providerContext.close(); - } - } - - // BUG: DUBBO-846 in version 2.0.9, config retry="false" on provider's method doesn't work - @Test - public void test_retrySettingFail() throws Exception { - ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext( - ConfigTest.class.getPackage().getName().replace('.', '/') + "/demo-provider-long-waiting.xml"); - providerContext.start(); - - try { - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( - ConfigTest.class.getPackage().getName().replace('.', '/') - + "/init-reference-retry-false.xml"); - ctx.start(); - try { - DemoService demoService = (DemoService) ctx.getBean("demoService"); - try { - demoService.sayName("Haha"); - fail(); - } catch (RpcException expected) { - assertThat(expected.getMessage(), containsString("Tried 1 times")); - } - - assertEquals(1, RpcContext.getContext().getUrls().size()); - } finally { - ctx.stop(); - ctx.close(); - } - } finally { - providerContext.stop(); - providerContext.close(); - } - } - - // BuG: DUBBO-146 Provider doesn't have exception output, and consumer has timeout error when serialization fails - // for example, object transported on the wire doesn't implement Serializable - @Test - public void test_returnSerializationFail() throws Exception { - ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/demo-provider-UnserializableBox.xml"); - providerContext.start(); - try { - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/init-reference.xml"); - ctx.start(); - try { - DemoService demoService = (DemoService) ctx.getBean("demoService"); - try { - demoService.getBox(); - fail(); - } catch (RpcException expected) { - assertThat(expected.getMessage(), containsString("must implement java.io.Serializable")); - } - } finally { - ctx.stop(); - ctx.close(); - } - } finally { - providerContext.stop(); - providerContext.close(); - } - } - - @Test - public void testXmlOverrideProperties() throws Exception { - ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/xml-override-properties.xml"); - providerContext.start(); - try { - ApplicationConfig application = (ApplicationConfig) providerContext.getBean("application"); - assertEquals("demo-provider", application.getName()); - assertEquals("world", application.getOwner()); - - RegistryConfig registry = (RegistryConfig) providerContext.getBean("registry"); - assertEquals("N/A", registry.getAddress()); - - ProtocolConfig dubbo = (ProtocolConfig) providerContext.getBean("dubbo"); - assertEquals(20813, dubbo.getPort().intValue()); - - } finally { - providerContext.stop(); - providerContext.close(); - } - } - - @Test - public void testApiOverrideProperties() throws Exception { - ApplicationConfig application = new ApplicationConfig(); - application.setName("api-override-properties"); - - RegistryConfig registry = new RegistryConfig(); - registry.setAddress("N/A"); - - ProtocolConfig protocol = new ProtocolConfig(); - protocol.setName("dubbo"); - protocol.setPort(13123); - - ServiceConfig service = new ServiceConfig(); - service.setInterface(DemoService.class); - service.setRef(new DemoServiceImpl()); - service.setApplication(application); - service.setRegistry(registry); - service.setProtocol(protocol); - service.export(); - - try { - URL url = service.toUrls().get(0); - assertEquals("api-override-properties", url.getParameter("application")); - assertEquals("world", url.getParameter("owner")); - assertEquals(13123, url.getPort()); - - ReferenceConfig reference = new ReferenceConfig(); - reference.setApplication(new ApplicationConfig("consumer")); - reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE)); - reference.setInterface(DemoService.class); - reference.setUrl("dubbo://127.0.0.1:13123"); - reference.get(); - try { - url = reference.toUrls().get(0); - assertEquals("2000", url.getParameter("timeout")); - } finally { - reference.destroy(); - } - } finally { - service.unexport(); - } - } - - @Test - public void testSystemPropertyOverrideProtocol() throws Exception { - System.setProperty("dubbo.protocol.port", "20812"); - ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/override-protocol.xml"); - providerContext.start(); - try { - ProtocolConfig dubbo = (ProtocolConfig) providerContext.getBean("dubbo"); - assertEquals(20812, dubbo.getPort().intValue()); - } finally { - System.setProperty("dubbo.protocol.port", ""); - providerContext.stop(); - providerContext.close(); - } - } - - @Test - public void testSystemPropertyOverrideMultiProtocol() throws Exception { - System.setProperty("dubbo.protocol.dubbo.port", "20814"); - System.setProperty("dubbo.protocol.rmi.port", "10914"); - ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/override-multi-protocol.xml"); - providerContext.start(); - try { - ProtocolConfig dubbo = (ProtocolConfig) providerContext.getBean("dubbo"); - assertEquals(20814, dubbo.getPort().intValue()); - ProtocolConfig rmi = (ProtocolConfig) providerContext.getBean("rmi"); - assertEquals(10914, rmi.getPort().intValue()); - } finally { - System.setProperty("dubbo.protocol.dubbo.port", ""); - System.setProperty("dubbo.protocol.rmi.port", ""); - providerContext.stop(); - providerContext.close(); - } - } - - @SuppressWarnings("unchecked") - @Test - public void testSystemPropertyOverrideXmlDefault() throws Exception { - System.setProperty("dubbo.application.name", "sysover"); - System.setProperty("dubbo.application.owner", "sysowner"); - System.setProperty("dubbo.registry.address", "N/A"); - System.setProperty("dubbo.protocol.name", "dubbo"); - System.setProperty("dubbo.protocol.port", "20819"); - ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/system-properties-override-default.xml"); - providerContext.start(); - try { - ServiceConfig service = (ServiceConfig) providerContext.getBean("demoServiceConfig"); - assertEquals("sysover", service.getApplication().getName()); - assertEquals("sysowner", service.getApplication().getOwner()); - assertEquals("N/A", service.getRegistry().getAddress()); - assertEquals("dubbo", service.getProtocol().getName()); - assertEquals(20819, service.getProtocol().getPort().intValue()); - } finally { - System.setProperty("dubbo.application.name", ""); - System.setProperty("dubbo.application.owner", ""); - System.setProperty("dubbo.registry.address", ""); - System.setProperty("dubbo.protocol.name", ""); - System.setProperty("dubbo.protocol.port", ""); - providerContext.stop(); - providerContext.close(); - } - } - - @SuppressWarnings("unchecked") - @Test - public void testSystemPropertyOverrideXml() throws Exception { - System.setProperty("dubbo.application.name", "sysover"); - System.setProperty("dubbo.application.owner", "sysowner"); - System.setProperty("dubbo.registry.address", "N/A"); - System.setProperty("dubbo.protocol.name", "dubbo"); - System.setProperty("dubbo.protocol.port", "20819"); - System.setProperty("dubbo.service.register", "false"); - ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/system-properties-override.xml"); - providerContext.start(); - try { - ServiceConfig service = (ServiceConfig) providerContext.getBean("demoServiceConfig"); - URL url = service.toUrls().get(0); - assertEquals("sysover", url.getParameter("application")); - assertEquals("sysowner", url.getParameter("owner")); - assertEquals("dubbo", url.getProtocol()); - assertEquals(20819, url.getPort()); - String register = url.getParameter("register"); - assertTrue(register != null && !"".equals(register)); - assertEquals(false, Boolean.valueOf(register)); - } finally { - System.setProperty("dubbo.application.name", ""); - System.setProperty("dubbo.application.owner", ""); - System.setProperty("dubbo.registry.address", ""); - System.setProperty("dubbo.protocol.name", ""); - System.setProperty("dubbo.protocol.port", ""); - System.setProperty("dubbo.service.register", ""); - providerContext.stop(); - providerContext.close(); - } - } - - @Test - public void testSystemPropertyOverrideReferenceConfig() throws Exception { - System.setProperty("dubbo.reference.retries", "5"); - - try { - ServiceConfig service = new ServiceConfig(); - service.setInterface(DemoService.class); - service.setRef(new DemoServiceImpl()); - service.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE)); - ProtocolConfig protocolConfig = new ProtocolConfig("injvm"); - service.setProtocol(protocolConfig); - service.export(); - - ReferenceConfig reference = new ReferenceConfig(); - reference.setInterface(DemoService.class); - reference.setInjvm(true); - reference.setRetries(2); - reference.get(); - assertEquals(Integer.valueOf(5), reference.getRetries()); - } finally { - System.setProperty("dubbo.reference.retries", ""); - } - } - - @Test - public void testSystemPropertyOverrideApiDefault() throws Exception { - System.setProperty("dubbo.application.name", "sysover"); - System.setProperty("dubbo.application.owner", "sysowner"); - System.setProperty("dubbo.registry.address", "N/A"); - System.setProperty("dubbo.protocol.name", "dubbo"); - System.setProperty("dubbo.protocol.port", "20834"); - try { - ServiceConfig serviceConfig = new ServiceConfig(); - serviceConfig.setInterface(DemoService.class); - serviceConfig.setRef(new DemoServiceImpl()); - serviceConfig.export(); - try { - assertEquals("sysover", serviceConfig.getApplication().getName()); - assertEquals("sysowner", serviceConfig.getApplication().getOwner()); - assertEquals("N/A", serviceConfig.getRegistry().getAddress()); - assertEquals("dubbo", serviceConfig.getProtocol().getName()); - assertEquals(20834, serviceConfig.getProtocol().getPort().intValue()); - } finally { - serviceConfig.unexport(); - } - } finally { - System.setProperty("dubbo.application.name", ""); - System.setProperty("dubbo.application.owner", ""); - System.setProperty("dubbo.registry.address", ""); - System.setProperty("dubbo.protocol.name", ""); - System.setProperty("dubbo.protocol.port", ""); - } - } - - @Test - public void testSystemPropertyOverrideApi() throws Exception { - System.setProperty("dubbo.application.name", "sysover"); - System.setProperty("dubbo.application.owner", "sysowner"); - System.setProperty("dubbo.registry.address", "N/A"); - System.setProperty("dubbo.protocol.name", "dubbo"); - System.setProperty("dubbo.protocol.port", "20834"); - try { - ApplicationConfig application = new ApplicationConfig(); - application.setName("aaa"); - - RegistryConfig registry = new RegistryConfig(); - registry.setAddress("127.0.0.1"); - - ProtocolConfig protocol = new ProtocolConfig(); - protocol.setName("rmi"); - protocol.setPort(1099); - - ServiceConfig service = new ServiceConfig(); - service.setInterface(DemoService.class); - service.setRef(new DemoServiceImpl()); - service.setApplication(application); - service.setRegistry(registry); - service.setProtocol(protocol); - service.export(); - - try { - URL url = service.toUrls().get(0); - assertEquals("sysover", url.getParameter("application")); - assertEquals("sysowner", url.getParameter("owner")); - assertEquals("dubbo", url.getProtocol()); - assertEquals(20834, url.getPort()); - } finally { - service.unexport(); - } - } finally { - System.setProperty("dubbo.application.name", ""); - System.setProperty("dubbo.application.owner", ""); - System.setProperty("dubbo.registry.address", ""); - System.setProperty("dubbo.protocol.name", ""); - System.setProperty("dubbo.protocol.port", ""); - } - } - - @Test - public void testSystemPropertyOverrideProperties() throws Exception { - String portString = System.getProperty("dubbo.protocol.port"); - System.clearProperty("dubbo.protocol.port"); - try { - int port = 1234; - System.setProperty("dubbo.protocol.port", String.valueOf(port)); - ApplicationConfig application = new ApplicationConfig(); - application.setName("aaa"); - - RegistryConfig registry = new RegistryConfig(); - registry.setAddress("N/A"); - - ProtocolConfig protocol = new ProtocolConfig(); - protocol.setName("rmi"); - - ServiceConfig service = new ServiceConfig(); - service.setInterface(DemoService.class); - service.setRef(new DemoServiceImpl()); - service.setApplication(application); - service.setRegistry(registry); - service.setProtocol(protocol); - service.export(); - - try { - URL url = service.toUrls().get(0); - // from api - assertEquals("aaa", url.getParameter("application")); - // from dubbo.properties - assertEquals("world", url.getParameter("owner")); - // from system property - assertEquals(1234, url.getPort()); - } finally { - service.unexport(); - } - } finally { - if (portString != null) { - System.setProperty("dubbo.protocol.port", portString); - } - } - } - - @Test - @SuppressWarnings("unchecked") - public void testCustomizeParameter() throws Exception { - ClassPathXmlApplicationContext context = - new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/customize-parameter.xml"); - context.start(); - ServiceBean serviceBean = (ServiceBean) context.getBean("demoServiceExport"); - URL url = (URL) serviceBean.toUrls().get(0); - assertEquals("protocol-paramA", url.getParameter("protocol.paramA")); - assertEquals("service-paramA", url.getParameter("service.paramA")); - } - - @Test - public void testPath() throws Exception { - ServiceConfig service = new ServiceConfig(); - service.setPath("a/b$c"); - try { - service.setPath("a?b"); - fail(); - } catch (IllegalStateException e) { - assertTrue(e.getMessage().contains("")); - } - } - - @Test - public void testAnnotation() { - SimpleRegistryService registryService = new SimpleRegistryService(); - Exporter exporter = SimpleRegistryExporter.export(4548, registryService); - try { - ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/annotation-provider.xml"); - providerContext.start(); - try { - ClassPathXmlApplicationContext consumerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/annotation-consumer.xml"); - consumerContext.start(); - try { - AnnotationAction annotationAction = (AnnotationAction) consumerContext.getBean("annotationAction"); - String hello = annotationAction.doSayName("hello"); - assertEquals("annotation:hello", hello); - } finally { - consumerContext.stop(); - consumerContext.close(); - } - } finally { - providerContext.stop(); - providerContext.close(); - } - } finally { - exporter.unexport(); - } - } - - @Test - public void testDubboProtocolPortOverride() throws Exception { - String dubboPort = System.getProperty("dubbo.protocol.dubbo.port"); - int port = 55555; - System.setProperty("dubbo.protocol.dubbo.port", String.valueOf(port)); - ServiceConfig service = null; - try { - ApplicationConfig application = new ApplicationConfig(); - application.setName("dubbo-protocol-port-override"); - - RegistryConfig registry = new RegistryConfig(); - registry.setAddress("N/A"); - - ProtocolConfig protocol = new ProtocolConfig(); - - service = new ServiceConfig(); - service.setInterface(DemoService.class); - service.setRef(new DemoServiceImpl()); - service.setApplication(application); - service.setRegistry(registry); - service.setProtocol(protocol); - service.export(); - - Assert.assertEquals(port, service.getExportedUrls().get(0).getPort()); - } finally { - if (StringUtils.isNotEmpty(dubboPort)) { - System.setProperty("dubbo.protocol.dubbo.port", dubboPort); - } - if (service != null) { - service.unexport(); - } - } - } - - @Test - public void testProtocolRandomPort() throws Exception { - ServiceConfig demoService = null; - ServiceConfig helloService = null; - - ApplicationConfig application = new ApplicationConfig(); - application.setName("test-protocol-random-port"); - - RegistryConfig registry = new RegistryConfig(); - registry.setAddress("N/A"); - - ProtocolConfig protocol = new ProtocolConfig(); - protocol.setName("dubbo"); - protocol.setPort(-1); - - demoService = new ServiceConfig(); - demoService.setInterface(DemoService.class); - demoService.setRef(new DemoServiceImpl()); - demoService.setApplication(application); - demoService.setRegistry(registry); - demoService.setProtocol(protocol); - - helloService = new ServiceConfig(); - helloService.setInterface(HelloService.class); - helloService.setRef(new HelloServiceImpl()); - helloService.setApplication(application); - helloService.setRegistry(registry); - helloService.setProtocol(protocol); - - try { - demoService.export(); - helloService.export(); - - Assert.assertEquals(demoService.getExportedUrls().get(0).getPort(), - helloService.getExportedUrls().get(0).getPort()); - } finally { - unexportService(demoService); - unexportService(helloService); - } - } - - @Test - public void testReferGenericExport() throws Exception { - ApplicationConfig ac = new ApplicationConfig("test-refer-generic-export"); - RegistryConfig rc = new RegistryConfig(); - rc.setAddress(RegistryConfig.NO_AVAILABLE); - - ServiceConfig sc = new ServiceConfig(); - sc.setApplication(ac); - sc.setRegistry(rc); - sc.setInterface(DemoService.class.getName()); - sc.setRef(new GenericService() { - - public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException { - return null; - } - }); - - ReferenceConfig ref = new ReferenceConfig(); - ref.setApplication(ac); - ref.setRegistry(rc); - ref.setInterface(DemoService.class.getName()); - - try { - sc.export(); - ref.get(); - Assert.fail(); - } catch (Exception e) { - e.printStackTrace(); - } finally { - sc.unexport(); - ref.destroy(); - } - } - - @Test - public void testGenericServiceConfig() throws Exception { - ServiceConfig service = new ServiceConfig(); - service.setApplication(new ApplicationConfig("test")); - service.setRegistry(new RegistryConfig("mock://localhost")); - service.setInterface(DemoService.class.getName()); - service.setGeneric(Constants.GENERIC_SERIALIZATION_BEAN); - service.setRef(new GenericService() { - - public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException { - return null; - } - }); - try { - service.export(); - Collection collection = MockRegistryFactory.getCachedRegistry(); - MockRegistry registry = (MockRegistry) collection.iterator().next(); - URL url = registry.getRegistered().get(0); - Assert.assertEquals(Constants.GENERIC_SERIALIZATION_BEAN, url.getParameter(Constants.GENERIC_KEY)); - } finally { - MockRegistryFactory.cleanCachedRegistry(); - service.unexport(); - } - } - - @Test - public void testGenericServiceConfigThroughSpring() throws Exception { - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/generic-export.xml"); - try { - ctx.start(); - ServiceConfig serviceConfig = (ServiceConfig) ctx.getBean("dubboDemoService"); - URL url = (URL) serviceConfig.getExportedUrls().get(0); - Assert.assertEquals(Constants.GENERIC_SERIALIZATION_BEAN, url.getParameter(Constants.GENERIC_KEY)); - } finally { - ctx.destroy(); - } - } +/* + * 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 org.apache.dubbo.config.spring; + +import org.apache.dubbo.common.Constants; +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.extension.ExtensionLoader; +import org.apache.dubbo.common.utils.NetUtils; +import org.apache.dubbo.common.utils.StringUtils; +import org.apache.dubbo.config.ApplicationConfig; +import org.apache.dubbo.config.ConsumerConfig; +import org.apache.dubbo.config.ProtocolConfig; +import org.apache.dubbo.config.ProviderConfig; +import org.apache.dubbo.config.ReferenceConfig; +import org.apache.dubbo.config.RegistryConfig; +import org.apache.dubbo.config.ServiceConfig; +import org.apache.dubbo.config.spring.action.DemoActionByAnnotation; +import org.apache.dubbo.config.spring.action.DemoActionBySetter; +import org.apache.dubbo.config.spring.annotation.consumer.AnnotationAction; +import org.apache.dubbo.config.spring.api.DemoService; +import org.apache.dubbo.config.spring.api.HelloService; +import org.apache.dubbo.config.spring.context.annotation.provider.ProviderConfiguration; +import org.apache.dubbo.config.spring.filter.MockFilter; +import org.apache.dubbo.config.spring.impl.DemoServiceImpl; +import org.apache.dubbo.config.spring.impl.HelloServiceImpl; +import org.apache.dubbo.config.spring.registry.MockRegistry; +import org.apache.dubbo.config.spring.registry.MockRegistryFactory; +import org.apache.dubbo.registry.Registry; +import org.apache.dubbo.registry.RegistryService; +import org.apache.dubbo.rpc.Exporter; +import org.apache.dubbo.rpc.Filter; +import org.apache.dubbo.rpc.RpcContext; +import org.apache.dubbo.rpc.RpcException; +import org.apache.dubbo.rpc.service.GenericException; +import org.apache.dubbo.rpc.service.GenericService; +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Test; +import org.springframework.beans.factory.BeanCreationException; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import java.util.Collection; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.junit.matchers.JUnitMatchers.containsString; + + +/** + * ConfigTest + */ +@Ignore +public class ConfigTest { + + private static void unexportService(ServiceConfig config) { + if (config != null) { + config.unexport(); + } + } + + @Test + public void testSpringExtensionInject() { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/spring-extension-inject.xml"); + ctx.start(); + try { + MockFilter filter = (MockFilter) ExtensionLoader.getExtensionLoader(Filter.class).getExtension("mymock"); + assertNotNull(filter.getMockDao()); + assertNotNull(filter.getProtocol()); + assertNotNull(filter.getLoadBalance()); + } finally { + ctx.stop(); + ctx.close(); + } + } + + @Test + public void testServiceClass() { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/service-class.xml"); + ctx.start(); + try { + DemoService demoService = refer("dubbo://127.0.0.1:30887"); + String hello = demoService.sayName("hello"); + assertEquals("welcome:hello", hello); + } finally { + ctx.stop(); + ctx.close(); + } + } + + @Test + public void testServiceAnnotation() { + AnnotationConfigApplicationContext providerContext = new AnnotationConfigApplicationContext(); + providerContext.register(ProviderConfiguration.class); + + providerContext.refresh(); + + ReferenceConfig reference = new ReferenceConfig(); + reference.setApplication(new ApplicationConfig("consumer")); + reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE)); + reference.setInterface(HelloService.class); + reference.setUrl("dubbo://127.0.0.1:12345"); + String hello = reference.get().sayHello("hello"); + assertEquals("Hello, hello", hello); + + } + + @Test + @SuppressWarnings("unchecked") + public void testProviderNestedService() { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/provider-nested-service.xml"); + ctx.start(); + try { + ServiceConfig serviceConfig = (ServiceConfig) ctx.getBean("serviceConfig"); + assertNotNull(serviceConfig.getProvider()); + assertEquals(2000, serviceConfig.getProvider().getTimeout().intValue()); + + ServiceConfig serviceConfig2 = (ServiceConfig) ctx.getBean("serviceConfig2"); + assertNotNull(serviceConfig2.getProvider()); + assertEquals(1000, serviceConfig2.getProvider().getTimeout().intValue()); + } finally { + ctx.stop(); + ctx.close(); + } + } + + private DemoService refer(String url) { + ReferenceConfig reference = new ReferenceConfig(); + reference.setApplication(new ApplicationConfig("consumer")); + reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE)); + reference.setInterface(DemoService.class); + reference.setUrl(url); + return reference.get(); + } + + @Test + public void testToString() { + ReferenceConfig reference = new ReferenceConfig(); + reference.setApplication(new ApplicationConfig("consumer")); + reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE)); + reference.setInterface(DemoService.class); + reference.setUrl("dubbo://127.0.0.1:20881"); + String str = reference.toString(); + assertTrue(str.startsWith("")); + } + + @Test + public void testForks() { + ReferenceConfig reference = new ReferenceConfig(); + reference.setApplication(new ApplicationConfig("consumer")); + reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE)); + reference.setInterface(DemoService.class); + reference.setUrl("dubbo://127.0.0.1:20881"); + + int forks = 10; + reference.setForks(forks); + String str = reference.toString(); + assertTrue(str.contains("forks=\"" + forks + "\"")); + } + + @Test + public void testMultiProtocol() { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-protocol.xml"); + ctx.start(); + try { + DemoService demoService = refer("dubbo://127.0.0.1:20881"); + String hello = demoService.sayName("hello"); + assertEquals("say:hello", hello); + } finally { + ctx.stop(); + ctx.close(); + } + } + + @Test + public void testMultiProtocolDefault() { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-protocol-default.xml"); + ctx.start(); + try { + DemoService demoService = refer("rmi://127.0.0.1:10991"); + String hello = demoService.sayName("hello"); + assertEquals("say:hello", hello); + } finally { + ctx.stop(); + ctx.close(); + } + } + + @Test + public void testMultiProtocolError() { + try { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-protocol-error.xml"); + ctx.start(); + ctx.stop(); + ctx.close(); + } catch (BeanCreationException e) { + assertTrue(e.getMessage().contains("Found multi-protocols")); + } + } + + @Test + public void testMultiProtocolRegister() { + SimpleRegistryService registryService = new SimpleRegistryService(); + Exporter exporter = SimpleRegistryExporter.export(4547, registryService); + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-protocol-register.xml"); + ctx.start(); + try { + List urls = registryService.getRegistered().get("org.apache.dubbo.config.spring.api.DemoService"); + assertNotNull(urls); + assertEquals(1, urls.size()); + assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20824/org.apache.dubbo.config.spring.api.DemoService", urls.get(0).toIdentityString()); + } finally { + ctx.stop(); + ctx.close(); + exporter.unexport(); + } + } + + @Test + public void testMultiRegistry() { + SimpleRegistryService registryService1 = new SimpleRegistryService(); + Exporter exporter1 = SimpleRegistryExporter.export(4545, registryService1); + SimpleRegistryService registryService2 = new SimpleRegistryService(); + Exporter exporter2 = SimpleRegistryExporter.export(4546, registryService2); + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-registry.xml"); + ctx.start(); + try { + List urls1 = registryService1.getRegistered().get("org.apache.dubbo.config.spring.api.DemoService"); + assertNull(urls1); + List urls2 = registryService2.getRegistered().get("org.apache.dubbo.config.spring.api.DemoService"); + assertNotNull(urls2); + assertEquals(1, urls2.size()); + assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20880/org.apache.dubbo.config.spring.api.DemoService", urls2.get(0).toIdentityString()); + } finally { + ctx.stop(); + ctx.close(); + exporter1.unexport(); + exporter2.unexport(); + } + } + + @Test + public void testDelayFixedTime() throws Exception { + SimpleRegistryService registryService = new SimpleRegistryService(); + Exporter exporter = SimpleRegistryExporter.export(4548, registryService); + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/delay-fixed-time.xml"); + ctx.start(); + try { + List urls = registryService.getRegistered().get("org.apache.dubbo.config.spring.api.DemoService"); + assertNull(urls); + int i = 0; + while ((i++) < 60 && urls == null) { + urls = registryService.getRegistered().get("org.apache.dubbo.config.spring.api.DemoService"); + Thread.sleep(10); + } + assertNotNull(urls); + assertEquals(1, urls.size()); + assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20888/org.apache.dubbo.config.spring.api.DemoService", urls.get(0).toIdentityString()); + } finally { + ctx.stop(); + ctx.close(); + exporter.unexport(); + } + } + + @Test + public void testDelayOnInitialized() throws Exception { + SimpleRegistryService registryService = new SimpleRegistryService(); + Exporter exporter = SimpleRegistryExporter.export(4548, registryService); + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/delay-on-initialized.xml"); + //ctx.start(); + try { + List urls = registryService.getRegistered().get("org.apache.dubbo.config.spring.api.DemoService"); + assertNotNull(urls); + assertEquals(1, urls.size()); + assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20888/org.apache.dubbo.config.spring.api.DemoService", urls.get(0).toIdentityString()); + } finally { + ctx.stop(); + ctx.close(); + exporter.unexport(); + } + } + + @Test + public void testRmiTimeout() throws Exception { + if (System.getProperty("sun.rmi.transport.tcp.responseTimeout") != null) { + System.setProperty("sun.rmi.transport.tcp.responseTimeout", ""); + } + ConsumerConfig consumer = new ConsumerConfig(); + consumer.setTimeout(1000); + assertEquals("1000", System.getProperty("sun.rmi.transport.tcp.responseTimeout")); + consumer.setTimeout(2000); + assertEquals("1000", System.getProperty("sun.rmi.transport.tcp.responseTimeout")); + } + + @Test + public void testAutowireAndAOP() throws Exception { + ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/demo-provider.xml"); + providerContext.start(); + try { + ClassPathXmlApplicationContext byNameContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/aop-autowire-byname.xml"); + byNameContext.start(); + try { + DemoActionBySetter demoActionBySetter = (DemoActionBySetter) byNameContext.getBean("demoActionBySetter"); + assertNotNull(demoActionBySetter.getDemoService()); + assertEquals("aop:say:hello", demoActionBySetter.getDemoService().sayName("hello")); + DemoActionByAnnotation demoActionByAnnotation = (DemoActionByAnnotation) byNameContext.getBean("demoActionByAnnotation"); + assertNotNull(demoActionByAnnotation.getDemoService()); + assertEquals("aop:say:hello", demoActionByAnnotation.getDemoService().sayName("hello")); + } finally { + byNameContext.stop(); + byNameContext.close(); + } + ClassPathXmlApplicationContext byTypeContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/aop-autowire-bytype.xml"); + byTypeContext.start(); + try { + DemoActionBySetter demoActionBySetter = (DemoActionBySetter) byTypeContext.getBean("demoActionBySetter"); + assertNotNull(demoActionBySetter.getDemoService()); + assertEquals("aop:say:hello", demoActionBySetter.getDemoService().sayName("hello")); + DemoActionByAnnotation demoActionByAnnotation = (DemoActionByAnnotation) byTypeContext.getBean("demoActionByAnnotation"); + assertNotNull(demoActionByAnnotation.getDemoService()); + assertEquals("aop:say:hello", demoActionByAnnotation.getDemoService().sayName("hello")); + } finally { + byTypeContext.stop(); + byTypeContext.close(); + } + } finally { + providerContext.stop(); + providerContext.close(); + } + } + + @Test + public void testAppendFilter() throws Exception { + ProviderConfig provider = new ProviderConfig(); + provider.setFilter("classloader,monitor"); + ServiceConfig service = new ServiceConfig(); + service.setFilter("accesslog,trace"); + service.setProvider(provider); + service.setProtocol(new ProtocolConfig("dubbo", 20880)); + service.setApplication(new ApplicationConfig("provider")); + service.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE)); + service.setInterface(DemoService.class); + service.setRef(new DemoServiceImpl()); + try { + service.export(); + List urls = service.toUrls(); + assertNotNull(urls); + assertEquals(1, urls.size()); + assertEquals("classloader,monitor,accesslog,trace", urls.get(0).getParameter("service.filter")); + + ConsumerConfig consumer = new ConsumerConfig(); + consumer.setFilter("classloader,monitor"); + ReferenceConfig reference = new ReferenceConfig(); + reference.setFilter("accesslog,trace"); + reference.setConsumer(consumer); + reference.setApplication(new ApplicationConfig("consumer")); + reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE)); + reference.setInterface(DemoService.class); + reference.setUrl("dubbo://" + NetUtils.getLocalHost() + ":20880?" + DemoService.class.getName() + "?check=false"); + try { + reference.get(); + urls = reference.toUrls(); + assertNotNull(urls); + assertEquals(1, urls.size()); + assertEquals("classloader,monitor,accesslog,trace", urls.get(0).getParameter("reference.filter")); + } finally { + reference.destroy(); + } + } finally { + service.unexport(); + } + } + + @Test + public void testInitReference() throws Exception { + ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/demo-provider.xml"); + providerContext.start(); + try { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/init-reference.xml"); + ctx.start(); + try { + DemoService demoService = (DemoService) ctx.getBean("demoService"); + assertEquals("say:world", demoService.sayName("world")); + } finally { + ctx.stop(); + ctx.close(); + } + } finally { + providerContext.stop(); + providerContext.close(); + } + } + + // DUBBO-571 methods key in provider's URLONE doesn't contain the methods from inherited super interface + @Test + public void test_noMethodInterface_methodsKeyHasValue() throws Exception { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/demo-provider-no-methods-interface.xml"); + ctx.start(); + try { + ServiceBean bean = (ServiceBean) ctx.getBean("service"); + List urls = bean.getExportedUrls(); + assertEquals(1, urls.size()); + URL url = urls.get(0); + assertEquals("sayName,getBox", url.getParameter("methods")); + } finally { + ctx.stop(); + ctx.close(); + } + } + + // DUBBO-147 find all invoker instances which have been tried from RpcContext + @Test + public void test_RpcContext_getUrls() throws Exception { + ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext( + ConfigTest.class.getPackage().getName().replace('.', '/') + "/demo-provider-long-waiting.xml"); + providerContext.start(); + + try { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( + ConfigTest.class.getPackage().getName().replace('.', '/') + + "/init-reference-getUrls.xml"); + ctx.start(); + try { + DemoService demoService = (DemoService) ctx.getBean("demoService"); + try { + demoService.sayName("Haha"); + fail(); + } catch (RpcException expected) { + assertThat(expected.getMessage(), containsString("Tried 3 times")); + } + + assertEquals(3, RpcContext.getContext().getUrls().size()); + } finally { + ctx.stop(); + ctx.close(); + } + } finally { + providerContext.stop(); + providerContext.close(); + } + } + + // BUG: DUBBO-846 in version 2.0.9, config retry="false" on provider's method doesn't work + @Test + public void test_retrySettingFail() throws Exception { + ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext( + ConfigTest.class.getPackage().getName().replace('.', '/') + "/demo-provider-long-waiting.xml"); + providerContext.start(); + + try { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( + ConfigTest.class.getPackage().getName().replace('.', '/') + + "/init-reference-retry-false.xml"); + ctx.start(); + try { + DemoService demoService = (DemoService) ctx.getBean("demoService"); + try { + demoService.sayName("Haha"); + fail(); + } catch (RpcException expected) { + assertThat(expected.getMessage(), containsString("Tried 1 times")); + } + + assertEquals(1, RpcContext.getContext().getUrls().size()); + } finally { + ctx.stop(); + ctx.close(); + } + } finally { + providerContext.stop(); + providerContext.close(); + } + } + + // BuG: DUBBO-146 Provider doesn't have exception output, and consumer has timeout error when serialization fails + // for example, object transported on the wire doesn't implement Serializable + @Test + public void test_returnSerializationFail() throws Exception { + ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/demo-provider-UnserializableBox.xml"); + providerContext.start(); + try { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/init-reference.xml"); + ctx.start(); + try { + DemoService demoService = (DemoService) ctx.getBean("demoService"); + try { + demoService.getBox(); + fail(); + } catch (RpcException expected) { + assertThat(expected.getMessage(), containsString("must implement java.io.Serializable")); + } + } finally { + ctx.stop(); + ctx.close(); + } + } finally { + providerContext.stop(); + providerContext.close(); + } + } + + @Test + public void testXmlOverrideProperties() throws Exception { + ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/xml-override-properties.xml"); + providerContext.start(); + try { + ApplicationConfig application = (ApplicationConfig) providerContext.getBean("application"); + assertEquals("demo-provider", application.getName()); + assertEquals("world", application.getOwner()); + + RegistryConfig registry = (RegistryConfig) providerContext.getBean("registry"); + assertEquals("N/A", registry.getAddress()); + + ProtocolConfig dubbo = (ProtocolConfig) providerContext.getBean("dubbo"); + assertEquals(20813, dubbo.getPort().intValue()); + + } finally { + providerContext.stop(); + providerContext.close(); + } + } + + @Test + public void testApiOverrideProperties() throws Exception { + ApplicationConfig application = new ApplicationConfig(); + application.setName("api-override-properties"); + + RegistryConfig registry = new RegistryConfig(); + registry.setAddress("N/A"); + + ProtocolConfig protocol = new ProtocolConfig(); + protocol.setName("dubbo"); + protocol.setPort(13123); + + ServiceConfig service = new ServiceConfig(); + service.setInterface(DemoService.class); + service.setRef(new DemoServiceImpl()); + service.setApplication(application); + service.setRegistry(registry); + service.setProtocol(protocol); + service.export(); + + try { + URL url = service.toUrls().get(0); + assertEquals("api-override-properties", url.getParameter("application")); + assertEquals("world", url.getParameter("owner")); + assertEquals(13123, url.getPort()); + + ReferenceConfig reference = new ReferenceConfig(); + reference.setApplication(new ApplicationConfig("consumer")); + reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE)); + reference.setInterface(DemoService.class); + reference.setUrl("dubbo://127.0.0.1:13123"); + reference.get(); + try { + url = reference.toUrls().get(0); + assertEquals("2000", url.getParameter("timeout")); + } finally { + reference.destroy(); + } + } finally { + service.unexport(); + } + } + + @Test + public void testSystemPropertyOverrideProtocol() throws Exception { + System.setProperty("dubbo.protocol.port", "20812"); + ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/override-protocol.xml"); + providerContext.start(); + try { + ProtocolConfig dubbo = (ProtocolConfig) providerContext.getBean("dubbo"); + assertEquals(20812, dubbo.getPort().intValue()); + } finally { + System.setProperty("dubbo.protocol.port", ""); + providerContext.stop(); + providerContext.close(); + } + } + + @Test + public void testSystemPropertyOverrideMultiProtocol() throws Exception { + System.setProperty("dubbo.protocol.dubbo.port", "20814"); + System.setProperty("dubbo.protocol.rmi.port", "10914"); + ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/override-multi-protocol.xml"); + providerContext.start(); + try { + ProtocolConfig dubbo = (ProtocolConfig) providerContext.getBean("dubbo"); + assertEquals(20814, dubbo.getPort().intValue()); + ProtocolConfig rmi = (ProtocolConfig) providerContext.getBean("rmi"); + assertEquals(10914, rmi.getPort().intValue()); + } finally { + System.setProperty("dubbo.protocol.dubbo.port", ""); + System.setProperty("dubbo.protocol.rmi.port", ""); + providerContext.stop(); + providerContext.close(); + } + } + + @SuppressWarnings("unchecked") + @Test + public void testSystemPropertyOverrideXmlDefault() throws Exception { + System.setProperty("dubbo.application.name", "sysover"); + System.setProperty("dubbo.application.owner", "sysowner"); + System.setProperty("dubbo.registry.address", "N/A"); + System.setProperty("dubbo.protocol.name", "dubbo"); + System.setProperty("dubbo.protocol.port", "20819"); + ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/system-properties-override-default.xml"); + providerContext.start(); + try { + ServiceConfig service = (ServiceConfig) providerContext.getBean("demoServiceConfig"); + assertEquals("sysover", service.getApplication().getName()); + assertEquals("sysowner", service.getApplication().getOwner()); + assertEquals("N/A", service.getRegistry().getAddress()); + assertEquals("dubbo", service.getProtocol().getName()); + assertEquals(20819, service.getProtocol().getPort().intValue()); + } finally { + System.setProperty("dubbo.application.name", ""); + System.setProperty("dubbo.application.owner", ""); + System.setProperty("dubbo.registry.address", ""); + System.setProperty("dubbo.protocol.name", ""); + System.setProperty("dubbo.protocol.port", ""); + providerContext.stop(); + providerContext.close(); + } + } + + @SuppressWarnings("unchecked") + @Test + public void testSystemPropertyOverrideXml() throws Exception { + System.setProperty("dubbo.application.name", "sysover"); + System.setProperty("dubbo.application.owner", "sysowner"); + System.setProperty("dubbo.registry.address", "N/A"); + System.setProperty("dubbo.protocol.name", "dubbo"); + System.setProperty("dubbo.protocol.port", "20819"); + System.setProperty("dubbo.service.register", "false"); + ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/system-properties-override.xml"); + providerContext.start(); + try { + ServiceConfig service = (ServiceConfig) providerContext.getBean("demoServiceConfig"); + URL url = service.toUrls().get(0); + assertEquals("sysover", url.getParameter("application")); + assertEquals("sysowner", url.getParameter("owner")); + assertEquals("dubbo", url.getProtocol()); + assertEquals(20819, url.getPort()); + String register = url.getParameter("register"); + assertTrue(register != null && !"".equals(register)); + assertEquals(false, Boolean.valueOf(register)); + } finally { + System.setProperty("dubbo.application.name", ""); + System.setProperty("dubbo.application.owner", ""); + System.setProperty("dubbo.registry.address", ""); + System.setProperty("dubbo.protocol.name", ""); + System.setProperty("dubbo.protocol.port", ""); + System.setProperty("dubbo.service.register", ""); + providerContext.stop(); + providerContext.close(); + } + } + + @Test + public void testSystemPropertyOverrideReferenceConfig() throws Exception { + System.setProperty("dubbo.reference.retries", "5"); + + try { + ServiceConfig service = new ServiceConfig(); + service.setInterface(DemoService.class); + service.setRef(new DemoServiceImpl()); + service.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE)); + ProtocolConfig protocolConfig = new ProtocolConfig("injvm"); + service.setProtocol(protocolConfig); + service.export(); + + ReferenceConfig reference = new ReferenceConfig(); + reference.setInterface(DemoService.class); + reference.setInjvm(true); + reference.setRetries(2); + reference.get(); + assertEquals(Integer.valueOf(5), reference.getRetries()); + } finally { + System.setProperty("dubbo.reference.retries", ""); + } + } + + @Test + public void testSystemPropertyOverrideApiDefault() throws Exception { + System.setProperty("dubbo.application.name", "sysover"); + System.setProperty("dubbo.application.owner", "sysowner"); + System.setProperty("dubbo.registry.address", "N/A"); + System.setProperty("dubbo.protocol.name", "dubbo"); + System.setProperty("dubbo.protocol.port", "20834"); + try { + ServiceConfig serviceConfig = new ServiceConfig(); + serviceConfig.setInterface(DemoService.class); + serviceConfig.setRef(new DemoServiceImpl()); + serviceConfig.export(); + try { + assertEquals("sysover", serviceConfig.getApplication().getName()); + assertEquals("sysowner", serviceConfig.getApplication().getOwner()); + assertEquals("N/A", serviceConfig.getRegistry().getAddress()); + assertEquals("dubbo", serviceConfig.getProtocol().getName()); + assertEquals(20834, serviceConfig.getProtocol().getPort().intValue()); + } finally { + serviceConfig.unexport(); + } + } finally { + System.setProperty("dubbo.application.name", ""); + System.setProperty("dubbo.application.owner", ""); + System.setProperty("dubbo.registry.address", ""); + System.setProperty("dubbo.protocol.name", ""); + System.setProperty("dubbo.protocol.port", ""); + } + } + + @Test + public void testSystemPropertyOverrideApi() throws Exception { + System.setProperty("dubbo.application.name", "sysover"); + System.setProperty("dubbo.application.owner", "sysowner"); + System.setProperty("dubbo.registry.address", "N/A"); + System.setProperty("dubbo.protocol.name", "dubbo"); + System.setProperty("dubbo.protocol.port", "20834"); + try { + ApplicationConfig application = new ApplicationConfig(); + application.setName("aaa"); + + RegistryConfig registry = new RegistryConfig(); + registry.setAddress("127.0.0.1"); + + ProtocolConfig protocol = new ProtocolConfig(); + protocol.setName("rmi"); + protocol.setPort(1099); + + ServiceConfig service = new ServiceConfig(); + service.setInterface(DemoService.class); + service.setRef(new DemoServiceImpl()); + service.setApplication(application); + service.setRegistry(registry); + service.setProtocol(protocol); + service.export(); + + try { + URL url = service.toUrls().get(0); + assertEquals("sysover", url.getParameter("application")); + assertEquals("sysowner", url.getParameter("owner")); + assertEquals("dubbo", url.getProtocol()); + assertEquals(20834, url.getPort()); + } finally { + service.unexport(); + } + } finally { + System.setProperty("dubbo.application.name", ""); + System.setProperty("dubbo.application.owner", ""); + System.setProperty("dubbo.registry.address", ""); + System.setProperty("dubbo.protocol.name", ""); + System.setProperty("dubbo.protocol.port", ""); + } + } + + @Test + public void testSystemPropertyOverrideProperties() throws Exception { + String portString = System.getProperty("dubbo.protocol.port"); + System.clearProperty("dubbo.protocol.port"); + try { + int port = 1234; + System.setProperty("dubbo.protocol.port", String.valueOf(port)); + ApplicationConfig application = new ApplicationConfig(); + application.setName("aaa"); + + RegistryConfig registry = new RegistryConfig(); + registry.setAddress("N/A"); + + ProtocolConfig protocol = new ProtocolConfig(); + protocol.setName("rmi"); + + ServiceConfig service = new ServiceConfig(); + service.setInterface(DemoService.class); + service.setRef(new DemoServiceImpl()); + service.setApplication(application); + service.setRegistry(registry); + service.setProtocol(protocol); + service.export(); + + try { + URL url = service.toUrls().get(0); + // from api + assertEquals("aaa", url.getParameter("application")); + // from dubbo.properties + assertEquals("world", url.getParameter("owner")); + // from system property + assertEquals(1234, url.getPort()); + } finally { + service.unexport(); + } + } finally { + if (portString != null) { + System.setProperty("dubbo.protocol.port", portString); + } + } + } + + @Test + @SuppressWarnings("unchecked") + public void testCustomizeParameter() throws Exception { + ClassPathXmlApplicationContext context = + new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/customize-parameter.xml"); + context.start(); + ServiceBean serviceBean = (ServiceBean) context.getBean("demoServiceExport"); + URL url = (URL) serviceBean.toUrls().get(0); + assertEquals("protocol-paramA", url.getParameter("protocol.paramA")); + assertEquals("service-paramA", url.getParameter("service.paramA")); + } + + @Test + public void testPath() throws Exception { + ServiceConfig service = new ServiceConfig(); + service.setPath("a/b$c"); + try { + service.setPath("a?b"); + fail(); + } catch (IllegalStateException e) { + assertTrue(e.getMessage().contains("")); + } + } + + @Test + public void testAnnotation() { + SimpleRegistryService registryService = new SimpleRegistryService(); + Exporter exporter = SimpleRegistryExporter.export(4548, registryService); + try { + ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/annotation-provider.xml"); + providerContext.start(); + try { + ClassPathXmlApplicationContext consumerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/annotation-consumer.xml"); + consumerContext.start(); + try { + AnnotationAction annotationAction = (AnnotationAction) consumerContext.getBean("annotationAction"); + String hello = annotationAction.doSayName("hello"); + assertEquals("annotation:hello", hello); + } finally { + consumerContext.stop(); + consumerContext.close(); + } + } finally { + providerContext.stop(); + providerContext.close(); + } + } finally { + exporter.unexport(); + } + } + + @Test + public void testDubboProtocolPortOverride() throws Exception { + String dubboPort = System.getProperty("dubbo.protocol.dubbo.port"); + int port = 55555; + System.setProperty("dubbo.protocol.dubbo.port", String.valueOf(port)); + ServiceConfig service = null; + try { + ApplicationConfig application = new ApplicationConfig(); + application.setName("dubbo-protocol-port-override"); + + RegistryConfig registry = new RegistryConfig(); + registry.setAddress("N/A"); + + ProtocolConfig protocol = new ProtocolConfig(); + + service = new ServiceConfig(); + service.setInterface(DemoService.class); + service.setRef(new DemoServiceImpl()); + service.setApplication(application); + service.setRegistry(registry); + service.setProtocol(protocol); + service.export(); + + Assert.assertEquals(port, service.getExportedUrls().get(0).getPort()); + } finally { + if (StringUtils.isNotEmpty(dubboPort)) { + System.setProperty("dubbo.protocol.dubbo.port", dubboPort); + } + if (service != null) { + service.unexport(); + } + } + } + + @Test + public void testProtocolRandomPort() throws Exception { + ServiceConfig demoService = null; + ServiceConfig helloService = null; + + ApplicationConfig application = new ApplicationConfig(); + application.setName("test-protocol-random-port"); + + RegistryConfig registry = new RegistryConfig(); + registry.setAddress("N/A"); + + ProtocolConfig protocol = new ProtocolConfig(); + protocol.setName("dubbo"); + protocol.setPort(-1); + + demoService = new ServiceConfig(); + demoService.setInterface(DemoService.class); + demoService.setRef(new DemoServiceImpl()); + demoService.setApplication(application); + demoService.setRegistry(registry); + demoService.setProtocol(protocol); + + helloService = new ServiceConfig(); + helloService.setInterface(HelloService.class); + helloService.setRef(new HelloServiceImpl()); + helloService.setApplication(application); + helloService.setRegistry(registry); + helloService.setProtocol(protocol); + + try { + demoService.export(); + helloService.export(); + + Assert.assertEquals(demoService.getExportedUrls().get(0).getPort(), + helloService.getExportedUrls().get(0).getPort()); + } finally { + unexportService(demoService); + unexportService(helloService); + } + } + + @Test + public void testReferGenericExport() throws Exception { + ApplicationConfig ac = new ApplicationConfig("test-refer-generic-export"); + RegistryConfig rc = new RegistryConfig(); + rc.setAddress(RegistryConfig.NO_AVAILABLE); + + ServiceConfig sc = new ServiceConfig(); + sc.setApplication(ac); + sc.setRegistry(rc); + sc.setInterface(DemoService.class.getName()); + sc.setRef(new GenericService() { + + public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException { + return null; + } + }); + + ReferenceConfig ref = new ReferenceConfig(); + ref.setApplication(ac); + ref.setRegistry(rc); + ref.setInterface(DemoService.class.getName()); + + try { + sc.export(); + ref.get(); + Assert.fail(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + sc.unexport(); + ref.destroy(); + } + } + + @Test + public void testGenericServiceConfig() throws Exception { + ServiceConfig service = new ServiceConfig(); + service.setApplication(new ApplicationConfig("test")); + service.setRegistry(new RegistryConfig("mock://localhost")); + service.setInterface(DemoService.class.getName()); + service.setGeneric(Constants.GENERIC_SERIALIZATION_BEAN); + service.setRef(new GenericService() { + + public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException { + return null; + } + }); + try { + service.export(); + Collection collection = MockRegistryFactory.getCachedRegistry(); + MockRegistry registry = (MockRegistry) collection.iterator().next(); + URL url = registry.getRegistered().get(0); + Assert.assertEquals(Constants.GENERIC_SERIALIZATION_BEAN, url.getParameter(Constants.GENERIC_KEY)); + } finally { + MockRegistryFactory.cleanCachedRegistry(); + service.unexport(); + } + } + + @Test + public void testGenericServiceConfigThroughSpring() throws Exception { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/generic-export.xml"); + try { + ctx.start(); + ServiceConfig serviceConfig = (ServiceConfig) ctx.getBean("dubboDemoService"); + URL url = (URL) serviceConfig.getExportedUrls().get(0); + Assert.assertEquals(Constants.GENERIC_SERIALIZATION_BEAN, url.getParameter(Constants.GENERIC_KEY)); + } finally { + ctx.destroy(); + } + } } \ No newline at end of file diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/ServiceBeanTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/ServiceBeanTest.java index 4def87135d8..467baf50be4 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/ServiceBeanTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/ServiceBeanTest.java @@ -17,9 +17,8 @@ package org.apache.dubbo.config.spring; import org.apache.dubbo.config.annotation.Service; - -import org.hamcrest.MatcherAssert; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.nullValue; @@ -32,7 +31,7 @@ public void testGetService() { ServiceBean serviceBean = new ServiceBean(service); Service beanService = serviceBean.getService(); - MatcherAssert.assertThat(beanService, not(nullValue())); + Assert.assertThat(beanService, not(nullValue())); } abstract class TestService implements Service { diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/SimpleRegistryExporter.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/SimpleRegistryExporter.java index 0db362af074..39878595e09 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/SimpleRegistryExporter.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/SimpleRegistryExporter.java @@ -18,7 +18,6 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.registry.RegistryService; @@ -53,15 +52,14 @@ public static Exporter export(int port) { public static Exporter export(int port, RegistryService registryService) { return protocol.export(proxyFactory.getInvoker(registryService, RegistryService.class, - new URLBuilder(Constants.DUBBO_PROTOCOL, NetUtils.getLocalHost(), port, RegistryService.class.getName()) + new URL("dubbo", NetUtils.getLocalHost(), port, RegistryService.class.getName()) .setPath(RegistryService.class.getName()) .addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()) .addParameter(Constants.CLUSTER_STICKY_KEY, "true") .addParameter(Constants.CALLBACK_INSTANCES_LIMIT_KEY, "1000") .addParameter("ondisconnect", "disconnect") .addParameter("subscribe.1.callback", "true") - .addParameter("unsubscribe.1.callback", "false") - .build())); + .addParameter("unsubscribe.1.callback", "false"))); } -} +} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/SimpleRegistryService.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/SimpleRegistryService.java index d90829d4a42..eebd354e77a 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/SimpleRegistryService.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/SimpleRegistryService.java @@ -19,7 +19,6 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.common.utils.UrlUtils; import org.apache.dubbo.registry.NotifyListener; @@ -73,7 +72,7 @@ public void subscribe(String service, URL url, NotifyListener listener) { } List urls = getRegistered().get(service); if ((RegistryService.class.getName() + ":0.0.0").equals(service) - && CollectionUtils.isEmpty(urls)) { + && (urls == null || urls.size() == 0)) { register(service, new URL("dubbo", NetUtils.getLocalHost(), RpcContext.getContext().getLocalPort(), diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/action/DemoActionByAnnotation.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/action/DemoActionByAnnotation.java index 6f0ea410555..f39ff2e6814 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/action/DemoActionByAnnotation.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/action/DemoActionByAnnotation.java @@ -1,35 +1,34 @@ -/* - * 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 org.apache.dubbo.config.spring.action; - -import org.apache.dubbo.config.spring.api.DemoService; - -import org.springframework.beans.factory.annotation.Autowired; - -/** - * DemoAction - */ -public class DemoActionByAnnotation { - - @Autowired - private DemoService demoService; - - public DemoService getDemoService() { - return demoService; - } - +/* + * 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 org.apache.dubbo.config.spring.action; + +import org.apache.dubbo.config.spring.api.DemoService; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * DemoAction + */ +public class DemoActionByAnnotation { + + @Autowired + private DemoService demoService; + + public DemoService getDemoService() { + return demoService; + } + } \ No newline at end of file diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/annotation/consumer/AnnotationAction.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/annotation/consumer/AnnotationAction.java index 1e71f01bb01..be6844532b4 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/annotation/consumer/AnnotationAction.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/annotation/consumer/AnnotationAction.java @@ -1,38 +1,36 @@ -/* - * 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 org.apache.dubbo.config.spring.annotation.consumer; - -import org.apache.dubbo.config.annotation.Method; -import org.apache.dubbo.config.annotation.Reference; -import org.apache.dubbo.config.spring.api.DemoService; - -import org.springframework.stereotype.Controller; - -/** - * AnnotationAction - */ -@Controller("annotationAction") -public class AnnotationAction { - - @Reference(version = "1.2", methods = {@Method(name = "sayHello", timeout = 5000)}) - private DemoService demoService; - - public String doSayName(String name) { - return demoService.sayName(name); - } - -} +/* + * 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 org.apache.dubbo.config.spring.annotation.consumer; + +import org.apache.dubbo.config.annotation.Reference; +import org.apache.dubbo.config.spring.api.DemoService; +import org.springframework.stereotype.Controller; + +/** + * AnnotationAction + */ +@Controller("annotationAction") +public class AnnotationAction { + + @Reference(version = "1.2") + private DemoService demoService; + + public String doSayName(String name) { + return demoService.sayName(name); + } + +} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationPropertyValuesAdapterTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationPropertyValuesAdapterTest.java index 0baa7bbe450..94148e66d74 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationPropertyValuesAdapterTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationPropertyValuesAdapterTest.java @@ -21,7 +21,6 @@ import org.apache.dubbo.config.annotation.Reference; import org.apache.dubbo.config.spring.ReferenceBean; import org.apache.dubbo.config.spring.api.DemoService; - import org.junit.Assert; import org.junit.Test; import org.springframework.core.annotation.AnnotationUtils; @@ -125,6 +124,7 @@ public Map convert(String[] source) { Assert.assertEquals("cache", referenceBean.getCache()); Assert.assertEquals("default,default", referenceBean.getFilter()); Assert.assertEquals("default,default", referenceBean.getListener()); + Assert.assertEquals("protocol", referenceBean.getProtocol()); Map data = new LinkedHashMap(); data.put("key1", "value1"); @@ -142,7 +142,7 @@ public Map convert(String[] source) { private static class TestBean { @Reference( - interfaceClass = DemoService.class, interfaceName = "com.alibaba.dubbo.config.spring.api.DemoService", version = "${version}", group = "group", + interfaceClass = DemoService.class, interfaceName = "org.apache.dubbo.config.spring.api.DemoService", version = "${version}", group = "group", url = "${url} ", client = "client", generic = true, injvm = true, check = false, init = true, lazy = true, stubevent = true, reconnect = "reconnect", sticky = true, proxy = "javassist", stub = "stub", @@ -151,7 +151,7 @@ private static class TestBean { loadbalance = "random", async = true, actives = 1, sent = true, mock = "mock", validation = "validation", timeout = 2, cache = "cache", filter = {"default", "default"}, listener = {"default", "default"}, parameters = {"key1", "value1"}, application = "application", - module = "module", consumer = "consumer", monitor = "monitor", registry = {"registry1", "registry2"} + module = "module", consumer = "consumer", monitor = "monitor", registry = {"registry1", "registry2"}, protocol = "protocol" ) private DemoService demoService; diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboConfigBindingBeanPostProcessorTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboConfigBindingBeanPostProcessorTest.java index 7811c7371f1..ec6e49a99e1 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboConfigBindingBeanPostProcessorTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboConfigBindingBeanPostProcessorTest.java @@ -19,8 +19,8 @@ import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.config.spring.context.properties.DefaultDubboConfigBinder; import org.apache.dubbo.config.spring.context.properties.DubboConfigBinder; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -60,7 +60,7 @@ public void test() { ApplicationConfig applicationConfig = applicationContext.getBean(ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application", applicationConfig.getName()); + Assert.assertEquals("dubbo-demo-application", applicationConfig.getName()); } } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessorTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessorTest.java index 6f626784db3..253ecf8a45a 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessorTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessorTest.java @@ -17,73 +17,83 @@ package org.apache.dubbo.config.spring.beans.factory.annotation; import org.apache.dubbo.config.annotation.Reference; +import org.apache.dubbo.config.context.ConfigManager; import org.apache.dubbo.config.spring.ReferenceBean; import org.apache.dubbo.config.spring.api.DemoService; +import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan; +import org.junit.After; import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; -import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.InjectionMetadata; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.ImportResource; import java.util.Collection; import java.util.Map; import static org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.BEAN_NAME; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.MatcherAssert.assertThat; /** * {@link ReferenceAnnotationBeanPostProcessor} Test * * @since 2.5.7 */ -@RunWith(SpringRunner.class) -@ContextConfiguration( - classes = { - ServiceAnnotationTestConfiguration.class, - ReferenceAnnotationBeanPostProcessorTest.class - }) -@TestPropertySource(properties = { - "packagesToScan = org.apache.dubbo.config.spring.context.annotation.provider", - "consumer.version = ${demo.service.version}", - "consumer.url = dubbo://127.0.0.1:12345", -}) public class ReferenceAnnotationBeanPostProcessorTest { - @Bean - public TestBean testBean() { - return new TestBean(); + private ConfigurableApplicationContext providerApplicationContext; + + @BeforeClass + public static void prepare() { + System.setProperty("provider.version", "1.2"); + System.setProperty("package1", "org.apache.dubbo.config.spring.annotation.provider"); + System.setProperty("packagesToScan", "${package1}"); + System.setProperty("consumer.version", "1.2"); + System.setProperty("consumer.url", "dubbo://127.0.0.1:12345"); } - @Bean(BEAN_NAME) - public ReferenceAnnotationBeanPostProcessor referenceAnnotationBeanPostProcessor() { - return new ReferenceAnnotationBeanPostProcessor(); + @Before + public void init() { + // Starts Provider + providerApplicationContext = new AnnotationConfigApplicationContext(ServiceAnnotationBeanPostProcessorTest.TestConfiguration.class); + ConfigManager.getInstance().clear(); } - @Autowired - private ConfigurableApplicationContext context; + @After + public void destroy() { + // Shutdowns Provider + providerApplicationContext.close(); + ConfigManager.getInstance().clear(); + } @Test public void test() throws Exception { - TestBean testBean = context.getBean(TestBean.class); - - DemoService demoService = testBean.getDemoService(); + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestBean.class); - Assert.assertEquals("Hello,Mercy", demoService.sayName("Mercy")); + TestBean testBean = context.getBean(TestBean.class); Assert.assertNotNull(testBean.getDemoServiceFromAncestor()); Assert.assertNotNull(testBean.getDemoServiceFromParent()); Assert.assertNotNull(testBean.getDemoService()); - Assert.assertEquals("Hello,Mercy", testBean.getDemoServiceFromAncestor().sayName("Mercy")); - Assert.assertEquals("Hello,Mercy", testBean.getDemoServiceFromParent().sayName("Mercy")); - Assert.assertEquals("Hello,Mercy", testBean.getDemoService().sayName("Mercy")); + Assert.assertEquals(testBean.getDemoServiceFromAncestor(), testBean.getDemoServiceFromParent()); + Assert.assertEquals(testBean.getDemoService(), testBean.getDemoServiceFromParent()); + + DemoService demoService = testBean.getDemoService(); + + Assert.assertEquals("annotation:Mercy", demoService.sayName("Mercy")); + + context.close(); } @@ -93,24 +103,35 @@ public void test() throws Exception { @Test public void testGetReferenceBeans() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestBean.class); + ReferenceAnnotationBeanPostProcessor beanPostProcessor = context.getBean(BEAN_NAME, ReferenceAnnotationBeanPostProcessor.class); Collection> referenceBeans = beanPostProcessor.getReferenceBeans(); - Assert.assertEquals(1, referenceBeans.size()); + /** + * 1 -> demoService、demoServiceShouldBeSame + * 1 -> demoServiceShouldNotBeSame + * 1 -> demoServiceWithArray、demoServiceWithArrayShouldBeSame + */ + Assert.assertEquals(3, referenceBeans.size()); ReferenceBean referenceBean = referenceBeans.iterator().next(); TestBean testBean = context.getBean(TestBean.class); - Assert.assertNotNull(referenceBean.get()); + Assert.assertEquals(referenceBean.get(), testBean.getDemoServiceFromAncestor()); + Assert.assertEquals(referenceBean.get(), testBean.getDemoServiceFromParent()); + Assert.assertEquals(referenceBean.get(), testBean.getDemoService()); } @Test public void testGetInjectedFieldReferenceBeanMap() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestBean.class); + ReferenceAnnotationBeanPostProcessor beanPostProcessor = context.getBean(BEAN_NAME, ReferenceAnnotationBeanPostProcessor.class); @@ -118,18 +139,21 @@ public void testGetInjectedFieldReferenceBeanMap() { Map> referenceBeanMap = beanPostProcessor.getInjectedFieldReferenceBeanMap(); - Assert.assertEquals(1, referenceBeanMap.size()); + /** + * contains 5 fields. + */ + Assert.assertEquals(5, referenceBeanMap.size()); for (Map.Entry> entry : referenceBeanMap.entrySet()) { InjectionMetadata.InjectedElement injectedElement = entry.getKey(); - Assert.assertEquals("org.apache.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor$AnnotatedFieldElement", + Assert.assertEquals("org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor$ReferenceFieldElement", injectedElement.getClass().getName()); ReferenceBean referenceBean = entry.getValue(); - Assert.assertEquals("2.5.7", referenceBean.getVersion()); + Assert.assertEquals("1.2", referenceBean.getVersion()); Assert.assertEquals("dubbo://127.0.0.1:12345", referenceBean.getUrl()); } @@ -139,6 +163,8 @@ public void testGetInjectedFieldReferenceBeanMap() { @Test public void testGetInjectedMethodReferenceBeanMap() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestBean.class); + ReferenceAnnotationBeanPostProcessor beanPostProcessor = context.getBean(BEAN_NAME, ReferenceAnnotationBeanPostProcessor.class); @@ -152,35 +178,79 @@ public void testGetInjectedMethodReferenceBeanMap() { InjectionMetadata.InjectedElement injectedElement = entry.getKey(); - Assert.assertEquals("org.apache.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor$AnnotatedMethodElement", + Assert.assertEquals("org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor$ReferenceMethodElement", injectedElement.getClass().getName()); ReferenceBean referenceBean = entry.getValue(); - Assert.assertEquals("2.5.7", referenceBean.getVersion()); + Assert.assertEquals("1.2", referenceBean.getVersion()); Assert.assertEquals("dubbo://127.0.0.1:12345", referenceBean.getUrl()); } } -// @Test -// public void testModuleInfo() { -// -// ReferenceAnnotationBeanPostProcessor beanPostProcessor = context.getBean(BEAN_NAME, -// ReferenceAnnotationBeanPostProcessor.class); -// -// -// Map> referenceBeanMap = -// beanPostProcessor.getInjectedMethodReferenceBeanMap(); -// -// for (Map.Entry> entry : referenceBeanMap.entrySet()) { -// ReferenceBean referenceBean = entry.getValue(); -// -// assertThat(referenceBean.getModule().getName(), is("defaultModule")); -// assertThat(referenceBean.getMonitor(), not(nullValue())); -// } -// } + @Test + public void testModuleInfo() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestBean.class); + + ReferenceAnnotationBeanPostProcessor beanPostProcessor = context.getBean(BEAN_NAME, + ReferenceAnnotationBeanPostProcessor.class); + + + Map> referenceBeanMap = + beanPostProcessor.getInjectedMethodReferenceBeanMap(); + + for (Map.Entry> entry : referenceBeanMap.entrySet()) { + ReferenceBean referenceBean = entry.getValue(); + + assertThat(referenceBean.getModule().getName(),is("defaultModule")); + assertThat(referenceBean.getMonitor(), not(nullValue())); + } + } + + @Test + public void testReferenceCache() throws Exception { + + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestBean.class); + + TestBean testBean = context.getBean(TestBean.class); + + Assert.assertNotNull(testBean.getDemoServiceFromAncestor()); + Assert.assertNotNull(testBean.getDemoServiceFromParent()); + Assert.assertNotNull(testBean.getDemoService()); + + Assert.assertEquals(testBean.getDemoServiceFromAncestor(), testBean.getDemoServiceFromParent()); + Assert.assertEquals(testBean.getDemoService(), testBean.getDemoServiceFromParent()); + + DemoService demoService = testBean.getDemoService(); + + Assert.assertEquals(demoService, testBean.getDemoServiceShouldBeSame()); + Assert.assertNotEquals(demoService, testBean.getDemoServiceShouldNotBeSame()); + + context.close(); + + } + + @Test + public void testReferenceCacheWithArray() throws Exception { + + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestBean.class); + + TestBean testBean = context.getBean(TestBean.class); + + Assert.assertNotNull(testBean.getDemoServiceFromAncestor()); + Assert.assertNotNull(testBean.getDemoServiceFromParent()); + Assert.assertNotNull(testBean.getDemoService()); + + Assert.assertEquals(testBean.getDemoServiceFromAncestor(), testBean.getDemoServiceFromParent()); + Assert.assertEquals(testBean.getDemoService(), testBean.getDemoServiceFromParent()); + + Assert.assertEquals(testBean.getDemoServiceWithArray(), testBean.getDemoServiceWithArrayShouldBeSame()); + + context.close(); + + } private static class AncestorBean { @@ -194,7 +264,7 @@ public DemoService getDemoServiceFromAncestor() { return demoServiceFromAncestor; } - @Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345") + @Reference(version = "1.2", url = "dubbo://127.0.0.1:12345") public void setDemoServiceFromAncestor(DemoService demoServiceFromAncestor) { this.demoServiceFromAncestor = demoServiceFromAncestor; } @@ -218,10 +288,25 @@ public DemoService getDemoServiceFromParent() { } + @ImportResource("META-INF/spring/dubbo-annotation-consumer.xml") + @DubboComponentScan(basePackageClasses = ReferenceAnnotationBeanPostProcessorTest.class) static class TestBean extends ParentBean { private DemoService demoService; + @Reference(version = "1.2", url = "dubbo://127.0.0.1:12345") + private DemoService demoServiceShouldBeSame; + + @Reference(version = "1.2", url = "dubbo://127.0.0.1:12345", async = true) + private DemoService demoServiceShouldNotBeSame; + + + @Reference(version = "1.2", url = "dubbo://127.0.0.1:12345", parameters = { "key1", "value1"}) + private DemoService demoServiceWithArray; + + @Reference(version = "1.2", url = "dubbo://127.0.0.1:12345", parameters = { "key1", "value1"}) + private DemoService demoServiceWithArrayShouldBeSame; + @Autowired private ApplicationContext applicationContext; @@ -229,10 +314,26 @@ public DemoService getDemoService() { return demoService; } - @Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345") + @Reference(version = "1.2", url = "dubbo://127.0.0.1:12345") public void setDemoService(DemoService demoService) { this.demoService = demoService; } + + public DemoService getDemoServiceShouldNotBeSame() { + return demoServiceShouldNotBeSame; + } + + public DemoService getDemoServiceShouldBeSame() { + return demoServiceShouldBeSame; + } + + public DemoService getDemoServiceWithArray() { + return demoServiceWithArray; + } + + public DemoService getDemoServiceWithArrayShouldBeSame() { + return demoServiceWithArrayShouldBeSame; + } } } \ No newline at end of file diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilderTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilderTest.java deleted file mode 100644 index 27f6c9663ed..00000000000 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilderTest.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.beans.factory.annotation; - - -import org.apache.dubbo.config.annotation.Reference; -import org.apache.dubbo.config.spring.ReferenceBean; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -import static org.springframework.core.annotation.AnnotationUtils.findAnnotation; -import static org.springframework.util.ReflectionUtils.findField; - -/** - * {@link ReferenceBeanBuilder} Test - * - * @author Mercy - * @see ReferenceBeanBuilder - * @see Reference - * @since 2.6.4 - */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = ReferenceBeanBuilderTest.class) -public class ReferenceBeanBuilderTest { - - @Reference( - interfaceClass = CharSequence.class, - interfaceName = "java.lang.CharSequence", - version = "1.0.0", group = "TEST_GROUP", url = "dubbo://localhost:12345", - client = "client", generic = true, injvm = true, - check = false, init = false, lazy = true, - stubevent = true, reconnect = "reconnect", sticky = true, - proxy = "javassist", stub = "java.lang.CharSequence", cluster = "failover", - connections = 3, callbacks = 1, onconnect = "onconnect", ondisconnect = "ondisconnect", - owner = "owner", layer = "layer", retries = 1, - loadbalance = "random", async = true, actives = 3, - sent = true, mock = "mock", validation = "validation", - timeout = 3, cache = "cache", filter = {"echo", "generic", "accesslog"}, - listener = {"deprecated"}, parameters = {"n1=v1 ", "n2 = v2 ", " n3 = v3 "}, - application = "application", - module = "module", consumer = "consumer", monitor = "monitor", registry = {"registry"} - ) - private static final Object TEST_FIELD = new Object(); - - @Autowired - private ApplicationContext context; - - @Test - public void testBuild() throws Exception { - Reference reference = findAnnotation(findField(getClass(), "TEST_FIELD"), Reference.class); - ReferenceBeanBuilder beanBuilder = ReferenceBeanBuilder.create(reference, context.getClassLoader(), context); - beanBuilder.interfaceClass(CharSequence.class); - ReferenceBean referenceBean = beanBuilder.build(); - Assert.assertEquals(CharSequence.class, referenceBean.getInterfaceClass()); - Assert.assertEquals("1.0.0", referenceBean.getVersion()); - Assert.assertEquals("TEST_GROUP", referenceBean.getGroup()); - Assert.assertEquals("dubbo://localhost:12345", referenceBean.getUrl()); - Assert.assertEquals("client", referenceBean.getClient()); - Assert.assertEquals(true, referenceBean.isGeneric()); - Assert.assertEquals(true, referenceBean.isInjvm()); - Assert.assertEquals(false, referenceBean.isCheck()); - Assert.assertEquals(null, referenceBean.isInit()); - Assert.assertEquals(true, referenceBean.getLazy()); - Assert.assertEquals(true, referenceBean.getStubevent()); - Assert.assertEquals("reconnect", referenceBean.getReconnect()); - Assert.assertEquals(true, referenceBean.getSticky()); - Assert.assertEquals("javassist", referenceBean.getProxy()); - Assert.assertEquals("java.lang.CharSequence", referenceBean.getStub()); - Assert.assertEquals("failover", referenceBean.getCluster()); - Assert.assertEquals(Integer.valueOf(3), referenceBean.getConnections()); - Assert.assertEquals(Integer.valueOf(1), referenceBean.getCallbacks()); - Assert.assertEquals("onconnect", referenceBean.getOnconnect()); - Assert.assertEquals("ondisconnect", referenceBean.getOndisconnect()); - Assert.assertEquals("owner", referenceBean.getOwner()); - Assert.assertEquals("layer", referenceBean.getLayer()); - Assert.assertEquals(Integer.valueOf(1), referenceBean.getRetries()); - Assert.assertEquals("random", referenceBean.getLoadbalance()); - Assert.assertEquals(true, referenceBean.isAsync()); - Assert.assertEquals(Integer.valueOf(3), referenceBean.getActives()); - Assert.assertEquals(true, referenceBean.getSent()); - Assert.assertEquals("mock", referenceBean.getMock()); - Assert.assertEquals("validation", referenceBean.getValidation()); - Assert.assertEquals(Integer.valueOf(3), referenceBean.getTimeout()); - Assert.assertEquals("cache", referenceBean.getCache()); - Assert.assertEquals("echo,generic,accesslog", referenceBean.getFilter()); - Assert.assertEquals("deprecated", referenceBean.getListener()); - - // parameters - Map parameters = new HashMap(); - parameters.put("n1", "v1"); - parameters.put("n2", "v2"); - parameters.put("n3", "v3"); - Assert.assertEquals(parameters, referenceBean.getParameters()); - - // Asserts Null fields - Assert.assertNull(referenceBean.getApplication()); - Assert.assertNull(referenceBean.getModule()); - Assert.assertNull(referenceBean.getConsumer()); - Assert.assertNull(referenceBean.getMonitor()); - Assert.assertEquals(Collections.emptyList(), referenceBean.getRegistries()); - } -} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessorTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessorTest.java index 22325d96ebf..35de555c30d 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessorTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessorTest.java @@ -18,7 +18,6 @@ import org.apache.dubbo.config.spring.ServiceBean; import org.apache.dubbo.config.spring.api.HelloService; - import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -26,6 +25,9 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.ImportResource; +import org.springframework.context.annotation.PropertySource; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; @@ -39,25 +41,17 @@ */ @RunWith(SpringRunner.class) @ContextConfiguration( - classes = { - ServiceAnnotationTestConfiguration.class, - ServiceAnnotationBeanPostProcessorTest.class - }) + classes = {ServiceAnnotationBeanPostProcessorTest.TestConfiguration.class}) @TestPropertySource(properties = { - "provider.package = org.apache.dubbo.config.spring.context.annotation.provider", - "packagesToScan = ${provider.package}", + "package1 = org.apache.dubbo.config.spring.context.annotation", + "packagesToScan = ${package1}", + "provider.version = 1.2" }) public class ServiceAnnotationBeanPostProcessorTest { @Autowired private ConfigurableListableBeanFactory beanFactory; - @Bean - public ServiceAnnotationBeanPostProcessor serviceAnnotationBeanPostProcessor2 - (@Value("${packagesToScan}") String... packagesToScan) { - return new ServiceAnnotationBeanPostProcessor(packagesToScan); - } - @Test public void test() { @@ -67,16 +61,38 @@ public void test() { Map serviceBeansMap = beanFactory.getBeansOfType(ServiceBean.class); - Assert.assertEquals(2, serviceBeansMap.size()); + Assert.assertEquals(3, serviceBeansMap.size()); Map beanPostProcessorsMap = beanFactory.getBeansOfType(ServiceAnnotationBeanPostProcessor.class); - Assert.assertEquals(2, beanPostProcessorsMap.size()); + Assert.assertEquals(4, beanPostProcessorsMap.size()); + Assert.assertTrue(beanPostProcessorsMap.containsKey("doubleServiceAnnotationBeanPostProcessor")); + Assert.assertTrue(beanPostProcessorsMap.containsKey("emptyServiceAnnotationBeanPostProcessor")); Assert.assertTrue(beanPostProcessorsMap.containsKey("serviceAnnotationBeanPostProcessor")); Assert.assertTrue(beanPostProcessorsMap.containsKey("serviceAnnotationBeanPostProcessor2")); } + @ImportResource("META-INF/spring/dubbo-annotation-provider.xml") + @PropertySource("META-INF/default.properties") + @ComponentScan("org.apache.dubbo.config.spring.context.annotation.provider") + public static class TestConfiguration { + + @Bean + public ServiceAnnotationBeanPostProcessor serviceAnnotationBeanPostProcessor + (@Value("${packagesToScan}") String... packagesToScan) { + return new ServiceAnnotationBeanPostProcessor(packagesToScan); + } + + @Bean + public ServiceAnnotationBeanPostProcessor serviceAnnotationBeanPostProcessor2 + (@Value("${packagesToScan}") String... packagesToScan) { + return new ServiceAnnotationBeanPostProcessor(packagesToScan); + } + + + } + } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationTestConfiguration.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationTestConfiguration.java deleted file mode 100644 index 7a206db7852..00000000000 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationTestConfiguration.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.beans.factory.annotation; - - -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.config.ProtocolConfig; -import org.apache.dubbo.config.RegistryConfig; -import org.apache.dubbo.config.annotation.Service; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Primary; -import org.springframework.context.annotation.PropertySource; -import org.springframework.transaction.PlatformTransactionManager; -import org.springframework.transaction.TransactionDefinition; -import org.springframework.transaction.TransactionException; -import org.springframework.transaction.TransactionStatus; - -/** - * {@link Service} Bean - * - * @since 2.6.5 - */ -@PropertySource("classpath:/META-INF/default.properties") -public class ServiceAnnotationTestConfiguration { - - /** - * Current application configuration, to replace XML config: - * - * <dubbo:application name="dubbo-demo-application"/> - * - * - * @return {@link ApplicationConfig} Bean - */ - @Bean("dubbo-demo-application") - public ApplicationConfig applicationConfig() { - ApplicationConfig applicationConfig = new ApplicationConfig(); - applicationConfig.setName("dubbo-demo-application"); - return applicationConfig; - } - - /** - * Current registry center configuration, to replace XML config: - * - * <dubbo:registry id="my-registry" address="N/A"/> - * - * - * @return {@link RegistryConfig} Bean - */ - @Bean("my-registry") - public RegistryConfig registryConfig() { - RegistryConfig registryConfig = new RegistryConfig(); - registryConfig.setAddress("N/A"); - return registryConfig; - } - - /** - * Current protocol configuration, to replace XML config: - * - * <dubbo:protocol name="dubbo" port="12345"/> - * - * - * @return {@link ProtocolConfig} Bean - */ - @Bean("dubbo") - public ProtocolConfig protocolConfig() { - ProtocolConfig protocolConfig = new ProtocolConfig(); - protocolConfig.setName("dubbo"); - protocolConfig.setPort(12345); - return protocolConfig; - } - - @Primary - @Bean - public PlatformTransactionManager platformTransactionManager() { - return new PlatformTransactionManager() { - - @Override - public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException { - return null; - } - - @Override - public void commit(TransactionStatus status) throws TransactionException { - - } - - @Override - public void rollback(TransactionStatus status) throws TransactionException { - - } - }; - } - - @Bean - public ServiceAnnotationBeanPostProcessor serviceAnnotationBeanPostProcessor - (@Value("${packagesToScan}") String... packagesToScan) { - return new ServiceAnnotationBeanPostProcessor(packagesToScan); - } - -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilderTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilderTest.java deleted file mode 100644 index b3616cd58a3..00000000000 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilderTest.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.beans.factory.annotation; - - -import org.apache.dubbo.config.annotation.Reference; -import org.apache.dubbo.config.annotation.Service; -import org.apache.dubbo.config.spring.api.DemoService; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.springframework.core.annotation.AnnotationUtils; -import org.springframework.mock.env.MockEnvironment; -import org.springframework.util.ReflectionUtils; - - -/** - * {@link ServiceBeanNameBuilder} Test - * - * @see ServiceBeanNameBuilder - * @since 2.6.5 - */ -@Service(interfaceClass = DemoService.class, group = ServiceBeanNameBuilderTest.GROUP, version = ServiceBeanNameBuilderTest.VERSION, - application = "application", module = "module", registry = {"1", "2", "3"}) -public class ServiceBeanNameBuilderTest { - - @Reference(interfaceClass = DemoService.class, group = "DUBBO", version = "1.0.0", - application = "application", module = "module", registry = {"1", "2", "3"}) - static final Class INTERFACE_CLASS = DemoService.class; - - static final String GROUP = "DUBBO"; - - static final String VERSION = "1.0.0"; - - static final String BEAN_NAME = "ServiceBean:org.apache.dubbo.config.spring.api.DemoService:1.0.0:DUBBO"; - - private MockEnvironment environment = new MockEnvironment(); - - @Test - public void testRequiredAttributes() { - ServiceBeanNameBuilder builder = ServiceBeanNameBuilder.create(INTERFACE_CLASS, environment); - Assertions.assertEquals("ServiceBean:org.apache.dubbo.config.spring.api.DemoService", builder.build()); - } - - @Test - public void testServiceAnnotation() { - Service service = AnnotationUtils.getAnnotation(ServiceBeanNameBuilderTest.class, Service.class); - ServiceBeanNameBuilder builder = ServiceBeanNameBuilder.create(service, INTERFACE_CLASS, environment); - Assertions.assertEquals(BEAN_NAME, - builder.build()); - } - - @Test - public void testReferenceAnnotation() { - Reference reference = AnnotationUtils.getAnnotation(ReflectionUtils.findField(ServiceBeanNameBuilderTest.class, "INTERFACE_CLASS"), Reference.class); - ServiceBeanNameBuilder builder = ServiceBeanNameBuilder.create(reference, INTERFACE_CLASS, environment); - Assertions.assertEquals(BEAN_NAME, - builder.build()); - } - -} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/config/YamlPropertySourceFactory.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/config/YamlPropertySourceFactory.java deleted file mode 100644 index c9b44c94ad1..00000000000 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/config/YamlPropertySourceFactory.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.beans.factory.config; - -import org.springframework.beans.factory.config.YamlProcessor; -import org.springframework.core.env.MapPropertySource; -import org.springframework.core.env.PropertySource; -import org.springframework.core.io.support.EncodedResource; -import org.springframework.core.io.support.PropertySourceFactory; -import org.yaml.snakeyaml.DumperOptions; -import org.yaml.snakeyaml.Yaml; -import org.yaml.snakeyaml.nodes.Tag; -import org.yaml.snakeyaml.representer.Representer; -import org.yaml.snakeyaml.resolver.Resolver; - -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Properties; -import java.util.regex.Pattern; - -/** - * YAML {@link PropertySourceFactory} implementation, some source code is copied Spring Boot - * org.springframework.boot.env.YamlPropertySourceLoader , see {@link #createYaml()} and {@link #process()} - * - * @since 2.6.5 - */ -public class YamlPropertySourceFactory extends YamlProcessor implements PropertySourceFactory { - - @Override - public PropertySource createPropertySource(String name, EncodedResource resource) throws IOException { - setResources(resource.getResource()); - return new MapPropertySource(name, process()); - } - - @Override - protected Yaml createYaml() { - return new Yaml(new StrictMapAppenderConstructor(), new Representer(), - new DumperOptions(), new Resolver() { - @Override - public void addImplicitResolver(Tag tag, Pattern regexp, - String first) { - if (tag == Tag.TIMESTAMP) { - return; - } - super.addImplicitResolver(tag, regexp, first); - } - }); - } - - public Map process() { - final Map result = new LinkedHashMap(); - process(new MatchCallback() { - @Override - public void process(Properties properties, Map map) { - result.putAll(getFlattenedMap(map)); - } - }); - return result; - } - -} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/config/YamlPropertySourceFactoryTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/config/YamlPropertySourceFactoryTest.java deleted file mode 100644 index a9861d33c1b..00000000000 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/config/YamlPropertySourceFactoryTest.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.beans.factory.config; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.core.env.Environment; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringRunner; - -/** - * {@link YamlPropertySourceFactory} Test - * - * @since 2.6.5 - */ -@RunWith(SpringRunner.class) -@PropertySource(name = "yaml-source", value = {"classpath:/META-INF/dubbo.yml"}, factory = YamlPropertySourceFactory.class) -@Configuration -@ContextConfiguration(classes = YamlPropertySourceFactoryTest.class) -public class YamlPropertySourceFactoryTest { - - @Autowired - private Environment environment; - - @Value("${dubbo.consumer.default}") - private Boolean isDefault; - - @Value("${dubbo.consumer.client}") - private String client; - - @Value("${dubbo.consumer.threadpool}") - private String threadPool; - - @Value("${dubbo.consumer.corethreads}") - private Integer coreThreads; - - @Value("${dubbo.consumer.threads}") - private Integer threads; - - @Value("${dubbo.consumer.queues}") - private Integer queues; - - @Test - public void testProperty() { - Assert.assertEquals(isDefault, environment.getProperty("dubbo.consumer.default", Boolean.class)); - Assert.assertEquals(client, environment.getProperty("dubbo.consumer.client", String.class)); - Assert.assertEquals(threadPool, environment.getProperty("dubbo.consumer.threadpool", String.class)); - Assert.assertEquals(coreThreads, environment.getProperty("dubbo.consumer.corethreads", Integer.class)); - Assert.assertEquals(threads, environment.getProperty("dubbo.consumer.threads", Integer.class)); - Assert.assertEquals(queues, environment.getProperty("dubbo.consumer.queues", Integer.class)); - } -} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/DubboComponentScanRegistrarTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/DubboComponentScanRegistrarTest.java index a6d219ad803..c8b35776ac7 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/DubboComponentScanRegistrarTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/DubboComponentScanRegistrarTest.java @@ -22,10 +22,10 @@ import org.apache.dubbo.config.spring.context.annotation.provider.DemoServiceImpl; import org.apache.dubbo.config.spring.context.annotation.provider.ProviderConfiguration; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import org.springframework.aop.support.AopUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.transaction.annotation.Transactional; @@ -39,12 +39,12 @@ */ public class DubboComponentScanRegistrarTest { - @BeforeEach + @Before public void setUp() { ConfigManager.getInstance().clear(); } - @AfterEach + @After public void tearDown() { ConfigManager.getInstance().clear(); } @@ -62,15 +62,15 @@ public void test() { String value = demoService.sayName("Mercy"); - Assertions.assertEquals("Hello,Mercy", value); + Assert.assertEquals("Hello,Mercy", value); Class beanClass = AopUtils.getTargetClass(demoService); // DemoServiceImpl with @Transactional - Assertions.assertEquals(DemoServiceImpl.class, beanClass); + Assert.assertEquals(DemoServiceImpl.class, beanClass); // Test @Transactional is present or not - Assertions.assertNotNull(findAnnotation(beanClass, Transactional.class)); + Assert.assertNotNull(findAnnotation(beanClass, Transactional.class)); AnnotationConfigApplicationContext consumerContext = new AnnotationConfigApplicationContext(); @@ -84,7 +84,7 @@ public void test() { value = demoService.sayName("Mercy"); - Assertions.assertEquals("Hello,Mercy", value); + Assert.assertEquals("Hello,Mercy", value); ConsumerConfiguration.Child child = consumerContext.getBean(ConsumerConfiguration.Child.class); @@ -92,31 +92,31 @@ public void test() { demoService = child.getDemoServiceFromChild(); - Assertions.assertNotNull(demoService); + Assert.assertNotNull(demoService); value = demoService.sayName("Mercy"); - Assertions.assertEquals("Hello,Mercy", value); + Assert.assertEquals("Hello,Mercy", value); // From Parent demoService = child.getDemoServiceFromParent(); - Assertions.assertNotNull(demoService); + Assert.assertNotNull(demoService); value = demoService.sayName("Mercy"); - Assertions.assertEquals("Hello,Mercy", value); + Assert.assertEquals("Hello,Mercy", value); // From Ancestor demoService = child.getDemoServiceFromAncestor(); - Assertions.assertNotNull(demoService); + Assert.assertNotNull(demoService); value = demoService.sayName("Mercy"); - Assertions.assertEquals("Hello,Mercy", value); + Assert.assertEquals("Hello,Mercy", value); providerContext.close(); consumerContext.close(); diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingRegistrarTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingRegistrarTest.java index e11e93548dc..a5e8e682828 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingRegistrarTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingRegistrarTest.java @@ -17,8 +17,8 @@ package org.apache.dubbo.config.spring.context.annotation; import org.apache.dubbo.config.ApplicationConfig; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.PropertySource; @@ -40,7 +40,7 @@ public void testRegisterBeanDefinitionsForSingle() { ApplicationConfig applicationConfig = context.getBean("applicationBean", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application", applicationConfig.getName()); + Assert.assertEquals("dubbo-demo-application", applicationConfig.getName()); } @@ -56,15 +56,15 @@ public void testRegisterBeanDefinitionsForMultiple() { ApplicationConfig applicationConfig = context.getBean("applicationBean", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application", applicationConfig.getName()); + Assert.assertEquals("dubbo-demo-application", applicationConfig.getName()); applicationConfig = context.getBean("applicationBean2", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application2", applicationConfig.getName()); + Assert.assertEquals("dubbo-demo-application2", applicationConfig.getName()); applicationConfig = context.getBean("applicationBean3", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application3", applicationConfig.getName()); + Assert.assertEquals("dubbo-demo-application3", applicationConfig.getName()); } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingsRegistrarTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingsRegistrarTest.java index 04337edf8f3..93c50d720f2 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingsRegistrarTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingsRegistrarTest.java @@ -17,8 +17,8 @@ package org.apache.dubbo.config.spring.context.annotation; import org.apache.dubbo.config.ApplicationConfig; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.PropertySource; @@ -40,9 +40,9 @@ public void test() { ApplicationConfig applicationConfig = context.getBean("applicationBean", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application", applicationConfig.getName()); + Assert.assertEquals("dubbo-demo-application", applicationConfig.getName()); - Assertions.assertEquals(2, context.getBeansOfType(ApplicationConfig.class).size()); + Assert.assertEquals(2, context.getBeansOfType(ApplicationConfig.class).size()); } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfigurationTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfigurationTest.java index a0e9b48c60a..a1242337c60 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfigurationTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfigurationTest.java @@ -20,12 +20,10 @@ import org.apache.dubbo.config.ModuleConfig; import org.apache.dubbo.config.ProtocolConfig; import org.apache.dubbo.config.RegistryConfig; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.core.io.support.ResourcePropertySource; @@ -40,7 +38,7 @@ public class DubboConfigConfigurationTest { private AnnotationConfigApplicationContext context; - @BeforeEach + @Before public void before() throws IOException { context = new AnnotationConfigApplicationContext(); @@ -49,7 +47,7 @@ public void before() throws IOException { } - @AfterEach + @After public void after() { context.close(); } @@ -62,20 +60,20 @@ public void testSingle() throws IOException { // application ApplicationConfig applicationConfig = context.getBean("applicationBean", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application", applicationConfig.getName()); + Assert.assertEquals("dubbo-demo-application", applicationConfig.getName()); // module ModuleConfig moduleConfig = context.getBean("moduleBean", ModuleConfig.class); - Assertions.assertEquals("dubbo-demo-module", moduleConfig.getName()); + Assert.assertEquals("dubbo-demo-module", moduleConfig.getName()); // registry RegistryConfig registryConfig = context.getBean(RegistryConfig.class); - Assertions.assertEquals("zookeeper://192.168.99.100:32770", registryConfig.getAddress()); + Assert.assertEquals("zookeeper://192.168.99.100:32770", registryConfig.getAddress()); // protocol ProtocolConfig protocolConfig = context.getBean(ProtocolConfig.class); - Assertions.assertEquals("dubbo", protocolConfig.getName()); - Assertions.assertEquals(Integer.valueOf(20880), protocolConfig.getPort()); + Assert.assertEquals("dubbo", protocolConfig.getName()); + Assert.assertEquals(Integer.valueOf(20880), protocolConfig.getPort()); } @Test @@ -86,13 +84,13 @@ public void testMultiple() { // application ApplicationConfig applicationConfig = context.getBean("applicationBean", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application", applicationConfig.getName()); + Assert.assertEquals("dubbo-demo-application", applicationConfig.getName()); ApplicationConfig applicationBean2 = context.getBean("applicationBean2", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application2", applicationBean2.getName()); + Assert.assertEquals("dubbo-demo-application2", applicationBean2.getName()); ApplicationConfig applicationBean3 = context.getBean("applicationBean3", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application3", applicationBean3.getName()); + Assert.assertEquals("dubbo-demo-application3", applicationBean3.getName()); } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigTest.java index 5f52a3e0ec0..1b85aa2190b 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigTest.java @@ -23,10 +23,8 @@ import org.apache.dubbo.config.ProtocolConfig; import org.apache.dubbo.config.ProviderConfig; import org.apache.dubbo.config.RegistryConfig; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - +import org.junit.Assert; +import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.PropertySource; @@ -46,33 +44,33 @@ public void testSingle() { // application ApplicationConfig applicationConfig = context.getBean("applicationBean", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application", applicationConfig.getName()); + Assert.assertEquals("dubbo-demo-application", applicationConfig.getName()); // module ModuleConfig moduleConfig = context.getBean("moduleBean", ModuleConfig.class); - Assertions.assertEquals("dubbo-demo-module", moduleConfig.getName()); + Assert.assertEquals("dubbo-demo-module", moduleConfig.getName()); // registry RegistryConfig registryConfig = context.getBean(RegistryConfig.class); - Assertions.assertEquals("zookeeper://192.168.99.100:32770", registryConfig.getAddress()); + Assert.assertEquals("zookeeper://192.168.99.100:32770", registryConfig.getAddress()); // protocol ProtocolConfig protocolConfig = context.getBean(ProtocolConfig.class); - Assertions.assertEquals("dubbo", protocolConfig.getName()); - Assertions.assertEquals(Integer.valueOf(20880), protocolConfig.getPort()); + Assert.assertEquals("dubbo", protocolConfig.getName()); + Assert.assertEquals(Integer.valueOf(20880), protocolConfig.getPort()); // monitor MonitorConfig monitorConfig = context.getBean(MonitorConfig.class); - Assertions.assertEquals("zookeeper://127.0.0.1:32770", monitorConfig.getAddress()); + Assert.assertEquals("zookeeper://127.0.0.1:32770", monitorConfig.getAddress()); // provider ProviderConfig providerConfig = context.getBean(ProviderConfig.class); - Assertions.assertEquals("127.0.0.1", providerConfig.getHost()); + Assert.assertEquals("127.0.0.1", providerConfig.getHost()); // consumer ConsumerConfig consumerConfig = context.getBean(ConsumerConfig.class); - Assertions.assertEquals("netty", consumerConfig.getClient()); + Assert.assertEquals("netty", consumerConfig.getClient()); } @@ -85,13 +83,13 @@ public void testMultiple() { // application ApplicationConfig applicationConfig = context.getBean("applicationBean", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application", applicationConfig.getName()); + Assert.assertEquals("dubbo-demo-application", applicationConfig.getName()); ApplicationConfig applicationBean2 = context.getBean("applicationBean2", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application2", applicationBean2.getName()); + Assert.assertEquals("dubbo-demo-application2", applicationBean2.getName()); ApplicationConfig applicationBean3 = context.getBean("applicationBean3", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application3", applicationBean3.getName()); + Assert.assertEquals("dubbo-demo-application3", applicationBean3.getName()); } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboTest.java index b61198949c0..85b8b71ac29 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboTest.java @@ -22,10 +22,10 @@ import org.apache.dubbo.config.spring.context.annotation.consumer.test.TestConsumerConfiguration; import org.apache.dubbo.config.spring.context.annotation.provider.DemoServiceImpl; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import org.springframework.aop.support.AopUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; @@ -48,101 +48,100 @@ */ public class EnableDubboTest { - private AnnotationConfigApplicationContext context; - - @BeforeEach + @Before public void setUp() { ConfigManager.getInstance().clear(); - context = new AnnotationConfigApplicationContext(); } - @AfterEach + @After public void tearDown() { ConfigManager.getInstance().clear(); - context.close(); } @Test - public void testProvider() { + public void test() { + + AnnotationConfigApplicationContext providerContext = new AnnotationConfigApplicationContext(); - context.register(TestProviderConfiguration.class); + providerContext.register(TestProviderConfiguration.class); - context.refresh(); + providerContext.refresh(); - DemoService demoService = context.getBean(DemoService.class); + DemoService demoService = providerContext.getBean(DemoService.class); String value = demoService.sayName("Mercy"); - Assertions.assertEquals("Hello,Mercy", value); + Assert.assertEquals("Hello,Mercy", value); Class beanClass = AopUtils.getTargetClass(demoService); // DemoServiceImpl with @Transactional - Assertions.assertEquals(DemoServiceImpl.class, beanClass); + Assert.assertEquals(DemoServiceImpl.class, beanClass); // Test @Transactional is present or not - Assertions.assertNotNull(findAnnotation(beanClass, Transactional.class)); + Assert.assertNotNull(findAnnotation(beanClass, Transactional.class)); - } - - @Test - public void testConsumer() { + AnnotationConfigApplicationContext consumerContext = new AnnotationConfigApplicationContext(); - context.register(TestProviderConfiguration.class, TestConsumerConfiguration.class); + consumerContext.register(TestConsumerConfiguration.class); - context.refresh(); + consumerContext.refresh(); - TestConsumerConfiguration consumerConfiguration = context.getBean(TestConsumerConfiguration.class); + TestConsumerConfiguration consumerConfiguration = consumerContext.getBean(TestConsumerConfiguration.class); - DemoService demoService = consumerConfiguration.getDemoService(); + demoService = consumerConfiguration.getDemoService(); - String value = demoService.sayName("Mercy"); + value = demoService.sayName("Mercy"); - Assertions.assertEquals("Hello,Mercy", value); + Assert.assertEquals("Hello,Mercy", value); - TestConsumerConfiguration.Child child = context.getBean(TestConsumerConfiguration.Child.class); + TestConsumerConfiguration.Child child = consumerContext.getBean(TestConsumerConfiguration.Child.class); // From Child demoService = child.getDemoServiceFromChild(); - Assertions.assertNotNull(demoService); + Assert.assertNotNull(demoService); value = demoService.sayName("Mercy"); - Assertions.assertEquals("Hello,Mercy", value); + Assert.assertEquals("Hello,Mercy", value); // From Parent demoService = child.getDemoServiceFromParent(); - Assertions.assertNotNull(demoService); + Assert.assertNotNull(demoService); value = demoService.sayName("Mercy"); - Assertions.assertEquals("Hello,Mercy", value); + Assert.assertEquals("Hello,Mercy", value); // From Ancestor demoService = child.getDemoServiceFromAncestor(); - Assertions.assertNotNull(demoService); + Assert.assertNotNull(demoService); value = demoService.sayName("Mercy"); - Assertions.assertEquals("Hello,Mercy", value); + Assert.assertEquals("Hello,Mercy", value); - // Test my-registry2 bean presentation - RegistryConfig registryConfig = context.getBean("my-registry2", RegistryConfig.class); + // Test dubbo-annotation-consumer2 bean presentation + RegistryConfig registryConfig = consumerContext.getBean("my-registry2", RegistryConfig.class); // Test multiple binding - Assertions.assertEquals("N/A", registryConfig.getAddress()); + Assert.assertEquals("N/A", registryConfig.getAddress()); + + providerContext.close(); + consumerContext.close(); + } @EnableDubbo(scanBasePackages = "org.apache.dubbo.config.spring.context.annotation.provider") @ComponentScan(basePackages = "org.apache.dubbo.config.spring.context.annotation.provider") - @PropertySource("classpath:/META-INF/dubbo-provider.properties") + @PropertySource("META-INF/dubbo-provider.properties") @EnableTransactionManagement public static class TestProviderConfiguration { diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/consumer/ConsumerConfiguration.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/consumer/ConsumerConfiguration.java index c12d4d3bab1..2c07d5ba9aa 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/consumer/ConsumerConfiguration.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/consumer/ConsumerConfiguration.java @@ -36,15 +36,15 @@ public class ConsumerConfiguration { /** * Current application configuration, to replace XML config: * - * <dubbo:application name="dubbo-demo-application"/> + * <dubbo:application name="dubbo-annotation-consumer"/> * * * @return {@link ApplicationConfig} Bean */ - @Bean("dubbo-demo-application") + @Bean("dubbo-annotation-test") public ApplicationConfig applicationConfig() { ApplicationConfig applicationConfig = new ApplicationConfig(); - applicationConfig.setName("dubbo-demo-application"); + applicationConfig.setName("dubbo-annotation-test"); return applicationConfig; } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/consumer/test/TestConsumerConfiguration.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/consumer/test/TestConsumerConfiguration.java index 231754501f8..d6ed2bd55f3 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/consumer/test/TestConsumerConfiguration.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/consumer/test/TestConsumerConfiguration.java @@ -30,11 +30,11 @@ * @since 2.5.7 */ @EnableDubbo(scanBasePackageClasses = TestConsumerConfiguration.class, multipleConfig = true) -@PropertySource("classpath:/META-INF/dubbb-consumer.properties") +@PropertySource("META-INF/dubbo-consumer.properties") @EnableTransactionManagement public class TestConsumerConfiguration { - @Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345", application = "dubbo-demo-application") + @Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345", application = "dubbo-annotation-test") private DemoService demoService; public DemoService getDemoService() { @@ -53,7 +53,7 @@ public TestConsumerConfiguration.Child c() { public static abstract class Ancestor { - @Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345", application = "dubbo-demo-application") + @Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345", application = "dubbo-annotation-test") private DemoService demoServiceFromAncestor; public DemoService getDemoServiceFromAncestor() { @@ -73,7 +73,7 @@ public DemoService getDemoServiceFromParent() { return demoServiceFromParent; } - @Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345", application = "dubbo-demo-application") + @Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345", application = "dubbo-annotation-test") public void setDemoServiceFromParent(DemoService demoServiceFromParent) { this.demoServiceFromParent = demoServiceFromParent; } @@ -82,7 +82,7 @@ public void setDemoServiceFromParent(DemoService demoServiceFromParent) { public static class Child extends TestConsumerConfiguration.Parent { - @Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345", application = "dubbo-demo-application") + @Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345", application = "dubbo-annotation-test") private DemoService demoServiceFromChild; public DemoService getDemoServiceFromChild() { diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/DefaultHelloService.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/DefaultHelloService.java index f7923644095..bb5c7fd4c17 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/DefaultHelloService.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/DefaultHelloService.java @@ -17,7 +17,6 @@ package org.apache.dubbo.config.spring.context.annotation.provider; import org.apache.dubbo.config.spring.api.HelloService; - import org.springframework.stereotype.Service; /** diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/DemoServiceImpl.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/DemoServiceImpl.java index eb12225588f..1b21369f0f7 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/DemoServiceImpl.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/DemoServiceImpl.java @@ -18,7 +18,6 @@ import org.apache.dubbo.config.spring.api.Box; import org.apache.dubbo.config.spring.api.DemoService; - import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/ProviderConfiguration.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/ProviderConfiguration.java index d168af8bc2b..abac294ae8d 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/ProviderConfiguration.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/ProviderConfiguration.java @@ -31,22 +31,22 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; @DubboComponentScan(basePackages = "org.apache.dubbo.config.spring.context.annotation.provider") -@PropertySource("classpath:/META-INF/default.properties") +@PropertySource("META-INF/default.properties") @EnableTransactionManagement public class ProviderConfiguration { /** * Current application configuration, to replace XML config: * - * <dubbo:application name="dubbo-demo-application"/> + * <dubbo:application name="dubbo-annotation-provider"/> * * * @return {@link ApplicationConfig} Bean */ - @Bean("dubbo-demo-application") + @Bean("dubbo-annotation-test") public ApplicationConfig applicationConfig() { ApplicationConfig applicationConfig = new ApplicationConfig(); - applicationConfig.setName("dubbo-demo-application"); + applicationConfig.setName("dubbo-annotation-test"); return applicationConfig; } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/DubboComponentScanRegistrarTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/DubboComponentScanRegistrarTest.java deleted file mode 100644 index 5d28adfd4a2..00000000000 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/DubboComponentScanRegistrarTest.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.context.context.annotation; - -import org.apache.dubbo.config.spring.api.DemoService; -import org.apache.dubbo.config.spring.context.annotation.DubboComponentScanRegistrar; -import org.apache.dubbo.config.spring.context.annotation.consumer.ConsumerConfiguration; -import org.apache.dubbo.config.spring.context.annotation.provider.DemoServiceImpl; -import org.apache.dubbo.config.spring.context.annotation.provider.ProviderConfiguration; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.springframework.aop.support.AopUtils; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.transaction.annotation.Transactional; - -import static org.springframework.core.annotation.AnnotationUtils.findAnnotation; - -/** - * {@link DubboComponentScanRegistrar} Test - * - * @since 2.5.8 - */ -public class DubboComponentScanRegistrarTest { - - @Test - public void test() { - - AnnotationConfigApplicationContext providerContext = new AnnotationConfigApplicationContext(); - - providerContext.register(ProviderConfiguration.class); - - providerContext.refresh(); - - DemoService demoService = providerContext.getBean(DemoService.class); - - String value = demoService.sayName("Mercy"); - - Assertions.assertEquals("Hello,Mercy", value); - - Class beanClass = AopUtils.getTargetClass(demoService); - - // DemoServiceImpl with @Transactional - Assertions.assertEquals(DemoServiceImpl.class, beanClass); - - // Test @Transactional is present or not - Assertions.assertNotNull(findAnnotation(beanClass, Transactional.class)); - - AnnotationConfigApplicationContext consumerContext = new AnnotationConfigApplicationContext(); - - consumerContext.register(ConsumerConfiguration.class); - - consumerContext.refresh(); - - ConsumerConfiguration consumerConfiguration = consumerContext.getBean(ConsumerConfiguration.class); - - demoService = consumerConfiguration.getDemoService(); - - value = demoService.sayName("Mercy"); - - Assertions.assertEquals("Hello,Mercy", value); - - ConsumerConfiguration.Child child = consumerContext.getBean(ConsumerConfiguration.Child.class); - - // From Child - - demoService = child.getDemoServiceFromChild(); - - Assertions.assertNotNull(demoService); - - value = demoService.sayName("Mercy"); - - Assertions.assertEquals("Hello,Mercy", value); - - // From Parent - - demoService = child.getDemoServiceFromParent(); - - Assertions.assertNotNull(demoService); - - value = demoService.sayName("Mercy"); - - Assertions.assertEquals("Hello,Mercy", value); - - // From Ancestor - - demoService = child.getDemoServiceFromAncestor(); - - Assertions.assertNotNull(demoService); - - value = demoService.sayName("Mercy"); - - Assertions.assertEquals("Hello,Mercy", value); - - providerContext.close(); - consumerContext.close(); - - - } - - -} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/DubboConfigBindingRegistrarTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/DubboConfigBindingRegistrarTest.java deleted file mode 100644 index 48c06d4b47d..00000000000 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/DubboConfigBindingRegistrarTest.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.context.context.annotation; - -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.config.spring.context.annotation.DubboConfigBindingRegistrar; -import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfigBinding; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.PropertySource; - -/** - * {@link DubboConfigBindingRegistrar} - * - * @since 2.5.8 - */ -public class DubboConfigBindingRegistrarTest { - - @Test - public void testRegisterBeanDefinitionsForSingle() { - - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - - context.register(TestApplicationConfig.class); - - context.refresh(); - - ApplicationConfig applicationConfig = context.getBean("applicationBean", ApplicationConfig.class); - - Assertions.assertEquals("dubbo-demo-application", applicationConfig.getName()); - - - } - - @Test - public void testRegisterBeanDefinitionsForMultiple() { - - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - - context.register(TestMultipleApplicationConfig.class); - - context.refresh(); - - ApplicationConfig applicationConfig = context.getBean("applicationBean", ApplicationConfig.class); - - Assertions.assertEquals("dubbo-demo-application", applicationConfig.getName()); - - applicationConfig = context.getBean("applicationBean2", ApplicationConfig.class); - - Assertions.assertEquals("dubbo-demo-application2", applicationConfig.getName()); - - applicationConfig = context.getBean("applicationBean3", ApplicationConfig.class); - - Assertions.assertEquals("dubbo-demo-application3", applicationConfig.getName()); - - - } - - @EnableDubboConfigBinding(prefix = "${application.prefixes}", type = ApplicationConfig.class, multiple = true) - @PropertySource("META-INF/config.properties") - private static class TestMultipleApplicationConfig { - - } - - @EnableDubboConfigBinding(prefix = "${application.prefix}", type = ApplicationConfig.class) - @PropertySource("META-INF/config.properties") - private static class TestApplicationConfig { - - } - - -} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/DubboConfigBindingsRegistrarTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/DubboConfigBindingsRegistrarTest.java deleted file mode 100644 index f09782913d3..00000000000 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/DubboConfigBindingsRegistrarTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.context.context.annotation; - -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.config.spring.context.annotation.DubboConfigBindingsRegistrar; -import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfigBinding; -import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfigBindings; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.PropertySource; - -/** - * {@link DubboConfigBindingsRegistrar} Test - * - * @since DubboConfigBindingsRegistrar - */ -public class DubboConfigBindingsRegistrarTest { - - @Test - public void test() { - - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - - context.register(TestConfig.class); - - context.refresh(); - - ApplicationConfig applicationConfig = context.getBean("applicationBean", ApplicationConfig.class); - - Assertions.assertEquals("dubbo-demo-application", applicationConfig.getName()); - - Assertions.assertEquals(2, context.getBeansOfType(ApplicationConfig.class).size()); - - } - - - @EnableDubboConfigBindings({ - @EnableDubboConfigBinding(prefix = "${application.prefix}", type = ApplicationConfig.class), - @EnableDubboConfigBinding(prefix = "dubbo.applications.applicationBean", type = ApplicationConfig.class) - }) - @PropertySource("META-INF/config.properties") - private static class TestConfig { - - } - -} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/DubboConfigConfigurationTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/DubboConfigConfigurationTest.java deleted file mode 100644 index d4f28aae301..00000000000 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/DubboConfigConfigurationTest.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.context.context.annotation; - -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.config.ModuleConfig; -import org.apache.dubbo.config.ProtocolConfig; -import org.apache.dubbo.config.RegistryConfig; -import org.apache.dubbo.config.spring.context.annotation.DubboConfigConfiguration; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.core.io.support.ResourcePropertySource; - -import java.io.IOException; - -/** - * {@link DubboConfigConfiguration} Test - * - * @since 2.5.8 - */ -public class DubboConfigConfigurationTest { - - private AnnotationConfigApplicationContext context; - - @BeforeEach - public void before() throws IOException { - - context = new AnnotationConfigApplicationContext(); - ResourcePropertySource propertySource = new ResourcePropertySource("META-INF/config.properties"); - context.getEnvironment().getPropertySources().addFirst(propertySource); - - } - - @AfterEach - public void after() { - context.close(); - } - - @Test - public void testSingle() throws IOException { - - context.register(DubboConfigConfiguration.Single.class); - context.refresh(); - - // application - ApplicationConfig applicationConfig = context.getBean("applicationBean", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application", applicationConfig.getName()); - - // module - ModuleConfig moduleConfig = context.getBean("moduleBean", ModuleConfig.class); - Assertions.assertEquals("dubbo-demo-module", moduleConfig.getName()); - - // registry - RegistryConfig registryConfig = context.getBean(RegistryConfig.class); - Assertions.assertEquals("zookeeper://192.168.99.100:32770", registryConfig.getAddress()); - - // protocol - ProtocolConfig protocolConfig = context.getBean(ProtocolConfig.class); - Assertions.assertEquals("dubbo", protocolConfig.getName()); - Assertions.assertEquals(Integer.valueOf(20880), protocolConfig.getPort()); - } - - @Test - public void testMultiple() { - - context.register(DubboConfigConfiguration.Multiple.class); - context.refresh(); - - // application - ApplicationConfig applicationConfig = context.getBean("applicationBean", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application", applicationConfig.getName()); - - ApplicationConfig applicationBean2 = context.getBean("applicationBean2", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application2", applicationBean2.getName()); - - ApplicationConfig applicationBean3 = context.getBean("applicationBean3", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application3", applicationBean3.getName()); - - } - -} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/EnableDubboConfigTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/EnableDubboConfigTest.java deleted file mode 100644 index fba874332b3..00000000000 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/EnableDubboConfigTest.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.context.context.annotation; - -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.config.ConsumerConfig; -import org.apache.dubbo.config.ModuleConfig; -import org.apache.dubbo.config.MonitorConfig; -import org.apache.dubbo.config.ProtocolConfig; -import org.apache.dubbo.config.ProviderConfig; -import org.apache.dubbo.config.RegistryConfig; -import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.PropertySource; - -/** - * {@link EnableDubboConfig} Test - * - * @since 2.5.8 - */ -public class EnableDubboConfigTest { - - @Test - public void testSingle() { - - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - context.register(TestConfig.class); - context.refresh(); - - // application - ApplicationConfig applicationConfig = context.getBean("applicationBean", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application", applicationConfig.getName()); - - // module - ModuleConfig moduleConfig = context.getBean("moduleBean", ModuleConfig.class); - Assertions.assertEquals("dubbo-demo-module", moduleConfig.getName()); - - // registry - RegistryConfig registryConfig = context.getBean(RegistryConfig.class); - Assertions.assertEquals("zookeeper://192.168.99.100:32770", registryConfig.getAddress()); - - // protocol - ProtocolConfig protocolConfig = context.getBean(ProtocolConfig.class); - Assertions.assertEquals("dubbo", protocolConfig.getName()); - Assertions.assertEquals(Integer.valueOf(20880), protocolConfig.getPort()); - - // monitor - MonitorConfig monitorConfig = context.getBean(MonitorConfig.class); - Assertions.assertEquals("zookeeper://127.0.0.1:32770", monitorConfig.getAddress()); - - // provider - ProviderConfig providerConfig = context.getBean(ProviderConfig.class); - Assertions.assertEquals("127.0.0.1", providerConfig.getHost()); - - - // consumer - ConsumerConfig consumerConfig = context.getBean(ConsumerConfig.class); - Assertions.assertEquals("netty", consumerConfig.getClient()); - - } - - @Test - public void testMultiple() { - - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - context.register(TestMultipleConfig.class); - context.refresh(); - - // application - ApplicationConfig applicationConfig = context.getBean("applicationBean", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application", applicationConfig.getName()); - - ApplicationConfig applicationBean2 = context.getBean("applicationBean2", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application2", applicationBean2.getName()); - - ApplicationConfig applicationBean3 = context.getBean("applicationBean3", ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application3", applicationBean3.getName()); - - } - - @EnableDubboConfig(multiple = true) - @PropertySource("META-INF/config.properties") - private static class TestMultipleConfig { - - } - - @EnableDubboConfig - @PropertySource("META-INF/config.properties") - private static class TestConfig { - - } -} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/EnableDubboTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/EnableDubboTest.java deleted file mode 100644 index 1366a35d252..00000000000 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/EnableDubboTest.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.context.context.annotation; - -import org.apache.dubbo.config.RegistryConfig; -import org.apache.dubbo.config.spring.api.DemoService; -import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; -import org.apache.dubbo.config.spring.context.annotation.consumer.test.TestConsumerConfiguration; -import org.apache.dubbo.config.spring.context.annotation.provider.DemoServiceImpl; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.aop.support.AopUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Primary; -import org.springframework.context.annotation.PropertySource; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.transaction.PlatformTransactionManager; -import org.springframework.transaction.TransactionDefinition; -import org.springframework.transaction.TransactionException; -import org.springframework.transaction.TransactionStatus; -import org.springframework.transaction.annotation.EnableTransactionManagement; -import org.springframework.transaction.annotation.Transactional; - -import static org.springframework.core.annotation.AnnotationUtils.findAnnotation; - -/** - * {@link EnableDubbo} Test - * - * @since 2.5.8 - */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = {EnableDubboTest.class}) -@TestPropertySource(locations = "classpath:/META-INF/dubbb-provider.properties", - properties = "demo.service.version = 2.5.7") -@EnableDubbo(scanBasePackages = "org.apache.dubbo.config.spring.context.annotation.provider") -@ComponentScan(basePackages = "org.apache.dubbo.config.spring.context.annotation.provider") -@EnableTransactionManagement -public class EnableDubboTest { - - @Autowired - private ApplicationContext providerContext; - - @Test - public void test() { - - DemoService demoService = providerContext.getBean(DemoService.class); - - String value = demoService.sayName("Mercy"); - - Assert.assertEquals("Hello,Mercy", value); - - Class beanClass = AopUtils.getTargetClass(demoService); - - // DemoServiceImpl with @Transactional - Assert.assertEquals(DemoServiceImpl.class, beanClass); - - // Test @Transactional is present or not - Assert.assertNotNull(findAnnotation(beanClass, Transactional.class)); - - AnnotationConfigApplicationContext consumerContext = new AnnotationConfigApplicationContext(TestConsumerConfiguration.class); - - TestConsumerConfiguration consumerConfiguration = consumerContext.getBean(TestConsumerConfiguration.class); - - demoService = consumerConfiguration.getDemoService(); - - value = demoService.sayName("Mercy"); - - Assert.assertEquals("Hello,Mercy", value); - - TestConsumerConfiguration.Child child = consumerContext.getBean(TestConsumerConfiguration.Child.class); - - // From Child - - demoService = child.getDemoServiceFromChild(); - - Assert.assertNotNull(demoService); - - value = demoService.sayName("Mercy"); - - Assert.assertEquals("Hello,Mercy", value); - - // From Parent - - demoService = child.getDemoServiceFromParent(); - - Assert.assertNotNull(demoService); - - value = demoService.sayName("Mercy"); - - Assert.assertEquals("Hello,Mercy", value); - - // From Ancestor - - demoService = child.getDemoServiceFromAncestor(); - - Assert.assertNotNull(demoService); - - value = demoService.sayName("Mercy"); - - Assert.assertEquals("Hello,Mercy", value); - - // Test my-registry2 bean presentation - RegistryConfig registryConfig = consumerContext.getBean("my-registry2", RegistryConfig.class); - - // Test multiple binding - Assert.assertEquals("N/A", registryConfig.getAddress()); - - } - - @EnableDubbo(scanBasePackages = "org.apache.dubbo.config.spring.context.annotation.provider") - @ComponentScan(basePackages = "org.apache.dubbo.config.spring.context.annotation.provider") - @PropertySource("classpath:/META-INF/dubbb-provider.properties") - @EnableTransactionManagement - public static class TestProviderConfiguration { - - @Primary - @Bean - public PlatformTransactionManager platformTransactionManager() { - return new PlatformTransactionManager() { - - @Override - public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException { - return null; - } - - @Override - public void commit(TransactionStatus status) throws TransactionException { - - } - - @Override - public void rollback(TransactionStatus status) throws TransactionException { - - } - }; - } - } - - -} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/consumer/ConsumerConfiguration.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/consumer/ConsumerConfiguration.java deleted file mode 100644 index 04866986197..00000000000 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/consumer/ConsumerConfiguration.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.context.context.annotation.consumer; - -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.config.RegistryConfig; -import org.apache.dubbo.config.annotation.Reference; -import org.apache.dubbo.config.spring.api.DemoService; -import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; - -@Configuration("consumerConfiguration") -@DubboComponentScan( - basePackageClasses = ConsumerConfiguration.class -) -@PropertySource("META-INF/default.properties") -public class ConsumerConfiguration { - - /** - * Current application configuration, to replace XML config: - * - * <dubbo:application name="dubbo-demo-application"/> - * - * - * @return {@link ApplicationConfig} Bean - */ - @Bean("dubbo-demo-application") - public ApplicationConfig applicationConfig() { - ApplicationConfig applicationConfig = new ApplicationConfig(); - applicationConfig.setName("dubbo-demo-application"); - return applicationConfig; - } - - /** - * Current registry center configuration, to replace XML config: - * - * <dubbo:registry address="N/A"/> - * - * - * @return {@link RegistryConfig} Bean - */ - @Bean - public RegistryConfig registryConfig() { - RegistryConfig registryConfig = new RegistryConfig(); - registryConfig.setAddress("N/A"); - return registryConfig; - } - - @Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345") - private DemoService demoService; - - public DemoService getDemoService() { - return demoService; - } - - public void setDemoService(DemoService demoService) { - this.demoService = demoService; - } - - - @Bean - public Child c() { - return new Child(); - } - - public static abstract class Ancestor { - - @Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345") - private DemoService demoServiceFromAncestor; - - public DemoService getDemoServiceFromAncestor() { - return demoServiceFromAncestor; - } - - public void setDemoServiceFromAncestor(DemoService demoServiceFromAncestor) { - this.demoServiceFromAncestor = demoServiceFromAncestor; - } - } - - public static abstract class Parent extends Ancestor { - - private DemoService demoServiceFromParent; - - public DemoService getDemoServiceFromParent() { - return demoServiceFromParent; - } - - @Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345") - public void setDemoServiceFromParent(DemoService demoServiceFromParent) { - this.demoServiceFromParent = demoServiceFromParent; - } - - } - - public static class Child extends Parent { - - @Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345") - private DemoService demoServiceFromChild; - - public DemoService getDemoServiceFromChild() { - return demoServiceFromChild; - } - - public void setDemoServiceFromChild(DemoService demoServiceFromChild) { - this.demoServiceFromChild = demoServiceFromChild; - } - } - -} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/consumer/test/TestConsumerConfiguration.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/consumer/test/TestConsumerConfiguration.java deleted file mode 100644 index dc41c614cd8..00000000000 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/consumer/test/TestConsumerConfiguration.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.context.context.annotation.consumer.test; - -import org.apache.dubbo.config.annotation.Reference; -import org.apache.dubbo.config.spring.api.DemoService; -import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.PropertySource; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -/** - * Test Consumer Configuration - * - * @since 2.5.7 - */ -@EnableDubbo(scanBasePackageClasses = TestConsumerConfiguration.class, multipleConfig = true) -@PropertySource("META-INF/dubbb-consumer.properties") -@EnableTransactionManagement -public class TestConsumerConfiguration { - - @Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345", application = "dubbo-demo-application") - private DemoService demoService; - - public DemoService getDemoService() { - return demoService; - } - - public void setDemoService(DemoService demoService) { - this.demoService = demoService; - } - - - @Bean - public TestConsumerConfiguration.Child c() { - return new TestConsumerConfiguration.Child(); - } - - public static abstract class Ancestor { - - @Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345", application = "dubbo-demo-application") - private DemoService demoServiceFromAncestor; - - public DemoService getDemoServiceFromAncestor() { - return demoServiceFromAncestor; - } - - public void setDemoServiceFromAncestor(DemoService demoServiceFromAncestor) { - this.demoServiceFromAncestor = demoServiceFromAncestor; - } - } - - public static abstract class Parent extends TestConsumerConfiguration.Ancestor { - - private DemoService demoServiceFromParent; - - public DemoService getDemoServiceFromParent() { - return demoServiceFromParent; - } - - @Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345", application = "dubbo-demo-application") - public void setDemoServiceFromParent(DemoService demoServiceFromParent) { - this.demoServiceFromParent = demoServiceFromParent; - } - - } - - public static class Child extends TestConsumerConfiguration.Parent { - - @Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345", application = "dubbo-demo-application") - private DemoService demoServiceFromChild; - - public DemoService getDemoServiceFromChild() { - return demoServiceFromChild; - } - - public void setDemoServiceFromChild(DemoService demoServiceFromChild) { - this.demoServiceFromChild = demoServiceFromChild; - } - } -} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/provider/DemoServiceImpl.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/provider/DemoServiceImpl.java deleted file mode 100644 index 155bd2a5539..00000000000 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/provider/DemoServiceImpl.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.context.context.annotation.provider; - -import org.apache.dubbo.config.spring.api.Box; -import org.apache.dubbo.config.spring.api.DemoService; - -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - - -/** - * {@link DemoService} Service implementation - * - * @since 2.5.8 - */ -@org.apache.dubbo.config.annotation.Service( - version = "${demo.service.version}", - application = "${demo.service.application}", - protocol = "${demo.service.protocol}", - registry = "${demo.service.registry}" -) -@Service -@Transactional -public class DemoServiceImpl implements DemoService { - - @Override - public String sayName(String name) { - return "Hello," + name; - } - - @Override - public Box getBox() { - return new Box() { - @Override - public String getName() { - return "MyBox"; - } - }; - } -} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/provider/ProviderConfiguration.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/provider/ProviderConfiguration.java deleted file mode 100644 index c77322848d6..00000000000 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/provider/ProviderConfiguration.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.context.context.annotation.provider; - -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.config.ProtocolConfig; -import org.apache.dubbo.config.RegistryConfig; -import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Primary; -import org.springframework.context.annotation.PropertySource; -import org.springframework.transaction.PlatformTransactionManager; -import org.springframework.transaction.TransactionDefinition; -import org.springframework.transaction.TransactionException; -import org.springframework.transaction.TransactionStatus; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -@DubboComponentScan(basePackages = "org.apache.dubbo.config.spring.context.annotation.provider") -@PropertySource("META-INF/default.properties") -@EnableTransactionManagement -public class ProviderConfiguration { - - /** - * Current application configuration, to replace XML config: - * - * <dubbo:application name="dubbo-demo-application"/> - * - * - * @return {@link ApplicationConfig} Bean - */ - @Bean("dubbo-demo-application") - public ApplicationConfig applicationConfig() { - ApplicationConfig applicationConfig = new ApplicationConfig(); - applicationConfig.setName("dubbo-demo-application"); - return applicationConfig; - } - - /** - * Current registry center configuration, to replace XML config: - * - * <dubbo:registry id="my-registry" address="N/A"/> - * - * - * @return {@link RegistryConfig} Bean - */ - @Bean("my-registry") - public RegistryConfig registryConfig() { - RegistryConfig registryConfig = new RegistryConfig(); - registryConfig.setAddress("N/A"); - return registryConfig; - } - - /** - * Current protocol configuration, to replace XML config: - * - * <dubbo:protocol name="dubbo" port="12345"/> - * - * - * @return {@link ProtocolConfig} Bean - */ - @Bean("dubbo") - public ProtocolConfig protocolConfig() { - ProtocolConfig protocolConfig = new ProtocolConfig(); - protocolConfig.setName("dubbo"); - protocolConfig.setPort(12345); - return protocolConfig; - } - - @Primary - @Bean - public PlatformTransactionManager platformTransactionManager() { - return new PlatformTransactionManager() { - - @Override - public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException { - return null; - } - - @Override - public void commit(TransactionStatus status) throws TransactionException { - - } - - @Override - public void rollback(TransactionStatus status) throws TransactionException { - - } - }; - } - -} - diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/properties/DefaultDubboConfigBinderTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/properties/DefaultDubboConfigBinderTest.java deleted file mode 100644 index 1caddb0b31b..00000000000 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/properties/DefaultDubboConfigBinderTest.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * 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 org.apache.dubbo.config.spring.context.context.properties; - - -import org.apache.dubbo.config.ApplicationConfig; -import org.apache.dubbo.config.ConsumerConfig; -import org.apache.dubbo.config.ProtocolConfig; -import org.apache.dubbo.config.RegistryConfig; -import org.apache.dubbo.config.spring.beans.factory.config.YamlPropertySourceFactory; -import org.apache.dubbo.config.spring.context.properties.DefaultDubboConfigBinder; -import org.apache.dubbo.config.spring.context.properties.DubboConfigBinder; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@TestPropertySource(locations = "classpath:/dubbo.properties") -@PropertySource(name = "yaml-source", value = {"classpath:/META-INF/dubbo.yml"}, factory = YamlPropertySourceFactory.class) -@Configuration -@ContextConfiguration(classes = {DefaultDubboConfigBinder.class, DefaultDubboConfigBinderTest.class}) -public class DefaultDubboConfigBinderTest { - - @Autowired - private DubboConfigBinder dubboConfigBinder; - - @Value("${dubbo.consumer.default}") - private Boolean isDefault; - - @Value("${dubbo.consumer.client}") - private String client; - - @Value("${dubbo.consumer.threadpool}") - private String threadPool; - - @Value("${dubbo.consumer.corethreads}") - private Integer coreThreads; - - @Value("${dubbo.consumer.threads}") - private Integer threads; - - @Value("${dubbo.consumer.queues}") - private Integer queues; - - @Test - public void testBinder() { - - ApplicationConfig applicationConfig = new ApplicationConfig(); - dubboConfigBinder.bind("dubbo.application", applicationConfig); - Assert.assertEquals("hello", applicationConfig.getName()); - Assert.assertEquals("world", applicationConfig.getOwner()); - - RegistryConfig registryConfig = new RegistryConfig(); - dubboConfigBinder.bind("dubbo.registry", registryConfig); - Assert.assertEquals("10.20.153.17", registryConfig.getAddress()); - - ProtocolConfig protocolConfig = new ProtocolConfig(); - dubboConfigBinder.bind("dubbo.protocol", protocolConfig); - Assert.assertEquals(Integer.valueOf(20881), protocolConfig.getPort()); - - ConsumerConfig consumerConfig = new ConsumerConfig(); - dubboConfigBinder.bind("dubbo.consumer", consumerConfig); - - Assert.assertEquals(isDefault, consumerConfig.isDefault()); - Assert.assertEquals(client, consumerConfig.getClient()); - Assert.assertEquals(threadPool, consumerConfig.getThreadpool()); - Assert.assertEquals(coreThreads, consumerConfig.getCorethreads()); - Assert.assertEquals(threads, consumerConfig.getThreads()); - Assert.assertEquals(queues, consumerConfig.getQueues()); - } -} - diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/properties/DefaultDubboConfigBinderTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/properties/DefaultDubboConfigBinderTest.java index 030fbc8bf9d..f6e741d17fc 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/properties/DefaultDubboConfigBinderTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/properties/DefaultDubboConfigBinderTest.java @@ -20,16 +20,15 @@ import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.config.ProtocolConfig; import org.apache.dubbo.config.RegistryConfig; - +import org.junit.Assert; import org.junit.Test; -import org.junit.jupiter.api.Assertions; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit4.SpringRunner; -@RunWith(SpringJUnit4ClassRunner.class) +@RunWith(SpringRunner.class) @TestPropertySource(locations = "classpath:/dubbo.properties") @ContextConfiguration(classes = DefaultDubboConfigBinder.class) public class DefaultDubboConfigBinderTest { @@ -42,16 +41,16 @@ public void testBinder() { ApplicationConfig applicationConfig = new ApplicationConfig(); dubboConfigBinder.bind("dubbo.application", applicationConfig); - Assertions.assertEquals("hello", applicationConfig.getName()); - Assertions.assertEquals("world", applicationConfig.getOwner()); + Assert.assertEquals("hello", applicationConfig.getName()); + Assert.assertEquals("world", applicationConfig.getOwner()); RegistryConfig registryConfig = new RegistryConfig(); dubboConfigBinder.bind("dubbo.registry", registryConfig); - Assertions.assertEquals("10.20.153.17", registryConfig.getAddress()); + Assert.assertEquals("10.20.153.17", registryConfig.getAddress()); ProtocolConfig protocolConfig = new ProtocolConfig(); dubboConfigBinder.bind("dubbo.protocol", protocolConfig); - Assertions.assertEquals(Integer.valueOf(20881), protocolConfig.getPort()); + Assert.assertEquals(Integer.valueOf(20881), protocolConfig.getPort()); } } diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/NewRouter.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/convert/converter/StringArrayToMapConverterTest.java similarity index 51% rename from dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/NewRouter.java rename to dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/convert/converter/StringArrayToMapConverterTest.java index 86269062e0a..58efd7ad5cf 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/NewRouter.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/convert/converter/StringArrayToMapConverterTest.java @@ -14,41 +14,39 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.rpc.cluster; +package org.apache.dubbo.config.spring.convert.converter; -import org.apache.dubbo.common.URL; -import org.apache.dubbo.rpc.Invocation; -import org.apache.dubbo.rpc.Invoker; -import org.apache.dubbo.rpc.RpcException; +import org.junit.Assert; +import org.junit.Test; -import java.util.List; +import java.util.LinkedHashMap; +import java.util.Map; /** - * + * {@link StringArrayToMapConverter} Test */ -public class NewRouter implements Router { - @Override - public URL getUrl() { - return null; - } +public class StringArrayToMapConverterTest { - @Override - public List> route(List> invokers, URL url, Invocation invocation) throws RpcException { - return null; - } + @Test + public void testConvert() { - @Override - public boolean isRuntime() { - return false; - } + StringArrayToMapConverter converter = new StringArrayToMapConverter(); - @Override - public boolean isForce() { - return false; - } + Map value = converter.convert(new String[]{"Hello", "World"}); + + Map expected = new LinkedHashMap(); + + expected.put("Hello", "World"); + + Assert.assertEquals(expected, value); + + value = converter.convert(new String[]{}); + + Assert.assertNull(value); + + value = converter.convert(null); + + Assert.assertNull(value); - @Override - public int getPriority() { - return 0; } } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/Man.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/convert/converter/StringArrayToStringConverterTest.java similarity index 57% rename from dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/Man.java rename to dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/convert/converter/StringArrayToStringConverterTest.java index 440630ada1b..83fd804f228 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/Man.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/convert/converter/StringArrayToStringConverterTest.java @@ -14,33 +14,33 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.rpc.protocol.dubbo.support; +package org.apache.dubbo.config.spring.convert.converter; - -import java.io.Serializable; +import org.junit.Assert; +import org.junit.Test; /** - * Man.java + * {@link StringArrayToStringConverter} Test */ -public class Man implements Serializable { +public class StringArrayToStringConverterTest { - private static final long serialVersionUID = 1L; - private String name; - private int age; + @Test + public void testConvert() { - public String getName() { - return name; - } + StringArrayToStringConverter converter = new StringArrayToStringConverter(); - public void setName(String name) { - this.name = name; - } + String value = converter.convert(new String[]{"Hello", "World"}); - public int getAge() { - return age; - } + Assert.assertEquals("Hello,World", value); + + value = converter.convert(new String[]{}); + + Assert.assertNull(value); + + value = converter.convert(null); + + Assert.assertNull(value); - public void setAge(int age) { - this.age = age; } -} \ No newline at end of file + +} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/extension/SpringExtensionFactoryTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/extension/SpringExtensionFactoryTest.java index 0ae08e3a038..134b50bcb58 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/extension/SpringExtensionFactoryTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/extension/SpringExtensionFactoryTest.java @@ -20,12 +20,12 @@ import org.apache.dubbo.config.spring.api.HelloService; import org.apache.dubbo.config.spring.impl.DemoServiceImpl; import org.apache.dubbo.config.spring.impl.HelloServiceImpl; -import org.apache.dubbo.rpc.Protocol; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.apache.dubbo.rpc.Protocol; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import org.springframework.beans.factory.NoUniqueBeanDefinitionException; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; @@ -38,7 +38,7 @@ public class SpringExtensionFactoryTest { private AnnotationConfigApplicationContext context1; private AnnotationConfigApplicationContext context2; - @BeforeEach + @Before public void init() { context1 = new AnnotationConfigApplicationContext(); context1.register(getClass()); @@ -53,13 +53,13 @@ public void init() { @Test public void testGetExtensionBySPI() { Protocol protocol = springExtensionFactory.getExtension(Protocol.class, "protocol"); - Assertions.assertNull(protocol); + Assert.assertNull(protocol); } @Test public void testGetExtensionByName() { DemoService bean = springExtensionFactory.getExtension(DemoService.class, "bean1"); - Assertions.assertNotNull(bean); + Assert.assertNotNull(bean); } @Test @@ -68,17 +68,17 @@ public void testGetExtensionByTypeMultiple() { springExtensionFactory.getExtension(DemoService.class, "beanname-not-exist"); } catch (Exception e) { e.printStackTrace(); - Assertions.assertTrue(e instanceof NoUniqueBeanDefinitionException); + Assert.assertTrue(e instanceof NoUniqueBeanDefinitionException); } } @Test public void testGetExtensionByType() { HelloService bean = springExtensionFactory.getExtension(HelloService.class, "beanname-not-exist"); - Assertions.assertNotNull(bean); + Assert.assertNotNull(bean); } - @AfterEach + @After public void destroy() { SpringExtensionFactory.clearContexts(); context1.close(); diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/schema/DubboNamespaceHandlerTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/schema/DubboNamespaceHandlerTest.java index d0ec9ea9487..1b7cb8b49cb 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/schema/DubboNamespaceHandlerTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/schema/DubboNamespaceHandlerTest.java @@ -27,10 +27,9 @@ import org.apache.dubbo.config.spring.api.DemoService; import org.apache.dubbo.config.spring.impl.DemoServiceImpl; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import org.springframework.beans.factory.BeanCreationException; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -39,15 +38,15 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class DubboNamespaceHandlerTest { - @BeforeEach + @Before public void setUp() { ConfigManager.getInstance().clear(); } - @AfterEach + @After public void tearDown() { ConfigManager.getInstance().clear(); } @@ -111,7 +110,7 @@ public void testCustomParameter() { @Test public void testDelayFixedTime() { - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/" + ConfigTest.class.getPackage().getName().replace('.', '/') + "/delay-fixed-time.xml"); + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/delay-fixed-time.xml"); ctx.start(); assertThat(ctx.getBean(ServiceBean.class).getDelay(), is(300)); @@ -135,20 +134,16 @@ public void testMonitor() { assertThat(ctx.getBean(MonitorConfig.class), not(nullValue())); } - @Test + @Test(expected = BeanCreationException.class) public void testMultiMonitor() { - Assertions.assertThrows(BeanCreationException.class, () -> { - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-monitor.xml"); - ctx.start(); - }); + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-monitor.xml"); + ctx.start(); } - @Test + @Test(expected = BeanCreationException.class) public void testMultiProviderConfig() { - Assertions.assertThrows(BeanCreationException.class, () -> { - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/provider-multi.xml"); - ctx.start(); - }); + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/provider-multi.xml"); + ctx.start(); } @Test @@ -160,12 +155,10 @@ public void testModuleInfo() { assertThat(moduleConfig.getName(), is("test-module")); } - @Test + @Test(expected = BeanCreationException.class) public void testNotificationWithWrongBean() { - Assertions.assertThrows(BeanCreationException.class, () -> { - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/consumer-notification.xml"); - ctx.start(); - }); + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/consumer-notification.xml"); + ctx.start(); } @Test diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/status/DataSourceStatusCheckerTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/status/DataSourceStatusCheckerTest.java index c0a2add641c..c82fd4af615 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/status/DataSourceStatusCheckerTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/status/DataSourceStatusCheckerTest.java @@ -19,9 +19,9 @@ import org.apache.dubbo.common.status.Status; import org.apache.dubbo.config.spring.ServiceBean; import org.apache.dubbo.config.spring.extension.SpringExtensionFactory; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import org.mockito.Answers; import org.mockito.Mock; import org.mockito.Mockito; @@ -34,7 +34,7 @@ import java.util.Map; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.BDDMockito.given; @@ -47,7 +47,7 @@ public class DataSourceStatusCheckerTest { @Mock private ApplicationContext applicationContext; - @BeforeEach + @Before public void setUp() throws Exception { SpringExtensionFactory.clearContexts(); initMocks(this); @@ -55,7 +55,7 @@ public void setUp() throws Exception { new ServiceBean().setApplicationContext(applicationContext); } - @AfterEach + @After public void tearDown() throws Exception { Mockito.reset(applicationContext); } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/status/SpringStatusCheckerTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/status/SpringStatusCheckerTest.java index f3be445103c..66155e17135 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/status/SpringStatusCheckerTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/status/SpringStatusCheckerTest.java @@ -19,18 +19,16 @@ import org.apache.dubbo.common.status.Status; import org.apache.dubbo.config.spring.ServiceBean; import org.apache.dubbo.config.spring.extension.SpringExtensionFactory; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import org.mockito.Mock; import org.mockito.Mockito; import org.springframework.context.ApplicationContext; import org.springframework.context.Lifecycle; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; import static org.mockito.MockitoAnnotations.initMocks; @@ -41,14 +39,14 @@ public class SpringStatusCheckerTest { @Mock private ApplicationContext applicationContext; - @BeforeEach + @Before public void setUp() throws Exception { initMocks(this); this.springStatusChecker = new SpringStatusChecker(); new ServiceBean().setApplicationContext(applicationContext); } - @AfterEach + @After public void tearDown() throws Exception { SpringExtensionFactory.clearContexts(); Mockito.reset(applicationContext); diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/BeanFactoryUtilsTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/BeanFactoryUtilsTest.java index a445ae9dd88..c33e9f3f147 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/BeanFactoryUtilsTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/BeanFactoryUtilsTest.java @@ -16,9 +16,9 @@ */ package org.apache.dubbo.config.spring.util; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.stereotype.Component; @@ -33,7 +33,7 @@ public class BeanFactoryUtilsTest { private AnnotationConfigApplicationContext applicationContext; - @BeforeEach + @Before public void init() { applicationContext = new AnnotationConfigApplicationContext(); } @@ -47,9 +47,9 @@ public void testGetOptionalBean() { TestBean testBean = BeanFactoryUtils.getOptionalBean(applicationContext, "testBean", TestBean.class); - Assertions.assertNotNull(testBean); + Assert.assertNotNull(testBean); - Assertions.assertEquals("Hello,World", testBean.getName()); + Assert.assertEquals("Hello,World", testBean.getName()); } @@ -60,7 +60,7 @@ public void testGetOptionalBeanIfAbsent() { TestBean testBean = BeanFactoryUtils.getOptionalBean(applicationContext, "testBean", TestBean.class); - Assertions.assertNull(testBean); + Assert.assertNull(testBean); } @Test @@ -72,9 +72,9 @@ public void testGetBeans() { List testBeans = BeanFactoryUtils.getBeans(applicationContext, new String[]{"testBean"}, TestBean.class); - Assertions.assertEquals(1, testBeans.size()); + Assert.assertEquals(1, testBeans.size()); - Assertions.assertEquals("Hello,World", testBeans.get(0).getName()); + Assert.assertEquals("Hello,World", testBeans.get(0).getName()); } @@ -85,7 +85,7 @@ public void testGetBeansIfAbsent() { List testBeans = BeanFactoryUtils.getBeans(applicationContext, new String[]{"testBean"}, TestBean.class); - Assertions.assertTrue(testBeans.isEmpty()); + Assert.assertTrue(testBeans.isEmpty()); } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/PropertySourcesUtilsTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/PropertySourcesUtilsTest.java index 9e8c29f963a..40481317aca 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/PropertySourcesUtilsTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/PropertySourcesUtilsTest.java @@ -16,8 +16,8 @@ */ package org.apache.dubbo.config.spring.util; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MutablePropertySources; @@ -39,40 +39,45 @@ public void testGetSubProperties() { MutablePropertySources propertySources = new MutablePropertySources(); Map source = new HashMap(); - Map source2 = new HashMap(); - MapPropertySource propertySource = new MapPropertySource("propertySource", source); - MapPropertySource propertySource2 = new MapPropertySource("propertySource2", source2); + MapPropertySource propertySource = new MapPropertySource("test", source); - propertySources.addLast(propertySource); - propertySources.addLast(propertySource2); + propertySources.addFirst(propertySource); - Map result = PropertySourcesUtils.getSubProperties(propertySources, "user"); + String KEY_PREFIX = "user"; + String KEY_NAME = "name"; + String KEY_AGE = "age"; + Map result = PropertySourcesUtils.getSubProperties(propertySources, KEY_PREFIX); - Assertions.assertEquals(Collections.emptyMap(), result); + Assert.assertEquals(Collections.emptyMap(), result); - source.put("age", "31"); - source.put("user.name", "Mercy"); - source.put("user.age", "${age}"); - - source2.put("user.name", "mercyblitz"); - source2.put("user.age", "32"); + source.put(KEY_PREFIX + "." + KEY_NAME, "Mercy"); + source.put(KEY_PREFIX + "." + KEY_AGE, 31); Map expected = new HashMap(); - expected.put("name", "Mercy"); - expected.put("age", "31"); + expected.put(KEY_NAME, "Mercy"); + expected.put(KEY_AGE, "31"); - result = PropertySourcesUtils.getSubProperties(propertySources, "user"); - Assertions.assertEquals(expected, result); + result = PropertySourcesUtils.getSubProperties(propertySources, KEY_PREFIX); + Assert.assertEquals(expected, result); result = PropertySourcesUtils.getSubProperties(propertySources, ""); - Assertions.assertEquals(Collections.emptyMap(), result); + Assert.assertEquals(Collections.emptyMap(), result); result = PropertySourcesUtils.getSubProperties(propertySources, "no-exists"); - Assertions.assertEquals(Collections.emptyMap(), result); + Assert.assertEquals(Collections.emptyMap(), result); + + source.put(KEY_PREFIX + ".app.name", "${info.name}"); + source.put("info.name", "Hello app"); + + result = PropertySourcesUtils.getSubProperties(propertySources, KEY_PREFIX); + + String appName = result.get("app.name"); + + Assert.assertEquals("Hello app", appName); } -} \ No newline at end of file +} diff --git a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/default.properties b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/default.properties index 101030bba99..c717e1b4e5d 100644 --- a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/default.properties +++ b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/default.properties @@ -1,4 +1,4 @@ demo.service.version = 2.5.7 -demo.service.application = dubbo-demo-application +demo.service.application = dubbo-annotation-test demo.service.protocol = dubbo demo.service.registry = my-registry diff --git a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbb-consumer.properties b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbb-consumer.properties deleted file mode 100644 index 4583323bbd7..00000000000 --- a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbb-consumer.properties +++ /dev/null @@ -1,12 +0,0 @@ -# Dubbo Consumer Properties as an alternative for -# Spring XML Bean definition : META-INF/spring/dubbo-annotation-consumer.xml -demo.service.application = dubbo-demo-application -demo.service.registry = my-registry - -## Dubbo configs binding properties -### -dubbo.applications.dubbo-demo-application.name = dubbo-demo-application - -### -dubbo.registries.my-registry.address = N/A -dubbo.registries.my-registry2.address = N/A \ No newline at end of file diff --git a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbb-provider.properties b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbb-provider.properties deleted file mode 100644 index f2e20c4df97..00000000000 --- a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbb-provider.properties +++ /dev/null @@ -1,24 +0,0 @@ -# Dubbo Provider Properties as an alternative for -# Spring XML Bean definition : META-INF/spring/dubbo-annotation-provider.xml - -## Service Providers' Placeholders for org.apache.dubbo.config.spring.context.annotation.provider.DemoServiceImpl - -demo.service.application = dubbo-demo-application -demo.service.protocol = dubbo -demo.service.registry = my-registry - - -## Dubbo configs binding properties - -### -dubbo.application.id = dubbo-demo-application -dubbo.application.name = dubbo-demo-application - -### -dubbo.registry.id = my-registry -dubbo.registry.address = N/A - -### -dubbo.protocol.id = dubbo -dubbo.protocol.name = dubbo -dubbo.protocol.port = 12345 \ No newline at end of file diff --git a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbo-consumer.properties b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbo-consumer.properties index 094b709165e..6f735a28148 100644 --- a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbo-consumer.properties +++ b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbo-consumer.properties @@ -4,9 +4,9 @@ demo.service.application = dubbo-annotation-test demo.service.registry = my-registry ## Dubbo configs binding properties -### +### # In this UT, the provider will be responsible of loading ApplicationConfig. -dubbo.applications.dubbo-demo-application.name = dubbo-demo-application +dubbo.applications.dubbo-annotation-consumer.name = dubbo-annotation-test ### dubbo.registries.my-registry.address = N/A diff --git a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbo-provider.properties b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbo-provider.properties index b676caa9fd7..a2a3141962b 100644 --- a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbo-provider.properties +++ b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbo-provider.properties @@ -3,16 +3,16 @@ ## Service Providers' Placeholders for org.apache.dubbo.config.spring.context.annotation.provider.DemoServiceImpl -demo.service.application = dubbo-demo-application +demo.service.application = dubbo-annotation-test demo.service.protocol = dubbo demo.service.registry = my-registry ## Dubbo configs binding properties -### -dubbo.application.id = dubbo-demo-application -dubbo.application.name = dubbo-demo-application +### +dubbo.application.id = dubbo-annotation-test +dubbo.application.name = dubbo-annotation-test ### dubbo.registry.id = my-registry diff --git a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbo.yml b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbo.yml deleted file mode 100644 index ed0f0ddbc4c..00000000000 --- a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbo.yml +++ /dev/null @@ -1,8 +0,0 @@ -dubbo: - consumer: - default: false - client: netty - threadpool: cached - corethreads: 1 - threads: 10 - queues: 99 \ No newline at end of file diff --git a/dubbo-config/pom.xml b/dubbo-config/pom.xml index d1f16c146de..9a75eea5cdd 100644 --- a/dubbo-config/pom.xml +++ b/dubbo-config/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-parent - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-config pom diff --git a/dubbo-configcenter/dubbo-configcenter-api/pom.xml b/dubbo-configcenter/dubbo-configcenter-api/pom.xml index 7f2715e5b66..68b51ff7dd0 100644 --- a/dubbo-configcenter/dubbo-configcenter-api/pom.xml +++ b/dubbo-configcenter/dubbo-configcenter-api/pom.xml @@ -14,12 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. --> - + 4.0.0 org.apache.dubbo dubbo-configcenter - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-configcenter-api jar diff --git a/dubbo-configcenter/dubbo-configcenter-api/src/main/java/org/apache/dubbo/configcenter/ConfigChangeEvent.java b/dubbo-configcenter/dubbo-configcenter-api/src/main/java/org/apache/dubbo/configcenter/ConfigChangeEvent.java index cdedd15e34e..950227595a6 100644 --- a/dubbo-configcenter/dubbo-configcenter-api/src/main/java/org/apache/dubbo/configcenter/ConfigChangeEvent.java +++ b/dubbo-configcenter/dubbo-configcenter-api/src/main/java/org/apache/dubbo/configcenter/ConfigChangeEvent.java @@ -17,15 +17,15 @@ package org.apache.dubbo.configcenter; /** - * Config change event, immutable. + * Config change event. * * @see ConfigChangeType */ public class ConfigChangeEvent { - private final String key; + private String key; - private final String value; - private final ConfigChangeType changeType; + private String value; + private ConfigChangeType changeType; public ConfigChangeEvent(String key, String value) { this(key, value, ConfigChangeType.MODIFIED); @@ -41,20 +41,23 @@ public String getKey() { return key; } + public void setKey(String key) { + this.key = key; + } + public String getValue() { return value; } + public void setValue(String value) { + this.value = value; + } + public ConfigChangeType getChangeType() { return changeType; } - @Override - public String toString() { - return "ConfigChangeEvent{" + - "key='" + key + '\'' + - ", value='" + value + '\'' + - ", changeType=" + changeType + - '}'; + public void setChangeType(ConfigChangeType changeType) { + this.changeType = changeType; } } diff --git a/dubbo-configcenter/dubbo-configcenter-api/src/test/java/org/apache/dubbo/configcenter/mock/AbstractDynamicConfigurationTest.java b/dubbo-configcenter/dubbo-configcenter-api/src/test/java/org/apache/dubbo/configcenter/mock/AbstractDynamicConfigurationTest.java index a5f669a2715..7d187e82a46 100644 --- a/dubbo-configcenter/dubbo-configcenter-api/src/test/java/org/apache/dubbo/configcenter/mock/AbstractDynamicConfigurationTest.java +++ b/dubbo-configcenter/dubbo-configcenter-api/src/test/java/org/apache/dubbo/configcenter/mock/AbstractDynamicConfigurationTest.java @@ -22,8 +22,8 @@ import org.apache.dubbo.configcenter.DynamicConfigurationFactory; import org.apache.dubbo.configcenter.support.nop.NopDynamicConfigurationFactory; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; /** * @@ -36,12 +36,12 @@ public class AbstractDynamicConfigurationTest { public void testInit() { DynamicConfiguration configuration1 = configurationFactory.getDynamicConfiguration(url); DynamicConfiguration configuration2 = configurationFactory.getDynamicConfiguration(url); - Assertions.assertEquals(configuration1, configuration2); + Assert.assertEquals(configuration1, configuration2); } @Test public void testDefaultExtension() { DynamicConfigurationFactory factory = ExtensionLoader.getExtensionLoader(DynamicConfigurationFactory.class).getDefaultExtension(); - Assertions.assertTrue(factory instanceof NopDynamicConfigurationFactory); + Assert.assertTrue(factory instanceof NopDynamicConfigurationFactory); } } diff --git a/dubbo-configcenter/dubbo-configcenter-apollo/pom.xml b/dubbo-configcenter/dubbo-configcenter-apollo/pom.xml index 040b0eb33a2..18a9fbe870b 100644 --- a/dubbo-configcenter/dubbo-configcenter-apollo/pom.xml +++ b/dubbo-configcenter/dubbo-configcenter-apollo/pom.xml @@ -14,12 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. --> - + 4.0.0 org.apache.dubbo dubbo-configcenter - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-configcenter-apollo jar diff --git a/dubbo-configcenter/dubbo-configcenter-apollo/src/main/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfiguration.java b/dubbo-configcenter/dubbo-configcenter-apollo/src/main/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfiguration.java index ebce3031d73..c73dcd75f90 100644 --- a/dubbo-configcenter/dubbo-configcenter-apollo/src/main/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfiguration.java +++ b/dubbo-configcenter/dubbo-configcenter-apollo/src/main/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfiguration.java @@ -33,13 +33,10 @@ import com.ctrip.framework.apollo.enums.PropertyChangeType; import com.ctrip.framework.apollo.model.ConfigChange; -import java.util.Arrays; -import java.util.Collections; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.CopyOnWriteArraySet; -import java.util.stream.Collectors; /** * Apollo implementation, https://github.com/ctripcorp/apollo @@ -49,7 +46,6 @@ public class ApolloDynamicConfiguration implements DynamicConfiguration { private static final String APOLLO_ENV_KEY = "env"; private static final String APOLLO_ADDR_KEY = "apollo.meta"; private static final String APOLLO_CLUSTER_KEY = "apollo.cluster"; - private static final String APOLLO_PROTOCOL_PREFIX = "http://"; private URL url; private Config dubboConfig; @@ -59,12 +55,12 @@ public class ApolloDynamicConfiguration implements DynamicConfiguration { this.url = url; // Instead of using Dubbo's configuration, I would suggest use the original configuration method Apollo provides. String configEnv = url.getParameter(APOLLO_ENV_KEY); - String configAddr = getAddressWithProtocolPrefix(url); + String configAddr = url.getBackupAddress(); String configCluster = url.getParameter(Constants.CONFIG_CLUSTER_KEY); if (configEnv != null) { System.setProperty(APOLLO_ENV_KEY, configEnv); } - if (StringUtils.isEmpty(System.getProperty(APOLLO_ENV_KEY)) && !Constants.ANYHOST_VALUE.equals(configAddr)) { + if (StringUtils.isEmpty(configEnv) && !Constants.ANYHOST_VALUE.equals(configAddr)) { System.setProperty(APOLLO_ADDR_KEY, configAddr); } if (configCluster != null) { @@ -86,21 +82,6 @@ public class ApolloDynamicConfiguration implements DynamicConfiguration { } } - private String getAddressWithProtocolPrefix (URL url) { - String address = url.getBackupAddress(); - if (StringUtils.isNotEmpty(address)) { - address = Arrays.stream(Constants.COMMA_SPLIT_PATTERN.split(address)) - .map(addr -> { - if (addr.startsWith(APOLLO_PROTOCOL_PREFIX)) { - return addr; - } - return APOLLO_PROTOCOL_PREFIX + addr; - }) - .collect(Collectors.joining(",")); - } - return address; - } - /** * Since all governance rules will lay under dubbo group, this method now always uses the default dubboConfig and * ignores the group parameter. @@ -109,7 +90,7 @@ private String getAddressWithProtocolPrefix (URL url) { public void addListener(String key, String group, ConfigurationListener listener) { ApolloListener apolloListener = listeners.computeIfAbsent(group + key, k -> createTargetListener(key, group)); apolloListener.addListener(listener); - dubboConfig.addChangeListener(apolloListener, Collections.singleton(key)); + dubboConfig.addChangeListener(apolloListener); } @Override @@ -131,8 +112,11 @@ public void removeListener(String key, String group, ConfigurationListener liste @Override public String getConfig(String key, String group, long timeout) throws IllegalStateException { if (StringUtils.isNotEmpty(group) && !url.getParameter(Constants.CONFIG_GROUP_KEY, DEFAULT_GROUP).equals(group)) { - Config config = ConfigService.getAppConfig(); - return config.getProperty(key, null); + Config config = ConfigService.getConfig(group); + if (config != null) { + return config.getProperty(key, null); + } + return null; } return dubboConfig.getProperty(key, null); } @@ -176,8 +160,11 @@ public void onChange(com.ctrip.framework.apollo.model.ConfigChangeEvent changeEv return; } - ConfigChangeEvent event = new ConfigChangeEvent(key, change.getNewValue(), getChangeType(change)); - listeners.forEach(listener -> listener.process(event)); + listeners.forEach(listener -> { + ConfigChangeEvent event = new ConfigChangeEvent(key, change.getNewValue(), getChangeType(change)); + listener.process(event); + } + ); } } diff --git a/dubbo-configcenter/dubbo-configcenter-consul/pom.xml b/dubbo-configcenter/dubbo-configcenter-consul/pom.xml deleted file mode 100644 index 2a7580e837e..00000000000 --- a/dubbo-configcenter/dubbo-configcenter-consul/pom.xml +++ /dev/null @@ -1,44 +0,0 @@ - - - - - - dubbo-configcenter - org.apache.dubbo - 2.7.1-SNAPSHOT - - 4.0.0 - - dubbo-configcenter-consul - - - - org.apache.dubbo - dubbo-configcenter-api - ${project.parent.version} - - - com.ecwid.consul - consul-api - - - - - diff --git a/dubbo-configcenter/dubbo-configcenter-consul/src/main/java/org/apache/dubbo/configcenter/consul/ConsulDynamicConfiguration.java b/dubbo-configcenter/dubbo-configcenter-consul/src/main/java/org/apache/dubbo/configcenter/consul/ConsulDynamicConfiguration.java deleted file mode 100644 index 5ef1985e96f..00000000000 --- a/dubbo-configcenter/dubbo-configcenter-consul/src/main/java/org/apache/dubbo/configcenter/consul/ConsulDynamicConfiguration.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * 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 org.apache.dubbo.configcenter.consul; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.NamedThreadFactory; -import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.configcenter.ConfigChangeEvent; -import org.apache.dubbo.configcenter.ConfigurationListener; -import org.apache.dubbo.configcenter.DynamicConfiguration; - -import com.ecwid.consul.v1.ConsulClient; -import com.ecwid.consul.v1.QueryParams; -import com.ecwid.consul.v1.Response; -import com.ecwid.consul.v1.kv.model.GetValue; - -import java.util.HashSet; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.ExecutorService; - -import static java.util.concurrent.Executors.newCachedThreadPool; -import static org.apache.dubbo.common.Constants.CONFIG_NAMESPACE_KEY; -import static org.apache.dubbo.common.Constants.PATH_SEPARATOR; - -/** - * config center implementation for consul - */ -public class ConsulDynamicConfiguration implements DynamicConfiguration { - private static final Logger logger = LoggerFactory.getLogger(ConsulDynamicConfiguration.class); - - private static final int DEFAULT_PORT = 8500; - private static final int DEFAULT_WATCH_TIMEOUT = 60 * 1000; - private static final String WATCH_TIMEOUT = "consul-watch-timeout"; - - private URL url; - private String rootPath; - private ConsulClient client; - private int watchTimeout = -1; - private ConcurrentMap watchers = new ConcurrentHashMap<>(); - private ConcurrentMap consulIndexes = new ConcurrentHashMap<>(); - private ExecutorService watcherService = newCachedThreadPool( - new NamedThreadFactory("dubbo-consul-configuration-watcher", true)); - - public ConsulDynamicConfiguration(URL url) { - this.url = url; - this.rootPath = PATH_SEPARATOR + url.getParameter(CONFIG_NAMESPACE_KEY, DEFAULT_GROUP) + PATH_SEPARATOR + "config"; - this.watchTimeout = buildWatchTimeout(url); - String host = url.getHost(); - int port = url.getPort() != 0 ? url.getPort() : DEFAULT_PORT; - client = new ConsulClient(host, port); - } - - @Override - public void addListener(String key, String group, ConfigurationListener listener) { - logger.info("register listener " + listener.getClass() + " for config with key: " + key + ", group: " + group); - ConsulKVWatcher watcher = watchers.putIfAbsent(key, new ConsulKVWatcher(key)); - if (watcher == null) { - watcher = watchers.get(key); - watcherService.submit(watcher); - } - watcher.addListener(listener); - } - - @Override - public void removeListener(String key, String group, ConfigurationListener listener) { - logger.info("unregister listener " + listener.getClass() + " for config with key: " + key + ", group: " + group); - ConsulKVWatcher watcher = watchers.get(key); - if (watcher != null) { - watcher.removeListener(listener); - } - } - - @Override - public String getConfig(String key, String group, long timeout) throws IllegalStateException { - if (StringUtils.isNotEmpty(group)) { - key = group + PATH_SEPARATOR + key; - } else { - int i = key.lastIndexOf("."); - key = key.substring(0, i) + PATH_SEPARATOR + key.substring(i + 1); - } - - return (String) getInternalProperty(rootPath + PATH_SEPARATOR + key); - } - - @Override - public Object getInternalProperty(String key) { - logger.info("get config from: " + key); - Long currentIndex = consulIndexes.computeIfAbsent(key, k -> -1L); - Response response = client.getKVValue(key, new QueryParams(watchTimeout, currentIndex)); - GetValue value = response.getValue(); - consulIndexes.put(key, response.getConsulIndex()); - return value != null ? value.getDecodedValue() : null; - } - - private int buildWatchTimeout(URL url) { - return url.getParameter(WATCH_TIMEOUT, DEFAULT_WATCH_TIMEOUT) / 1000; - } - - private class ConsulKVWatcher implements Runnable { - private String key; - private Set listeners; - private boolean running = true; - - public ConsulKVWatcher(String key) { - this.key = convertKey(key); - this.listeners = new HashSet<>(); - } - - @Override - public void run() { - while (running) { - Long lastIndex = consulIndexes.computeIfAbsent(key, k -> -1L); - Response response = client.getKVValue(key, new QueryParams(watchTimeout, lastIndex)); - - Long currentIndex = response.getConsulIndex(); - if (currentIndex == null || currentIndex <= lastIndex) { - continue; - } - - consulIndexes.put(key, currentIndex); - String value = response.getValue().getDecodedValue(); - logger.info("notify change for key: " + key + ", the value is: " + value); - ConfigChangeEvent event = new ConfigChangeEvent(key, value); - for (ConfigurationListener listener : listeners) { - listener.process(event); - } - } - } - - private void addListener(ConfigurationListener listener) { - this.listeners.add(listener); - } - - private void removeListener(ConfigurationListener listener) { - this.listeners.remove(listener); - } - - private String convertKey(String key) { - int index = key.lastIndexOf('.'); - return rootPath + PATH_SEPARATOR + key.substring(0, index) + PATH_SEPARATOR + key.substring(index + 1); - } - - private void stop() { - running = false; - } - } -} diff --git a/dubbo-configcenter/dubbo-configcenter-consul/src/main/java/org/apache/dubbo/configcenter/consul/ConsulDynamicConfigurationFactory.java b/dubbo-configcenter/dubbo-configcenter-consul/src/main/java/org/apache/dubbo/configcenter/consul/ConsulDynamicConfigurationFactory.java deleted file mode 100644 index 813b6174662..00000000000 --- a/dubbo-configcenter/dubbo-configcenter-consul/src/main/java/org/apache/dubbo/configcenter/consul/ConsulDynamicConfigurationFactory.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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 org.apache.dubbo.configcenter.consul; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.configcenter.AbstractDynamicConfigurationFactory; -import org.apache.dubbo.configcenter.DynamicConfiguration; - -/** - * Config center factory for consul - */ -public class ConsulDynamicConfigurationFactory extends AbstractDynamicConfigurationFactory { - @Override - protected DynamicConfiguration createDynamicConfiguration(URL url) { - return new ConsulDynamicConfiguration(url); - } -} diff --git a/dubbo-configcenter/dubbo-configcenter-consul/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.configcenter.DynamicConfigurationFactory b/dubbo-configcenter/dubbo-configcenter-consul/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.configcenter.DynamicConfigurationFactory deleted file mode 100644 index b7a5091efa3..00000000000 --- a/dubbo-configcenter/dubbo-configcenter-consul/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.configcenter.DynamicConfigurationFactory +++ /dev/null @@ -1 +0,0 @@ -consul=org.apache.dubbo.configcenter.consul.ConsulDynamicConfigurationFactory diff --git a/dubbo-configcenter/dubbo-configcenter-etcd/pom.xml b/dubbo-configcenter/dubbo-configcenter-etcd/pom.xml deleted file mode 100644 index 60efc8e4bb9..00000000000 --- a/dubbo-configcenter/dubbo-configcenter-etcd/pom.xml +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - dubbo-configcenter - org.apache.dubbo - 2.7.1-SNAPSHOT - - 4.0.0 - - dubbo-configcenter-etcd - jar - ${project.artifactId} - The etcd implementation of the config-center api - - - - org.apache.dubbo - dubbo-configcenter-api - ${project.parent.version} - - - org.apache.dubbo - dubbo-remoting-etcd3 - ${project.parent.version} - - - \ No newline at end of file diff --git a/dubbo-configcenter/dubbo-configcenter-etcd/src/main/java/org/apache/dubbo/configcenter/support/etcd/EtcdDynamicConfiguration.java b/dubbo-configcenter/dubbo-configcenter-etcd/src/main/java/org/apache/dubbo/configcenter/support/etcd/EtcdDynamicConfiguration.java deleted file mode 100644 index 18e90887592..00000000000 --- a/dubbo-configcenter/dubbo-configcenter-etcd/src/main/java/org/apache/dubbo/configcenter/support/etcd/EtcdDynamicConfiguration.java +++ /dev/null @@ -1,187 +0,0 @@ -/* - * 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 org.apache.dubbo.configcenter.support.etcd; - -import com.google.protobuf.ByteString; -import io.etcd.jetcd.api.Event; -import io.etcd.jetcd.api.WatchCancelRequest; -import io.etcd.jetcd.api.WatchCreateRequest; -import io.etcd.jetcd.api.WatchGrpc; -import io.etcd.jetcd.api.WatchRequest; -import io.etcd.jetcd.api.WatchResponse; -import io.grpc.ManagedChannel; -import io.grpc.stub.StreamObserver; -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.configcenter.ConfigChangeEvent; -import org.apache.dubbo.configcenter.ConfigChangeType; -import org.apache.dubbo.configcenter.ConfigurationListener; -import org.apache.dubbo.configcenter.DynamicConfiguration; -import org.apache.dubbo.remoting.etcd.StateListener; -import org.apache.dubbo.remoting.etcd.jetcd.JEtcdClient; - -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - -import static java.nio.charset.StandardCharsets.UTF_8; -import static org.apache.dubbo.common.Constants.CONFIG_NAMESPACE_KEY; -import static org.apache.dubbo.common.Constants.PATH_SEPARATOR; - -/** - * The etcd implementation of {@link DynamicConfiguration} - */ -public class EtcdDynamicConfiguration implements DynamicConfiguration { - - /** - * The final root path would be: /$NAME_SPACE/config - */ - private String rootPath; - - /** - * The etcd client - */ - private final JEtcdClient etcdClient; - - /** - * The map store the key to {@link EtcdConfigWatcher} mapping - */ - private final ConcurrentMap watchListenerMap; - - EtcdDynamicConfiguration(URL url) { - rootPath = "/" + url.getParameter(CONFIG_NAMESPACE_KEY, DEFAULT_GROUP) + "/config"; - etcdClient = new JEtcdClient(url); - etcdClient.addStateListener(state -> { - if (state == StateListener.CONNECTED) { - try { - recover(); - } catch (Exception e) { - // ignore - } - } - }); - watchListenerMap = new ConcurrentHashMap<>(); - } - - @Override - public void addListener(String key, String group, ConfigurationListener listener) { - if (watchListenerMap.get(listener) == null) { - String normalizedKey = convertKey(key); - EtcdConfigWatcher watcher = new EtcdConfigWatcher(normalizedKey, listener); - watchListenerMap.put(listener, watcher); - watcher.watch(); - } - } - - @Override - public void removeListener(String key, String group, ConfigurationListener listener) { - EtcdConfigWatcher watcher = watchListenerMap.get(listener); - watcher.cancelWatch(); - } - - // TODO Abstract the logic into super class - @Override - public String getConfig(String key, String group, long timeout) throws IllegalStateException { - if (StringUtils.isNotEmpty(group)) { - key = group + PATH_SEPARATOR + key; - } else { - int i = key.lastIndexOf("."); - key = key.substring(0, i) + PATH_SEPARATOR + key.substring(i + 1); - } - return (String) getInternalProperty(rootPath + PATH_SEPARATOR + key); - } - - @Override - public Object getInternalProperty(String key) { - return etcdClient.getKVValue(key); - } - - - private String convertKey(String key) { - int index = key.lastIndexOf('.'); - return rootPath + PATH_SEPARATOR + key.substring(0, index) + PATH_SEPARATOR + key.substring(index + 1); - } - - private void recover() { - for (EtcdConfigWatcher watcher: watchListenerMap.values()) { - watcher.watch(); - } - } - - public class EtcdConfigWatcher implements StreamObserver { - - private ConfigurationListener listener; - protected WatchGrpc.WatchStub watchStub; - private StreamObserver observer; - protected long watchId; - private ManagedChannel channel; - private String key; - - public EtcdConfigWatcher(String key, ConfigurationListener listener) { - this.key = key; - this.listener = listener; - this.channel = etcdClient.getChannel(); - } - - @Override - public void onNext(WatchResponse watchResponse) { - this.watchId = watchResponse.getWatchId(); - for (Event etcdEvent : watchResponse.getEventsList()) { - ConfigChangeType type = ConfigChangeType.MODIFIED; - if (etcdEvent.getType() == Event.EventType.DELETE) { - type = ConfigChangeType.DELETED; - } - ConfigChangeEvent event = new ConfigChangeEvent( - etcdEvent.getKv().getKey().toString(UTF_8), - etcdEvent.getKv().getValue().toString(UTF_8), type); - listener.process(event); - } - } - - @Override - public void onError(Throwable throwable) { - // ignore - } - - @Override - public void onCompleted() { - // ignore - } - - public long getWatchId() { - return watchId; - } - - private void watch() { - watchStub = WatchGrpc.newStub(channel); - observer = watchStub.watch(this); - WatchCreateRequest.Builder builder = WatchCreateRequest.newBuilder() - .setKey(ByteString.copyFromUtf8(key)) - .setProgressNotify(true); - WatchRequest req = WatchRequest.newBuilder().setCreateRequest(builder).build(); - observer.onNext(req); - } - - private void cancelWatch() { - WatchCancelRequest watchCancelRequest = - WatchCancelRequest.newBuilder().setWatchId(watchId).build(); - WatchRequest cancelRequest = WatchRequest.newBuilder() - .setCancelRequest(watchCancelRequest).build(); - observer.onNext(cancelRequest); - } - } -} diff --git a/dubbo-configcenter/dubbo-configcenter-etcd/src/main/java/org/apache/dubbo/configcenter/support/etcd/EtcdDynamicConfigurationFactory.java b/dubbo-configcenter/dubbo-configcenter-etcd/src/main/java/org/apache/dubbo/configcenter/support/etcd/EtcdDynamicConfigurationFactory.java deleted file mode 100644 index 02e91a62db7..00000000000 --- a/dubbo-configcenter/dubbo-configcenter-etcd/src/main/java/org/apache/dubbo/configcenter/support/etcd/EtcdDynamicConfigurationFactory.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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 org.apache.dubbo.configcenter.support.etcd; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.configcenter.AbstractDynamicConfigurationFactory; -import org.apache.dubbo.configcenter.DynamicConfiguration; - -/** - * The etcd implementation of {@link AbstractDynamicConfigurationFactory} - */ -public class EtcdDynamicConfigurationFactory extends AbstractDynamicConfigurationFactory { - - @Override - protected DynamicConfiguration createDynamicConfiguration(URL url) { - return new EtcdDynamicConfiguration(url); - } -} diff --git a/dubbo-configcenter/dubbo-configcenter-etcd/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.configcenter.DynamicConfigurationFactory b/dubbo-configcenter/dubbo-configcenter-etcd/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.configcenter.DynamicConfigurationFactory deleted file mode 100644 index d84b1ae0e1a..00000000000 --- a/dubbo-configcenter/dubbo-configcenter-etcd/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.configcenter.DynamicConfigurationFactory +++ /dev/null @@ -1 +0,0 @@ -etcd=org.apache.dubbo.configcenter.support.etcd.EtcdDynamicConfigurationFactory \ No newline at end of file diff --git a/dubbo-configcenter/dubbo-configcenter-etcd/src/test/java/org/apache/dubbo/configcenter/support/etcd/EtcdDynamicConfigurationTest.java b/dubbo-configcenter/dubbo-configcenter-etcd/src/test/java/org/apache/dubbo/configcenter/support/etcd/EtcdDynamicConfigurationTest.java deleted file mode 100644 index 87143fdcacc..00000000000 --- a/dubbo-configcenter/dubbo-configcenter-etcd/src/test/java/org/apache/dubbo/configcenter/support/etcd/EtcdDynamicConfigurationTest.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * 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 org.apache.dubbo.configcenter.support.etcd; - -import io.etcd.jetcd.ByteSequence; -import io.etcd.jetcd.Client; -import org.apache.dubbo.common.Constants; -import org.apache.dubbo.common.URL; -import org.apache.dubbo.configcenter.ConfigChangeEvent; -import org.apache.dubbo.configcenter.ConfigurationListener; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; - -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -import static java.nio.charset.StandardCharsets.UTF_8; - -/** - * Unit test for etcd config center support - * TODO Integrate with https://github.com/etcd-io/jetcd#launcher or using mock data. - */ -@Disabled -public class EtcdDynamicConfigurationTest { - - private static final String ENDPOINT = "http://127.0.0.1:2379"; - - private static EtcdDynamicConfiguration config; - - private static Client etcdClient; - - @Test - public void testGetConfig() { - put("/dubbo/config/org.apache.dubbo.etcd.testService/configurators", "hello"); - put("/dubbo/config/test/dubbo.properties", "aaa=bbb"); - Assertions.assertEquals("hello", config.getConfig("org.apache.dubbo.etcd.testService.configurators")); - Assertions.assertEquals("aaa=bbb", config.getConfig("dubbo.properties", "test")); - } - - @Test - public void testAddListener() throws Exception { - CountDownLatch latch = new CountDownLatch(4); - TestListener listener1 = new TestListener(latch); - TestListener listener2 = new TestListener(latch); - TestListener listener3 = new TestListener(latch); - TestListener listener4 = new TestListener(latch); - config.addListener("AService.configurators", listener1); - config.addListener("AService.configurators", listener2); - config.addListener("testapp.tagrouters", listener3); - config.addListener("testapp.tagrouters", listener4); - - put("/dubbo/config/AService/configurators", "new value1"); - Thread.sleep(200); - put("/dubbo/config/testapp/tagrouters", "new value2"); - Thread.sleep(200); - put("/dubbo/config/testapp", "new value3"); - - Thread.sleep(1000); - - Assertions.assertTrue(latch.await(5, TimeUnit.SECONDS)); - Assertions.assertEquals(1, listener1.getCount("/dubbo/config/AService/configurators")); - Assertions.assertEquals(1, listener2.getCount("/dubbo/config/AService/configurators")); - Assertions.assertEquals(1, listener3.getCount("/dubbo/config/testapp/tagrouters")); - Assertions.assertEquals(1, listener4.getCount("/dubbo/config/testapp/tagrouters")); - - Assertions.assertEquals("new value1", listener1.getValue()); - Assertions.assertEquals("new value1", listener2.getValue()); - Assertions.assertEquals("new value2", listener3.getValue()); - Assertions.assertEquals("new value2", listener4.getValue()); - } - - private class TestListener implements ConfigurationListener { - private CountDownLatch latch; - private String value; - private Map countMap = new HashMap<>(); - - public TestListener(CountDownLatch latch) { - this.latch = latch; - } - - @Override - public void process(ConfigChangeEvent event) { - Integer count = countMap.computeIfAbsent(event.getKey(), k -> 0); - countMap.put(event.getKey(), ++count); - value = event.getValue(); - latch.countDown(); - } - - public int getCount(String key) { - return countMap.get(key); - } - - public String getValue() { - return value; - } - } - - static void put(String key, String value) { - try { - etcdClient.getKVClient() - .put(ByteSequence.from(key, UTF_8), ByteSequence.from(value, UTF_8)) - .get(); - } catch (Exception e) { - System.out.println("Error put value to etcd."); - } - } - - @BeforeAll - static void setUp() { - etcdClient = Client.builder().endpoints(ENDPOINT).build(); - // timeout in 15 seconds. - URL url = URL.valueOf("etcd3://127.0.0.1:2379/org.apache.dubbo.etcd.testService") - .addParameter(Constants.SESSION_TIMEOUT_KEY, 15000); - config = new EtcdDynamicConfiguration(url); - } - - @AfterAll - static void tearDown() { - etcdClient.close(); - } -} diff --git a/dubbo-configcenter/dubbo-configcenter-zookeeper/pom.xml b/dubbo-configcenter/dubbo-configcenter-zookeeper/pom.xml index 5c84f6515ac..9b5717cf7b7 100644 --- a/dubbo-configcenter/dubbo-configcenter-zookeeper/pom.xml +++ b/dubbo-configcenter/dubbo-configcenter-zookeeper/pom.xml @@ -14,12 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. --> - + 4.0.0 org.apache.dubbo dubbo-configcenter - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-configcenter-zookeeper jar @@ -33,9 +34,16 @@ ${project.parent.version} - org.apache.dubbo - dubbo-remoting-zookeeper - ${project.parent.version} + org.apache.curator + curator-framework + + + org.apache.curator + curator-recipes + + + org.apache.zookeeper + zookeeper org.apache.curator @@ -43,4 +51,4 @@ test - + \ No newline at end of file diff --git a/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/CacheListener.java b/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/CacheListener.java index 4f6c6382952..a1e1f083523 100644 --- a/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/CacheListener.java +++ b/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/CacheListener.java @@ -21,9 +21,13 @@ import org.apache.dubbo.configcenter.ConfigChangeEvent; import org.apache.dubbo.configcenter.ConfigChangeType; import org.apache.dubbo.configcenter.ConfigurationListener; -import org.apache.dubbo.remoting.zookeeper.DataListener; -import org.apache.dubbo.remoting.zookeeper.EventType; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.ChildData; +import org.apache.curator.framework.recipes.cache.TreeCacheEvent; +import org.apache.curator.framework.recipes.cache.TreeCacheListener; + +import java.nio.charset.StandardCharsets; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -33,8 +37,7 @@ /** * */ - -public class CacheListener implements DataListener { +public class CacheListener implements TreeCacheListener { private Map> keyListeners = new ConcurrentHashMap<>(); private CountDownLatch initializedLatch; private String rootPath; @@ -44,73 +47,73 @@ public CacheListener(String rootPath, CountDownLatch initializedLatch) { this.initializedLatch = initializedLatch; } - public void addListener(String key, ConfigurationListener configurationListener) { - Set listeners = this.keyListeners.computeIfAbsent(key, k -> new CopyOnWriteArraySet<>()); - listeners.add(configurationListener); - } - - public void removeListener(String key, ConfigurationListener configurationListener) { - Set listeners = this.keyListeners.get(key); - if (listeners != null) { - listeners.remove(configurationListener); - } - } - - /** - * This is used to convert a configuration nodePath into a key - * TODO doc - * - * @param path - * @return key (nodePath less the config root path) - */ - private String pathToKey(String path) { - if (StringUtils.isEmpty(path)) { - return path; - } - return path.replace(rootPath + "/", "").replaceAll("/", "."); - } - - @Override - public void dataChanged(String path, Object value, EventType eventType) { - if (eventType == null) { - return; - } + public void childEvent(CuratorFramework aClient, TreeCacheEvent event) throws Exception { - if (eventType == EventType.INITIALIZED) { + TreeCacheEvent.Type type = event.getType(); + ChildData data = event.getData(); + if (type == TreeCacheEvent.Type.INITIALIZED) { initializedLatch.countDown(); return; } - if (path == null || (value == null && eventType != EventType.NodeDeleted)) { + // TODO, ignore other event types + if (data == null) { return; } // TODO We limit the notification of config changes to a specific path level, for example // /dubbo/config/service/configurators, other config changes not in this level will not get notified, // say /dubbo/config/dubbo.properties - if (path.split("/").length >= 5) { - String key = pathToKey(path); + if (data.getPath().split("/").length >= 5) { + byte[] value = data.getData(); + String key = pathToKey(data.getPath()); ConfigChangeType changeType; - switch (eventType) { - case NodeCreated: + switch (type) { + case NODE_ADDED: changeType = ConfigChangeType.ADDED; break; - case NodeDeleted: + case NODE_REMOVED: changeType = ConfigChangeType.DELETED; break; - case NodeDataChanged: + case NODE_UPDATED: changeType = ConfigChangeType.MODIFIED; break; default: return; } - ConfigChangeEvent configChangeEvent = new ConfigChangeEvent(key, (String) value, changeType); + ConfigChangeEvent configChangeEvent = new ConfigChangeEvent(key, new String(value, StandardCharsets.UTF_8), changeType); Set listeners = keyListeners.get(key); if (CollectionUtils.isNotEmpty(listeners)) { listeners.forEach(listener -> listener.process(configChangeEvent)); } } } + + public void addListener(String key, ConfigurationListener configurationListener) { + Set listeners = this.keyListeners.computeIfAbsent(key, k -> new CopyOnWriteArraySet<>()); + listeners.add(configurationListener); + } + + public void removeListener(String key, ConfigurationListener configurationListener) { + Set listeners = this.keyListeners.get(key); + if (listeners != null) { + listeners.remove(configurationListener); + } + } + + /** + * This is used to convert a configuration nodePath into a key + * TODO doc + * + * @param path + * @return key (nodePath less the config root path) + */ + private String pathToKey(String path) { + if (StringUtils.isEmpty(path)) { + return path; + } + return path.replace(rootPath + "/", "").replaceAll("/", "."); + } } diff --git a/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfiguration.java b/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfiguration.java index dac49ccb0e6..7a106f86dec 100644 --- a/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfiguration.java +++ b/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfiguration.java @@ -16,21 +16,28 @@ */ package org.apache.dubbo.configcenter.support.zookeeper; +import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.utils.NamedThreadFactory; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.configcenter.ConfigurationListener; import org.apache.dubbo.configcenter.DynamicConfiguration; -import org.apache.dubbo.remoting.zookeeper.ZookeeperClient; -import org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter; +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.ChildData; +import org.apache.curator.framework.recipes.cache.TreeCache; +import org.apache.curator.retry.ExponentialBackoffRetry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.nio.charset.StandardCharsets; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import static org.apache.curator.framework.CuratorFrameworkFactory.newClient; import static org.apache.dubbo.common.Constants.CONFIG_NAMESPACE_KEY; /** @@ -38,32 +45,52 @@ */ public class ZookeeperDynamicConfiguration implements DynamicConfiguration { private static final Logger logger = LoggerFactory.getLogger(ZookeeperDynamicConfiguration.class); - private Executor executor; + private CuratorFramework client; + // The final root path would be: /configRootPath/"config" private String rootPath; - private final ZookeeperClient zkClient; + private TreeCache treeCache; private CountDownLatch initializedLatch; private CacheListener cacheListener; private URL url; - ZookeeperDynamicConfiguration(URL url, ZookeeperTransporter zookeeperTransporter) { + ZookeeperDynamicConfiguration(URL url) { this.url = url; rootPath = "/" + url.getParameter(CONFIG_NAMESPACE_KEY, DEFAULT_GROUP) + "/config"; + RetryPolicy policy = new ExponentialBackoffRetry(1000, 3); + int sessionTimeout = url.getParameter("config.session.timeout", 60 * 1000); + int connectTimeout = url.getParameter("config.connect.timeout", 10 * 1000); + String connectString = url.getBackupAddress(); + client = newClient(connectString, sessionTimeout, connectTimeout, policy); + client.start(); + + try { + boolean connected = client.blockUntilConnected(3 * connectTimeout, TimeUnit.MILLISECONDS); + if (!connected) { + if (url.getParameter(Constants.CONFIG_CHECK_KEY, true)) { + throw new IllegalStateException("Failed to connect to config center (zookeeper): " + + connectString + " in " + 3 * connectTimeout + "ms."); + } else { + logger.warn("The config center (zookeeper) is not fully initialized in " + 3 * connectTimeout + "ms, address is: " + connectString); + } + } + } catch (InterruptedException e) { + throw new IllegalStateException("The thread was interrupted unexpectedly when trying connecting to zookeeper " + + connectString + " config center, ", e); + } + initializedLatch = new CountDownLatch(1); this.cacheListener = new CacheListener(rootPath, initializedLatch); this.executor = Executors.newFixedThreadPool(1, new NamedThreadFactory(this.getClass().getSimpleName(), true)); - - zkClient = zookeeperTransporter.connect(url); - zkClient.addDataListener(rootPath, cacheListener, executor); + // build local cache try { - // Wait for connection - this.initializedLatch.await(); - } catch (InterruptedException e) { - logger.warn("Failed to build local cache for config center (zookeeper)." + url); + this.buildCache(); + } catch (Exception e) { + logger.warn("Failed to build local cache for config center (zookeeper), address is ." + connectString); } } @@ -73,7 +100,11 @@ public class ZookeeperDynamicConfiguration implements DynamicConfiguration { */ @Override public Object getInternalProperty(String key) { - return zkClient.getContent(key); + ChildData childData = treeCache.getCurrentData(key); + if (childData != null) { + return new String(childData.getData(), StandardCharsets.UTF_8); + } + return null; } /** @@ -110,4 +141,18 @@ public String getConfig(String key, String group, long timeout) throws IllegalSt return (String) getInternalProperty(rootPath + "/" + key); } + + /** + * Adds a listener to the pathChildrenCache, initializes the cache, then starts the cache-management background + * thread + */ + private void buildCache() throws Exception { + this.treeCache = new TreeCache(client, rootPath); + // create the watcher for future configuration updates + treeCache.getListenable().addListener(cacheListener, executor); + + // it's not blocking, so we use an extra latch 'initializedLatch' to make sure cache fully initialized before use. + treeCache.start(); + initializedLatch.await(); + } } diff --git a/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfigurationFactory.java b/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfigurationFactory.java index 4d78133dbaf..7994e0461f3 100644 --- a/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfigurationFactory.java +++ b/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfigurationFactory.java @@ -19,22 +19,13 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.configcenter.AbstractDynamicConfigurationFactory; import org.apache.dubbo.configcenter.DynamicConfiguration; -import org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter; /** * */ public class ZookeeperDynamicConfigurationFactory extends AbstractDynamicConfigurationFactory { - - private ZookeeperTransporter zookeeperTransporter; - - public void setZookeeperTransporter(ZookeeperTransporter zookeeperTransporter) { - this.zookeeperTransporter = zookeeperTransporter; - } - - @Override protected DynamicConfiguration createDynamicConfiguration(URL url) { - return new ZookeeperDynamicConfiguration(url, zookeeperTransporter); + return new ZookeeperDynamicConfiguration(url); } } diff --git a/dubbo-configcenter/dubbo-configcenter-zookeeper/src/test/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfigurationTest.java b/dubbo-configcenter/dubbo-configcenter-zookeeper/src/test/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfigurationTest.java index 40f9f04a95a..b25fd7ba2e5 100644 --- a/dubbo-configcenter/dubbo-configcenter-zookeeper/src/test/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfigurationTest.java +++ b/dubbo-configcenter/dubbo-configcenter-zookeeper/src/test/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfigurationTest.java @@ -28,10 +28,10 @@ import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.test.TestingServer; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; import java.util.HashMap; import java.util.Map; @@ -48,7 +48,7 @@ public class ZookeeperDynamicConfigurationTest { private static TestingServer zkServer; private static DynamicConfiguration configuration; - @BeforeAll + @BeforeClass public static void setUp() throws Exception { zkServer = new TestingServer(zkServerPort, true); @@ -72,7 +72,7 @@ public static void setUp() throws Exception { configuration = ExtensionLoader.getExtensionLoader(DynamicConfigurationFactory.class).getExtension(configUrl.getProtocol()).getDynamicConfiguration(configUrl); } - @AfterAll + @AfterClass public static void tearDown() throws Exception { zkServer.stop(); } @@ -86,8 +86,8 @@ private static void setData(String path, String data) throws Exception { @Test public void testGetConfig() throws Exception { - Assertions.assertEquals("Never change value from configurators", configuration.getConfig("never.change.DemoService.configurators")); - Assertions.assertEquals("The content from dubbo.properties", configuration.getConfig("dubbo.properties", "dubbo")); + Assert.assertEquals("Never change value from configurators", configuration.getConfig("never.change.DemoService.configurators")); + Assert.assertEquals("The content from dubbo.properties", configuration.getConfig("dubbo.properties", "dubbo")); } @Test @@ -111,15 +111,15 @@ public void testAddListener() throws Exception { Thread.sleep(5000); latch.await(); - Assertions.assertEquals(1, listener1.getCount("group*service:version.configurators")); - Assertions.assertEquals(1, listener2.getCount("group*service:version.configurators")); - Assertions.assertEquals(1, listener3.getCount("appname.tagrouters")); - Assertions.assertEquals(1, listener4.getCount("appname.tagrouters")); - - Assertions.assertEquals("new value1", listener1.getValue()); - Assertions.assertEquals("new value1", listener2.getValue()); - Assertions.assertEquals("new value2", listener3.getValue()); - Assertions.assertEquals("new value2", listener4.getValue()); + Assert.assertEquals(1, listener1.getCount("group*service:version.configurators")); + Assert.assertEquals(1, listener2.getCount("group*service:version.configurators")); + Assert.assertEquals(1, listener3.getCount("appname.tagrouters")); + Assert.assertEquals(1, listener4.getCount("appname.tagrouters")); + + Assert.assertEquals("new value1", listener1.getValue()); + Assert.assertEquals("new value1", listener2.getValue()); + Assert.assertEquals("new value2", listener3.getValue()); + Assert.assertEquals("new value2", listener4.getValue()); } private class TestListener implements ConfigurationListener { @@ -133,7 +133,6 @@ public TestListener(CountDownLatch latch) { @Override public void process(ConfigChangeEvent event) { - System.out.println(this + ": " + event); Integer count = countMap.computeIfAbsent(event.getKey(), k -> new Integer(0)); countMap.put(event.getKey(), ++count); diff --git a/dubbo-configcenter/pom.xml b/dubbo-configcenter/pom.xml index 92f727d2321..e969c044b02 100644 --- a/dubbo-configcenter/pom.xml +++ b/dubbo-configcenter/pom.xml @@ -14,12 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. --> - + 4.0.0 org.apache.dubbo dubbo-parent - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-configcenter pom @@ -33,7 +34,5 @@ dubbo-configcenter-api dubbo-configcenter-zookeeper dubbo-configcenter-apollo - dubbo-configcenter-consul - dubbo-configcenter-etcd - + \ No newline at end of file diff --git a/dubbo-container/dubbo-container-api/pom.xml b/dubbo-container/dubbo-container-api/pom.xml index 2c1e9100af5..13efee6964b 100644 --- a/dubbo-container/dubbo-container-api/pom.xml +++ b/dubbo-container/dubbo-container-api/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-container - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-container-api jar @@ -51,4 +51,4 @@ - + \ No newline at end of file diff --git a/dubbo-container/dubbo-container-api/src/main/java/org/apache/dubbo/container/Container.java b/dubbo-container/dubbo-container-api/src/main/java/org/apache/dubbo/container/Container.java index bb3cc5cc105..ec552959fac 100644 --- a/dubbo-container/dubbo-container-api/src/main/java/org/apache/dubbo/container/Container.java +++ b/dubbo-container/dubbo-container-api/src/main/java/org/apache/dubbo/container/Container.java @@ -25,12 +25,12 @@ public interface Container { /** - * start method to load the container. + * start. */ void start(); /** - * stop method to unload the container. + * stop. */ void stop(); diff --git a/dubbo-container/dubbo-container-api/src/main/java/org/apache/dubbo/container/Main.java b/dubbo-container/dubbo-container-api/src/main/java/org/apache/dubbo/container/Main.java index 5ae08c27af6..80646ae7bb9 100644 --- a/dubbo-container/dubbo-container-api/src/main/java/org/apache/dubbo/container/Main.java +++ b/dubbo-container/dubbo-container-api/src/main/java/org/apache/dubbo/container/Main.java @@ -20,7 +20,6 @@ import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.ArrayUtils; import org.apache.dubbo.common.utils.ConfigUtils; import java.text.SimpleDateFormat; @@ -33,8 +32,6 @@ /** * Main. (API, Static, ThreadSafe) - * - * This class is entry point loading containers. */ public class Main { @@ -52,7 +49,7 @@ public class Main { public static void main(String[] args) { try { - if (ArrayUtils.isEmpty(args)) { + if (args == null || args.length == 0) { String config = ConfigUtils.getProperty(CONTAINER_KEY, loader.getDefaultExtensionName()); args = Constants.COMMA_SPLIT_PATTERN.split(config); } @@ -91,6 +88,7 @@ public void run() { } System.out.println(new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]").format(new Date()) + " Dubbo service server started!"); } catch (RuntimeException e) { + e.printStackTrace(); logger.error(e.getMessage(), e); System.exit(1); } diff --git a/dubbo-container/dubbo-container-api/src/main/resources/META-INF/assembly/bin/start.sh b/dubbo-container/dubbo-container-api/src/main/resources/META-INF/assembly/bin/start.sh index 56e39143f0c..595b7695d44 100755 --- a/dubbo-container/dubbo-container-api/src/main/resources/META-INF/assembly/bin/start.sh +++ b/dubbo-container/dubbo-container-api/src/main/resources/META-INF/assembly/bin/start.sh @@ -90,7 +90,7 @@ while [ $COUNT -lt 1 ]; do sleep 1 if [ -n "$SERVER_PORT" ]; then if [ "$SERVER_PROTOCOL" == "dubbo" ]; then - COUNT=`(sleep 1; echo -e '\n'; sleep 1; echo status; sleep 1)| telnet $SERVER_HOST $SERVER_PORT | grep -c OK` + COUNT=`echo status | nc -i 1 $SERVER_HOST $SERVER_PORT | grep -c OK` else COUNT=`netstat -an | grep $SERVER_PORT | wc -l` fi diff --git a/dubbo-container/dubbo-container-log4j/pom.xml b/dubbo-container/dubbo-container-log4j/pom.xml index be5a1733a36..91814b3a9dd 100644 --- a/dubbo-container/dubbo-container-log4j/pom.xml +++ b/dubbo-container/dubbo-container-log4j/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-container - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-container-log4j jar diff --git a/dubbo-container/dubbo-container-log4j/src/main/java/org/apache/dubbo/container/log4j/Log4jContainer.java b/dubbo-container/dubbo-container-log4j/src/main/java/org/apache/dubbo/container/log4j/Log4jContainer.java index 946e4c59eb3..4b90cc2b2e0 100644 --- a/dubbo-container/dubbo-container-log4j/src/main/java/org/apache/dubbo/container/log4j/Log4jContainer.java +++ b/dubbo-container/dubbo-container-log4j/src/main/java/org/apache/dubbo/container/log4j/Log4jContainer.java @@ -17,7 +17,6 @@ package org.apache.dubbo.container.log4j; import org.apache.dubbo.common.config.ConfigurationUtils; -import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.container.Container; import org.apache.log4j.Appender; @@ -30,8 +29,6 @@ /** * Log4jContainer. (SPI, Singleton, ThreadSafe) - * - * The container class implementation for Log4j */ public class Log4jContainer implements Container { @@ -49,7 +46,7 @@ public void start() { String file = ConfigurationUtils.getProperty(LOG4J_FILE); if (file != null && file.length() > 0) { String level = ConfigurationUtils.getProperty(LOG4J_LEVEL); - if (StringUtils.isEmpty(level)) { + if (level == null || level.length() == 0) { level = DEFAULT_LOG4J_LEVEL; } Properties properties = new Properties(); diff --git a/dubbo-container/dubbo-container-log4j/src/test/java/org/apache/dubbo/container/log4j/Log4jContainerTest.java b/dubbo-container/dubbo-container-log4j/src/test/java/org/apache/dubbo/container/log4j/Log4jContainerTest.java index 535a3d5c5a4..b34d643930c 100644 --- a/dubbo-container/dubbo-container-log4j/src/test/java/org/apache/dubbo/container/log4j/Log4jContainerTest.java +++ b/dubbo-container/dubbo-container-log4j/src/test/java/org/apache/dubbo/container/log4j/Log4jContainerTest.java @@ -19,7 +19,7 @@ import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.container.Container; -import org.junit.jupiter.api.Test; +import org.junit.Test; /** * StandaloneContainerTest diff --git a/dubbo-container/dubbo-container-logback/pom.xml b/dubbo-container/dubbo-container-logback/pom.xml index 9a1aa47eda2..6227931bc74 100644 --- a/dubbo-container/dubbo-container-logback/pom.xml +++ b/dubbo-container/dubbo-container-logback/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-container - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-container-logback jar diff --git a/dubbo-container/dubbo-container-logback/src/main/java/org/apache/dubbo/container/logback/LogbackContainer.java b/dubbo-container/dubbo-container-logback/src/main/java/org/apache/dubbo/container/logback/LogbackContainer.java index 430e2e2191a..080b8ae6bcf 100644 --- a/dubbo-container/dubbo-container-logback/src/main/java/org/apache/dubbo/container/logback/LogbackContainer.java +++ b/dubbo-container/dubbo-container-logback/src/main/java/org/apache/dubbo/container/logback/LogbackContainer.java @@ -31,8 +31,6 @@ /** * LogbackContainer. (SPI, Singleton, ThreadSafe) - * - * The container class implementation for Logback */ public class LogbackContainer implements Container { @@ -49,7 +47,7 @@ public void start() { String file = ConfigUtils.getProperty(LOGBACK_FILE); if (file != null && file.length() > 0) { String level = ConfigUtils.getProperty(LOGBACK_LEVEL); - if (StringUtils.isEmpty(level)) { + if (level == null || level.length() == 0) { level = DEFAULT_LOGBACK_LEVEL; } // maxHistory=0 Infinite history diff --git a/dubbo-container/dubbo-container-logback/src/test/java/org/apache/dubbo/container/logback/LogbackContainerTest.java b/dubbo-container/dubbo-container-logback/src/test/java/org/apache/dubbo/container/logback/LogbackContainerTest.java index d82e8a48a90..abf90f5df8f 100644 --- a/dubbo-container/dubbo-container-logback/src/test/java/org/apache/dubbo/container/logback/LogbackContainerTest.java +++ b/dubbo-container/dubbo-container-logback/src/test/java/org/apache/dubbo/container/logback/LogbackContainerTest.java @@ -21,7 +21,7 @@ import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.container.Container; -import org.junit.jupiter.api.Test; +import org.junit.Test; /** * StandaloneContainerTest diff --git a/dubbo-container/dubbo-container-spring/pom.xml b/dubbo-container/dubbo-container-spring/pom.xml index 83298634f27..987b2c9d2e6 100644 --- a/dubbo-container/dubbo-container-spring/pom.xml +++ b/dubbo-container/dubbo-container-spring/pom.xml @@ -1,42 +1,42 @@ - - - 4.0.0 - - org.apache.dubbo - dubbo-container - 2.7.1-SNAPSHOT - - dubbo-container-spring - jar - ${project.artifactId} - The spring container module of dubbo project - - false - - - - org.apache.dubbo - dubbo-container-api - ${project.parent.version} - - - org.springframework - spring-context - - - + + + 4.0.0 + + org.apache.dubbo + dubbo-container + 2.7.0-SNAPSHOT + + dubbo-container-spring + jar + ${project.artifactId} + The spring container module of dubbo project + + false + + + + org.apache.dubbo + dubbo-container-api + ${project.parent.version} + + + org.springframework + spring-context + + + diff --git a/dubbo-container/dubbo-container-spring/src/main/java/org/apache/dubbo/container/spring/SpringContainer.java b/dubbo-container/dubbo-container-spring/src/main/java/org/apache/dubbo/container/spring/SpringContainer.java index 884fe098e32..c6ec4746731 100644 --- a/dubbo-container/dubbo-container-spring/src/main/java/org/apache/dubbo/container/spring/SpringContainer.java +++ b/dubbo-container/dubbo-container-spring/src/main/java/org/apache/dubbo/container/spring/SpringContainer.java @@ -19,15 +19,12 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.ConfigUtils; -import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.container.Container; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * SpringContainer. (SPI, Singleton, ThreadSafe) - * - * The container class implementation for Spring */ public class SpringContainer implements Container { @@ -43,11 +40,10 @@ public static ClassPathXmlApplicationContext getContext() { @Override public void start() { String configPath = ConfigUtils.getProperty(SPRING_CONFIG); - if (StringUtils.isEmpty(configPath)) { + if (configPath == null || configPath.length() == 0) { configPath = DEFAULT_SPRING_CONFIG; } - context = new ClassPathXmlApplicationContext(configPath.split("[,\\s]+"), false); - context.refresh(); + context = new ClassPathXmlApplicationContext(configPath.split("[,\\s]+")); context.start(); } diff --git a/dubbo-container/dubbo-container-spring/src/test/java/org/apache/dubbo/container/spring/SpringContainerTest.java b/dubbo-container/dubbo-container-spring/src/test/java/org/apache/dubbo/container/spring/SpringContainerTest.java index 05acbb8e5c1..3822da718bd 100644 --- a/dubbo-container/dubbo-container-spring/src/test/java/org/apache/dubbo/container/spring/SpringContainerTest.java +++ b/dubbo-container/dubbo-container-spring/src/test/java/org/apache/dubbo/container/spring/SpringContainerTest.java @@ -19,8 +19,8 @@ import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.container.Container; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; /** * StandaloneContainerTest @@ -31,7 +31,7 @@ public class SpringContainerTest { public void testContainer() { SpringContainer container = (SpringContainer) ExtensionLoader.getExtensionLoader(Container.class).getExtension("spring"); container.start(); - Assertions.assertEquals(SpringContainer.class, container.context.getBean("container").getClass()); + Assert.assertEquals(SpringContainer.class, container.context.getBean("container").getClass()); container.stop(); } diff --git a/dubbo-container/pom.xml b/dubbo-container/pom.xml index 932899488ec..753a0cdfa6c 100644 --- a/dubbo-container/pom.xml +++ b/dubbo-container/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-parent - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-container pom diff --git a/dubbo-demo/dubbo-demo-annotation/dubbo-demo-annotation-consumer/pom.xml b/dubbo-demo/dubbo-demo-annotation/dubbo-demo-annotation-consumer/pom.xml index 58e260ed592..1162f374a38 100644 --- a/dubbo-demo/dubbo-demo-annotation/dubbo-demo-annotation-consumer/pom.xml +++ b/dubbo-demo/dubbo-demo-annotation/dubbo-demo-annotation-consumer/pom.xml @@ -18,11 +18,13 @@ ~ under the License. --> - + dubbo-demo-annotation org.apache.dubbo - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT 4.0.0 diff --git a/dubbo-demo/dubbo-demo-annotation/dubbo-demo-annotation-provider/pom.xml b/dubbo-demo/dubbo-demo-annotation/dubbo-demo-annotation-provider/pom.xml index bf6bc8fd9df..d53c34564d7 100644 --- a/dubbo-demo/dubbo-demo-annotation/dubbo-demo-annotation-provider/pom.xml +++ b/dubbo-demo/dubbo-demo-annotation/dubbo-demo-annotation-provider/pom.xml @@ -18,11 +18,13 @@ ~ under the License. --> - + dubbo-demo-annotation org.apache.dubbo - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT 4.0.0 diff --git a/dubbo-demo/dubbo-demo-annotation/pom.xml b/dubbo-demo/dubbo-demo-annotation/pom.xml index f94870dc563..abaabf3d282 100644 --- a/dubbo-demo/dubbo-demo-annotation/pom.xml +++ b/dubbo-demo/dubbo-demo-annotation/pom.xml @@ -18,11 +18,13 @@ ~ under the License. --> - + dubbo-demo org.apache.dubbo - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT 4.0.0 pom diff --git a/dubbo-demo/dubbo-demo-api/dubbo-demo-api-consumer/pom.xml b/dubbo-demo/dubbo-demo-api/dubbo-demo-api-consumer/pom.xml index 947537667b5..f6ab74e5f09 100644 --- a/dubbo-demo/dubbo-demo-api/dubbo-demo-api-consumer/pom.xml +++ b/dubbo-demo/dubbo-demo-api/dubbo-demo-api-consumer/pom.xml @@ -18,11 +18,13 @@ ~ under the License. --> - + dubbo-demo-api org.apache.dubbo - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT 4.0.0 diff --git a/dubbo-demo/dubbo-demo-api/dubbo-demo-api-provider/pom.xml b/dubbo-demo/dubbo-demo-api/dubbo-demo-api-provider/pom.xml index d2dd9af1c85..2bcf47b0ea0 100644 --- a/dubbo-demo/dubbo-demo-api/dubbo-demo-api-provider/pom.xml +++ b/dubbo-demo/dubbo-demo-api/dubbo-demo-api-provider/pom.xml @@ -18,11 +18,13 @@ ~ under the License. --> - + dubbo-demo-api org.apache.dubbo - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT 4.0.0 diff --git a/dubbo-demo/dubbo-demo-api/pom.xml b/dubbo-demo/dubbo-demo-api/pom.xml index b9088fc4d37..325b62bc116 100644 --- a/dubbo-demo/dubbo-demo-api/pom.xml +++ b/dubbo-demo/dubbo-demo-api/pom.xml @@ -18,11 +18,13 @@ ~ under the License. --> - + dubbo-demo org.apache.dubbo - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT 4.0.0 pom diff --git a/dubbo-demo/dubbo-demo-interface/pom.xml b/dubbo-demo/dubbo-demo-interface/pom.xml index f6376c91578..d84b594d3b9 100644 --- a/dubbo-demo/dubbo-demo-interface/pom.xml +++ b/dubbo-demo/dubbo-demo-interface/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-demo - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-demo-interface jar diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/pom.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/pom.xml index 84874591535..f2bcda4bbac 100644 --- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/pom.xml +++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-demo-xml - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-demo-xml-consumer jar diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/pom.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/pom.xml index fbd0f041497..99faa90e6fc 100644 --- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/pom.xml +++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-demo-xml - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-demo-xml-provider jar diff --git a/dubbo-demo/dubbo-demo-xml/pom.xml b/dubbo-demo/dubbo-demo-xml/pom.xml index fbbcae942b8..d61536254c1 100644 --- a/dubbo-demo/dubbo-demo-xml/pom.xml +++ b/dubbo-demo/dubbo-demo-xml/pom.xml @@ -18,11 +18,13 @@ ~ under the License. --> - + dubbo-demo org.apache.dubbo - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT 4.0.0 pom diff --git a/dubbo-demo/pom.xml b/dubbo-demo/pom.xml index 77d59aebcdd..9e506066b58 100644 --- a/dubbo-demo/pom.xml +++ b/dubbo-demo/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-parent - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-demo pom diff --git a/dubbo-dependencies-bom/pom.xml b/dubbo-dependencies-bom/pom.xml index 245b6ff61fc..939492bd8e0 100644 --- a/dubbo-dependencies-bom/pom.xml +++ b/dubbo-dependencies-bom/pom.xml @@ -1,35 +1,16 @@ - - - + 4.0.0 org.apache apache - 21 - + 19 org.apache.dubbo dubbo-dependencies-bom - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT pom dubbo-dependencies-bom @@ -101,10 +82,10 @@ 4.4.6 1.2.46 3.4.13 + 0.2 4.0.1 2.12.0 2.9.0 - 1.4.2 1.3.6 3.1.15 0.8.0 @@ -126,7 +107,6 @@ 2.0 3.0.19.Final 8.5.31 - 0.3.0 1.7.25 1.2 @@ -138,11 +118,8 @@ 2.2.7 1.2.0 - 3.2.5 + 3.2.3 1.5.19 - 4.3.16.RELEASE - - 2.0.1 2.8.5 @@ -150,7 +127,7 @@ -org.springframework + org.springframework spring-framework-bom ${spring_version} pom @@ -200,12 +177,11 @@ org.apache.zookeeper zookeeper ${zookeeper_version} - - - io.netty - netty - - + + + com.101tec + zkclient + ${zkclient_version} org.apache.curator @@ -223,11 +199,6 @@ jedis ${jedis_version} - - com.ecwid.consul - consul-api - ${consul_version} - com.googlecode.xmemcached xmemcached @@ -369,21 +340,6 @@ tomcat-embed-logging-juli ${tomcat_embed_version} - - io.etcd - jetcd-core - ${jetcd_version} - - - io.netty - netty-codec-http2 - - - io.netty - netty-handler-proxy - - - org.slf4j @@ -482,28 +438,6 @@ ${swagger_version} - - com.alibaba.middleware - metrics-core-api - ${metrics_version} - - - com.alibaba.middleware - metrics-core-impl - ${metrics_version} - - - com.alibaba.middleware - metrics-common - ${metrics_version} - - - - com.alibaba.middleware - metrics-rest - ${metrics_version} - - org.apache.curator @@ -523,13 +457,6 @@ ${embedded_redis_version} test - - org.springframework - spring-test - ${spring_test_version} - test - - com.google.code.gson gson diff --git a/dubbo-dependencies/dubbo-dependencies-zookeeper/pom.xml b/dubbo-dependencies/dubbo-dependencies-zookeeper/pom.xml deleted file mode 100644 index 18a3f2abcc7..00000000000 --- a/dubbo-dependencies/dubbo-dependencies-zookeeper/pom.xml +++ /dev/null @@ -1,81 +0,0 @@ - - - - - 4.0.0 - - - org.apache - apache - 21 - - - - org.apache.dubbo - dubbo-dependencies-zookeeper - 2.7.1-SNAPSHOT - pom - - - - - org.apache.dubbo - dubbo-dependencies-bom - 2.7.1-SNAPSHOT - pom - import - - - - - - - org.apache.curator - curator-recipes - - - org.apache.zookeeper - zookeeper - - - - - - release - - - - org.apache.maven.plugins - maven-gpg-plugin - - - verify - - sign - - - - - - - - - - diff --git a/dubbo-dependencies/pom.xml b/dubbo-dependencies/pom.xml deleted file mode 100644 index 6d144449d5f..00000000000 --- a/dubbo-dependencies/pom.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - - - - dubbo-parent - org.apache.dubbo - 2.7.1-SNAPSHOT - - 4.0.0 - - dubbo-dependencies - pom - - dubbo-dependencies-zookeeper - - - - diff --git a/dubbo-distribution/pom.xml b/dubbo-distribution/pom.xml index b018cec4e50..c5c6f0a2af6 100644 --- a/dubbo-distribution/pom.xml +++ b/dubbo-distribution/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-parent - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-distribution pom @@ -260,7 +260,7 @@ release - apache-dubbo-incubating-${project.version} + dubbo-incubating-${project.version} maven-assembly-plugin diff --git a/dubbo-distribution/src/assembly/source-release.xml b/dubbo-distribution/src/assembly/source-release.xml index 434377bdd3a..bd85f58b119 100644 --- a/dubbo-distribution/src/assembly/source-release.xml +++ b/dubbo-distribution/src/assembly/source-release.xml @@ -46,10 +46,6 @@ **/*.log release.properties **/*.xml.* - **/*.patch - **/.mvn/** - **/*.jar - **/mvnw* diff --git a/dubbo-filter/dubbo-filter-cache/pom.xml b/dubbo-filter/dubbo-filter-cache/pom.xml index bec6067478d..43f72b36ef5 100644 --- a/dubbo-filter/dubbo-filter-cache/pom.xml +++ b/dubbo-filter/dubbo-filter-cache/pom.xml @@ -1,48 +1,48 @@ - - - 4.0.0 - - org.apache.dubbo - dubbo-filter - 2.7.1-SNAPSHOT - - dubbo-filter-cache - jar - ${project.artifactId} - The cache module of dubbo project - - false - - - - org.apache.dubbo - dubbo-rpc-api - ${project.parent.version} - - - javax.cache - cache-api - - - com.hazelcast - hazelcast - test - ${hazelcast_version} - - + + + 4.0.0 + + org.apache.dubbo + dubbo-filter + 2.7.0-SNAPSHOT + + dubbo-filter-cache + jar + ${project.artifactId} + The cache module of dubbo project + + false + + + + org.apache.dubbo + dubbo-rpc-api + ${project.parent.version} + + + javax.cache + cache-api + + + com.hazelcast + hazelcast + test + ${hazelcast_version} + + \ No newline at end of file diff --git a/dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/support/jcache/JCache.java b/dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/support/jcache/JCache.java index 4be16c84b3e..92e16423e3d 100644 --- a/dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/support/jcache/JCache.java +++ b/dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/support/jcache/JCache.java @@ -18,7 +18,6 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.utils.StringUtils; import javax.cache.Cache; import javax.cache.CacheException; @@ -50,7 +49,7 @@ public JCache(URL url) { // jcache parameter is the full-qualified class name of SPI implementation String type = url.getParameter("jcache"); - CachingProvider provider = StringUtils.isEmpty(type) ? Caching.getCachingProvider() : Caching.getCachingProvider(type); + CachingProvider provider = type == null || type.length() == 0 ? Caching.getCachingProvider() : Caching.getCachingProvider(type); CacheManager cacheManager = provider.getCacheManager(); Cache cache = cacheManager.getCache(key); if (cache == null) { diff --git a/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/filter/CacheFilterTest.java b/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/filter/CacheFilterTest.java index c64647309f0..7d571dd6442 100644 --- a/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/filter/CacheFilterTest.java +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/filter/CacheFilterTest.java @@ -25,17 +25,20 @@ import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.RpcInvocation; import org.apache.dubbo.rpc.RpcResult; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -import java.util.stream.Stream; +import java.util.Arrays; +import java.util.List; +import static org.junit.runners.Parameterized.Parameters; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; +@RunWith(Parameterized.class) public class CacheFilterTest { private RpcInvocation invocation; private CacheFilter cacheFilter = new CacheFilter(); @@ -44,21 +47,30 @@ public class CacheFilterTest { private Invoker invoker2 = mock(Invoker.class); private Invoker invoker3 = mock(Invoker.class); private Invoker invoker4 = mock(Invoker.class); + private String cacheType; + private CacheFactory cacheFactory; + + public CacheFilterTest(String cacheType, CacheFactory cacheFactory) { + this.cacheType = cacheType; + this.cacheFactory = cacheFactory; + } - static Stream cacheFactories() { - return Stream.of( - Arguments.of("lru", new LruCacheFactory()), - Arguments.of("jcache", new JCacheFactory()), - Arguments.of("threadlocal", new ThreadLocalCacheFactory()), - Arguments.of("expiring", new ExpiringCacheFactory()) - ); + @Parameters + public static List cacheFactories() { + return Arrays.asList(new Object[][]{ + {"lru", new LruCacheFactory()}, + {"jcache", new JCacheFactory()}, + {"threadlocal", new ThreadLocalCacheFactory()}, + {"expiring", new ExpiringCacheFactory()} + }); } - public void setUp(String cacheType, CacheFactory cacheFactory) { + @Before + public void setUp() throws Exception { invocation = new RpcInvocation(); - cacheFilter.setCacheFactory(cacheFactory); + cacheFilter.setCacheFactory(this.cacheFactory); - URL url = URL.valueOf("test://test:11/test?cache=" + cacheType); + URL url = URL.valueOf("test://test:11/test?cache=" + this.cacheType); given(invoker.invoke(invocation)).willReturn(new RpcResult("value")); given(invoker.getUrl()).willReturn(url); @@ -76,10 +88,8 @@ public void setUp(String cacheType, CacheFactory cacheFactory) { given(invoker4.getUrl()).willReturn(url); } - @ParameterizedTest - @MethodSource("cacheFactories") - public void testNonArgsMethod(String cacheType, CacheFactory cacheFactory) { - setUp(cacheType, cacheFactory); + @Test + public void testNonArgsMethod() { invocation.setMethodName("echo"); invocation.setParameterTypes(new Class[]{}); invocation.setArguments(new Object[]{}); @@ -87,14 +97,12 @@ public void testNonArgsMethod(String cacheType, CacheFactory cacheFactory) { cacheFilter.invoke(invoker, invocation); RpcResult rpcResult1 = (RpcResult) cacheFilter.invoke(invoker1, invocation); RpcResult rpcResult2 = (RpcResult) cacheFilter.invoke(invoker2, invocation); - Assertions.assertEquals(rpcResult1.getValue(), rpcResult2.getValue()); - Assertions.assertEquals(rpcResult1.getValue(), "value"); + Assert.assertEquals(rpcResult1.getValue(), rpcResult2.getValue()); + Assert.assertEquals(rpcResult1.getValue(), "value"); } - @ParameterizedTest - @MethodSource("cacheFactories") - public void testMethodWithArgs(String cacheType, CacheFactory cacheFactory) { - setUp(cacheType, cacheFactory); + @Test + public void testMethodWithArgs() { invocation.setMethodName("echo1"); invocation.setParameterTypes(new Class[]{String.class}); invocation.setArguments(new Object[]{"arg1"}); @@ -102,27 +110,23 @@ public void testMethodWithArgs(String cacheType, CacheFactory cacheFactory) { cacheFilter.invoke(invoker, invocation); RpcResult rpcResult1 = (RpcResult) cacheFilter.invoke(invoker1, invocation); RpcResult rpcResult2 = (RpcResult) cacheFilter.invoke(invoker2, invocation); - Assertions.assertEquals(rpcResult1.getValue(), rpcResult2.getValue()); - Assertions.assertEquals(rpcResult1.getValue(), "value"); + Assert.assertEquals(rpcResult1.getValue(), rpcResult2.getValue()); + Assert.assertEquals(rpcResult1.getValue(), "value"); } - @ParameterizedTest - @MethodSource("cacheFactories") - public void testException(String cacheType, CacheFactory cacheFactory) { - setUp(cacheType, cacheFactory); + @Test + public void testException() { invocation.setMethodName("echo1"); invocation.setParameterTypes(new Class[]{String.class}); invocation.setArguments(new Object[]{"arg2"}); cacheFilter.invoke(invoker3, invocation); RpcResult rpcResult = (RpcResult) cacheFilter.invoke(invoker2, invocation); - Assertions.assertEquals(rpcResult.getValue(), "value2"); + Assert.assertEquals(rpcResult.getValue(), "value2"); } - @ParameterizedTest - @MethodSource("cacheFactories") - public void testNull(String cacheType, CacheFactory cacheFactory) { - setUp(cacheType, cacheFactory); + @Test + public void testNull() { invocation.setMethodName("echo1"); invocation.setParameterTypes(new Class[]{String.class}); invocation.setArguments(new Object[]{"arg3"}); @@ -130,7 +134,7 @@ public void testNull(String cacheType, CacheFactory cacheFactory) { cacheFilter.invoke(invoker4, invocation); RpcResult rpcResult1 = (RpcResult) cacheFilter.invoke(invoker1, invocation); RpcResult rpcResult2 = (RpcResult) cacheFilter.invoke(invoker2, invocation); - Assertions.assertEquals(rpcResult1.getValue(), null); - Assertions.assertEquals(rpcResult2.getValue(), null); + Assert.assertEquals(rpcResult1.getValue(), null); + Assert.assertEquals(rpcResult2.getValue(), null); } } diff --git a/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/support/expiring/ExpiringCacheFactoryTest.java b/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/support/expiring/ExpiringCacheFactoryTest.java index 23484c6c327..0deb996ad5b 100644 --- a/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/support/expiring/ExpiringCacheFactoryTest.java +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/support/expiring/ExpiringCacheFactoryTest.java @@ -19,10 +19,10 @@ import org.apache.dubbo.cache.Cache; import org.apache.dubbo.cache.support.AbstractCacheFactory; import org.apache.dubbo.cache.support.AbstractCacheFactoryTest; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.hamcrest.core.Is.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class ExpiringCacheFactoryTest extends AbstractCacheFactoryTest { @Test diff --git a/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/support/jcache/JCacheFactoryTest.java b/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/support/jcache/JCacheFactoryTest.java index 688e2832ca1..6dbd15d6f78 100644 --- a/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/support/jcache/JCacheFactoryTest.java +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/support/jcache/JCacheFactoryTest.java @@ -22,11 +22,11 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.RpcInvocation; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.hamcrest.core.Is.is; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; public class JCacheFactoryTest extends AbstractCacheFactoryTest { diff --git a/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/support/lru/LruCacheFactoryTest.java b/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/support/lru/LruCacheFactoryTest.java index 149c1227fd7..f266b42f8be 100644 --- a/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/support/lru/LruCacheFactoryTest.java +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/support/lru/LruCacheFactoryTest.java @@ -19,10 +19,10 @@ import org.apache.dubbo.cache.Cache; import org.apache.dubbo.cache.support.AbstractCacheFactory; import org.apache.dubbo.cache.support.AbstractCacheFactoryTest; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.hamcrest.core.Is.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class LruCacheFactoryTest extends AbstractCacheFactoryTest{ @Test diff --git a/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/support/threadlocal/ThreadLocalCacheFactoryTest.java b/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/support/threadlocal/ThreadLocalCacheFactoryTest.java index 800f5ad6284..542fdc55513 100644 --- a/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/support/threadlocal/ThreadLocalCacheFactoryTest.java +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/support/threadlocal/ThreadLocalCacheFactoryTest.java @@ -19,10 +19,10 @@ import org.apache.dubbo.cache.Cache; import org.apache.dubbo.cache.support.AbstractCacheFactory; import org.apache.dubbo.cache.support.AbstractCacheFactoryTest; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.hamcrest.core.Is.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class ThreadLocalCacheFactoryTest extends AbstractCacheFactoryTest { @Test diff --git a/dubbo-filter/dubbo-filter-validation/pom.xml b/dubbo-filter/dubbo-filter-validation/pom.xml index 9ce481061d2..9cebd4b805e 100644 --- a/dubbo-filter/dubbo-filter-validation/pom.xml +++ b/dubbo-filter/dubbo-filter-validation/pom.xml @@ -1,72 +1,72 @@ - - - 4.0.0 - - org.apache.dubbo - dubbo-filter - 2.7.1-SNAPSHOT - - dubbo-filter-validation - jar - ${project.artifactId} - The validation module of dubbo project - - false - - - - org.apache.dubbo - dubbo-rpc-api - ${project.parent.version} - - - javax.validation - validation-api - - - org.hibernate - hibernate-validator - test - ${hibernate_validator_version} - - - javax.el - javax.el-api - test - ${el_api_version} - - - javax.xml.bind - jaxb-api - test - ${jaxb_api_version} - - - com.sun.xml.bind - jaxb-impl - test - ${jaxb_api_version} - - - com.sun.xml.bind - jaxb-core - test - ${jaxb_api_version} - - + + + 4.0.0 + + org.apache.dubbo + dubbo-filter + 2.7.0-SNAPSHOT + + dubbo-filter-validation + jar + ${project.artifactId} + The validation module of dubbo project + + false + + + + org.apache.dubbo + dubbo-rpc-api + ${project.parent.version} + + + javax.validation + validation-api + + + org.hibernate + hibernate-validator + test + ${hibernate_validator_version} + + + javax.el + javax.el-api + test + ${el_api_version} + + + javax.xml.bind + jaxb-api + test + ${jaxb_api_version} + + + com.sun.xml.bind + jaxb-impl + test + ${jaxb_api_version} + + + com.sun.xml.bind + jaxb-core + test + ${jaxb_api_version} + + \ No newline at end of file diff --git a/dubbo-filter/dubbo-filter-validation/src/test/java/org/apache/dubbo/validation/filter/ValidationFilterTest.java b/dubbo-filter/dubbo-filter-validation/src/test/java/org/apache/dubbo/validation/filter/ValidationFilterTest.java index cade5936402..fe0f10cab79 100644 --- a/dubbo-filter/dubbo-filter-validation/src/test/java/org/apache/dubbo/validation/filter/ValidationFilterTest.java +++ b/dubbo-filter/dubbo-filter-validation/src/test/java/org/apache/dubbo/validation/filter/ValidationFilterTest.java @@ -17,17 +17,11 @@ package org.apache.dubbo.validation.filter; import org.apache.dubbo.common.URL; -import org.apache.dubbo.rpc.Invoker; -import org.apache.dubbo.rpc.Result; -import org.apache.dubbo.rpc.RpcException; -import org.apache.dubbo.rpc.RpcInvocation; -import org.apache.dubbo.rpc.RpcResult; +import org.apache.dubbo.rpc.*; import org.apache.dubbo.validation.Validation; import org.apache.dubbo.validation.Validator; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Before; +import org.junit.Test; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; @@ -42,7 +36,7 @@ public class ValidationFilterTest { private ValidationFilter validationFilter; - @BeforeEach + @Before public void setUp() throws Exception { this.validationFilter = new ValidationFilter(); } @@ -118,20 +112,18 @@ public void testItWhileMethodNameStartWithDollar() throws Exception { } - @Test + @Test(expected = RpcException.class) public void testItWhileThrowoutRpcException() throws Exception { - Assertions.assertThrows(RpcException.class, () -> { - URL url = URL.valueOf("test://test:11/test?default.validation=true"); - - given(validation.getValidator(url)).willThrow(new RpcException("rpc exception")); - given(invoker.invoke(invocation)).willReturn(new RpcResult("success")); - given(invoker.getUrl()).willReturn(url); - given(invocation.getMethodName()).willReturn("echo1"); - given(invocation.getParameterTypes()).willReturn(new Class[]{String.class}); - given(invocation.getArguments()).willReturn(new Object[]{"arg1"}); - - validationFilter.setValidation(validation); - validationFilter.invoke(invoker, invocation); - }); + URL url = URL.valueOf("test://test:11/test?default.validation=true"); + + given(validation.getValidator(url)).willThrow(new RpcException("rpc exception")); + given(invoker.invoke(invocation)).willReturn(new RpcResult("success")); + given(invoker.getUrl()).willReturn(url); + given(invocation.getMethodName()).willReturn("echo1"); + given(invocation.getParameterTypes()).willReturn(new Class[]{String.class}); + given(invocation.getArguments()).willReturn(new Object[]{"arg1"}); + + validationFilter.setValidation(validation); + validationFilter.invoke(invoker, invocation); } } \ No newline at end of file diff --git a/dubbo-filter/dubbo-filter-validation/src/test/java/org/apache/dubbo/validation/support/jvalidation/JValidationTest.java b/dubbo-filter/dubbo-filter-validation/src/test/java/org/apache/dubbo/validation/support/jvalidation/JValidationTest.java index 4531efd0fd9..4a7cda63017 100644 --- a/dubbo-filter/dubbo-filter-validation/src/test/java/org/apache/dubbo/validation/support/jvalidation/JValidationTest.java +++ b/dubbo-filter/dubbo-filter-validation/src/test/java/org/apache/dubbo/validation/support/jvalidation/JValidationTest.java @@ -19,30 +19,24 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.validation.Validation; import org.apache.dubbo.validation.Validator; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; import javax.validation.ValidationException; -import java.io.NotSerializableException; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; public class JValidationTest { - @Test - public void testReturnTypeWithInvalidValidationProvider() { - Assertions.assertThrows(ValidationException.class, () -> { - Validation jValidation = new JValidation(); - URL url = URL.valueOf("test://test:11/org.apache.dubbo.validation.support.jvalidation.JValidation?" + - "jvalidation=org.apache.dubbo.validation.Validation"); - jValidation.getValidator(url); - }); - + @Test(expected = ValidationException.class) + public void testReturnTypeWithInvalidValidationProvider() throws Exception { + Validation jValidation = new JValidation(); + URL url = URL.valueOf("test://test:11/org.apache.dubbo.validation.support.jvalidation.JValidation?" + + "jvalidation=org.apache.dubbo.validation.Validation"); + jValidation.getValidator(url); } @Test - public void testReturnTypeWithDefaultValidatorProvider() { + public void testReturnTypeWithDefaultValidatorProvider() throws Exception { Validation jValidation = new JValidation(); URL url = URL.valueOf("test://test:11/org.apache.dubbo.validation.support.jvalidation.JValidation"); Validator validator = jValidation.getValidator(url); diff --git a/dubbo-filter/dubbo-filter-validation/src/test/java/org/apache/dubbo/validation/support/jvalidation/JValidatorTest.java b/dubbo-filter/dubbo-filter-validation/src/test/java/org/apache/dubbo/validation/support/jvalidation/JValidatorTest.java index a45ea96992c..1bff36e863a 100644 --- a/dubbo-filter/dubbo-filter-validation/src/test/java/org/apache/dubbo/validation/support/jvalidation/JValidatorTest.java +++ b/dubbo-filter/dubbo-filter-validation/src/test/java/org/apache/dubbo/validation/support/jvalidation/JValidatorTest.java @@ -18,20 +18,16 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.validation.support.jvalidation.mock.ValidationParameter; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; import javax.validation.ConstraintViolationException; public class JValidatorTest { - @Test + @Test(expected = NoSuchMethodException.class) public void testItWithNonExistMethod() throws Exception { - Assertions.assertThrows(NoSuchMethodException.class, () -> { - URL url = URL.valueOf("test://test:11/org.apache.dubbo.validation.support.jvalidation.mock.JValidatorTestTarget"); - JValidator jValidator = new JValidator(url); - jValidator.validate("nonExistingMethod", new Class[]{String.class}, new Object[]{"arg1"}); - }); + URL url = URL.valueOf("test://test:11/org.apache.dubbo.validation.support.jvalidation.mock.JValidatorTestTarget"); + JValidator jValidator = new JValidator(url); + jValidator.validate("nonExistingMethod", new Class[]{String.class}, new Object[]{"arg1"}); } @Test @@ -41,13 +37,11 @@ public void testItWithExistMethod() throws Exception { jValidator.validate("someMethod1", new Class[]{String.class}, new Object[]{"anything"}); } - @Test + @Test(expected = ConstraintViolationException.class) public void testItWhenItViolatedConstraint() throws Exception { - Assertions.assertThrows(ConstraintViolationException.class, () -> { - URL url = URL.valueOf("test://test:11/org.apache.dubbo.validation.support.jvalidation.mock.JValidatorTestTarget"); - JValidator jValidator = new JValidator(url); - jValidator.validate("someMethod2", new Class[]{ValidationParameter.class}, new Object[]{new ValidationParameter()}); - }); + URL url = URL.valueOf("test://test:11/org.apache.dubbo.validation.support.jvalidation.mock.JValidatorTestTarget"); + JValidator jValidator = new JValidator(url); + jValidator.validate("someMethod2", new Class[]{ValidationParameter.class}, new Object[]{new ValidationParameter()}); } @Test diff --git a/dubbo-filter/pom.xml b/dubbo-filter/pom.xml index f9851412296..7b204977106 100644 --- a/dubbo-filter/pom.xml +++ b/dubbo-filter/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-parent - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-filter pom diff --git a/dubbo-metadata-report/dubbo-metadata-definition/pom.xml b/dubbo-metadata-report/dubbo-metadata-definition/pom.xml index 09dc860fb60..8311b28c4a0 100644 --- a/dubbo-metadata-report/dubbo-metadata-definition/pom.xml +++ b/dubbo-metadata-report/dubbo-metadata-definition/pom.xml @@ -14,11 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. --> - + dubbo-metadata-report org.apache.dubbo - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT 4.0.0 diff --git a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/TypeDefinitionBuilder.java b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/TypeDefinitionBuilder.java index 53e8c88b8f3..797d9f8a0e5 100755 --- a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/TypeDefinitionBuilder.java +++ b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/TypeDefinitionBuilder.java @@ -26,7 +26,6 @@ import java.lang.reflect.Type; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -36,16 +35,20 @@ */ public class TypeDefinitionBuilder { - private static final TypeBuilder ARRAY_BUILDER = new ArrayTypeBuilder(); - private static final TypeBuilder COLLECTION_BUILDER = new CollectionTypeBuilder(); - private static final TypeBuilder MAP_BUILDER = new MapTypeBuilder(); - private static final TypeBuilder ENUM_BUILDER = new EnumTypeBuilder(); + private static final ThreadLocal> builders; - private static final List BUILDERS = Arrays.asList(ARRAY_BUILDER, COLLECTION_BUILDER, MAP_BUILDER, ENUM_BUILDER); + static { + builders = new ThreadLocal>(); + builders.set(new ArrayList()); + builders.get().add(new ArrayTypeBuilder()); + builders.get().add(new CollectionTypeBuilder()); + builders.get().add(new MapTypeBuilder()); + builders.get().add(new EnumTypeBuilder()); + } public static TypeDefinition build(Type type, Class clazz, Map, TypeDefinition> typeCache) { TypeBuilder builder = getGenericTypeBuilder(type, clazz); - TypeDefinition td; + TypeDefinition td = null; if (builder != null) { td = builder.build(type, clazz, typeCache); } else { @@ -55,7 +58,7 @@ public static TypeDefinition build(Type type, Class clazz, Map, Type } private static TypeBuilder getGenericTypeBuilder(Type type, Class clazz) { - for (TypeBuilder builder : BUILDERS) { + for (TypeBuilder builder : builders.get()) { if (builder.accept(type, clazz)) { return builder; } @@ -63,14 +66,14 @@ private static TypeBuilder getGenericTypeBuilder(Type type, Class clazz) { return null; } - private Map, TypeDefinition> typeCache = new HashMap<>(); + private Map, TypeDefinition> typeCache = new HashMap, TypeDefinition>(); public TypeDefinition build(Type type, Class clazz) { return build(type, clazz, typeCache); } public List getTypeDefinitions() { - return new ArrayList<>(typeCache.values()); + return new ArrayList(typeCache.values()); } } diff --git a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/ArrayTypeBuilder.java b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/ArrayTypeBuilder.java index 9ad61cefb26..ce53c708682 100755 --- a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/ArrayTypeBuilder.java +++ b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/ArrayTypeBuilder.java @@ -32,7 +32,12 @@ public boolean accept(Type type, Class clazz) { if (clazz == null) { return false; } - return clazz.isArray(); + + if (clazz.isArray()) { + return true; + } + + return false; } @Override @@ -42,7 +47,8 @@ public TypeDefinition build(Type type, Class clazz, Map, TypeDefinit TypeDefinitionBuilder.build(componentType, componentType, typeCache); final String canonicalName = clazz.getCanonicalName(); - return new TypeDefinition(canonicalName); + TypeDefinition td = new TypeDefinition(canonicalName); + return td; } } diff --git a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/CollectionTypeBuilder.java b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/CollectionTypeBuilder.java index efdf239b209..14c3b4ba68c 100755 --- a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/CollectionTypeBuilder.java +++ b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/CollectionTypeBuilder.java @@ -22,21 +22,26 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.text.MessageFormat; -import java.util.Arrays; import java.util.Collection; import java.util.Map; /** * 2015/1/27. */ -public class CollectionTypeBuilder implements TypeBuilder { +public class +CollectionTypeBuilder implements TypeBuilder { @Override public boolean accept(Type type, Class clazz) { if (clazz == null) { return false; } - return Collection.class.isAssignableFrom(clazz); + + if (Collection.class.isAssignableFrom(clazz)) { + return true; + } + + return false; } @Override @@ -49,7 +54,7 @@ public TypeDefinition build(Type type, Class clazz, Map, TypeDefinit Type[] actualTypeArgs = parameterizedType.getActualTypeArguments(); if (actualTypeArgs == null || actualTypeArgs.length != 1) { throw new IllegalArgumentException(MessageFormat.format( - "[ServiceDefinitionBuilder] Collection type [{0}] with unexpected amount of arguments [{1}]." + Arrays.toString(actualTypeArgs), + "[ServiceDefinitionBuilder] Collection type [{0}] with unexpected amount of arguments [{1}]." + actualTypeArgs, new Object[]{type, actualTypeArgs})); } @@ -67,7 +72,8 @@ public TypeDefinition build(Type type, Class clazz, Map, TypeDefinit } } - return new TypeDefinition(type.toString()); + TypeDefinition td = new TypeDefinition(type.toString()); + return td; } } diff --git a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/EnumTypeBuilder.java b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/EnumTypeBuilder.java index d0dea96302c..cca8c9ca81d 100755 --- a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/EnumTypeBuilder.java +++ b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/EnumTypeBuilder.java @@ -32,7 +32,12 @@ public boolean accept(Type type, Class clazz) { if (clazz == null) { return false; } - return clazz.isEnum(); + + if (clazz.isEnum()) { + return true; + } + + return false; } @Override diff --git a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/MapTypeBuilder.java b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/MapTypeBuilder.java index 771e421928a..4c7cf93c866 100755 --- a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/MapTypeBuilder.java +++ b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/MapTypeBuilder.java @@ -22,7 +22,6 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.text.MessageFormat; -import java.util.Arrays; import java.util.Map; /** @@ -35,7 +34,12 @@ public boolean accept(Type type, Class clazz) { if (clazz == null) { return false; } - return Map.class.isAssignableFrom(clazz); + + if (Map.class.isAssignableFrom(clazz)) { + return true; + } + + return false; } @Override @@ -48,7 +52,7 @@ public TypeDefinition build(Type type, Class clazz, Map, TypeDefinit Type[] actualTypeArgs = parameterizedType.getActualTypeArguments(); if (actualTypeArgs == null || actualTypeArgs.length != 2) { throw new IllegalArgumentException(MessageFormat.format( - "[ServiceDefinitionBuilder] Map type [{0}] with unexpected amount of arguments [{1}]." + Arrays.toString(actualTypeArgs), new Object[]{ + "[ServiceDefinitionBuilder] Map type [{0}] with unexpected amount of arguments [{1}]." + actualTypeArgs, new Object[]{ type, actualTypeArgs})); } diff --git a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/model/MethodDefinition.java b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/model/MethodDefinition.java index 3746ab5b9bd..c1e154840d6 100755 --- a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/model/MethodDefinition.java +++ b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/model/MethodDefinition.java @@ -74,12 +74,8 @@ public String toString() { @Override public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof MethodDefinition)) { - return false; - } + if (this == o) return true; + if (!(o instanceof MethodDefinition)) return false; MethodDefinition that = (MethodDefinition) o; return Objects.equals(getName(), that.getName()) && Arrays.equals(getParameterTypes(), that.getParameterTypes()) && diff --git a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/model/ServiceDefinition.java b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/model/ServiceDefinition.java index 78d3bcb6f66..f94e4d5704c 100755 --- a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/model/ServiceDefinition.java +++ b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/model/ServiceDefinition.java @@ -80,12 +80,8 @@ public String toString() { @Override public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof ServiceDefinition)) { - return false; - } + if (this == o) return true; + if (!(o instanceof ServiceDefinition)) return false; ServiceDefinition that = (ServiceDefinition) o; return Objects.equals(getCanonicalName(), that.getCanonicalName()) && Objects.equals(getCodeSource(), that.getCodeSource()) && diff --git a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/model/TypeDefinition.java b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/model/TypeDefinition.java index 7ca5bd894aa..f4cc145f8d6 100755 --- a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/model/TypeDefinition.java +++ b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/model/TypeDefinition.java @@ -106,12 +106,8 @@ public String toString() { @Override public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof TypeDefinition)) { - return false; - } + if (this == o) return true; + if (!(o instanceof TypeDefinition)) return false; TypeDefinition that = (TypeDefinition) o; return Objects.equals(getId(), that.getId()) && Objects.equals(getType(), that.getType()) && diff --git a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/util/JaketConfigurationUtils.java b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/util/JaketConfigurationUtils.java index a21de3f5dc4..3a7dda19889 100755 --- a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/util/JaketConfigurationUtils.java +++ b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/util/JaketConfigurationUtils.java @@ -16,8 +16,6 @@ */ package org.apache.dubbo.metadata.definition.util; -import org.apache.dubbo.common.utils.StringUtils; - import java.io.InputStream; import java.util.Properties; @@ -38,17 +36,17 @@ public class JaketConfigurationUtils { try { props.load(inStream); String value = (String) props.get("included_interface_packages"); - if (StringUtils.isNotEmpty(value)) { + if (value != null && !value.isEmpty()) { includedInterfacePackages = value.split(","); } value = props.getProperty("included_type_packages"); - if (StringUtils.isNotEmpty(value)) { + if (value != null && !value.isEmpty()) { includedTypePackages = value.split(","); } value = props.getProperty("closed_types"); - if (StringUtils.isNotEmpty(value)) { + if (value != null && !value.isEmpty()) { closedTypes = value.split(","); } diff --git a/dubbo-metadata-report/dubbo-metadata-definition/src/test/java/org/apache/dubbo/metadata/definition/MetadataTest.java b/dubbo-metadata-report/dubbo-metadata-definition/src/test/java/org/apache/dubbo/metadata/definition/MetadataTest.java index c0be3dcbd90..00f476194c7 100644 --- a/dubbo-metadata-report/dubbo-metadata-definition/src/test/java/org/apache/dubbo/metadata/definition/MetadataTest.java +++ b/dubbo-metadata-report/dubbo-metadata-definition/src/test/java/org/apache/dubbo/metadata/definition/MetadataTest.java @@ -25,8 +25,8 @@ import org.apache.dubbo.metadata.definition.model.TypeDefinition; import com.google.gson.Gson; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import junit.framework.TestCase; +import org.junit.Test; /** * TypeDefinitionBuilder @@ -43,15 +43,15 @@ public void testInnerClassType() { TypeDefinition td = builder.build(OuterClass.InnerClass.class, OuterClass.InnerClass.class); System.out.println(">> testInnerClassType: " + new Gson().toJson(td)); - Assertions.assertEquals("org.apache.dubbo.metadata.definition.common.OuterClass$InnerClass", td.getType()); - Assertions.assertEquals(1, td.getProperties().size()); - Assertions.assertNotNull(td.getProperties().get("name")); + TestCase.assertEquals("org.apache.dubbo.metadata.definition.common.OuterClass$InnerClass", td.getType()); + TestCase.assertEquals(1, td.getProperties().size()); + TestCase.assertNotNull(td.getProperties().get("name")); ServiceDefinition sd = MetadataUtils.generateMetadata(TestService.class); System.out.println(">> testInnerClassType: " + new Gson().toJson(sd)); - Assertions.assertEquals(TestService.class.getName(), sd.getCanonicalName()); - Assertions.assertEquals(TestService.class.getMethods().length, sd.getMethods().size()); + TestCase.assertEquals(TestService.class.getName(), sd.getCanonicalName()); + TestCase.assertEquals(TestService.class.getMethods().length, sd.getMethods().size()); boolean containsType = false; for (TypeDefinition type : sd.getTypes()) { if (type.getType().equals("org.apache.dubbo.metadata.definition.common.OuterClass$InnerClass")) { @@ -59,7 +59,7 @@ public void testInnerClassType() { break; } } - Assertions.assertTrue(containsType); + TestCase.assertTrue(containsType); } /** @@ -70,16 +70,16 @@ public void testRawMap() { TypeDefinition td = builder.build(ResultWithRawCollections.class, ResultWithRawCollections.class); System.out.println(">> testRawMap: " + new Gson().toJson(td)); - Assertions.assertEquals("org.apache.dubbo.metadata.definition.common.ResultWithRawCollections", td.getType()); - Assertions.assertEquals(2, td.getProperties().size()); - Assertions.assertEquals("java.util.Map", td.getProperties().get("map").getType()); - Assertions.assertEquals("java.util.List", td.getProperties().get("list").getType()); + TestCase.assertEquals("org.apache.dubbo.metadata.definition.common.ResultWithRawCollections", td.getType()); + TestCase.assertEquals(2, td.getProperties().size()); + TestCase.assertEquals("java.util.Map", td.getProperties().get("map").getType()); + TestCase.assertEquals("java.util.List", td.getProperties().get("list").getType()); ServiceDefinition sd = MetadataUtils.generateMetadata(TestService.class); System.out.println(">> testRawMap: " + new Gson().toJson(sd)); - Assertions.assertEquals(TestService.class.getName(), sd.getCanonicalName()); - Assertions.assertEquals(TestService.class.getMethods().length, sd.getMethods().size()); + TestCase.assertEquals(TestService.class.getName(), sd.getCanonicalName()); + TestCase.assertEquals(TestService.class.getMethods().length, sd.getMethods().size()); boolean containsType = false; for (TypeDefinition type : sd.getTypes()) { if (type.getType().equals("org.apache.dubbo.metadata.definition.common.ResultWithRawCollections")) { @@ -87,7 +87,7 @@ public void testRawMap() { break; } } - Assertions.assertTrue(containsType); + TestCase.assertTrue(containsType); } @Test @@ -96,17 +96,17 @@ public void testEnum() { TypeDefinition td = builder.build(ColorEnum.class, ColorEnum.class); System.out.println(">> testEnum: " + new Gson().toJson(td)); - Assertions.assertEquals("org.apache.dubbo.metadata.definition.common.ColorEnum", td.getType()); - Assertions.assertEquals(3, td.getEnums().size()); - Assertions.assertTrue(td.getEnums().contains("RED")); - Assertions.assertTrue(td.getEnums().contains("YELLOW")); - Assertions.assertTrue(td.getEnums().contains("BLUE")); + TestCase.assertEquals("org.apache.dubbo.metadata.definition.common.ColorEnum", td.getType()); + TestCase.assertEquals(3, td.getEnums().size()); + TestCase.assertTrue(td.getEnums().contains("RED")); + TestCase.assertTrue(td.getEnums().contains("YELLOW")); + TestCase.assertTrue(td.getEnums().contains("BLUE")); ServiceDefinition sd = MetadataUtils.generateMetadata(TestService.class); System.out.println(">> testEnum: " + new Gson().toJson(sd)); - Assertions.assertEquals(TestService.class.getName(), sd.getCanonicalName()); - Assertions.assertEquals(TestService.class.getMethods().length, sd.getMethods().size()); + TestCase.assertEquals(TestService.class.getName(), sd.getCanonicalName()); + TestCase.assertEquals(TestService.class.getMethods().length, sd.getMethods().size()); boolean containsType = false; for (TypeDefinition type : sd.getTypes()) { if (type.getType().equals("org.apache.dubbo.metadata.definition.common.ColorEnum")) { @@ -114,7 +114,7 @@ public void testEnum() { break; } } - Assertions.assertTrue(containsType); + TestCase.assertTrue(containsType); } @Test @@ -123,14 +123,14 @@ public void testExtendsMap() { TypeDefinition td = builder.build(ClassExtendsMap.class, ClassExtendsMap.class); System.out.println(">> testExtendsMap: " + new Gson().toJson(td)); - Assertions.assertEquals("org.apache.dubbo.metadata.definition.common.ClassExtendsMap", td.getType()); - Assertions.assertEquals(0, td.getProperties().size()); + TestCase.assertEquals("org.apache.dubbo.metadata.definition.common.ClassExtendsMap", td.getType()); + TestCase.assertEquals(0, td.getProperties().size()); ServiceDefinition sd = MetadataUtils.generateMetadata(TestService.class); System.out.println(">> testExtendsMap: " + new Gson().toJson(sd)); - Assertions.assertEquals(TestService.class.getName(), sd.getCanonicalName()); - Assertions.assertEquals(TestService.class.getMethods().length, sd.getMethods().size()); + TestCase.assertEquals(TestService.class.getName(), sd.getCanonicalName()); + TestCase.assertEquals(TestService.class.getMethods().length, sd.getMethods().size()); boolean containsType = false; for (TypeDefinition type : sd.getTypes()) { if (type.getType().equals("org.apache.dubbo.metadata.definition.common.ClassExtendsMap")) { @@ -138,6 +138,6 @@ public void testExtendsMap() { break; } } - Assertions.assertFalse(containsType); + TestCase.assertFalse(containsType); } } diff --git a/dubbo-metadata-report/dubbo-metadata-definition/src/test/java/org/apache/dubbo/metadata/definition/ServiceDefinitionBuildderTest.java b/dubbo-metadata-report/dubbo-metadata-definition/src/test/java/org/apache/dubbo/metadata/definition/ServiceDefinitionBuildderTest.java index 89c81a5ae53..5b4b79412d8 100644 --- a/dubbo-metadata-report/dubbo-metadata-definition/src/test/java/org/apache/dubbo/metadata/definition/ServiceDefinitionBuildderTest.java +++ b/dubbo-metadata-report/dubbo-metadata-definition/src/test/java/org/apache/dubbo/metadata/definition/ServiceDefinitionBuildderTest.java @@ -22,8 +22,8 @@ import org.apache.dubbo.metadata.definition.service.ComplexObject; import org.apache.dubbo.metadata.definition.service.DemoService; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.util.Arrays; import java.util.List; @@ -51,12 +51,12 @@ void checkComplextObjectAsParam(FullServiceDefinition fullServiceDefinition) { findComplexObject = methodDefinition; } } - Assertions.assertTrue(Arrays.equals(complexCompute.getParameterTypes(), new String[]{String.class.getName(), ComplexObject.class.getName()})); - Assertions.assertEquals(complexCompute.getReturnType(), String.class.getName()); + Assert.assertTrue(Arrays.equals(complexCompute.getParameterTypes(), new String[]{String.class.getName(), ComplexObject.class.getName()})); + Assert.assertEquals(complexCompute.getReturnType(), String.class.getName()); - Assertions.assertTrue(Arrays.equals(findComplexObject.getParameterTypes(), new String[]{String.class.getName(), "int", "long", + Assert.assertTrue(Arrays.equals(findComplexObject.getParameterTypes(), new String[]{String.class.getName(), "int", "long", String[].class.getCanonicalName(), "java.util.List", ComplexObject.TestEnum.class.getCanonicalName()})); - Assertions.assertEquals(findComplexObject.getReturnType(), ComplexObject.class.getCanonicalName()); + Assert.assertEquals(findComplexObject.getReturnType(), ComplexObject.class.getCanonicalName()); List typeDefinitions = fullServiceDefinition.getTypes(); @@ -76,22 +76,22 @@ void checkComplextObjectAsParam(FullServiceDefinition fullServiceDefinition) { inner3TypeDefinition = typeDefinition; } } - Assertions.assertEquals(topTypeDefinition.getProperties().get("v").getType(), "long"); - Assertions.assertEquals(topTypeDefinition.getProperties().get("maps").getType(), "java.util.Map"); - Assertions.assertEquals(topTypeDefinition.getProperties().get("innerObject").getType(), ComplexObject.InnerObject.class.getName()); - Assertions.assertEquals(topTypeDefinition.getProperties().get("intList").getType(), "java.util.List"); - Assertions.assertEquals(topTypeDefinition.getProperties().get("strArrays").getType(), "java.lang.String[]"); - Assertions.assertEquals(topTypeDefinition.getProperties().get("innerObject3").getType(), "org.apache.dubbo.metadata.definition.service.ComplexObject.InnerObject3[]"); - Assertions.assertEquals(topTypeDefinition.getProperties().get("testEnum").getType(), "org.apache.dubbo.metadata.definition.service.ComplexObject.TestEnum"); - Assertions.assertEquals(topTypeDefinition.getProperties().get("innerObject2").getType(), "java.util.Set"); - - Assertions.assertSame(innerTypeDefinition.getProperties().get("innerA").getType(), "java.lang.String"); - Assertions.assertSame(innerTypeDefinition.getProperties().get("innerB").getType(), "int"); - - Assertions.assertSame(inner2TypeDefinition.getProperties().get("innerA2").getType(), "java.lang.String"); - Assertions.assertSame(inner2TypeDefinition.getProperties().get("innerB2").getType(), "int"); - - Assertions.assertSame(inner3TypeDefinition.getProperties().get("innerA3").getType(), "java.lang.String"); + Assert.assertEquals(topTypeDefinition.getProperties().get("v").getType(), "long"); + Assert.assertEquals(topTypeDefinition.getProperties().get("maps").getType(), "java.util.Map"); + Assert.assertEquals(topTypeDefinition.getProperties().get("innerObject").getType(), ComplexObject.InnerObject.class.getName()); + Assert.assertEquals(topTypeDefinition.getProperties().get("intList").getType(), "java.util.List"); + Assert.assertEquals(topTypeDefinition.getProperties().get("strArrays").getType(), "java.lang.String[]"); + Assert.assertEquals(topTypeDefinition.getProperties().get("innerObject3").getType(), "org.apache.dubbo.metadata.definition.service.ComplexObject.InnerObject3[]"); + Assert.assertEquals(topTypeDefinition.getProperties().get("testEnum").getType(), "org.apache.dubbo.metadata.definition.service.ComplexObject.TestEnum"); + Assert.assertEquals(topTypeDefinition.getProperties().get("innerObject2").getType(), "java.util.Set"); + + Assert.assertSame(innerTypeDefinition.getProperties().get("innerA").getType(), "java.lang.String"); + Assert.assertSame(innerTypeDefinition.getProperties().get("innerB").getType(), "int"); + + Assert.assertSame(inner2TypeDefinition.getProperties().get("innerA2").getType(), "java.lang.String"); + Assert.assertSame(inner2TypeDefinition.getProperties().get("innerB2").getType(), "int"); + + Assert.assertSame(inner3TypeDefinition.getProperties().get("innerA3").getType(), "java.lang.String"); } diff --git a/dubbo-metadata-report/dubbo-metadata-definition/src/test/java/org/apache/dubbo/metadata/definition/service/ComplexObject.java b/dubbo-metadata-report/dubbo-metadata-definition/src/test/java/org/apache/dubbo/metadata/definition/service/ComplexObject.java index 2859a13be8c..60a467a75b2 100644 --- a/dubbo-metadata-report/dubbo-metadata-definition/src/test/java/org/apache/dubbo/metadata/definition/service/ComplexObject.java +++ b/dubbo-metadata-report/dubbo-metadata-definition/src/test/java/org/apache/dubbo/metadata/definition/service/ComplexObject.java @@ -225,7 +225,7 @@ public String getInnerA2() { return innerA2; } - public void setInnerA2(String innerA2) { + public void setInnerA2(String innerA) { this.innerA2 = innerA2; } @@ -233,7 +233,7 @@ public int getInnerB2() { return innerB2; } - public void setInnerB2(int innerB2) { + public void setInnerB2(int innerB) { this.innerB2 = innerB2; } diff --git a/dubbo-metadata-report/dubbo-metadata-report-api/pom.xml b/dubbo-metadata-report/dubbo-metadata-report-api/pom.xml index 6244d0407f8..34970027a84 100644 --- a/dubbo-metadata-report/dubbo-metadata-report-api/pom.xml +++ b/dubbo-metadata-report/dubbo-metadata-report-api/pom.xml @@ -14,11 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. --> - + dubbo-metadata-report org.apache.dubbo - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT 4.0.0 @@ -66,5 +68,14 @@ com.google.code.gson gson + + org.apache.dubbo + dubbo-metadata-definition + ${project.parent.version} + + + com.google.code.gson + gson + diff --git a/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/integration/MetadataReportService.java b/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/integration/MetadataReportService.java index f2428f9fe9e..d0dfa733770 100644 --- a/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/integration/MetadataReportService.java +++ b/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/integration/MetadataReportService.java @@ -18,7 +18,6 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; @@ -39,7 +38,7 @@ public class MetadataReportService { protected final Logger logger = LoggerFactory.getLogger(getClass()); - private static volatile MetadataReportService metadataReportService; + private static MetadataReportService metadataReportService; private static Object lock = new Object(); private MetadataReportFactory metadataReportFactory = ExtensionLoader.getExtensionLoader(MetadataReportFactory.class).getAdaptiveExtension(); @@ -49,10 +48,7 @@ public class MetadataReportService { MetadataReportService(URL metadataReportURL) { if (Constants.METADATA_REPORT_KEY.equals(metadataReportURL.getProtocol())) { String protocol = metadataReportURL.getParameter(Constants.METADATA_REPORT_KEY, Constants.DEFAULT_DIRECTORY); - metadataReportURL = URLBuilder.from(metadataReportURL) - .setProtocol(protocol) - .removeParameter(Constants.METADATA_REPORT_KEY) - .build(); + metadataReportURL = metadataReportURL.setProtocol(protocol).removeParameter(Constants.METADATA_REPORT_KEY); } this.metadataReportUrl = metadataReportURL; metadataReport = metadataReportFactory.getMetadataReport(this.metadataReportUrl); diff --git a/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/support/AbstractMetadataReport.java b/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/support/AbstractMetadataReport.java index 95b44f44927..d29b6365c2e 100644 --- a/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/support/AbstractMetadataReport.java +++ b/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/support/AbstractMetadataReport.java @@ -40,19 +40,18 @@ import java.util.Iterator; import java.util.Map; import java.util.Properties; +import java.util.Random; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; /** - * */ public abstract class AbstractMetadataReport implements MetadataReport { @@ -62,13 +61,13 @@ public abstract class AbstractMetadataReport implements MetadataReport { // Log output protected final Logger logger = LoggerFactory.getLogger(getClass()); - // Local disk cache, where the special key value.registries records the list of metadata centers, and the others are the list of notified service providers + // Local disk cache, where the special key value.registies records the list of registry centers, and the others are the list of notified service providers final Properties properties = new Properties(); private final ExecutorService reportCacheExecutor = Executors.newFixedThreadPool(1, new NamedThreadFactory("DubboSaveMetadataReport", true)); - final Map allMetadataReports = new ConcurrentHashMap<>(4); + final Map allMetadataReports = new ConcurrentHashMap(4); private final AtomicLong lastCacheChanged = new AtomicLong(); - final Map failedReports = new ConcurrentHashMap<>(4); + final Map failedReports = new ConcurrentHashMap(4); private URL reportURL; boolean syncReport; // Local disk cache file @@ -101,7 +100,12 @@ public AbstractMetadataReport(URL reportServerURL) { // cycle report the data switch if (reportServerURL.getParameter(Constants.CYCLE_REPORT_KEY, Constants.DEFAULT_METADATA_REPORT_CYCLE_REPORT)) { ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("DubboMetadataReportTimer", true)); - scheduler.scheduleAtFixedRate(this::publishAll, calculateStartTime(), ONE_DAY_IN_MIll, TimeUnit.MILLISECONDS); + scheduler.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + publishAll(); + } + }, calculateStartTime(), ONE_DAY_IN_MIll, TimeUnit.MILLISECONDS); } } @@ -129,23 +133,27 @@ private void doSaveProperties(long version) { if (!lockfile.exists()) { lockfile.createNewFile(); } - try (RandomAccessFile raf = new RandomAccessFile(lockfile, "rw"); - FileChannel channel = raf.getChannel()) { - FileLock lock = channel.tryLock(); - if (lock == null) { - throw new IOException("Can not lock the metadataReport cache file " + file.getAbsolutePath() + ", ignore and retry later, maybe multi java process use the file, please config: dubbo.metadata.file=xxx.properties"); - } - // Save - try { - if (!file.exists()) { - file.createNewFile(); + RandomAccessFile raf = new RandomAccessFile(lockfile, "rw"); + try { + try (FileChannel channel = raf.getChannel()) { + FileLock lock = channel.tryLock(); + if (lock == null) { + throw new IOException("Can not lock the metadataReport cache file " + file.getAbsolutePath() + ", ignore and retry later, maybe multi java process use the file, please config: dubbo.metadata.file=xxx.properties"); } - try (FileOutputStream outputFile = new FileOutputStream(file)) { - properties.store(outputFile, "Dubbo metadataReport Cache"); + // Save + try { + if (!file.exists()) { + file.createNewFile(); + } + try (FileOutputStream outputFile = new FileOutputStream(file)) { + properties.store(outputFile, "Dubbo metadataReport Cache"); + } + } finally { + lock.release(); } - } finally { - lock.release(); } + } finally { + raf.close(); } } catch (Throwable e) { if (version < lastCacheChanged.get()) { @@ -159,13 +167,23 @@ private void doSaveProperties(long version) { void loadProperties() { if (file != null && file.exists()) { - try (InputStream in = new FileInputStream(file)) { + InputStream in = null; + try { + in = new FileInputStream(file); properties.load(in); if (logger.isInfoEnabled()) { logger.info("Load service store file " + file + ", data: " + properties); } } catch (Throwable e) { logger.warn("Failed to load service store file " + file, e); + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) { + logger.warn(e.getMessage(), e); + } + } } } } @@ -211,7 +229,6 @@ public void run() { } } - @Override public void storeProviderMetadata(MetadataIdentifier providerMetadataIdentifier, FullServiceDefinition serviceDefinition) { if (syncReport) { storeProviderMetadataTask(providerMetadataIdentifier, serviceDefinition); @@ -244,7 +261,6 @@ private void storeProviderMetadataTask(MetadataIdentifier providerMetadataIdenti } } - @Override public void storeConsumerMetadata(MetadataIdentifier consumerMetadataIdentifier, Map serviceParameterMap) { if (syncReport) { storeConsumerMetadataTask(consumerMetadataIdentifier, serviceParameterMap); @@ -330,14 +346,15 @@ long calculateStartTime() { calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); long subtract = calendar.getTimeInMillis() + ONE_DAY_IN_MIll - nowMill; - return subtract + (FOUR_HOURS_IN_MIll / 2) + ThreadLocalRandom.current().nextInt(FOUR_HOURS_IN_MIll); + Random r = new Random(); + return subtract + (FOUR_HOURS_IN_MIll / 2) + r.nextInt(FOUR_HOURS_IN_MIll); } class MetadataReportRetry { protected final Logger logger = LoggerFactory.getLogger(getClass()); - final ScheduledExecutorService retryExecutor = Executors.newScheduledThreadPool(0, new NamedThreadFactory("DubboMetadataReportRetryTimer", true)); - volatile ScheduledFuture retryScheduledFuture; + final ScheduledExecutorService retryExecutor = Executors.newScheduledThreadPool(0, new NamedThreadFactory("DubboRegistryFailedRetryTimer", true)); + ScheduledFuture retryScheduledFuture; AtomicInteger retryCounter = new AtomicInteger(0); // retry task schedule period long retryPeriod; @@ -358,7 +375,7 @@ void startRetryTask() { retryScheduledFuture = retryExecutor.scheduleWithFixedDelay(new Runnable() { @Override public void run() { - // Check and connect to the metadata + // Check and connect to the registry try { int times = retryCounter.incrementAndGet(); logger.info("start to retry task for metadata report. retry times:" + times); diff --git a/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/identifier/MetadataIdentifierTest.java b/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/identifier/MetadataIdentifierTest.java index 40fab45c4c8..d9a345441fd 100644 --- a/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/identifier/MetadataIdentifierTest.java +++ b/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/identifier/MetadataIdentifierTest.java @@ -17,9 +17,8 @@ package org.apache.dubbo.metadata.identifier; import org.apache.dubbo.common.Constants; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; /** * 2019/1/7 @@ -34,10 +33,10 @@ public void testGetUniqueKey() { String application = "vic.zk.md"; MetadataIdentifier providerMetadataIdentifier = new MetadataIdentifier(interfaceName, version, group, Constants.PROVIDER_SIDE, application); System.out.println(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.PATH)); - Assertions.assertEquals(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.PATH), "metadata" + Constants.PATH_SEPARATOR + interfaceName + Constants.PATH_SEPARATOR + (version == null ? "" : (version + Constants.PATH_SEPARATOR)) + Assert.assertEquals(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.PATH), "metadata" + Constants.PATH_SEPARATOR + interfaceName + Constants.PATH_SEPARATOR + (version == null ? "" : (version + Constants.PATH_SEPARATOR)) + (group == null ? "" : (group + Constants.PATH_SEPARATOR)) + Constants.PROVIDER_SIDE + Constants.PATH_SEPARATOR + application); System.out.println(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY)); - Assertions.assertEquals(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), + Assert.assertEquals(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), interfaceName + MetadataIdentifier.SEPARATOR + (version == null ? "" : version + MetadataIdentifier.SEPARATOR) + (group == null ? "" : group + MetadataIdentifier.SEPARATOR) + Constants.PROVIDER_SIDE + MetadataIdentifier.SEPARATOR + application); } } diff --git a/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/integration/MetadataReportServiceTest.java b/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/integration/MetadataReportServiceTest.java index b0e24b53b00..0a06fc1ebcb 100644 --- a/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/integration/MetadataReportServiceTest.java +++ b/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/integration/MetadataReportServiceTest.java @@ -22,9 +22,9 @@ import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.metadata.definition.model.FullServiceDefinition; import org.apache.dubbo.metadata.store.test.JTestMetadataReport4Test; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import java.util.Collections; import java.util.HashMap; @@ -38,9 +38,15 @@ public class MetadataReportServiceTest { URL url = URL.valueOf("JTest://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vic"); MetadataReportService metadataReportService1; - @BeforeEach + @Before public void before() { - metadataReportService1 = MetadataReportService.instance(() -> url); + + metadataReportService1 = MetadataReportService.instance(new Supplier() { + @Override + public URL get() { + return url; + } + }); } @Test @@ -52,8 +58,8 @@ public URL get() { return url; } }); - Assertions.assertSame(metadataReportService1, metadataReportService2); - Assertions.assertEquals(metadataReportService1.metadataReportUrl, url); + Assert.assertSame(metadataReportService1, metadataReportService2); + Assert.assertEquals(metadataReportService1.metadataReportUrl, url); } @Test @@ -63,10 +69,10 @@ public void testPublishProviderNoInterfaceName() { URL publishUrl = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vicpubprovder&side=provider"); metadataReportService1.publishProvider(publishUrl); - Assertions.assertTrue(metadataReportService1.metadataReport instanceof JTestMetadataReport4Test); + Assert.assertTrue(metadataReportService1.metadataReport instanceof JTestMetadataReport4Test); JTestMetadataReport4Test jTestMetadataReport4Test = (JTestMetadataReport4Test) metadataReportService1.metadataReport; - Assertions.assertTrue(!jTestMetadataReport4Test.store.containsKey(JTestMetadataReport4Test.getProviderKey(publishUrl))); + Assert.assertTrue(!jTestMetadataReport4Test.store.containsKey(JTestMetadataReport4Test.getProviderKey(publishUrl))); } @@ -76,10 +82,10 @@ public void testPublishProviderWrongInterface() { URL publishUrl = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vicpu&interface=ccc&side=provider"); metadataReportService1.publishProvider(publishUrl); - Assertions.assertTrue(metadataReportService1.metadataReport instanceof JTestMetadataReport4Test); + Assert.assertTrue(metadataReportService1.metadataReport instanceof JTestMetadataReport4Test); JTestMetadataReport4Test jTestMetadataReport4Test = (JTestMetadataReport4Test) metadataReportService1.metadataReport; - Assertions.assertTrue(!jTestMetadataReport4Test.store.containsKey(JTestMetadataReport4Test.getProviderKey(publishUrl))); + Assert.assertTrue(!jTestMetadataReport4Test.store.containsKey(JTestMetadataReport4Test.getProviderKey(publishUrl))); } @@ -90,17 +96,17 @@ public void testPublishProviderContainInterface() throws InterruptedException { metadataReportService1.publishProvider(publishUrl); Thread.sleep(300); - Assertions.assertTrue(metadataReportService1.metadataReport instanceof JTestMetadataReport4Test); + Assert.assertTrue(metadataReportService1.metadataReport instanceof JTestMetadataReport4Test); JTestMetadataReport4Test jTestMetadataReport4Test = (JTestMetadataReport4Test) metadataReportService1.metadataReport; - Assertions.assertTrue(jTestMetadataReport4Test.store.containsKey(JTestMetadataReport4Test.getProviderKey(publishUrl))); + Assert.assertTrue(jTestMetadataReport4Test.store.containsKey(JTestMetadataReport4Test.getProviderKey(publishUrl))); String value = jTestMetadataReport4Test.store.get(JTestMetadataReport4Test.getProviderKey(publishUrl)); FullServiceDefinition fullServiceDefinition = toServiceDefinition(value); Map map = fullServiceDefinition.getParameters(); - Assertions.assertEquals(map.get("application"), "vicpubp"); - Assertions.assertEquals(map.get("version"), "1.0.3"); - Assertions.assertEquals(map.get("interface"), "org.apache.dubbo.metadata.integration.InterfaceNameTestService"); + Assert.assertEquals(map.get("application"), "vicpubp"); + Assert.assertEquals(map.get("version"), "1.0.3"); + Assert.assertEquals(map.get("interface"), "org.apache.dubbo.metadata.integration.InterfaceNameTestService"); } @Test @@ -110,16 +116,16 @@ public void testPublishConsumer() throws InterruptedException { metadataReportService1.publishConsumer(publishUrl); Thread.sleep(300); - Assertions.assertTrue(metadataReportService1.metadataReport instanceof JTestMetadataReport4Test); + Assert.assertTrue(metadataReportService1.metadataReport instanceof JTestMetadataReport4Test); JTestMetadataReport4Test jTestMetadataReport4Test = (JTestMetadataReport4Test) metadataReportService1.metadataReport; - Assertions.assertTrue(jTestMetadataReport4Test.store.containsKey(JTestMetadataReport4Test.getConsumerKey(publishUrl))); + Assert.assertTrue(jTestMetadataReport4Test.store.containsKey(JTestMetadataReport4Test.getConsumerKey(publishUrl))); String value = jTestMetadataReport4Test.store.get(JTestMetadataReport4Test.getConsumerKey(publishUrl)); Gson gson = new Gson(); Map map = gson.fromJson(value, Map.class); - Assertions.assertEquals(map.get("application"), "vicpubconsumer"); - Assertions.assertEquals(map.get("version"), "1.0.x"); + Assert.assertEquals(map.get("application"), "vicpubconsumer"); + Assert.assertEquals(map.get("version"), "1.0.x"); } diff --git a/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/store/test/JTestMetadataReport4Test.java b/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/store/test/JTestMetadataReport4Test.java index 76778cee339..294fbbbc00d 100644 --- a/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/store/test/JTestMetadataReport4Test.java +++ b/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/store/test/JTestMetadataReport4Test.java @@ -22,6 +22,7 @@ import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.metadata.identifier.MetadataIdentifier; import org.apache.dubbo.metadata.support.AbstractMetadataReport; +import org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -34,7 +35,7 @@ public class JTestMetadataReport4Test extends AbstractMetadataReport { private final static Logger logger = LoggerFactory.getLogger(JTestMetadataReport4Test.class); - public JTestMetadataReport4Test(URL url) { + public JTestMetadataReport4Test(URL url, ZookeeperTransporter zookeeperTransporter) { super(url); } diff --git a/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/store/test/JTestMetadataReportFactory4Test.java b/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/store/test/JTestMetadataReportFactory4Test.java index 4e50a073fe1..0dd842a52ad 100644 --- a/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/store/test/JTestMetadataReportFactory4Test.java +++ b/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/store/test/JTestMetadataReportFactory4Test.java @@ -19,15 +19,22 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.metadata.store.MetadataReport; import org.apache.dubbo.metadata.support.AbstractMetadataReportFactory; +import org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter; /** * ZookeeperRegistryFactory. */ public class JTestMetadataReportFactory4Test extends AbstractMetadataReportFactory { + private ZookeeperTransporter zookeeperTransporter; + + public void setZookeeperTransporter(ZookeeperTransporter zookeeperTransporter) { + this.zookeeperTransporter = zookeeperTransporter; + } + @Override public MetadataReport createMetadataReport(URL url) { - return new JTestMetadataReport4Test(url); + return new JTestMetadataReport4Test(url, zookeeperTransporter); } } diff --git a/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/support/AbstractMetadataReportFactoryTest.java b/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/support/AbstractMetadataReportFactoryTest.java index 62bcbe81365..cc69c10db3a 100644 --- a/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/support/AbstractMetadataReportFactoryTest.java +++ b/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/support/AbstractMetadataReportFactoryTest.java @@ -24,8 +24,8 @@ import com.alibaba.fastjson.JSON; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -62,7 +62,7 @@ public void testGetOneMetadataReport() { URL url = URL.valueOf("zookeeper://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vic"); MetadataReport metadataReport1 = metadataReportFactory.getMetadataReport(url); MetadataReport metadataReport2 = metadataReportFactory.getMetadataReport(url); - Assertions.assertEquals(metadataReport1, metadataReport2); + Assert.assertEquals(metadataReport1, metadataReport2); } @Test @@ -71,7 +71,7 @@ public void testGetOneMetadataReportForIpFormat() { URL url2 = URL.valueOf("zookeeper://" + NetUtils.getLocalAddress().getHostAddress() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vic"); MetadataReport metadataReport1 = metadataReportFactory.getMetadataReport(url1); MetadataReport metadataReport2 = metadataReportFactory.getMetadataReport(url2); - Assertions.assertEquals(metadataReport1, metadataReport2); + Assert.assertEquals(metadataReport1, metadataReport2); } @Test @@ -80,7 +80,7 @@ public void testGetForDiffService() { URL url2 = URL.valueOf("zookeeper://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService2?version=1.0.0&application=vic"); MetadataReport metadataReport1 = metadataReportFactory.getMetadataReport(url1); MetadataReport metadataReport2 = metadataReportFactory.getMetadataReport(url2); - Assertions.assertEquals(metadataReport1, metadataReport2); + Assert.assertEquals(metadataReport1, metadataReport2); } @Test @@ -89,6 +89,6 @@ public void testGetForDiffGroup() { URL url2 = URL.valueOf("zookeeper://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vic&group=bbb"); MetadataReport metadataReport1 = metadataReportFactory.getMetadataReport(url1); MetadataReport metadataReport2 = metadataReportFactory.getMetadataReport(url2); - Assertions.assertNotEquals(metadataReport1, metadataReport2); + Assert.assertNotEquals(metadataReport1, metadataReport2); } } diff --git a/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/support/AbstractMetadataReportTest.java b/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/support/AbstractMetadataReportTest.java index 9544c18db93..d9a8e2d99cf 100644 --- a/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/support/AbstractMetadataReportTest.java +++ b/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/support/AbstractMetadataReportTest.java @@ -24,9 +24,9 @@ import org.apache.dubbo.metadata.identifier.MetadataIdentifier; import com.google.gson.Gson; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import java.util.Calendar; import java.util.Collections; @@ -42,7 +42,7 @@ public class AbstractMetadataReportTest { private NewMetadataReport abstractMetadataReport; - @BeforeEach + @Before public void before() { URL url = URL.valueOf("zookeeper://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vic"); abstractMetadataReport = new NewMetadataReport(url); @@ -52,11 +52,11 @@ public void before() { public void testGetProtocol() { URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vic&side=provider"); String protocol = abstractMetadataReport.getProtocol(url); - Assertions.assertEquals(protocol, "provider"); + Assert.assertEquals(protocol, "provider"); URL url2 = URL.valueOf("consumer://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vic"); String protocol2 = abstractMetadataReport.getProtocol(url2); - Assertions.assertEquals(protocol2, "consumer"); + Assert.assertEquals(protocol2, "consumer"); } @Test @@ -67,7 +67,7 @@ public void testStoreProviderUsual() throws ClassNotFoundException, InterruptedE String application = "vic"; MetadataIdentifier providerMetadataIdentifier = storePrivider(abstractMetadataReport, interfaceName, version, group, application); Thread.sleep(1500); - Assertions.assertNotNull(abstractMetadataReport.store.get(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY))); + Assert.assertNotNull(abstractMetadataReport.store.get(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY))); } @Test @@ -78,7 +78,7 @@ public void testStoreProviderSync() throws ClassNotFoundException, InterruptedEx String application = "vic"; abstractMetadataReport.syncReport = true; MetadataIdentifier providerMetadataIdentifier = storePrivider(abstractMetadataReport, interfaceName, version, group, application); - Assertions.assertNotNull(abstractMetadataReport.store.get(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY))); + Assert.assertNotNull(abstractMetadataReport.store.get(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY))); } @Test @@ -87,7 +87,7 @@ public void testFileExistAfterPut() throws InterruptedException, ClassNotFoundEx URL singleUrl = URL.valueOf("redis://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.metadata.integration.InterfaceNameTestService?version=1.0.0&application=singleTest"); NewMetadataReport singleMetadataReport = new NewMetadataReport(singleUrl); - Assertions.assertFalse(singleMetadataReport.file.exists()); + Assert.assertFalse(singleMetadataReport.file.exists()); String interfaceName = "org.apache.dubbo.metadata.integration.InterfaceNameTestService"; String version = "1.0.0"; @@ -96,8 +96,8 @@ public void testFileExistAfterPut() throws InterruptedException, ClassNotFoundEx MetadataIdentifier providerMetadataIdentifier = storePrivider(singleMetadataReport, interfaceName, version, group, application); Thread.sleep(2000); - Assertions.assertTrue(singleMetadataReport.file.exists()); - Assertions.assertTrue(singleMetadataReport.properties.containsKey(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY))); + Assert.assertTrue(singleMetadataReport.file.exists()); + Assert.assertTrue(singleMetadataReport.properties.containsKey(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY))); } @Test @@ -110,23 +110,23 @@ public void testRetry() throws InterruptedException, ClassNotFoundException { RetryMetadataReport retryReport = new RetryMetadataReport(storeUrl, 2); retryReport.metadataReportRetry.retryPeriod = 400L; URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vic"); - Assertions.assertNull(retryReport.metadataReportRetry.retryScheduledFuture); - Assertions.assertTrue(retryReport.metadataReportRetry.retryCounter.get() == 0); - Assertions.assertTrue(retryReport.store.isEmpty()); - Assertions.assertTrue(retryReport.failedReports.isEmpty()); + Assert.assertNull(retryReport.metadataReportRetry.retryScheduledFuture); + Assert.assertTrue(retryReport.metadataReportRetry.retryCounter.get() == 0); + Assert.assertTrue(retryReport.store.isEmpty()); + Assert.assertTrue(retryReport.failedReports.isEmpty()); storePrivider(retryReport, interfaceName, version, group, application); Thread.sleep(150); - Assertions.assertTrue(retryReport.store.isEmpty()); - Assertions.assertFalse(retryReport.failedReports.isEmpty()); - Assertions.assertNotNull(retryReport.metadataReportRetry.retryScheduledFuture); + Assert.assertTrue(retryReport.store.isEmpty()); + Assert.assertFalse(retryReport.failedReports.isEmpty()); + Assert.assertNotNull(retryReport.metadataReportRetry.retryScheduledFuture); Thread.sleep(2000L); - Assertions.assertTrue(retryReport.metadataReportRetry.retryCounter.get() != 0); - Assertions.assertTrue(retryReport.metadataReportRetry.retryCounter.get() >= 3); - Assertions.assertFalse(retryReport.store.isEmpty()); - Assertions.assertTrue(retryReport.failedReports.isEmpty()); + Assert.assertTrue(retryReport.metadataReportRetry.retryCounter.get() != 0); + Assert.assertTrue(retryReport.metadataReportRetry.retryCounter.get() >= 3); + Assert.assertFalse(retryReport.store.isEmpty()); + Assert.assertTrue(retryReport.failedReports.isEmpty()); } @Test @@ -143,11 +143,11 @@ public void testRetryCancel() throws InterruptedException, ClassNotFoundExceptio storePrivider(retryReport, interfaceName, version, group, application); Thread.sleep(80); - Assertions.assertFalse(retryReport.metadataReportRetry.retryScheduledFuture.isCancelled()); - Assertions.assertFalse(retryReport.metadataReportRetry.retryExecutor.isShutdown()); + Assert.assertFalse(retryReport.metadataReportRetry.retryScheduledFuture.isCancelled()); + Assert.assertFalse(retryReport.metadataReportRetry.retryExecutor.isShutdown()); Thread.sleep(1000L); - Assertions.assertTrue(retryReport.metadataReportRetry.retryScheduledFuture.isCancelled()); - Assertions.assertTrue(retryReport.metadataReportRetry.retryExecutor.isShutdown()); + Assert.assertTrue(retryReport.metadataReportRetry.retryScheduledFuture.isCancelled()); + Assert.assertTrue(retryReport.metadataReportRetry.retryExecutor.isShutdown()); } @@ -179,42 +179,42 @@ private MetadataIdentifier storeConsumer(AbstractMetadataReport abstractMetadata @Test public void testPublishAll() throws ClassNotFoundException, InterruptedException { - Assertions.assertTrue(abstractMetadataReport.store.isEmpty()); - Assertions.assertTrue(abstractMetadataReport.allMetadataReports.isEmpty()); + Assert.assertTrue(abstractMetadataReport.store.isEmpty()); + Assert.assertTrue(abstractMetadataReport.allMetadataReports.isEmpty()); String interfaceName = "org.apache.dubbo.metadata.integration.InterfaceNameTestService"; String version = "1.0.0"; String group = null; String application = "vic"; MetadataIdentifier providerMetadataIdentifier1 = storePrivider(abstractMetadataReport, interfaceName, version, group, application); Thread.sleep(1000); - Assertions.assertEquals(abstractMetadataReport.allMetadataReports.size(), 1); - Assertions.assertTrue(((FullServiceDefinition) abstractMetadataReport.allMetadataReports.get(providerMetadataIdentifier1)).getParameters().containsKey("testPKey")); + Assert.assertEquals(abstractMetadataReport.allMetadataReports.size(), 1); + Assert.assertTrue(((FullServiceDefinition) abstractMetadataReport.allMetadataReports.get(providerMetadataIdentifier1)).getParameters().containsKey("testPKey")); MetadataIdentifier providerMetadataIdentifier2 = storePrivider(abstractMetadataReport, interfaceName, version + "_2", group + "_2", application); Thread.sleep(1000); - Assertions.assertEquals(abstractMetadataReport.allMetadataReports.size(), 2); - Assertions.assertTrue(((FullServiceDefinition) abstractMetadataReport.allMetadataReports.get(providerMetadataIdentifier2)).getParameters().containsKey("testPKey")); - Assertions.assertEquals(((FullServiceDefinition) abstractMetadataReport.allMetadataReports.get(providerMetadataIdentifier2)).getParameters().get("version"), version + "_2"); + Assert.assertEquals(abstractMetadataReport.allMetadataReports.size(), 2); + Assert.assertTrue(((FullServiceDefinition) abstractMetadataReport.allMetadataReports.get(providerMetadataIdentifier2)).getParameters().containsKey("testPKey")); + Assert.assertEquals(((FullServiceDefinition) abstractMetadataReport.allMetadataReports.get(providerMetadataIdentifier2)).getParameters().get("version"), version + "_2"); Map tmpMap = new HashMap<>(); tmpMap.put("testKey", "value"); MetadataIdentifier consumerMetadataIdentifier = storeConsumer(abstractMetadataReport, interfaceName, version + "_3", group + "_3", application, tmpMap); Thread.sleep(1000); - Assertions.assertEquals(abstractMetadataReport.allMetadataReports.size(), 3); + Assert.assertEquals(abstractMetadataReport.allMetadataReports.size(), 3); Map tmpMapResult = (Map) abstractMetadataReport.allMetadataReports.get(consumerMetadataIdentifier); - Assertions.assertEquals(tmpMapResult.get("testPKey"), "9090"); - Assertions.assertEquals(tmpMapResult.get("testKey"), "value"); - Assertions.assertTrue(abstractMetadataReport.store.size() == 3); + Assert.assertEquals(tmpMapResult.get("testPKey"), "9090"); + Assert.assertEquals(tmpMapResult.get("testKey"), "value"); + Assert.assertTrue(abstractMetadataReport.store.size() == 3); abstractMetadataReport.store.clear(); - Assertions.assertTrue(abstractMetadataReport.store.size() == 0); + Assert.assertTrue(abstractMetadataReport.store.size() == 0); abstractMetadataReport.publishAll(); Thread.sleep(200); - Assertions.assertTrue(abstractMetadataReport.store.size() == 3); + Assert.assertTrue(abstractMetadataReport.store.size() == 3); String v = abstractMetadataReport.store.get(providerMetadataIdentifier1.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY)); Gson gson = new Gson(); @@ -238,8 +238,8 @@ public void testCalculateStartTime() { long t = abstractMetadataReport.calculateStartTime() + System.currentTimeMillis(); Calendar c = Calendar.getInstance(); c.setTimeInMillis(t); - Assertions.assertTrue(c.get(Calendar.HOUR_OF_DAY) >= 2); - Assertions.assertTrue(c.get(Calendar.HOUR_OF_DAY) <= 6); + Assert.assertTrue(c.get(Calendar.HOUR_OF_DAY) >= 2); + Assert.assertTrue(c.get(Calendar.HOUR_OF_DAY) <= 6); } } @@ -250,8 +250,8 @@ private FullServiceDefinition toServiceDefinition(String v) { } private void checkParam(Map map, String application, String version) { - Assertions.assertEquals(map.get("application"), application); - Assertions.assertEquals(map.get("version"), version); + Assert.assertEquals(map.get("application"), application); + Assert.assertEquals(map.get("version"), version); } private Map queryUrlToMap(String urlQuery) { diff --git a/dubbo-metadata-report/dubbo-metadata-report-consul/pom.xml b/dubbo-metadata-report/dubbo-metadata-report-consul/pom.xml deleted file mode 100644 index ad02eaedda6..00000000000 --- a/dubbo-metadata-report/dubbo-metadata-report-consul/pom.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - dubbo-metadata-report - org.apache.dubbo - 2.7.1-SNAPSHOT - - 4.0.0 - - dubbo-metadata-report-consul - - - - org.apache.dubbo - dubbo-metadata-report-api - ${project.parent.version} - - - com.ecwid.consul - consul-api - - - - diff --git a/dubbo-metadata-report/dubbo-metadata-report-consul/src/main/java/org/apache/dubbo/metadata/store/consul/ConsulMetadataReport.java b/dubbo-metadata-report/dubbo-metadata-report-consul/src/main/java/org/apache/dubbo/metadata/store/consul/ConsulMetadataReport.java deleted file mode 100644 index 6e24fd9dab3..00000000000 --- a/dubbo-metadata-report/dubbo-metadata-report-consul/src/main/java/org/apache/dubbo/metadata/store/consul/ConsulMetadataReport.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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 org.apache.dubbo.metadata.store.consul; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.metadata.identifier.MetadataIdentifier; -import org.apache.dubbo.metadata.support.AbstractMetadataReport; -import org.apache.dubbo.rpc.RpcException; - -import com.ecwid.consul.v1.ConsulClient; - -/** - * metadata report impl for consul - */ -public class ConsulMetadataReport extends AbstractMetadataReport { - private static final Logger logger = LoggerFactory.getLogger(ConsulMetadataReport.class); - private static final int DEFAULT_PORT = 8500; - - private ConsulClient client; - - public ConsulMetadataReport(URL url) { - super(url); - - String host = url.getHost(); - int port = url.getPort() != 0 ? url.getPort() : DEFAULT_PORT; - client = new ConsulClient(host, port); - } - - @Override - protected void doStoreProviderMetadata(MetadataIdentifier providerMetadataIdentifier, String serviceDefinitions) { - this.storeMetadata(providerMetadataIdentifier, serviceDefinitions); - } - - @Override - protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdentifier, String value) { - this.storeMetadata(consumerMetadataIdentifier, value); - } - - private void storeMetadata(MetadataIdentifier identifier, String v) { - try { - client.setKVValue(identifier.getIdentifierKey() + META_DATA_SOTRE_TAG, v); - } catch (Throwable t) { - logger.error("Failed to put " + identifier + " to consul " + v + ", cause: " + t.getMessage(), t); - throw new RpcException("Failed to put " + identifier + " to consul " + v + ", cause: " + t.getMessage(), t); - } - } -} diff --git a/dubbo-metadata-report/dubbo-metadata-report-consul/src/main/java/org/apache/dubbo/metadata/store/consul/ConsulMetadataReportFactory.java b/dubbo-metadata-report/dubbo-metadata-report-consul/src/main/java/org/apache/dubbo/metadata/store/consul/ConsulMetadataReportFactory.java deleted file mode 100644 index 66d7b5e5e45..00000000000 --- a/dubbo-metadata-report/dubbo-metadata-report-consul/src/main/java/org/apache/dubbo/metadata/store/consul/ConsulMetadataReportFactory.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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 org.apache.dubbo.metadata.store.consul; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.metadata.store.MetadataReport; -import org.apache.dubbo.metadata.support.AbstractMetadataReportFactory; - -/** - * metadata report factory impl for consul - */ -public class ConsulMetadataReportFactory extends AbstractMetadataReportFactory { - @Override - protected MetadataReport createMetadataReport(URL url) { - return new ConsulMetadataReport(url); - } -} diff --git a/dubbo-metadata-report/dubbo-metadata-report-consul/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.metadata.store.MetadataReportFactory b/dubbo-metadata-report/dubbo-metadata-report-consul/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.metadata.store.MetadataReportFactory deleted file mode 100644 index 1f27535d442..00000000000 --- a/dubbo-metadata-report/dubbo-metadata-report-consul/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.metadata.store.MetadataReportFactory +++ /dev/null @@ -1 +0,0 @@ -consul=org.apache.dubbo.metadata.store.consul.ConsulMetadataReportFactory diff --git a/dubbo-metadata-report/dubbo-metadata-report-redis/pom.xml b/dubbo-metadata-report/dubbo-metadata-report-redis/pom.xml index 6f5d418e28f..ecdf91fc513 100644 --- a/dubbo-metadata-report/dubbo-metadata-report-redis/pom.xml +++ b/dubbo-metadata-report/dubbo-metadata-report-redis/pom.xml @@ -14,11 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. --> - + dubbo-metadata-report org.apache.dubbo - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT 4.0.0 diff --git a/dubbo-metadata-report/dubbo-metadata-report-redis/src/test/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReportTest.java b/dubbo-metadata-report/dubbo-metadata-report-redis/src/test/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReportTest.java index 7f7393e43aa..82fc12b945f 100644 --- a/dubbo-metadata-report/dubbo-metadata-report-redis/src/test/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReportTest.java +++ b/dubbo-metadata-report/dubbo-metadata-report-redis/src/test/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReportTest.java @@ -24,10 +24,10 @@ import org.apache.dubbo.metadata.definition.model.FullServiceDefinition; import org.apache.dubbo.metadata.identifier.MetadataIdentifier; import org.apache.dubbo.rpc.RpcException; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import redis.clients.jedis.Jedis; import redis.embedded.RedisServer; @@ -46,7 +46,7 @@ public class RedisMetadataReportTest { RedisMetadataReport syncRedisMetadataReport; RedisServer redisServer; - @BeforeEach + @Before public void constructor() throws IOException { int redisPort = NetUtils.getAvailablePort(); this.redisServer = new RedisServer(redisPort); @@ -57,7 +57,7 @@ public void constructor() throws IOException { syncRedisMetadataReport = (RedisMetadataReport) new RedisMetadataReportFactory().createMetadataReport(registryUrl); } - @AfterEach + @After public void tearDown() throws Exception { this.redisServer.stop(); } @@ -88,11 +88,11 @@ private void testStoreProvider(RedisMetadataReport redisMetadataReport, String v value = jedis.get(keyTmp); } - Assertions.assertNotNull(value); + Assert.assertNotNull(value); Gson gson = new Gson(); FullServiceDefinition fullServiceDefinition = gson.fromJson(value, FullServiceDefinition.class); - Assertions.assertEquals(fullServiceDefinition.getParameters().get("paramTest"), "redisTest"); + Assert.assertEquals(fullServiceDefinition.getParameters().get("paramTest"), "redisTest"); } catch (Throwable e) { throw new RpcException("Failed to put to redis . cause: " + e.getMessage(), e); } finally { @@ -127,7 +127,7 @@ private void testStoreConsumer(RedisMetadataReport redisMetadataReport, String v Thread.sleep(moreTime); value = jedis.get(keyTmp); } - Assertions.assertEquals(value, "{\"paramConsumerTest\":\"redisCm\"}"); + Assert.assertEquals(value, "{\"paramConsumerTest\":\"redisCm\"}"); } catch (Throwable e) { throw new RpcException("Failed to put to redis . cause: " + e.getMessage(), e); } finally { diff --git a/dubbo-metadata-report/dubbo-metadata-report-zookeeper/pom.xml b/dubbo-metadata-report/dubbo-metadata-report-zookeeper/pom.xml index 4f7feb1b22b..f069d06be42 100644 --- a/dubbo-metadata-report/dubbo-metadata-report-zookeeper/pom.xml +++ b/dubbo-metadata-report/dubbo-metadata-report-zookeeper/pom.xml @@ -14,11 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. --> - + dubbo-metadata-report org.apache.dubbo - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT 4.0.0 diff --git a/dubbo-metadata-report/dubbo-metadata-report-zookeeper/src/test/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReportTest.java b/dubbo-metadata-report/dubbo-metadata-report-zookeeper/src/test/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReportTest.java index c9ed9cfa9bb..14672fb8ee8 100644 --- a/dubbo-metadata-report/dubbo-metadata-report-zookeeper/src/test/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReportTest.java +++ b/dubbo-metadata-report/dubbo-metadata-report-zookeeper/src/test/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReportTest.java @@ -26,11 +26,11 @@ import com.google.gson.Gson; import org.apache.curator.test.TestingServer; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; import java.util.HashMap; import java.util.Map; @@ -44,7 +44,7 @@ public class ZookeeperMetadataReportTest { private URL registryUrl; private ZookeeperMetadataReportFactory zookeeperMetadataReportFactory; - @BeforeEach + @Before public void setUp() throws Exception { int zkServerPort = NetUtils.getAvailablePort(); this.zkServer = new TestingServer(zkServerPort, true); @@ -55,7 +55,7 @@ public void setUp() throws Exception { this.zookeeperMetadataReport = (ZookeeperMetadataReport) zookeeperMetadataReportFactory.createMetadataReport(registryUrl); } - @AfterEach + @After public void tearDown() throws Exception { zkServer.stop(); } @@ -75,22 +75,22 @@ public void testStoreProvider() throws ClassNotFoundException, InterruptedExcept String fileContent = zookeeperMetadataReport.zkClient.getContent(zookeeperMetadataReport.getNodePath(providerMetadataIdentifier)); fileContent = waitSeconds(fileContent, 3500, zookeeperMetadataReport.getNodePath(providerMetadataIdentifier)); - Assertions.assertNotNull(fileContent); + Assert.assertNotNull(fileContent); deletePath(providerMetadataIdentifier, zookeeperMetadataReport); fileContent = zookeeperMetadataReport.zkClient.getContent(zookeeperMetadataReport.getNodePath(providerMetadataIdentifier)); fileContent = waitSeconds(fileContent, 1000, zookeeperMetadataReport.getNodePath(providerMetadataIdentifier)); - Assertions.assertNull(fileContent); + Assert.assertNull(fileContent); providerMetadataIdentifier = storePrivider(zookeeperMetadataReport, interfaceName, version, group, application); fileContent = zookeeperMetadataReport.zkClient.getContent(zookeeperMetadataReport.getNodePath(providerMetadataIdentifier)); fileContent = waitSeconds(fileContent, 3500, zookeeperMetadataReport.getNodePath(providerMetadataIdentifier)); - Assertions.assertNotNull(fileContent); + Assert.assertNotNull(fileContent); Gson gson = new Gson(); FullServiceDefinition fullServiceDefinition = gson.fromJson(fileContent, FullServiceDefinition.class); - Assertions.assertEquals(fullServiceDefinition.getParameters().get("paramTest"), "zkTest"); + Assert.assertEquals(fullServiceDefinition.getParameters().get("paramTest"), "zkTest"); } @@ -104,18 +104,18 @@ public void testConsumer() throws ClassNotFoundException, InterruptedException { String fileContent = zookeeperMetadataReport.zkClient.getContent(zookeeperMetadataReport.getNodePath(consumerMetadataIdentifier)); fileContent = waitSeconds(fileContent, 3500, zookeeperMetadataReport.getNodePath(consumerMetadataIdentifier)); - Assertions.assertNotNull(fileContent); + Assert.assertNotNull(fileContent); deletePath(consumerMetadataIdentifier, zookeeperMetadataReport); fileContent = zookeeperMetadataReport.zkClient.getContent(zookeeperMetadataReport.getNodePath(consumerMetadataIdentifier)); fileContent = waitSeconds(fileContent, 1000, zookeeperMetadataReport.getNodePath(consumerMetadataIdentifier)); - Assertions.assertNull(fileContent); + Assert.assertNull(fileContent); consumerMetadataIdentifier = storeConsumer(zookeeperMetadataReport, interfaceName, version, group, application); fileContent = zookeeperMetadataReport.zkClient.getContent(zookeeperMetadataReport.getNodePath(consumerMetadataIdentifier)); fileContent = waitSeconds(fileContent, 3000, zookeeperMetadataReport.getNodePath(consumerMetadataIdentifier)); - Assertions.assertNotNull(fileContent); - Assertions.assertEquals(fileContent, "{\"paramConsumerTest\":\"zkCm\"}"); + Assert.assertNotNull(fileContent); + Assert.assertEquals(fileContent, "{\"paramConsumerTest\":\"zkCm\"}"); } diff --git a/dubbo-metadata-report/pom.xml b/dubbo-metadata-report/pom.xml index aa14d56122e..0a7829056d3 100644 --- a/dubbo-metadata-report/pom.xml +++ b/dubbo-metadata-report/pom.xml @@ -14,11 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. --> - + dubbo-parent org.apache.dubbo - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT 4.0.0 @@ -29,7 +31,6 @@ dubbo-metadata-report-zookeeper dubbo-metadata-report-redis dubbo-metadata-definition - dubbo-metadata-report-consul diff --git a/dubbo-metrics/dubbo-metrics-api/pom.xml b/dubbo-metrics/dubbo-metrics-api/pom.xml new file mode 100644 index 00000000000..493955fb3e5 --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-api/pom.xml @@ -0,0 +1,30 @@ + + + + dubbo-metrics + org.apache.dubbo + 2.7.0-SNAPSHOT + + 4.0.0 + + dubbo-metrics-api + + + \ No newline at end of file diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/BucketCounter.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/BucketCounter.java new file mode 100644 index 00000000000..8c449fb4df0 --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/BucketCounter.java @@ -0,0 +1,57 @@ +/* + * 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 org.apache.dubbo.metrics; + +import java.util.Map; + +/** + * Store the count in multiple buckets, + * every event will go into one specific bucket according to the happening timestamp. + * The BucketCounter will reserve data for the last N time interval, + * older values will be automatically discarded. + */ +public interface BucketCounter extends Metric { + + /** + * update the counter to the given bucket + */ + void update(); + + /** + * update the counter to the given bucket + */ + void update(long n); + + /** + * Return the bucket count, keyed by timestamp + * @return the bucket count, keyed by timestamp + */ + Map getBucketCounts(); + + /** + * Return the bucket count, keyed by timestamp, since (including) the startTime. + * @param startTime the start time + * @return the bucket count, keyed by timestamp + */ + Map getBucketCounts(long startTime); + + /** + * Get the interval of the bucket + * @return the interval of the bucket + */ + int getBucketInterval(); +} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Compass.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Compass.java new file mode 100644 index 00000000000..eff395d111d --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Compass.java @@ -0,0 +1,75 @@ +/* + * 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 org.apache.dubbo.metrics; + +import java.util.Map; + +/** + * A metric that provides an easy way to collect method invocation, + * response time, success count, and error code count. + */ +public interface Compass extends Metric { + + /** + * record a method invocation with execution time and sub-categories + * @param duration must be milliseconds + * @param subCategory all the sub-categories should be orthogonal, + * which will be added up to the total number of method invocations + */ + void record(long duration, String subCategory); + + /** + * return method count per bucket per category + * @return + */ + Map> getMethodCountPerCategory(); + + /** + * return method count per bucket per category + * @return + */ + Map> getMethodCountPerCategory(long startTime); + + /** + * return method execution time per bucket per category + * @return + */ + Map> getMethodRtPerCategory(); + + /** + * return method execution time per bucket per category + * @return + */ + Map> getMethodRtPerCategory(long startTime); + + /** + * return method execution time and count per bucket per category + * @return + */ + Map> getCountAndRtPerCategory(); + + /** + * return method execution time and count per bucket per category + * @return + */ + Map> getCountAndRtPerCategory(long startTime); + + /** + * @return the bucket interval + */ + int getBucketInterval(); +} diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/RetryPolicy.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Counter.java similarity index 58% rename from dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/RetryPolicy.java rename to dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Counter.java index b1fc5256b1c..22be6620380 100644 --- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/RetryPolicy.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Counter.java @@ -14,18 +14,37 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.remoting.etcd; +package org.apache.dubbo.metrics; -public interface RetryPolicy { +/** + *
    + * An incrementing and decrementing counter metric.
    + * 
    + */ +public interface Counter extends Metric, Counting { + + /** + * Increment the counter by one. + */ + void inc(); + + /** + * Increment the counter by {@code n}. + * + * @param n the amount by which the counter will be increased + */ + void inc(long n); + + /** + * Decrement the counter by one. + */ + void dec(); /** - * Whether retry is supported when operation fails. + * Decrement the counter by {@code n}. * - * @param retried the number of times retried so far - * @param elapsed the elapsed time in millisecond since the operation was attempted - * @param sleep should be sleep - * @return true should be retry + * @param n the amount by which the counter will be decreased */ - public boolean shouldRetry(int retried, long elapsed, boolean sleep); + void dec(long n); -} +} \ No newline at end of file diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/DataListener.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Counting.java similarity index 78% rename from dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/DataListener.java rename to dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Counting.java index 95b948adafa..6795902e06b 100644 --- a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/DataListener.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Counting.java @@ -14,12 +14,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.remoting.zookeeper; +package org.apache.dubbo.metrics; /** - * 2019-02-26 + * An interface for metric types which have counts. */ -public interface DataListener { - - void dataChanged(String path, Object value, EventType eventType); +public interface Counting { + /** + * Returns the current count. + * + * @return the current count + */ + long getCount(); } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/provider/HelloServiceImpl.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Gauge.java similarity index 56% rename from dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/provider/HelloServiceImpl.java rename to dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Gauge.java index 10b00a8f5b3..a094225f671 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/provider/HelloServiceImpl.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Gauge.java @@ -14,21 +14,29 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.config.spring.context.context.annotation.provider; +package org.apache.dubbo.metrics; -import org.apache.dubbo.config.annotation.Service; -import org.apache.dubbo.config.spring.api.HelloService; /** - * {@link HelloService} Implementation just annotating Dubbo's {@link Service} + *
    + * A gauge metric is an instantaneous reading of a particular value. To instrument a queue's depth,
    + * for example:
      *
    - * @since 2.5.9
    + * final Queue<String> queue = new ConcurrentLinkedQueue<String>();
    + * final Gauge<Integer> queueDepth = new Gauge<Integer>() {
    + *     public Integer getValue() {
    + *         return queue.size();
    + *     }
    + * };
    + * 
    + * + * @param the type of the metric's value */ -@Service(interfaceName = "org.apache.dubbo.config.spring.api.HelloService") -public class HelloServiceImpl implements HelloService { - - @Override - public String sayHello(String name) { - return "Hello, " + name; - } +public interface Gauge extends Metric { + /** + * Returns the metric's current value. + * + * @return the metric's current value + */ + T getValue(); } diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/IMetricManager.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/IMetricManager.java new file mode 100644 index 00000000000..eef6720e2bc --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/IMetricManager.java @@ -0,0 +1,104 @@ +/* + * 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 org.apache.dubbo.metrics; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +public interface IMetricManager { + + /** + * Create a {@link Counter} metric in given group, and name. + * if not exist, an instance will be created. + * + * @param group the group of MetricRegistry + * @param name the name of the metric + * @return an instance of counter + */ + Counter getCounter(String group, MetricName name); + + /** + * Create a {@link BucketCounter} metric in given group, and name. + * if not exist, an instance will be created. + * + * @param group the group of MetricRegistry + * @param name the name of the metric + * @return an instance of {@link BucketCounter} + */ + BucketCounter getBucketCounter(String group, MetricName name); + + /** + * Create a {@link Compass} metric in give group, name, and type + * if not exist, an instance will be created. + * @param group the group of MetricRegistry + * @param name the name of the metric + * @return an instance of {@link Compass} + */ + Compass getCompass(String group, MetricName name); + + /** + * Register a customized metric to specified group. + * @param group: the group name of MetricRegistry + * @param metric the metric to register + */ + void register(String group, MetricName name, Metric metric); + + /** + * Get a list of group in current MetricManager + * @return a list of group name + */ + List listMetricGroups(); + + /** + * list all metric names by group + * @return a map of metric name set, keyed by group name + */ + Map> listMetricNamesByGroup(); + + /** + * Get metric registry by group name, + * if not found, null will be returned + * @param group the group name to query + * @return the MetricRegistry that is correspondent to the group + */ + MetricRegistry getMetricRegistryByGroup(String group); + + /** + * Get all the counters by the specific group and filter + * @param group the given group + * @param filter the given filter + * @return the MetricName to Counter map + */ + Map getCounters(String group, MetricFilter filter); + + /** + * Get all the compasses by the specific group and filter + * @param group the given group + * @param filter the given filter + * @return the MetricName to Compass map + */ + Map getCompasses(String group, MetricFilter filter); + + /** + * A map of metric names to metrics. + * + * @return all the metrics + */ + Map getMetrics(String group); + +} diff --git a/dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/service/DemoService.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Metric.java similarity index 74% rename from dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/service/DemoService.java rename to dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Metric.java index 149da77f8f1..70823f0998a 100644 --- a/dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/service/DemoService.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Metric.java @@ -14,17 +14,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.monitor.service; - -public interface DemoService { - - String sayName(String name); - - void timeoutException(); - - void throwDemoException() throws Exception; - - int echo(int i); +package org.apache.dubbo.metrics; +/** + * A tag interface to indicate that a class is a metric. + */ +public interface Metric { + /** + * Return the last update time in milliseconds + * @return the last updated time in milliseconds + */ + long lastUpdateTime(); } diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/AbstractRetryPolicy.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricFilter.java similarity index 53% rename from dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/AbstractRetryPolicy.java rename to dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricFilter.java index f626202467e..243c3b7813e 100644 --- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/AbstractRetryPolicy.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricFilter.java @@ -14,31 +14,29 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.remoting.etcd; +package org.apache.dubbo.metrics; -public abstract class AbstractRetryPolicy implements RetryPolicy { - - private final int maxRetried; - - protected AbstractRetryPolicy(int maxRetried) { - this.maxRetried = maxRetried; - } +/** + * A filter used to determine whether or not a metric should be reported, among other things. + */ +public interface MetricFilter { - public boolean shouldRetry(int retried, long elapsed, boolean sleep) { - if (retried < maxRetried) { - try { - if (sleep) { - Thread.sleep(getSleepTime(retried, elapsed)); - } - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return false; - } + /** + * Matches all metrics, regardless of type or name. + */ + MetricFilter ALL = new MetricFilter() { + @Override + public boolean matches(MetricName name, Metric metric) { return true; } - return false; - } - - protected abstract long getSleepTime(int retried, long elapsed); + }; + /** + * Returns {@code true} if the metric matches the filter; {@code false} otherwise. + * + * @param name the metric's name + * @param metric the metric + * @return {@code true} if the metric matches the filter + */ + boolean matches(MetricName name, Metric metric); } diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricLevel.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricLevel.java new file mode 100644 index 00000000000..66343070a3d --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricLevel.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 org.apache.dubbo.metrics; + +/** + * An enumeration class to represent the metric level + */ +public enum MetricLevel { + + TRIVIAL, // trivial metrics + + MINOR, // minor metrics + + NORMAL, // normal metrics + + MAJOR, // major metrics + + CRITICAL; // critical metrics + + static { + for (MetricLevel level : MetricLevel.values()) { + if (level.ordinal() < 0) { + throw new RuntimeException("MetricLevel can not < 0"); + } + } + } + + public static int getMaxValue() { + MetricLevel[] levels = MetricLevel.values(); + int max = levels[0].ordinal(); + for (MetricLevel level : levels) { + int value = level.ordinal(); + if (value > max) { + max = value; + } + } + return max; + } +} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricManager.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricManager.java new file mode 100644 index 00000000000..1c032ace08f --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricManager.java @@ -0,0 +1,108 @@ +/* + * 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 org.apache.dubbo.metrics; + +import java.lang.reflect.Method; + +/** + * The design concept is heavily borrowed from SLF4j (http://www.slf4j.org/), the logging framework. + * The application only depends on the metrics api. + * The implementation will be dynamically bound. + * If the implementation if not found in classpath, by default the {@link NOPMetricManager} will be bound. + */ +public class MetricManager { + + private static final String BINDER_CLASS = "org.apache.dubbo.metrics.MetricManagerBinder"; + + private static final IMetricManager NOP_METRIC_MANAGER = new NOPMetricManager(); + + private static volatile IMetricManager iMetricManager; + + /** + * Create a {@link Counter} metric in given group, and name. + * if not exist, an instance will be created. + * + * @param group the group of MetricRegistry + * @param name the name of the metric + * @return an instance of counter + */ + public static Counter getCounter(String group, MetricName name) { + IMetricManager manager = getIMetricManager(); + return manager.getCounter(group, name); + } + + /** + * Create a {@link BucketCounter} metric in given group, and name. + * if not exist, an instance will be created. + * + * @param group the group of MetricRegistry + * @param name the name of the metric + * @return an instance of {@link BucketCounter} + */ + public static BucketCounter getBucketCounters(String group, MetricName name) { + IMetricManager manager = getIMetricManager(); + return manager.getBucketCounter(group, name); + } + + /** + * Create a {@link Compass} metric in given group, and name + * if not exist, an instance will be created. + * + * @param group the group of MetricRegistry + * @param name the name of the metric + * @return an instance of {@link Compass} + */ + public static Compass getCompass(String group, MetricName name) { + IMetricManager manager = getIMetricManager(); + return manager.getCompass(group, name); + } + + /** + * Register a customized metric to specified group. + * @param group the group name of MetricRegistry + * @param metric the metric to register + */ + public static void register(String group, MetricName name, Metric metric) { + IMetricManager manager = getIMetricManager(); + manager.register(group, name, metric); + } + + /** + * get dynamically bound {@link IMetricManager} instance + * @return the {@link IMetricManager} instance bound + */ + @SuppressWarnings("unchecked") + public static IMetricManager getIMetricManager() { + if (iMetricManager == null) { + synchronized (MetricManager.class) { + if (iMetricManager == null) { + try { + Class binderClazz = MetricManager.class.getClassLoader().loadClass(BINDER_CLASS); + Method getSingleton = binderClazz.getMethod("getSingleton"); + Object binderObject = getSingleton.invoke(null); + Method getMetricManager = binderClazz.getMethod("getMetricManager"); + iMetricManager = (IMetricManager) getMetricManager.invoke(binderObject); + } catch (Exception e) { + iMetricManager = NOP_METRIC_MANAGER; + } + } + } + } + return iMetricManager; + } + +} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricName.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricName.java new file mode 100644 index 00000000000..ca7edb9292a --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricName.java @@ -0,0 +1,389 @@ +/* + * 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 org.apache.dubbo.metrics; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + +/** + * This class is based on Dropwizard metrics, see io/dropwizard/metrics/MetricName.java + * + * The following changes are made: + * * Add metric level + * * Cache the hash code + * + */ +public class MetricName implements Comparable { + + public static final String SEPARATOR = "."; + public static final Map EMPTY_TAGS = Collections.emptyMap(); + public static final MetricName EMPTY = new MetricName(); + + private final String key; + private final Map tags; + // the level to indicate the importance of a metric + private MetricLevel level; + + private int hashCode = 0; + + private boolean hashCodeCached = false; + + public MetricName() { + this(null, null, null); + } + + public MetricName(String key) { + this(key, null, null); + } + + public MetricName(String key, Map tags) { + this(key, tags, null); + } + + public MetricName(String key, MetricLevel level) { + this(key, null, level); + } + + public MetricName(String key, Map tags, MetricLevel level) { + this.key = key; + this.tags = checkTags(tags); + this.level = level == null ? MetricLevel.NORMAL : level; + } + + private Map checkTags(Map tags) { + if (tags == null || tags.isEmpty()) { + return EMPTY_TAGS; + } + + return Collections.unmodifiableMap(tags); + } + + public String getKey() { + return key; + } + + public Map getTags() { + return tags; + } + + /** + * Return the level of this metric + * The level indicates the importance of the metric + * + * @return when level tag do not exist or illegal tag, will return null. + */ + public MetricLevel getMetricLevel() { + return level; + } + + + /** + * Metric level can be changed during runtime + * @param level the level to set + */ + public MetricName level(MetricLevel level) { + this.level = level; + return this; + } + + + /** + * @see {@link #resolve(String, boolean)} + */ + public MetricName resolve(String p) { + return resolve(p, true); + } + + /** + * Build the MetricName that is this with another path appended to it. + * + * The new MetricName inherits the tags of this one. + * + * @param p The extra path element to add to the new metric. + * @param inheritTags if true, tags will be inherited + * @return A new metric name relative to the original by the path specified + * in p. + */ + public MetricName resolve(String p, boolean inheritTags) { + final String next; + + if (p != null && !p.isEmpty()) { + if (key != null && !key.isEmpty()) { + next = key + SEPARATOR + p; + } else { + next = p; + } + } else { + next = this.key; + } + + return inheritTags ? new MetricName(next, tags, level) : new MetricName(next, level); + } + + /** + * Add tags to a metric name and return the newly created MetricName. + * + * @param add Tags to add. + * @return A newly created metric name with the specified tags associated with it. + */ + public MetricName tag(Map add) { + final Map tags = new HashMap(add); + tags.putAll(this.tags); + return new MetricName(key, tags, level); + } + + /** + * Same as {@link #tag(Map)}, but takes a variadic list + * of arguments. + * + * @see #tag(Map) + * @param pairs An even list of strings acting as key-value pairs. + * @return A newly created metric name with the specified tags associated + * with it. + */ + public MetricName tag(String... pairs) { + if (pairs == null) { + return this; + } + + if (pairs.length % 2 != 0) { + throw new IllegalArgumentException("Argument count must be even"); + } + + final Map add = new HashMap(); + + for (int i = 0; i < pairs.length; i += 2) { + add.put(pairs[i], pairs[i+1]); + } + + return tag(add); + } + + /** + * Join the specified set of metric names. + * + * @param parts Multiple metric names to join using the separator. + * @return A newly created metric name which has the name of the specified + * parts and includes all tags of all child metric names. + **/ + public static MetricName join(MetricName... parts) { + final StringBuilder nameBuilder = new StringBuilder(); + final Map tags = new HashMap(); + + boolean first = true; + MetricName firstName = null; + + for (MetricName part : parts) { + final String name = part.getKey(); + + if (name != null && !name.isEmpty()) { + if (first) { + first = false; + firstName = part; + } else { + nameBuilder.append(SEPARATOR); + } + + nameBuilder.append(name); + } + + if (!part.getTags().isEmpty()) { + tags.putAll(part.getTags()); + } + } + + MetricLevel level = firstName == null ? null : firstName.getMetricLevel(); + return new MetricName(nameBuilder.toString(), tags, level); + } + + /** + * Build a new metric name using the specific path components. + * + * @param parts Path of the new metric name. + * @return A newly created metric name with the specified path. + **/ + public static MetricName build(String... parts) { + if (parts == null || parts.length == 0) { + return MetricName.EMPTY; + } + + if (parts.length == 1) { + return new MetricName(parts[0], EMPTY_TAGS); + } + + return new MetricName(buildName(parts), EMPTY_TAGS); + } + + private static String buildName(String... names) { + final StringBuilder builder = new StringBuilder(); + boolean first = true; + + for (String name : names) { + if (name == null || name.isEmpty()) { + continue; + } + + if (first) { + first = false; + } else { + builder.append(SEPARATOR); + } + + builder.append(name); + } + + return builder.toString(); + } + + @Override + public String toString() { + if (tags.isEmpty()) { + return key; + } + + return key + tags; + } + + @Override + public int hashCode() { + + if (!hashCodeCached){ + + final int prime = 31; + int result = 1; + result = prime * result + ((key == null) ? 0 : key.hashCode()); + result = prime * result + ((tags == null) ? 0 : tags.hashCode()); + + hashCode = result; + hashCodeCached = true; + } + + return hashCode; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj == null) { + return false; + } + + if (getClass() != obj.getClass()) { + return false; + } + + MetricName other = (MetricName) obj; + + if (key == null) { + if (other.key != null) { + return false; + } + } else if (!key.equals(other.key)) { + return false; + } + + if (!tags.equals(other.tags)) { + return false; + } + + return true; + } + + @Override + public int compareTo(MetricName o) { + if (o == null) { + return -1; + } + + int c = compareName(key, o.getKey()); + + if (c != 0) { + return c; + } + + return compareTags(tags, o.getTags()); + } + + private int compareName(String left, String right) { + if (left == null && right == null) { + return 0; + } + + if (left == null) { + return 1; + } + + if (right == null) { + return -1; + } + + return left.compareTo(right); + } + + private int compareTags(Map left, Map right) { + if (left == null && right == null) { + return 0; + } + + if (left == null) { + return 1; + } + + if (right == null) { + return -1; + } + + final Iterable keys = uniqueSortedKeys(left, right); + + for (final String key : keys) { + final String a = left.get(key); + final String b = right.get(key); + + if (a == null && b == null) { + continue; + } + + if (a == null) { + return -1; + } + + if (b == null) { + return 1; + } + + int c = a.compareTo(b); + + if (c != 0) { + return c; + } + } + + return 0; + } + + private Iterable uniqueSortedKeys(Map left, Map right) { + final Set set = new TreeSet(left.keySet()); + set.addAll(right.keySet()); + return set; + } +} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricRegistry.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricRegistry.java new file mode 100644 index 00000000000..567ee4aa733 --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricRegistry.java @@ -0,0 +1,157 @@ +/* + * 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 org.apache.dubbo.metrics; + +import java.util.Map; +import java.util.Set; + +/** + * A registry of metric instances. + */ +public abstract class MetricRegistry implements MetricSet { + + /** + * Given a {@link Metric}, registers it under the given name. + * + * @param name the name of the metric + * @param metric the metric + * @param the type of the metric + * @return {@code metric} + * @throws IllegalArgumentException if the name is already registered + */ + public abstract T register(String name, T metric) throws IllegalArgumentException; + + /** + * Given a {@link Metric}, registers it under the given name. + * + * @param name the name of the metric + * @param metric the metric + * @param the type of the metric + * @return {@code metric} + * @throws IllegalArgumentException if the name is already registered + */ + public abstract T register(MetricName name, T metric) throws IllegalArgumentException; + + /** + * Given a metric set, registers them. + * + * @param metrics a set of metrics + * @throws IllegalArgumentException if any of the names are already registered + */ + public abstract void registerAll(MetricSet metrics) throws IllegalArgumentException; + + /** + * Creates a new {@link Counter} and registers it under the given name. + * + * @param name the name of the metric + * @return a new {@link Counter} + */ + public abstract Counter counter(String name); + + /** + * Return the {@link Counter} registered under this name; or create and register + * a new {@link Counter} if none is registered. + * + * @param name the name of the metric + * @return a new or pre-existing {@link Counter} + */ + public abstract Counter counter(MetricName name); + + /** + * Create a FastCompass with given name + * @param name the name of the metric + * @return a FastCompass instance + */ + public abstract Compass compass(MetricName name); + + /** + * Removes the metric with the given name. + * + * @param name the name of the metric + * @return whether or not the metric was removed + */ + public abstract boolean remove(MetricName name); + + /** + * Removes all metrics which match the given filter. + * + * @param filter a filter + */ + public abstract void removeMatching(MetricFilter filter); + + /** + * Returns a set of the names of all the metrics in the registry. + * + * @return the names of all the metrics + */ + public abstract Set getNames(); + + /** + * Returns a map of all the gauges in the registry and their names. + * + * @return all the gauges in the registry + */ + public abstract Map getGauges(); + + /** + * Returns a map of all the gauges in the registry and their names which match the given filter. + * + * @param filter the metric filter to match + * @return all the gauges in the registry + */ + public abstract Map getGauges(MetricFilter filter); + + /** + * Returns a map of all the counters in the registry and their names. + * + * @return all the counters in the registry + */ + public abstract Map getCounters(); + + /** + * Returns a map of all the counters in the registry and their names which match the given + * filter. + * + * @param filter the metric filter to match + * @return all the counters in the registry + */ + public abstract Map getCounters(MetricFilter filter); + + /** + * Returns a map of all the compasses in the registry and their names. + * + * @return all the compasses in the registry + */ + public abstract Map getCompasses(); + + /** + * Returns a map of all the compasses in the registry and their names which match the given filter. + * + * @param filter the metric filter to match + * @return all the compasses in the registry + */ + public abstract Map getCompasses(MetricFilter filter); + + /** + * Returns a map of all the metrics in the registry and their names which match the given filter + * @param filter the metric filter to match + * @return all the metrics in the registry + */ + public abstract Map getMetrics(MetricFilter filter); + + +} diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/ConnectionStateListener.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricSet.java similarity index 73% rename from dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/ConnectionStateListener.java rename to dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricSet.java index 788aa401e9b..68c95a124e1 100644 --- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/ConnectionStateListener.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricSet.java @@ -14,18 +14,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package org.apache.dubbo.metrics; -package org.apache.dubbo.remoting.etcd.jetcd; - -import io.etcd.jetcd.Client; - -public interface ConnectionStateListener { +import java.util.Map; +/** + * A set of named metrics. + * + * @see MetricRegistry#registerAll(MetricSet) + */ +public interface MetricSet extends Metric { /** - * Called when there is a state change in the connection + * A map of metric names to metrics. * - * @param client the client - * @param newState the new state + * @return the metrics */ - void stateChanged(Client client, int newState); -} \ No newline at end of file + Map getMetrics(); +} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/NOPMetricManager.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/NOPMetricManager.java new file mode 100644 index 00000000000..2228fd774a7 --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/NOPMetricManager.java @@ -0,0 +1,292 @@ +/* + * 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 org.apache.dubbo.metrics; + +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * The empty implementation for IMetricManager + */ +public class NOPMetricManager implements IMetricManager { + + private static final Map emptyMap = new HashMap(); + private static final Set emptySet = new HashSet(); + + @Override + public Counter getCounter(String group, MetricName name) { + return NOP_COUNTER; + } + + @Override + public BucketCounter getBucketCounter(String group, MetricName name) { + return NOP_BUCKET_COUNTER; + } + + @Override + public Compass getCompass(String group, MetricName name) { + return NOP_COMPASS; + } + + @Override + public List listMetricGroups() { + return Collections.emptyList(); + } + + @Override + public Map> listMetricNamesByGroup() { + return Collections.emptyMap(); + } + + @Override + public MetricRegistry getMetricRegistryByGroup(String group) { + return NOP_REGISTRY; + } + + @Override + @SuppressWarnings("unchecked") + public Map getCounters(String group, MetricFilter filter) { + return emptyMap; + } + + @Override + @SuppressWarnings("unchecked") + public Map getCompasses(String group, MetricFilter filter) { + return emptyMap; + } + + @Override + public void register(String group, MetricName name, Metric metric) { + + } + + static final BucketCounter NOP_BUCKET_COUNTER = new BucketCounter() { + @Override + public void update() { + + } + + @Override + public void update(long n) { + + } + + @Override + public Map getBucketCounts() { + return emptyMap; + } + + @Override + public Map getBucketCounts(long startTime) { + return emptyMap; + } + + @Override + public int getBucketInterval() { + return 0; + } + + @Override + public long lastUpdateTime() { + return 0; + } + }; + + static final Counter NOP_COUNTER = new Counter() { + @Override + public void inc() { + } + + @Override + public void inc(long n) { + } + + @Override + public void dec() { + } + + @Override + public void dec(long n) { + } + + @Override + public long getCount() { + return 0; + } + + @Override + public long lastUpdateTime() { + return 0; + } + }; + + static final Compass NOP_COMPASS = new Compass() { + @Override + public void record(long duration, String subCategory) { + + } + + @Override + public Map> getMethodCountPerCategory() { + return emptyMap; + } + + @Override + public Map> getMethodRtPerCategory() { + return emptyMap; + } + + @Override + public Map> getMethodCountPerCategory(long startTime) { + return emptyMap; + } + + @Override + public Map> getMethodRtPerCategory(long startTime) { + return emptyMap; + } + + @Override + public int getBucketInterval() { + return 0; + } + + @Override + public Map> getCountAndRtPerCategory() { + return emptyMap; + } + + @Override + public Map> getCountAndRtPerCategory(long startTime) { + return emptyMap; + } + + @Override + public long lastUpdateTime() { + return 0; + } + }; + + private static final MetricRegistry NOP_REGISTRY = new MetricRegistry() { + @Override + public T register(String name, T metric) throws IllegalArgumentException { + return metric; + } + + @Override + public T register(MetricName name, T metric) throws IllegalArgumentException { + return metric; + } + + @Override + public void registerAll(MetricSet metrics) throws IllegalArgumentException { + + } + + @Override + public Counter counter(String name) { + return NOP_COUNTER; + } + + @Override + public Counter counter(MetricName name) { + return NOP_COUNTER; + } + + @Override + public Compass compass(MetricName name) { + return NOP_COMPASS; + } + + @Override + public boolean remove(MetricName name) { + return false; + } + + @Override + public void removeMatching(MetricFilter filter) { + + } + + @Override + @SuppressWarnings("unchecked") + public Set getNames() { + return emptySet; + } + + @Override + @SuppressWarnings("unchecked") + public Map getGauges() { + return emptyMap; + } + + @Override + @SuppressWarnings("unchecked") + public Map getGauges(MetricFilter filter) { + return emptyMap; + } + + @Override + @SuppressWarnings("unchecked") + public Map getCounters() { + return emptyMap; + } + + @Override + @SuppressWarnings("unchecked") + public Map getCounters(MetricFilter filter) { + return emptyMap; + } + + @Override + public Map getCompasses() { + return emptyMap; + } + + @Override + public Map getCompasses(MetricFilter filter) { + return emptyMap; + } + + @Override + @SuppressWarnings("unchecked") + public Map getMetrics(MetricFilter filter) { + return emptyMap; + } + + @Override + @SuppressWarnings("unchecked") + public Map getMetrics() { + return emptyMap; + } + + @Override + public long lastUpdateTime() { + return 0; + } + }; + + @Override + public Map getMetrics(String group) { + return emptyMap; + } + +} diff --git a/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/MetricManagerTest.java b/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/MetricManagerTest.java new file mode 100644 index 00000000000..366cffe9d2d --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/MetricManagerTest.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 org.apache.dubbo.metrics; + +import org.junit.Assert; +import org.junit.Test; + +public class MetricManagerTest { + + @Test + public void testNOPMetricManager() { + Assert.assertTrue(MetricManager.getIMetricManager() instanceof NOPMetricManager); + } + + @Test + public void testNOPCompass() { + Compass compass = MetricManager.getCompass("test", MetricName.build("test")); + compass.record(10, "success"); + + Assert.assertEquals(0, compass.getCountAndRtPerCategory().size()); + Assert.assertEquals(0, compass.getMethodCountPerCategory().size()); + Assert.assertEquals(0, compass.getMethodRtPerCategory().size()); + } + + @Test + public void testNopCounter() { + Counter counter = MetricManager.getCounter("test", MetricName.build("test2")); + counter.inc(); + Assert.assertEquals(0, counter.getCount()); + } + + @Test + public void testBucketCounter() { + BucketCounter bc = MetricManager.getBucketCounters("test", MetricName.build("test3")); + bc.update(); + Assert.assertEquals(0, bc.getBucketInterval()); + Assert.assertEquals(0, bc.getBucketCounts().size()); + } +} diff --git a/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/MetricNameTest.java b/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/MetricNameTest.java new file mode 100644 index 00000000000..d461c7bbf16 --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/MetricNameTest.java @@ -0,0 +1,138 @@ +/* + * 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 org.apache.dubbo.metrics; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + +public class MetricNameTest { + + @Test + public void testEmpty() { + Assert.assertEquals(MetricName.EMPTY.getTags(), MetricName.EMPTY_TAGS); + Assert.assertNull(MetricName.EMPTY.getKey()); + Assert.assertEquals(new MetricName().getTags(), MetricName.EMPTY_TAGS); + + Assert.assertEquals(MetricName.EMPTY, new MetricName()); + Assert.assertEquals(MetricName.build(), MetricName.EMPTY); + Assert.assertEquals(MetricName.EMPTY.resolve(null), MetricName.EMPTY); + } + + @Test + public void testEmptyResolve() { + final MetricName name = new MetricName(); + Assert.assertEquals(name.resolve("foo"), new MetricName("foo")); + } + + @Test + public void testResolveToEmpty() { + final MetricName name = new MetricName("foo"); + Assert.assertEquals(name.resolve(null), new MetricName("foo")); + } + + @Test + public void testResolve() { + final MetricName name = new MetricName("foo"); + Assert.assertEquals(name.resolve("bar"), new MetricName("foo.bar")); + } + + @Test + public void testResolveWithTags() { + final MetricName name = new MetricName("foo").tag("key", "value"); + Assert.assertEquals(name.resolve("bar"), new MetricName("foo.bar").tag("key", "value")); + } + + @Test + public void testResolveWithoutTags() { + final MetricName name = new MetricName("foo").tag("key", "value"); + Assert.assertEquals(name.resolve("bar", false), new MetricName("foo.bar")); + } + + @Test + public void testResolveBothEmpty() { + final MetricName name = new MetricName(null); + Assert.assertEquals(name.resolve(null), new MetricName()); + } + + @Test + public void testAddTagsVarious() { + final Map refTags = new HashMap(); + refTags.put("foo", "bar"); + final MetricName test = MetricName.EMPTY.tag("foo", "bar"); + final MetricName test2 = MetricName.EMPTY.tag(refTags); + + Assert.assertEquals(test, new MetricName(null, refTags)); + Assert.assertEquals(test.getTags(), refTags); + + Assert.assertEquals(test2, new MetricName(null, refTags)); + Assert.assertEquals(test2.getTags(), refTags); + } + + @Test + public void testTaggedMoreArguments() { + final Map refTags = new HashMap(); + refTags.put("foo", "bar"); + refTags.put("baz", "biz"); + Assert.assertEquals(MetricName.EMPTY.tag("foo", "bar", "baz", "biz").getTags(), refTags); + } + + @Test(expected=IllegalArgumentException.class) + public void testTaggedNotPairs() { + MetricName.EMPTY.tag("foo"); + } + + @Test(expected=IllegalArgumentException.class) + public void testTaggedNotPairs2() { + MetricName.EMPTY.tag("foo", "bar", "baz"); + } + + @Test + public void testCompareTo() { + final MetricName a = MetricName.EMPTY.tag("foo", "bar"); + final MetricName b = MetricName.EMPTY.tag("foo", "baz"); + + Assert.assertTrue(a.compareTo(b) < 0); + Assert.assertTrue(b.compareTo(a) > 0); + Assert.assertTrue(b.compareTo(b) == 0); + Assert.assertTrue(b.resolve("key").compareTo(b) < 0); + Assert.assertTrue(b.compareTo(b.resolve("key")) > 0); + } + + @Test + public void testTaggedWithLevel() { + MetricName name = MetricName.build("test").level(MetricLevel.CRITICAL); + MetricName tagged = name.tag("foo", "bar"); + Assert.assertEquals(tagged.getMetricLevel(), MetricLevel.CRITICAL); + } + + @Test + public void testJoinWithLevel() { + MetricName name = MetricName.build("test").level(MetricLevel.CRITICAL); + MetricName tagged = MetricName.join(name, MetricName.build("abc")); + Assert.assertEquals(tagged.getMetricLevel(), MetricLevel.CRITICAL); + } + + @Test + public void testResolveWithLevel() { + final MetricName name = new MetricName("foo").level(MetricLevel.CRITICAL).tag("key", "value"); + Assert.assertEquals(name.resolve("bar"), new MetricName("foo.bar").tag("key", "value").level(MetricLevel.CRITICAL)); + } +} diff --git a/dubbo-metrics/pom.xml b/dubbo-metrics/pom.xml new file mode 100644 index 00000000000..d76e4e737d3 --- /dev/null +++ b/dubbo-metrics/pom.xml @@ -0,0 +1,41 @@ + + + + org.apache.dubbo + dubbo-parent + 2.7.0-SNAPSHOT + + 4.0.0 + + dubbo-metrics + pom + ${project.artifactId} + The metrics module of dubbo project + + + false + + + + dubbo-metrics-api + + + + \ No newline at end of file diff --git a/dubbo-monitor/dubbo-monitor-api/pom.xml b/dubbo-monitor/dubbo-monitor-api/pom.xml index 5d649dc1fa4..899a511b0a8 100644 --- a/dubbo-monitor/dubbo-monitor-api/pom.xml +++ b/dubbo-monitor/dubbo-monitor-api/pom.xml @@ -1,25 +1,24 @@ 4.0.0 org.apache.dubbo dubbo-monitor - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-monitor-api jar @@ -34,20 +33,5 @@ dubbo-rpc-api ${project.parent.version} - - - com.alibaba.middleware - metrics-core-api - - - com.alibaba.middleware - metrics-core-impl - - - com.alibaba.middleware - metrics-common - - - - +
    \ No newline at end of file diff --git a/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/AbstractMonitorFactory.java b/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/AbstractMonitorFactory.java index 9708e905a49..74a129b99b5 100644 --- a/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/AbstractMonitorFactory.java +++ b/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/AbstractMonitorFactory.java @@ -85,8 +85,8 @@ public Monitor getMonitor(URL url) { final URL monitorUrl = url; final CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> AbstractMonitorFactory.this.createMonitor(monitorUrl)); - FUTURES.put(key, completableFuture); completableFuture.thenRunAsync(new MonitorListener(key), executor); + FUTURES.put(key, completableFuture); return null; } finally { diff --git a/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/MetricsFilter.java b/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/MetricsFilter.java deleted file mode 100644 index 648d2318208..00000000000 --- a/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/MetricsFilter.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * 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 org.apache.dubbo.monitor.support; - -import com.alibaba.metrics.FastCompass; -import com.alibaba.metrics.MetricLevel; -import com.alibaba.metrics.MetricManager; -import com.alibaba.metrics.MetricName; -import org.apache.dubbo.common.Constants; -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.rpc.Filter; -import org.apache.dubbo.rpc.Invocation; -import org.apache.dubbo.rpc.Invoker; -import org.apache.dubbo.rpc.Result; -import org.apache.dubbo.rpc.RpcContext; -import org.apache.dubbo.rpc.RpcException; -import org.apache.dubbo.rpc.support.RpcUtils; - -import java.util.HashMap; - -public class MetricsFilter implements Filter { - - private static final Logger logger = LoggerFactory.getLogger(MetricsFilter.class); - - @Override - public Result invoke(Invoker invoker, Invocation invocation) throws RpcException { - RpcContext context = RpcContext.getContext(); - boolean isProvider = context.isProviderSide(); - long start = System.currentTimeMillis(); - try { - Result result = invoker.invoke(invocation); // proceed invocation chain - long duration = System.currentTimeMillis() - start; - reportMetrics(invoker, invocation, duration, "success", isProvider); - return result; - } catch (RpcException e) { - long duration = System.currentTimeMillis() - start; - String result = "error"; - if (e.isTimeout()) { - result = "timeoutError"; - } - if (e.isBiz()) { - result = "bisError"; - } - if (e.isNetwork()) { - result = "networkError"; - } - if (e.isSerialization()) { - result = "serializationError"; - } - reportMetrics(invoker, invocation, duration, result, isProvider); - throw e; - } - } - - private void reportMetrics(Invoker invoker, Invocation invocation, long duration, String result, boolean isProvider) { - String serviceName = invoker.getInterface().getName(); - String methodName = RpcUtils.getMethodName(invocation); - MetricName global; - MetricName method; - if (isProvider) { - global = new MetricName(Constants.DUBBO_PROVIDER, MetricLevel.MAJOR); - method = new MetricName(Constants.DUBBO_PROVIDER_METHOD, new HashMap(4) { - { - put(Constants.SERVICE, serviceName); - put(Constants.METHOD, methodName); - } - }, MetricLevel.NORMAL); - } else { - global = new MetricName(Constants.DUBBO_CONSUMER, MetricLevel.MAJOR); - method = new MetricName(Constants.DUBBO_CONSUMER_METHOD, new HashMap(4) { - { - put(Constants.SERVICE, serviceName); - put(Constants.METHOD, methodName); - } - }, MetricLevel.NORMAL); - } - setCompassQuantity(Constants.DUBBO_GROUP, result, duration, global, method); - } - - private void setCompassQuantity(String groupName, String result, long duration, MetricName... metricNames) { - for (MetricName metricName : metricNames) { - FastCompass compass = MetricManager.getFastCompass(groupName, metricName); - compass.record(duration, result); - } - } - -} diff --git a/dubbo-monitor/dubbo-monitor-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.Filter b/dubbo-monitor/dubbo-monitor-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.Filter index 2407d6b7935..1fd771694f1 100644 --- a/dubbo-monitor/dubbo-monitor-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.Filter +++ b/dubbo-monitor/dubbo-monitor-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.Filter @@ -1,2 +1 @@ -monitor=org.apache.dubbo.monitor.support.MonitorFilter -metrics=org.apache.dubbo.monitor.support.MetricsFilter +monitor=org.apache.dubbo.monitor.support.MonitorFilter \ No newline at end of file diff --git a/dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/support/AbstractMonitorFactoryTest.java b/dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/support/AbstractMonitorFactoryTest.java index 06e1251dd97..65f80f9932d 100644 --- a/dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/support/AbstractMonitorFactoryTest.java +++ b/dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/support/AbstractMonitorFactoryTest.java @@ -21,8 +21,8 @@ import org.apache.dubbo.monitor.Monitor; import org.apache.dubbo.monitor.MonitorFactory; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.util.List; @@ -70,7 +70,7 @@ public void testMonitorFactoryCache() throws Exception { monitor1 = monitorFactory.getMonitor(url); monitor2 = monitorFactory.getMonitor(url); } - Assertions.assertEquals(monitor1, monitor2); + Assert.assertEquals(monitor1, monitor2); } @Test @@ -83,7 +83,7 @@ public void testMonitorFactoryIpCache() throws Exception { monitor1 = monitorFactory.getMonitor(url); monitor2 = monitorFactory.getMonitor(url); } - Assertions.assertEquals(monitor1, monitor2); + Assert.assertEquals(monitor1, monitor2); } @Test @@ -97,7 +97,7 @@ public void testMonitorFactoryGroupCache() throws Exception { monitor1 = monitorFactory.getMonitor(url1); monitor2 = monitorFactory.getMonitor(url2); } - Assertions.assertNotSame(monitor1, monitor2); + Assert.assertNotSame(monitor1, monitor2); } } diff --git a/dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/support/MetricsFilterTest.java b/dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/support/MetricsFilterTest.java deleted file mode 100644 index 8645aa28bd1..00000000000 --- a/dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/support/MetricsFilterTest.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * 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 org.apache.dubbo.monitor.support; - -import com.alibaba.metrics.FastCompass; -import com.alibaba.metrics.IMetricManager; -import com.alibaba.metrics.MetricLevel; -import com.alibaba.metrics.MetricManager; -import com.alibaba.metrics.MetricName; -import org.apache.dubbo.common.Constants; -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.utils.NetUtils; -import org.apache.dubbo.monitor.service.DemoService; -import org.apache.dubbo.rpc.Invocation; -import org.apache.dubbo.rpc.Invoker; -import org.apache.dubbo.rpc.Result; -import org.apache.dubbo.rpc.RpcContext; -import org.apache.dubbo.rpc.RpcException; -import org.apache.dubbo.rpc.RpcInvocation; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.HashMap; - -public class MetricsFilterTest { - - private final Invoker serviceInvoker = new Invoker() { - @Override - public Class getInterface() { - return DemoService.class; - } - - public URL getUrl() { - return URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/org.apache.dubbo.monitor.service.DemoService"); - } - - @Override - public boolean isAvailable() { - return false; - } - - public Result invoke(Invocation invocation) throws RpcException { - return null; - } - - @Override - public void destroy() { - } - }; - - private final Invoker timeoutInvoker = new Invoker() { - @Override - public Class getInterface() { - return DemoService.class; - } - - public URL getUrl() { - return URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/org.apache.dubbo.monitor.service.DemoService"); - } - - @Override - public boolean isAvailable() { - return false; - } - - public Result invoke(Invocation invocation) throws RpcException { - throw new RpcException(RpcException.TIMEOUT_EXCEPTION); - } - - @Override - public void destroy() { - } - }; - - @Test - public void testConsumerSuccess() throws Exception { - IMetricManager metricManager = MetricManager.getIMetricManager(); - metricManager.clear(); - MetricsFilter metricsFilter = new MetricsFilter(); - Invocation invocation = new RpcInvocation("sayName", new Class[0], new Object[0]); - RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345); - RpcContext.getContext().setUrl(serviceInvoker.getUrl().addParameter(Constants.SIDE_KEY, Constants.CONSUMER_SIDE)); - for (int i = 0; i < 100; i++) { - metricsFilter.invoke(serviceInvoker, invocation); - } - FastCompass dubboClient = metricManager.getFastCompass(Constants.DUBBO_GROUP, new MetricName(Constants.DUBBO_CONSUMER, MetricLevel.MAJOR)); - FastCompass dubboMethod = metricManager.getFastCompass(Constants.DUBBO_GROUP, new MetricName(Constants.DUBBO_CONSUMER_METHOD, new HashMap(4) { - { - put(Constants.SERVICE, "org.apache.dubbo.monitor.service.DemoService"); - put(Constants.METHOD, "sayName"); - } - }, MetricLevel.NORMAL)); - long timestamp = System.currentTimeMillis() / 5000 * 5000; - Assertions.assertEquals(100, dubboClient.getMethodCountPerCategory(0).get("success").get(timestamp)); - timestamp = timestamp / 15000 * 15000; - Assertions.assertEquals(100, dubboMethod.getMethodCountPerCategory(0).get("success").get(timestamp)); - - } - - @Test - public void testConsumerTimeout() { - IMetricManager metricManager = MetricManager.getIMetricManager(); - metricManager.clear(); - MetricsFilter metricsFilter = new MetricsFilter(); - Invocation invocation = new RpcInvocation("timeoutException", null, null); - RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345); - RpcContext.getContext().setUrl(timeoutInvoker.getUrl().addParameter(Constants.SIDE_KEY, Constants.CONSUMER_SIDE) - .addParameter(Constants.TIMEOUT_KEY, 300)); - for (int i = 0; i < 10; i++) { - try { - metricsFilter.invoke(timeoutInvoker, invocation); - } catch (RpcException e) { - //ignore - } - } - FastCompass dubboClient = metricManager.getFastCompass(Constants.DUBBO_GROUP, new MetricName(Constants.DUBBO_CONSUMER, MetricLevel.MAJOR)); - FastCompass dubboMethod = metricManager.getFastCompass(Constants.DUBBO_GROUP, new MetricName(Constants.DUBBO_CONSUMER_METHOD, new HashMap(4) { - { - put(Constants.SERVICE, "org.apache.dubbo.monitor.service.DemoService"); - put(Constants.METHOD, "timeoutException"); - } - }, MetricLevel.NORMAL)); - long timestamp = System.currentTimeMillis() / 5000 * 5000; - Assertions.assertEquals(10, dubboClient.getMethodCountPerCategory(0).get("timeoutError").get(timestamp)); - timestamp = timestamp / 15000 * 15000; - Assertions.assertEquals(10, dubboMethod.getMethodCountPerCategory(0).get("timeoutError").get(timestamp)); - - } - - @Test - public void testProviderSuccess() throws Exception { - IMetricManager metricManager = MetricManager.getIMetricManager(); - metricManager.clear(); - MetricsFilter metricsFilter = new MetricsFilter(); - Invocation invocation = new RpcInvocation("sayName", new Class[0], new Object[0]); - RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345); - RpcContext.getContext().setUrl(serviceInvoker.getUrl().addParameter(Constants.SIDE_KEY, Constants.PROVIDER)); - for (int i = 0; i < 100; i++) { - metricsFilter.invoke(serviceInvoker, invocation); - } - FastCompass dubboClient = metricManager.getFastCompass(Constants.DUBBO_GROUP, new MetricName(Constants.DUBBO_PROVIDER, MetricLevel.MAJOR)); - FastCompass dubboMethod = metricManager.getFastCompass(Constants.DUBBO_GROUP, new MetricName(Constants.DUBBO_PROVIDER_METHOD, new HashMap(4) { - { - put(Constants.SERVICE, "org.apache.dubbo.monitor.service.DemoService"); - put(Constants.METHOD, "sayName"); - } - }, MetricLevel.NORMAL)); - long timestamp = System.currentTimeMillis() / 5000 * 5000; - Assertions.assertEquals(100, dubboClient.getMethodCountPerCategory(0).get("success").get(timestamp)); - timestamp = timestamp / 15000 * 15000; - Assertions.assertEquals(100, dubboMethod.getMethodCountPerCategory(0).get("success").get(timestamp)); - } -} diff --git a/dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/support/MonitorFilterTest.java b/dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/support/MonitorFilterTest.java index 01ba57dd7b8..ae9a509178a 100644 --- a/dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/support/MonitorFilterTest.java +++ b/dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/support/MonitorFilterTest.java @@ -28,8 +28,8 @@ import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.RpcInvocation; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import org.mockito.Mockito; import java.io.UnsupportedEncodingException; @@ -119,16 +119,16 @@ public void testFilter() throws Exception { while (lastStatistics == null) { Thread.sleep(10); } - Assertions.assertEquals("abc", lastStatistics.getParameter(MonitorService.APPLICATION)); - Assertions.assertEquals(MonitorService.class.getName(), lastStatistics.getParameter(MonitorService.INTERFACE)); - Assertions.assertEquals("aaa", lastStatistics.getParameter(MonitorService.METHOD)); - Assertions.assertEquals(NetUtils.getLocalHost() + ":20880", lastStatistics.getParameter(MonitorService.PROVIDER)); - Assertions.assertEquals(NetUtils.getLocalHost(), lastStatistics.getAddress()); - Assertions.assertEquals(null, lastStatistics.getParameter(MonitorService.CONSUMER)); - Assertions.assertEquals(1, lastStatistics.getParameter(MonitorService.SUCCESS, 0)); - Assertions.assertEquals(0, lastStatistics.getParameter(MonitorService.FAILURE, 0)); - Assertions.assertEquals(1, lastStatistics.getParameter(MonitorService.CONCURRENT, 0)); - Assertions.assertEquals(invocation, lastInvocation); + Assert.assertEquals("abc", lastStatistics.getParameter(MonitorService.APPLICATION)); + Assert.assertEquals(MonitorService.class.getName(), lastStatistics.getParameter(MonitorService.INTERFACE)); + Assert.assertEquals("aaa", lastStatistics.getParameter(MonitorService.METHOD)); + Assert.assertEquals(NetUtils.getLocalHost() + ":20880", lastStatistics.getParameter(MonitorService.PROVIDER)); + Assert.assertEquals(NetUtils.getLocalHost(), lastStatistics.getAddress()); + Assert.assertEquals(null, lastStatistics.getParameter(MonitorService.CONSUMER)); + Assert.assertEquals(1, lastStatistics.getParameter(MonitorService.SUCCESS, 0)); + Assert.assertEquals(0, lastStatistics.getParameter(MonitorService.FAILURE, 0)); + Assert.assertEquals(1, lastStatistics.getParameter(MonitorService.CONCURRENT, 0)); + Assert.assertEquals(invocation, lastInvocation); } @Test @@ -155,16 +155,16 @@ public void testGenericFilter() throws Exception { while (lastStatistics == null) { Thread.sleep(10); } - Assertions.assertEquals("abc", lastStatistics.getParameter(MonitorService.APPLICATION)); - Assertions.assertEquals(MonitorService.class.getName(), lastStatistics.getParameter(MonitorService.INTERFACE)); - Assertions.assertEquals("xxx", lastStatistics.getParameter(MonitorService.METHOD)); - Assertions.assertEquals(NetUtils.getLocalHost() + ":20880", lastStatistics.getParameter(MonitorService.PROVIDER)); - Assertions.assertEquals(NetUtils.getLocalHost(), lastStatistics.getAddress()); - Assertions.assertEquals(null, lastStatistics.getParameter(MonitorService.CONSUMER)); - Assertions.assertEquals(1, lastStatistics.getParameter(MonitorService.SUCCESS, 0)); - Assertions.assertEquals(0, lastStatistics.getParameter(MonitorService.FAILURE, 0)); - Assertions.assertEquals(1, lastStatistics.getParameter(MonitorService.CONCURRENT, 0)); - Assertions.assertEquals(invocation, lastInvocation); + Assert.assertEquals("abc", lastStatistics.getParameter(MonitorService.APPLICATION)); + Assert.assertEquals(MonitorService.class.getName(), lastStatistics.getParameter(MonitorService.INTERFACE)); + Assert.assertEquals("xxx", lastStatistics.getParameter(MonitorService.METHOD)); + Assert.assertEquals(NetUtils.getLocalHost() + ":20880", lastStatistics.getParameter(MonitorService.PROVIDER)); + Assert.assertEquals(NetUtils.getLocalHost(), lastStatistics.getAddress()); + Assert.assertEquals(null, lastStatistics.getParameter(MonitorService.CONSUMER)); + Assert.assertEquals(1, lastStatistics.getParameter(MonitorService.SUCCESS, 0)); + Assert.assertEquals(0, lastStatistics.getParameter(MonitorService.FAILURE, 0)); + Assert.assertEquals(1, lastStatistics.getParameter(MonitorService.CONCURRENT, 0)); + Assert.assertEquals(invocation, lastInvocation); } @Test diff --git a/dubbo-monitor/dubbo-monitor-default/pom.xml b/dubbo-monitor/dubbo-monitor-default/pom.xml index a7ae0ecd00c..a786f42ffa1 100644 --- a/dubbo-monitor/dubbo-monitor-default/pom.xml +++ b/dubbo-monitor/dubbo-monitor-default/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-monitor - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-monitor-default jar diff --git a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitor.java b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitor.java index f7f0d87d8e1..04df72df97f 100644 --- a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitor.java +++ b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitor.java @@ -20,7 +20,6 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.ExecutorUtil; import org.apache.dubbo.common.utils.NamedThreadFactory; import org.apache.dubbo.monitor.Monitor; import org.apache.dubbo.monitor.MonitorService; @@ -210,7 +209,7 @@ public boolean isAvailable() { @Override public void destroy() { try { - ExecutorUtil.cancelScheduledFuture(sendFuture); + sendFuture.cancel(true); } catch (Throwable t) { logger.error("Unexpected error occur at cancel sender timer, cause: " + t.getMessage(), t); } diff --git a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java index 2c80bc4e5b5..e5637416305 100644 --- a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java +++ b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java @@ -1,71 +1,64 @@ -/* - * 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 org.apache.dubbo.monitor.dubbo; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.URLBuilder; -import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.monitor.Monitor; -import org.apache.dubbo.monitor.MonitorService; -import org.apache.dubbo.monitor.support.AbstractMonitorFactory; -import org.apache.dubbo.rpc.Invoker; -import org.apache.dubbo.rpc.Protocol; -import org.apache.dubbo.rpc.ProxyFactory; - -import static org.apache.dubbo.common.Constants.CHECK_KEY; -import static org.apache.dubbo.common.Constants.DUBBO_PROTOCOL; -import static org.apache.dubbo.common.Constants.PROTOCOL_KEY; -import static org.apache.dubbo.common.Constants.REFERENCE_FILTER_KEY; - -/** - * DefaultMonitorFactory - */ -public class DubboMonitorFactory extends AbstractMonitorFactory { - - private Protocol protocol; - - private ProxyFactory proxyFactory; - - public void setProtocol(Protocol protocol) { - this.protocol = protocol; - } - - public void setProxyFactory(ProxyFactory proxyFactory) { - this.proxyFactory = proxyFactory; - } - - @Override - protected Monitor createMonitor(URL url) { - URLBuilder urlBuilder = URLBuilder.from(url); - urlBuilder.setProtocol(url.getParameter(PROTOCOL_KEY, DUBBO_PROTOCOL)); - if (StringUtils.isEmpty(url.getPath())) { - urlBuilder.setPath(MonitorService.class.getName()); - } - String filter = url.getParameter(REFERENCE_FILTER_KEY); - if (StringUtils.isEmpty(filter)) { - filter = ""; - } else { - filter = filter + ","; - } - urlBuilder.addParameters(CHECK_KEY, String.valueOf(false), - REFERENCE_FILTER_KEY, filter + "-monitor"); - Invoker monitorInvoker = protocol.refer(MonitorService.class, urlBuilder.build()); - MonitorService monitorService = proxyFactory.getProxy(monitorInvoker); - return new DubboMonitor(monitorInvoker, monitorService); - } - -} +/* + * 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 org.apache.dubbo.monitor.dubbo; + +import org.apache.dubbo.common.Constants; +import org.apache.dubbo.common.URL; +import org.apache.dubbo.monitor.Monitor; +import org.apache.dubbo.monitor.MonitorService; +import org.apache.dubbo.monitor.support.AbstractMonitorFactory; +import org.apache.dubbo.rpc.Invoker; +import org.apache.dubbo.rpc.Protocol; +import org.apache.dubbo.rpc.ProxyFactory; + +/** + * DefaultMonitorFactory + */ +public class DubboMonitorFactory extends AbstractMonitorFactory { + + private Protocol protocol; + + private ProxyFactory proxyFactory; + + public void setProtocol(Protocol protocol) { + this.protocol = protocol; + } + + public void setProxyFactory(ProxyFactory proxyFactory) { + this.proxyFactory = proxyFactory; + } + + @Override + protected Monitor createMonitor(URL url) { + url = url.setProtocol(url.getParameter(Constants.PROTOCOL_KEY, "dubbo")); + if (url.getPath() == null || url.getPath().length() == 0) { + url = url.setPath(MonitorService.class.getName()); + } + String filter = url.getParameter(Constants.REFERENCE_FILTER_KEY); + if (filter == null || filter.length() == 0) { + filter = ""; + } else { + filter = filter + ","; + } + url = url.addParameters(Constants.CLUSTER_KEY, "failsafe", Constants.CHECK_KEY, String.valueOf(false), + Constants.REFERENCE_FILTER_KEY, filter + "-monitor"); + Invoker monitorInvoker = protocol.refer(MonitorService.class, url); + MonitorService monitorService = proxyFactory.getProxy(monitorInvoker); + return new DubboMonitor(monitorInvoker, monitorService); + } + +} \ No newline at end of file diff --git a/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactoryTest.java b/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactoryTest.java index a3a1b176602..bb3c5bb556b 100644 --- a/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactoryTest.java +++ b/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactoryTest.java @@ -22,15 +22,15 @@ import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.ProxyFactory; import org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Before; +import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.Matchers.not; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.verify; import static org.mockito.MockitoAnnotations.initMocks; @@ -40,7 +40,7 @@ public class DubboMonitorFactoryTest { @Mock private ProxyFactory proxyFactory; - @BeforeEach + @Before public void setUp() throws Exception { initMocks(this); this.dubboMonitorFactory = new DubboMonitorFactory(); diff --git a/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/DubboMonitorTest.java b/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/DubboMonitorTest.java index 67c7a5a4eb6..08493d7d92d 100644 --- a/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/DubboMonitorTest.java +++ b/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/DubboMonitorTest.java @@ -16,9 +16,7 @@ */ package org.apache.dubbo.monitor.dubbo; -import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.monitor.Monitor; import org.apache.dubbo.monitor.MonitorFactory; @@ -31,8 +29,8 @@ import org.apache.dubbo.rpc.Result; import org.apache.dubbo.rpc.RpcException; import org.hamcrest.CustomMatcher; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import org.mockito.ArgumentCaptor; import java.util.Arrays; @@ -42,7 +40,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.Matchers.not; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.atLeastOnce; @@ -61,7 +59,7 @@ public Class getInterface() { } public URL getUrl() { - return URL.valueOf("dubbo://127.0.0.1:7070?interval=1000"); + return URL.valueOf("dubbo://127.0.0.1:7070?interval=20"); } @Override @@ -93,7 +91,7 @@ public List lookup(URL query) { @Test public void testCount() throws Exception { DubboMonitor monitor = new DubboMonitor(monitorInvoker, monitorService); - URL statistics = new URLBuilder(Constants.DUBBO_PROTOCOL, "10.20.153.10", 0) + URL statistics = new URL("dubbo", "10.20.153.10", 0) .addParameter(MonitorService.APPLICATION, "morgan") .addParameter(MonitorService.INTERFACE, "MemberService") .addParameter(MonitorService.METHOD, "findPerson") @@ -103,33 +101,31 @@ public void testCount() throws Exception { .addParameter(MonitorService.ELAPSED, 3) .addParameter(MonitorService.MAX_ELAPSED, 3) .addParameter(MonitorService.CONCURRENT, 1) - .addParameter(MonitorService.MAX_CONCURRENT, 1) - .build(); + .addParameter(MonitorService.MAX_CONCURRENT, 1); monitor.collect(statistics); - monitor.send(); while (lastStatistics == null) { Thread.sleep(10); } - Assertions.assertEquals("morgan", lastStatistics.getParameter(MonitorService.APPLICATION)); - Assertions.assertEquals("dubbo", lastStatistics.getProtocol()); - Assertions.assertEquals("10.20.153.10", lastStatistics.getHost()); - Assertions.assertEquals("morgan", lastStatistics.getParameter(MonitorService.APPLICATION)); - Assertions.assertEquals("MemberService", lastStatistics.getParameter(MonitorService.INTERFACE)); - Assertions.assertEquals("findPerson", lastStatistics.getParameter(MonitorService.METHOD)); - Assertions.assertEquals("10.20.153.11", lastStatistics.getParameter(MonitorService.CONSUMER)); - Assertions.assertEquals("1", lastStatistics.getParameter(MonitorService.SUCCESS)); - Assertions.assertEquals("0", lastStatistics.getParameter(MonitorService.FAILURE)); - Assertions.assertEquals("3", lastStatistics.getParameter(MonitorService.ELAPSED)); - Assertions.assertEquals("3", lastStatistics.getParameter(MonitorService.MAX_ELAPSED)); - Assertions.assertEquals("1", lastStatistics.getParameter(MonitorService.CONCURRENT)); - Assertions.assertEquals("1", lastStatistics.getParameter(MonitorService.MAX_CONCURRENT)); + Assert.assertEquals(lastStatistics.getParameter(MonitorService.APPLICATION), "morgan"); + Assert.assertEquals(lastStatistics.getProtocol(), "dubbo"); + Assert.assertEquals(lastStatistics.getHost(), "10.20.153.10"); + Assert.assertEquals(lastStatistics.getParameter(MonitorService.APPLICATION), "morgan"); + Assert.assertEquals(lastStatistics.getParameter(MonitorService.INTERFACE), "MemberService"); + Assert.assertEquals(lastStatistics.getParameter(MonitorService.METHOD), "findPerson"); + Assert.assertEquals(lastStatistics.getParameter(MonitorService.CONSUMER), "10.20.153.11"); + Assert.assertEquals(lastStatistics.getParameter(MonitorService.SUCCESS), "1"); + Assert.assertEquals(lastStatistics.getParameter(MonitorService.FAILURE), "0"); + Assert.assertEquals(lastStatistics.getParameter(MonitorService.ELAPSED), "3"); + Assert.assertEquals(lastStatistics.getParameter(MonitorService.MAX_ELAPSED), "3"); + Assert.assertEquals(lastStatistics.getParameter(MonitorService.CONCURRENT), "1"); + Assert.assertEquals(lastStatistics.getParameter(MonitorService.MAX_CONCURRENT), "1"); monitor.destroy(); } @Test public void testMonitorFactory() throws Exception { MockMonitorService monitorService = new MockMonitorService(); - URL statistics = new URLBuilder(Constants.DUBBO_PROTOCOL, "10.20.153.10", 0) + URL statistics = new URL("dubbo", "10.20.153.10", 0) .addParameter(MonitorService.APPLICATION, "morgan") .addParameter(MonitorService.INTERFACE, "MemberService") .addParameter(MonitorService.METHOD, "findPerson") @@ -139,8 +135,7 @@ public void testMonitorFactory() throws Exception { .addParameter(MonitorService.ELAPSED, 3) .addParameter(MonitorService.MAX_ELAPSED, 3) .addParameter(MonitorService.CONCURRENT, 1) - .addParameter(MonitorService.MAX_CONCURRENT, 1) - .build(); + .addParameter(MonitorService.MAX_CONCURRENT, 1); Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); @@ -163,14 +158,14 @@ public void testMonitorFactory() throws Exception { Thread.sleep(10); } URL result = monitorService.getStatistics(); - Assertions.assertEquals(1, result.getParameter(MonitorService.SUCCESS, 0)); - Assertions.assertEquals(3, result.getParameter(MonitorService.ELAPSED, 0)); + Assert.assertEquals(1, result.getParameter(MonitorService.SUCCESS, 0)); + Assert.assertEquals(3, result.getParameter(MonitorService.ELAPSED, 0)); } finally { monitor.destroy(); } break; } - Assertions.assertNotNull(monitor); + Assert.assertNotNull(monitor); } finally { exporter.unexport(); } @@ -191,7 +186,7 @@ public void testAvailable() { @Test public void testSum() { - URL statistics = new URLBuilder(Constants.DUBBO_PROTOCOL, "10.20.153.11", 0) + URL statistics = new URL("dubbo", "10.20.153.11", 0) .addParameter(MonitorService.APPLICATION, "morgan") .addParameter(MonitorService.INTERFACE, "MemberService") .addParameter(MonitorService.METHOD, "findPerson") @@ -201,8 +196,7 @@ public void testSum() { .addParameter(MonitorService.ELAPSED, 3) .addParameter(MonitorService.MAX_ELAPSED, 3) .addParameter(MonitorService.CONCURRENT, 1) - .addParameter(MonitorService.MAX_CONCURRENT, 1) - .build(); + .addParameter(MonitorService.MAX_CONCURRENT, 1); Invoker invoker = mock(Invoker.class); MonitorService monitorService = mock(MonitorService.class); diff --git a/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/StatisticsTest.java b/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/StatisticsTest.java index e2bdc897718..cd182c043ea 100644 --- a/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/StatisticsTest.java +++ b/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/StatisticsTest.java @@ -16,13 +16,10 @@ */ package org.apache.dubbo.monitor.dubbo; -import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.monitor.MonitorService; - -import org.hamcrest.MatcherAssert; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; @@ -32,7 +29,7 @@ public class StatisticsTest { @Test public void testEquals() { - URL statistics = new URLBuilder(Constants.DUBBO_PROTOCOL, "10.20.153.10", 0) + URL statistics = new URL("dubbo", "10.20.153.10", 0) .addParameter(MonitorService.APPLICATION, "morgan") .addParameter(MonitorService.INTERFACE, "MemberService") .addParameter(MonitorService.METHOD, "findPerson") @@ -42,26 +39,25 @@ public void testEquals() { .addParameter(MonitorService.ELAPSED, 3) .addParameter(MonitorService.MAX_ELAPSED, 3) .addParameter(MonitorService.CONCURRENT, 1) - .addParameter(MonitorService.MAX_CONCURRENT, 1) - .build(); + .addParameter(MonitorService.MAX_CONCURRENT, 1); Statistics statistics1 = new Statistics(statistics); Statistics statistics2 = new Statistics(statistics); - MatcherAssert.assertThat(statistics1, equalTo(statistics1)); - MatcherAssert.assertThat(statistics1, equalTo(statistics2)); + Assert.assertThat(statistics1, equalTo(statistics1)); + Assert.assertThat(statistics1, equalTo(statistics2)); statistics1.setVersion("2"); - MatcherAssert.assertThat(statistics1, not(equalTo(statistics2))); - MatcherAssert.assertThat(statistics1.hashCode(), not(equalTo(statistics2.hashCode()))); + Assert.assertThat(statistics1, not(equalTo(statistics2))); + Assert.assertThat(statistics1.hashCode(), not(equalTo(statistics2.hashCode()))); statistics1.setMethod("anotherMethod"); - MatcherAssert.assertThat(statistics1, not(equalTo(statistics2))); - MatcherAssert.assertThat(statistics1.hashCode(), not(equalTo(statistics2.hashCode()))); + Assert.assertThat(statistics1, not(equalTo(statistics2))); + Assert.assertThat(statistics1.hashCode(), not(equalTo(statistics2.hashCode()))); statistics1.setClient("anotherClient"); - MatcherAssert.assertThat(statistics1, not(equalTo(statistics2))); - MatcherAssert.assertThat(statistics1.hashCode(), not(equalTo(statistics2.hashCode()))); + Assert.assertThat(statistics1, not(equalTo(statistics2))); + Assert.assertThat(statistics1.hashCode(), not(equalTo(statistics2.hashCode()))); } @Test @@ -74,7 +70,7 @@ public void testToString() { statistics.setService("MemberService"); assertThat(statistics.toString(), is("dubbo://10.20.153.10")); - Statistics statisticsWithDetailInfo = new Statistics(new URLBuilder(Constants.DUBBO_PROTOCOL, "10.20.153.10", 0) + Statistics statisticsWithDetailInfo = new Statistics(new URL("dubbo", "10.20.153.10", 0) .addParameter(MonitorService.APPLICATION, "morgan") .addParameter(MonitorService.INTERFACE, "MemberService") .addParameter(MonitorService.METHOD, "findPerson") @@ -85,14 +81,13 @@ public void testToString() { .addParameter(MonitorService.ELAPSED, 3) .addParameter(MonitorService.MAX_ELAPSED, 3) .addParameter(MonitorService.CONCURRENT, 1) - .addParameter(MonitorService.MAX_CONCURRENT, 1) - .build()); + .addParameter(MonitorService.MAX_CONCURRENT, 1)); - MatcherAssert.assertThat(statisticsWithDetailInfo.getServer(), equalTo(statistics.getServer())); - MatcherAssert.assertThat(statisticsWithDetailInfo.getService(), equalTo(statistics.getService())); - MatcherAssert.assertThat(statisticsWithDetailInfo.getMethod(), equalTo(statistics.getMethod())); + Assert.assertThat(statisticsWithDetailInfo.getServer(), equalTo(statistics.getServer())); + Assert.assertThat(statisticsWithDetailInfo.getService(), equalTo(statistics.getService())); + Assert.assertThat(statisticsWithDetailInfo.getMethod(), equalTo(statistics.getMethod())); - MatcherAssert.assertThat(statisticsWithDetailInfo.getGroup(), equalTo(statistics.getGroup())); - MatcherAssert.assertThat(statisticsWithDetailInfo, not(equalTo(statistics))); + Assert.assertThat(statisticsWithDetailInfo.getGroup(), equalTo(statistics.getGroup())); + Assert.assertThat(statisticsWithDetailInfo, not(equalTo(statistics))); } -} +} \ No newline at end of file diff --git a/dubbo-monitor/pom.xml b/dubbo-monitor/pom.xml index 6e0171d303c..37923da0f29 100644 --- a/dubbo-monitor/pom.xml +++ b/dubbo-monitor/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-parent - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-monitor pom diff --git a/dubbo-plugin/dubbo-qos/pom.xml b/dubbo-plugin/dubbo-qos/pom.xml index 717c20ceac1..a5fd3950f82 100644 --- a/dubbo-plugin/dubbo-qos/pom.xml +++ b/dubbo-plugin/dubbo-qos/pom.xml @@ -18,7 +18,7 @@ org.apache.dubbo dubbo-plugin - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT 4.0.0 diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Online.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Online.java index 30ba14b6fe0..ce92d612827 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Online.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Online.java @@ -19,7 +19,6 @@ import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.ArrayUtils; import org.apache.dubbo.qos.command.BaseCommand; import org.apache.dubbo.qos.command.CommandContext; import org.apache.dubbo.qos.command.annotation.Cmd; @@ -45,7 +44,7 @@ public class Online implements BaseCommand { public String execute(CommandContext commandContext, String[] args) { logger.info("receive online command"); String servicePattern = ".*"; - if (ArrayUtils.isNotEmpty(args)) { + if (args != null && args.length > 0) { servicePattern = args[0]; } diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/protocol/QosProtocolWrapper.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/protocol/QosProtocolWrapper.java index ae0b22eeeed..6c2fbaed91e 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/protocol/QosProtocolWrapper.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/protocol/QosProtocolWrapper.java @@ -80,11 +80,7 @@ public void destroy() { private void startQosServer(URL url) { try { - if (!hasStarted.compareAndSet(false, true)) { - return; - } - - boolean qosEnable = url.getParameter(QOS_ENABLE, true); + boolean qosEnable = url.getParameter(QOS_ENABLE,true); if (!qosEnable) { logger.info("qos won't be started because it is disabled. " + "Please check dubbo.application.qos.enable is configured either in system property, " + @@ -92,8 +88,12 @@ private void startQosServer(URL url) { return; } + if (!hasStarted.compareAndSet(false, true)) { + return; + } + int port = url.getParameter(QOS_PORT, QosConstants.DEFAULT_PORT); - boolean acceptForeignIp = Boolean.parseBoolean(url.getParameter(ACCEPT_FOREIGN_IP, "false")); + boolean acceptForeignIp = Boolean.parseBoolean(url.getParameter(ACCEPT_FOREIGN_IP,"false")); Server server = Server.getInstance(); server.setPort(port); server.setAcceptForeignIp(acceptForeignIp); diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/Server.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/Server.java index 53bbc81493a..b39221dcbf8 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/Server.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/Server.java @@ -82,7 +82,7 @@ public void start() throws Throwable { if (!started.compareAndSet(false, true)) { return; } - boss = new NioEventLoopGroup(1, new DefaultThreadFactory("qos-boss", true)); + boss = new NioEventLoopGroup(0, new DefaultThreadFactory("qos-boss", true)); worker = new NioEventLoopGroup(0, new DefaultThreadFactory("qos-worker", true)); ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(boss, worker); diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/handler/HttpProcessHandler.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/handler/HttpProcessHandler.java index 408e1857c96..9a36c9758c0 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/handler/HttpProcessHandler.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/handler/HttpProcessHandler.java @@ -58,27 +58,27 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws E // return 404 when fail to construct command context if (commandContext == null) { log.warn("can not found commandContext url: " + msg.getUri()); - FullHttpResponse response = http404(); + FullHttpResponse response = http_404(); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } else { commandContext.setRemote(ctx.channel()); try { String result = commandExecutor.execute(commandContext); - FullHttpResponse response = http200(result); + FullHttpResponse response = http_200(result); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } catch (NoSuchCommandException ex) { log.error("can not find commandContext: " + commandContext, ex); - FullHttpResponse response = http404(); + FullHttpResponse response = http_404(); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } catch (Exception qosEx) { log.error("execute commandContext: " + commandContext + " got exception", qosEx); - FullHttpResponse response = http500(qosEx.getMessage()); + FullHttpResponse response = http_500(qosEx.getMessage()); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } } } - private static final FullHttpResponse http200(String result) { + private static final FullHttpResponse http_200(String result) { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(result.getBytes())); HttpHeaders httpHeaders = response.headers(); @@ -87,7 +87,7 @@ private static final FullHttpResponse http200(String result) { return response; } - private static final FullHttpResponse http404() { + private static final FullHttpResponse http_404() { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND); HttpHeaders httpHeaders = response.headers(); httpHeaders.set(HttpHeaders.Names.CONTENT_TYPE, "text/plain"); @@ -95,7 +95,7 @@ private static final FullHttpResponse http404() { return response; } - private static final FullHttpResponse http500(String errorMessage) { + private static final FullHttpResponse http_500(String errorMessage) { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR , Unpooled.wrappedBuffer(errorMessage.getBytes())); HttpHeaders httpHeaders = response.headers(); diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/handler/QosProcessHandler.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/handler/QosProcessHandler.java index 9312bb7e4f6..80274173c66 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/handler/QosProcessHandler.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/handler/QosProcessHandler.java @@ -26,11 +26,9 @@ import io.netty.handler.codec.http.HttpServerCodec; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; -import io.netty.handler.timeout.IdleStateEvent; import io.netty.handler.timeout.IdleStateHandler; import io.netty.util.CharsetUtil; import io.netty.util.concurrent.ScheduledFuture; -import org.apache.dubbo.common.utils.ExecutorUtil; import java.util.List; import java.util.concurrent.TimeUnit; @@ -95,14 +93,6 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) t } } - @Override - public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { - if (evt instanceof IdleStateEvent) { - ExecutorUtil.cancelScheduledFuture(welcomeFuture); - ctx.close(); - } - } - // G for GET, and P for POST private static boolean isHttp(int magic) { return magic == 'G' || magic == 'P'; diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/textui/TKv.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/textui/TKv.java index 483b3f63dbf..5bf517c6475 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/textui/TKv.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/textui/TKv.java @@ -59,7 +59,9 @@ public String rendering() { private String filterEmptyLine(String content) { final StringBuilder sb = new StringBuilder(); - try (Scanner scanner = new Scanner(content)) { + Scanner scanner = null; + try { + scanner = new Scanner(content); while (scanner.hasNextLine()) { String line = scanner.nextLine(); if (line != null) { @@ -71,6 +73,10 @@ private String filterEmptyLine(String content) { } sb.append(line).append(System.lineSeparator()); } + } finally { + if (null != scanner) { + scanner.close(); + } } return sb.toString(); diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/textui/TTable.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/textui/TTable.java index c9a67157282..051ae62c323 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/textui/TTable.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/textui/TTable.java @@ -419,10 +419,13 @@ private static String replaceTab(String string) { */ private static int width(String string) { int maxWidth = 0; - try (Scanner scanner = new Scanner(new StringReader(string))) { + final Scanner scanner = new Scanner(new StringReader(string)); + try { while (scanner.hasNextLine()) { maxWidth = max(length(scanner.nextLine()), maxWidth); } + } finally { + scanner.close(); } return maxWidth; } diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/textui/TTree.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/textui/TTree.java index 50dca53f537..b4aaf986de2 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/textui/TTree.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/textui/TTree.java @@ -78,14 +78,16 @@ public void callback(int deep, boolean isLast, String prefix, Node node) { treeSB.append(costPrefix); } - try (Scanner scanner = new Scanner(new StringReader(node.data.toString()))) { + final Scanner scanner = new Scanner(new StringReader(node.data.toString())); + try { boolean isFirst = true; while (scanner.hasNextLine()) { if (isFirst) { treeSB.append(scanner.nextLine()).append("\n"); isFirst = false; } else { - treeSB.append(prefix) + treeSB + .append(prefix) .append(repeat(' ', stepStringLength)) .append(hasChild ? "|" : EMPTY) .append(repeat(' ', costPrefixLength)) @@ -93,6 +95,8 @@ public void callback(int deep, boolean isLast, String prefix, Node node) { .append(System.lineSeparator()); } } + } finally { + scanner.close(); } } diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/CommandContextFactoryTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/CommandContextFactoryTest.java index e65c3cd78e6..3ed31840aaa 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/CommandContextFactoryTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/CommandContextFactoryTest.java @@ -18,11 +18,11 @@ package org.apache.dubbo.qos.command; import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; public class CommandContextFactoryTest { @Test diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/CommandContextTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/CommandContextTest.java index 5dec30d3394..5ec78b0b3bc 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/CommandContextTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/CommandContextTest.java @@ -18,15 +18,15 @@ package org.apache.dubbo.qos.command; import io.netty.channel.Channel; -import org.junit.jupiter.api.Test; +import org.junit.Test; import org.mockito.Mockito; -import static org.junit.jupiter.api.Assertions.assertFalse; +import static junit.framework.TestCase.assertFalse; import static org.hamcrest.Matchers.arrayContaining; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; public class CommandContextTest { @Test diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/DefaultCommandExecutorTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/DefaultCommandExecutorTest.java index 65339009233..c1c0e3f8adc 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/DefaultCommandExecutorTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/DefaultCommandExecutorTest.java @@ -17,19 +17,16 @@ package org.apache.dubbo.qos.command; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; public class DefaultCommandExecutorTest { - @Test + @Test(expected = NoSuchCommandException.class) public void testExecute1() throws Exception { - Assertions.assertThrows(NoSuchCommandException.class, () -> { - DefaultCommandExecutor executor = new DefaultCommandExecutor(); - executor.execute(CommandContextFactory.newInstance("not-exit")); - }); + DefaultCommandExecutor executor = new DefaultCommandExecutor(); + executor.execute(CommandContextFactory.newInstance("not-exit")); } @Test diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/GreetingCommand.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/GreetingCommand.java index 221a81c0d6c..ff49804b750 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/GreetingCommand.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/GreetingCommand.java @@ -17,7 +17,6 @@ package org.apache.dubbo.qos.command; -import org.apache.dubbo.common.utils.ArrayUtils; import org.apache.dubbo.qos.command.annotation.Cmd; @@ -25,6 +24,6 @@ public class GreetingCommand implements BaseCommand { @Override public String execute(CommandContext commandContext, String[] args) { - return ArrayUtils.isNotEmpty(args) ? "greeting " + args[0] : "greeting"; + return args != null && args.length > 0 ? "greeting " + args[0] : "greeting"; } } diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/decoder/HttpCommandDecoderTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/decoder/HttpCommandDecoderTest.java index b66fbba202c..d1613cef6e1 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/decoder/HttpCommandDecoderTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/decoder/HttpCommandDecoderTest.java @@ -23,14 +23,14 @@ import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpRequest; import org.apache.dubbo.qos.command.CommandContext; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.nio.charset.StandardCharsets; import static org.hamcrest.Matchers.arrayContaining; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/decoder/TelnetCommandDecoderTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/decoder/TelnetCommandDecoderTest.java index 50a1add30c5..d8847ababdd 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/decoder/TelnetCommandDecoderTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/decoder/TelnetCommandDecoderTest.java @@ -17,12 +17,12 @@ package org.apache.dubbo.qos.command.decoder; import org.apache.dubbo.qos.command.CommandContext; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.hamcrest.Matchers.arrayContaining; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class TelnetCommandDecoderTest { @Test diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/HelpTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/HelpTest.java index 0b973135de3..bcd35e208d6 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/HelpTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/HelpTest.java @@ -17,11 +17,11 @@ package org.apache.dubbo.qos.command.impl; import org.apache.dubbo.qos.command.CommandContext; -import org.junit.jupiter.api.Test; +import org.junit.Test; import org.mockito.Mockito; import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class HelpTest { @Test diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java index c82e2d5d685..8cef26d2af5 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java @@ -25,14 +25,14 @@ import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.model.ConsumerModel; import org.apache.dubbo.rpc.model.ProviderModel; -import org.junit.jupiter.api.Test; +import org.junit.Test; import org.mockito.Mockito; import java.util.Map; import static org.apache.dubbo.registry.support.ProviderConsumerRegTable.getProviderInvoker; import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java index f498d5aeb34..f0f31ac7ac5 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java @@ -24,13 +24,13 @@ import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.model.ProviderModel; -import org.junit.jupiter.api.Test; +import org.junit.Test; import org.mockito.Mockito; import static org.apache.dubbo.registry.support.ProviderConsumerRegTable.getProviderInvoker; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.core.Is.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java index 71132c09551..d82cb496508 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java @@ -24,12 +24,12 @@ import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.model.ProviderModel; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.apache.dubbo.registry.support.ProviderConsumerRegTable.getProviderInvoker; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/QuitTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/QuitTest.java index 012c96b7c06..5d2cd793712 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/QuitTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/QuitTest.java @@ -18,11 +18,11 @@ import org.apache.dubbo.qos.command.CommandContext; import org.apache.dubbo.qos.common.QosConstants; -import org.junit.jupiter.api.Test; +import org.junit.Test; import org.mockito.Mockito; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class QuitTest { @Test diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/util/CommandHelperTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/util/CommandHelperTest.java index c45c0f78185..d35aab14d17 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/util/CommandHelperTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/util/CommandHelperTest.java @@ -23,16 +23,16 @@ import org.apache.dubbo.qos.command.impl.Online; import org.apache.dubbo.qos.command.impl.Quit; import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.List; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.equalTo; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; public class CommandHelperTest { @Test diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/protocol/QosProtocolWrapperTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/protocol/QosProtocolWrapperTest.java index cbffcd5ac24..1ac65d4c24f 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/protocol/QosProtocolWrapperTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/protocol/QosProtocolWrapperTest.java @@ -22,13 +22,13 @@ import org.apache.dubbo.qos.server.Server; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Protocol; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import org.mockito.Mockito; import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -40,7 +40,7 @@ public class QosProtocolWrapperTest { private QosProtocolWrapper wrapper = new QosProtocolWrapper(protocol); private Server server = Server.getInstance(); - @BeforeEach + @Before public void setUp() throws Exception { when(url.getParameter(Constants.QOS_ENABLE, true)).thenReturn(true); when(url.getParameter(Constants.QOS_PORT, 22222)).thenReturn(12345); @@ -49,7 +49,7 @@ public void setUp() throws Exception { when(url.getProtocol()).thenReturn(Constants.REGISTRY_PROTOCOL); } - @AfterEach + @After public void tearDown() throws Exception { if (server.isStarted()) { server.stop(); diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/HttpProcessHandlerTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/HttpProcessHandlerTest.java index 12194adfa2a..e5d86c16ae3 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/HttpProcessHandlerTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/HttpProcessHandlerTest.java @@ -22,12 +22,12 @@ import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpRequest; -import org.junit.jupiter.api.Test; +import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/LocalHostPermitHandlerTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/LocalHostPermitHandlerTest.java index 7c35efe0c38..a0b3f7c28ae 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/LocalHostPermitHandlerTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/LocalHostPermitHandlerTest.java @@ -21,14 +21,14 @@ import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; -import org.junit.jupiter.api.Test; +import org.junit.Test; import org.mockito.ArgumentCaptor; import java.net.InetAddress; import java.net.InetSocketAddress; import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/QosProcessHandlerTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/QosProcessHandlerTest.java index a89aa10e0fb..1115341cf05 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/QosProcessHandlerTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/QosProcessHandlerTest.java @@ -25,7 +25,7 @@ import io.netty.handler.codec.http.HttpServerCodec; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; -import org.junit.jupiter.api.Test; +import org.junit.Test; import org.mockito.Mockito; import java.util.Collections; diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/TelnetProcessHandlerTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/TelnetProcessHandlerTest.java index 9928066bfe5..4f391786445 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/TelnetProcessHandlerTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/TelnetProcessHandlerTest.java @@ -19,13 +19,13 @@ import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; -import org.junit.jupiter.api.Test; +import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/textui/TKvTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/textui/TKvTest.java index e630f71c7b5..e5d629b743a 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/textui/TKvTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/textui/TKvTest.java @@ -16,10 +16,10 @@ */ package org.apache.dubbo.qos.textui; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class TKvTest { @Test @@ -35,9 +35,9 @@ public void test1() { }); String kv = tKv.rendering(); - assertThat(kv, containsString("ABCDEFGHIJ" + System.lineSeparator())); - assertThat(kv, containsString("KLMNOPQRST" + System.lineSeparator())); - assertThat(kv, containsString("UVWXYZ" + System.lineSeparator())); + assertThat(kv, containsString("ABCDEFGHIJ\n")); + assertThat(kv, containsString("KLMNOPQRST\n")); + assertThat(kv, containsString("UVWXYZ\n")); tTable.addRow("OPTIONS", kv); String table = tTable.rendering(); diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/textui/TLadderTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/textui/TLadderTest.java index 4feb69e55fb..dab25e09c0e 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/textui/TLadderTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/textui/TLadderTest.java @@ -16,11 +16,11 @@ */ package org.apache.dubbo.qos.textui; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class TLadderTest { @Test @@ -31,10 +31,10 @@ public void testRendering() throws Exception { ladder.addItem("3"); ladder.addItem("4"); String result = ladder.rendering(); - String expected = "1" + System.lineSeparator() + - " `-2" + System.lineSeparator() + - " `-3" + System.lineSeparator() + - " `-4" + System.lineSeparator(); + String expected = "1\n" + + " `-2\n" + + " `-3\n" + + " `-4\n"; assertThat(result, equalTo(expected)); System.out.println(result); } diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/textui/TTableTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/textui/TTableTest.java index abc70292dab..888b3f56ad9 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/textui/TTableTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/textui/TTableTest.java @@ -16,10 +16,10 @@ */ package org.apache.dubbo.qos.textui; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class TTableTest { @Test @@ -28,11 +28,11 @@ public void test1() throws Exception { table.addRow(1, "one", "uno", "un"); table.addRow(2, "two", "dos", "deux"); String result = table.rendering(); - String expected = "+-+---+---+----+" + System.lineSeparator() + - "|1|one|uno|un |" + System.lineSeparator() + - "+-+---+---+----+" + System.lineSeparator() + - "|2|two|dos|deux|" + System.lineSeparator() + - "+-+---+---+----+" + System.lineSeparator(); + String expected = "+-+---+---+----+\n" + + "|1|one|uno|un |\n" + + "+-+---+---+----+\n" + + "|2|two|dos|deux|\n" + + "+-+---+---+----+\n"; assertThat(result, equalTo(expected)); System.out.println(result); } @@ -46,9 +46,9 @@ public void test2() throws Exception { }); table.addRow(1, "abcde", "ABCDE"); String result = table.rendering(); - String expected = "+-+----------+----------+" + System.lineSeparator() + - "|1| abcde | ABCDE|" + System.lineSeparator() + - "+-+----------+----------+" + System.lineSeparator(); + String expected = "+-+----------+----------+\n" + + "|1| abcde | ABCDE|\n" + + "+-+----------+----------+\n"; assertThat(result, equalTo(expected)); System.out.println(result); } diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/textui/TTreeTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/textui/TTreeTest.java index 0a9be36f6d1..61e84ba685e 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/textui/TTreeTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/textui/TTreeTest.java @@ -16,10 +16,10 @@ */ package org.apache.dubbo.qos.textui; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class TTreeTest { @Test diff --git a/dubbo-plugin/pom.xml b/dubbo-plugin/pom.xml index e5427760240..23ae191dfc1 100644 --- a/dubbo-plugin/pom.xml +++ b/dubbo-plugin/pom.xml @@ -18,7 +18,7 @@ dubbo-parent org.apache.dubbo - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT 4.0.0 diff --git a/dubbo-registry/dubbo-registry-api/pom.xml b/dubbo-registry/dubbo-registry-api/pom.xml index d96df68084f..b8be6f46f76 100644 --- a/dubbo-registry/dubbo-registry-api/pom.xml +++ b/dubbo-registry/dubbo-registry-api/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-registry - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-registry-api jar diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java index 7b3bee07d05..8163191391b 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java @@ -18,13 +18,11 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.Version; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.Assert; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.common.utils.UrlUtils; @@ -54,7 +52,6 @@ import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -67,6 +64,7 @@ import static org.apache.dubbo.common.Constants.PROVIDERS_CATEGORY; import static org.apache.dubbo.common.Constants.ROUTERS_CATEGORY; import static org.apache.dubbo.common.Constants.ROUTE_PROTOCOL; +import static org.apache.dubbo.common.utils.UrlUtils.classifyUrls; /** @@ -92,8 +90,6 @@ public class RegistryDirectory extends AbstractDirectory implements Notify private volatile URL overrideDirectoryUrl; // Initialization at construction time, assertion not null, and always assign non null value - private volatile URL registeredConsumerUrl; - /** * override rules * Priority: override>-D>consumer>provider @@ -126,7 +122,7 @@ public RegistryDirectory(Class serviceType, URL url) { this.queryMap = StringUtils.parseQueryString(url.getParameterAndDecoded(Constants.REFER_KEY)); this.overrideDirectoryUrl = this.directoryUrl = turnRegistryUrlToConsumerUrl(url); String group = directoryUrl.getParameter(Constants.GROUP_KEY, ""); - this.multiGroup = group != null && (Constants.ANY_VALUE.equals(group) || group.contains(",")); + this.multiGroup = group != null && ("*".equals(group) || group.contains(",")); } private URL turnRegistryUrlToConsumerUrl(URL url) { @@ -135,12 +131,10 @@ private URL turnRegistryUrlToConsumerUrl(URL url) { if (StringUtils.isNotEmpty(isDefault)) { queryMap.put(Constants.REGISTRY_KEY + "." + Constants.DEFAULT_KEY, isDefault); } - return URLBuilder.from(url) - .setPath(url.getServiceInterface()) + return url.setPath(url.getServiceInterface()) .clearParameters() .addParameters(queryMap) - .removeParameter(Constants.MONITOR_KEY) - .build(); + .removeParameter(Constants.MONITOR_KEY); } public void setProtocol(Protocol protocol) { @@ -164,15 +158,6 @@ public void destroy() { if (isDestroyed()) { return; } - - // unregister. - try { - if (getRegisteredConsumerUrl() != null && registry != null && registry.isAvailable()) { - registry.unregister(getRegisteredConsumerUrl()); - } - } catch (Throwable t) { - logger.warn("unexpected error when unregister service " + serviceKey + "from registry" + registry.getUrl(), t); - } // unsubscribe. try { if (getConsumerUrl() != null && registry != null && registry.isAvailable()) { @@ -193,29 +178,11 @@ public void destroy() { @Override public synchronized void notify(List urls) { - Map> categoryUrls = urls.stream() - .filter(Objects::nonNull) + List categoryUrls = urls.stream() .filter(this::isValidCategory) .filter(this::isNotCompatibleFor26x) - .collect(Collectors.groupingBy(url -> { - if (UrlUtils.isConfigurator(url)) { - return CONFIGURATORS_CATEGORY; - } else if (UrlUtils.isRoute(url)) { - return ROUTERS_CATEGORY; - } else if (UrlUtils.isProvider(url)) { - return PROVIDERS_CATEGORY; - } - return ""; - })); - - List configuratorURLs = categoryUrls.getOrDefault(CONFIGURATORS_CATEGORY, Collections.emptyList()); - this.configurators = Configurator.toConfigurators(configuratorURLs).orElse(this.configurators); + .collect(Collectors.toList()); - List routerURLs = categoryUrls.getOrDefault(ROUTERS_CATEGORY, Collections.emptyList()); - toRouters(routerURLs).ifPresent(this::addRouters); - - // providers - List providerURLs = categoryUrls.getOrDefault(PROVIDERS_CATEGORY, Collections.emptyList()); /** * 3.x added for extend URL address */ @@ -223,10 +190,20 @@ public synchronized void notify(List urls) { Set surpportedListeners = addressListenerExtensionLoader.getSupportedExtensions(); if (surpportedListeners != null && !surpportedListeners.isEmpty()) { for (String addressListenerName : surpportedListeners) { - providerURLs = addressListenerExtensionLoader.getExtension(addressListenerName).notify(providerURLs); + categoryUrls = addressListenerExtensionLoader.getExtension(addressListenerName).notify(categoryUrls); } } - refreshOverrideAndInvoker(providerURLs); + + /** + * TODO Try to refactor the processing of these three type of urls using Collectors.groupBy()? + */ + this.configurators = Configurator.toConfigurators(classifyUrls(categoryUrls, UrlUtils::isConfigurator)) + .orElse(configurators); + + toRouters(classifyUrls(categoryUrls, UrlUtils::isRoute)).ifPresent(this::addRouters); + + // providers + refreshOverrideAndInvoker(classifyUrls(categoryUrls, UrlUtils::isProvider)); } private void refreshOverrideAndInvoker(List urls) { @@ -251,9 +228,9 @@ private void refreshOverrideAndInvoker(List urls) { private void refreshInvoker(List invokerUrls) { Assert.notNull(invokerUrls, "invokerUrls should not be null"); - if (invokerUrls.size() == 1 - && invokerUrls.get(0) != null - && Constants.EMPTY_PROTOCOL.equals(invokerUrls.get(0).getProtocol())) { + if (invokerUrls.size() == 1 && invokerUrls.get(0) != null && Constants.EMPTY_PROTOCOL.equals(invokerUrls + .get(0) + .getProtocol())) { this.forbidden = true; // Forbid to access this.invokers = Collections.emptyList(); routerChain.setInvokers(this.invokers); @@ -275,15 +252,9 @@ private void refreshInvoker(List invokerUrls) { } Map> newUrlInvokerMap = toInvokers(invokerUrls);// Translate url list to Invoker map - /** - * If the calculation is wrong, it is not processed. - * - * 1. The protocol configured by the client is inconsistent with the protocol of the server. - * eg: consumer protocol = dubbo, provider only has other protocol services(rest). - * 2. The registration center is not robust and pushes illegal specification data. - * - */ - if (CollectionUtils.isEmptyMap(newUrlInvokerMap)) { + // state change + // If the calculation is wrong, it is not processed. + if (newUrlInvokerMap == null || newUrlInvokerMap.size() == 0) { logger.error(new IllegalStateException("urls to invokers error .invokerUrls.size :" + invokerUrls.size() + ", invoker.size :0. urls :" + invokerUrls .toString())); return; @@ -306,7 +277,7 @@ private void refreshInvoker(List invokerUrls) { private List> toMergeInvokerList(List> invokers) { List> mergedInvokers = new ArrayList<>(); - Map>> groupMap = new HashMap<>(); + Map>> groupMap = new HashMap>>(); for (Invoker invoker : invokers) { String group = invoker.getUrl().getParameter(Constants.GROUP_KEY, ""); groupMap.computeIfAbsent(group, k -> new ArrayList<>()); @@ -366,11 +337,11 @@ private Optional> toRouters(List urls) { * @return invokers */ private Map> toInvokers(List urls) { - Map> newUrlInvokerMap = new HashMap<>(); + Map> newUrlInvokerMap = new HashMap>(); if (urls == null || urls.isEmpty()) { return newUrlInvokerMap; } - Set keys = new HashSet<>(); + Set keys = new HashSet(); String queryProtocols = this.queryMap.get(Constants.PROTOCOL_KEY); for (URL providerUrl : urls) { // If protocol is configured at the reference side, only the matching protocol is selected @@ -416,7 +387,7 @@ private Map> toInvokers(List urls) { enabled = url.getParameter(Constants.ENABLED_KEY, true); } if (enabled) { - invoker = new InvokerDelegate<>(protocol.refer(serviceType, url), url, providerUrl); + invoker = new InvokerDelegate(protocol.refer(serviceType, url), url, providerUrl); } } catch (Throwable t) { logger.error("Failed to refer invoker for interface:" + serviceType + ",url:(" + url + ")" + t.getMessage(), t); @@ -449,7 +420,7 @@ private URL mergeUrl(URL providerUrl) { this.overrideDirectoryUrl = this.overrideDirectoryUrl.addParametersIfAbsent(providerUrl.getParameters()); // Merge the provider side parameters if ((providerUrl.getPath() == null || providerUrl.getPath() - .length() == 0) && Constants.DUBBO_PROTOCOL.equals(providerUrl.getProtocol())) { // Compatible version 1.0 + .length() == 0) && "dubbo".equals(providerUrl.getProtocol())) { // Compatible version 1.0 //fix by tony.chenl DUBBO-44 String path = directoryUrl.getParameter(Constants.INTERFACE_KEY); if (path != null) { @@ -483,7 +454,7 @@ private URL overrideWithConfigurator(URL providerUrl) { } private URL overrideWithConfigurators(List configurators, URL url) { - if (CollectionUtils.isNotEmpty(configurators)) { + if (configurators != null && !configurators.isEmpty()) { for (Configurator configurator : configurators) { url = configurator.configure(url); } @@ -497,7 +468,7 @@ private URL overrideWithConfigurators(List configurators, URL url) private void destroyAllInvokers() { Map> localUrlInvokerMap = this.urlInvokerMap; // local reference if (localUrlInvokerMap != null) { - for (Invoker invoker : new ArrayList<>(localUrlInvokerMap.values())) { + for (Invoker invoker : new ArrayList>(localUrlInvokerMap.values())) { try { invoker.destroy(); } catch (Throwable t) { @@ -528,7 +499,7 @@ private void destroyUnusedInvokers(Map> oldUrlInvokerMap, Map for (Map.Entry> entry : oldUrlInvokerMap.entrySet()) { if (!newInvokers.contains(entry.getValue())) { if (deleted == null) { - deleted = new ArrayList<>(); + deleted = new ArrayList(); } deleted.add(entry.getKey()); } @@ -605,14 +576,6 @@ public URL getUrl() { return this.overrideDirectoryUrl; } - public URL getRegisteredConsumerUrl() { - return registeredConsumerUrl; - } - - public void setRegisteredConsumerUrl(URL registeredConsumerUrl) { - this.registeredConsumerUrl = registeredConsumerUrl; - } - @Override public boolean isAvailable() { if (isDestroyed()) { @@ -620,7 +583,7 @@ public boolean isAvailable() { } Map> localUrlInvokerMap = urlInvokerMap; if (localUrlInvokerMap != null && localUrlInvokerMap.size() > 0) { - for (Invoker invoker : new ArrayList<>(localUrlInvokerMap.values())) { + for (Invoker invoker : new ArrayList>(localUrlInvokerMap.values())) { if (invoker.isAvailable()) { return true; } @@ -675,7 +638,7 @@ private void overrideDirectoryUrl() { } private void doOverrideUrl(List configurators) { - if (CollectionUtils.isNotEmpty(configurators)) { + if (configurators != null && !configurators.isEmpty()) { for (Configurator configurator : configurators) { this.overrideDirectoryUrl = configurator.configure(overrideDirectoryUrl); } diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java index a514728b62f..79ccc406a29 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java @@ -18,12 +18,10 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.config.ConfigurationUtils; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.NamedThreadFactory; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.common.utils.UrlUtils; @@ -49,7 +47,6 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ExecutorService; import static java.util.concurrent.Executors.newSingleThreadExecutor; @@ -101,7 +98,7 @@ public class RegistryProtocol implements Protocol { private final ProviderConfigurationListener providerConfigurationListener = new ProviderConfigurationListener(); //To solve the problem of RMI repeated exposure port conflicts, the services that have been exposed are no longer exposed. //providerurl <--> exporter - private final ConcurrentMap> bounds = new ConcurrentHashMap<>(); + private final Map> bounds = new ConcurrentHashMap<>(); private Cluster cluster; private Protocol protocol; private RegistryFactory registryFactory; @@ -121,7 +118,7 @@ public static RegistryProtocol getRegistryProtocol() { //Filter the parameters that do not need to be output in url(Starting with .) private static String[] getFilteredKeys(URL url) { Map params = url.getParameters(); - if (CollectionUtils.isNotEmptyMap(params)) { + if (params != null && !params.isEmpty()) { return params.keySet().stream() .filter(k -> k.startsWith(HIDE_KEY_PREFIX)) .toArray(String[]::new); @@ -214,11 +211,19 @@ private URL overrideUrlWithConfig(URL providerUrl, OverrideListener listener) { @SuppressWarnings("unchecked") private ExporterChangeableWrapper doLocalExport(final Invoker originInvoker, URL providerUrl) { String key = getCacheKey(originInvoker); + ExporterChangeableWrapper exporter = (ExporterChangeableWrapper) bounds.get(key); + if (exporter == null) { + synchronized (bounds) { + exporter = (ExporterChangeableWrapper) bounds.get(key); + if (exporter == null) { - return (ExporterChangeableWrapper) bounds.computeIfAbsent(key, s -> { - Invoker invokerDelegate = new InvokerDelegate<>(originInvoker, providerUrl); - return new ExporterChangeableWrapper<>((Exporter) protocol.export(invokerDelegate), originInvoker); - }); + final Invoker invokerDelegete = new InvokerDelegate(originInvoker, providerUrl); + exporter = new ExporterChangeableWrapper((Exporter) protocol.export(invokerDelegete), originInvoker); + bounds.put(key, exporter); + } + } + } + return exporter; } public void reExport(final Invoker originInvoker, URL newInvokerUrl) { @@ -256,8 +261,8 @@ private ExporterChangeableWrapper doChangeLocalExport(final Invoker origi if (exporter == null) { logger.warn(new IllegalStateException("error state, exporter should not be null")); } else { - final Invoker invokerDelegate = new InvokerDelegate(originInvoker, newInvokerUrl); - exporter.setExporter(protocol.export(invokerDelegate)); + final Invoker invokerDelegete = new InvokerDelegate(originInvoker, newInvokerUrl); + exporter.setExporter(protocol.export(invokerDelegete)); } return exporter; } @@ -296,18 +301,8 @@ private URL getRegisteredProviderUrl(final URL providerUrl, final URL registryUr MONITOR_KEY, BIND_IP_KEY, BIND_PORT_KEY, QOS_ENABLE, QOS_PORT, ACCEPT_FOREIGN_IP, VALIDATION_KEY, INTERFACES); } else { - String extra_keys = registryUrl.getParameter(EXTRA_KEYS_KEY, ""); - // if path is not the same as interface name then we should keep INTERFACE_KEY, - // otherwise, the registry structure of zookeeper would be '/dubbo/path/providers', - // but what we expect is '/dubbo/interface/providers' - if (!providerUrl.getPath().equals(providerUrl.getParameter(Constants.INTERFACE_KEY))) { - if (StringUtils.isNotEmpty(extra_keys)) { - extra_keys += ","; - } - extra_keys += Constants.INTERFACE_KEY; - } - String[] paramsToRegistry = getParamsToRegistry(DEFAULT_REGISTER_PROVIDER_KEYS - , Constants.COMMA_SPLIT_PATTERN.split(extra_keys)); + String[] paramsToRegistry = getParamsToRegistry(DEFAULT_REGISTER_PROVIDER_KEYS, + registryUrl.getParameter(EXTRA_KEYS_KEY, new String[0])); return URL.valueOf(providerUrl, paramsToRegistry, providerUrl.getParameter(METHODS_KEY, (String[]) null)); } @@ -347,10 +342,7 @@ private String getCacheKey(final Invoker originInvoker) { @Override @SuppressWarnings("unchecked") public Invoker refer(Class type, URL url) throws RpcException { - url = URLBuilder.from(url) - .setProtocol(url.getParameter(REGISTRY_KEY, DEFAULT_REGISTRY)) - .removeParameter(REGISTRY_KEY) - .build(); + url = url.setProtocol(url.getParameter(REGISTRY_KEY, DEFAULT_REGISTRY)).removeParameter(REGISTRY_KEY); Registry registry = registryFactory.getRegistry(url); if (RegistryService.class.equals(type)) { return proxyFactory.getInvoker((T) registry, type, url); @@ -379,8 +371,7 @@ private Invoker doRefer(Cluster cluster, Registry registry, Class type Map parameters = new HashMap(directory.getUrl().getParameters()); URL subscribeUrl = new URL(CONSUMER_PROTOCOL, parameters.remove(REGISTER_IP_KEY), 0, type.getName(), parameters); if (!ANY_VALUE.equals(url.getServiceInterface()) && url.getParameter(REGISTER_KEY, true)) { - directory.setRegisteredConsumerUrl(getRegisteredConsumerUrl(subscribeUrl, url)); - registry.register(directory.getRegisteredConsumerUrl()); + registry.register(getRegisteredConsumerUrl(subscribeUrl, url)); } directory.buildRouterChain(subscribeUrl); directory.subscribe(subscribeUrl.addParameter(CATEGORY_KEY, @@ -391,7 +382,7 @@ private Invoker doRefer(Cluster cluster, Registry registry, Class type return invoker; } - public URL getRegisteredConsumerUrl(final URL consumerUrl, URL registryUrl) { + private URL getRegisteredConsumerUrl(final URL consumerUrl, URL registryUrl) { if (!registryUrl.getParameter(SIMPLIFIED_KEY, false)) { return consumerUrl.addParameters(CATEGORY_KEY, CONSUMERS_CATEGORY, CHECK_KEY, String.valueOf(false)); @@ -402,11 +393,11 @@ public URL getRegisteredConsumerUrl(final URL consumerUrl, URL registryUrl) { } // available to test - public String[] getParamsToRegistry(String[] defaultKeys, String[] additionalParameterKeys) { - int additionalLen = additionalParameterKeys.length; + public String[] getParamsToRegistry(String[] defaultKeys, String[] addionalParameterKeys) { + int additionalLen = addionalParameterKeys.length; String[] registryParams = new String[defaultKeys.length + additionalLen]; System.arraycopy(defaultKeys, 0, registryParams, 0, defaultKeys.length); - System.arraycopy(additionalParameterKeys, 0, registryParams, defaultKeys.length, additionalLen); + System.arraycopy(addionalParameterKeys, 0, registryParams, defaultKeys.length, additionalLen); return registryParams; } diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java index 31cdbf8d467..fc2f86d31ea 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java @@ -20,7 +20,6 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.ConcurrentHashSet; import org.apache.dubbo.common.utils.ConfigUtils; import org.apache.dubbo.common.utils.NamedThreadFactory; @@ -37,7 +36,6 @@ import java.nio.channels.FileChannel; import java.nio.channels.FileLock; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -53,6 +51,7 @@ /** * AbstractRegistry. (SPI, Prototype, ThreadSafe) + * */ public abstract class AbstractRegistry implements Registry { @@ -62,16 +61,16 @@ public abstract class AbstractRegistry implements Registry { private static final String URL_SPLIT = "\\s+"; // Log output protected final Logger logger = LoggerFactory.getLogger(getClass()); - // Local disk cache, where the special key value.registries records the list of registry centers, and the others are the list of notified service providers + // Local disk cache, where the special key value.registies records the list of registry centers, and the others are the list of notified service providers private final Properties properties = new Properties(); // File cache timing writing private final ExecutorService registryCacheExecutor = Executors.newFixedThreadPool(1, new NamedThreadFactory("DubboSaveRegistryCache", true)); // Is it synchronized to save the file private final boolean syncSaveFile; private final AtomicLong lastCacheChanged = new AtomicLong(); - private final Set registered = new ConcurrentHashSet<>(); - private final ConcurrentMap> subscribed = new ConcurrentHashMap<>(); - private final ConcurrentMap>> notified = new ConcurrentHashMap<>(); + private final Set registered = new ConcurrentHashSet(); + private final ConcurrentMap> subscribed = new ConcurrentHashMap>(); + private final ConcurrentMap>> notified = new ConcurrentHashMap>>(); private URL registryUrl; // Local disk cache file private File file; @@ -86,20 +85,18 @@ public AbstractRegistry(URL url) { file = new File(filename); if (!file.exists() && file.getParentFile() != null && !file.getParentFile().exists()) { if (!file.getParentFile().mkdirs()) { - throw new IllegalArgumentException("Invalid registry cache file " + file + ", cause: Failed to create directory " + file.getParentFile() + "!"); + throw new IllegalArgumentException("Invalid registry store file " + file + ", cause: Failed to create directory " + file.getParentFile() + "!"); } } } this.file = file; - // When starting the subscription center, - // we need to read the local cache file for future Registry fault tolerance processing. loadProperties(); notify(url.getBackupUrls()); } protected static List filterEmpty(URL url, List urls) { - if (CollectionUtils.isEmpty(urls)) { - List result = new ArrayList<>(1); + if (urls == null || urls.isEmpty()) { + List result = new ArrayList(1); result.add(url.setProtocol(Constants.EMPTY_PROTOCOL)); return result; } @@ -119,15 +116,15 @@ protected void setUrl(URL url) { } public Set getRegistered() { - return Collections.unmodifiableSet(registered); + return registered; } public Map> getSubscribed() { - return Collections.unmodifiableMap(subscribed); + return subscribed; } public Map>> getNotified() { - return Collections.unmodifiableMap(notified); + return notified; } public File getCacheFile() { @@ -155,23 +152,33 @@ public void doSaveProperties(long version) { if (!lockfile.exists()) { lockfile.createNewFile(); } - try (RandomAccessFile raf = new RandomAccessFile(lockfile, "rw"); - FileChannel channel = raf.getChannel()) { - FileLock lock = channel.tryLock(); - if (lock == null) { - throw new IOException("Can not lock the registry cache file " + file.getAbsolutePath() + ", ignore and retry later, maybe multi java process use the file, please config: dubbo.registry.file=xxx.properties"); - } - // Save + RandomAccessFile raf = new RandomAccessFile(lockfile, "rw"); + try { + FileChannel channel = raf.getChannel(); try { - if (!file.exists()) { - file.createNewFile(); + FileLock lock = channel.tryLock(); + if (lock == null) { + throw new IOException("Can not lock the registry cache file " + file.getAbsolutePath() + ", ignore and retry later, maybe multi java process use the file, please config: dubbo.registry.file=xxx.properties"); } - try (FileOutputStream outputFile = new FileOutputStream(file)) { - properties.store(outputFile, "Dubbo Registry Cache"); + // Save + try { + if (!file.exists()) { + file.createNewFile(); + } + FileOutputStream outputFile = new FileOutputStream(file); + try { + properties.store(outputFile, "Dubbo Registry Cache"); + } finally { + outputFile.close(); + } + } finally { + lock.release(); } } finally { - lock.release(); + channel.close(); } + } finally { + raf.close(); } } catch (Throwable e) { if (version < lastCacheChanged.get()) { @@ -179,7 +186,7 @@ public void doSaveProperties(long version) { } else { registryCacheExecutor.execute(new SaveProperties(lastCacheChanged.incrementAndGet())); } - logger.warn("Failed to save registry cache file, cause: " + e.getMessage(), e); + logger.warn("Failed to save registry store file, cause: " + e.getMessage(), e); } } @@ -190,10 +197,10 @@ private void loadProperties() { in = new FileInputStream(file); properties.load(in); if (logger.isInfoEnabled()) { - logger.info("Load registry cache file " + file + ", data: " + properties); + logger.info("Load registry store file " + file + ", data: " + properties); } } catch (Throwable e) { - logger.warn("Failed to load registry cache file " + file, e); + logger.warn("Failed to load registry store file " + file, e); } finally { if (in != null) { try { @@ -214,7 +221,7 @@ public List getCacheUrls(URL url) { && (Character.isLetter(key.charAt(0)) || key.charAt(0) == '_') && value != null && value.length() > 0) { String[] arr = value.trim().split(URL_SPLIT); - List urls = new ArrayList<>(); + List urls = new ArrayList(); for (String u : arr) { urls.add(URL.valueOf(u)); } @@ -226,7 +233,7 @@ public List getCacheUrls(URL url) { @Override public List lookup(URL url) { - List result = new ArrayList<>(); + List result = new ArrayList(); Map> notifiedUrls = getNotified().get(url); if (notifiedUrls != null && notifiedUrls.size() > 0) { for (List urls : notifiedUrls.values()) { @@ -237,11 +244,11 @@ public List lookup(URL url) { } } } else { - final AtomicReference> reference = new AtomicReference<>(); + final AtomicReference> reference = new AtomicReference>(); NotifyListener listener = reference::set; subscribe(url, listener); // Subscribe logic guarantees the first notify to return List urls = reference.get(); - if (CollectionUtils.isNotEmpty(urls)) { + if (urls != null && !urls.isEmpty()) { for (URL u : urls) { if (!Constants.EMPTY_PROTOCOL.equals(u.getProtocol())) { result.add(u); @@ -285,7 +292,11 @@ public void subscribe(URL url, NotifyListener listener) { if (logger.isInfoEnabled()) { logger.info("Subscribe: " + url); } - Set listeners = subscribed.computeIfAbsent(url, n -> new ConcurrentHashSet<>()); + Set listeners = subscribed.get(url); + if (listeners == null) { + subscribed.putIfAbsent(url, new ConcurrentHashSet()); + listeners = subscribed.get(url); + } listeners.add(listener); } @@ -308,7 +319,7 @@ public void unsubscribe(URL url, NotifyListener listener) { protected void recover() throws Exception { // register - Set recoverRegistered = new HashSet<>(getRegistered()); + Set recoverRegistered = new HashSet(getRegistered()); if (!recoverRegistered.isEmpty()) { if (logger.isInfoEnabled()) { logger.info("Recover register url " + recoverRegistered); @@ -318,7 +329,7 @@ protected void recover() throws Exception { } } // subscribe - Map> recoverSubscribed = new HashMap<>(getSubscribed()); + Map> recoverSubscribed = new HashMap>(getSubscribed()); if (!recoverSubscribed.isEmpty()) { if (logger.isInfoEnabled()) { logger.info("Recover subscribe url " + recoverSubscribed.keySet()); @@ -333,7 +344,7 @@ protected void recover() throws Exception { } protected void notify(List urls) { - if (CollectionUtils.isEmpty(urls)) { + if (urls == null || urls.isEmpty()) { return; } @@ -357,13 +368,6 @@ protected void notify(List urls) { } } - /** - * Notify changes from the Provider side. - * - * @param url consumer side url - * @param listener listener - * @param urls provider latest urls - */ protected void notify(URL url, NotifyListener listener, List urls) { if (url == null) { throw new IllegalArgumentException("notify url == null"); @@ -371,7 +375,7 @@ protected void notify(URL url, NotifyListener listener, List urls) { if (listener == null) { throw new IllegalArgumentException("notify listener == null"); } - if ((CollectionUtils.isEmpty(urls)) + if ((urls == null || urls.isEmpty()) && !Constants.ANY_VALUE.equals(url.getServiceInterface())) { logger.warn("Ignore empty notify urls for subscribe url " + url); return; @@ -379,27 +383,32 @@ protected void notify(URL url, NotifyListener listener, List urls) { if (logger.isInfoEnabled()) { logger.info("Notify urls for subscribe url " + url + ", urls: " + urls); } - // keep every provider's category. - Map> result = new HashMap<>(); + Map> result = new HashMap>(); for (URL u : urls) { if (UrlUtils.isMatch(url, u)) { String category = u.getParameter(Constants.CATEGORY_KEY, Constants.DEFAULT_CATEGORY); - List categoryList = result.computeIfAbsent(category, k -> new ArrayList<>()); + List categoryList = result.get(category); + if (categoryList == null) { + categoryList = new ArrayList(); + result.put(category, categoryList); + } categoryList.add(u); } } if (result.size() == 0) { return; } - Map> categoryNotified = notified.computeIfAbsent(url, u -> new ConcurrentHashMap<>()); + Map> categoryNotified = notified.get(url); + if (categoryNotified == null) { + notified.putIfAbsent(url, new ConcurrentHashMap>()); + categoryNotified = notified.get(url); + } for (Map.Entry> entry : result.entrySet()) { String category = entry.getKey(); List categoryList = entry.getValue(); categoryNotified.put(category, categoryList); - listener.notify(categoryList); - // We will update our cache file after each notification. - // When our Registry has a subscribe failure due to network jitter, we can return at least the existing cache URL. saveProperties(url); + listener.notify(categoryList); } } @@ -438,9 +447,9 @@ public void destroy() { if (logger.isInfoEnabled()) { logger.info("Destroy registry:" + getUrl()); } - Set destroyRegistered = new HashSet<>(getRegistered()); + Set destroyRegistered = new HashSet(getRegistered()); if (!destroyRegistered.isEmpty()) { - for (URL url : new HashSet<>(getRegistered())) { + for (URL url : new HashSet(getRegistered())) { if (url.getParameter(Constants.DYNAMIC_KEY, true)) { try { unregister(url); @@ -453,7 +462,7 @@ public void destroy() { } } } - Map> destroySubscribed = new HashMap<>(getSubscribed()); + Map> destroySubscribed = new HashMap>(getSubscribed()); if (!destroySubscribed.isEmpty()) { for (Map.Entry> entry : destroySubscribed.entrySet()) { URL url = entry.getKey(); diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java index 4ef392ef42a..64647f4ba60 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java @@ -18,7 +18,6 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.registry.Registry; @@ -83,11 +82,9 @@ public static void destroyAll() { @Override public Registry getRegistry(URL url) { - url = URLBuilder.from(url) - .setPath(RegistryService.class.getName()) + url = url.setPath(RegistryService.class.getName()) .addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()) - .removeParameters(Constants.EXPORT_KEY, Constants.REFER_KEY) - .build(); + .removeParameters(Constants.EXPORT_KEY, Constants.REFER_KEY); String key = url.toServiceStringWithoutResolving(); // Lock the registry access process to ensure a single instance of the registry LOCK.lock(); @@ -96,7 +93,6 @@ public Registry getRegistry(URL url) { if (registry != null) { return registry; } - //create registry by spi/ioc registry = createRegistry(url); if (registry == null) { throw new IllegalStateException("Can not create registry " + url); diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/FailbackRegistry.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/FailbackRegistry.java index cbf1527231f..c7a4bb9bcba 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/FailbackRegistry.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/FailbackRegistry.java @@ -19,7 +19,6 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.timer.HashedWheelTimer; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.NamedThreadFactory; import org.apache.dubbo.registry.NotifyListener; import org.apache.dubbo.registry.retry.FailedNotifiedTask; @@ -292,7 +291,7 @@ public void subscribe(URL url, NotifyListener listener) { Throwable t = e; List urls = getCacheUrls(url); - if (CollectionUtils.isNotEmpty(urls)) { + if (urls != null && !urls.isEmpty()) { notify(url, listener, urls); logger.error("Failed to subscribe " + url + ", Using cached list: " + urls + " from cache file: " + getUrl().getParameter(Constants.FILE_KEY, System.getProperty("user.home") + "/dubbo-registry-" + url.getHost() + ".cache") + ", cause: " + t.getMessage(), t); } else { diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/ProviderConsumerRegTable.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/ProviderConsumerRegTable.java index 9af39888e21..620c534fb8e 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/ProviderConsumerRegTable.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/ProviderConsumerRegTable.java @@ -72,9 +72,9 @@ public static ProviderInvokerWrapper getProviderWrapper(URL registeredPro return null; } - for (Map.Entry entry : invokers.entrySet()) { - if (entry.getKey() == invoker) { - return entry.getValue(); + for (Invoker inv : invokers.keySet()) { + if (inv == invoker) { + return invokers.get(inv); } } diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/PerformanceRegistryTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/PerformanceRegistryTest.java index a71cd39dc45..67bda752f0c 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/PerformanceRegistryTest.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/PerformanceRegistryTest.java @@ -22,14 +22,14 @@ import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.NetUtils; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import junit.framework.TestCase; +import org.junit.Test; /** * RegistryPerformanceTest * */ -public class PerformanceRegistryTest { +public class PerformanceRegistryTest extends TestCase { private static final Logger logger = LoggerFactory.getLogger(PerformanceRegistryTest.class); diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/AbstractRegistryFactoryTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/AbstractRegistryFactoryTest.java index 1fd1e6d3753..d0dd1cd068d 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/AbstractRegistryFactoryTest.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/AbstractRegistryFactoryTest.java @@ -22,8 +22,8 @@ import org.apache.dubbo.registry.Registry; import org.apache.dubbo.registry.RegistryFactory; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.util.Collection; import java.util.List; @@ -83,7 +83,7 @@ public void testRegistryFactoryCache() throws Exception { URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostAddress() + ":2233"); Registry registry1 = registryFactory.getRegistry(url); Registry registry2 = registryFactory.getRegistry(url); - Assertions.assertEquals(registry1, registry2); + Assert.assertEquals(registry1, registry2); } /** @@ -93,14 +93,14 @@ public void testRegistryFactoryCache() throws Exception { public void testRegistryFactoryIpCache() throws Exception { Registry registry1 = registryFactory.getRegistry(URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":2233")); Registry registry2 = registryFactory.getRegistry(URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostAddress() + ":2233")); - Assertions.assertEquals(registry1, registry2); + Assert.assertEquals(registry1, registry2); } @Test public void testRegistryFactoryGroupCache() throws Exception { Registry registry1 = registryFactory.getRegistry(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":2233?group=aaa")); Registry registry2 = registryFactory.getRegistry(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":2233?group=bbb")); - Assertions.assertNotSame(registry1, registry2); + Assert.assertNotSame(registry1, registry2); } @Test @@ -108,10 +108,10 @@ public void testDestroyAllRegistries() { Registry registry1 = registryFactory.getRegistry(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":8888?group=xxx")); Registry registry2 = registryFactory.getRegistry(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":9999?group=yyy")); Collection registries = AbstractRegistryFactory.getRegistries(); - Assertions.assertTrue(registries.contains(registry1)); - Assertions.assertTrue(registries.contains(registry2)); + Assert.assertTrue(registries.contains(registry1)); + Assert.assertTrue(registries.contains(registry2)); AbstractRegistryFactory.destroyAll(); - Assertions.assertFalse(registries.contains(registry1)); - Assertions.assertFalse(registries.contains(registry2)); + Assert.assertFalse(registries.contains(registry1)); + Assert.assertFalse(registries.contains(registry2)); } } diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/AbstractRegistryTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/AbstractRegistryTest.java index 23364d06e4f..a6b688fad9e 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/AbstractRegistryTest.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/AbstractRegistryTest.java @@ -18,14 +18,13 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.registry.NotifyListener; - -import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -47,11 +46,11 @@ public class AbstractRegistryTest { private boolean notifySuccess; private Map parametersConsumer = new LinkedHashMap<>(); - @BeforeEach + @Before public void init() { URL url = URL.valueOf("dubbo://192.168.0.2:2233"); //sync update cache file - url = url.addParameter("save.file", true); + url = url.addParameter("save.file",true); testUrl = URL.valueOf("http://192.168.0.3:9090/registry?check=false&file=N/A&interface=com.test"); mockUrl = new URL("dubbo", "192.168.0.1", 2200); @@ -79,7 +78,7 @@ public boolean isAvailable() { notifySuccess = false; } - @AfterEach + @After public void after() { abstractRegistry.destroy(); } @@ -96,22 +95,18 @@ public void testRegister() throws Exception { abstractRegistry.register(mockUrl); assert abstractRegistry.getRegistered().contains(mockUrl); //test multiple urls - for (URL url : abstractRegistry.getRegistered()) { - abstractRegistry.unregister(url); - } + abstractRegistry.getRegistered().clear(); List urlList = getList(); for (URL url : urlList) { abstractRegistry.register(url); } - MatcherAssert.assertThat(abstractRegistry.getRegistered().size(), Matchers.equalTo(urlList.size())); + Assert.assertThat(abstractRegistry.getRegistered().size(), Matchers.equalTo(urlList.size())); } - @Test + @Test(expected = IllegalArgumentException.class) public void testRegisterIfURLNULL() throws Exception { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - abstractRegistry.register(null); - Assertions.fail("register url == null"); - }); + abstractRegistry.register(null); + Assert.fail("register url == null"); } /** @@ -124,29 +119,25 @@ public void testRegisterIfURLNULL() throws Exception { public void testUnregister() throws Exception { //test one unregister URL url = new URL("dubbo", "192.168.0.1", 2200); - abstractRegistry.register(url); + abstractRegistry.getRegistered().add(url); abstractRegistry.unregister(url); - MatcherAssert.assertThat(false, Matchers.equalTo(abstractRegistry.getRegistered().contains(url))); + Assert.assertThat(false, Matchers.equalTo(abstractRegistry.getRegistered().contains(url))); //test multiple unregisters - for (URL u : abstractRegistry.getRegistered()) { - abstractRegistry.unregister(u); - } + abstractRegistry.getRegistered().clear(); List urlList = getList(); for (URL urlSub : urlList) { - abstractRegistry.register(urlSub); + abstractRegistry.getRegistered().add(urlSub); } for (URL urlSub : urlList) { abstractRegistry.unregister(urlSub); } - MatcherAssert.assertThat(0, Matchers.equalTo(abstractRegistry.getRegistered().size())); + Assert.assertThat(0, Matchers.equalTo(abstractRegistry.getRegistered().size())); } - @Test + @Test(expected = IllegalArgumentException.class) public void testUnregisterIfUrlNull() throws Exception { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - abstractRegistry.unregister(null); - Assertions.fail("unregister url == null"); - }); + abstractRegistry.unregister(null); + Assert.fail("unregister url == null"); } /** @@ -160,53 +151,45 @@ public void testSubscribeAndUnsubscribe() throws Exception { URL url = new URL("dubbo", "192.168.0.1", 2200); abstractRegistry.subscribe(url, listener); Set subscribeListeners = abstractRegistry.getSubscribed().get(url); - MatcherAssert.assertThat(true, Matchers.equalTo(subscribeListeners.contains(listener))); + Assert.assertThat(true, Matchers.equalTo(subscribeListeners.contains(listener))); //test unsubscribe abstractRegistry.unsubscribe(url, listener); Set unsubscribeListeners = abstractRegistry.getSubscribed().get(url); - MatcherAssert.assertThat(false, Matchers.equalTo(unsubscribeListeners.contains(listener))); + Assert.assertThat(false, Matchers.equalTo(unsubscribeListeners.contains(listener))); } - @Test + @Test(expected = IllegalArgumentException.class) public void testSubscribeIfUrlNull() throws Exception { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - final AtomicReference notified = new AtomicReference(false); - NotifyListener listener = urls -> notified.set(Boolean.TRUE); - URL url = new URL("dubbo", "192.168.0.1", 2200); - abstractRegistry.subscribe(null, listener); - Assertions.fail("subscribe url == null"); - }); + final AtomicReference notified = new AtomicReference(false); + NotifyListener listener = urls -> notified.set(Boolean.TRUE); + URL url = new URL("dubbo", "192.168.0.1", 2200); + abstractRegistry.subscribe(null, listener); + Assert.fail("subscribe url == null"); } - @Test + @Test(expected = IllegalArgumentException.class) public void testSubscribeIfListenerNull() throws Exception { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - final AtomicReference notified = new AtomicReference(false); - NotifyListener listener = urls -> notified.set(Boolean.TRUE); - URL url = new URL("dubbo", "192.168.0.1", 2200); - abstractRegistry.subscribe(url, null); - Assertions.fail("listener url == null"); - }); + final AtomicReference notified = new AtomicReference(false); + NotifyListener listener = urls -> notified.set(Boolean.TRUE); + URL url = new URL("dubbo", "192.168.0.1", 2200); + abstractRegistry.subscribe(url, null); + Assert.fail("listener url == null"); } - @Test + @Test(expected = IllegalArgumentException.class) public void testUnsubscribeIfUrlNull() throws Exception { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - final AtomicReference notified = new AtomicReference(false); - NotifyListener listener = urls -> notified.set(Boolean.TRUE); - abstractRegistry.unsubscribe(null, listener); - Assertions.fail("unsubscribe url == null"); - }); + final AtomicReference notified = new AtomicReference(false); + NotifyListener listener = urls -> notified.set(Boolean.TRUE); + abstractRegistry.unsubscribe(null, listener); + Assert.fail("unsubscribe url == null"); } - @Test + @Test(expected = IllegalArgumentException.class) public void testUnsubscribeIfNotifyNull() throws Exception { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - final AtomicReference notified = new AtomicReference(false); - URL url = new URL("dubbo", "192.168.0.1", 2200); - abstractRegistry.unsubscribe(url, null); - Assertions.fail("unsubscribe listener == null"); - }); + final AtomicReference notified = new AtomicReference(false); + URL url = new URL("dubbo", "192.168.0.1", 2200); + abstractRegistry.unsubscribe(url, null); + Assert.fail("unsubscribe listener == null"); } /** @@ -220,22 +203,22 @@ public void testSubscribe() throws Exception { // check parameters try { abstractRegistry.subscribe(testUrl, null); - Assertions.fail(); + Assert.fail(); } catch (Exception e) { - Assertions.assertTrue(e instanceof IllegalArgumentException); + Assert.assertTrue(e instanceof IllegalArgumentException); } // check parameters try { abstractRegistry.subscribe(null, null); - Assertions.fail(); + Assert.fail(); } catch (Exception e) { - Assertions.assertTrue(e instanceof IllegalArgumentException); + Assert.assertTrue(e instanceof IllegalArgumentException); } // check if subscribe successfully - Assertions.assertNull(abstractRegistry.getSubscribed().get(testUrl)); + Assert.assertNull(abstractRegistry.getSubscribed().get(testUrl)); abstractRegistry.subscribe(testUrl, listener); - Assertions.assertNotNull(abstractRegistry.getSubscribed().get(testUrl)); - Assertions.assertTrue(abstractRegistry.getSubscribed().get(testUrl).contains(listener)); + Assert.assertNotNull(abstractRegistry.getSubscribed().get(testUrl)); + Assert.assertTrue(abstractRegistry.getSubscribed().get(testUrl).contains(listener)); } /** @@ -249,25 +232,25 @@ public void testUnsubscribe() throws Exception { // check parameters try { abstractRegistry.unsubscribe(testUrl, null); - Assertions.fail(); + Assert.fail(); } catch (Exception e) { - Assertions.assertTrue(e instanceof IllegalArgumentException); + Assert.assertTrue(e instanceof IllegalArgumentException); } // check parameters try { abstractRegistry.unsubscribe(null, null); - Assertions.fail(); + Assert.fail(); } catch (Exception e) { - Assertions.assertTrue(e instanceof IllegalArgumentException); + Assert.assertTrue(e instanceof IllegalArgumentException); } - Assertions.assertNull(abstractRegistry.getSubscribed().get(testUrl)); + Assert.assertNull(abstractRegistry.getSubscribed().get(testUrl)); // check if unsubscribe successfully abstractRegistry.subscribe(testUrl, listener); abstractRegistry.unsubscribe(testUrl, listener); // Since we have subscribe testUrl, here should return a empty set instead of null - Assertions.assertNotNull(abstractRegistry.getSubscribed().get(testUrl)); - Assertions.assertFalse(abstractRegistry.getSubscribed().get(testUrl).contains(listener)); + Assert.assertNotNull(abstractRegistry.getSubscribed().get(testUrl)); + Assert.assertFalse(abstractRegistry.getSubscribed().get(testUrl).contains(listener)); } /** @@ -278,17 +261,17 @@ public void testUnsubscribe() throws Exception { public void testRecover() throws Exception { // test recover nothing abstractRegistry.recover(); - Assertions.assertFalse(abstractRegistry.getRegistered().contains(testUrl)); - Assertions.assertNull(abstractRegistry.getSubscribed().get(testUrl)); + Assert.assertFalse(abstractRegistry.getRegistered().contains(testUrl)); + Assert.assertNull(abstractRegistry.getSubscribed().get(testUrl)); // test recover abstractRegistry.register(testUrl); abstractRegistry.subscribe(testUrl, listener); abstractRegistry.recover(); // check if recover successfully - Assertions.assertTrue(abstractRegistry.getRegistered().contains(testUrl)); - Assertions.assertNotNull(abstractRegistry.getSubscribed().get(testUrl)); - Assertions.assertTrue(abstractRegistry.getSubscribed().get(testUrl).contains(listener)); + Assert.assertTrue(abstractRegistry.getRegistered().contains(testUrl)); + Assert.assertNotNull(abstractRegistry.getSubscribed().get(testUrl)); + Assert.assertTrue(abstractRegistry.getSubscribed().get(testUrl).contains(listener)); } @@ -296,13 +279,13 @@ public void testRecover() throws Exception { public void testRecover2() throws Exception { List list = getList(); abstractRegistry.recover(); - Assertions.assertEquals(0, abstractRegistry.getRegistered().size()); + Assert.assertEquals(0, abstractRegistry.getRegistered().size()); for (URL url : list) { abstractRegistry.register(url); } - Assertions.assertEquals(3, abstractRegistry.getRegistered().size()); + Assert.assertEquals(3, abstractRegistry.getRegistered().size()); abstractRegistry.recover(); - Assertions.assertEquals(3, abstractRegistry.getRegistered().size()); + Assert.assertEquals(3, abstractRegistry.getRegistered().size()); } /** @@ -327,9 +310,9 @@ public void testNotify() throws Exception { urls.add(url3); abstractRegistry.notify(url1, listner1, urls); Map>> map = abstractRegistry.getNotified(); - MatcherAssert.assertThat(true, Matchers.equalTo(map.containsKey(url1))); - MatcherAssert.assertThat(false, Matchers.equalTo(map.containsKey(url2))); - MatcherAssert.assertThat(false, Matchers.equalTo(map.containsKey(url3))); + Assert.assertThat(true, Matchers.equalTo(map.containsKey(url1))); + Assert.assertThat(false, Matchers.equalTo(map.containsKey(url2))); + Assert.assertThat(false, Matchers.equalTo(map.containsKey(url3))); } /** @@ -353,53 +336,49 @@ public void testNotifyList() throws Exception { urls.add(url3); abstractRegistry.notify(urls); Map>> map = abstractRegistry.getNotified(); - MatcherAssert.assertThat(true, Matchers.equalTo(map.containsKey(url1))); - MatcherAssert.assertThat(true, Matchers.equalTo(map.containsKey(url2))); - MatcherAssert.assertThat(true, Matchers.equalTo(map.containsKey(url3))); + Assert.assertThat(true, Matchers.equalTo(map.containsKey(url1))); + Assert.assertThat(true, Matchers.equalTo(map.containsKey(url2))); + Assert.assertThat(true, Matchers.equalTo(map.containsKey(url3))); } - @Test + @Test(expected = IllegalArgumentException.class) public void testNotifyIfURLNull() throws Exception { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - final AtomicReference notified = new AtomicReference(false); - NotifyListener listner1 = urls -> notified.set(Boolean.TRUE); - URL url1 = new URL("dubbo", "192.168.0.1", 2200, parametersConsumer); - abstractRegistry.subscribe(url1, listner1); - NotifyListener listner2 = urls -> notified.set(Boolean.TRUE); - URL url2 = new URL("dubbo", "192.168.0.2", 2201, parametersConsumer); - abstractRegistry.subscribe(url2, listner2); - NotifyListener listner3 = urls -> notified.set(Boolean.TRUE); - URL url3 = new URL("dubbo", "192.168.0.3", 2202, parametersConsumer); - abstractRegistry.subscribe(url3, listner3); - List urls = new ArrayList<>(); - urls.add(url1); - urls.add(url2); - urls.add(url3); - abstractRegistry.notify(null, listner1, urls); - Assertions.fail("notify url == null"); - }); + final AtomicReference notified = new AtomicReference(false); + NotifyListener listner1 = urls -> notified.set(Boolean.TRUE); + URL url1 = new URL("dubbo", "192.168.0.1", 2200, parametersConsumer); + abstractRegistry.subscribe(url1, listner1); + NotifyListener listner2 = urls -> notified.set(Boolean.TRUE); + URL url2 = new URL("dubbo", "192.168.0.2", 2201, parametersConsumer); + abstractRegistry.subscribe(url2, listner2); + NotifyListener listner3 = urls -> notified.set(Boolean.TRUE); + URL url3 = new URL("dubbo", "192.168.0.3", 2202, parametersConsumer); + abstractRegistry.subscribe(url3, listner3); + List urls = new ArrayList<>(); + urls.add(url1); + urls.add(url2); + urls.add(url3); + abstractRegistry.notify(null, listner1, urls); + Assert.fail("notify url == null"); } - @Test + @Test(expected = IllegalArgumentException.class) public void testNotifyIfNotifyNull() { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - final AtomicReference notified = new AtomicReference(false); - NotifyListener listner1 = urls -> notified.set(Boolean.TRUE); - URL url1 = new URL("dubbo", "192.168.0.1", 2200, parametersConsumer); - abstractRegistry.subscribe(url1, listner1); - NotifyListener listner2 = urls -> notified.set(Boolean.TRUE); - URL url2 = new URL("dubbo", "192.168.0.2", 2201, parametersConsumer); - abstractRegistry.subscribe(url2, listner2); - NotifyListener listner3 = urls -> notified.set(Boolean.TRUE); - URL url3 = new URL("dubbo", "192.168.0.3", 2202, parametersConsumer); - abstractRegistry.subscribe(url3, listner3); - List urls = new ArrayList<>(); - urls.add(url1); - urls.add(url2); - urls.add(url3); - abstractRegistry.notify(url1, null, urls); - Assertions.fail("notify listener == null"); - }); + final AtomicReference notified = new AtomicReference(false); + NotifyListener listner1 = urls -> notified.set(Boolean.TRUE); + URL url1 = new URL("dubbo", "192.168.0.1", 2200, parametersConsumer); + abstractRegistry.subscribe(url1, listner1); + NotifyListener listner2 = urls -> notified.set(Boolean.TRUE); + URL url2 = new URL("dubbo", "192.168.0.2", 2201, parametersConsumer); + abstractRegistry.subscribe(url2, listner2); + NotifyListener listner3 = urls -> notified.set(Boolean.TRUE); + URL url3 = new URL("dubbo", "192.168.0.3", 2202, parametersConsumer); + abstractRegistry.subscribe(url3, listner3); + List urls = new ArrayList<>(); + urls.add(url1); + urls.add(url2); + urls.add(url3); + abstractRegistry.notify(url1, null, urls); + Assert.fail("notify listener == null"); } @@ -414,35 +393,35 @@ public void testNotifyArgs() throws Exception { // check parameters try { abstractRegistry.notify(null, null, null); - Assertions.fail(); + Assert.fail(); } catch (Exception e) { - Assertions.assertTrue(e instanceof IllegalArgumentException); + Assert.assertTrue(e instanceof IllegalArgumentException); } // check parameters try { abstractRegistry.notify(testUrl, null, null); - Assertions.fail(); + Assert.fail(); } catch (Exception e) { - Assertions.assertTrue(e instanceof IllegalArgumentException); + Assert.assertTrue(e instanceof IllegalArgumentException); } // check parameters try { abstractRegistry.notify(null, listener, null); - Assertions.fail(); + Assert.fail(); } catch (Exception e) { - Assertions.assertTrue(e instanceof IllegalArgumentException); + Assert.assertTrue(e instanceof IllegalArgumentException); } - Assertions.assertFalse(notifySuccess); + Assert.assertFalse(notifySuccess); abstractRegistry.notify(testUrl, listener, null); - Assertions.assertFalse(notifySuccess); + Assert.assertFalse(notifySuccess); List urls = new ArrayList<>(); urls.add(testUrl); // check if notify successfully - Assertions.assertFalse(notifySuccess); + Assert.assertFalse(notifySuccess); abstractRegistry.notify(testUrl, listener, urls); - Assertions.assertTrue(notifySuccess); + Assert.assertTrue(notifySuccess); } @Test @@ -450,30 +429,30 @@ public void filterEmptyTest() throws Exception { // check parameters try { AbstractRegistry.filterEmpty(null, null); - Assertions.fail(); + Assert.fail(); } catch (Exception e) { - Assertions.assertTrue(e instanceof NullPointerException); + Assert.assertTrue(e instanceof NullPointerException); } // check parameters List urls = new ArrayList<>(); try { AbstractRegistry.filterEmpty(null, urls); - Assertions.fail(); + Assert.fail(); } catch (Exception e) { - Assertions.assertTrue(e instanceof NullPointerException); + Assert.assertTrue(e instanceof NullPointerException); } // check if the output is generated by a fixed way urls.add(testUrl.setProtocol(Constants.EMPTY_PROTOCOL)); - Assertions.assertEquals(AbstractRegistry.filterEmpty(testUrl, null), urls); + Assert.assertEquals(AbstractRegistry.filterEmpty(testUrl, null), urls); List testUrls = new ArrayList<>(); - Assertions.assertEquals(AbstractRegistry.filterEmpty(testUrl, testUrls), urls); + Assert.assertEquals(AbstractRegistry.filterEmpty(testUrl, testUrls), urls); // check if the output equals the input urls testUrls.add(testUrl); - Assertions.assertEquals(AbstractRegistry.filterEmpty(testUrl, testUrls), testUrls); + Assert.assertEquals(AbstractRegistry.filterEmpty(testUrl, testUrls), testUrls); } @@ -483,18 +462,18 @@ public void lookupTest() throws Exception { // loop up before registry try { abstractRegistry.lookup(null); - Assertions.fail(); + Assert.fail(); } catch (Exception e) { - Assertions.assertTrue(e instanceof NullPointerException); + Assert.assertTrue(e instanceof NullPointerException); } List urlList1 = abstractRegistry.lookup(testUrl); - Assertions.assertFalse(urlList1.contains(testUrl)); + Assert.assertFalse(urlList1.contains(testUrl)); // loop up after registry List urls = new ArrayList<>(); urls.add(testUrl); abstractRegistry.notify(urls); List urlList2 = abstractRegistry.lookup(testUrl); - Assertions.assertTrue(urlList2.contains(testUrl)); + Assert.assertTrue(urlList2.contains(testUrl)); } @@ -502,12 +481,12 @@ public void lookupTest() throws Exception { public void destroyTest() throws Exception { abstractRegistry.register(testUrl); abstractRegistry.subscribe(testUrl, listener); - Assertions.assertEquals(1, abstractRegistry.getRegistered().size()); - Assertions.assertEquals(1, abstractRegistry.getSubscribed().get(testUrl).size()); + Assert.assertEquals(1, abstractRegistry.getRegistered().size()); + Assert.assertEquals(1, abstractRegistry.getSubscribed().get(testUrl).size()); // delete listener and register abstractRegistry.destroy(); - Assertions.assertEquals(0, abstractRegistry.getRegistered().size()); - Assertions.assertEquals(0, abstractRegistry.getSubscribed().get(testUrl).size()); + Assert.assertEquals(0, abstractRegistry.getRegistered().size()); + Assert.assertEquals(0, abstractRegistry.getSubscribed().get(testUrl).size()); } @Test @@ -517,16 +496,16 @@ public void allTest() throws Exception { urls.add(testUrl); // register, subscribe, notify, unsubscribe, unregister abstractRegistry.register(testUrl); - Assertions.assertTrue(abstractRegistry.getRegistered().contains(testUrl)); + Assert.assertTrue(abstractRegistry.getRegistered().contains(testUrl)); abstractRegistry.subscribe(testUrl, listener); - Assertions.assertTrue(abstractRegistry.getSubscribed().containsKey(testUrl)); - Assertions.assertFalse(notifySuccess); + Assert.assertTrue(abstractRegistry.getSubscribed().containsKey(testUrl)); + Assert.assertFalse(notifySuccess); abstractRegistry.notify(urls); - Assertions.assertTrue(notifySuccess); + Assert.assertTrue(notifySuccess); abstractRegistry.unsubscribe(testUrl, listener); - Assertions.assertFalse(abstractRegistry.getSubscribed().containsKey(listener)); + Assert.assertFalse(abstractRegistry.getSubscribed().containsKey(listener)); abstractRegistry.unregister(testUrl); - Assertions.assertFalse(abstractRegistry.getRegistered().contains(testUrl)); + Assert.assertFalse(abstractRegistry.getRegistered().contains(testUrl)); } private List getList() { @@ -541,17 +520,17 @@ private List getList() { } @Test - public void getCacheUrlsTest() { + public void getCacheUrlsTest(){ List urls = new ArrayList<>(); urls.add(testUrl); // check if notify successfully - Assertions.assertFalse(notifySuccess); + Assert.assertFalse(notifySuccess); abstractRegistry.notify(testUrl, listener, urls); - Assertions.assertTrue(notifySuccess); + Assert.assertTrue(notifySuccess); List cacheUrl = abstractRegistry.getCacheUrls(testUrl); - Assertions.assertTrue(cacheUrl.size() == 1); + Assert.assertTrue(cacheUrl.size() == 1); URL nullUrl = URL.valueOf("http://1.2.3.4:9090/registry?check=false&file=N/A&interface=com.testa"); cacheUrl = abstractRegistry.getCacheUrls(nullUrl); - Assertions.assertTrue(Objects.isNull(cacheUrl)); + Assert.assertTrue(Objects.isNull(cacheUrl)); } } diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/FailbackRegistryTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/FailbackRegistryTest.java index dbd7db30d5d..6db670c66c8 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/FailbackRegistryTest.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/FailbackRegistryTest.java @@ -20,9 +20,9 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.registry.NotifyListener; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import java.util.Arrays; import java.util.List; @@ -30,7 +30,7 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; public class FailbackRegistryTest { static String service; @@ -44,7 +44,7 @@ public class FailbackRegistryTest { /** * @throws java.lang.Exception */ - @BeforeEach + @Before public void setUp() throws Exception { service = "org.apache.dubbo.test.DemoService"; serviceUrl = URL.valueOf("remote://127.0.0.1/demoservice?method=get"); @@ -195,14 +195,14 @@ public void notify(List urls) { MockRegistry mockRegistry = new MockRegistry(registryUrl, countDownLatch); mockRegistry.register(serviceUrl); mockRegistry.subscribe(serviceUrl, listener); - Assertions.assertEquals(1, mockRegistry.getRegistered().size()); - Assertions.assertEquals(1, mockRegistry.getSubscribed().size()); + Assert.assertEquals(1, mockRegistry.getRegistered().size()); + Assert.assertEquals(1, mockRegistry.getSubscribed().size()); mockRegistry.recover(); countDownLatch.await(); - Assertions.assertEquals(0, mockRegistry.getFailedRegistered().size()); + Assert.assertEquals(0, mockRegistry.getFailedRegistered().size()); FailbackRegistry.Holder h = new FailbackRegistry.Holder(registryUrl, listener); - Assertions.assertEquals(null, mockRegistry.getFailedSubscribed().get(h)); - Assertions.assertEquals(countDownLatch.getCount(), 0); + Assert.assertEquals(null, mockRegistry.getFailedSubscribed().get(h)); + Assert.assertEquals(countDownLatch.getCount(), 0); } private static class MockRegistry extends FailbackRegistry { diff --git a/dubbo-registry/dubbo-registry-consul/pom.xml b/dubbo-registry/dubbo-registry-consul/pom.xml deleted file mode 100644 index 38647f259ac..00000000000 --- a/dubbo-registry/dubbo-registry-consul/pom.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - dubbo-registry - org.apache.dubbo - 2.7.1-SNAPSHOT - - 4.0.0 - - dubbo-registry-consul - - - - org.apache.dubbo - dubbo-registry-api - ${project.parent.version} - - - com.ecwid.consul - consul-api - - - - diff --git a/dubbo-registry/dubbo-registry-consul/src/main/java/org/apache/dubbo/registry/consul/ConsulRegistry.java b/dubbo-registry/dubbo-registry-consul/src/main/java/org/apache/dubbo/registry/consul/ConsulRegistry.java deleted file mode 100644 index 72f7ff43b86..00000000000 --- a/dubbo-registry/dubbo-registry-consul/src/main/java/org/apache/dubbo/registry/consul/ConsulRegistry.java +++ /dev/null @@ -1,300 +0,0 @@ -/* - * 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 org.apache.dubbo.registry.consul; - -import org.apache.dubbo.common.Constants; -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.NamedThreadFactory; -import org.apache.dubbo.registry.NotifyListener; -import org.apache.dubbo.registry.support.FailbackRegistry; - -import com.ecwid.consul.v1.ConsulClient; -import com.ecwid.consul.v1.QueryParams; -import com.ecwid.consul.v1.Response; -import com.ecwid.consul.v1.agent.model.NewService; -import com.ecwid.consul.v1.catalog.CatalogServicesRequest; -import com.ecwid.consul.v1.health.HealthServicesRequest; -import com.ecwid.consul.v1.health.model.HealthService; - -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.ExecutorService; -import java.util.stream.Collectors; - -import static java.util.concurrent.Executors.newCachedThreadPool; -import static org.apache.dubbo.common.Constants.ANY_VALUE; - -/** - * registry center implementation for consul - */ -public class ConsulRegistry extends FailbackRegistry { - private static final Logger logger = LoggerFactory.getLogger(ConsulRegistry.class); - - private static final String SERVICE_TAG = "dubbo"; - private static final String URL_META_KEY = "url"; - private static final String WATCH_TIMEOUT = "consul-watch-timeout"; - private static final String CHECK_INTERVAL = "consul-check-interval"; - private static final String CHECK_TIMEOUT = "consul-check-timeout"; - private static final String DEREGISTER_AFTER = "consul-deregister-critical-service-after"; - - private static final int DEFAULT_PORT = 8500; - // default watch timeout in millisecond - private static final int DEFAULT_WATCH_TIMEOUT = 60 * 1000; - // default tcp check interval - private static final String DEFAULT_CHECK_INTERVAL = "10s"; - // default tcp check timeout - private static final String DEFAULT_CHECK_TIMEOUT = "1s"; - // default deregister critical server after - private static final String DEFAULT_DEREGISTER_TIME = "20s"; - - private ConsulClient client; - - private ExecutorService notifierExecutor = newCachedThreadPool( - new NamedThreadFactory("dubbo-consul-notifier", true)); - private ConcurrentMap notifiers = new ConcurrentHashMap<>(); - - public ConsulRegistry(URL url) { - super(url); - String host = url.getHost(); - int port = url.getPort() != 0 ? url.getPort() : DEFAULT_PORT; - client = new ConsulClient(host, port); - } - - @Override - public void register(URL url) { - if (isConsumerSide(url)) { - return; - } - - super.register(url); - } - - @Override - public void doRegister(URL url) { - client.agentServiceRegister(buildService(url)); - } - - @Override - public void unregister(URL url) { - if (isConsumerSide(url)) { - return; - } - - super.unregister(url); - } - - @Override - public void doUnregister(URL url) { - client.agentServiceDeregister(buildId(url)); - } - - @Override - public void subscribe(URL url, NotifyListener listener) { - if (isProviderSide(url)) { - return; - } - - super.subscribe(url, listener); - } - - @Override - public void doSubscribe(URL url, NotifyListener listener) { - Long index; - List urls; - if (ANY_VALUE.equals(url.getServiceInterface())) { - Response>> response = getAllServices(-1, buildWatchTimeout(url)); - index = response.getConsulIndex(); - List services = getHealthServices(response.getValue()); - urls = convert(services); - } else { - String service = url.getServiceKey(); - Response> response = getHealthServices(service, -1, buildWatchTimeout(url)); - index = response.getConsulIndex(); - urls = convert(response.getValue()); - } - - notify(url, listener, urls); - ConsulNotifier notifier = notifiers.computeIfAbsent(url, k -> new ConsulNotifier(url, index)); - notifierExecutor.submit(notifier); - } - - @Override - public void unsubscribe(URL url, NotifyListener listener) { - if (isProviderSide(url)) { - return; - } - - super.unsubscribe(url, listener); - } - - @Override - public void doUnsubscribe(URL url, NotifyListener listener) { - ConsulNotifier notifier = notifiers.remove(url); - notifier.stop(); - } - - @Override - public boolean isAvailable() { - return client.getAgentSelf() != null; - } - - @Override - public void destroy() { - super.destroy(); - notifierExecutor.shutdown(); - } - - private Response> getHealthServices(String service, long index, int watchTimeout) { - HealthServicesRequest request = HealthServicesRequest.newBuilder() - .setTag(SERVICE_TAG) - .setQueryParams(new QueryParams(watchTimeout, index)) - .setPassing(true) - .build(); - return client.getHealthServices(service, request); - } - - private Response>> getAllServices(long index, int watchTimeout) { - CatalogServicesRequest request = CatalogServicesRequest.newBuilder() - .setQueryParams(new QueryParams(watchTimeout, index)) - .build(); - return client.getCatalogServices(request); - } - - private List getHealthServices(Map> services) { - return services.keySet().stream() - .filter(s -> services.get(s).contains(SERVICE_TAG)) - .map(s -> getHealthServices(s, -1, -1).getValue()) - .flatMap(Collection::stream) - .collect(Collectors.toList()); - } - - - private boolean isConsumerSide(URL url) { - return url.getProtocol().equals(Constants.CONSUMER_PROTOCOL); - } - - private boolean isProviderSide(URL url) { - return url.getProtocol().equals(Constants.PROVIDER_PROTOCOL); - } - - private List convert(List services) { - return services.stream() - .map(s -> s.getService().getMeta().get(URL_META_KEY)) - .map(URL::valueOf) - .collect(Collectors.toList()); - } - - private NewService buildService(URL url) { - NewService service = new NewService(); - service.setAddress(url.getHost()); - service.setPort(url.getPort()); - service.setId(buildId(url)); - service.setName(url.getServiceInterface()); - service.setCheck(buildCheck(url)); - service.setTags(buildTags(url)); - service.setMeta(Collections.singletonMap(URL_META_KEY, url.toFullString())); - return service; - } - - private List buildTags(URL url) { - Map params = url.getParameters(); - List tags = params.keySet().stream() - .map(k -> k + "=" + params.get(k)) - .collect(Collectors.toList()); - tags.add(SERVICE_TAG); - return tags; - } - - private String buildId(URL url) { - // let's simply use url's hashcode to generate unique service id for now - return Integer.toHexString(url.hashCode()); - } - - private NewService.Check buildCheck(URL url) { - NewService.Check check = new NewService.Check(); - check.setTcp(url.getAddress()); - check.setInterval(url.getParameter(CHECK_INTERVAL, DEFAULT_CHECK_INTERVAL)); - check.setTimeout(url.getParameter(CHECK_TIMEOUT, DEFAULT_CHECK_TIMEOUT)); - check.setDeregisterCriticalServiceAfter(url.getParameter(DEREGISTER_AFTER, DEFAULT_DEREGISTER_TIME)); - return check; - } - - private int buildWatchTimeout(URL url) { - return url.getParameter(WATCH_TIMEOUT, DEFAULT_WATCH_TIMEOUT) / 1000; - } - - private class ConsulNotifier implements Runnable { - private URL url; - private long consulIndex; - private boolean running; - - ConsulNotifier(URL url, long consulIndex) { - this.url = url; - this.consulIndex = consulIndex; - this.running = true; - } - - @Override - public void run() { - while (this.running) { - if (ANY_VALUE.equals(url.getServiceInterface())) { - processServices(); - } else { - processService(); - } - } - } - - private void processService() { - String service = url.getServiceKey(); - Response> response = getHealthServices(service, consulIndex, buildWatchTimeout(url)); - Long currentIndex = response.getConsulIndex(); - if (currentIndex != null && currentIndex > consulIndex) { - consulIndex = currentIndex; - List services = response.getValue(); - List urls = convert(services); - for (NotifyListener listener : getSubscribed().get(url)) { - doNotify(url, listener, urls); - } - } - } - - private void processServices() { - Response>> response = getAllServices(consulIndex, buildWatchTimeout(url)); - Long currentIndex = response.getConsulIndex(); - if (currentIndex != null && currentIndex > consulIndex) { - consulIndex = currentIndex; - List services = getHealthServices(response.getValue()); - List urls = convert(services); - for (NotifyListener listener : getSubscribed().get(url)) { - doNotify(url, listener, urls); - } - } - } - - void stop() { - this.running = false; - } - } -} diff --git a/dubbo-registry/dubbo-registry-consul/src/main/java/org/apache/dubbo/registry/consul/ConsulRegistryFactory.java b/dubbo-registry/dubbo-registry-consul/src/main/java/org/apache/dubbo/registry/consul/ConsulRegistryFactory.java deleted file mode 100644 index c36f009c0d0..00000000000 --- a/dubbo-registry/dubbo-registry-consul/src/main/java/org/apache/dubbo/registry/consul/ConsulRegistryFactory.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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 org.apache.dubbo.registry.consul; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.registry.Registry; -import org.apache.dubbo.registry.support.AbstractRegistryFactory; - -/** - * registry center factory implementation for consul - */ -public class ConsulRegistryFactory extends AbstractRegistryFactory { - @Override - protected Registry createRegistry(URL url) { - return new ConsulRegistry(url); - } -} diff --git a/dubbo-registry/dubbo-registry-consul/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.RegistryFactory b/dubbo-registry/dubbo-registry-consul/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.RegistryFactory deleted file mode 100644 index 7aea18f4d8f..00000000000 --- a/dubbo-registry/dubbo-registry-consul/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.RegistryFactory +++ /dev/null @@ -1 +0,0 @@ -consul=org.apache.dubbo.registry.consul.ConsulRegistryFactory diff --git a/dubbo-registry/dubbo-registry-default/pom.xml b/dubbo-registry/dubbo-registry-default/pom.xml index 73bda5ceab8..ff9bb630f0e 100644 --- a/dubbo-registry/dubbo-registry-default/pom.xml +++ b/dubbo-registry/dubbo-registry-default/pom.xml @@ -20,7 +20,7 @@ org.apache.dubbo dubbo-registry - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-registry-default jar diff --git a/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistry.java b/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistry.java index c936d2979be..2c6b8c1f1ec 100644 --- a/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistry.java +++ b/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistry.java @@ -38,6 +38,7 @@ /** * DubboRegistry + * */ public class DubboRegistry extends FailbackRegistry { @@ -70,12 +71,15 @@ public DubboRegistry(Invoker registryInvoker, RegistryService r this.registryService = registryService; // Start reconnection timer this.reconnectPeriod = registryInvoker.getUrl().getParameter(Constants.REGISTRY_RECONNECT_PERIOD_KEY, RECONNECT_PERIOD_DEFAULT); - reconnectFuture = reconnectTimer.scheduleWithFixedDelay(() -> { - // Check and connect to the registry - try { - connect(); - } catch (Throwable t) { // Defensive fault tolerance - logger.error("Unexpected error occur at reconnect, cause: " + t.getMessage(), t); + reconnectFuture = reconnectTimer.scheduleWithFixedDelay(new Runnable() { + @Override + public void run() { + // Check and connect to the registry + try { + connect(); + } catch (Throwable t) { // Defensive fault tolerance + logger.error("Unexpected error occur at reconnect, cause: " + t.getMessage(), t); + } } }, reconnectPeriod, reconnectPeriod, TimeUnit.MILLISECONDS); } @@ -123,7 +127,9 @@ public void destroy() { super.destroy(); try { // Cancel the reconnection timer - ExecutorUtil.cancelScheduledFuture(reconnectFuture); + if (!reconnectFuture.isCancelled()) { + reconnectFuture.cancel(true); + } } catch (Throwable t) { logger.warn("Failed to cancel reconnect timer", t); } diff --git a/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistryFactory.java b/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistryFactory.java index ee331c83186..ea9fc802ce1 100644 --- a/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistryFactory.java +++ b/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistryFactory.java @@ -18,7 +18,6 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.bytecode.Wrapper; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.common.utils.StringUtils; @@ -48,8 +47,7 @@ public class DubboRegistryFactory extends AbstractRegistryFactory { private Cluster cluster; private static URL getRegistryURL(URL url) { - return URLBuilder.from(url) - .setPath(RegistryService.class.getName()) + return url.setPath(RegistryService.class.getName()) .removeParameter(Constants.EXPORT_KEY).removeParameter(Constants.REFER_KEY) .addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()) .addParameter(Constants.CLUSTER_STICKY_KEY, "true") @@ -58,13 +56,12 @@ private static URL getRegistryURL(URL url) { .addParameterIfAbsent(Constants.TIMEOUT_KEY, "10000") .addParameterIfAbsent(Constants.CALLBACK_INSTANCES_LIMIT_KEY, "10000") .addParameterIfAbsent(Constants.CONNECT_TIMEOUT_KEY, "10000") - .addParameter(Constants.METHODS_KEY, StringUtils.join(new HashSet<>(Arrays.asList(Wrapper.getWrapper(RegistryService.class).getDeclaredMethodNames())), ",")) + .addParameter(Constants.METHODS_KEY, StringUtils.join(new HashSet(Arrays.asList(Wrapper.getWrapper(RegistryService.class).getDeclaredMethodNames())), ",")) //.addParameter(Constants.STUB_KEY, RegistryServiceStub.class.getName()) //.addParameter(Constants.STUB_EVENT_KEY, Boolean.TRUE.toString()) //for event dispatch //.addParameter(Constants.ON_DISCONNECT_KEY, "disconnect") .addParameter("subscribe.1.callback", "true") - .addParameter("unsubscribe.1.callback", "false") - .build(); + .addParameter("unsubscribe.1.callback", "false"); } public void setProtocol(Protocol protocol) { @@ -82,7 +79,7 @@ public void setCluster(Cluster cluster) { @Override public Registry createRegistry(URL url) { url = getRegistryURL(url); - List urls = new ArrayList<>(); + List urls = new ArrayList(); urls.add(url.removeParameter(Constants.BACKUP_KEY)); String backup = url.getParameter(Constants.BACKUP_KEY); if (backup != null && backup.length() > 0) { @@ -91,7 +88,7 @@ public Registry createRegistry(URL url) { urls.add(url.setAddress(address)); } } - RegistryDirectory directory = new RegistryDirectory<>(RegistryService.class, url.addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()).addParameterAndEncoded(Constants.REFER_KEY, url.toParameterString())); + RegistryDirectory directory = new RegistryDirectory(RegistryService.class, url.addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()).addParameterAndEncoded(Constants.REFER_KEY, url.toParameterString())); Invoker registryInvoker = cluster.join(directory); RegistryService registryService = proxyFactory.getProxy(registryInvoker); DubboRegistry registry = new DubboRegistry(registryInvoker, registryService); diff --git a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/DubboRegistryTest.java b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/DubboRegistryTest.java index 3a317785a09..7129d128b54 100644 --- a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/DubboRegistryTest.java +++ b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/DubboRegistryTest.java @@ -28,10 +28,10 @@ import org.apache.dubbo.rpc.RpcInvocation; import org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Before; +import org.junit.Test; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.mock; @@ -52,7 +52,7 @@ public class DubboRegistryTest { private RegistryService registryService; - @BeforeEach + @Before public void setUp() { registryURL = new URL(Constants.REGISTRY_PROTOCOL, NetUtils.getLocalHost(), NetUtils.getAvailablePort()) .addParameter(Constants.CHECK_KEY, false) diff --git a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryDirectoryTest.java b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryDirectoryTest.java index 7d672097596..1126fd1c433 100644 --- a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryDirectoryTest.java +++ b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryDirectoryTest.java @@ -35,21 +35,20 @@ import org.apache.dubbo.rpc.cluster.router.script.ScriptRouterFactory; import org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; import org.mockito.Mockito; import javax.script.ScriptEngineManager; import java.lang.reflect.Field; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.CountDownLatch; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.fail; @SuppressWarnings({"rawtypes", "unchecked"}) public class RegistryDirectoryTest { @@ -67,7 +66,7 @@ public class RegistryDirectoryTest { private Registry registry = Mockito.mock(Registry.class); - @BeforeEach + @Before public void setUp() { } @@ -80,8 +79,8 @@ private RegistryDirectory getRegistryDirectory(URL url) { registryDirectory.subscribe(url); // asert empty List invokers = registryDirectory.list(invocation); - Assertions.assertEquals(0, invokers.size()); - Assertions.assertEquals(false, registryDirectory.isAvailable()); + Assert.assertEquals(0, invokers.size()); + Assert.assertEquals(false, registryDirectory.isAvailable()); return registryDirectory; } @@ -121,8 +120,8 @@ public void test_Constructor_CheckStatus() throws Exception { Field field = reg.getClass().getDeclaredField("queryMap"); field.setAccessible(true); Map queryMap = (Map) field.get(reg); - Assertions.assertEquals("bar", queryMap.get("foo")); - Assertions.assertEquals(url.clearParameters().addParameter("foo", "bar"), reg.getUrl()); + Assert.assertEquals("bar", queryMap.get("foo")); + Assert.assertEquals(url.clearParameters().addParameter("foo", "bar"), reg.getUrl()); } @Test @@ -143,8 +142,8 @@ public void testNotified_Normal_withRouters() { RegistryDirectory registryDirectory = getRegistryDirectory(); test_Notified1invokers(registryDirectory); test_Notified_only_routers(registryDirectory); - Assertions.assertEquals(true, registryDirectory.isAvailable()); - Assertions.assertTrue(LogUtil.checkNoError(), "notify no invoker urls ,should not error"); + Assert.assertEquals(true, registryDirectory.isAvailable()); + Assert.assertTrue("notify no invoker urls ,should not error", LogUtil.checkNoError()); LogUtil.stop(); test_Notified2invokers(registryDirectory); @@ -160,9 +159,9 @@ public void testNotified_WithError() { serviceUrls.add(SERVICEURL); registryDirectory.notify(serviceUrls); - Assertions.assertEquals(true, registryDirectory.isAvailable()); + Assert.assertEquals(true, registryDirectory.isAvailable()); List invokers = registryDirectory.list(invocation); - Assertions.assertEquals(1, invokers.size()); + Assert.assertEquals(1, invokers.size()); } @Test @@ -175,7 +174,7 @@ public void testNotified_WithDuplicateUrls() { RegistryDirectory registryDirectory = getRegistryDirectory(); registryDirectory.notify(serviceUrls); List invokers = registryDirectory.list(invocation); - Assertions.assertEquals(1, invokers.size()); + Assert.assertEquals(1, invokers.size()); } // forbid @@ -184,13 +183,13 @@ private void testforbid(RegistryDirectory registryDirectory) { List serviceUrls = new ArrayList(); serviceUrls.add(new URL(Constants.EMPTY_PROTOCOL, Constants.ANYHOST_VALUE, 0, service, Constants.CATEGORY_KEY, Constants.PROVIDERS_CATEGORY)); registryDirectory.notify(serviceUrls); - Assertions.assertEquals(false, - registryDirectory.isAvailable(), "invokers size=0 ,then the registry directory is not available"); + Assert.assertEquals("invokers size=0 ,then the registry directory is not available", false, + registryDirectory.isAvailable()); try { registryDirectory.list(invocation); fail("forbid must throw RpcException"); } catch (RpcException e) { - Assertions.assertEquals(RpcException.FORBIDDEN_EXCEPTION, e.getCode()); + Assert.assertEquals(RpcException.FORBIDDEN_EXCEPTION, e.getCode()); } } @@ -203,17 +202,17 @@ public void test_NotifiedDubbo1() { URL Dubbo1URL = URL.valueOf("dubbo://127.0.0.1:9098?lazy=true"); serviceUrls.add(Dubbo1URL.addParameter("methods", "getXXX")); registryDirectory.notify(serviceUrls); - Assertions.assertEquals(true, registryDirectory.isAvailable()); + Assert.assertEquals(true, registryDirectory.isAvailable()); invocation = new RpcInvocation(); List> invokers = registryDirectory.list(invocation); - Assertions.assertEquals(1, invokers.size()); + Assert.assertEquals(1, invokers.size()); invocation.setMethodName("getXXX"); invokers = registryDirectory.list(invocation); - Assertions.assertEquals(1, invokers.size()); - Assertions.assertEquals(DemoService.class.getName(), invokers.get(0).getUrl().getPath()); + Assert.assertEquals(1, invokers.size()); + Assert.assertEquals(DemoService.class.getName(), invokers.get(0).getUrl().getPath()); } // notify one invoker @@ -229,24 +228,24 @@ private void test_Notified1invokers(RegistryDirectory registryDirectory) { List serviceUrls = new ArrayList(); serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1").addParameter(Constants.APPLICATION_KEY, "mockApplicationName"));// .addParameter("refer.autodestroy", "true") registryDirectory.notify(serviceUrls); - Assertions.assertEquals(true, registryDirectory.isAvailable()); + Assert.assertEquals(true, registryDirectory.isAvailable()); invocation = new RpcInvocation(); List invokers = registryDirectory.list(invocation); - Assertions.assertEquals(1, invokers.size()); + Assert.assertEquals(1, invokers.size()); invocation.setMethodName("getXXX"); invokers = registryDirectory.list(invocation); - Assertions.assertEquals(1, invokers.size()); + Assert.assertEquals(1, invokers.size()); invocation.setMethodName("getXXX1"); invokers = registryDirectory.list(invocation); - Assertions.assertEquals(1, invokers.size()); + Assert.assertEquals(1, invokers.size()); invocation.setMethodName("getXXX2"); invokers = registryDirectory.list(invocation); - Assertions.assertEquals(1, invokers.size()); + Assert.assertEquals(1, invokers.size()); } // 2 invokers=================================== @@ -257,20 +256,20 @@ private void test_Notified2invokers(RegistryDirectory registryDirectory) { serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2")); registryDirectory.notify(serviceUrls); - Assertions.assertEquals(true, registryDirectory.isAvailable()); + Assert.assertEquals(true, registryDirectory.isAvailable()); invocation = new RpcInvocation(); List invokers = registryDirectory.list(invocation); - Assertions.assertEquals(2, invokers.size()); + Assert.assertEquals(2, invokers.size()); invocation.setMethodName("getXXX"); invokers = registryDirectory.list(invocation); - Assertions.assertEquals(2, invokers.size()); + Assert.assertEquals(2, invokers.size()); invocation.setMethodName("getXXX1"); invokers = registryDirectory.list(invocation); - Assertions.assertEquals(2, invokers.size()); + Assert.assertEquals(2, invokers.size()); } // 3 invoker notifications=================================== @@ -281,28 +280,28 @@ private void test_Notified3invokers(RegistryDirectory registryDirectory) { serviceUrls.add(SERVICEURL3.addParameter("methods", "getXXX1,getXXX2,getXXX3")); registryDirectory.notify(serviceUrls); - Assertions.assertEquals(true, registryDirectory.isAvailable()); + Assert.assertEquals(true, registryDirectory.isAvailable()); invocation = new RpcInvocation(); List invokers = registryDirectory.list(invocation); - Assertions.assertEquals(3, invokers.size()); + Assert.assertEquals(3, invokers.size()); invocation.setMethodName("getXXX"); invokers = registryDirectory.list(invocation); - Assertions.assertEquals(3, invokers.size()); + Assert.assertEquals(3, invokers.size()); invocation.setMethodName("getXXX1"); invokers = registryDirectory.list(invocation); - Assertions.assertEquals(3, invokers.size()); + Assert.assertEquals(3, invokers.size()); invocation.setMethodName("getXXX2"); invokers = registryDirectory.list(invocation); - Assertions.assertEquals(3, invokers.size()); + Assert.assertEquals(3, invokers.size()); invocation.setMethodName("getXXX3"); invokers = registryDirectory.list(invocation); - Assertions.assertEquals(3, invokers.size()); + Assert.assertEquals(3, invokers.size()); } @Test @@ -330,7 +329,7 @@ public void testParametersMerge() { Invoker invoker = (Invoker) invokers.get(0); URL url = invoker.getUrl(); - Assertions.assertEquals(null, url.getParameter("key")); + Assert.assertEquals(null, url.getParameter("key")); } // The parameters of the provider for the inspection service need merge { @@ -343,7 +342,7 @@ public void testParametersMerge() { Invoker invoker = (Invoker) invokers.get(0); URL url = invoker.getUrl(); - Assertions.assertEquals("provider", url.getParameter("key")); + Assert.assertEquals("provider", url.getParameter("key")); } // The parameters of the test service query need to be with the providermerge. { @@ -358,7 +357,7 @@ public void testParametersMerge() { Invoker invoker = (Invoker) invokers.get(0); URL url = invoker.getUrl(); - Assertions.assertEquals("query", url.getParameter("key")); + Assert.assertEquals("query", url.getParameter("key")); } { @@ -371,7 +370,7 @@ public void testParametersMerge() { Invoker invoker = (Invoker) invokers.get(0); URL url = invoker.getUrl(); - Assertions.assertEquals(false, url.getParameter(Constants.CHECK_KEY, false)); + Assert.assertEquals(false, url.getParameter(Constants.CHECK_KEY, false)); } { serviceUrls.clear(); @@ -384,16 +383,16 @@ public void testParametersMerge() { Invoker invoker = (Invoker) invokers.get(0); URL url = invoker.getUrl(); - Assertions.assertEquals(LeastActiveLoadBalance.NAME, url.getMethodParameter("get", Constants.LOADBALANCE_KEY)); + Assert.assertEquals(LeastActiveLoadBalance.NAME, url.getMethodParameter("get", Constants.LOADBALANCE_KEY)); } //test geturl { - Assertions.assertEquals(null, registryDirectory2.getUrl().getParameter("mock")); + Assert.assertEquals(null, registryDirectory2.getUrl().getParameter("mock")); serviceUrls.clear(); serviceUrls.add(SERVICEURL.addParameter(Constants.MOCK_KEY, "true")); registryDirectory2.notify(serviceUrls); - Assertions.assertEquals("true", registryDirectory2.getUrl().getParameter("mock")); + Assert.assertEquals("true", registryDirectory2.getUrl().getParameter("mock")); } } @@ -411,19 +410,19 @@ public void testDestroy() { registryDirectory.notify(serviceUrls); List invokers = registryDirectory.list(invocation); - Assertions.assertEquals(true, registryDirectory.isAvailable()); - Assertions.assertEquals(true, invokers.get(0).isAvailable()); + Assert.assertEquals(true, registryDirectory.isAvailable()); + Assert.assertEquals(true, invokers.get(0).isAvailable()); registryDirectory.destroy(); - Assertions.assertEquals(false, registryDirectory.isAvailable()); - Assertions.assertEquals(false, invokers.get(0).isAvailable()); + Assert.assertEquals(false, registryDirectory.isAvailable()); + Assert.assertEquals(false, invokers.get(0).isAvailable()); registryDirectory.destroy(); List> cachedInvokers = registryDirectory.getInvokers(); Map> urlInvokerMap = registryDirectory.getUrlInvokerMap(); - Assertions.assertTrue(cachedInvokers == null); - Assertions.assertEquals(0, urlInvokerMap.size()); + Assert.assertTrue(cachedInvokers == null); + Assert.assertEquals(0, urlInvokerMap.size()); // List urls = mockRegistry.getSubscribedUrls(); RpcInvocation inv = new RpcInvocation(); @@ -431,7 +430,7 @@ public void testDestroy() { registryDirectory.list(inv); fail(); } catch (RpcException e) { - Assertions.assertTrue(e.getMessage().contains("already destroyed")); + Assert.assertTrue(e.getMessage().contains("already destroyed")); } } @@ -442,7 +441,7 @@ public void testDestroy_WithDestroyRegistry() { registryDirectory.setRegistry(new MockRegistry(latch)); registryDirectory.subscribe(URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/DemoService?category=providers")); registryDirectory.destroy(); - Assertions.assertEquals(0, latch.getCount()); + Assert.assertEquals(0, latch.getCount()); } @Test @@ -469,8 +468,8 @@ public void testDubbo1UrlWithGenericInvocation() { List invokers = registryDirectory.list(invocation); - Assertions.assertEquals(1, invokers.size()); -// Assertions.assertEquals( + Assert.assertEquals(1, invokers.size()); +// Assert.assertEquals( // serviceURL.setPath(service).addParameters("check", "false", "interface", DemoService.class.getName(), REMOTE_APPLICATION_KEY, serviceURL.getParameter(APPLICATION_KEY)) // , invokers.get(0).getUrl() // ); @@ -482,7 +481,7 @@ public void testDubbo1UrlWithGenericInvocation() { /** * When the first arg of a method is String or Enum, Registry server can do parameter-value-based routing. */ - @Disabled("Parameter routing is not available at present.") + @Ignore("Parameter routing is not available at present.") @Test public void testParmeterRoute() { RegistryDirectory registryDirectory = getRegistryDirectory(); @@ -499,7 +498,7 @@ public void testParmeterRoute() { new Object[]{"getXXX1", new String[]{"Enum"}, new Object[]{Param.MORGAN}}); List invokers = registryDirectory.list(invocation); - Assertions.assertEquals(1, invokers.size()); + Assert.assertEquals(1, invokers.size()); } /** @@ -517,8 +516,8 @@ public void testEmptyNotifyCauseForbidden() { try { invokers = registryDirectory.list(inv); } catch (RpcException e) { - Assertions.assertEquals(RpcException.FORBIDDEN_EXCEPTION, e.getCode()); - Assertions.assertEquals(false, registryDirectory.isAvailable()); + Assert.assertEquals(RpcException.FORBIDDEN_EXCEPTION, e.getCode()); + Assert.assertEquals(false, registryDirectory.isAvailable()); } serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1")); @@ -528,8 +527,8 @@ public void testEmptyNotifyCauseForbidden() { registryDirectory.notify(serviceUrls); inv.setMethodName("getXXX2"); invokers = registryDirectory.list(inv); - Assertions.assertEquals(true, registryDirectory.isAvailable()); - Assertions.assertEquals(3, invokers.size()); + Assert.assertEquals(true, registryDirectory.isAvailable()); + Assert.assertEquals(3, invokers.size()); } /** @@ -557,19 +556,19 @@ public void testNotifyRouterUrls() { /*registryDirectory.notify(serviceUrls); RouterChain routerChain = registryDirectory.getRouterChain(); //default invocation selector - Assertions.assertEquals(1 + 1, routers.size()); - Assertions.assertTrue(ScriptRouter.class == routers.get(1).getClass() || ScriptRouter.class == routers.get(0).getClass()); + Assert.assertEquals(1 + 1, routers.size()); + Assert.assertTrue(ScriptRouter.class == routers.get(1).getClass() || ScriptRouter.class == routers.get(0).getClass()); registryDirectory.notify(new ArrayList()); routers = registryDirectory.getRouters(); - Assertions.assertEquals(1 + 1, routers.size()); - Assertions.assertTrue(ScriptRouter.class == routers.get(1).getClass() || ScriptRouter.class == routers.get(0).getClass()); + Assert.assertEquals(1 + 1, routers.size()); + Assert.assertTrue(ScriptRouter.class == routers.get(1).getClass() || ScriptRouter.class == routers.get(0).getClass()); serviceUrls.clear(); serviceUrls.add(routerurl.addParameter(Constants.ROUTER_KEY, Constants.ROUTER_TYPE_CLEAR)); registryDirectory.notify(serviceUrls); routers = registryDirectory.getRouters(); - Assertions.assertEquals(0 + 1, routers.size());*/ + Assert.assertEquals(0 + 1, routers.size());*/ } /** @@ -583,7 +582,7 @@ public void testNotifyoverrideUrls_beforeInvoker() { overrideUrls.add(URL.valueOf("override://0.0.0.0?timeout=1&connections=5")); registryDirectory.notify(overrideUrls); //The registry is initially pushed to override only, and the dirctory state should be false because there is no invoker. - Assertions.assertEquals(false, registryDirectory.isAvailable()); + Assert.assertEquals(false, registryDirectory.isAvailable()); //After pushing two provider, the directory state is restored to true List serviceUrls = new ArrayList(); @@ -591,17 +590,17 @@ public void testNotifyoverrideUrls_beforeInvoker() { serviceUrls.add(SERVICEURL2.addParameter("timeout", "1000").addParameter("connections", "10")); registryDirectory.notify(serviceUrls); - Assertions.assertEquals(true, registryDirectory.isAvailable()); + Assert.assertEquals(true, registryDirectory.isAvailable()); //Start validation of parameter values invocation = new RpcInvocation(); List> invokers = registryDirectory.list(invocation); - Assertions.assertEquals(2, invokers.size()); + Assert.assertEquals(2, invokers.size()); - Assertions.assertEquals("1", invokers.get(0).getUrl().getParameter("timeout"), "override rute must be first priority"); - Assertions.assertEquals("5", invokers.get(0).getUrl().getParameter("connections"), "override rute must be first priority"); + Assert.assertEquals("override rute must be first priority", "1", invokers.get(0).getUrl().getParameter("timeout")); + Assert.assertEquals("override rute must be first priority", "5", invokers.get(0).getUrl().getParameter("connections")); } /** @@ -618,7 +617,7 @@ public void testNotifyoverrideUrls_afterInvoker() { serviceUrls.add(SERVICEURL2.addParameter("timeout", "1000").addParameter("connections", "10")); registryDirectory.notify(serviceUrls); - Assertions.assertEquals(true, registryDirectory.isAvailable()); + Assert.assertEquals(true, registryDirectory.isAvailable()); List overrideUrls = new ArrayList(); overrideUrls.add(URL.valueOf("override://0.0.0.0?timeout=1&connections=5")); @@ -629,10 +628,10 @@ public void testNotifyoverrideUrls_afterInvoker() { invocation = new RpcInvocation(); List> invokers = registryDirectory.list(invocation); - Assertions.assertEquals(2, invokers.size()); + Assert.assertEquals(2, invokers.size()); - Assertions.assertEquals("1", invokers.get(0).getUrl().getParameter("timeout"), "override rute must be first priority"); - Assertions.assertEquals("5", invokers.get(0).getUrl().getParameter("connections"), "override rute must be first priority"); + Assert.assertEquals("override rute must be first priority", "1", invokers.get(0).getUrl().getParameter("timeout")); + Assert.assertEquals("override rute must be first priority", "5", invokers.get(0).getUrl().getParameter("connections")); } /** @@ -649,17 +648,17 @@ public void testNotifyoverrideUrls_withInvoker() { durls.add(URL.valueOf("override://0.0.0.0?timeout=1&connections=5")); registryDirectory.notify(durls); - Assertions.assertEquals(true, registryDirectory.isAvailable()); + Assert.assertEquals(true, registryDirectory.isAvailable()); //Start validation of parameter values invocation = new RpcInvocation(); List> invokers = registryDirectory.list(invocation); - Assertions.assertEquals(2, invokers.size()); + Assert.assertEquals(2, invokers.size()); - Assertions.assertEquals("1", invokers.get(0).getUrl().getParameter("timeout"), "override rute must be first priority"); - Assertions.assertEquals("5", invokers.get(0).getUrl().getParameter("connections"), "override rute must be first priority"); + Assert.assertEquals("override rute must be first priority", "1", invokers.get(0).getUrl().getParameter("timeout")); + Assert.assertEquals("override rute must be first priority", "5", invokers.get(0).getUrl().getParameter("connections")); } /** @@ -677,28 +676,25 @@ public void testNotifyoverrideUrls_Nouse() { durls.add(SERVICEURL2.addParameter("timeout", "1").addParameter("connections", "5")); registryDirectory.notify(durls); List> invokers = registryDirectory.list(invocation); - Assertions.assertEquals(2, invokers.size()); - Map> map = new HashMap<>(); - map.put(invokers.get(0).getUrl().getAddress(), invokers.get(0)); - map.put(invokers.get(1).getUrl().getAddress(), invokers.get(1)); + Assert.assertEquals(2, invokers.size()); + Invoker a1Invoker = invokers.get(0); + Invoker b1Invoker = invokers.get(1); durls = new ArrayList(); durls.add(URL.valueOf("override://0.0.0.0?timeout=1&connections=5")); registryDirectory.notify(durls); - Assertions.assertEquals(true, registryDirectory.isAvailable()); + Assert.assertEquals(true, registryDirectory.isAvailable()); invokers = registryDirectory.list(invocation); - Assertions.assertEquals(2, invokers.size()); - - Map> map2 = new HashMap<>(); - map2.put(invokers.get(0).getUrl().getAddress(), invokers.get(0)); - map2.put(invokers.get(1).getUrl().getAddress(), invokers.get(1)); + Assert.assertEquals(2, invokers.size()); + Invoker a2Invoker = invokers.get(0); + Invoker b2Invoker = invokers.get(1); //The parameters are different and must be rereferenced. - Assertions.assertFalse(map.get(SERVICEURL.getAddress()) == map2.get(SERVICEURL.getAddress()), "object should not same"); + Assert.assertTrue("object should not same", a1Invoker == a2Invoker); //The parameters can not be rereferenced - Assertions.assertTrue(map.get(SERVICEURL2.getAddress()) == map2.get(SERVICEURL2.getAddress()), "object should not same"); + Assert.assertFalse("object should same", b1Invoker == b2Invoker); } /** @@ -722,8 +718,8 @@ public void testNofityOverrideUrls_Provider() { List> invokers = registryDirectory.list(invocation); URL aUrl = invokers.get(0).getUrl(); URL bUrl = invokers.get(1).getUrl(); - Assertions.assertEquals(aUrl.getHost().equals("10.20.30.140") ? "3" : "4", aUrl.getParameter("timeout")); - Assertions.assertEquals(bUrl.getHost().equals("10.20.30.141") ? "4" : "3", bUrl.getParameter("timeout")); + Assert.assertEquals(aUrl.getHost().equals("10.20.30.140") ? "3" : "4", aUrl.getParameter("timeout")); + Assert.assertEquals(bUrl.getHost().equals("10.20.30.141") ? "4" : "3", bUrl.getParameter("timeout")); } /** @@ -751,7 +747,7 @@ public void testNofityOverrideUrls_Clean1() { List> invokers = registryDirectory.list(invocation); Invoker aInvoker = invokers.get(0); //Need to be restored to the original providerUrl - Assertions.assertEquals("1", aInvoker.getUrl().getParameter("timeout")); + Assert.assertEquals("1", aInvoker.getUrl().getParameter("timeout")); } /** @@ -766,7 +762,7 @@ public void testNofityOverrideUrls_CleanOnly() { List durls = new ArrayList(); durls.add(SERVICEURL.setHost("10.20.30.140").addParameter("timeout", "1")); registryDirectory.notify(durls); - Assertions.assertEquals(null, registryDirectory.getUrl().getParameter("mock")); + Assert.assertEquals(null, registryDirectory.getUrl().getParameter("mock")); //override durls = new ArrayList(); @@ -774,8 +770,8 @@ public void testNofityOverrideUrls_CleanOnly() { registryDirectory.notify(durls); List> invokers = registryDirectory.list(invocation); Invoker aInvoker = invokers.get(0); - Assertions.assertEquals("1000", aInvoker.getUrl().getParameter("timeout")); - Assertions.assertEquals("fail", registryDirectory.getUrl().getParameter("mock")); + Assert.assertEquals("1000", aInvoker.getUrl().getParameter("timeout")); + Assert.assertEquals("fail", registryDirectory.getUrl().getParameter("mock")); //override clean durls = new ArrayList(); @@ -784,9 +780,9 @@ public void testNofityOverrideUrls_CleanOnly() { invokers = registryDirectory.list(invocation); aInvoker = invokers.get(0); //Need to be restored to the original providerUrl - Assertions.assertEquals("1", aInvoker.getUrl().getParameter("timeout")); + Assert.assertEquals("1", aInvoker.getUrl().getParameter("timeout")); - Assertions.assertEquals(null, registryDirectory.getUrl().getParameter("mock")); + Assert.assertEquals(null, registryDirectory.getUrl().getParameter("mock")); } /** @@ -810,7 +806,7 @@ public void testNofityOverrideUrls_CleanNOverride() { List> invokers = registryDirectory.list(invocation); Invoker aInvoker = invokers.get(0); - Assertions.assertEquals("4", aInvoker.getUrl().getParameter("timeout")); + Assert.assertEquals("4", aInvoker.getUrl().getParameter("timeout")); } /** @@ -833,7 +829,7 @@ public void testNofityOverrideUrls_disabled_allProvider() { List> invokers = registryDirectory.list(invocation); //All service providers can not be disabled through override. - Assertions.assertEquals(2, invokers.size()); + Assert.assertEquals(2, invokers.size()); } /** @@ -855,14 +851,14 @@ public void testNofityOverrideUrls_disabled_specifiedProvider() { registryDirectory.notify(durls); List> invokers = registryDirectory.list(invocation); - Assertions.assertEquals(1, invokers.size()); - Assertions.assertEquals("10.20.30.141", invokers.get(0).getUrl().getHost()); + Assert.assertEquals(1, invokers.size()); + Assert.assertEquals("10.20.30.141", invokers.get(0).getUrl().getHost()); durls = new ArrayList(); durls.add(URL.valueOf("empty://0.0.0.0?" + Constants.DISABLED_KEY + "=true&" + Constants.CATEGORY_KEY + "=" + Constants.CONFIGURATORS_CATEGORY)); registryDirectory.notify(durls); List> invokers2 = registryDirectory.list(invocation); - Assertions.assertEquals(2, invokers2.size()); + Assert.assertEquals(2, invokers2.size()); } /** @@ -880,20 +876,20 @@ public void testNofity_To_Decrease_provider() { registryDirectory.notify(durls); List> invokers = registryDirectory.list(invocation); - Assertions.assertEquals(2, invokers.size()); + Assert.assertEquals(2, invokers.size()); durls = new ArrayList(); durls.add(SERVICEURL.setHost("10.20.30.140")); registryDirectory.notify(durls); List> invokers2 = registryDirectory.list(invocation); - Assertions.assertEquals(1, invokers2.size()); - Assertions.assertEquals("10.20.30.140", invokers2.get(0).getUrl().getHost()); + Assert.assertEquals(1, invokers2.size()); + Assert.assertEquals("10.20.30.140", invokers2.get(0).getUrl().getHost()); durls = new ArrayList(); durls.add(URL.valueOf("empty://0.0.0.0?" + Constants.DISABLED_KEY + "=true&" + Constants.CATEGORY_KEY + "=" + Constants.CONFIGURATORS_CATEGORY)); registryDirectory.notify(durls); List> invokers3 = registryDirectory.list(invocation); - Assertions.assertEquals(1, invokers3.size()); + Assert.assertEquals(1, invokers3.size()); } /** @@ -912,15 +908,15 @@ public void testNofity_disabled_specifiedProvider() { registryDirectory.notify(durls); List> invokers = registryDirectory.list(invocation); - Assertions.assertEquals(1, invokers.size()); - Assertions.assertEquals("10.20.30.141", invokers.get(0).getUrl().getHost()); + Assert.assertEquals(1, invokers.size()); + Assert.assertEquals("10.20.30.141", invokers.get(0).getUrl().getHost()); //Enabled by override rule durls = new ArrayList(); durls.add(URL.valueOf("override://10.20.30.140:9091?" + Constants.DISABLED_KEY + "=false")); registryDirectory.notify(durls); List> invokers2 = registryDirectory.list(invocation); - Assertions.assertEquals(2, invokers2.size()); + Assert.assertEquals(2, invokers2.size()); } @Test @@ -939,13 +935,13 @@ public void testNotifyRouterUrls_Clean() { registryDirectory.notify(serviceUrls); // FIXME /* List routers = registryDirectory.getRouters(); - Assertions.assertEquals(1 + 1, routers.size()); + Assert.assertEquals(1 + 1, routers.size()); serviceUrls.clear(); serviceUrls.add(routerurl.addParameter(Constants.ROUTER_KEY, Constants.ROUTER_TYPE_CLEAR)); registryDirectory.notify(serviceUrls); routers = registryDirectory.getRouters(); - Assertions.assertEquals(0 + 1, routers.size());*/ + Assert.assertEquals(0 + 1, routers.size());*/ } /** @@ -961,16 +957,16 @@ public void testNotify_MockProviderOnly() { serviceUrls.add(SERVICEURL.setProtocol(Constants.MOCK_PROTOCOL)); registryDirectory.notify(serviceUrls); - Assertions.assertEquals(true, registryDirectory.isAvailable()); + Assert.assertEquals(true, registryDirectory.isAvailable()); invocation = new RpcInvocation(); List invokers = registryDirectory.list(invocation); - Assertions.assertEquals(2, invokers.size()); + Assert.assertEquals(2, invokers.size()); RpcInvocation mockinvocation = new RpcInvocation(); mockinvocation.setAttachment(Constants.INVOCATION_NEED_MOCK, "true"); invokers = registryDirectory.list(mockinvocation); - Assertions.assertEquals(1, invokers.size()); + Assert.assertEquals(1, invokers.size()); } // mock protocol @@ -990,7 +986,7 @@ public void test_Notified_acceptProtocol0() { invocation = new RpcInvocation(); List> invokers = registryDirectory.list(invocation); - Assertions.assertEquals(2, invokers.size()); + Assert.assertEquals(2, invokers.size()); } //Test the matching of protocol and select only the matched protocol for refer @@ -1009,7 +1005,7 @@ public void test_Notified_acceptProtocol1() { invocation = new RpcInvocation(); List> invokers = registryDirectory.list(invocation); - Assertions.assertEquals(1, invokers.size()); + Assert.assertEquals(1, invokers.size()); } //Test the matching of protocol and select only the matched protocol for refer @@ -1028,38 +1024,38 @@ public void test_Notified_acceptProtocol2() { invocation = new RpcInvocation(); List> invokers = registryDirectory.list(invocation); - Assertions.assertEquals(2, invokers.size()); + Assert.assertEquals(2, invokers.size()); } - + @Test public void test_Notified_withGroupFilter() { - URL directoryUrl = noMeaningUrl.addParameterAndEncoded(Constants.REFER_KEY, "interface" + service + "&group=group1,group2"); - RegistryDirectory directory = this.getRegistryDirectory(directoryUrl); - URL provider1 = URL.valueOf("dubbo://10.134.108.1:20880/" + service + "?methods=getXXX&group=group1&mock=false&application=mockApplication"); - URL provider2 = URL.valueOf("dubbo://10.134.108.1:20880/" + service + "?methods=getXXX&group=group2&mock=false&application=mockApplication"); - - List providers = new ArrayList<>(); - providers.add(provider1); - providers.add(provider2); - directory.notify(providers); - - invocation = new RpcInvocation(); - invocation.setMethodName("getXXX"); - List> invokers = directory.list(invocation); - - Assertions.assertEquals(2, invokers.size()); - Assertions.assertTrue(invokers.get(0) instanceof MockClusterInvoker); - Assertions.assertTrue(invokers.get(1) instanceof MockClusterInvoker); - - directoryUrl = noMeaningUrl.addParameterAndEncoded(Constants.REFER_KEY, "interface" + service + "&group=group1"); - directory = this.getRegistryDirectory(directoryUrl); - directory.notify(providers); - - invokers = directory.list(invocation); - - Assertions.assertEquals(2, invokers.size()); - Assertions.assertFalse(invokers.get(0) instanceof MockClusterInvoker); - Assertions.assertFalse(invokers.get(1) instanceof MockClusterInvoker); + URL directoryUrl = noMeaningUrl.addParameterAndEncoded(Constants.REFER_KEY, "interface" + service + "&group=group1,group2"); + RegistryDirectory directory = this.getRegistryDirectory(directoryUrl); + URL provider1 = URL.valueOf("dubbo://10.134.108.1:20880/"+service+"?methods=getXXX&group=group1&mock=false&application=mockApplication"); + URL provider2 = URL.valueOf("dubbo://10.134.108.1:20880/"+service+"?methods=getXXX&group=group2&mock=false&application=mockApplication"); + + List providers = new ArrayList<>(); + providers.add(provider1); + providers.add(provider2); + directory.notify(providers); + + invocation = new RpcInvocation(); + invocation.setMethodName("getXXX"); + List> invokers = directory.list(invocation); + + Assert.assertEquals(2, invokers.size()); + Assert.assertTrue(invokers.get(0) instanceof MockClusterInvoker); + Assert.assertTrue(invokers.get(1) instanceof MockClusterInvoker); + + directoryUrl = noMeaningUrl.addParameterAndEncoded(Constants.REFER_KEY, "interface" + service + "&group=group1"); + directory = this.getRegistryDirectory(directoryUrl); + directory.notify(providers); + + invokers = directory.list(invocation); + + Assert.assertEquals(2, invokers.size()); + Assert.assertFalse(invokers.get(0) instanceof MockClusterInvoker); + Assert.assertFalse(invokers.get(1) instanceof MockClusterInvoker); } enum Param { diff --git a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryProtocolTest.java b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryProtocolTest.java index f212a5c638f..1c102894232 100644 --- a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryProtocolTest.java +++ b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryProtocolTest.java @@ -36,17 +36,18 @@ import org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol; import org.apache.commons.lang3.ArrayUtils; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.util.ArrayList; import java.util.List; import static org.apache.dubbo.common.Constants.DEFAULT_REGISTER_PROVIDER_KEYS; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; /** * RegistryProtocolTest + * */ public class RegistryProtocolTest { @@ -65,18 +66,16 @@ public void testDefaultPort() { assertEquals(9090, registryProtocol.getDefaultPort()); } - @Test + @Test(expected = IllegalArgumentException.class) public void testExportUrlNull() { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - RegistryProtocol registryProtocol = new RegistryProtocol(); - registryProtocol.setCluster(new FailfastCluster()); - - Protocol dubboProtocol = DubboProtocol.getDubboProtocol(); - registryProtocol.setProtocol(dubboProtocol); - Invoker invoker = new DubboInvoker(DemoService.class, - registryUrl, new ExchangeClient[]{new MockedClient("10.20.20.20", 2222, true)}); - registryProtocol.export(invoker); - }); + RegistryProtocol registryProtocol = new RegistryProtocol(); + registryProtocol.setCluster(new FailfastCluster()); + + Protocol dubboProtocol = DubboProtocol.getDubboProtocol(); + registryProtocol.setProtocol(dubboProtocol); + Invoker invoker = new DubboInvoker(DemoService.class, + registryUrl, new ExchangeClient[]{new MockedClient("10.20.20.20", 2222, true)}); + registryProtocol.export(invoker); } @Test @@ -93,7 +92,7 @@ public void testExport() { Exporter exporter = registryProtocol.export(invoker); Exporter exporter2 = registryProtocol.export(invoker); //The same invoker, exporter that multiple exported are different - Assertions.assertNotSame(exporter, exporter2); + Assert.assertNotSame(exporter, exporter2); exporter.unexport(); exporter2.unexport(); @@ -169,7 +168,7 @@ public void testGetParamsToRegistry() { String[] additionalParams = new String[]{"key1", "key2"}; String[] registryParams = registryProtocol.getParamsToRegistry(DEFAULT_REGISTER_PROVIDER_KEYS, additionalParams); String[] expectParams = ArrayUtils.addAll(DEFAULT_REGISTER_PROVIDER_KEYS, additionalParams); - Assertions.assertArrayEquals(expectParams, registryParams); + Assert.assertArrayEquals(expectParams, registryParams); } private void destroyRegistryProtocol() { diff --git a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryStatusCheckerTest.java b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryStatusCheckerTest.java index 012251a3ae3..01b637d5203 100644 --- a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryStatusCheckerTest.java +++ b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryStatusCheckerTest.java @@ -23,11 +23,11 @@ import org.apache.dubbo.registry.status.RegistryStatusChecker; import org.apache.dubbo.registry.support.AbstractRegistryFactory; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; /** * StatusTest @@ -43,7 +43,7 @@ public class RegistryStatusCheckerTest { URL registryUrl = URL.valueOf("dubbo://cat:cat@127.0.0.1:9090/"); URL registryUrl2 = URL.valueOf("dubbo://cat:cat@127.0.0.1:9091"); - @BeforeEach + @Before public void setUp() { AbstractRegistryFactory.destroyAll(); } @@ -59,7 +59,7 @@ public void testCheckOK() { ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension().getRegistry(registryUrl2); assertEquals(Status.Level.OK, new RegistryStatusChecker().check().getLevel()); String message = new RegistryStatusChecker().check().getMessage(); - Assertions.assertTrue(message.contains(registryUrl.getAddress() + "(connected)")); - Assertions.assertTrue(message.contains(registryUrl2.getAddress() + "(connected)")); + Assert.assertTrue(message.contains(registryUrl.getAddress() + "(connected)")); + Assert.assertTrue(message.contains(registryUrl2.getAddress() + "(connected)")); } } \ No newline at end of file diff --git a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/SimpleRegistryExporter.java b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/SimpleRegistryExporter.java index 2b67e634143..b30462f7bf0 100644 --- a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/SimpleRegistryExporter.java +++ b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/SimpleRegistryExporter.java @@ -18,7 +18,6 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.registry.RegistryService; @@ -54,15 +53,14 @@ public static Exporter export(int port) { public static Exporter export(int port, RegistryService registryService) { return protocol.export(proxyFactory.getInvoker(registryService, RegistryService.class, - new URLBuilder(Constants.DUBBO_PROTOCOL, NetUtils.getLocalHost(), port, RegistryService.class.getName()) + new URL("dubbo", NetUtils.getLocalHost(), port, RegistryService.class.getName()) .setPath(RegistryService.class.getName()) .addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()) .addParameter(Constants.CLUSTER_STICKY_KEY, "true") .addParameter(Constants.CALLBACK_INSTANCES_LIMIT_KEY, "1000") .addParameter("ondisconnect", "disconnect") .addParameter("subscribe.1.callback", "true") - .addParameter("unsubscribe.1.callback", "false") - .build())); + .addParameter("unsubscribe.1.callback", "false"))); } -} +} \ No newline at end of file diff --git a/dubbo-registry/dubbo-registry-default/src/test/resources/log4j.xml b/dubbo-registry/dubbo-registry-default/src/test/resources/log4j.xml index 91905fd10b2..c8d6d8c5b58 100644 --- a/dubbo-registry/dubbo-registry-default/src/test/resources/log4j.xml +++ b/dubbo-registry/dubbo-registry-default/src/test/resources/log4j.xml @@ -21,7 +21,6 @@ - @@ -31,8 +30,14 @@ --> + + + + + + \ No newline at end of file diff --git a/dubbo-registry/dubbo-registry-etcd3/pom.xml b/dubbo-registry/dubbo-registry-etcd3/pom.xml deleted file mode 100644 index 00b0ae53902..00000000000 --- a/dubbo-registry/dubbo-registry-etcd3/pom.xml +++ /dev/null @@ -1,53 +0,0 @@ - - - - - 4.0.0 - - dubbo-registry - org.apache.dubbo - 2.7.1-SNAPSHOT - - - dubbo-registry-etcd3 - jar - ${project.artifactId} - The etcd3 registry module of Dubbo project - - - - org.apache.dubbo - dubbo-registry-api - ${project.parent.version} - - - org.apache.dubbo - dubbo-common - ${project.parent.version} - - - org.apache.dubbo - dubbo-remoting-etcd3 - ${project.parent.version} - - - - - \ No newline at end of file diff --git a/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdRegistry.java b/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdRegistry.java deleted file mode 100644 index 2e297fa604d..00000000000 --- a/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdRegistry.java +++ /dev/null @@ -1,362 +0,0 @@ -/* - * 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. - */ - -/* - * 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 org.apache.dubbo.registry.etcd; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.ConcurrentHashSet; -import org.apache.dubbo.common.utils.UrlUtils; -import org.apache.dubbo.registry.NotifyListener; -import org.apache.dubbo.registry.support.FailbackRegistry; -import org.apache.dubbo.remoting.etcd.ChildListener; -import org.apache.dubbo.remoting.etcd.EtcdClient; -import org.apache.dubbo.remoting.etcd.EtcdTransporter; -import org.apache.dubbo.remoting.etcd.StateListener; -import org.apache.dubbo.remoting.etcd.option.Constants; -import org.apache.dubbo.remoting.etcd.option.OptionUtil; -import org.apache.dubbo.rpc.RpcException; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - - -/** - * Support for ectd3 registry. - */ -public class EtcdRegistry extends FailbackRegistry { - - private final static Logger logger = LoggerFactory.getLogger(EtcdRegistry.class); - - private final static int DEFAULT_ETCD_PORT = 2379; - - private final static String DEFAULT_ROOT = "dubbo"; - - private final String root; - - private final Set anyServices = new ConcurrentHashSet<>(); - - private final ConcurrentMap> etcdListeners = new ConcurrentHashMap<>(); - private final EtcdClient etcdClient; - - public EtcdRegistry(URL url, EtcdTransporter etcdTransporter) { - super(url); - if (url.isAnyHost()) { - throw new IllegalStateException("registry address is invalid, actual: '" + url.getHost() + "'"); - } - String group = url.getParameter(Constants.GROUP_KEY, DEFAULT_ROOT); - if (!group.startsWith(Constants.PATH_SEPARATOR)) { - group = Constants.PATH_SEPARATOR + group; - } - this.root = group; - etcdClient = etcdTransporter.connect(url); - etcdClient.addStateListener(state -> { - if (state == StateListener.CONNECTED) { - try { - recover(); - } catch (Exception e) { - logger.error(e.getMessage(), e); - } - } - }); - } - - protected static String appendDefaultPort(String address) { - if (address != null && address.length() > 0) { - int i = address.indexOf(':'); - if (i < 0) { - return address + ":" + DEFAULT_ETCD_PORT; - } else if (Integer.parseInt(address.substring(i + 1)) == 0) { - return address.substring(0, i + 1) + DEFAULT_ETCD_PORT; - } - } - return address; - } - - @Override - public void doRegister(URL url) { - try { - String path = toUrlPath(url); - if (url.getParameter(Constants.DYNAMIC_KEY, true)) { - etcdClient.createEphemeral(path); - return; - } - etcdClient.create(path); - } catch (Throwable e) { - throw new RpcException("Failed to register " + url + " to etcd " + getUrl() - + ", cause: " + (OptionUtil.isProtocolError(e) - ? "etcd3 registry may not be supported yet or etcd3 registry is not available." - : e.getMessage()), e); - } - } - - @Override - public void doUnregister(URL url) { - try { - String path = toUrlPath(url); - etcdClient.delete(path); - } catch (Throwable e) { - throw new RpcException("Failed to unregister " + url + " to etcd " + getUrl() + ", cause: " + e.getMessage(), e); - } - } - - @Override - public void doSubscribe(URL url, NotifyListener listener) { - try { - if (Constants.ANY_VALUE.equals(url.getServiceInterface())) { - String root = toRootPath(); - - /* - * if we are interested in all interfaces, - * find out the current container or create one for the url, put or get only once. - */ - ConcurrentMap listeners = - Optional.ofNullable(etcdListeners.get(url)) - .orElseGet(() -> { - ConcurrentMap container, prev; - prev = etcdListeners.putIfAbsent(url, container = new ConcurrentHashMap<>()); - return prev != null ? prev : container; - }); - - /* - * if we have no interface watcher listener, - * find the current listener or create one for the current root, put or get only once. - */ - ChildListener interfaceListener = - Optional.ofNullable(listeners.get(listener)) - .orElseGet(() -> { - ChildListener childListener, prev; - prev = listeners.putIfAbsent(listener, childListener = (parentPath, currentChildren) -> { - /* - * because etcd3 does not support direct children watch events, - * we should filter not interface events. if we watch /dubbo - * and /dubbo/interface, when we put a key-value pair {/dubbo/interface/hello hello}, - * we will got events in watching path /dubbo. - */ - for (String child : currentChildren) { - child = URL.decode(child); - if (!anyServices.contains(child)) { - anyServices.add(child); - /* - * if new interface event arrived, we watch their direct children, - * eg: /dubbo/interface, /dubbo/interface and so on. - */ - subscribe(url.setPath(child).addParameters(Constants.INTERFACE_KEY, child, - Constants.CHECK_KEY, String.valueOf(false)), listener); - } - } - }); - return prev != null ? prev : childListener; - }); - - etcdClient.create(root); - /* - * at the first time, we want to pull already interface and then watch their direct children, - * eg: /dubbo/interface, /dubbo/interface and so on. - */ - List services = etcdClient.addChildListener(root, interfaceListener); - for (String service : services) { - service = URL.decode(service); - anyServices.add(service); - subscribe(url.setPath(service).addParameters(Constants.INTERFACE_KEY, service, - Constants.CHECK_KEY, String.valueOf(false)), listener); - } - } else { - List urls = new ArrayList<>(); - for (String path : toCategoriesPath(url)) { - - /* - * if we are interested in special categories (providers, consumers, routers and so on), - * we find out the current container or create one for the url, put or get only once. - */ - ConcurrentMap listeners = - Optional.ofNullable(etcdListeners.get(url)) - .orElseGet(() -> { - ConcurrentMap container, prev; - prev = etcdListeners.putIfAbsent(url, - container = new ConcurrentHashMap<>()); - return prev != null ? prev : container; - }); - - /* - * if we have no category watcher listener, - * we find out the current listener or create one for the current category, put or get only once. - */ - ChildListener childListener = - Optional.ofNullable(listeners.get(listener)) - .orElseGet(() -> { - ChildListener watchListener, prev; - prev = listeners.putIfAbsent(listener, watchListener = (parentPath, currentChildren) -> EtcdRegistry.this.notify(url, listener, - toUrlsWithEmpty(url, parentPath, currentChildren))); - return prev != null ? prev : watchListener; - }); - - etcdClient.create(path); - /* - * at the first time, we want to pull already category and then watch their direct children, - * eg: /dubbo/interface/providers, /dubbo/interface/consumers and so on. - */ - List children = etcdClient.addChildListener(path, childListener); - if (children != null) { - urls.addAll(toUrlsWithEmpty(url, path, children)); - } - } - notify(url, listener, urls); - } - } catch (Throwable e) { - throw new RpcException("Failed to subscribe " + url + " to etcd " + getUrl() - + ", cause: " + (OptionUtil.isProtocolError(e) - ? "etcd3 registry may not be supported yet or etcd3 registry is not available." - : e.getMessage()), e); - } - } - - @Override - public void doUnsubscribe(URL url, NotifyListener listener) { - ConcurrentMap listeners = etcdListeners.get(url); - if (listeners != null) { - ChildListener etcdListener = listeners.get(listener); - if (etcdListener != null) { - // maybe url has many subscribed paths - for (String path : toUnsubscribedPath(url)) { - etcdClient.removeChildListener(path, etcdListener); - } - } - } - } - - @Override - public boolean isAvailable() { - return etcdClient.isConnected(); - } - - @Override - public void destroy() { - super.destroy(); - try { - etcdClient.close(); - } catch (Exception e) { - logger.warn("Failed to close etcd client " + getUrl() + ", cause: " + e.getMessage(), e); - } - } - - protected String toRootDir() { - if (root.startsWith(Constants.PATH_SEPARATOR)) { - return root; - } - return Constants.PATH_SEPARATOR + root; - } - - protected String toRootPath() { - return root; - } - - protected String toServicePath(URL url) { - String name = url.getServiceInterface(); - if (Constants.ANY_VALUE.equals(name)) { - return toRootPath(); - } - return toRootDir() + Constants.PATH_SEPARATOR + URL.encode(name); - } - - protected String[] toCategoriesPath(URL url) { - String[] categories; - if (Constants.ANY_VALUE.equals(url.getParameter(Constants.CATEGORY_KEY))) { - categories = new String[]{Constants.PROVIDERS_CATEGORY, Constants.CONSUMERS_CATEGORY, - Constants.ROUTERS_CATEGORY, Constants.CONFIGURATORS_CATEGORY}; - } else { - categories = url.getParameter(Constants.CATEGORY_KEY, new String[]{Constants.DEFAULT_CATEGORY}); - } - String[] paths = new String[categories.length]; - for (int i = 0; i < categories.length; i++) { - paths[i] = toServicePath(url) + Constants.PATH_SEPARATOR + categories[i]; - } - return paths; - } - - protected String toCategoryPath(URL url) { - return toServicePath(url) + Constants.PATH_SEPARATOR + url.getParameter(Constants.CATEGORY_KEY, Constants.DEFAULT_CATEGORY); - } - - protected String toUrlPath(URL url) { - return toCategoryPath(url) + Constants.PATH_SEPARATOR + URL.encode(url.toFullString()); - } - - protected List toUnsubscribedPath(URL url) { - List categories = new ArrayList<>(); - if (Constants.ANY_VALUE.equals(url.getServiceInterface())) { - String group = url.getParameter(Constants.GROUP_KEY, DEFAULT_ROOT); - if (!group.startsWith(Constants.PATH_SEPARATOR)) { - group = Constants.PATH_SEPARATOR + group; - } - categories.add(group); - return categories; - } else { - categories.addAll(Arrays.asList(toCategoriesPath(url))); - } - return categories; - } - - protected List toUrlsWithoutEmpty(URL consumer, List providers) { - List urls = new ArrayList<>(); - if (providers != null && providers.size() > 0) { - for (String provider : providers) { - provider = URL.decode(provider); - if (provider.contains(Constants.HTTP_SUBFIX_KEY)) { - URL url = URL.valueOf(provider); - if (UrlUtils.isMatch(consumer, url)) { - urls.add(url); - } - } - } - } - return urls; - } - - protected List toUrlsWithEmpty(URL consumer, String path, List providers) { - List urls = toUrlsWithoutEmpty(consumer, providers); - if (urls == null || urls.isEmpty()) { - int i = path.lastIndexOf('/'); - String category = i < 0 ? path : path.substring(i + 1); - URL empty = consumer.setProtocol(Constants.EMPTY_PROTOCOL).addParameter(Constants.CATEGORY_KEY, category); - urls.add(empty); - } - return urls; - } -} diff --git a/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdRegistryFactory.java b/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdRegistryFactory.java deleted file mode 100644 index 187da199090..00000000000 --- a/dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdRegistryFactory.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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. - */ - -/* - * 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 org.apache.dubbo.registry.etcd; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.registry.Registry; -import org.apache.dubbo.registry.support.AbstractRegistryFactory; -import org.apache.dubbo.remoting.etcd.EtcdTransporter; - -public class EtcdRegistryFactory extends AbstractRegistryFactory { - - private EtcdTransporter etcdTransporter; - - @Override - protected Registry createRegistry(URL url) { - return new EtcdRegistry(url, etcdTransporter); - } - - public void setEtcdTransporter(EtcdTransporter etcdTransporter) { - this.etcdTransporter = etcdTransporter; - } -} diff --git a/dubbo-registry/dubbo-registry-etcd3/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.RegistryFactory b/dubbo-registry/dubbo-registry-etcd3/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.RegistryFactory deleted file mode 100644 index 4a6d09c9f0e..00000000000 --- a/dubbo-registry/dubbo-registry-etcd3/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.RegistryFactory +++ /dev/null @@ -1 +0,0 @@ -etcd3=org.apache.dubbo.registry.etcd.EtcdRegistryFactory \ No newline at end of file diff --git a/dubbo-registry/dubbo-registry-etcd3/src/test/java/org/apache/dubbo/registry/etcd/EtcdRegistryTest.java b/dubbo-registry/dubbo-registry-etcd3/src/test/java/org/apache/dubbo/registry/etcd/EtcdRegistryTest.java deleted file mode 100644 index c23c1d23a0a..00000000000 --- a/dubbo-registry/dubbo-registry-etcd3/src/test/java/org/apache/dubbo/registry/etcd/EtcdRegistryTest.java +++ /dev/null @@ -1,316 +0,0 @@ -/* - * 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. - */ - -/* - * 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. - */ - -/* - * 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 org.apache.dubbo.registry.etcd; - -import org.apache.dubbo.common.Constants; -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.extension.ExtensionLoader; -import org.apache.dubbo.common.utils.NetUtils; -import org.apache.dubbo.registry.NotifyListener; -import org.apache.dubbo.registry.RegistryFactory; -import org.apache.dubbo.registry.support.AbstractRegistryFactory; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; - -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicReference; - -@Disabled -public class EtcdRegistryTest { - - String service = "org.apache.dubbo.internal.test.DemoServie"; - String outerService = "org.apache.dubbo.outer.test.OuterDemoServie"; - URL serviceUrl = URL.valueOf("dubbo://" + NetUtils.getLocalHost() + "/" + service + "?methods=test1,test2"); - URL serviceUrl2 = URL.valueOf("dubbo://" + NetUtils.getLocalHost() + "/" + service + "?methods=test1,test2,test3"); - URL serviceUrl3 = URL.valueOf("dubbo://" + NetUtils.getLocalHost() + "/" + outerService + "?methods=test1,test2"); - URL registryUrl = URL.valueOf("etcd3://127.0.0.1:2379/org.apache.dubbo.registry.RegistryService"); - URL consumerUrl = URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":2018" + "/" + service + "?methods=test1,test2"); - RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension(); - EtcdRegistry registry; - URL subscribe = new URL( - Constants.ADMIN_PROTOCOL, NetUtils.getLocalHost(), 0, "", - Constants.INTERFACE_KEY, Constants.ANY_VALUE, - Constants.GROUP_KEY, Constants.ANY_VALUE, - Constants.VERSION_KEY, Constants.ANY_VALUE, - Constants.CLASSIFIER_KEY, Constants.ANY_VALUE, - Constants.CATEGORY_KEY, Constants.PROVIDERS_CATEGORY + "," - + Constants.CONSUMERS_CATEGORY + "," - + Constants.ROUTERS_CATEGORY + "," - + Constants.CONFIGURATORS_CATEGORY, - Constants.ENABLED_KEY, Constants.ANY_VALUE, - Constants.CHECK_KEY, String.valueOf(false)); - - @Test - public void test_register() { - - registry.register(serviceUrl); - Set registered = registry.getRegistered(); - Assertions.assertEquals(1, registered.size()); - Assertions.assertTrue(registered.contains(serviceUrl)); - - registry.unregister(serviceUrl); - } - - @Test - public void test_unregister() { - - registry.register(serviceUrl); - Set registered = registry.getRegistered(); - Assertions.assertTrue(registered.size() == 1); - Assertions.assertTrue(registered.contains(serviceUrl)); - - registry.unregister(serviceUrl); - - registered = registry.getRegistered(); - Assertions.assertTrue(registered.size() == 0); - } - - @Test - public void test_subscribe() { - - registry.register(serviceUrl); - - final AtomicReference notifiedUrl = new AtomicReference(); - registry.subscribe(consumerUrl, new NotifyListener() { - public void notify(List urls) { - notifiedUrl.set(urls.get(0)); - } - }); - Assertions.assertEquals(serviceUrl.toFullString(), notifiedUrl.get().toFullString()); - Map> arg = registry.getSubscribed(); - Assertions.assertEquals(consumerUrl, arg.keySet().iterator().next()); - } - - @Test - public void test_subscribe_when_register() throws InterruptedException { - - Assertions.assertTrue(registry.getRegistered().size() == 0); - Assertions.assertTrue(registry.getSubscribed().size() == 0); - - CountDownLatch notNotified = new CountDownLatch(2); - - final AtomicReference notifiedUrl = new AtomicReference(); - registry.subscribe(consumerUrl, new NotifyListener() { - public void notify(List urls) { - notifiedUrl.set(urls.get(0)); - notNotified.countDown(); - } - }); - - registry.register(serviceUrl); - - Assertions.assertTrue(notNotified.await(15, TimeUnit.SECONDS)); - - Assertions.assertEquals(serviceUrl.toFullString(), notifiedUrl.get().toFullString()); - Map> subscribed = registry.getSubscribed(); - Assertions.assertEquals(consumerUrl, subscribed.keySet().iterator().next()); - } - - @Test - public void test_subscribe_when_register0() throws InterruptedException { - - Assertions.assertTrue(registry.getRegistered().size() == 0); - Assertions.assertTrue(registry.getSubscribed().size() == 0); - - CountDownLatch notNotified = new CountDownLatch(3); - ConcurrentHashMap notifiedUrls = new ConcurrentHashMap<>(); - registry.subscribe(consumerUrl, new NotifyListener() { - public void notify(List urls) { - if (urls != null && urls.size() > 0) { - if (!urls.get(0).getProtocol().equals("empty")) { - for (Iterator iterator = urls.iterator(); iterator.hasNext(); ) { - notifiedUrls.put(iterator.next(), true); - } - } - } - - notNotified.countDown(); - } - }); - - registry.register(serviceUrl); - registry.register(serviceUrl2); - - Assertions.assertTrue(notNotified.await(15, TimeUnit.SECONDS)); - - Assertions.assertTrue(notifiedUrls.containsKey(serviceUrl)); - Assertions.assertTrue(notifiedUrls.containsKey(serviceUrl2)); - Map> subscribed = registry.getSubscribed(); - Assertions.assertEquals(consumerUrl, subscribed.keySet().iterator().next()); - } - - @Test - public void test_subscribe_when_register1() throws InterruptedException { - - Assertions.assertTrue(registry.getRegistered().size() == 0); - Assertions.assertTrue(registry.getSubscribed().size() == 0); - - CountDownLatch notNotified = new CountDownLatch(2); - - final AtomicReference notifiedUrls = new AtomicReference(); - registry.subscribe(consumerUrl, new NotifyListener() { - public void notify(List urls) { - notifiedUrls.set(urls.get(0)); - notNotified.countDown(); - } - }); - - registry.register(serviceUrl); - // register service3 should not trigger notify - registry.register(serviceUrl3); - - Assertions.assertTrue(notNotified.await(15, TimeUnit.SECONDS)); - - Assertions.assertEquals(serviceUrl, notifiedUrls.get()); - Map> subscribed = registry.getSubscribed(); - Assertions.assertEquals(consumerUrl, subscribed.keySet().iterator().next()); - } - - @Test - public void test_subscribe_when_register2() throws InterruptedException { - - Assertions.assertTrue(registry.getRegistered().size() == 0); - Assertions.assertTrue(registry.getSubscribed().size() == 0); - - CountDownLatch notNotified = new CountDownLatch(3); - - ConcurrentHashMap notifiedUrls = new ConcurrentHashMap<>(); - - registry.subscribe(subscribe, new NotifyListener() { - public void notify(List urls) { - if (urls != null && urls.size() > 0) { - if (!urls.get(0).getProtocol().equals("empty")) { - for (Iterator iterator = urls.iterator(); iterator.hasNext(); ) { - notifiedUrls.put(iterator.next(), true); - } - notNotified.countDown(); - } - } - } - }); - - registry.register(serviceUrl); - registry.register(serviceUrl2); - // service3 interface is not equals server2 - registry.register(serviceUrl3); - - Assertions.assertTrue(notNotified.await(15, TimeUnit.SECONDS)); - Assertions.assertTrue(notifiedUrls.size() == 3); - Assertions.assertTrue(notifiedUrls.containsKey(serviceUrl)); - Assertions.assertTrue(notifiedUrls.containsKey(serviceUrl2)); - Assertions.assertTrue(notifiedUrls.containsKey(serviceUrl3)); - } - - @Test - public void test_unsubscribe() throws InterruptedException { - - Assertions.assertTrue(registry.getRegistered().size() == 0); - Assertions.assertTrue(registry.getSubscribed().size() == 0); - - CountDownLatch notNotified = new CountDownLatch(2); - - final AtomicReference notifiedUrl = new AtomicReference(); - - NotifyListener listener = new NotifyListener() { - public void notify(List urls) { - if (urls != null) { - for (Iterator iterator = urls.iterator(); iterator.hasNext(); ) { - URL url = iterator.next(); - if (!url.getProtocol().equals("empty")) { - notifiedUrl.set(url); - notNotified.countDown(); - } - } - } - } - }; - registry.subscribe(consumerUrl, listener); - registry.unsubscribe(consumerUrl, listener); - - registry.register(serviceUrl); - - Assertions.assertFalse(notNotified.await(2, TimeUnit.SECONDS)); - // expect nothing happen - Assertions.assertTrue(notifiedUrl.get() == null); - } - - @BeforeEach - public void setUp() { - registry = (EtcdRegistry) registryFactory.getRegistry(registryUrl); - Assertions.assertTrue(registry != null); - if (!registry.isAvailable()) { - AbstractRegistryFactory.destroyAll(); - registry = (EtcdRegistry) registryFactory.getRegistry(registryUrl); - } - } - - @AfterEach - public void tearDown() { - - registry.unregister(serviceUrl); - registry.unregister(serviceUrl2); - registry.unregister(serviceUrl3); - registry.unregister(subscribe); - - registry.destroy(); - } - - -} diff --git a/dubbo-registry/dubbo-registry-multicast/pom.xml b/dubbo-registry/dubbo-registry-multicast/pom.xml index 61a8669e8fb..79e2e4ee57f 100644 --- a/dubbo-registry/dubbo-registry-multicast/pom.xml +++ b/dubbo-registry/dubbo-registry-multicast/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-registry - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-registry-multicast jar diff --git a/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java b/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java index 3223532c07f..06835dac6e5 100644 --- a/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java +++ b/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java @@ -20,7 +20,6 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.ConcurrentHashSet; import org.apache.dubbo.common.utils.ExecutorUtil; import org.apache.dubbo.common.utils.NamedThreadFactory; @@ -31,7 +30,6 @@ import java.io.IOException; import java.net.DatagramPacket; -import java.net.Inet4Address; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.MulticastSocket; @@ -82,11 +80,14 @@ public MulticastRegistry(URL url) { } try { multicastAddress = InetAddress.getByName(url.getHost()); - checkMulticastAddress(multicastAddress); - + if (!multicastAddress.isMulticastAddress()) { + throw new IllegalArgumentException("Invalid multicast address " + url.getHost() + + ", ipv4 multicast address scope: 224.0.0.0 - 239.255.255.255."); + } multicastPort = url.getPort() <= 0 ? DEFAULT_MULTICAST_PORT : url.getPort(); multicastSocket = new MulticastSocket(multicastPort); - NetUtils.joinMulticastGroup(multicastSocket, multicastAddress); + multicastSocket.setLoopbackMode(false); + multicastSocket.joinGroup(multicastAddress); Thread thread = new Thread(new Runnable() { @Override public void run() { @@ -132,19 +133,6 @@ public void run() { } } - private void checkMulticastAddress(InetAddress multicastAddress) { - if (!multicastAddress.isMulticastAddress()) { - String message = "Invalid multicast address " + multicastAddress; - if (!(multicastAddress instanceof Inet4Address)) { - throw new IllegalArgumentException(message + ", " + - "ipv4 multicast address scope: 224.0.0.0 - 239.255.255.255."); - } else { - throw new IllegalArgumentException(message + ", " + "ipv6 multicast address must start with ff, " + - "for example: ff01::1"); - } - } - } - /** * Remove the expired providers, only when "clean" parameter is true. */ @@ -164,18 +152,40 @@ private void clean() { } private boolean isExpired(URL url) { - if (!url.getParameter(Constants.DYNAMIC_KEY, true) || url.getPort() <= 0 || Constants.CONSUMER_PROTOCOL.equals(url.getProtocol()) || Constants.ROUTE_PROTOCOL.equals(url.getProtocol()) || Constants.OVERRIDE_PROTOCOL.equals(url.getProtocol())) { + if (!url.getParameter(Constants.DYNAMIC_KEY, true) + || url.getPort() <= 0 + || Constants.CONSUMER_PROTOCOL.equals(url.getProtocol()) + || Constants.ROUTE_PROTOCOL.equals(url.getProtocol()) + || Constants.OVERRIDE_PROTOCOL.equals(url.getProtocol())) { return false; } - try (Socket socket = new Socket(url.getHost(), url.getPort())) { + Socket socket = null; + try { + socket = new Socket(url.getHost(), url.getPort()); } catch (Throwable e) { try { Thread.sleep(100); } catch (Throwable e2) { } - try (Socket socket2 = new Socket(url.getHost(), url.getPort())) { + Socket socket2 = null; + try { + socket2 = new Socket(url.getHost(), url.getPort()); } catch (Throwable e2) { return true; + } finally { + if (socket2 != null) { + try { + socket2.close(); + } catch (Throwable e2) { + } + } + } + } finally { + if (socket != null) { + try { + socket.close(); + } catch (Throwable e) { + } } } return false; @@ -194,10 +204,11 @@ private void receive(String msg, InetSocketAddress remoteAddress) { } else if (msg.startsWith(Constants.SUBSCRIBE)) { URL url = URL.valueOf(msg.substring(Constants.SUBSCRIBE.length()).trim()); Set urls = getRegistered(); - if (CollectionUtils.isNotEmpty(urls)) { + if (urls != null && !urls.isEmpty()) { for (URL u : urls) { if (UrlUtils.isMatch(url, u)) { - String host = remoteAddress != null && remoteAddress.getAddress() != null ? remoteAddress.getAddress().getHostAddress() : url.getIp(); + String host = remoteAddress != null && remoteAddress.getAddress() != null + ? remoteAddress.getAddress().getHostAddress() : url.getIp(); if (url.getParameter("unicast", true) // Whether the consumer's machine has only one process && !NetUtils.getLocalHost().equals(host)) { // Multiple processes in the same machine cannot be unicast with unicast or there will be only one process receiving information unicast(Constants.REGISTER + " " + u.toFullString(), host); @@ -263,7 +274,8 @@ public void doSubscribe(URL url, NotifyListener listener) { @Override public void doUnsubscribe(URL url, NotifyListener listener) { - if (!Constants.ANY_VALUE.equals(url.getServiceInterface()) && url.getParameter(Constants.REGISTER_KEY, true)) { + if (!Constants.ANY_VALUE.equals(url.getServiceInterface()) + && url.getParameter(Constants.REGISTER_KEY, true)) { unregister(url); } multicast(Constants.UNSUBSCRIBE + " " + url.toFullString()); @@ -285,7 +297,9 @@ public boolean isAvailable() { public void destroy() { super.destroy(); try { - ExecutorUtil.cancelScheduledFuture(cleanFuture); + if (cleanFuture != null) { + cleanFuture.cancel(true); + } } catch (Throwable t) { logger.warn(t.getMessage(), t); } @@ -327,8 +341,8 @@ protected void unregistered(URL url) { if (urls != null) { urls.remove(url); } - if (urls == null || urls.isEmpty()) { - if (urls == null) { + if (urls == null || urls.isEmpty()){ + if (urls == null){ urls = new ConcurrentHashSet(); } URL empty = url.setProtocol(Constants.EMPTY_PROTOCOL); @@ -349,7 +363,7 @@ protected void subscribed(URL url, NotifyListener listener) { private List toList(Set urls) { List list = new ArrayList(); - if (CollectionUtils.isNotEmpty(urls)) { + if (urls != null && !urls.isEmpty()) { for (URL url : urls) { list.add(url); } @@ -383,7 +397,7 @@ public void unsubscribe(URL url, NotifyListener listener) { @Override public List lookup(URL url) { - List urls = new ArrayList<>(); + List urls = new ArrayList(); Map> notifiedUrls = getNotified().get(url); if (notifiedUrls != null && notifiedUrls.size() > 0) { for (List values : notifiedUrls.values()) { @@ -392,7 +406,7 @@ public List lookup(URL url) { } if (urls.isEmpty()) { List cacheUrls = getCacheUrls(url); - if (CollectionUtils.isNotEmpty(cacheUrls)) { + if (cacheUrls != null && !cacheUrls.isEmpty()) { urls.addAll(cacheUrls); } } diff --git a/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryFactoryTest.java b/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryFactoryTest.java index 1da250704f9..1f74d6ff38d 100644 --- a/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryFactoryTest.java +++ b/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryFactoryTest.java @@ -18,10 +18,10 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.registry.Registry; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.hamcrest.CoreMatchers.*; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class MulticastRegistryFactoryTest { @Test diff --git a/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java b/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java index 76eece027e5..22d933f5551 100644 --- a/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java +++ b/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java @@ -19,22 +19,20 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.registry.NotifyListener; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.net.InetAddress; import java.net.MulticastSocket; import java.util.List; import java.util.Map; import java.util.Set; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; public class MulticastRegistryTest { @@ -46,7 +44,7 @@ public class MulticastRegistryTest { private URL consumerUrl = URL.valueOf("subscribe://" + NetUtils.getLocalHost() + "/" + service + "?arg1=1&arg2=2"); private MulticastRegistry registry = new MulticastRegistry(registryUrl); - @BeforeEach + @Before public void setUp() { registry.register(serviceUrl); } @@ -54,23 +52,19 @@ public void setUp() { /** * Test method for {@link org.apache.dubbo.registry.multicast.MulticastRegistry#MulticastRegistry(URL)}. */ - @Test + @Test(expected = IllegalStateException.class) public void testUrlError() { - Assertions.assertThrows(IllegalStateException.class, () -> { - URL errorUrl = URL.valueOf("multicast://mullticast/"); - new MulticastRegistry(errorUrl); - }); + URL errorUrl = URL.valueOf("multicast://mullticast/"); + new MulticastRegistry(errorUrl); } /** * Test method for {@link org.apache.dubbo.registry.multicast.MulticastRegistry#MulticastRegistry(URL)}. */ - @Test + @Test(expected = IllegalStateException.class) public void testAnyHost() { - Assertions.assertThrows(IllegalStateException.class, () -> { - URL errorUrl = URL.valueOf("multicast://0.0.0.0/"); - new MulticastRegistry(errorUrl); - }); + URL errorUrl = URL.valueOf("multicast://0.0.0.0/"); + new MulticastRegistry(errorUrl); } /** @@ -92,9 +86,7 @@ public void testRegister() { Set registered; // clear first registered = registry.getRegistered(); - for (URL url : registered) { - registry.unregister(url); - } + registered.clear(); for (int i = 0; i < 2; i++) { registry.register(serviceUrl); @@ -202,7 +194,7 @@ public void testDefaultPort() { MulticastRegistry multicastRegistry = new MulticastRegistry(URL.valueOf("multicast://224.5.6.7")); try { MulticastSocket multicastSocket = multicastRegistry.getMulticastSocket(); - Assertions.assertEquals(1234, multicastSocket.getLocalPort()); + Assert.assertEquals(1234, multicastSocket.getLocalPort()); } finally { multicastRegistry.destroy(); } @@ -223,41 +215,4 @@ public void testCustomedPort() { } } - @Test - public void testMulticastAddress() { - InetAddress multicastAddress = null; - MulticastSocket multicastSocket = null; - try { - // ipv4 multicast address - multicastAddress = InetAddress.getByName("224.55.66.77"); - multicastSocket = new MulticastSocket(2345); - multicastSocket.setLoopbackMode(false); - NetUtils.setInterface(multicastSocket, false); - multicastSocket.joinGroup(multicastAddress); - } catch (Exception e) { - Assertions.fail(e); - } finally { - if (multicastSocket != null) { - multicastSocket.close(); - } - } - - // multicast ipv6 address, - try { - multicastAddress = InetAddress.getByName("ff01::1"); - - multicastSocket = new MulticastSocket(); - multicastSocket.setLoopbackMode(false); - NetUtils.setInterface(multicastSocket, true); - multicastSocket.joinGroup(multicastAddress); - } catch (Throwable t) { - t.printStackTrace(); - } finally { - if (multicastSocket != null) { - multicastSocket.close(); - } - } - - } - } diff --git a/dubbo-registry/dubbo-registry-redis/pom.xml b/dubbo-registry/dubbo-registry-redis/pom.xml index 7644e1ca85a..12f2aecbe6f 100644 --- a/dubbo-registry/dubbo-registry-redis/pom.xml +++ b/dubbo-registry/dubbo-registry-redis/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-registry - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-registry-redis jar diff --git a/dubbo-registry/dubbo-registry-redis/src/main/java/org/apache/dubbo/registry/redis/RedisRegistry.java b/dubbo-registry/dubbo-registry-redis/src/main/java/org/apache/dubbo/registry/redis/RedisRegistry.java index b693842eb91..d37276345df 100644 --- a/dubbo-registry/dubbo-registry-redis/src/main/java/org/apache/dubbo/registry/redis/RedisRegistry.java +++ b/dubbo-registry/dubbo-registry-redis/src/main/java/org/apache/dubbo/registry/redis/RedisRegistry.java @@ -16,14 +16,10 @@ */ package org.apache.dubbo.registry.redis; -import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.ArrayUtils; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.ExecutorUtil; import org.apache.dubbo.common.utils.NamedThreadFactory; import org.apache.dubbo.common.utils.StringUtils; @@ -31,6 +27,8 @@ import org.apache.dubbo.registry.NotifyListener; import org.apache.dubbo.registry.support.FailbackRegistry; import org.apache.dubbo.rpc.RpcException; + +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPubSub; @@ -38,24 +36,24 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Random; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; /** * RedisRegistry + * */ public class RedisRegistry extends FailbackRegistry { @@ -71,9 +69,9 @@ public class RedisRegistry extends FailbackRegistry { private final String root; - private final Map jedisPools = new ConcurrentHashMap<>(); + private final Map jedisPools = new ConcurrentHashMap(); - private final ConcurrentMap notifiers = new ConcurrentHashMap<>(); + private final ConcurrentMap notifiers = new ConcurrentHashMap(); private final int reconnectPeriod; @@ -123,10 +121,10 @@ public RedisRegistry(URL url) { } replicate = "replicate".equals(cluster); - List addresses = new ArrayList<>(); + List addresses = new ArrayList(); addresses.add(url.getAddress()); String[] backups = url.getParameter(Constants.BACKUP_KEY, new String[0]); - if (ArrayUtils.isNotEmpty(backups)) { + if (backups != null && backups.length > 0) { addresses.addAll(Arrays.asList(backups)); } @@ -157,11 +155,14 @@ public RedisRegistry(URL url) { this.root = group; this.expirePeriod = url.getParameter(Constants.SESSION_TIMEOUT_KEY, Constants.DEFAULT_SESSION_TIMEOUT); - this.expireFuture = expireExecutor.scheduleWithFixedDelay(() -> { - try { - deferExpired(); // Extend the expiration time - } catch (Throwable t) { // Defensive fault tolerance - logger.error("Unexpected exception occur at defer expire time, cause: " + t.getMessage(), t); + this.expireFuture = expireExecutor.scheduleWithFixedDelay(new Runnable() { + @Override + public void run() { + try { + deferExpired(); // Extend the expiration time + } catch (Throwable t) { // Defensive fault tolerance + logger.error("Unexpected exception occur at defer expire time, cause: " + t.getMessage(), t); + } } }, expirePeriod / 2, expirePeriod / 2, TimeUnit.MILLISECONDS); } @@ -170,8 +171,9 @@ private void deferExpired() { for (Map.Entry entry : jedisPools.entrySet()) { JedisPool jedisPool = entry.getValue(); try { - try (Jedis jedis = jedisPool.getResource()) { - for (URL url : new HashSet<>(getRegistered())) { + Jedis jedis = jedisPool.getResource(); + try { + for (URL url : new HashSet(getRegistered())) { if (url.getParameter(Constants.DYNAMIC_KEY, true)) { String key = toCategoryPath(url); if (jedis.hset(key, url.toFullString(), String.valueOf(System.currentTimeMillis() + expirePeriod)) == 1) { @@ -185,6 +187,8 @@ private void deferExpired() { if (!replicate) { break;//  If the server side has synchronized data, just write a single machine } + } finally { + jedis.close(); } } catch (Throwable t) { logger.warn("Failed to write provider heartbeat to redis registry. registry: " + entry.getKey() + ", cause: " + t.getMessage(), t); @@ -195,10 +199,10 @@ private void deferExpired() { // The monitoring center is responsible for deleting outdated dirty data private void clean(Jedis jedis) { Set keys = jedis.keys(root + Constants.ANY_VALUE); - if (CollectionUtils.isNotEmpty(keys)) { + if (keys != null && !keys.isEmpty()) { for (String key : keys) { Map values = jedis.hgetAll(key); - if (CollectionUtils.isNotEmptyMap(values)) { + if (values != null && values.size() > 0) { boolean delete = false; long now = System.currentTimeMillis(); for (Map.Entry entry : values.entrySet()) { @@ -225,9 +229,14 @@ private void clean(Jedis jedis) { @Override public boolean isAvailable() { for (JedisPool jedisPool : jedisPools.values()) { - try (Jedis jedis = jedisPool.getResource()) { - if (jedis.isConnected()) { - return true; // At least one single machine is available. + try { + Jedis jedis = jedisPool.getResource(); + try { + if (jedis.isConnected()) { + return true; // At least one single machine is available. + } + } finally { + jedis.close(); } } catch (Throwable t) { } @@ -271,13 +280,16 @@ public void doRegister(URL url) { for (Map.Entry entry : jedisPools.entrySet()) { JedisPool jedisPool = entry.getValue(); try { - try (Jedis jedis = jedisPool.getResource()) { + Jedis jedis = jedisPool.getResource(); + try { jedis.hset(key, value, expire); jedis.publish(key, Constants.REGISTER); success = true; if (!replicate) { break; //  If the server side has synchronized data, just write a single machine } + } finally { + jedis.close(); } } catch (Throwable t) { exception = new RpcException("Failed to register service to redis registry. registry: " + entry.getKey() + ", service: " + url + ", cause: " + t.getMessage(), t); @@ -301,13 +313,16 @@ public void doUnregister(URL url) { for (Map.Entry entry : jedisPools.entrySet()) { JedisPool jedisPool = entry.getValue(); try { - try (Jedis jedis = jedisPool.getResource()) { + Jedis jedis = jedisPool.getResource(); + try { jedis.hdel(key, value); jedis.publish(key, Constants.UNREGISTER); success = true; if (!replicate) { break; //  If the server side has synchronized data, just write a single machine } + } finally { + jedis.close(); } } catch (Throwable t) { exception = new RpcException("Failed to unregister service to redis registry. registry: " + entry.getKey() + ", service: " + url + ", cause: " + t.getMessage(), t); @@ -339,26 +354,33 @@ public void doSubscribe(final URL url, final NotifyListener listener) { for (Map.Entry entry : jedisPools.entrySet()) { JedisPool jedisPool = entry.getValue(); try { - try (Jedis jedis = jedisPool.getResource()) { + Jedis jedis = jedisPool.getResource(); + try { if (service.endsWith(Constants.ANY_VALUE)) { admin = true; Set keys = jedis.keys(service); - if (CollectionUtils.isNotEmpty(keys)) { - Map> serviceKeys = new HashMap<>(); + if (keys != null && !keys.isEmpty()) { + Map> serviceKeys = new HashMap>(); for (String key : keys) { String serviceKey = toServicePath(key); - Set sk = serviceKeys.computeIfAbsent(serviceKey, k -> new HashSet<>()); + Set sk = serviceKeys.get(serviceKey); + if (sk == null) { + sk = new HashSet(); + serviceKeys.put(serviceKey, sk); + } sk.add(key); } for (Set sk : serviceKeys.values()) { - doNotify(jedis, sk, url, Collections.singletonList(listener)); + doNotify(jedis, sk, url, Arrays.asList(listener)); } } } else { - doNotify(jedis, jedis.keys(service + Constants.PATH_SEPARATOR + Constants.ANY_VALUE), url, Collections.singletonList(listener)); + doNotify(jedis, jedis.keys(service + Constants.PATH_SEPARATOR + Constants.ANY_VALUE), url, Arrays.asList(listener)); } success = true; break; // Just read one server's data + } finally { + jedis.close(); } } catch (Throwable t) { // Try the next server exception = new RpcException("Failed to subscribe service from redis registry. registry: " + entry.getKey() + ", service: " + url + ", cause: " + t.getMessage(), t); @@ -378,8 +400,8 @@ public void doUnsubscribe(URL url, NotifyListener listener) { } private void doNotify(Jedis jedis, String key) { - for (Map.Entry> entry : new HashMap<>(getSubscribed()).entrySet()) { - doNotify(jedis, Collections.singletonList(key), entry.getKey(), new HashSet<>(entry.getValue())); + for (Map.Entry> entry : new HashMap>(getSubscribed()).entrySet()) { + doNotify(jedis, Arrays.asList(key), entry.getKey(), new HashSet(entry.getValue())); } } @@ -389,7 +411,7 @@ private void doNotify(Jedis jedis, Collection keys, URL url, Collection< return; } long now = System.currentTimeMillis(); - List result = new ArrayList<>(); + List result = new ArrayList(); List categories = Arrays.asList(url.getParameter(Constants.CATEGORY_KEY, new String[0])); String consumerService = url.getServiceInterface(); for (String key : keys) { @@ -403,9 +425,9 @@ private void doNotify(Jedis jedis, Collection keys, URL url, Collection< if (!categories.contains(Constants.ANY_VALUE) && !categories.contains(category)) { continue; } - List urls = new ArrayList<>(); + List urls = new ArrayList(); Map values = jedis.hgetAll(key); - if (CollectionUtils.isNotEmptyMap(values)) { + if (values != null && values.size() > 0) { for (Map.Entry entry : values.entrySet()) { URL u = URL.valueOf(entry.getKey()); if (!u.getParameter(Constants.DYNAMIC_KEY, true) @@ -417,19 +439,17 @@ private void doNotify(Jedis jedis, Collection keys, URL url, Collection< } } if (urls.isEmpty()) { - urls.add(URLBuilder.from(url) - .setProtocol(Constants.EMPTY_PROTOCOL) + urls.add(url.setProtocol(Constants.EMPTY_PROTOCOL) .setAddress(Constants.ANYHOST_VALUE) .setPath(toServiceName(key)) - .addParameter(Constants.CATEGORY_KEY, category) - .build()); + .addParameter(Constants.CATEGORY_KEY, category)); } result.addAll(urls); if (logger.isInfoEnabled()) { logger.info("redis notify: " + key + " = " + urls); } } - if (CollectionUtils.isEmpty(result)) { + if (result == null || result.isEmpty()) { return; } for (NotifyListener listener : listeners) { @@ -521,6 +541,7 @@ private class Notifier extends Thread { private final String service; private final AtomicInteger connectSkip = new AtomicInteger(); private final AtomicInteger connectSkipped = new AtomicInteger(); + private final Random random = new Random(); private volatile Jedis jedis; private volatile boolean first = true; private volatile boolean running = true; @@ -542,7 +563,7 @@ private boolean isSkip() { int skip = connectSkip.get(); // Growth of skipping times if (skip >= 10) { // If the number of skipping times increases by more than 10, take the random number if (connectRandom == 0) { - connectRandom = ThreadLocalRandom.current().nextInt(10); + connectRandom = random.nextInt(10); } skip = 10 + connectRandom; } @@ -567,10 +588,10 @@ public void run() { jedis = jedisPool.getResource(); try { if (service.endsWith(Constants.ANY_VALUE)) { - if (first) { + if (!first) { first = false; Set keys = jedis.keys(service); - if (CollectionUtils.isNotEmpty(keys)) { + if (keys != null && !keys.isEmpty()) { for (String s : keys) { doNotify(jedis, s); } @@ -579,7 +600,7 @@ public void run() { } jedis.psubscribe(new NotifySub(jedisPool), service); // blocking } else { - if (first) { + if (!first) { first = false; doNotify(jedis, service); resetSkip(); diff --git a/dubbo-registry/dubbo-registry-redis/src/test/java/org/apache/dubbo/registry/redis/RedisRegistryTest.java b/dubbo-registry/dubbo-registry-redis/src/test/java/org/apache/dubbo/registry/redis/RedisRegistryTest.java index 44650cef722..e03c939191a 100644 --- a/dubbo-registry/dubbo-registry-redis/src/test/java/org/apache/dubbo/registry/redis/RedisRegistryTest.java +++ b/dubbo-registry/dubbo-registry-redis/src/test/java/org/apache/dubbo/registry/redis/RedisRegistryTest.java @@ -20,11 +20,9 @@ import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.registry.NotifyListener; import org.apache.dubbo.registry.Registry; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import redis.embedded.RedisServer; import java.util.List; @@ -33,7 +31,7 @@ import static org.apache.dubbo.common.Constants.BACKUP_KEY; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class RedisRegistryTest { @@ -43,7 +41,7 @@ public class RedisRegistryTest { private RedisRegistry redisRegistry; private URL registryUrl; - @BeforeEach + @Before public void setUp() throws Exception { int redisPort = NetUtils.getAvailablePort(); this.redisServer = new RedisServer(redisPort); @@ -53,7 +51,7 @@ public void setUp() throws Exception { redisRegistry = (RedisRegistry) new RedisRegistryFactory().createRegistry(registryUrl); } - @AfterEach + @After public void tearDown() throws Exception { this.redisServer.stop(); } @@ -72,12 +70,10 @@ public void testRegister() { assertThat(registered.size(), is(1)); } - @Test + @Test(expected = IllegalStateException.class) public void testAnyHost() { - Assertions.assertThrows(IllegalStateException.class, () -> { - URL errorUrl = URL.valueOf("multicast://0.0.0.0/"); - new RedisRegistryFactory().createRegistry(errorUrl); - }); + URL errorUrl = URL.valueOf("multicast://0.0.0.0/"); + new RedisRegistryFactory().createRegistry(errorUrl); } diff --git a/dubbo-registry/dubbo-registry-zookeeper/pom.xml b/dubbo-registry/dubbo-registry-zookeeper/pom.xml index 2c565d8b1a8..edadb7a9c99 100644 --- a/dubbo-registry/dubbo-registry-zookeeper/pom.xml +++ b/dubbo-registry/dubbo-registry-zookeeper/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-registry - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-registry-zookeeper jar diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java index 93f584aac22..d7a8ee097ac 100644 --- a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java +++ b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java @@ -18,10 +18,8 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.ConcurrentHashSet; import org.apache.dubbo.common.utils.UrlUtils; import org.apache.dubbo.registry.NotifyListener; @@ -52,7 +50,7 @@ public class ZookeeperRegistry extends FailbackRegistry { private final String root; - private final Set anyServices = new ConcurrentHashSet<>(); + private final Set anyServices = new ConcurrentHashSet(); private final ConcurrentMap> zkListeners = new ConcurrentHashMap>(); @@ -69,17 +67,32 @@ public ZookeeperRegistry(URL url, ZookeeperTransporter zookeeperTransporter) { } this.root = group; zkClient = zookeeperTransporter.connect(url); - zkClient.addStateListener(state -> { - if (state == StateListener.RECONNECTED) { - try { - recover(); - } catch (Exception e) { - logger.error(e.getMessage(), e); + zkClient.addStateListener(new StateListener() { + @Override + public void stateChanged(int state) { + if (state == RECONNECTED) { + try { + recover(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + } } } }); } + static String appendDefaultPort(String address) { + if (address != null && address.length() > 0) { + int i = address.indexOf(':'); + if (i < 0) { + return address + ":" + DEFAULT_ZOOKEEPER_PORT; + } else if (Integer.parseInt(address.substring(i + 1)) == 0) { + return address.substring(0, i + 1) + DEFAULT_ZOOKEEPER_PORT; + } + } + return address; + } + @Override public boolean isAvailable() { return zkClient.isConnected(); @@ -120,18 +133,21 @@ public void doSubscribe(final URL url, final NotifyListener listener) { String root = toRootPath(); ConcurrentMap listeners = zkListeners.get(url); if (listeners == null) { - zkListeners.putIfAbsent(url, new ConcurrentHashMap<>()); + zkListeners.putIfAbsent(url, new ConcurrentHashMap()); listeners = zkListeners.get(url); } ChildListener zkListener = listeners.get(listener); if (zkListener == null) { - listeners.putIfAbsent(listener, (parentPath, currentChilds) -> { - for (String child : currentChilds) { - child = URL.decode(child); - if (!anyServices.contains(child)) { - anyServices.add(child); - subscribe(url.setPath(child).addParameters(Constants.INTERFACE_KEY, child, - Constants.CHECK_KEY, String.valueOf(false)), listener); + listeners.putIfAbsent(listener, new ChildListener() { + @Override + public void childChanged(String parentPath, List currentChilds) { + for (String child : currentChilds) { + child = URL.decode(child); + if (!anyServices.contains(child)) { + anyServices.add(child); + subscribe(url.setPath(child).addParameters(Constants.INTERFACE_KEY, child, + Constants.CHECK_KEY, String.valueOf(false)), listener); + } } } }); @@ -139,7 +155,7 @@ public void doSubscribe(final URL url, final NotifyListener listener) { } zkClient.create(root, false); List services = zkClient.addChildListener(root, zkListener); - if (CollectionUtils.isNotEmpty(services)) { + if (services != null && !services.isEmpty()) { for (String service : services) { service = URL.decode(service); anyServices.add(service); @@ -148,16 +164,21 @@ public void doSubscribe(final URL url, final NotifyListener listener) { } } } else { - List urls = new ArrayList<>(); + List urls = new ArrayList(); for (String path : toCategoriesPath(url)) { ConcurrentMap listeners = zkListeners.get(url); if (listeners == null) { - zkListeners.putIfAbsent(url, new ConcurrentHashMap<>()); + zkListeners.putIfAbsent(url, new ConcurrentHashMap()); listeners = zkListeners.get(url); } ChildListener zkListener = listeners.get(listener); if (zkListener == null) { - listeners.putIfAbsent(listener, (parentPath, currentChilds) -> ZookeeperRegistry.this.notify(url, listener, toUrlsWithEmpty(url, parentPath, currentChilds))); + listeners.putIfAbsent(listener, new ChildListener() { + @Override + public void childChanged(String parentPath, List currentChilds) { + ZookeeperRegistry.this.notify(url, listener, toUrlsWithEmpty(url, parentPath, currentChilds)); + } + }); zkListener = listeners.get(listener); } zkClient.create(path, false); @@ -197,7 +218,7 @@ public List lookup(URL url) { throw new IllegalArgumentException("lookup url == null"); } try { - List providers = new ArrayList<>(); + List providers = new ArrayList(); for (String path : toCategoriesPath(url)) { List children = zkClient.getChildren(path); if (children != null) { @@ -253,8 +274,8 @@ private String toUrlPath(URL url) { } private List toUrlsWithoutEmpty(URL consumer, List providers) { - List urls = new ArrayList<>(); - if (CollectionUtils.isNotEmpty(providers)) { + List urls = new ArrayList(); + if (providers != null && !providers.isEmpty()) { for (String provider : providers) { provider = URL.decode(provider); if (provider.contains(Constants.PROTOCOL_SEPARATOR)) { @@ -273,10 +294,7 @@ private List toUrlsWithEmpty(URL consumer, String path, List provid if (urls == null || urls.isEmpty()) { int i = path.lastIndexOf(Constants.PATH_SEPARATOR); String category = i < 0 ? path : path.substring(i + 1); - URL empty = URLBuilder.from(consumer) - .setProtocol(Constants.EMPTY_PROTOCOL) - .addParameter(Constants.CATEGORY_KEY, category) - .build(); + URL empty = consumer.setProtocol(Constants.EMPTY_PROTOCOL).addParameter(Constants.CATEGORY_KEY, category); urls.add(empty); } return urls; diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistryFactory.java b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistryFactory.java index d702d0184cb..6fd33431b56 100644 --- a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistryFactory.java +++ b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistryFactory.java @@ -29,10 +29,6 @@ public class ZookeeperRegistryFactory extends AbstractRegistryFactory { private ZookeeperTransporter zookeeperTransporter; - /** - * Invisible injection of zookeeper client via IOC/SPI - * @param zookeeperTransporter - */ public void setZookeeperTransporter(ZookeeperTransporter zookeeperTransporter) { this.zookeeperTransporter = zookeeperTransporter; } diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/test/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistryTest.java b/dubbo-registry/dubbo-registry-zookeeper/src/test/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistryTest.java index c2dd23129d6..54fbdaafeb3 100644 --- a/dubbo-registry/dubbo-registry-zookeeper/src/test/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistryTest.java +++ b/dubbo-registry/dubbo-registry-zookeeper/src/test/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistryTest.java @@ -23,23 +23,20 @@ import org.apache.dubbo.registry.Registry; import org.apache.dubbo.registry.status.RegistryStatusChecker; import org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperTransporter; - import org.apache.curator.test.TestingServer; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.CountDownLatch; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.CoreMatchers.not; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; public class ZookeeperRegistryTest { @@ -51,7 +48,7 @@ public class ZookeeperRegistryTest { private URL registryUrl; private ZookeeperRegistryFactory zookeeperRegistryFactory; - @BeforeEach + @Before public void setUp() throws Exception { int zkServerPort = NetUtils.getAvailablePort(); this.zkServer = new TestingServer(zkServerPort, true); @@ -62,17 +59,21 @@ public void setUp() throws Exception { this.zookeeperRegistry = (ZookeeperRegistry) zookeeperRegistryFactory.createRegistry(registryUrl); } - @AfterEach + @After public void tearDown() throws Exception { zkServer.stop(); } @Test + public void testDefaultPort() { + Assert.assertEquals("10.20.153.10:2181", ZookeeperRegistry.appendDefaultPort("10.20.153.10:0")); + Assert.assertEquals("10.20.153.10:2181", ZookeeperRegistry.appendDefaultPort("10.20.153.10")); + } + + @Test(expected = IllegalStateException.class) public void testAnyHost() { - Assertions.assertThrows(IllegalStateException.class, () -> { - URL errorUrl = URL.valueOf("multicast://0.0.0.0/"); - new ZookeeperRegistryFactory().createRegistry(errorUrl); - }); + URL errorUrl = URL.valueOf("multicast://0.0.0.0/"); + new ZookeeperRegistryFactory().createRegistry(errorUrl); } @Test @@ -123,7 +124,7 @@ public void testLookup() { assertThat(lookup.size(), is(1)); } - @Disabled + @Ignore @Test /* This UT is unstable, consider remove it later. @@ -149,8 +150,13 @@ public void testStatusChecker() { public void testSubscribeAnyValue() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); zookeeperRegistry.register(serviceUrl); - zookeeperRegistry.subscribe(anyUrl, urls -> latch.countDown()); + zookeeperRegistry.subscribe(anyUrl, new NotifyListener() { + @Override + public void notify(List urls) { + latch.countDown(); + } + }); zookeeperRegistry.register(serviceUrl); latch.await(); } -} +} \ No newline at end of file diff --git a/dubbo-registry/pom.xml b/dubbo-registry/pom.xml index 33a329bb0fd..822aec8a078 100644 --- a/dubbo-registry/pom.xml +++ b/dubbo-registry/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-parent - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-registry pom @@ -34,7 +34,5 @@ dubbo-registry-multicast dubbo-registry-zookeeper dubbo-registry-redis - dubbo-registry-consul - dubbo-registry-etcd3 diff --git a/dubbo-remoting/dubbo-remoting-api/pom.xml b/dubbo-remoting/dubbo-remoting-api/pom.xml index ec278e96e02..629a4da2cb2 100644 --- a/dubbo-remoting/dubbo-remoting-api/pom.xml +++ b/dubbo-remoting/dubbo-remoting-api/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-remoting - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-remoting-api jar diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/Client.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/Client.java index 7f15535353c..b8ccad201c8 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/Client.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/Client.java @@ -25,7 +25,7 @@ * * @see org.apache.dubbo.remoting.Transporter#connect(org.apache.dubbo.common.URL, ChannelHandler) */ -public interface Client extends Endpoint, Channel, Resetable, IdleSensible { +public interface Client extends Endpoint, Channel, Resetable { /** * reconnect. @@ -35,4 +35,4 @@ public interface Client extends Endpoint, Channel, Resetable, IdleSensible { @Deprecated void reset(org.apache.dubbo.common.Parameters parameters); -} +} \ No newline at end of file diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/IdleSensible.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/IdleSensible.java deleted file mode 100644 index 14e371e60d6..00000000000 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/IdleSensible.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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 org.apache.dubbo.remoting; - -/** - * Indicate whether the implementation (for both server and client) has the ability to sense and handle idle connection. - * If the server has the ability to handle idle connection, it should close the connection when it happens, and if - * the client has the ability to handle idle connection, it should send the heartbeat to the server. - */ -public interface IdleSensible { - /** - * Whether the implementation can sense and handle the idle connection. By default it's false, the implementation - * relies on dedicated timer to take care of idle connection. - * - * @return whether has the ability to handle idle connection - */ - default boolean canHandleIdle() { - return false; - } -} diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/Server.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/Server.java index c6ecf16d44b..f413cb0bb85 100755 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/Server.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/Server.java @@ -28,7 +28,7 @@ * * @see org.apache.dubbo.remoting.Transporter#bind(org.apache.dubbo.common.URL, ChannelHandler) */ -public interface Server extends Endpoint, Resetable, IdleSensible { +public interface Server extends Endpoint, Resetable { /** * is bound. @@ -55,4 +55,4 @@ public interface Server extends Endpoint, Resetable, IdleSensible { @Deprecated void reset(org.apache.dubbo.common.Parameters parameters); -} +} \ No newline at end of file diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/Request.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/Request.java index e5d233aa9ce..abd058294b3 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/Request.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/Request.java @@ -95,12 +95,8 @@ public boolean isEvent() { } public void setEvent(String event) { - this.mEvent = true; - this.mData = event; - } - - public void setEvent(boolean mEvent) { - this.mEvent = mEvent; + mEvent = true; + mData = event; } public boolean isBroken() { diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/Response.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/Response.java index 568ecf1160b..d1ed88b1251 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/Response.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/Response.java @@ -137,10 +137,6 @@ public void setEvent(String event) { mResult = event; } - public void setEvent(boolean mEvent) { - this.mEvent = mEvent; - } - public boolean isHeartbeat() { return mEvent && HEARTBEAT_EVENT == mResult; } diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/codec/ExchangeCodec.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/codec/ExchangeCodec.java index 8f25250c98e..4031688e04e 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/codec/ExchangeCodec.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/codec/ExchangeCodec.java @@ -43,6 +43,9 @@ /** * ExchangeCodec. + * + * + * */ public class ExchangeCodec extends TelnetCodec { @@ -142,7 +145,7 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro // decode response. Response res = new Response(id); if ((flag & FLAG_EVENT) != 0) { - res.setEvent(true); + res.setEvent(Response.HEARTBEAT_EVENT); } // get status. byte status = header[3]; @@ -173,7 +176,7 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro req.setVersion(Version.getProtocolVersion()); req.setTwoWay((flag & FLAG_TWOWAY) != 0); if ((flag & FLAG_EVENT) != 0) { - req.setEvent(true); + req.setEvent(Request.HEARTBEAT_EVENT); } try { ObjectInput in = CodecSupport.deserialize(channel.getUrl(), is, proto); diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java index 290d668241a..f79d99df504 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java @@ -126,7 +126,7 @@ public static void sent(Channel channel, Request request) { * @param channel channel to close */ public static void closeChannel(Channel channel) { - for (Map.Entry entry : CHANNELS.entrySet()) { + for (Map.Entry entry: CHANNELS.entrySet()) { if (channel.equals(entry.getValue())) { DefaultFuture future = getFuture(entry.getKey()); if (future != null && !future.isDone()) { @@ -255,6 +255,7 @@ private void invokeCallback(ResponseCallback c) { if (callbackCopy == null) { throw new NullPointerException("callback cannot be null."); } + c = null; Response res = response; if (res == null) { throw new IllegalStateException("response cannot be null. url:" + channel.getUrl()); @@ -329,7 +330,9 @@ private void doReceived(Response res) { lock.lock(); try { response = res; - done.signalAll(); + if (done != null) { + done.signal(); + } } finally { lock.unlock(); } diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/AbstractTimerTask.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/AbstractTimerTask.java index befa6887806..003af243d86 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/AbstractTimerTask.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/AbstractTimerTask.java @@ -34,8 +34,6 @@ public abstract class AbstractTimerTask implements TimerTask { private final Long tick; - protected volatile boolean cancel = false; - AbstractTimerTask(ChannelProvider channelProvider, Long tick) { if (channelProvider == null || tick == null) { throw new IllegalArgumentException(); @@ -56,19 +54,11 @@ static Long now() { return System.currentTimeMillis(); } - public void cancel() { - this.cancel = true; - } - private void reput(Timeout timeout, Long tick) { if (timeout == null || tick == null) { throw new IllegalArgumentException(); } - if (cancel) { - return; - } - Timer timer = timeout.timer(); if (timer.isStop() || timeout.isCancelled()) { return; diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/CloseTimerTask.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/CloseTimerTask.java deleted file mode 100644 index 4081c309d05..00000000000 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/CloseTimerTask.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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 org.apache.dubbo.remoting.exchange.support.header; - -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.remoting.Channel; - -/** - * CloseTimerTask - */ -public class CloseTimerTask extends AbstractTimerTask { - - private static final Logger logger = LoggerFactory.getLogger(CloseTimerTask.class); - - private final int idleTimeout; - - public CloseTimerTask(ChannelProvider channelProvider, Long heartbeatTimeoutTick, int idleTimeout) { - super(channelProvider, heartbeatTimeoutTick); - this.idleTimeout = idleTimeout; - } - - @Override - protected void doTask(Channel channel) { - try { - Long lastRead = lastRead(channel); - Long lastWrite = lastWrite(channel); - Long now = now(); - // check ping & pong at server - if ((lastRead != null && now - lastRead > idleTimeout) - || (lastWrite != null && now - lastWrite > idleTimeout)) { - logger.warn("Close channel " + channel + ", because idleCheck timeout: " - + idleTimeout + "ms"); - channel.close(); - } - } catch (Throwable t) { - logger.warn("Exception when close remote channel " + channel.getRemoteAddress(), t); - } - } -} diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeClient.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeClient.java index 3e57fba90c4..3abbe5b5422 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeClient.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeClient.java @@ -19,7 +19,6 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.timer.HashedWheelTimer; -import org.apache.dubbo.common.utils.Assert; import org.apache.dubbo.common.utils.NamedThreadFactory; import org.apache.dubbo.remoting.ChannelHandler; import org.apache.dubbo.remoting.Client; @@ -33,9 +32,6 @@ import java.util.Collections; import java.util.concurrent.TimeUnit; -import static org.apache.dubbo.common.utils.UrlUtils.getHeartbeat; -import static org.apache.dubbo.common.utils.UrlUtils.getIdleTimeout; - /** * DefaultMessageClient */ @@ -43,21 +39,32 @@ public class HeaderExchangeClient implements ExchangeClient { private final Client client; private final ExchangeChannel channel; + // heartbeat(ms), default value is 0 , won't execute a heartbeat. + private int heartbeat; + private int heartbeatTimeout; - private static final HashedWheelTimer IDLE_CHECK_TIMER = new HashedWheelTimer( - new NamedThreadFactory("dubbo-client-idleCheck", true), 1, TimeUnit.SECONDS, Constants.TICKS_PER_WHEEL); - private HeartbeatTimerTask heartBeatTimerTask; - private ReconnectTimerTask reconnectTimerTask; + private HashedWheelTimer heartbeatTimer; - public HeaderExchangeClient(Client client, boolean startTimer) { - Assert.notNull(client, "Client can't be null"); + public HeaderExchangeClient(Client client, boolean needHeartbeat) { + if (client == null) { + throw new IllegalArgumentException("client == null"); + } this.client = client; this.channel = new HeaderExchangeChannel(client); + String dubbo = client.getUrl().getParameter(Constants.DUBBO_VERSION_KEY); + + this.heartbeat = client.getUrl().getParameter(Constants.HEARTBEAT_KEY, dubbo != null && + dubbo.startsWith("1.0.") ? Constants.DEFAULT_HEARTBEAT : 0); + this.heartbeatTimeout = client.getUrl().getParameter(Constants.HEARTBEAT_TIMEOUT_KEY, heartbeat * 3); + if (heartbeatTimeout < heartbeat * 2) { + throw new IllegalStateException("heartbeatTimeout < heartbeatInterval * 2"); + } - if (startTimer) { - URL url = client.getUrl(); - startReconnectTask(url); - startHeartBeatTask(url); + if (needHeartbeat) { + long tickDuration = calculateLeastDuration(heartbeat); + heartbeatTimer = new HashedWheelTimer(new NamedThreadFactory("dubbo-client-heartbeat", true), tickDuration, + TimeUnit.MILLISECONDS, Constants.TICKS_PER_WHEEL); + startHeartbeatTimer(); } } @@ -138,7 +145,6 @@ public void startClose() { @Override public void reset(URL url) { client.reset(url); - // FIXME, should cancel and restart timer tasks if parameters in the new URL are different? } @Override @@ -172,34 +178,28 @@ public boolean hasAttribute(String key) { return channel.hasAttribute(key); } - private void startHeartBeatTask(URL url) { - if (!client.canHandleIdle()) { - AbstractTimerTask.ChannelProvider cp = () -> Collections.singletonList(HeaderExchangeClient.this); - int heartbeat = getHeartbeat(url); - long heartbeatTick = calculateLeastDuration(heartbeat); - this.heartBeatTimerTask = new HeartbeatTimerTask(cp, heartbeatTick, heartbeat); - IDLE_CHECK_TIMER.newTimeout(heartBeatTimerTask, heartbeatTick, TimeUnit.MILLISECONDS); - } + private void startHeartbeatTimer() { + AbstractTimerTask.ChannelProvider cp = () -> Collections.singletonList(HeaderExchangeClient.this); + + long heartbeatTick = calculateLeastDuration(heartbeat); + long heartbeatTimeoutTick = calculateLeastDuration(heartbeatTimeout); + HeartbeatTimerTask heartBeatTimerTask = new HeartbeatTimerTask(cp, heartbeatTick, heartbeat); + ReconnectTimerTask reconnectTimerTask = new ReconnectTimerTask(cp, heartbeatTimeoutTick, heartbeatTimeout); + + // init task and start timer. + heartbeatTimer.newTimeout(heartBeatTimerTask, heartbeatTick, TimeUnit.MILLISECONDS); + heartbeatTimer.newTimeout(reconnectTimerTask, heartbeatTimeoutTick, TimeUnit.MILLISECONDS); } - private void startReconnectTask(URL url) { - if (shouldReconnect(url)) { - AbstractTimerTask.ChannelProvider cp = () -> Collections.singletonList(HeaderExchangeClient.this); - int idleTimeout = getIdleTimeout(url); - long heartbeatTimeoutTick = calculateLeastDuration(idleTimeout); - this.reconnectTimerTask = new ReconnectTimerTask(cp, heartbeatTimeoutTick, idleTimeout); - IDLE_CHECK_TIMER.newTimeout(reconnectTimerTask, heartbeatTimeoutTick, TimeUnit.MILLISECONDS); + private void stopHeartbeatTimer() { + if (heartbeatTimer != null) { + heartbeatTimer.stop(); + heartbeatTimer = null; } } private void doClose() { - if (heartBeatTimerTask != null) { - heartBeatTimerTask.cancel(); - } - - if (reconnectTimerTask != null) { - reconnectTimerTask.cancel(); - } + stopHeartbeatTimer(); } /** @@ -213,10 +213,6 @@ private long calculateLeastDuration(int time) { } } - private boolean shouldReconnect(URL url) { - return url.getParameter(Constants.RECONNECT_KEY, true); - } - @Override public String toString() { return "HeaderExchangeClient [channel=" + channel + "]"; diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeServer.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeServer.java index 8a74e234134..7f3067d60cc 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeServer.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeServer.java @@ -22,10 +22,7 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.timer.HashedWheelTimer; -import org.apache.dubbo.common.utils.Assert; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.NamedThreadFactory; -import org.apache.dubbo.common.utils.UrlUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.ChannelHandler; import org.apache.dubbo.remoting.RemotingException; @@ -50,17 +47,25 @@ public class HeaderExchangeServer implements ExchangeServer { protected final Logger logger = LoggerFactory.getLogger(getClass()); private final Server server; + // heartbeat timeout (ms), default value is 0 , won't execute a heartbeat. + private int heartbeat; + private int heartbeatTimeout; private AtomicBoolean closed = new AtomicBoolean(false); - private static final HashedWheelTimer IDLE_CHECK_TIMER = new HashedWheelTimer(new NamedThreadFactory("dubbo-server-idleCheck", true), 1, - TimeUnit.SECONDS, Constants.TICKS_PER_WHEEL); - - private CloseTimerTask closeTimerTask; + private HashedWheelTimer heartbeatTimer; public HeaderExchangeServer(Server server) { - Assert.notNull(server, "server == null"); + if (server == null) { + throw new IllegalArgumentException("server == null"); + } this.server = server; - startIdleCheckTask(getUrl()); + this.heartbeat = server.getUrl().getParameter(Constants.HEARTBEAT_KEY, 0); + this.heartbeatTimeout = server.getUrl().getParameter(Constants.HEARTBEAT_TIMEOUT_KEY, heartbeat * 3); + if (heartbeatTimeout < heartbeat * 2) { + throw new IllegalStateException("heartbeatTimeout < heartbeatInterval * 2"); + } + + startHeartbeatTimer(); } public Server getServer() { @@ -143,20 +148,14 @@ private void doClose() { if (!closed.compareAndSet(false, true)) { return; } - cancelCloseTask(); - } - - private void cancelCloseTask() { - if (closeTimerTask != null) { - closeTimerTask.cancel(); - } + stopHeartbeatTimer(); } @Override public Collection getExchangeChannels() { Collection exchangeChannels = new ArrayList(); Collection channels = server.getChannels(); - if (CollectionUtils.isNotEmpty(channels)) { + if (channels != null && !channels.isEmpty()) { for (Channel channel : channels) { exchangeChannels.add(HeaderExchangeChannel.getOrAddChannel(channel)); } @@ -205,13 +204,20 @@ public ChannelHandler getChannelHandler() { public void reset(URL url) { server.reset(url); try { - int currHeartbeat = UrlUtils.getHeartbeat(getUrl()); - int currIdleTimeout = UrlUtils.getIdleTimeout(getUrl()); - int heartbeat = UrlUtils.getHeartbeat(url); - int idleTimeout = UrlUtils.getIdleTimeout(url); - if (currHeartbeat != heartbeat || currIdleTimeout != idleTimeout) { - cancelCloseTask(); - startIdleCheckTask(url); + if (url.hasParameter(Constants.HEARTBEAT_KEY) + || url.hasParameter(Constants.HEARTBEAT_TIMEOUT_KEY)) { + int h = url.getParameter(Constants.HEARTBEAT_KEY, heartbeat); + int t = url.getParameter(Constants.HEARTBEAT_TIMEOUT_KEY, h * 3); + if (t < h * 2) { + throw new IllegalStateException("heartbeatTimeout < heartbeatInterval * 2"); + } + if (h != heartbeat || t != heartbeatTimeout) { + heartbeat = h; + heartbeatTimeout = t; + + stopHeartbeatTimer(); + startHeartbeatTimer(); + } } } catch (Throwable t) { logger.error(t.getMessage(), t); @@ -253,16 +259,28 @@ private long calculateLeastDuration(int time) { } } - private void startIdleCheckTask(URL url) { - if (!server.canHandleIdle()) { - AbstractTimerTask.ChannelProvider cp = () -> unmodifiableCollection(HeaderExchangeServer.this.getChannels()); - int idleTimeout = UrlUtils.getIdleTimeout(url); - long idleTimeoutTick = calculateLeastDuration(idleTimeout); - CloseTimerTask closeTimerTask = new CloseTimerTask(cp, idleTimeoutTick, idleTimeout); - this.closeTimerTask = closeTimerTask; + private void startHeartbeatTimer() { + long tickDuration = calculateLeastDuration(heartbeat); + heartbeatTimer = new HashedWheelTimer(new NamedThreadFactory("dubbo-server-heartbeat", true), tickDuration, + TimeUnit.MILLISECONDS, Constants.TICKS_PER_WHEEL); + + AbstractTimerTask.ChannelProvider cp = () -> unmodifiableCollection(HeaderExchangeServer.this.getChannels()); + + long heartbeatTick = calculateLeastDuration(heartbeat); + long heartbeatTimeoutTick = calculateLeastDuration(heartbeatTimeout); + HeartbeatTimerTask heartBeatTimerTask = new HeartbeatTimerTask(cp, heartbeatTick, heartbeat); + ReconnectTimerTask reconnectTimerTask = new ReconnectTimerTask(cp, heartbeatTimeoutTick, heartbeatTimeout); - // init task and start timer. - IDLE_CHECK_TIMER.newTimeout(closeTimerTask, idleTimeoutTick, TimeUnit.MILLISECONDS); + // init task and start timer. + heartbeatTimer.newTimeout(heartBeatTimerTask, heartbeatTick, TimeUnit.MILLISECONDS); + heartbeatTimer.newTimeout(reconnectTimerTask, heartbeatTimeoutTick, TimeUnit.MILLISECONDS); + } + + private void stopHeartbeatTimer() { + if (heartbeatTimer != null) { + heartbeatTimer.stop(); + heartbeatTimer = null; } } + } diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/ReconnectTimerTask.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/ReconnectTimerTask.java index 5fd8c90b956..dccbe5332cd 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/ReconnectTimerTask.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/ReconnectTimerTask.java @@ -29,11 +29,11 @@ public class ReconnectTimerTask extends AbstractTimerTask { private static final Logger logger = LoggerFactory.getLogger(ReconnectTimerTask.class); - private final int idleTimeout; + private final int heartbeatTimeout; - public ReconnectTimerTask(ChannelProvider channelProvider, Long heartbeatTimeoutTick, int idleTimeout) { + ReconnectTimerTask(ChannelProvider channelProvider, Long heartbeatTimeoutTick, int heartbeatTimeout1) { super(channelProvider, heartbeatTimeoutTick); - this.idleTimeout = idleTimeout; + this.heartbeatTimeout = heartbeatTimeout1; } @Override @@ -41,23 +41,17 @@ protected void doTask(Channel channel) { try { Long lastRead = lastRead(channel); Long now = now(); - - // Rely on reconnect timer to reconnect when AbstractClient.doConnect fails to init the connection - if (!channel.isConnected()) { - try { - logger.info("Initial connection to " + channel); - ((Client) channel).reconnect(); - } catch (Exception e) { - logger.error("Fail to connect to " + channel, e); - } - // check pong at client - } else if (lastRead != null && now - lastRead > idleTimeout) { - logger.warn("Reconnect to channel " + channel + ", because heartbeat read idle time out: " - + idleTimeout + "ms"); - try { - ((Client) channel).reconnect(); - } catch (Exception e) { - logger.error(channel + "reconnect failed during idle time.", e); + if (lastRead != null && now - lastRead > heartbeatTimeout) { + logger.warn("Close channel " + channel + ", because heartbeat read idle time out: " + + heartbeatTimeout + "ms"); + if (channel instanceof Client) { + try { + ((Client) channel).reconnect(); + } catch (Exception e) { + //do nothing + } + } else { + channel.close(); } } } catch (Throwable t) { diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/codec/TelnetCodec.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/codec/TelnetCodec.java index ce481fc8519..7b4f1ca5db9 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/codec/TelnetCodec.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/codec/TelnetCodec.java @@ -20,7 +20,6 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.RemotingException; @@ -49,14 +48,9 @@ public class TelnetCodec extends TransportCodec { private static final byte[] DOWN = new byte[]{27, 91, 66}; - private static final List ENTER = Arrays.asList( - new byte[]{'\r', '\n'} /* Windows Enter */, - new byte[]{'\n'} /* Linux Enter */); + private static final List ENTER = Arrays.asList(new Object[]{new byte[]{'\r', '\n'} /* Windows Enter */, new byte[]{'\n'} /* Linux Enter */}); - private static final List EXIT = Arrays.asList( - new byte[]{3} /* Windows Ctrl+C */, - new byte[]{-1, -12, -1, -3, 6} /* Linux Ctrl+C */, - new byte[]{-1, -19, -1, -3, 6} /* Linux Pause */); + private static final List EXIT = Arrays.asList(new Object[]{new byte[]{3} /* Windows Ctrl+C */, new byte[]{-1, -12, -1, -3, 6} /* Linux Ctrl+C */, new byte[]{-1, -19, -1, -3, 6} /* Linux Pause */}); private static Charset getCharset(Channel channel) { if (channel != null) { @@ -73,7 +67,7 @@ private static Charset getCharset(Channel channel) { URL url = channel.getUrl(); if (url != null) { String parameter = url.getParameter(Constants.CHARSET_KEY); - if (StringUtils.isNotEmpty(parameter)) { + if (parameter != null && parameter.length() > 0) { try { return Charset.forName(parameter); } catch (Throwable t) { @@ -197,7 +191,7 @@ protected Object decode(Channel channel, ChannelBuffer buffer, int readable, byt boolean down = endsWith(message, DOWN); if (up || down) { LinkedList history = (LinkedList) channel.getAttribute(HISTORY_LIST_KEY); - if (CollectionUtils.isEmpty(history)) { + if (history == null || history.isEmpty()) { return DecodeResult.NEED_MORE_INPUT; } Integer index = (Integer) channel.getAttribute(HISTORY_INDEX_KEY); @@ -264,7 +258,7 @@ protected Object decode(Channel channel, ChannelBuffer buffer, int readable, byt LinkedList history = (LinkedList) channel.getAttribute(HISTORY_LIST_KEY); Integer index = (Integer) channel.getAttribute(HISTORY_INDEX_KEY); channel.removeAttribute(HISTORY_INDEX_KEY); - if (CollectionUtils.isNotEmpty(history) && index != null && index >= 0 && index < history.size()) { + if (history != null && !history.isEmpty() && index != null && index >= 0 && index < history.size()) { String value = history.get(index); if (value != null) { byte[] b1 = value.getBytes(); diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/TelnetHandlerAdapter.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/TelnetHandlerAdapter.java index 66418f3b573..a5ceda82f88 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/TelnetHandlerAdapter.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/TelnetHandlerAdapter.java @@ -74,24 +74,27 @@ public String telnet(Channel channel, String message) throws RemotingException { if (buf.length() > 0) { buf.append("\r\n"); } - if (StringUtils.isNotEmpty(prompt) && !noprompt) { + if (prompt != null && prompt.length() > 0 && !noprompt) { buf.append(prompt); } return buf.toString(); } private boolean commandEnabled(URL url, String command) { + boolean commandEnable = false; String supportCommands = url.getParameter(Constants.TELNET); if (StringUtils.isEmpty(supportCommands)) { - return true; - } - String[] commands = Constants.COMMA_SPLIT_PATTERN.split(supportCommands); - for (String c : commands) { - if (command.equals(c)) { - return true; + commandEnable = true; + } else { + String[] commands = Constants.COMMA_SPLIT_PATTERN.split(supportCommands); + for (String c : commands) { + if (command.equals(c)) { + commandEnable = true; + break; + } } } - return false; + return commandEnable; } } diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/HelpTelnetHandler.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/HelpTelnetHandler.java index b22cd1ac667..84325918f81 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/HelpTelnetHandler.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/HelpTelnetHandler.java @@ -18,7 +18,6 @@ import org.apache.dubbo.common.extension.Activate; import org.apache.dubbo.common.extension.ExtensionLoader; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.telnet.TelnetHandler; import org.apache.dubbo.remoting.telnet.support.Help; @@ -55,7 +54,7 @@ public String telnet(Channel channel, String message) { } else { List> table = new ArrayList>(); List handlers = extensionLoader.getActivateExtension(channel.getUrl(), "telnet"); - if (CollectionUtils.isNotEmpty(handlers)) { + if (handlers != null && !handlers.isEmpty()) { for (TelnetHandler handler : handlers) { Help help = handler.getClass().getAnnotation(Help.class); List row = new ArrayList(); diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/LogTelnetHandler.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/LogTelnetHandler.java index db531dc626f..bdbdebf6cfa 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/LogTelnetHandler.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/LogTelnetHandler.java @@ -56,8 +56,10 @@ public String telnet(Channel channel, String message) { if (file != null && file.exists()) { try { - try (FileInputStream fis = new FileInputStream(file)) { - try (FileChannel filechannel = fis.getChannel()) { + FileInputStream fis = new FileInputStream(file); + try { + FileChannel filechannel = fis.getChannel(); + try { size = filechannel.size(); ByteBuffer bb; if (size <= showLogLength) { @@ -76,7 +78,11 @@ public String telnet(Channel channel, String message) { buf.append("\r\nmodified:" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") .format(new Date(file.lastModified())))); buf.append("\r\nsize:" + size + "\r\n"); + } finally { + filechannel.close(); } + } finally { + fis.close(); } } catch (Exception e) { buf.append(e.getMessage()); diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/StatusTelnetHandler.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/StatusTelnetHandler.java index 69611f60a0e..6f22da21daf 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/StatusTelnetHandler.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/StatusTelnetHandler.java @@ -22,7 +22,6 @@ import org.apache.dubbo.common.status.Status; import org.apache.dubbo.common.status.StatusChecker; import org.apache.dubbo.common.status.support.StatusUtils; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.telnet.TelnetHandler; import org.apache.dubbo.remoting.telnet.support.Help; @@ -49,7 +48,7 @@ public String telnet(Channel channel, String message) { String[] header = new String[]{"resource", "status", "message"}; List> table = new ArrayList>(); Map statuses = new HashMap(); - if (CollectionUtils.isNotEmpty(checkers)) { + if (checkers != null && !checkers.isEmpty()) { for (StatusChecker checker : checkers) { String name = extensionLoader.getExtensionName(checker); Status stat; @@ -80,7 +79,7 @@ public String telnet(Channel channel, String message) { } String status = channel.getUrl().getParameter("status"); Map statuses = new HashMap(); - if (CollectionUtils.isNotEmptyMap(statuses)) { + if (status != null && status.length() > 0) { String[] ss = Constants.COMMA_SPLIT_PATTERN.split(status); for (String s : ss) { StatusChecker handler = extensionLoader.getExtension(s); diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java index 2afdc4d0f02..ed913d2a0f7 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java @@ -24,6 +24,7 @@ import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.store.DataStore; import org.apache.dubbo.common.utils.ExecutorUtil; +import org.apache.dubbo.common.utils.NamedThreadFactory; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.ChannelHandler; @@ -33,6 +34,12 @@ import java.net.InetSocketAddress; import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -43,14 +50,31 @@ public abstract class AbstractClient extends AbstractEndpoint implements Client protected static final String CLIENT_THREAD_POOL_NAME = "DubboClientHandler"; private static final Logger logger = LoggerFactory.getLogger(AbstractClient.class); + private static final AtomicInteger CLIENT_THREAD_POOL_ID = new AtomicInteger(); + private static final ScheduledThreadPoolExecutor reconnectExecutorService = new ScheduledThreadPoolExecutor(2, new NamedThreadFactory("DubboClientReconnectTimer", true)); private final Lock connectLock = new ReentrantLock(); - private final boolean needReconnect; + private final boolean send_reconnect; + private final AtomicInteger reconnect_count = new AtomicInteger(0); + // Reconnection error log has been called before? + private final AtomicBoolean reconnect_error_log_flag = new AtomicBoolean(false); + // reconnect warning period. Reconnect warning interval (log warning after how many times) //for test + private final int reconnect_warning_period; + private final long shutdown_timeout; protected volatile ExecutorService executor; + private volatile ScheduledFuture reconnectExecutorFuture = null; + // the last successed connected time + private long lastConnectedTime = System.currentTimeMillis(); + public AbstractClient(URL url, ChannelHandler handler) throws RemotingException { super(url, handler); - needReconnect = url.getParameter(Constants.SEND_RECONNECT_KEY, false); + send_reconnect = url.getParameter(Constants.SEND_RECONNECT_KEY, false); + + shutdown_timeout = url.getParameter(Constants.SHUTDOWN_TIMEOUT_KEY, Constants.DEFAULT_SHUTDOWN_TIMEOUT); + + // The default reconnection interval is 2s, 1800 means warning interval is 1 hour. + reconnect_warning_period = url.getParameter("reconnect.waring.period", 1800); try { doOpen(); @@ -93,6 +117,81 @@ protected static ChannelHandler wrapChannelHandler(URL url, ChannelHandler handl return ChannelHandlers.wrap(handler, url); } + /** + * @param url + * @return 0-false + */ + private static int getReconnectParam(URL url) { + int reconnect; + String param = url.getParameter(Constants.RECONNECT_KEY); + if (param == null || param.length() == 0 || "true".equalsIgnoreCase(param)) { + reconnect = Constants.DEFAULT_RECONNECT_PERIOD; + } else if ("false".equalsIgnoreCase(param)) { + reconnect = 0; + } else { + try { + reconnect = Integer.parseInt(param); + } catch (Exception e) { + throw new IllegalArgumentException("reconnect param must be nonnegative integer or false/true. input is:" + param); + } + if (reconnect < 0) { + throw new IllegalArgumentException("reconnect param must be nonnegative integer or false/true. input is:" + param); + } + } + return reconnect; + } + + /** + * init reconnect thread + */ + private synchronized void initConnectStatusCheckCommand() { + //reconnect=false to close reconnect + int reconnect = getReconnectParam(getUrl()); + if (reconnect > 0 && (reconnectExecutorFuture == null || reconnectExecutorFuture.isCancelled())) { + Runnable connectStatusCheckCommand = new Runnable() { + @Override + public void run() { + try { + if (!isConnected()) { + connect(); + } else { + lastConnectedTime = System.currentTimeMillis(); + } + } catch (Throwable t) { + String errorMsg = "client reconnect to " + getUrl().getAddress() + " find error . url: " + getUrl(); + // wait registry sync provider list + if (System.currentTimeMillis() - lastConnectedTime > shutdown_timeout) { + if (!reconnect_error_log_flag.get()) { + reconnect_error_log_flag.set(true); + logger.error(errorMsg, t); + return; + } + } + if (reconnect_count.getAndIncrement() % reconnect_warning_period == 0) { + logger.warn(errorMsg, t); + } + } + } + }; + reconnectExecutorFuture = reconnectExecutorService.scheduleWithFixedDelay(connectStatusCheckCommand, reconnect, reconnect, TimeUnit.MILLISECONDS); + } + } + + private synchronized void destroyConnectStatusCheckCommand() { + try { + if (reconnectExecutorFuture != null && !reconnectExecutorFuture.isDone()) { + reconnectExecutorFuture.cancel(true); + reconnectExecutorService.purge(); + } + } catch (Throwable e) { + logger.warn(e.getMessage(), e); + } + } + + protected ExecutorService createExecutor() { + return Executors.newCachedThreadPool(new NamedThreadFactory(CLIENT_THREAD_POOL_NAME + CLIENT_THREAD_POOL_ID.incrementAndGet() + "-" + getUrl().getAddress(), true)); + } + public InetSocketAddress getConnectAddress() { return new InetSocketAddress(NetUtils.filterLocalHost(getUrl().getHost()), getUrl().getPort()); } @@ -162,7 +261,7 @@ public boolean hasAttribute(String key) { @Override public void send(Object message, boolean sent) throws RemotingException { - if (needReconnect && !isConnected()) { + if (send_reconnect && !isConnected()) { connect(); } Channel channel = getChannel(); @@ -174,22 +273,17 @@ public void send(Object message, boolean sent) throws RemotingException { } protected void connect() throws RemotingException { - connectLock.lock(); - try { - if (isConnected()) { return; } - + initConnectStatusCheckCommand(); doConnect(); - if (!isConnected()) { throw new RemotingException(this, "Failed connect to server " + getRemoteAddress() + " from " + getClass().getSimpleName() + " " + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion() + ", cause: Connect wait timeout: " + getConnectTimeout() + "ms."); - } else { if (logger.isInfoEnabled()) { logger.info("Successed connect to server " + getRemoteAddress() + " from " + getClass().getSimpleName() + " " @@ -197,15 +291,14 @@ protected void connect() throws RemotingException { + ", channel is " + this.getChannel()); } } - + reconnect_count.set(0); + reconnect_error_log_flag.set(false); } catch (RemotingException e) { throw e; - } catch (Throwable e) { throw new RemotingException(this, "Failed connect to server " + getRemoteAddress() + " from " + getClass().getSimpleName() + " " + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion() + ", cause: " + e.getMessage(), e); - } finally { connectLock.unlock(); } @@ -214,6 +307,7 @@ protected void connect() throws RemotingException { public void disconnect() { connectLock.lock(); try { + destroyConnectStatusCheckCommand(); try { Channel channel = getChannel(); if (channel != null) { @@ -249,27 +343,23 @@ public void reconnect() throws RemotingException { @Override public void close() { - try { - super.close(); + if (executor != null) { + ExecutorUtil.shutdownNow(executor, 100); + } } catch (Throwable e) { logger.warn(e.getMessage(), e); } - try { - if (executor != null) { - ExecutorUtil.shutdownNow(executor, 100); - } + super.close(); } catch (Throwable e) { logger.warn(e.getMessage(), e); } - try { disconnect(); } catch (Throwable e) { logger.warn(e.getMessage(), e); } - try { doClose(); } catch (Throwable e) { @@ -322,4 +412,5 @@ public String toString() { * @return channel */ protected abstract Channel getChannel(); + } diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractServer.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractServer.java index 2020fee79fb..c95183835b9 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractServer.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractServer.java @@ -54,7 +54,7 @@ public AbstractServer(URL url, ChannelHandler handler) throws RemotingException String bindIp = getUrl().getParameter(Constants.BIND_IP_KEY, getUrl().getHost()); int bindPort = getUrl().getParameter(Constants.BIND_PORT_KEY, getUrl().getPort()); if (url.getParameter(Constants.ANYHOST_KEY, false) || NetUtils.isInvalidLocalHost(bindIp)) { - bindIp = Constants.ANYHOST_VALUE; + bindIp = NetUtils.ANYHOST; } bindAddress = new InetSocketAddress(bindIp, bindPort); this.accepts = url.getParameter(Constants.ACCEPTS_KEY, Constants.DEFAULT_ACCEPTS); diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/ChannelHandlerDispatcher.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/ChannelHandlerDispatcher.java index 783741b78ef..cec80068826 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/ChannelHandlerDispatcher.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/ChannelHandlerDispatcher.java @@ -18,7 +18,6 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.ChannelHandler; @@ -43,7 +42,7 @@ public ChannelHandlerDispatcher(ChannelHandler... handlers) { } public ChannelHandlerDispatcher(Collection handlers) { - if (CollectionUtils.isNotEmpty(handlers)) { + if (handlers != null && !handlers.isEmpty()) { this.channelHandlers.addAll(handlers); } } diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/ChanelHandlerTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/ChanelHandlerTest.java index 7f226554383..412de8a9d02 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/ChanelHandlerTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/ChanelHandlerTest.java @@ -23,15 +23,15 @@ import org.apache.dubbo.remoting.exchange.Exchangers; import org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import junit.framework.TestCase; +import org.junit.Test; /** * ChanelHandlerTest *

    * mvn clean test -Dtest=*PerformanceClientTest -Dserver=10.20.153.187:9911 */ -public class ChanelHandlerTest { +public class ChanelHandlerTest extends TestCase { private static final Logger logger = LoggerFactory.getLogger(ChanelHandlerTest.class); diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientCloseTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientCloseTest.java index cb6b99f51f9..d71736e9b7e 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientCloseTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientCloseTest.java @@ -22,8 +22,8 @@ import org.apache.dubbo.remoting.exchange.ExchangeClient; import org.apache.dubbo.remoting.exchange.Exchangers; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import junit.framework.TestCase; +import org.junit.Test; import java.util.concurrent.atomic.AtomicInteger; @@ -31,7 +31,7 @@ * ProformanceClient * The test class will report abnormal thread pool, because the judgment on the thread pool concurrency problems produced in DefaultChannelHandler (connected event has been executed asynchronously, judgment, then closed the thread pool, thread pool and execution error, this problem can be specified through the Constants.CHANNEL_HANDLER_KEY=connection.) */ -public class PerformanceClientCloseTest { +public class PerformanceClientCloseTest extends TestCase { private static final Logger logger = LoggerFactory.getLogger(PerformanceClientCloseTest.class); diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientFixedTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientFixedTest.java index 24009e83768..8c5541b8da0 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientFixedTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientFixedTest.java @@ -22,13 +22,13 @@ import org.apache.dubbo.remoting.exchange.ExchangeClient; import org.apache.dubbo.remoting.exchange.Exchangers; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import junit.framework.TestCase; +import org.junit.Test; import java.util.ArrayList; import java.util.Random; -public class PerformanceClientFixedTest { +public class PerformanceClientFixedTest extends TestCase { private static final Logger logger = LoggerFactory.getLogger(PerformanceClientTest.class); diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientTest.java index e3e2347a9ee..dcd4799cda0 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientTest.java @@ -23,8 +23,8 @@ import org.apache.dubbo.remoting.exchange.Exchangers; import org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import junit.framework.TestCase; +import org.junit.Test; import java.text.DecimalFormat; import java.text.SimpleDateFormat; @@ -39,7 +39,7 @@ *

    * mvn clean test -Dtest=*PerformanceClientTest -Dserver=10.20.153.187:9911 */ -public class PerformanceClientTest { +public class PerformanceClientTest extends TestCase { private static final Logger logger = LoggerFactory.getLogger(PerformanceClientTest.class); diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceServerTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceServerTest.java index 413aa05b657..64130a3f619 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceServerTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceServerTest.java @@ -25,8 +25,8 @@ import org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter; import org.apache.dubbo.remoting.transport.dispatcher.execution.ExecutionDispatcher; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import junit.framework.TestCase; +import org.junit.Test; import java.util.ArrayList; import java.util.List; @@ -37,7 +37,7 @@ *

    * mvn clean test -Dtest=*PerformanceServerTest -Dport=9911 */ -public class PerformanceServerTest { +public class PerformanceServerTest extends TestCase { private static final Logger logger = LoggerFactory.getLogger(PerformanceServerTest.class); private static ExchangeServer server = null; diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/AbstractChannelBufferTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/AbstractChannelBufferTest.java index 0f1412326f7..78cce382a38 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/AbstractChannelBufferTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/AbstractChannelBufferTest.java @@ -16,10 +16,9 @@ */ package org.apache.dubbo.remoting.buffer; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -29,10 +28,10 @@ import static org.apache.dubbo.remoting.buffer.ChannelBuffers.directBuffer; import static org.apache.dubbo.remoting.buffer.ChannelBuffers.wrappedBuffer; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; public abstract class AbstractChannelBufferTest { @@ -53,14 +52,14 @@ protected boolean discardReadBytesDoesNotMoveWritableBytes() { } - @BeforeEach + @Before public void init() { buffer = newBuffer(CAPACITY); seed = System.currentTimeMillis(); random = new Random(seed); } - @AfterEach + @After public void dispose() { buffer = null; } @@ -71,40 +70,34 @@ public void initialState() { assertEquals(0, buffer.readerIndex()); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void readerIndexBoundaryCheck1() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { - try { - buffer.writerIndex(0); - } catch (IndexOutOfBoundsException e) { - fail(); - } - buffer.readerIndex(-1); - }); + try { + buffer.writerIndex(0); + } catch (IndexOutOfBoundsException e) { + fail(); + } + buffer.readerIndex(-1); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void readerIndexBoundaryCheck2() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { - try { - buffer.writerIndex(buffer.capacity()); - } catch (IndexOutOfBoundsException e) { - fail(); - } - buffer.readerIndex(buffer.capacity() + 1); - }); + try { + buffer.writerIndex(buffer.capacity()); + } catch (IndexOutOfBoundsException e) { + fail(); + } + buffer.readerIndex(buffer.capacity() + 1); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void readerIndexBoundaryCheck3() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { - try { - buffer.writerIndex(CAPACITY / 2); - } catch (IndexOutOfBoundsException e) { - fail(); - } - buffer.readerIndex(CAPACITY * 3 / 2); - }); + try { + buffer.writerIndex(CAPACITY / 2); + } catch (IndexOutOfBoundsException e) { + fail(); + } + buffer.readerIndex(CAPACITY * 3 / 2); } @Test @@ -115,38 +108,31 @@ public void readerIndexBoundaryCheck4() { buffer.readerIndex(buffer.capacity()); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void writerIndexBoundaryCheck1() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { - buffer.writerIndex(-1); - }); + buffer.writerIndex(-1); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void writerIndexBoundaryCheck2() { - - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { - try { - buffer.writerIndex(CAPACITY); - buffer.readerIndex(CAPACITY); - } catch (IndexOutOfBoundsException e) { - fail(); - } - buffer.writerIndex(buffer.capacity() + 1); - }); + try { + buffer.writerIndex(CAPACITY); + buffer.readerIndex(CAPACITY); + } catch (IndexOutOfBoundsException e) { + fail(); + } + buffer.writerIndex(buffer.capacity() + 1); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void writerIndexBoundaryCheck3() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { - try { - buffer.writerIndex(CAPACITY); - buffer.readerIndex(CAPACITY / 2); - } catch (IndexOutOfBoundsException e) { - fail(); - } - buffer.writerIndex(CAPACITY / 4); - }); + try { + buffer.writerIndex(CAPACITY); + buffer.readerIndex(CAPACITY / 2); + } catch (IndexOutOfBoundsException e) { + fail(); + } + buffer.writerIndex(CAPACITY / 4); } @Test @@ -156,64 +142,64 @@ public void writerIndexBoundaryCheck4() { buffer.writerIndex(CAPACITY); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void getByteBoundaryCheck1() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.getByte(-1)); + buffer.getByte(-1); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void getByteBoundaryCheck2() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.getByte(buffer.capacity())); + buffer.getByte(buffer.capacity()); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void getByteArrayBoundaryCheck1() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.getBytes(-1, new byte[0])); + buffer.getBytes(-1, new byte[0]); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void getByteArrayBoundaryCheck2() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.getBytes(-1, new byte[0], 0, 0)); + buffer.getBytes(-1, new byte[0], 0, 0); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void getByteBufferBoundaryCheck() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.getBytes(-1, ByteBuffer.allocate(0))); + buffer.getBytes(-1, ByteBuffer.allocate(0)); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void copyBoundaryCheck1() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.copy(-1, 0)); + buffer.copy(-1, 0); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void copyBoundaryCheck2() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.copy(0, buffer.capacity() + 1)); + buffer.copy(0, buffer.capacity() + 1); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void copyBoundaryCheck3() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.copy(buffer.capacity() + 1, 0)); + buffer.copy(buffer.capacity() + 1, 0); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void copyBoundaryCheck4() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.copy(buffer.capacity(), 1)); + buffer.copy(buffer.capacity(), 1); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void setIndexBoundaryCheck1() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.setIndex(-1, CAPACITY)); + buffer.setIndex(-1, CAPACITY); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void setIndexBoundaryCheck2() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.setIndex(CAPACITY / 2, CAPACITY / 4)); + buffer.setIndex(CAPACITY / 2, CAPACITY / 4); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void setIndexBoundaryCheck3() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.setIndex(0, CAPACITY + 1)); + buffer.setIndex(0, CAPACITY + 1); } @Test @@ -238,9 +224,9 @@ public void getByteBufferState() { assertEquals(0, dst.get(3)); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void getDirectByteBufferBoundaryCheck() { - Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.getBytes(-1, ByteBuffer.allocateDirect(0))); + buffer.getBytes(-1, ByteBuffer.allocateDirect(0)); } @Test diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/ChannelBufferStreamTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/ChannelBufferStreamTest.java index 569059211ca..64e92167b7e 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/ChannelBufferStreamTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/ChannelBufferStreamTest.java @@ -16,13 +16,13 @@ */ package org.apache.dubbo.remoting.buffer; -import org.junit.jupiter.api.Test; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; public class ChannelBufferStreamTest { diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/DirectChannelBufferTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/DirectChannelBufferTest.java index 6ee7860f6b0..c4ce299a039 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/DirectChannelBufferTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/DirectChannelBufferTest.java @@ -16,7 +16,7 @@ */ package org.apache.dubbo.remoting.buffer; -import org.junit.jupiter.api.Assertions; +import org.junit.Assert; public class DirectChannelBufferTest extends AbstractChannelBufferTest { @@ -25,7 +25,7 @@ public class DirectChannelBufferTest extends AbstractChannelBufferTest { @Override protected ChannelBuffer newBuffer(int capacity) { buffer = ChannelBuffers.directBuffer(capacity); - Assertions.assertEquals(0, buffer.writerIndex()); + Assert.assertEquals(0, buffer.writerIndex()); return buffer; } diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/DynamicChannelBufferTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/DynamicChannelBufferTest.java index 7accc4d60ec..8b4ca6ea400 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/DynamicChannelBufferTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/DynamicChannelBufferTest.java @@ -16,9 +16,9 @@ */ package org.apache.dubbo.remoting.buffer; -import org.junit.jupiter.api.Test; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; public class DynamicChannelBufferTest extends AbstractChannelBufferTest { diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/HeapChannelBufferTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/HeapChannelBufferTest.java index dbe6c6a58a5..f1c70b2ecbd 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/HeapChannelBufferTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/HeapChannelBufferTest.java @@ -16,7 +16,7 @@ */ package org.apache.dubbo.remoting.buffer; -import org.junit.jupiter.api.Assertions; +import org.junit.Assert; public class HeapChannelBufferTest extends AbstractChannelBufferTest { @@ -25,7 +25,7 @@ public class HeapChannelBufferTest extends AbstractChannelBufferTest { @Override protected ChannelBuffer newBuffer(int capacity) { buffer = ChannelBuffers.buffer(capacity); - Assertions.assertEquals(0, buffer.writerIndex()); + Assert.assertEquals(0, buffer.writerIndex()); return buffer; } diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/codec/ExchangeCodecTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/codec/ExchangeCodecTest.java index ac17a41ed86..2d026edecbd 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/codec/ExchangeCodecTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/codec/ExchangeCodecTest.java @@ -33,9 +33,9 @@ import org.apache.dubbo.remoting.telnet.codec.TelnetCodec; import org.apache.dubbo.remoting.transport.CodecSupport; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -43,7 +43,7 @@ import java.util.HashMap; import java.util.Map; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.fail; /** * @@ -103,7 +103,7 @@ private byte[] assemblyDataProtocol(byte[] header) { } //=================================================================================== - @BeforeEach + @Before public void setUp() throws Exception { codec = new ExchangeCodec(); } @@ -130,9 +130,9 @@ public void test_Decode_Error_Length() throws IOException { byte[] baddata = new byte[]{1, 2}; ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(join(request, baddata)); Response obj = (Response) codec.decode(channel, buffer); - Assertions.assertEquals(person, obj.getResult()); + Assert.assertEquals(person, obj.getResult()); //only decode necessary bytes - Assertions.assertEquals(request.length, buffer.readerIndex()); + Assert.assertEquals(request.length, buffer.readerIndex()); } @Test @@ -146,24 +146,24 @@ public void test_Decode_Error_Response_Object() throws IOException { System.arraycopy(badbytes, 0, request, 21, badbytes.length); Response obj = (Response) decode(request); - Assertions.assertEquals(90, obj.getStatus()); + Assert.assertEquals(90, obj.getStatus()); } @Test public void testInvalidSerializaitonId() throws Exception { byte[] header = new byte[]{MAGIC_HIGH, MAGIC_LOW, (byte)0x8F, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; Object obj = decode(header); - Assertions.assertTrue(obj instanceof Request); + Assert.assertTrue(obj instanceof Request); Request request = (Request) obj; - Assertions.assertTrue(request.isBroken()); - Assertions.assertTrue(request.getData() instanceof IOException); + Assert.assertTrue(request.isBroken()); + Assert.assertTrue(request.getData() instanceof IOException); header = new byte[]{MAGIC_HIGH, MAGIC_LOW, (byte)0x1F, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; obj = decode(header); - Assertions.assertTrue(obj instanceof Response); + Assert.assertTrue(obj instanceof Response); Response response = (Response) obj; - Assertions.assertEquals(response.getStatus(), Response.CLIENT_ERROR); - Assertions.assertTrue(response.getErrorMessage().contains("IOException")); + Assert.assertEquals(response.getStatus(), Response.CLIENT_ERROR); + Assert.assertTrue(response.getErrorMessage().contains("IOException")); } @Test @@ -174,7 +174,7 @@ public void test_Decode_Check_Payload() throws IOException { testDecode_assertEquals(request, TelnetCodec.DecodeResult.NEED_MORE_INPUT); fail(); } catch (IOException expected) { - Assertions.assertTrue(expected.getMessage().startsWith("Data length too large: " + Bytes.bytes2int(new byte[]{1, 1, 1, 1}))); + Assert.assertTrue(expected.getMessage().startsWith("Data length too large: " + Bytes.bytes2int(new byte[]{1, 1, 1, 1}))); } } @@ -197,9 +197,9 @@ public void test_Decode_MigicCodec_Contain_ExchangeHeader() throws IOException { Channel channel = getServerSideChannel(url); ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(header); Object obj = codec.decode(channel, buffer); - Assertions.assertEquals(TelnetCodec.DecodeResult.NEED_MORE_INPUT, obj); + Assert.assertEquals(TelnetCodec.DecodeResult.NEED_MORE_INPUT, obj); //If the telnet data and request data are in the same data packet, we should guarantee that the receipt of request data won't be affected by the factor that telnet does not have an end characters. - Assertions.assertEquals(2, buffer.readerIndex()); + Assert.assertEquals(2, buffer.readerIndex()); } @Test @@ -210,8 +210,8 @@ public void test_Decode_Return_Response_Person() throws IOException { byte[] request = getRequestBytes(person, header); Response obj = (Response) decode(request); - Assertions.assertEquals(20, obj.getStatus()); - Assertions.assertEquals(person, obj.getResult()); + Assert.assertEquals(20, obj.getStatus()); + Assert.assertEquals(person, obj.getResult()); System.out.println(obj); } @@ -221,8 +221,8 @@ public void test_Decode_Return_Response_Error() throws IOException { String errorString = "encode request data error "; byte[] request = getRequestBytes(errorString, header); Response obj = (Response) decode(request); - Assertions.assertEquals(90, obj.getStatus()); - Assertions.assertEquals(errorString, obj.getErrorMessage()); + Assert.assertEquals(90, obj.getStatus()); + Assert.assertEquals(errorString, obj.getErrorMessage()); } @Test @@ -233,10 +233,10 @@ public void test_Decode_Return_Request_Event_Object() throws IOException { byte[] request = getRequestBytes(person, header); Request obj = (Request) decode(request); - Assertions.assertEquals(person, obj.getData()); - Assertions.assertEquals(true, obj.isTwoWay()); - Assertions.assertEquals(true, obj.isEvent()); - Assertions.assertEquals(Version.getProtocolVersion(), obj.getVersion()); + Assert.assertEquals(person, obj.getData()); + Assert.assertEquals(true, obj.isTwoWay()); + Assert.assertEquals(true, obj.isEvent()); + Assert.assertEquals(Version.getProtocolVersion(), obj.getVersion()); System.out.println(obj); } @@ -248,10 +248,10 @@ public void test_Decode_Return_Request_Event_String() throws IOException { byte[] request = getRequestBytes(event, header); Request obj = (Request) decode(request); - Assertions.assertEquals(event, obj.getData()); - Assertions.assertEquals(true, obj.isTwoWay()); - Assertions.assertEquals(true, obj.isEvent()); - Assertions.assertEquals(Version.getProtocolVersion(), obj.getVersion()); + Assert.assertEquals(event, obj.getData()); + Assert.assertEquals(true, obj.isTwoWay()); + Assert.assertEquals(true, obj.isEvent()); + Assert.assertEquals(Version.getProtocolVersion(), obj.getVersion()); System.out.println(obj); } @@ -261,10 +261,10 @@ public void test_Decode_Return_Request_Heartbeat_Object() throws IOException { byte[] header = new byte[]{MAGIC_HIGH, MAGIC_LOW, (byte) 0xe2, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; byte[] request = getRequestBytes(null, header); Request obj = (Request) decode(request); - Assertions.assertEquals(null, obj.getData()); - Assertions.assertEquals(true, obj.isTwoWay()); - Assertions.assertEquals(true, obj.isHeartbeat()); - Assertions.assertEquals(Version.getProtocolVersion(), obj.getVersion()); + Assert.assertEquals(null, obj.getData()); + Assert.assertEquals(true, obj.isTwoWay()); + Assert.assertEquals(true, obj.isHeartbeat()); + Assert.assertEquals(Version.getProtocolVersion(), obj.getVersion()); System.out.println(obj); } @@ -276,10 +276,10 @@ public void test_Decode_Return_Request_Object() throws IOException { byte[] request = getRequestBytes(person, header); Request obj = (Request) decode(request); - Assertions.assertEquals(person, obj.getData()); - Assertions.assertEquals(true, obj.isTwoWay()); - Assertions.assertEquals(false, obj.isHeartbeat()); - Assertions.assertEquals(Version.getProtocolVersion(), obj.getVersion()); + Assert.assertEquals(person, obj.getData()); + Assert.assertEquals(true, obj.isTwoWay()); + Assert.assertEquals(false, obj.isHeartbeat()); + Assert.assertEquals(Version.getProtocolVersion(), obj.getVersion()); System.out.println(obj); } @@ -294,8 +294,8 @@ public void test_Decode_Error_Request_Object() throws IOException { System.arraycopy(badbytes, 0, request, 21, badbytes.length); Request obj = (Request) decode(request); - Assertions.assertEquals(true, obj.isBroken()); - Assertions.assertEquals(true, obj.getData() instanceof Throwable); + Assert.assertEquals(true, obj.isBroken()); + Assert.assertEquals(true, obj.getData() instanceof Throwable); } @Test @@ -306,8 +306,8 @@ public void test_Header_Response_NoSerializationFlag() throws IOException { byte[] request = getRequestBytes(person, header); Response obj = (Response) decode(request); - Assertions.assertEquals(20, obj.getStatus()); - Assertions.assertEquals(person, obj.getResult()); + Assert.assertEquals(20, obj.getStatus()); + Assert.assertEquals(person, obj.getResult()); System.out.println(obj); } @@ -319,8 +319,8 @@ public void test_Header_Response_Heartbeat() throws IOException { byte[] request = getRequestBytes(person, header); Response obj = (Response) decode(request); - Assertions.assertEquals(20, obj.getStatus()); - Assertions.assertEquals(person, obj.getResult()); + Assert.assertEquals(20, obj.getStatus()); + Assert.assertEquals(person, obj.getResult()); System.out.println(obj); } @@ -339,10 +339,10 @@ public void test_Encode_Request() throws IOException { encodeBuffer.readBytes(data); ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(data); Request obj = (Request) codec.decode(channel, decodeBuffer); - Assertions.assertEquals(request.isBroken(), obj.isBroken()); - Assertions.assertEquals(request.isHeartbeat(), obj.isHeartbeat()); - Assertions.assertEquals(request.isTwoWay(), obj.isTwoWay()); - Assertions.assertEquals(person, obj.getData()); + Assert.assertEquals(request.isBroken(), obj.isBroken()); + Assert.assertEquals(request.isHeartbeat(), obj.isHeartbeat()); + Assert.assertEquals(request.isTwoWay(), obj.isTwoWay()); + Assert.assertEquals(person, obj.getData()); } @Test @@ -365,12 +365,12 @@ public void test_Encode_Response() throws IOException { ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(data); Response obj = (Response) codec.decode(channel, decodeBuffer); - Assertions.assertEquals(response.getId(), obj.getId()); - Assertions.assertEquals(response.getStatus(), obj.getStatus()); - Assertions.assertEquals(response.isHeartbeat(), obj.isHeartbeat()); - Assertions.assertEquals(person, obj.getResult()); + Assert.assertEquals(response.getId(), obj.getId()); + Assert.assertEquals(response.getStatus(), obj.getStatus()); + Assert.assertEquals(response.isHeartbeat(), obj.isHeartbeat()); + Assert.assertEquals(person, obj.getResult()); // encode response verson ?? -// Assertions.assertEquals(response.getProtocolVersion(), obj.getVersion()); +// Assert.assertEquals(response.getProtocolVersion(), obj.getVersion()); } @@ -395,12 +395,12 @@ public void test_Encode_Error_Response() throws IOException { //encode resault check need decode ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(data); Response obj = (Response) codec.decode(channel, decodeBuffer); - Assertions.assertEquals(response.getId(), obj.getId()); - Assertions.assertEquals(response.getStatus(), obj.getStatus()); - Assertions.assertEquals(response.isHeartbeat(), obj.isHeartbeat()); - Assertions.assertEquals(badString, obj.getErrorMessage()); - Assertions.assertEquals(null, obj.getResult()); -// Assertions.assertEquals(response.getProtocolVersion(), obj.getVersion()); + Assert.assertEquals(response.getId(), obj.getId()); + Assert.assertEquals(response.getStatus(), obj.getStatus()); + Assert.assertEquals(response.isHeartbeat(), obj.isHeartbeat()); + Assert.assertEquals(badString, obj.getErrorMessage()); + Assert.assertEquals(null, obj.getResult()); +// Assert.assertEquals(response.getProtocolVersion(), obj.getVersion()); } // http://code.alibabatech.com/jira/browse/DUBBO-392 @@ -432,10 +432,10 @@ public void testMessageLengthGreaterThanMessageActualLength() throws Exception { /* request|1111...|request */ ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(out.toByteArray()); Request decodedRequest = (Request) codec.decode(channel, decodeBuffer); - Assertions.assertTrue(date.equals(decodedRequest.getData())); - Assertions.assertEquals(bytes.length + padding, decodeBuffer.readerIndex()); + Assert.assertTrue(date.equals(decodedRequest.getData())); + Assert.assertEquals(bytes.length + padding, decodeBuffer.readerIndex()); decodedRequest = (Request) codec.decode(channel, decodeBuffer); - Assertions.assertTrue(date.equals(decodedRequest.getData())); + Assert.assertTrue(date.equals(decodedRequest.getData())); } @Test @@ -446,9 +446,9 @@ public void testMessageLengthExceedPayloadLimitWhenEncode() throws Exception { AbstractMockChannel channel = getCliendSideChannel(url.addParameter(Constants.PAYLOAD_KEY, 4)); try { codec.encode(channel, encodeBuffer, request); - Assertions.fail(); + Assert.fail(); } catch (IOException e) { - Assertions.assertTrue(e.getMessage().startsWith("Data length too large: " + 6)); + Assert.assertTrue(e.getMessage().startsWith("Data length too large: " + 6)); } Response response = new Response(1L); @@ -456,9 +456,9 @@ public void testMessageLengthExceedPayloadLimitWhenEncode() throws Exception { encodeBuffer = ChannelBuffers.dynamicBuffer(512); channel = getServerSideChannel(url.addParameter(Constants.PAYLOAD_KEY, 4)); codec.encode(channel, encodeBuffer, response); - Assertions.assertTrue(channel.getReceivedMessage() instanceof Response); + Assert.assertTrue(channel.getReceivedMessage() instanceof Response); Response receiveMessage = (Response) channel.getReceivedMessage(); - Assertions.assertEquals(Response.BAD_RESPONSE, receiveMessage.getStatus()); - Assertions.assertTrue(receiveMessage.getErrorMessage().contains("Data length too large: ")); + Assert.assertEquals(Response.BAD_RESPONSE, receiveMessage.getStatus()); + Assert.assertTrue(receiveMessage.getErrorMessage().contains("Data length too large: ")); } } diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/codec/TelnetCodecTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/codec/TelnetCodecTest.java index 8b16c1bfaa3..47eb2212687 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/codec/TelnetCodecTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/codec/TelnetCodecTest.java @@ -24,9 +24,9 @@ import org.apache.dubbo.remoting.buffer.ChannelBuffers; import org.apache.dubbo.remoting.telnet.codec.TelnetCodec; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -47,7 +47,7 @@ public class TelnetCodecTest { /** * @throws java.lang.Exception */ - @BeforeEach + @Before public void setUp() throws Exception { codec = new TelnetCodec(); } @@ -116,7 +116,7 @@ protected void testDecode_assertEquals(byte[] request, Object ret, boolean isSer //decode Object obj = codec.decode(channel, buffer); - Assertions.assertEquals(ret, obj); + Assert.assertEquals(ret, obj); } @@ -130,10 +130,10 @@ protected void testEecode_assertEquals(Object request, byte[] ret, boolean isSer byte[] data = new byte[buffer.readableBytes()]; buffer.readBytes(data); - Assertions.assertEquals(ret.length, data.length); + Assert.assertEquals(ret.length, data.length); for (int i = 0; i < ret.length; i++) { if (ret[i] != data[i]) { - Assertions.fail(); + Assert.fail(); } } } @@ -157,8 +157,8 @@ private void testDecode_assertEquals(AbstractMockChannel channel, Object request //decode Object obj = codec.decode(channel, buffer); - Assertions.assertEquals(expectret, obj); - Assertions.assertEquals(channelReceive, channel.getReceivedMessage()); + Assert.assertEquals(expectret, obj); + Assert.assertEquals(channelReceive, channel.getReceivedMessage()); } private void testDecode_PersonWithEnterByte(byte[] enterbytes, boolean isNeedmore) throws IOException { @@ -172,9 +172,9 @@ private void testDecode_PersonWithEnterByte(byte[] enterbytes, boolean isNeedmor //decode Object obj = codec.decode(channel, buffer); if (isNeedmore) { - Assertions.assertEquals(Codec2.DecodeResult.NEED_MORE_INPUT, obj); + Assert.assertEquals(Codec2.DecodeResult.NEED_MORE_INPUT, obj); } else { - Assertions.assertTrue(obj instanceof String, "return must string "); + Assert.assertTrue("return must string ", obj instanceof String); } } @@ -185,7 +185,7 @@ private void testDecode_WithExitByte(byte[] exitbytes, boolean isChannelClose) t //decode codec.decode(channel, buffer); - Assertions.assertEquals(isChannelClose, channel.isClosed()); + Assert.assertEquals(isChannelClose, channel.isClosed()); } @Test @@ -254,13 +254,11 @@ public void testDecode_Backspace() throws IOException { testDecode_assertEquals(new byte[]{'a', 'x', -1, 'x', '\b'}, Codec2.DecodeResult.NEED_MORE_INPUT, new String(new byte[]{32, 32, 8, 8})); } - @Test + @Test(expected = IOException.class) public void testDecode_Backspace_WithError() throws IOException { - Assertions.assertThrows(IOException.class, () -> { - url = url.addParameter(AbstractMockChannel.ERROR_WHEN_SEND, Boolean.TRUE.toString()); - testDecode_Backspace(); - url = url.removeParameter(AbstractMockChannel.ERROR_WHEN_SEND); - }); + url = url.addParameter(AbstractMockChannel.ERROR_WHEN_SEND, Boolean.TRUE.toString()); + testDecode_Backspace(); + url = url.removeParameter(AbstractMockChannel.ERROR_WHEN_SEND); } @Test() @@ -278,25 +276,23 @@ public void testDecode_History_UP() throws IOException { testDecode_assertEquals(channel, UP, Codec2.DecodeResult.NEED_MORE_INPUT, expected1); } - @Test + @Test(expected = IOException.class) public void testDecode_UPorDOWN_WithError() throws IOException { - Assertions.assertThrows(IOException.class, () -> { - url = url.addParameter(AbstractMockChannel.ERROR_WHEN_SEND, Boolean.TRUE.toString()); + url = url.addParameter(AbstractMockChannel.ERROR_WHEN_SEND, Boolean.TRUE.toString()); - //init channel - AbstractMockChannel channel = getServerSideChannel(url); + //init channel + AbstractMockChannel channel = getServerSideChannel(url); - testDecode_assertEquals(channel, UP, Codec2.DecodeResult.NEED_MORE_INPUT, null); + testDecode_assertEquals(channel, UP, Codec2.DecodeResult.NEED_MORE_INPUT, null); - String request1 = "aaa\n"; - Object expected1 = "aaa"; - //init history - testDecode_assertEquals(channel, request1, expected1, null); + String request1 = "aaa\n"; + Object expected1 = "aaa"; + //init history + testDecode_assertEquals(channel, request1, expected1, null); - testDecode_assertEquals(channel, UP, Codec2.DecodeResult.NEED_MORE_INPUT, expected1); + testDecode_assertEquals(channel, UP, Codec2.DecodeResult.NEED_MORE_INPUT, expected1); - url = url.removeParameter(AbstractMockChannel.ERROR_WHEN_SEND); - }); + url = url.removeParameter(AbstractMockChannel.ERROR_WHEN_SEND); } //============================================================================================================================= diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/DefaultFutureTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/DefaultFutureTest.java index 1ab41243dd4..d088a56f9f4 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/DefaultFutureTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/DefaultFutureTest.java @@ -21,9 +21,8 @@ import org.apache.dubbo.remoting.TimeoutException; import org.apache.dubbo.remoting.exchange.Request; import org.apache.dubbo.remoting.handler.MockedChannel; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @@ -36,17 +35,17 @@ public class DefaultFutureTest { @Test public void newFuture() { DefaultFuture future = defaultFuture(3000); - Assertions.assertNotNull(future, "new future return null"); + Assert.assertNotNull("new future return null", future); } @Test public void isDone() { DefaultFuture future = defaultFuture(3000); - Assertions.assertTrue(!future.isDone(), "init future is finished!"); + Assert.assertTrue("init future is finished!", !future.isDone()); //cancel a future future.cancel(); - Assertions.assertTrue(future.isDone(), "cancel a future failed!"); + Assert.assertTrue("cancel a future failed!", future.isDone()); } /** @@ -74,7 +73,7 @@ public void timeoutNotSend() throws Exception { try { f.get(); } catch (Exception e) { - Assertions.assertTrue(e instanceof TimeoutException, "catch exception is not timeout exception!"); + Assert.assertTrue("catch exception is not timeout exception!", e instanceof TimeoutException); System.out.println(e.getMessage()); } } @@ -108,7 +107,7 @@ public void timeoutSend() throws Exception { try { f.get(); } catch (Exception e) { - Assertions.assertTrue(e instanceof TimeoutException, "catch exception is not timeout exception!"); + Assert.assertTrue("catch exception is not timeout exception!", e instanceof TimeoutException); System.out.println(e.getMessage()); } } diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannelTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannelTest.java index 92739bfef97..8572f89087a 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannelTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannelTest.java @@ -18,20 +18,15 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.remoting.exchange.support.DefaultFuture; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.exchange.Request; - import org.mockito.ArgumentCaptor; import org.mockito.Mockito; - import java.util.List; - import static org.mockito.Mockito.verify; import static org.mockito.Mockito.times; import static org.mockito.Mockito.when; @@ -43,7 +38,7 @@ public class HeaderExchangeChannelTest { private URL url = URL.valueOf("dubbo://localhost:20880"); private static final String CHANNEL_KEY = HeaderExchangeChannel.class.getName() + ".CHANNEL"; - @BeforeEach + @Before public void setup() { channel = new MockChannel() { @Override @@ -59,45 +54,45 @@ public URL getUrl() { public void getOrAddChannelTest00() { channel.setAttribute("CHANNEL_KEY", "attribute"); HeaderExchangeChannel ret = HeaderExchangeChannel.getOrAddChannel(channel); - Assertions.assertNotNull(ret); + Assert.assertNotNull(ret); } @Test public void getOrAddChannelTest01() { - channel = new MockChannel() { + channel = new MockChannel(){ @Override - public URL getUrl() { + public URL getUrl(){ return url; } @Override - public boolean isConnected() { + public boolean isConnected(){ return true; } }; - Assertions.assertNull(channel.getAttribute(CHANNEL_KEY)); + Assert.assertNull(channel.getAttribute(CHANNEL_KEY)); HeaderExchangeChannel ret = HeaderExchangeChannel.getOrAddChannel(channel); - Assertions.assertNotNull(ret); - Assertions.assertNotNull(channel.getAttribute(CHANNEL_KEY)); - Assertions.assertEquals(channel.getAttribute(CHANNEL_KEY).getClass(), HeaderExchangeChannel.class); + Assert.assertNotNull(ret); + Assert.assertNotNull(channel.getAttribute(CHANNEL_KEY)); + Assert.assertEquals(channel.getAttribute(CHANNEL_KEY).getClass(), HeaderExchangeChannel.class); } @Test public void getOrAddChannelTest02() { channel = null; HeaderExchangeChannel ret = HeaderExchangeChannel.getOrAddChannel(channel); - Assertions.assertNull(ret); + Assert.assertNull(ret); } @Test public void removeChannelIfDisconnectedTest() { - Assertions.assertNull(channel.getAttribute(CHANNEL_KEY)); + Assert.assertNull(channel.getAttribute(CHANNEL_KEY)); channel.setAttribute(CHANNEL_KEY, header); channel.close(); HeaderExchangeChannel.removeChannelIfDisconnected(channel); - Assertions.assertNull(channel.getAttribute(CHANNEL_KEY)); + Assert.assertNull(channel.getAttribute(CHANNEL_KEY)); } @Test @@ -108,7 +103,7 @@ public void sendTest00() { header.close(1); header.send(message, sent); } catch (Exception e) { - Assertions.assertTrue(e instanceof RemotingException); + Assert.assertTrue(e instanceof RemotingException); } } @@ -118,7 +113,7 @@ public void sendTest01() throws RemotingException { String message = "this is a test message"; header.send(message, sent); List objects = channel.getSentObjects(); - Assertions.assertEquals(objects.get(0), "this is a test message"); + Assert.assertEquals(objects.get(0), "this is a test message"); } @Test @@ -127,9 +122,9 @@ public void sendTest02() throws RemotingException { int message = 1; header.send(message, sent); List objects = channel.getSentObjects(); - Assertions.assertEquals(objects.get(0).getClass(), Request.class); + Assert.assertEquals(objects.get(0).getClass(), Request.class); Request request = (Request) objects.get(0); - Assertions.assertEquals(request.getVersion(), "2.0.2"); + Assert.assertEquals(request.getVersion(), "2.0.2"); } @Test @@ -137,16 +132,14 @@ public void sendTest04() throws RemotingException { String message = "this is a test message"; header.send(message); List objects = channel.getSentObjects(); - Assertions.assertEquals(objects.get(0), "this is a test message"); + Assert.assertEquals(objects.get(0), "this is a test message"); } - @Test + @Test(expected = RemotingException.class) public void requestTest01() throws RemotingException { - Assertions.assertThrows(RemotingException.class, () -> { - header.close(1000); - Object requestob = new Object(); - header.request(requestob); - }); + header.close(1000); + Object requestob = new Object(); + header.request(requestob); } @Test @@ -158,40 +151,38 @@ public void requestTest02() throws RemotingException { header.request(requestob); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(Request.class); verify(channel, times(1)).send(argumentCaptor.capture()); - Assertions.assertEquals(argumentCaptor.getValue().getData(), requestob); + Assert.assertEquals(argumentCaptor.getValue().getData(), requestob); } - @Test - public void requestTest03() throws RemotingException { - Assertions.assertThrows(RemotingException.class, () -> { - channel = new MockChannel() { - @Override - public void send(Object req) throws RemotingException { - throw new RemotingException(channel.getLocalAddress(), channel.getRemoteAddress(), "throw error"); - } - }; - header = new HeaderExchangeChannel(channel); - Object requestob = new Object(); - header.request(requestob, 1000); - }); + @Test(expected = RemotingException.class) + public void requestTest03() throws RemotingException{ + channel = new MockChannel() { + @Override + public void send(Object req) throws RemotingException { + throw new RemotingException(channel.getLocalAddress(), channel.getRemoteAddress(), "throw error"); + } + }; + header = new HeaderExchangeChannel(channel); + Object requestob = new Object(); + header.request(requestob, 1000); } @Test - public void isClosedTest() { - Assertions.assertFalse(header.isClosed()); + public void isClosedTest(){ + Assert.assertFalse(header.isClosed()); } @Test public void closeTest() { - Assertions.assertFalse(channel.isClosed()); + Assert.assertFalse(channel.isClosed()); header.close(); - Assertions.assertTrue(channel.isClosed()); + Assert.assertTrue(channel.isClosed()); } @Test public void closeWithTimeoutTest02() { - Assertions.assertFalse(channel.isClosed()); + Assert.assertFalse(channel.isClosed()); Request request = new Request(); DefaultFuture.newFuture(channel, request, 100); header.close(100); @@ -204,10 +195,10 @@ public void closeWithTimeoutTest02() { public void startCloseTest() { try { boolean isClosing = channel.isClosing(); - Assertions.assertFalse(isClosing); + Assert.assertFalse(isClosing); header.startClose(); isClosing = channel.isClosing(); - Assertions.assertTrue(isClosing); + Assert.assertTrue(isClosing); } catch (Exception e) { e.printStackTrace(); } @@ -215,81 +206,79 @@ public void startCloseTest() { @Test public void getLocalAddressTest() { - Assertions.assertNull(header.getLocalAddress()); + Assert.assertNull(header.getLocalAddress()); } @Test public void getRemoteAddressTest() { - Assertions.assertNull(header.getRemoteAddress()); + Assert.assertNull(header.getRemoteAddress()); } @Test public void getUrlTest() { - Assertions.assertEquals(header.getUrl(), URL.valueOf("dubbo://localhost:20880")); + Assert.assertEquals(header.getUrl(), URL.valueOf("dubbo://localhost:20880")); } @Test public void isConnectedTest() { - Assertions.assertFalse(header.isConnected()); + Assert.assertFalse(header.isConnected()); } @Test public void getChannelHandlerTest() { - Assertions.assertNull(header.getChannelHandler()); + Assert.assertNull(header.getChannelHandler()); } @Test public void getExchangeHandlerTest() { - Assertions.assertNull(header.getExchangeHandler()); + Assert.assertNull(header.getExchangeHandler()); } @Test public void getAttributeAndSetAttributeTest() { header.setAttribute("test", "test"); - Assertions.assertEquals(header.getAttribute("test"), "test"); - Assertions.assertTrue(header.hasAttribute("test")); + Assert.assertEquals(header.getAttribute("test"), "test"); + Assert.assertTrue(header.hasAttribute("test")); } @Test public void removeAttributeTest() { header.setAttribute("test", "test"); - Assertions.assertEquals(header.getAttribute("test"), "test"); + Assert.assertEquals(header.getAttribute("test"), "test"); header.removeAttribute("test"); - Assertions.assertFalse(header.hasAttribute("test")); + Assert.assertFalse(header.hasAttribute("test")); } @Test public void hasAttributeTest() { - Assertions.assertFalse(header.hasAttribute("test")); + Assert.assertFalse(header.hasAttribute("test")); header.setAttribute("test", "test"); - Assertions.assertTrue(header.hasAttribute("test")); + Assert.assertTrue(header.hasAttribute("test")); } @Test - public void hashCodeTest() { + public void hashCodeTest(){ final int prime = 31; int result = 1; result = prime * result + ((channel == null) ? 0 : channel.hashCode()); - Assertions.assertEquals(header.hashCode(), result); + Assert.assertEquals(header.hashCode(),result); } - @Test - public void equalsTest() { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - Assertions.assertEquals(header, new HeaderExchangeChannel(channel)); - header = new HeaderExchangeChannel(null); - Assertions.assertNotEquals(header, new HeaderExchangeChannel(channel)); - }); + @Test(expected = IllegalArgumentException.class) + public void equalsTest(){ + Assert.assertEquals(header, new HeaderExchangeChannel(channel)); + header = new HeaderExchangeChannel(null); + Assert.assertNotEquals(header, new HeaderExchangeChannel(channel)); } @Test - public void toStringTest() { - Assertions.assertEquals(header.toString(), channel.toString()); + public void toStringTest(){ + Assert.assertEquals(header.toString(),channel.toString()); } } diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeartBeatTaskTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeartBeatTaskTest.java index 782b8480cdc..b302b555f5b 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeartBeatTaskTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeartBeatTaskTest.java @@ -22,9 +22,9 @@ import org.apache.dubbo.common.timer.HashedWheelTimer; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.exchange.Request; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import java.util.Collections; import java.util.List; @@ -39,7 +39,7 @@ public class HeartBeatTaskTest { private HeartbeatTimerTask heartbeatTimerTask; private HashedWheelTimer heartbeatTimer; - @BeforeEach + @Before public void setup() throws Exception { long tickDuration = 1000; heartbeatTimer = new HashedWheelTimer(tickDuration / Constants.HEARTBEAT_CHECK_TICK, TimeUnit.MILLISECONDS); @@ -70,11 +70,11 @@ public void testHeartBeat() throws Exception { Thread.sleep(2000L); List objects = channel.getSentObjects(); - Assertions.assertTrue(objects.size() > 0); + Assert.assertTrue(objects.size() > 0); Object obj = objects.get(0); - Assertions.assertTrue(obj instanceof Request); + Assert.assertTrue(obj instanceof Request); Request request = (Request) obj; - Assertions.assertTrue(request.isHeartbeat()); + Assert.assertTrue(request.isHeartbeat()); } } diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/handler/ConnectChannelHandlerTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/handler/ConnectChannelHandlerTest.java index b61a6004283..699ad5c0599 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/handler/ConnectChannelHandlerTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/handler/ConnectChannelHandlerTest.java @@ -22,10 +22,10 @@ import org.apache.dubbo.remoting.exchange.Response; import org.apache.dubbo.remoting.transport.dispatcher.connection.ConnectionOrderedChannelHandler; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.atomic.AtomicInteger; @@ -33,7 +33,7 @@ public class ConnectChannelHandlerTest extends WrappedChannelHandlerTest { - @BeforeEach + @Before public void setUp() throws Exception { handler = new ConnectionOrderedChannelHandler(new BizChannelHander(true), url); } @@ -42,24 +42,24 @@ public void setUp() throws Exception { public void test_Connect_Blocked() throws RemotingException { handler = new ConnectionOrderedChannelHandler(new BizChannelHander(false), url); ThreadPoolExecutor executor = (ThreadPoolExecutor) getField(handler, "connectionExecutor", 1); - Assertions.assertEquals(1, executor.getMaximumPoolSize()); + Assert.assertEquals(1, executor.getMaximumPoolSize()); int runs = 20; int taskCount = runs * 2; for (int i = 0; i < runs; i++) { handler.connected(new MockedChannel()); handler.disconnected(new MockedChannel()); - Assertions.assertTrue(executor.getActiveCount() <= 1, executor.getActiveCount() + " must <=1"); + Assert.assertTrue(executor.getActiveCount() + " must <=1", executor.getActiveCount() <= 1); } //queue.size - Assertions.assertEquals(taskCount - 1, executor.getQueue().size()); + Assert.assertEquals(taskCount - 1, executor.getQueue().size()); for (int i = 0; i < taskCount; i++) { if (executor.getCompletedTaskCount() < taskCount) { sleep(100); } } - Assertions.assertEquals(taskCount, executor.getCompletedTaskCount()); + Assert.assertEquals(taskCount, executor.getCompletedTaskCount()); } @Test //biz error should not throw and affect biz thread. @@ -74,24 +74,20 @@ public void test_Disconnect_Biz_Error() throws RemotingException { handler.disconnected(new MockedChannel()); } - @Test + @Test(expected = ExecutionException.class) public void test_Connect_Execute_Error() throws RemotingException { - Assertions.assertThrows(ExecutionException.class, () -> { - handler = new ConnectionOrderedChannelHandler(new BizChannelHander(false), url); - ThreadPoolExecutor executor = (ThreadPoolExecutor) getField(handler, "connectionExecutor", 1); - executor.shutdown(); - handler.connected(new MockedChannel()); - }); + handler = new ConnectionOrderedChannelHandler(new BizChannelHander(false), url); + ThreadPoolExecutor executor = (ThreadPoolExecutor) getField(handler, "connectionExecutor", 1); + executor.shutdown(); + handler.connected(new MockedChannel()); } - @Test + @Test(expected = ExecutionException.class) public void test_Disconnect_Execute_Error() throws RemotingException { - Assertions.assertThrows(ExecutionException.class, () -> { - handler = new ConnectionOrderedChannelHandler(new BizChannelHander(false), url); - ThreadPoolExecutor executor = (ThreadPoolExecutor) getField(handler, "connectionExecutor", 1); - executor.shutdown(); - handler.disconnected(new MockedChannel()); - }); + handler = new ConnectionOrderedChannelHandler(new BizChannelHander(false), url); + ThreadPoolExecutor executor = (ThreadPoolExecutor) getField(handler, "connectionExecutor", 1); + executor.shutdown(); + handler.disconnected(new MockedChannel()); } //throw ChannelEventRunnable.runtimeExeception(int logger) not in execute exception @@ -106,23 +102,21 @@ public void test_Caught_Biz_Error() throws RemotingException { handler.caught(new MockedChannel(), new BizException()); } - @Test + @Test(expected = ExecutionException.class) public void test_Received_InvokeInExecuter() throws RemotingException { - Assertions.assertThrows(ExecutionException.class, () -> { - handler = new ConnectionOrderedChannelHandler(new BizChannelHander(false), url); - ThreadPoolExecutor executor = (ThreadPoolExecutor) getField(handler, "SHARED_EXECUTOR", 1); - executor.shutdown(); - executor = (ThreadPoolExecutor) getField(handler, "executor", 1); - executor.shutdown(); - handler.received(new MockedChannel(), ""); - }); + handler = new ConnectionOrderedChannelHandler(new BizChannelHander(false), url); + ThreadPoolExecutor executor = (ThreadPoolExecutor) getField(handler, "SHARED_EXECUTOR", 1); + executor.shutdown(); + executor = (ThreadPoolExecutor) getField(handler, "executor", 1); + executor.shutdown(); + handler.received(new MockedChannel(), ""); } /** * Events do not pass through the thread pool and execute directly on the IO */ @SuppressWarnings("deprecation") - @Disabled("Heartbeat is processed in HeartbeatHandler not WrappedChannelHandler.") + @Ignore("Heartbeat is processed in HeartbeatHandler not WrappedChannelHandler.") @Test public void test_Received_Event_invoke_direct() throws RemotingException { handler = new ConnectionOrderedChannelHandler(new BizChannelHander(false), url); @@ -136,10 +130,10 @@ public void test_Received_Event_invoke_direct() throws RemotingException { handler.received(new MockedChannel() { @Override public void send(Object message) throws RemotingException { - Assertions.assertEquals(true, ((Response) message).isHeartbeat(), "response.heartbeat"); + Assert.assertEquals("response.heartbeat", true, ((Response) message).isHeartbeat()); count.incrementAndGet(); } }, req); - Assertions.assertEquals(1, count.get(), "channel.send must be invoke"); + Assert.assertEquals("channel.send must be invoke", 1, count.get()); } } \ No newline at end of file diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/handler/HeaderExchangeHandlerTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/handler/HeaderExchangeHandlerTest.java index 3c11eca1c45..660f2d6e852 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/handler/HeaderExchangeHandlerTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/handler/HeaderExchangeHandlerTest.java @@ -26,8 +26,8 @@ import org.apache.dubbo.remoting.exchange.Response; import org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -47,7 +47,7 @@ public void test_received_request_oneway() throws RemotingException { ExchangeHandler exhandler = new MockedExchangeHandler() { @Override public void received(Channel channel, Object message) throws RemotingException { - Assertions.assertEquals(requestdata, message); + Assert.assertEquals(requestdata, message); } }; HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(exhandler); @@ -66,11 +66,11 @@ public void test_received_request_twoway() throws RemotingException { @Override public void send(Object message) throws RemotingException { Response res = (Response) message; - Assertions.assertEquals(request.getId(), res.getId()); - Assertions.assertEquals(request.getVersion(), res.getVersion()); - Assertions.assertEquals(Response.OK, res.getStatus()); - Assertions.assertEquals(requestdata, res.getResult()); - Assertions.assertEquals(null, res.getErrorMessage()); + Assert.assertEquals(request.getId(), res.getId()); + Assert.assertEquals(request.getVersion(), res.getVersion()); + Assert.assertEquals(Response.OK, res.getStatus()); + Assert.assertEquals(requestdata, res.getResult()); + Assert.assertEquals(null, res.getErrorMessage()); count.incrementAndGet(); } }; @@ -82,17 +82,17 @@ public CompletableFuture reply(ExchangeChannel channel, Object request) @Override public void received(Channel channel, Object message) throws RemotingException { - Assertions.fail(); + Assert.fail(); } }; HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(exhandler); hexhandler.received(mchannel, request); - Assertions.assertEquals(1, count.get()); + Assert.assertEquals(1, count.get()); } - @Test + @Test(expected = IllegalArgumentException.class) public void test_received_request_twoway_error_nullhandler() throws RemotingException { - Assertions.assertThrows(IllegalArgumentException.class, () -> new HeaderExchangeHandler(null)); + new HeaderExchangeHandler(null); } @Test @@ -107,11 +107,11 @@ public void test_received_request_twoway_error_reply() throws RemotingException @Override public void send(Object message) throws RemotingException { Response res = (Response) message; - Assertions.assertEquals(request.getId(), res.getId()); - Assertions.assertEquals(request.getVersion(), res.getVersion()); - Assertions.assertEquals(Response.SERVICE_ERROR, res.getStatus()); - Assertions.assertNull(res.getResult()); - Assertions.assertTrue(res.getErrorMessage().contains(BizException.class.getName())); + Assert.assertEquals(request.getId(), res.getId()); + Assert.assertEquals(request.getVersion(), res.getVersion()); + Assert.assertEquals(Response.SERVICE_ERROR, res.getStatus()); + Assert.assertNull(res.getResult()); + Assert.assertTrue(res.getErrorMessage().contains(BizException.class.getName())); count.incrementAndGet(); } }; @@ -123,7 +123,7 @@ public CompletableFuture reply(ExchangeChannel channel, Object request) }; HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(exhandler); hexhandler.received(mchannel, request); - Assertions.assertEquals(1, count.get()); + Assert.assertEquals(1, count.get()); } @Test @@ -138,17 +138,17 @@ public void test_received_request_twoway_error_reqeustBroken() throws RemotingEx @Override public void send(Object message) throws RemotingException { Response res = (Response) message; - Assertions.assertEquals(request.getId(), res.getId()); - Assertions.assertEquals(request.getVersion(), res.getVersion()); - Assertions.assertEquals(Response.BAD_REQUEST, res.getStatus()); - Assertions.assertNull(res.getResult()); - Assertions.assertTrue(res.getErrorMessage().contains(BizException.class.getName())); + Assert.assertEquals(request.getId(), res.getId()); + Assert.assertEquals(request.getVersion(), res.getVersion()); + Assert.assertEquals(Response.BAD_REQUEST, res.getStatus()); + Assert.assertNull(res.getResult()); + Assert.assertTrue(res.getErrorMessage().contains(BizException.class.getName())); count.incrementAndGet(); } }; HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(new MockedExchangeHandler()); hexhandler.received(mchannel, request); - Assertions.assertEquals(1, count.get()); + Assert.assertEquals(1, count.get()); } @Test @@ -160,7 +160,7 @@ public void test_received_request_event_readonly() throws RemotingException { final Channel mchannel = new MockedChannel(); HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(new MockedExchangeHandler()); hexhandler.received(mchannel, request); - Assertions.assertTrue(mchannel.hasAttribute(Constants.CHANNEL_ATTRIBUTE_READONLY_KEY)); + Assert.assertTrue(mchannel.hasAttribute(Constants.CHANNEL_ATTRIBUTE_READONLY_KEY)); } @Test @@ -172,20 +172,20 @@ public void test_received_request_event_other_discard() throws RemotingException final Channel mchannel = new MockedChannel() { @Override public void send(Object message) throws RemotingException { - Assertions.fail(); + Assert.fail(); } }; HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(new MockedExchangeHandler() { @Override public CompletableFuture reply(ExchangeChannel channel, Object request) throws RemotingException { - Assertions.fail(); + Assert.fail(); throw new RemotingException(channel, ""); } @Override public void received(Channel channel, Object message) throws RemotingException { - Assertions.fail(); + Assert.fail(); throw new RemotingException(channel, ""); } }); diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/handler/WrappedChannelHandlerTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/handler/WrappedChannelHandlerTest.java index 91ea0c40a66..1fa2a897195 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/handler/WrappedChannelHandlerTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/handler/WrappedChannelHandlerTest.java @@ -22,19 +22,19 @@ import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.transport.dispatcher.WrappedChannelHandler; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import java.lang.reflect.Field; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.fail; public class WrappedChannelHandlerTest { WrappedChannelHandler handler; URL url = URL.valueOf("test://10.20.30.40:1234"); - @BeforeEach + @Before public void setUp() throws Exception { handler = new WrappedChannelHandler(new BizChannelHander(true), url); } @@ -78,21 +78,21 @@ protected void sleep(int ms) { } } - @Test + @Test(expected = RemotingException.class) public void test_Connect_Biz_Error() throws RemotingException { - Assertions.assertThrows(RemotingException.class, () -> handler.connected(new MockedChannel())); + handler.connected(new MockedChannel()); } ; - @Test + @Test(expected = RemotingException.class) public void test_Disconnect_Biz_Error() throws RemotingException { - Assertions.assertThrows(RemotingException.class, () -> handler.disconnected(new MockedChannel())); + handler.disconnected(new MockedChannel()); } - @Test + @Test(expected = RemotingException.class) public void test_MessageReceived_Biz_Error() throws RemotingException { - Assertions.assertThrows(RemotingException.class, () -> handler.received(new MockedChannel(), "")); + handler.received(new MockedChannel(), ""); } @Test @@ -101,7 +101,7 @@ public void test_Caught_Biz_Error() throws RemotingException { handler.caught(new MockedChannel(), new BizException()); fail(); } catch (Exception e) { - Assertions.assertEquals(BizException.class, e.getCause().getClass()); + Assert.assertEquals(BizException.class, e.getCause().getClass()); } } diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/transport/AbstractCodecTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/transport/AbstractCodecTest.java index ec6c83d0873..735db70286b 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/transport/AbstractCodecTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/transport/AbstractCodecTest.java @@ -18,19 +18,19 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.remoting.Channel; -import org.junit.jupiter.api.Assertions; +import junit.framework.TestCase; import org.hamcrest.CoreMatchers; import org.mockito.internal.verification.VerificationModeFactory; import java.io.IOException; import static org.hamcrest.CoreMatchers.allOf; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; -public class AbstractCodecTest { +public class AbstractCodecTest extends TestCase { public void test_checkPayload_default8M() throws Exception { Channel channel = mock(Channel.class); diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/transport/codec/CodecAdapterTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/transport/codec/CodecAdapterTest.java index 3a157b32cae..c986a8b942d 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/transport/codec/CodecAdapterTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/transport/codec/CodecAdapterTest.java @@ -18,11 +18,11 @@ import org.apache.dubbo.remoting.codec.ExchangeCodecTest; -import org.junit.jupiter.api.BeforeEach; +import org.junit.Before; public class CodecAdapterTest extends ExchangeCodecTest { - @BeforeEach + @Before public void setUp() throws Exception { codec = new CodecAdapter(new DeprecatedExchangeCodec()); } diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/transport/codec/DeprecatedExchangeCodec.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/transport/codec/DeprecatedExchangeCodec.java index 552222db982..1dce90c8158 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/transport/codec/DeprecatedExchangeCodec.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/transport/codec/DeprecatedExchangeCodec.java @@ -136,7 +136,7 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro // decode response. Response res = new Response(id); if ((flag & FLAG_EVENT) != 0) { - res.setEvent(true); + res.setEvent(Response.HEARTBEAT_EVENT); } // get status. byte status = header[3]; @@ -167,7 +167,7 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro req.setVersion(Version.getProtocolVersion()); req.setTwoWay((flag & FLAG_TWOWAY) != 0); if ((flag & FLAG_EVENT) != 0) { - req.setEvent(true); + req.setEvent(Request.HEARTBEAT_EVENT); } try { ObjectInput in = CodecSupport.deserialize(channel.getUrl(), is, proto); diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/transport/codec/DeprecatedTelnetCodec.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/transport/codec/DeprecatedTelnetCodec.java index 49d3a02ce4d..b0a10524095 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/transport/codec/DeprecatedTelnetCodec.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/transport/codec/DeprecatedTelnetCodec.java @@ -81,7 +81,7 @@ private static Charset getCharset(Channel channel) { URL url = channel.getUrl(); if (url != null) { String parameter = url.getParameter(Constants.CHARSET_KEY); - if (StringUtils.isNotEmpty(parameter)) { + if (parameter != null && parameter.length() > 0) { try { return Charset.forName(parameter); } catch (Throwable t) { diff --git a/dubbo-remoting/dubbo-remoting-etcd3/pom.xml b/dubbo-remoting/dubbo-remoting-etcd3/pom.xml deleted file mode 100644 index 768052f6b1d..00000000000 --- a/dubbo-remoting/dubbo-remoting-etcd3/pom.xml +++ /dev/null @@ -1,52 +0,0 @@ - - - - - 4.0.0 - - dubbo-remoting - org.apache.dubbo - 2.7.1-SNAPSHOT - - dubbo-remoting-etcd3 - jar - ${project.artifactId} - The etcd3 remoting module of Dubbo project - - false - - - - org.apache.dubbo - dubbo-common - ${project.parent.version} - - - io.etcd - jetcd-core - - - io.netty - netty-all - - - - - \ No newline at end of file diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/ChildListener.java b/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/ChildListener.java deleted file mode 100644 index 46a0af8ceed..00000000000 --- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/ChildListener.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * 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 org.apache.dubbo.remoting.etcd; - -import java.util.List; - -public interface ChildListener { - - void childChanged(String path, List children); - -} diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/EtcdClient.java b/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/EtcdClient.java deleted file mode 100644 index 286be934469..00000000000 --- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/EtcdClient.java +++ /dev/null @@ -1,183 +0,0 @@ -/* - * 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. - */ - -/* - * 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 org.apache.dubbo.remoting.etcd; - -import org.apache.dubbo.common.URL; - -import java.util.List; -import java.util.concurrent.CancellationException; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -public interface EtcdClient { - - /** - * save the specified path to the etcd registry. - * - * @param path the path to be saved - */ - void create(String path); - - /** - * save the specified path to the etcd registry. - * if node disconnect from etcd, it will be deleted - * automatically by etcd when sessian timeout. - * - * @param path the path to be saved - * @return the lease of current path. - */ - long createEphemeral(String path); - - /** - * remove the specified from etcd registry. - * - * @param path the path to be removed - */ - void delete(String path); - - /** - * find direct children directory, excluding path self, - * Never return null. - * - * @param path the path to be found direct children. - * @return direct children directory, contains zero element - * list if children directory not exists. - */ - List getChildren(String path); - - /** - * register children listener for specified path. - * - * @param path the path to be watched when children is added, delete or update. - * @param listener when children is changed , listener will be trigged. - * @return direct children directory, contains zero element - * list if children directory not exists. - */ - List addChildListener(String path, ChildListener listener); - - /** - * find watcher of the children listener for specified path. - * - * @param path the path to be watched when children is added, delete or update. - * @param listener when children is changed , listener will be trigged. - * @return watcher if find else null - */ - T getChildListener(String path, ChildListener listener); - - /** - * unregister children lister for specified path. - * - * @param path the path to be unwatched . - * @param listener when children is changed , lister will be trigged. - */ - void removeChildListener(String path, ChildListener listener); - - /** - * support connection notify if connection state was changed. - * - * @param listener if state changed, listener will be triggered. - */ - void addStateListener(StateListener listener); - - /** - * remove connection notify if connection state was changed. - * - * @param listener remove already registered listener, if listener - * not exists nothing happened. - */ - void removeStateListener(StateListener listener); - - /** - * test if current client is active. - * - * @return true if connection is active else false. - */ - boolean isConnected(); - - /** - * close current client and release all resourses. - */ - void close(); - - URL getUrl(); - - /*** - * create new lease from specified second ,it should be waiting if failed.

    - * - * @param second lease time (support second only). - * @return lease id from etcd - */ - long createLease(long second); - - /*** - * create new lease from specified ttl second before waiting specified timeout.

    - * - * @param ttl lease time (support second only). - * @param timeout the maximum time to wait - * @param unit the time unit of the timeout argument - * @throws CancellationException if this future was cancelled - * @throws ExecutionException if this future completed exceptionally - * @throws InterruptedException if the current thread was interrupted - * while waiting - * @throws TimeoutException if the wait timed out - * @return lease id from etcd - */ - public long createLease(long ttl, long timeout, TimeUnit unit) - throws InterruptedException, ExecutionException, TimeoutException; - - /** - * revoke specified lease, any associated path will removed automatically. - * - * @param lease to be removed lease - */ - void revokeLease(long lease); - - - /** - * Get the value of the specified key. - * @param key the specified key - * @return null if the value is not found - */ - String getKVValue(String key); - - /** - * Put the key value pair to etcd - * @param key the specified key - * @param value the paired value - * @return true if put success - */ - boolean put(String key, String value); - -} diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/EtcdTransporter.java b/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/EtcdTransporter.java deleted file mode 100644 index 2c0befb0a4c..00000000000 --- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/EtcdTransporter.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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. - */ - -/* - * 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 org.apache.dubbo.remoting.etcd; - -import org.apache.dubbo.common.Constants; -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.extension.Adaptive; -import org.apache.dubbo.common.extension.SPI; - -@SPI("jetcd") -public interface EtcdTransporter { - - @Adaptive({Constants.CLIENT_KEY, Constants.TRANSPORTER_KEY}) - EtcdClient connect(URL url); - -} diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClient.java b/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClient.java deleted file mode 100644 index ff4c118b2a3..00000000000 --- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClient.java +++ /dev/null @@ -1,433 +0,0 @@ -/* - * 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 org.apache.dubbo.remoting.etcd.jetcd; - -import io.grpc.ManagedChannel; -import org.apache.dubbo.common.Constants; -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.ExecutorUtil; -import org.apache.dubbo.common.utils.NamedThreadFactory; -import org.apache.dubbo.remoting.etcd.ChildListener; -import org.apache.dubbo.remoting.etcd.StateListener; -import org.apache.dubbo.remoting.etcd.option.OptionUtil; -import org.apache.dubbo.remoting.etcd.support.AbstractEtcdClient; - -import com.google.protobuf.ByteString; -import io.etcd.jetcd.ByteSequence; -import io.etcd.jetcd.api.Event; -import io.etcd.jetcd.api.KeyValue; -import io.etcd.jetcd.api.WatchCancelRequest; -import io.etcd.jetcd.api.WatchCreateRequest; -import io.etcd.jetcd.api.WatchGrpc; -import io.etcd.jetcd.api.WatchRequest; -import io.etcd.jetcd.api.WatchResponse; -import io.etcd.jetcd.common.exception.ClosedClientException; -import io.grpc.Status; -import io.grpc.stub.StreamObserver; -import io.netty.util.internal.ConcurrentSet; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.Random; -import java.util.Set; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; -import java.util.concurrent.locks.ReentrantLock; - -import static java.util.stream.Collectors.toList; -import static org.apache.dubbo.remoting.etcd.jetcd.JEtcdClientWrapper.UTF_8; - -/** - * etct3 client. - */ -public class JEtcdClient extends AbstractEtcdClient { - - private JEtcdClientWrapper clientWrapper; - private ScheduledExecutorService reconnectSchedule; - - private ExecutorService notifyExecutor; - - private int delayPeriod; - private Logger logger = LoggerFactory.getLogger(JEtcdClient.class); - - public JEtcdClient(URL url) { - super(url); - try { - clientWrapper = new JEtcdClientWrapper(url); - clientWrapper.setConnectionStateListener((client, state) -> { - if (state == StateListener.CONNECTED) { - JEtcdClient.this.stateChanged(StateListener.CONNECTED); - } else if (state == StateListener.DISCONNECTED) { - JEtcdClient.this.stateChanged(StateListener.DISCONNECTED); - } - }); - delayPeriod = getUrl().getParameter(Constants.REGISTRY_RETRY_PERIOD_KEY, Constants.DEFAULT_REGISTRY_RETRY_PERIOD); - reconnectSchedule = Executors.newScheduledThreadPool(1, - new NamedThreadFactory("etcd3-watch-auto-reconnect")); - - notifyExecutor = new ThreadPoolExecutor( - 1 - , url.getParameter(Constants.ETCD3_NOTIFY_MAXTHREADS_KEYS, Constants.DEFAULT_ETCD3_NOTIFY_THREADS) - , Constants.DEFAULT_SESSION_TIMEOUT - , TimeUnit.MILLISECONDS - , new LinkedBlockingQueue(url.getParameter(Constants.DEFAULT_ETCD3_NOTIFY_QUEUES_KEY, Constants.DEFAULT_GRPC_QUEUES * 3)) - , new NamedThreadFactory("etcd3-notify", true)); - - clientWrapper.start(); - } catch (Exception e) { - throw new IllegalStateException(e.getMessage(), e); - } - } - - @Override - public void doCreatePersistent(String path) { - clientWrapper.createPersistent(path); - } - - @Override - public long doCreateEphemeral(String path) { - return clientWrapper.createEphemeral(path); - } - - @Override - public boolean checkExists(String path) { - return clientWrapper.checkExists(path); - } - - @Override - public EtcdWatcher createChildWatcherListener(String path, ChildListener listener) { - return new EtcdWatcher(listener); - } - - @Override - public List addChildWatcherListener(String path, EtcdWatcher etcdWatcher) { - return etcdWatcher.forPath(path); - } - - @Override - public void removeChildWatcherListener(String path, EtcdWatcher etcdWatcher) { - etcdWatcher.unwatch(); - } - - @Override - public List getChildren(String path) { - return clientWrapper.getChildren(path); - } - - @Override - public boolean isConnected() { - return clientWrapper.isConnected(); - } - - @Override - public long createLease(long second) { - return clientWrapper.createLease(second); - } - - @Override - public long createLease(long ttl, long timeout, TimeUnit unit) - throws InterruptedException, ExecutionException, TimeoutException { - return clientWrapper.createLease(ttl, timeout, unit); - } - - @Override - public void delete(String path) { - clientWrapper.delete(path); - } - - @Override - public void revokeLease(long lease) { - clientWrapper.revokeLease(lease); - } - - @Override - public void doClose() { - try { - if (notifyExecutor != null) { - ExecutorUtil.shutdownNow(notifyExecutor, 100); - } - } catch (Exception e) { - logger.warn(e.getMessage(), e); - } - - try { - if (reconnectSchedule != null) { - ExecutorUtil.shutdownNow(reconnectSchedule, 100); - } - } catch (Exception e) { - logger.warn(e.getMessage(), e); - } finally { - clientWrapper.doClose(); - } - } - - @Override - public String getKVValue(String key) { - return clientWrapper.getKVValue(key); - } - - @Override - public boolean put(String key, String value) { - return clientWrapper.put(key, value); - } - - public ManagedChannel getChannel() { - return clientWrapper.getChannel(); - } - - public class EtcdWatcher implements StreamObserver { - - protected WatchGrpc.WatchStub watchStub; - protected StreamObserver watchRequest; - protected long watchId; - protected String path; - protected Throwable throwable; - protected volatile Set urls = new ConcurrentSet<>(); - private ChildListener listener; - - protected ReentrantLock lock = new ReentrantLock(true); - - public EtcdWatcher(ChildListener listener) { - this.listener = listener; - } - - @Override - public void onNext(WatchResponse response) { - - // prevents grpc on sending watchResponse to a closed watch client. - if (!isConnected()) { - return; - } - - watchId = response.getWatchId(); - - if (listener != null) { - int modified = 0; - String service = null; - Iterator iterator = response.getEventsList().iterator(); - while (iterator.hasNext()) { - Event event = iterator.next(); - switch (event.getType()) { - case PUT: { - if (((service = find(event)) != null) - && safeUpdate(service, true)) modified++; - break; - } - case DELETE: { - if (((service = find(event)) != null) - && safeUpdate(service, false)) modified++; - break; - } - default: - break; - } - } - if (modified > 0) { - notifyExecutor.execute(() -> listener.childChanged(path, new ArrayList<>(urls))); - } - - } - } - - @Override - public void onError(Throwable e) { - tryReconnect(e); - } - - public void unwatch() { - - // prevents grpc on sending watchResponse to a closed watch client. - if (!isConnected()) { - return; - } - - try { - this.listener = null; - if (watchRequest != null) { - WatchCancelRequest watchCancelRequest = - WatchCancelRequest.newBuilder().setWatchId(watchId).build(); - WatchRequest cancelRequest = WatchRequest.newBuilder() - .setCancelRequest(watchCancelRequest).build(); - this.watchRequest.onNext(cancelRequest); - } - } catch (Exception ignored) { - logger.warn("Failed to cancel watch for path '" + path + "'", ignored); - } - } - - public List forPath(String path) { - - if (!isConnected()) { - throw new ClosedClientException("watch client has been closed, path '" + path + "'"); - } - if (this.path != null) { - unwatch(); - } - - this.path = path; - - lock.lock(); - try { - - this.watchStub = WatchGrpc.newStub(clientWrapper.getChannel()); - this.watchRequest = watchStub.watch(this); - this.watchRequest.onNext(nextRequest()); - - List children = clientWrapper.getChildren(path); - /** - * caching the current service - */ - if (!children.isEmpty()) { - this.urls.addAll(filterChildren(children)); - } - - return new ArrayList<>(urls); - } finally { - lock.unlock(); - } - } - - private boolean safeUpdate(String service, boolean add) { - lock.lock(); - try { - /** - * If the collection already contains the specified service, do nothing - */ - return add ? this.urls.add(service) : this.urls.remove(service); - } finally { - lock.unlock(); - } - } - - private String find(Event event) { - KeyValue keyValue = event.getKv(); - String key = keyValue.getKey().toStringUtf8(); - - int len = path.length(), index = len, count = 0; - if (key.length() >= index) { - for (; (index = key.indexOf(Constants.PATH_SEPARATOR, index)) != -1; ++index) { - if (count++ > 1) break; - } - } - - /** - * if children changed , we should refresh invokers - */ - if (count == 1) { - /** - * remove prefix - */ - return key.substring(len + 1); - } - - return null; - } - - private List filterChildren(List children) { - if (children == null) return Collections.emptyList(); - if (children.size() <= 0) return children; - final int len = path.length(); - return children.stream().parallel() - .filter(child -> { - int index = len, count = 0; - if (child.length() > len) { - for (; (index = child.indexOf(Constants.PATH_SEPARATOR, index)) != -1; ++index) { - if (count++ > 1) break; - } - } - return count == 1; - }) - .map(child -> child.substring(len + 1)) - .collect(toList()); - } - - /** - * create new watching request for current path. - */ - protected WatchRequest nextRequest() { - - WatchCreateRequest.Builder builder = WatchCreateRequest.newBuilder() - .setKey(ByteString.copyFromUtf8(path)) - .setRangeEnd(ByteString.copyFrom( - OptionUtil.prefixEndOf(ByteSequence.from(path, UTF_8)).getBytes())) - .setProgressNotify(true); - - return WatchRequest.newBuilder().setCreateRequest(builder).build(); - } - - public void tryReconnect(Throwable e) { - - this.throwable = e; - - logger.error("watcher client has error occurred, current path '" + path + "'", e); - - // prevents grpc on sending error to a closed watch client. - if (!isConnected()) { - return; - } - - - Status status = Status.fromThrowable(e); - // system may be recover later, current connect won't be lost - if (OptionUtil.isHaltError(status) || OptionUtil.isNoLeaderError(status)) { - reconnectSchedule.schedule(this::reconnect, new Random().nextInt(delayPeriod), TimeUnit.MILLISECONDS); - return; - } - // reconnect with a delay; avoiding immediate retry on a long connection downtime. - reconnectSchedule.schedule(this::reconnect, new Random().nextInt(delayPeriod), TimeUnit.MILLISECONDS); - } - - protected synchronized void reconnect() { - this.closeWatchRequest(); - this.recreateWatchRequest(); - } - - protected void recreateWatchRequest() { - if (watchRequest == null) { - this.watchStub = WatchGrpc.newStub(clientWrapper.getChannel()); - this.watchRequest = watchStub.watch(this); - } - this.watchRequest.onNext(nextRequest()); - this.throwable = null; - logger.warn("watch client retried connect for path '" + path + "', connection status : " + isConnected()); - } - - protected void closeWatchRequest() { - if (this.watchRequest == null) { - return; - } - this.watchRequest.onCompleted(); - this.watchRequest = null; - } - - @Override - public void onCompleted() { - // do not touch this method, if you want terminate this stream. - } - } -} diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientWrapper.java b/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientWrapper.java deleted file mode 100644 index b122f53b120..00000000000 --- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientWrapper.java +++ /dev/null @@ -1,714 +0,0 @@ -/* - * 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 org.apache.dubbo.remoting.etcd.jetcd; - -import io.etcd.jetcd.kv.PutResponse; -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.ConcurrentHashSet; -import org.apache.dubbo.common.utils.NamedThreadFactory; -import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.remoting.etcd.RetryPolicy; -import org.apache.dubbo.remoting.etcd.StateListener; -import org.apache.dubbo.remoting.etcd.option.Constants; - -import io.etcd.jetcd.ByteSequence; -import io.etcd.jetcd.Client; -import io.etcd.jetcd.ClientBuilder; -import io.etcd.jetcd.CloseableClient; -import io.etcd.jetcd.KeyValue; -import io.etcd.jetcd.Observers; -import io.etcd.jetcd.common.exception.ErrorCode; -import io.etcd.jetcd.common.exception.EtcdException; -import io.etcd.jetcd.kv.GetResponse; -import io.etcd.jetcd.lease.LeaseKeepAliveResponse; -import io.etcd.jetcd.options.GetOption; -import io.etcd.jetcd.options.PutOption; -import io.grpc.ConnectivityState; -import io.grpc.ManagedChannel; -import io.grpc.Status; -import io.grpc.stub.StreamObserver; -import io.grpc.util.RoundRobinLoadBalancerFactory; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.nio.charset.Charset; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.concurrent.Callable; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; -import java.util.concurrent.atomic.AtomicReference; -import java.util.function.Consumer; - -import static java.util.stream.Collectors.toList; - -public class JEtcdClientWrapper { - - private Logger logger = LoggerFactory.getLogger(JEtcdClientWrapper.class); - - private final URL url; - private volatile Client client; - private volatile boolean started = false; - private volatile boolean connectState = false; - private ScheduledFuture future; - private ScheduledExecutorService reconnectNotify; - private AtomicReference channel; - - private ConnectionStateListener connectionStateListener; - - private long expirePeriod; - - private CompletableFuture completableFuture; - - private RetryPolicy retryPolicy; - - private RuntimeException failed; - - private final ScheduledFuture retryFuture; - private final ScheduledExecutorService retryExecutor = Executors.newScheduledThreadPool(1, - new NamedThreadFactory("Etcd3RegistryKeepAliveFailedRetryTimer", true)); - - private final Set failedRegistered = new ConcurrentHashSet(); - - private final Set registeredPaths = new ConcurrentHashSet<>(); - private volatile CloseableClient keepAlive = null; - - /** - * Support temporary nodes to reuse the same lease - */ - private volatile long globalLeaseId; - - private volatile boolean cancelKeepAlive = false; - - public static final Charset UTF_8 = Charset.forName("UTF-8"); - - public JEtcdClientWrapper(URL url) { - this.url = url; - this.expirePeriod = url.getParameter(Constants.SESSION_TIMEOUT_KEY, Constants.DEFAULT_KEEPALIVE_TIMEOUT) / 1000; - if (expirePeriod <= 0) { - this.expirePeriod = Constants.DEFAULT_KEEPALIVE_TIMEOUT / 1000; - } - this.channel = new AtomicReference<>(); - this.completableFuture = CompletableFuture.supplyAsync(() -> prepareClient(url)); - this.reconnectNotify = Executors.newScheduledThreadPool(1, - new NamedThreadFactory("reconnectNotify", true)); - this.retryPolicy = new RetryNTimes(1, 1000, TimeUnit.MILLISECONDS); - - this.failed = new IllegalStateException("Etcd3 registry is not connected yet, url:" + url); - int retryPeriod = url.getParameter(Constants.REGISTRY_RETRY_PERIOD_KEY, Constants.DEFAULT_REGISTRY_RETRY_PERIOD); - this.retryFuture = retryExecutor.scheduleWithFixedDelay(() -> { - try { - retry(); - } catch (Throwable t) { - logger.error("Unexpected error occur at failed retry, cause: " + t.getMessage(), t); - } - }, retryPeriod, retryPeriod, TimeUnit.MILLISECONDS); - } - - private Client prepareClient(URL url) { - - int maxInboundSize = DEFAULT_INBOUND_SIZE; - if (StringUtils.isNotEmpty(System.getProperty(GRPC_MAX_INBOUND_SIZE_KEY))) { - maxInboundSize = Integer.valueOf(System.getProperty(GRPC_MAX_INBOUND_SIZE_KEY)); - } - - ClientBuilder clientBuilder = Client.builder() - .loadBalancerFactory(RoundRobinLoadBalancerFactory.getInstance()) - .endpoints(endPoints(url.getBackupAddress())) - .maxInboundMessageSize(maxInboundSize); - - return clientBuilder.build(); - } - - public Client getClient() { - return client; - } - - /** - * try to get current connected channel. - * - * @return connected channel. - */ - public ManagedChannel getChannel() { - if (channel.get() == null || (channel.get().isShutdown() || channel.get().isTerminated())) { - channel.set(newChannel(client)); - } - return channel.get(); - } - - /** - * find direct children directory, excluding path self, - * Never return null. - * - * @param path the path to be found direct children. - * @return direct children directory, contains zero element - * list if children directory not exists. - */ - public List getChildren(String path) { - try { - return RetryLoops.invokeWithRetry( - () -> { - requiredNotNull(client, failed); - int len = path.length(); - return client.getKVClient() - .get(ByteSequence.from(path, UTF_8), - GetOption.newBuilder().withPrefix(ByteSequence.from(path, UTF_8)).build()) - .get(DEFAULT_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS) - .getKvs().stream().parallel() - .filter(pair -> { - String key = pair.getKey().toString(UTF_8); - int index = len, count = 0; - if (key.length() > len) { - for (; (index = key.indexOf(Constants.PATH_SEPARATOR, index)) != -1; ++index) { - if (count++ > 1) break; - } - } - return count == 1; - }) - .map(pair -> pair.getKey().toString(UTF_8)) - .collect(toList()); - }, retryPolicy); - } catch (Exception e) { - throw new IllegalStateException(e.getMessage(), e); - } - } - - public boolean isConnected() { - return ConnectivityState.READY == (getChannel().getState(false)) - || ConnectivityState.IDLE == (getChannel().getState(false)); - } - - public long createLease(long second) { - try { - return RetryLoops.invokeWithRetry( - () -> { - requiredNotNull(client, failed); - return client.getLeaseClient() - .grant(second) - .get(DEFAULT_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS) - .getID(); - }, retryPolicy); - } catch (Exception e) { - throw new IllegalStateException(e.getMessage(), e); - } - } - - public void revokeLease(long lease) { - try { - RetryLoops.invokeWithRetry( - (Callable) () -> { - requiredNotNull(client, failed); - client.getLeaseClient() - .revoke(lease) - .get(DEFAULT_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS); - return null; - }, retryPolicy); - } catch (Exception e) { - throw new IllegalStateException(e.getMessage(), e); - } - } - - public long createLease(long ttl, long timeout, TimeUnit unit) - throws InterruptedException, ExecutionException, TimeoutException { - - if (timeout <= 0) { - return createLease(ttl); - } - - requiredNotNull(client, failed); - return client.getLeaseClient() - .grant(ttl) - .get(timeout, unit).getID(); - } - - - /** - * try to check if path exists. - */ - public boolean checkExists(String path) { - try { - return RetryLoops.invokeWithRetry( - () -> { - requiredNotNull(client, failed); - return client.getKVClient() - .get(ByteSequence.from(path, UTF_8), GetOption.newBuilder().withCountOnly(true).build()) - .get(DEFAULT_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS) - .getCount() > 0; - }, retryPolicy); - } catch (Exception e) { - throw new IllegalStateException(e.getMessage(), e); - } - } - - /** - * only internal use only, maybe change in the future - */ - protected Long find(String path) { - try { - return RetryLoops.invokeWithRetry( - () -> { - requiredNotNull(client, failed); - return client.getKVClient() - .get(ByteSequence.from(path, UTF_8)) - .get(DEFAULT_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS) - .getKvs().stream() - .mapToLong(keyValue -> Long.valueOf(keyValue.getValue().toString(UTF_8))) - .findFirst().getAsLong(); - }, retryPolicy); - } catch (Exception e) { - throw new IllegalStateException(e.getMessage(), e); - } - } - - public void createPersistent(String path) { - try { - RetryLoops.invokeWithRetry( - (Callable) () -> { - requiredNotNull(client, failed); - client.getKVClient() - .put(ByteSequence.from(path, UTF_8), - ByteSequence.from(String.valueOf(path.hashCode()), UTF_8)) - .get(DEFAULT_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS); - return null; - }, retryPolicy); - } catch (Exception e) { - throw new IllegalStateException(e.getMessage(), e); - } - } - - /** - * create new ephemeral path save to etcd . - * if node disconnect from etcd, it will be deleted - * automatically by etcd when sessian timeout. - * - * @param path the path to be saved - * @return the lease of current path. - */ - public long createEphemeral(String path) { - try { - return RetryLoops.invokeWithRetry( - () -> { - requiredNotNull(client, failed); - - registeredPaths.add(path); - keepAlive(); - final long leaseId = globalLeaseId; - client.getKVClient() - .put(ByteSequence.from(path, UTF_8) - , ByteSequence.from(String.valueOf(leaseId), UTF_8) - , PutOption.newBuilder().withLeaseId(leaseId).build()) - .get(DEFAULT_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS); - return leaseId; - }, retryPolicy); - } catch (Exception e) { - throw new IllegalStateException(e.getMessage(), e); - } - } - - // easy for mock - public void keepAlive(long lease) { - this.keepAlive(lease, null); - } - - @SuppressWarnings("unchecked") - private void keepAlive(long lease, Consumer onFailed) { - final StreamObserver observer = new Observers.Builder() - .onError((e) -> { - if (e instanceof EtcdException) { - EtcdException error = (EtcdException) e; - /** - * ttl has expired - */ - if (error.getErrorCode() == ErrorCode.NOT_FOUND) { - keepAlive0(onFailed); - } - } - }).onCompleted(() -> { - /** - * deadline reached. - */ - keepAlive0(onFailed); - }).build(); - - /** - * If there is already a keepalive, cancel first - */ - cancelKeepAlive(); - - /** - * create and set new keepAlive to globalKeepAliveRef - */ - this.keepAlive = client.getLeaseClient().keepAlive(lease, observer); - } - - private void keepAlive() throws Exception { - if (keepAlive == null) { - synchronized (this) { - if (keepAlive == null) { - this.globalLeaseId = client.getLeaseClient() - .grant(expirePeriod) - .get(DEFAULT_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS) - .getID(); - /** - * If the keepAlive expires, the registration will be re-attempted - */ - keepAlive(globalLeaseId, (NULL) -> recovery()); - } - } - } - } - - private void keepAlive0(Consumer onFailed) { - if (onFailed != null) { - - /** - * The following two scenarios will cause the keep-alive failure: - * - * 1. Service is offline - * 2. Local deadline check expired - * - * The multiplex lease cannot update the local deadline, - * causing the extreme scene service to be dropped. - * - */ - long leaseId = globalLeaseId; - try { - if (logger.isWarnEnabled()) { - logger.warn("Failed to keep alive for global lease '" + leaseId + "', waiting for retry again."); - } - onFailed.accept(null); - } catch (Exception ignored) { - logger.warn("Failed to recover from global lease expired or lease deadline exceeded. lease '" + leaseId + "'", ignored); - } - } - } - - private void recovery() { - - try { - /** - * The client is processing reconnection - */ - if (cancelKeepAlive) return; - - cancelKeepAlive(); - - Set ephemeralPaths = new HashSet(registeredPaths); - if (!ephemeralPaths.isEmpty()) { - for (String path : ephemeralPaths) { - try { - - /** - * The client is processing reconnection, - * cancel remaining service registration - */ - if (cancelKeepAlive) return; - - createEphemeral(path); - failedRegistered.remove(path); - } catch (Exception e) { - - /** - * waiting for retry again - */ - failedRegistered.add(path); - - Status status = Status.fromThrowable(e); - if (status.getCode() == Status.Code.NOT_FOUND) { - cancelKeepAlive(); - } - } - } - } - } catch (Throwable t) { - logger.warn("Unexpected error, failed to recover from global lease expired or deadline exceeded.", t); - } - } - - public void delete(String path) { - try { - RetryLoops.invokeWithRetry( - (Callable) () -> { - requiredNotNull(client, failed); - client.getKVClient() - .delete(ByteSequence.from(path, UTF_8)) - .get(DEFAULT_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS); - registeredPaths.remove(path); - return null; - }, retryPolicy); - } catch (Exception e) { - throw new IllegalStateException(e.getMessage(), e); - } finally { - /** - * Cancel retry - */ - failedRegistered.remove(path); - } - } - - public String[] endPoints(String backupAddress) { - String[] endpoints = backupAddress.split(Constants.COMMA_SEPARATOR); - List addresses = Arrays.stream(endpoints) - .map(address -> address.contains(Constants.HTTP_SUBFIX_KEY) - ? address - : Constants.HTTP_KEY + address) - .collect(toList()); - Collections.shuffle(addresses); - return addresses.toArray(new String[0]); - } - - /** - * because jetcd's connection change callback not supported yet, we must - * loop to test if connect or disconnect event happend or not. It will be changed - * in the future if we found better choice. - */ - public void start() { - if (!started) { - try { - this.client = completableFuture.get(expirePeriod, TimeUnit.SECONDS); - this.connectState = isConnected(); - this.started = true; - } catch (Throwable t) { - logger.error("Timeout! etcd3 server can not be connected in : " + expirePeriod + " seconds! url: " + url, t); - - completableFuture.whenComplete((c, e) -> { - this.client = c; - if (e != null) { - logger.error("Got an exception when trying to create etcd3 instance, can not connect to etcd3 server, url: " + url, e); - } - }); - - } - - try { - this.future = reconnectNotify.scheduleWithFixedDelay(() -> { - boolean connected = isConnected(); - if (connectState != connected) { - int notifyState = connected ? StateListener.CONNECTED : StateListener.DISCONNECTED; - if (connectionStateListener != null) { - try { - if (connected) { - clearKeepAlive(); - } - connectionStateListener.stateChanged(getClient(), notifyState); - } finally { - cancelKeepAlive = false; - } - } - connectState = connected; - } - }, Constants.DEFAULT_REGISTRY_RECONNECT_PERIOD, Constants.DEFAULT_REGISTRY_RECONNECT_PERIOD, TimeUnit.MILLISECONDS); - } catch (Throwable t) { - logger.error("monitor reconnect status failed.", t); - } - } - } - - private void cancelKeepAlive() { - try { - if (keepAlive != null) { - keepAlive.close(); - } - } finally { - // help for gc - keepAlive = null; - } - } - - private void clearKeepAlive() { - cancelKeepAlive = true; - failedRegistered.clear(); - cancelKeepAlive(); - } - - protected void doClose() { - - try { - cancelKeepAlive = true; - if (globalLeaseId != 0) { - revokeLease(this.globalLeaseId); - } - } catch (Exception e) { - logger.warn("revoke global lease '" + globalLeaseId + "' failed, registry: " + url, e); - } - - try { - if (started && future != null) { - started = false; - future.cancel(true); - reconnectNotify.shutdownNow(); - } - } catch (Exception e) { - logger.warn("stop reconnect Notify failed, registry: " + url, e); - } - - try { - retryFuture.cancel(true); - retryExecutor.shutdownNow(); - } catch (Throwable t) { - logger.warn(t.getMessage(), t); - } - - if (getClient() != null) getClient().close(); - } - - /** - * try get client's shared channel, becase all fields is private on jetcd, - * we must using it by reflect, in the future, jetcd may provider better tools. - * - * @param client get channel from current client - * @return current connection channel - */ - private ManagedChannel newChannel(Client client) { - try { - Field connectionField = client.getClass().getDeclaredField("connectionManager"); - if (!connectionField.isAccessible()) { - connectionField.setAccessible(true); - } - Object connection = connectionField.get(client); - Method channel = connection.getClass().getDeclaredMethod("getChannel"); - if (!channel.isAccessible()) { - channel.setAccessible(true); - } - return (ManagedChannel) channel.invoke(connection); - } catch (Exception e) { - throw new RuntimeException("Failed to obtain connection channel from " + url.getBackupAddress(), e); - } - } - - public ConnectionStateListener getConnectionStateListener() { - return connectionStateListener; - } - - public void setConnectionStateListener(ConnectionStateListener connectionStateListener) { - this.connectionStateListener = connectionStateListener; - } - - public static void requiredNotNull(Object obj, RuntimeException exeception) { - if (obj == null) { - throw exeception; - } - } - - public String getKVValue(String key) { - if (null == key) { - return null; - } - - CompletableFuture responseFuture = this.client.getKVClient().get(ByteSequence.from(key, UTF_8)); - - try { - List result = responseFuture.get(DEFAULT_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS).getKvs(); - if (!result.isEmpty()) { - return result.get(0).getValue().toString(UTF_8); - } - } catch (Exception e) { - // ignore - } - - return null; - } - - - public boolean put(String key, String value) { - if (key == null || value == null) { - return false; - } - CompletableFuture putFuture = - this.client.getKVClient().put(ByteSequence.from(key, UTF_8), ByteSequence.from(value, UTF_8)); - try { - putFuture.get(DEFAULT_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS); - return true; - } catch (Exception e) { - // ignore - } - return false; - } - - private void retry() { - if (!failedRegistered.isEmpty()) { - Set failed = new HashSet(failedRegistered); - if (!failed.isEmpty()) { - - if (cancelKeepAlive) return; - - if (logger.isWarnEnabled()) { - logger.warn("Retry failed register(keep alive) for path '" + failed - + "', path size: " + failed.size()); - } - try { - for (String path : failed) { - try { - - /** - * Is it currently reconnecting ? - */ - if (cancelKeepAlive) return; - - createEphemeral(path); - failedRegistered.remove(path); - } catch (Throwable e) { - - failedRegistered.add(path); - - Status status = Status.fromThrowable(e); - if (status.getCode() == Status.Code.NOT_FOUND) { - cancelKeepAlive(); - } - - logger.warn("Failed to retry register(keep alive) for path '" + path + "', waiting for again, cause: " + e.getMessage(), e); - } - } - } catch (Throwable t) { - logger.warn("Failed to retry register(keep alive) for path '" + failed + "', waiting for again, cause: " + t.getMessage(), t); - } - } - } - } - - /** - * default request timeout - */ - public static final long DEFAULT_REQUEST_TIMEOUT = obtainRequestTimeout(); - - public static final int DEFAULT_INBOUND_SIZE = 100 * 1024 * 1024; - - public static final String GRPC_MAX_INBOUND_SIZE_KEY = "grpc.max.inbound.size"; - - public static final String ETCD_REQUEST_TIMEOUT_KEY = "etcd.request.timeout"; - - private static int obtainRequestTimeout() { - if (StringUtils.isNotEmpty(System.getProperty(ETCD_REQUEST_TIMEOUT_KEY))) { - return Integer.valueOf(System.getProperty(ETCD_REQUEST_TIMEOUT_KEY)); - } - /** - * 10 seconds. - */ - return 10 * 1000; - } -} \ No newline at end of file diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/RetryLoops.java b/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/RetryLoops.java deleted file mode 100644 index cf8617c3ab2..00000000000 --- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/RetryLoops.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * 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. - */ - -/* - * 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 org.apache.dubbo.remoting.etcd.jetcd; - -import io.grpc.Status; -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.remoting.etcd.RetryPolicy; -import org.apache.dubbo.remoting.etcd.option.OptionUtil; - -import java.util.concurrent.Callable; - -public class RetryLoops { - - private final long startTimeMs = System.currentTimeMillis(); - private boolean isDone = false; - private int retriedCount = 0; - private Logger logger = LoggerFactory.getLogger(RetryLoops.class); - - public static R invokeWithRetry(Callable task, RetryPolicy retryPolicy) throws Exception { - R result = null; - RetryLoops retryLoop = new RetryLoops(); - while (retryLoop.shouldContinue()) { - try { - result = task.call(); - retryLoop.complete(); - } catch (Exception e) { - retryLoop.fireException(e, retryPolicy); - } - } - return result; - } - - public void fireException(Exception e, RetryPolicy retryPolicy) throws Exception { - - if (e instanceof InterruptedException) Thread.currentThread().interrupt(); - - boolean rethrow = true; - if (isRetryException(e) - && retryPolicy.shouldRetry(retriedCount++, System.currentTimeMillis() - startTimeMs, true)) { - rethrow = false; - } - - if (rethrow) { - throw e; - } - } - - private boolean isRetryException(Throwable e) { - Status status = Status.fromThrowable(e); - if (OptionUtil.isRecoverable(status)) return true; - - return false; - } - - public boolean shouldContinue() { - return !isDone; - } - - public void complete() { - isDone = true; - } - -} diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/RetryNTimes.java b/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/RetryNTimes.java deleted file mode 100644 index 745320830ca..00000000000 --- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/RetryNTimes.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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 org.apache.dubbo.remoting.etcd.jetcd; - -import org.apache.dubbo.remoting.etcd.AbstractRetryPolicy; - -import java.util.concurrent.TimeUnit; - -public class RetryNTimes extends AbstractRetryPolicy { - - private final long sleepMilliseconds; - - public RetryNTimes(int maxRetried, int sleepTime, TimeUnit unit) { - super(maxRetried); - this.sleepMilliseconds = unit.convert(sleepTime, TimeUnit.MILLISECONDS); - } - - @Override - protected long getSleepTime(int retried, long elapsed) { - return sleepMilliseconds; - } -} diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/option/OptionUtil.java b/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/option/OptionUtil.java deleted file mode 100644 index 609f289e91c..00000000000 --- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/option/OptionUtil.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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 org.apache.dubbo.remoting.etcd.option; - -import io.etcd.jetcd.ByteSequence; -import io.grpc.Status; -import io.netty.handler.codec.http2.Http2Exception; - -import java.util.Arrays; - -public class OptionUtil { - - public static final byte[] NO_PREFIX_END = {0}; - - public static final ByteSequence prefixEndOf(ByteSequence prefix) { - byte[] endKey = prefix.getBytes().clone(); - for (int i = endKey.length - 1; i >= 0; i--) { - if (endKey[i] < 0xff) { - endKey[i] = (byte) (endKey[i] + 1); - return ByteSequence.from(Arrays.copyOf(endKey, i + 1)); - } - } - - return ByteSequence.from(NO_PREFIX_END); - } - - public static boolean isRecoverable(Status status) { - return isHaltError(status) - || isNoLeaderError(status) - // ephemeral is expired - || status.getCode() == Status.Code.NOT_FOUND; - } - - public static boolean isHaltError(Status status) { - // Unavailable codes mean the system will be right back. - // (e.g., can't connect, lost leader) - // Treat Internal codes as if something failed, leaving the - // system in an inconsistent state, but retrying could make progress. - // (e.g., failed in middle of send, corrupted frame) - return status.getCode() != Status.Code.UNAVAILABLE && status.getCode() != Status.Code.INTERNAL; - } - - public static boolean isNoLeaderError(Status status) { - return status.getCode() == Status.Code.UNAVAILABLE - && "etcdserver: no leader".equals(status.getDescription()); - } - - public static boolean isProtocolError(Throwable e) { - if (e == null) return false; - Throwable cause = e.getCause(); - while (cause != null) { - if (cause instanceof Http2Exception) { - Http2Exception t = (Http2Exception) cause; - if ("PROTOCOL_ERROR".equals(t.error().name())) { - return true; - } - } - cause = cause.getCause(); - } - return false; - } -} diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/support/AbstractEtcdClient.java b/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/support/AbstractEtcdClient.java deleted file mode 100644 index 5fecd14d395..00000000000 --- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/support/AbstractEtcdClient.java +++ /dev/null @@ -1,194 +0,0 @@ -/* - * 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. - */ - -/* - * 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 org.apache.dubbo.remoting.etcd.support; - -import org.apache.dubbo.common.Constants; -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.ConcurrentHashSet; -import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.remoting.etcd.ChildListener; -import org.apache.dubbo.remoting.etcd.EtcdClient; -import org.apache.dubbo.remoting.etcd.StateListener; - -import java.util.Arrays; -import java.util.List; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - -public abstract class AbstractEtcdClient implements EtcdClient { - - protected static final Logger logger = LoggerFactory.getLogger(AbstractEtcdClient.class); - - private final URL url; - - private final Set stateListeners = new ConcurrentHashSet<>(); - - private final ConcurrentMap> childListeners = new ConcurrentHashMap<>(); - private final List categroies = Arrays.asList(Constants.PROVIDERS_CATEGORY - , Constants.CONSUMERS_CATEGORY - , Constants.ROUTERS_CATEGORY - , Constants.CONFIGURATORS_CATEGORY); - private volatile boolean closed = false; - - public AbstractEtcdClient(URL url) { - this.url = url; - } - - public URL getUrl() { - return url; - } - - public void create(String path) { - String fixedPath = fixNamespace(path); - createParentIfAbsent(fixedPath); - doCreatePersistent(fixedPath); - } - - public long createEphemeral(String path) { - String fixedPath = fixNamespace(path); - createParentIfAbsent(fixedPath); - return doCreateEphemeral(path); - } - - public void addStateListener(StateListener listener) { - stateListeners.add(listener); - } - - public void removeStateListener(StateListener listener) { - stateListeners.remove(listener); - } - - public Set getSessionListeners() { - return stateListeners; - } - - public List addChildListener(String path, final ChildListener listener) { - ConcurrentMap listeners = childListeners.get(path); - if (listeners == null) { - childListeners.putIfAbsent(path, new ConcurrentHashMap<>()); - listeners = childListeners.get(path); - } - WatcherListener targetListener = listeners.get(listener); - if (targetListener == null) { - listeners.putIfAbsent(listener, createChildWatcherListener(path, listener)); - targetListener = listeners.get(listener); - } - return addChildWatcherListener(path, targetListener); - } - - public WatcherListener getChildListener(String path, ChildListener listener) { - ConcurrentMap listeners = childListeners.get(path); - if (listeners == null) { - return null; - } - WatcherListener targetListener = listeners.get(listener); - if (targetListener == null) { - listeners.putIfAbsent(listener, createChildWatcherListener(path, listener)); - targetListener = listeners.get(listener); - } - return targetListener; - } - - public void removeChildListener(String path, ChildListener listener) { - ConcurrentMap listeners = childListeners.get(path); - if (listeners != null) { - WatcherListener targetListener = listeners.remove(listener); - if (targetListener != null) { - removeChildWatcherListener(path, targetListener); - } - } - } - - protected void stateChanged(int state) { - for (StateListener sessionListener : getSessionListeners()) { - sessionListener.stateChanged(state); - } - } - - protected String fixNamespace(String path) { - if (StringUtils.isEmpty(path)) { - throw new IllegalArgumentException("path is required, actual null or ''"); - } - return (path.charAt(0) != '/') ? (Constants.PATH_SEPARATOR + path) : path; - } - - protected void createParentIfAbsent(String fixedPath) { - int i = fixedPath.lastIndexOf('/'); - if (i > 0) { - String parentPath = fixedPath.substring(0, i); - if (categroies.stream().anyMatch(c -> fixedPath.endsWith(c))) { - if (!checkExists(parentPath)) { - this.doCreatePersistent(parentPath); - } - } else if (categroies.stream().anyMatch(c -> parentPath.endsWith(c))) { - String grandfather = parentPath.substring(0, parentPath.lastIndexOf('/')); - if (!checkExists(grandfather)) { - this.doCreatePersistent(grandfather); - } - } - } - } - - public void close() { - if (closed) { - return; - } - closed = true; - try { - doClose(); - } catch (Throwable t) { - logger.warn(t.getMessage(), t); - } - } - - public abstract void doClose(); - - public abstract void doCreatePersistent(String path); - - public abstract long doCreateEphemeral(String path); - - public abstract void delete(String path); - - public abstract boolean checkExists(String path); - - public abstract WatcherListener createChildWatcherListener(String path, ChildListener listener); - - public abstract List addChildWatcherListener(String path, WatcherListener listener); - - public abstract void removeChildWatcherListener(String path, WatcherListener listener); - -} diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.etcd.EtcdTransporter b/dubbo-remoting/dubbo-remoting-etcd3/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.etcd.EtcdTransporter deleted file mode 100644 index d10733a0dd5..00000000000 --- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.etcd.EtcdTransporter +++ /dev/null @@ -1 +0,0 @@ -jetcd=org.apache.dubbo.remoting.etcd.jetcd.JEtcdTransporter \ No newline at end of file diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/test/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientTest.java b/dubbo-remoting/dubbo-remoting-etcd3/src/test/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientTest.java deleted file mode 100644 index 9674feec35d..00000000000 --- a/dubbo-remoting/dubbo-remoting-etcd3/src/test/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientTest.java +++ /dev/null @@ -1,426 +0,0 @@ -/* - * 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. - */ - -/* - * 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 org.apache.dubbo.remoting.etcd.jetcd; - -import com.google.protobuf.ByteString; -import io.etcd.jetcd.ByteSequence; -import io.etcd.jetcd.Client; -import io.etcd.jetcd.Watch; -import io.etcd.jetcd.api.Event; -import io.etcd.jetcd.api.WatchCancelRequest; -import io.etcd.jetcd.api.WatchCreateRequest; -import io.etcd.jetcd.api.WatchGrpc; -import io.etcd.jetcd.api.WatchRequest; -import io.etcd.jetcd.api.WatchResponse; -import io.etcd.jetcd.common.exception.ClosedClientException; -import io.etcd.jetcd.watch.WatchEvent; -import io.grpc.ManagedChannel; -import io.grpc.Status; -import io.grpc.stub.StreamObserver; -import org.apache.dubbo.common.Constants; -import org.apache.dubbo.common.URL; -import org.apache.dubbo.remoting.etcd.ChildListener; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicLong; - -import static java.nio.charset.StandardCharsets.UTF_8; - -@Disabled -public class JEtcdClientTest { - - JEtcdClient client; - - @Test - public void test_watch_when_create_path() throws InterruptedException { - - String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers"; - String child = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers/demoService1"; - - final CountDownLatch notNotified = new CountDownLatch(1); - - ChildListener childListener = (parent, children) -> { - Assertions.assertEquals(1, children.size()); - Assertions.assertEquals(child.substring(child.lastIndexOf("/") + 1), children.get(0)); - notNotified.countDown(); - }; - - client.addChildListener(path, childListener); - - client.createEphemeral(child); - Assertions.assertTrue(notNotified.await(10, TimeUnit.SECONDS)); - - client.removeChildListener(path, childListener); - client.delete(child); - } - - @Test - public void test_watch_when_modify() { - String path = "/dubbo/config/jetcd-client-unit-test/configurators"; - String endpoint = "http://127.0.0.1:2379"; - CountDownLatch latch = new CountDownLatch(1); - ByteSequence key = ByteSequence.from(path, UTF_8); - - Watch.Listener listener = Watch.listener(response -> { - for (WatchEvent event : response.getEvents()) { - Assertions.assertEquals("PUT", event.getEventType().toString()); - Assertions.assertEquals(path, event.getKeyValue().getKey().toString(UTF_8)); - Assertions.assertEquals("Hello", event.getKeyValue().getValue().toString(UTF_8)); - latch.countDown(); - } - - }); - - try (Client client = Client.builder().endpoints(endpoint).build(); - Watch watch = client.getWatchClient(); - Watch.Watcher watcher = watch.watch(key, listener)) { - // try to modify the key - client.getKVClient().put(ByteSequence.from(path, UTF_8), ByteSequence.from("Hello", UTF_8)); - latch.await(); - } catch (Exception e) { - Assertions.fail(e.getMessage()); - } - } - - @Test - public void testWatchWithGrpc() { - String path = "/dubbo/config/test_watch_with_grpc/configurators"; - String endpoint = "http://127.0.0.1:2379"; - CountDownLatch latch = new CountDownLatch(1); - try (Client client = Client.builder().endpoints(endpoint).build()) { - ManagedChannel channel = getChannel(client); - StreamObserver observer = WatchGrpc.newStub(channel).watch(new StreamObserver() { - @Override - public void onNext(WatchResponse response) { - for (Event event : response.getEventsList()) { - Assertions.assertEquals("PUT", event.getType().toString()); - Assertions.assertEquals(path, event.getKv().getKey().toString(UTF_8)); - Assertions.assertEquals("Hello", event.getKv().getValue().toString(UTF_8)); - latch.countDown(); - } - } - - @Override - public void onError(Throwable throwable) { - - } - - @Override - public void onCompleted() { - - } - }); - WatchCreateRequest.Builder builder = WatchCreateRequest.newBuilder() - .setKey(ByteString.copyFrom(path, UTF_8)); - - observer.onNext(WatchRequest.newBuilder().setCreateRequest(builder).build()); - - // try to modify the key - client.getKVClient().put(ByteSequence.from(path, UTF_8), ByteSequence.from("Hello", UTF_8)); - latch.await(5, TimeUnit.SECONDS); - } catch (Exception e) { - Assertions.fail(e.getMessage()); - } - } - - @Test - public void testCancelWatchWithGrpc() { - String path = "/dubbo/config/testCancelWatchWithGrpc/configurators"; - String endpoint = "http://127.0.0.1:2379"; - CountDownLatch updateLatch = new CountDownLatch(1); - CountDownLatch cancelLatch = new CountDownLatch(1); - final AtomicLong watchID = new AtomicLong(-1L); - try (Client client = Client.builder().endpoints(endpoint).build()) { - ManagedChannel channel = getChannel(client); - StreamObserver observer = WatchGrpc.newStub(channel).watch(new StreamObserver() { - @Override - public void onNext(WatchResponse response) { - watchID.set(response.getWatchId()); - for (Event event : response.getEventsList()) { - Assertions.assertEquals("PUT", event.getType().toString()); - Assertions.assertEquals(path, event.getKv().getKey().toString(UTF_8)); - Assertions.assertEquals("Hello", event.getKv().getValue().toString(UTF_8)); - updateLatch.countDown(); - } - if (response.getCanceled()) { - // received the cancel response - cancelLatch.countDown(); - } - } - - @Override - public void onError(Throwable throwable) { - - } - - @Override - public void onCompleted() { - - } - }); - // create - WatchCreateRequest.Builder builder = WatchCreateRequest.newBuilder() - .setKey(ByteString.copyFrom(path, UTF_8)); - - // make the grpc call to watch the key - observer.onNext(WatchRequest.newBuilder().setCreateRequest(builder).build()); - - // try to put the value - client.getKVClient().put(ByteSequence.from(path, UTF_8), ByteSequence.from("Hello", UTF_8)); - - // response received, latch counts down to zero - updateLatch.await(); - - WatchCancelRequest watchCancelRequest = - WatchCancelRequest.newBuilder().setWatchId(watchID.get()).build(); - WatchRequest cancelRequest = WatchRequest.newBuilder() - .setCancelRequest(watchCancelRequest).build(); - observer.onNext(cancelRequest); - - // try to put the value - client.getKVClient().put(ByteSequence.from(path, UTF_8), ByteSequence.from("Hello world", UTF_8)); - - cancelLatch.await(); - } catch (Exception e) { - Assertions.fail(e.getMessage()); - } - - } - - @Test - public void test_watch_when_create_wrong_path() throws InterruptedException { - - String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers"; - String child = "/dubbo/com.alibaba.dubbo.demo.DemoService/routers/demoService1"; - - final CountDownLatch notNotified = new CountDownLatch(1); - - ChildListener childListener = (parent, children) -> { - Assertions.assertEquals(1, children.size()); - Assertions.assertEquals(child, children.get(0)); - notNotified.countDown(); - }; - - client.addChildListener(path, childListener); - - client.createEphemeral(child); - Assertions.assertFalse(notNotified.await(1, TimeUnit.SECONDS)); - - client.removeChildListener(path, childListener); - client.delete(child); - } - - @Test - public void test_watch_when_delete_path() throws InterruptedException { - - String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers"; - String child = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers/demoService1"; - - final CountDownLatch notNotified = new CountDownLatch(1); - - ChildListener childListener = (parent, children) -> { - Assertions.assertEquals(0, children.size()); - notNotified.countDown(); - }; - - client.createEphemeral(child); - - client.addChildListener(path, childListener); - client.delete(child); - - Assertions.assertTrue(notNotified.await(10, TimeUnit.SECONDS)); - client.removeChildListener(path, childListener); - } - - @Test - public void test_watch_then_unwatch() throws InterruptedException { - - String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers"; - String child = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers/demoService2"; - - final CountDownLatch notNotified = new CountDownLatch(1); - final CountDownLatch notTwiceNotified = new CountDownLatch(2); - - final Holder notified = new Holder(); - - ChildListener childListener = (parent, children) -> { - Assertions.assertEquals(1, children.size()); - Assertions.assertEquals(child.substring(child.lastIndexOf("/") + 1), children.get(0)); - notNotified.countDown(); - notTwiceNotified.countDown(); - notified.getAndIncrease(); - }; - - client.addChildListener(path, childListener); - - client.createEphemeral(child); - Assertions.assertTrue(notNotified.await(15, TimeUnit.SECONDS)); - - client.removeChildListener(path, childListener); - client.delete(child); - - Assertions.assertFalse(notTwiceNotified.await(5, TimeUnit.SECONDS)); - Assertions.assertEquals(1, notified.value); - client.delete(child); - } - - @Test - public void test_watch_on_unrecoverable_connection() throws InterruptedException { - - String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers"; - JEtcdClient.EtcdWatcher watcher = null; - try { - ChildListener childListener = (parent, children) -> { - Assertions.assertEquals(path, parent); - }; - client.addChildListener(path, childListener); - watcher = client.getChildListener(path, childListener); - watcher.watchRequest.onError(Status.ABORTED.withDescription("connection error").asRuntimeException()); - - watcher.watchRequest.onNext(watcher.nextRequest()); - } catch (Exception e) { - Assertions.assertTrue(e.getMessage().contains("call was cancelled")); - } - } - - @Test - public void test_watch_on_recoverable_connection() throws InterruptedException { - - String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/connection"; - String child = "/dubbo/com.alibaba.dubbo.demo.DemoService/connection/demoService1"; - - final CountDownLatch notNotified = new CountDownLatch(1); - final CountDownLatch notTwiceNotified = new CountDownLatch(2); - final Holder notified = new Holder(); - ChildListener childListener = (parent, children) -> { - notTwiceNotified.countDown(); - switch (notified.increaseAndGet()) { - case 1: { - notNotified.countDown(); - Assertions.assertTrue(children.size() == 1); - Assertions.assertEquals(child.substring(child.lastIndexOf("/") + 1), children.get(0)); - break; - } - case 2: { - Assertions.assertTrue(children.size() == 0); - Assertions.assertEquals(path, parent); - break; - } - default: - Assertions.fail("two many callback invoked."); - } - }; - - client.addChildListener(path, childListener); - client.createEphemeral(child); - - // make sure first time callback successfully - Assertions.assertTrue(notNotified.await(15, TimeUnit.SECONDS)); - - // connection error causes client to release all resources including current watcher - JEtcdClient.EtcdWatcher watcher = client.getChildListener(path, childListener); - watcher.onError(Status.UNAVAILABLE.withDescription("temporary connection issue").asRuntimeException()); - - // trigger delete after unavailable - client.delete(child); - Assertions.assertTrue(notTwiceNotified.await(15, TimeUnit.SECONDS)); - - client.removeChildListener(path, childListener); - } - - @Test - public void test_watch_after_client_closed() throws InterruptedException { - - String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers"; - client.close(); - - try { - client.addChildListener(path, (parent, children) -> { - Assertions.assertEquals(path, parent); - }); - } catch (ClosedClientException e) { - Assertions.assertEquals("watch client has been closed, path '" + path + "'", e.getMessage()); - } - } - - @BeforeEach - public void setUp() { - // timeout in 15 seconds. - URL url = URL.valueOf("etcd3://127.0.0.1:2379/com.alibaba.dubbo.registry.RegistryService") - .addParameter(Constants.SESSION_TIMEOUT_KEY, 15000); - - client = new JEtcdClient(url); - } - - @AfterEach - public void tearDown() { - client.close(); - } - - static class Holder { - - volatile int value; - - synchronized int getAndIncrease() { - return value++; - } - - synchronized int increaseAndGet() { - return ++value; - } - } - - private ManagedChannel getChannel(Client client) { - try { - // hack, use reflection to get the shared channel. - Field connectionField = client.getClass().getDeclaredField("connectionManager"); - connectionField.setAccessible(true); - Object connection = connectionField.get(client); - Method channelMethod = connection.getClass().getDeclaredMethod("getChannel"); - channelMethod.setAccessible(true); - ManagedChannel channel = (ManagedChannel) channelMethod.invoke(connection); - return channel; - } catch (Exception e) { - return null; - } - } -} diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/test/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientWrapperTest.java b/dubbo-remoting/dubbo-remoting-etcd3/src/test/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientWrapperTest.java deleted file mode 100644 index b7d2671e435..00000000000 --- a/dubbo-remoting/dubbo-remoting-etcd3/src/test/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientWrapperTest.java +++ /dev/null @@ -1,186 +0,0 @@ -/* - * 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. - */ - -/* - * 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 org.apache.dubbo.remoting.etcd.jetcd; - -import org.apache.dubbo.common.Constants; -import org.apache.dubbo.common.URL; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; - -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; -import java.util.concurrent.locks.LockSupport; - -import static org.mockito.ArgumentMatchers.anyLong; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.spy; - -@Disabled -public class JEtcdClientWrapperTest { - - JEtcdClientWrapper clientWrapper; - - @Test - public void test_path_exists() { - String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers"; - clientWrapper.createPersistent(path); - Assertions.assertTrue(clientWrapper.checkExists(path)); - Assertions.assertFalse(clientWrapper.checkExists(path + "/noneexits")); - clientWrapper.delete(path); - } - - @Test - public void test_create_emerphal_path() { - String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers"; - clientWrapper.createEphemeral(path); - Assertions.assertTrue(clientWrapper.checkExists(path)); - clientWrapper.delete(path); - } - - @Test - public void test_grant_lease_then_revoke() { - long lease = clientWrapper.createLease(1); - clientWrapper.revokeLease(lease); - - long newLease = clientWrapper.createLease(1); - LockSupport.parkNanos(this, TimeUnit.SECONDS.toNanos(2)); - // test timeout of lease - clientWrapper.revokeLease(newLease); - } - - @Test - public void test_create_emerphal_path_then_timeout() { - String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers"; - - URL url = URL.valueOf("etcd3://127.0.0.1:2379/org.apache.dubbo.registry.RegistryService") - .addParameter(Constants.SESSION_TIMEOUT_KEY, 1000); - - JEtcdClientWrapper saved = clientWrapper; - - try { - clientWrapper = spy(new JEtcdClientWrapper(url)); - clientWrapper.start(); - - doAnswer(new Answer() { - int timeout; - - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - LockSupport.parkNanos(this, TimeUnit.SECONDS.toNanos(2)); - if (timeout++ > 0) { - throw new TimeoutException(); - } - return null; - } - }).when(clientWrapper).keepAlive(anyLong()); - - try { - clientWrapper.createEphemeral(path); - } catch (IllegalStateException ex) { - Assertions.assertEquals("failed to create ephereral by path '" + path + "'", ex.getMessage()); - } - - } finally { - clientWrapper.doClose(); - clientWrapper = saved; - } - } - - @Test - public void test_get_emerphal_children_path() { - String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers"; - String[] children = { - "/dubbo/org.apache.dubbo.demo.DemoService/providers/service1" - , "/dubbo/org.apache.dubbo.demo.DemoService/providers/service2" - , "/dubbo/org.apache.dubbo.demo.DemoService/providers/service3" - , "/dubbo/org.apache.dubbo.demo.DemoService/providers/service4" - , "/dubbo/org.apache.dubbo.demo.DemoService/providers/service5/exclude" - }; - - Arrays.stream(children).forEach((child) -> { - Assertions.assertFalse(clientWrapper.checkExists(child)); - clientWrapper.createEphemeral(child); - }); - - List extected = clientWrapper.getChildren(path); - - Assertions.assertEquals(4, extected.size()); - extected.stream().forEach((child) -> { - boolean found = false; - for (int i = 0; i < children.length; ++i) { - if (child.equals(children[i])) { - found = true; - break; - } - } - Assertions.assertTrue(found); - clientWrapper.delete(child); - }); - } - - @Test - public void test_connect_cluster() { - URL url = URL.valueOf("etcd3://127.0.0.1:22379/org.apache.dubbo.registry.RegistryService?backup=127.0.0.1:2379,127.0.0.1:32379"); - JEtcdClientWrapper clientWrapper = new JEtcdClientWrapper(url); - try { - clientWrapper.start(); - String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers"; - clientWrapper.createEphemeral(path); - Assertions.assertTrue(clientWrapper.checkExists(path)); - Assertions.assertFalse(clientWrapper.checkExists(path + "/noneexits")); - clientWrapper.delete(path); - } finally { - clientWrapper.doClose(); - } - } - - @BeforeEach - public void setUp() { - URL url = URL.valueOf("etcd3://127.0.0.1:2379/org.apache.dubbo.registry.RegistryService"); - clientWrapper = new JEtcdClientWrapper(url); - clientWrapper.start(); - } - - @AfterEach - public void tearDown() { - clientWrapper.doClose(); - } -} diff --git a/dubbo-remoting/dubbo-remoting-grizzly/pom.xml b/dubbo-remoting/dubbo-remoting-grizzly/pom.xml index f740422a7ae..6c57b7312c3 100644 --- a/dubbo-remoting/dubbo-remoting-grizzly/pom.xml +++ b/dubbo-remoting/dubbo-remoting-grizzly/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-remoting - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-remoting-grizzly jar diff --git a/dubbo-remoting/dubbo-remoting-grizzly/src/main/test/org/apache/dubbo/remoting/transport/grizzly/GrizzlyTransporterTest.java b/dubbo-remoting/dubbo-remoting-grizzly/src/main/test/org/apache/dubbo/remoting/transport/grizzly/GrizzlyTransporterTest.java index 3fb0dde3e42..e11c73267dc 100644 --- a/dubbo-remoting/dubbo-remoting-grizzly/src/main/test/org/apache/dubbo/remoting/transport/grizzly/GrizzlyTransporterTest.java +++ b/dubbo-remoting/dubbo-remoting-grizzly/src/main/test/org/apache/dubbo/remoting/transport/grizzly/GrizzlyTransporterTest.java @@ -21,10 +21,10 @@ import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.remoting.Server; import org.apache.dubbo.remoting.transport.ChannelHandlerAdapter; -import org.junit.jupiter.api.Test; +import org.junit.Test; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class GrizzlyTransporterTest { @Test diff --git a/dubbo-remoting/dubbo-remoting-http/pom.xml b/dubbo-remoting/dubbo-remoting-http/pom.xml index 85ea81dff12..755fdd019bd 100644 --- a/dubbo-remoting/dubbo-remoting-http/pom.xml +++ b/dubbo-remoting/dubbo-remoting-http/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-remoting - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-remoting-http jar diff --git a/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/tomcat/TomcatHttpServer.java b/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/tomcat/TomcatHttpServer.java index f8a898c9713..ac050f16ebd 100755 --- a/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/tomcat/TomcatHttpServer.java +++ b/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/tomcat/TomcatHttpServer.java @@ -67,9 +67,6 @@ public TomcatHttpServer(URL url, final HttpHandler handler) { context.addServletMapping("/*", "dispatcher"); ServletManager.getInstance().addServletContext(url.getPort(), context.getServletContext()); - // tell tomcat to fail on startup failures. - System.setProperty("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE", "true"); - try { tomcat.start(); } catch (LifecycleException e) { diff --git a/dubbo-remoting/dubbo-remoting-http/src/test/java/org/apache/dubbo/remoting/http/jetty/JettyHttpBinderTest.java b/dubbo-remoting/dubbo-remoting-http/src/test/java/org/apache/dubbo/remoting/http/jetty/JettyHttpBinderTest.java index 0aca829f2be..4496e579a56 100644 --- a/dubbo-remoting/dubbo-remoting-http/src/test/java/org/apache/dubbo/remoting/http/jetty/JettyHttpBinderTest.java +++ b/dubbo-remoting/dubbo-remoting-http/src/test/java/org/apache/dubbo/remoting/http/jetty/JettyHttpBinderTest.java @@ -22,7 +22,7 @@ import org.apache.dubbo.remoting.http.HttpHandler; import org.apache.dubbo.remoting.http.HttpServer; import org.apache.http.client.fluent.Request; -import org.junit.jupiter.api.Test; +import org.junit.Test; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; diff --git a/dubbo-remoting/dubbo-remoting-http/src/test/java/org/apache/dubbo/remoting/http/tomcat/TomcatHttpBinderTest.java b/dubbo-remoting/dubbo-remoting-http/src/test/java/org/apache/dubbo/remoting/http/tomcat/TomcatHttpBinderTest.java index 9b3e3b16d63..0d38db40e26 100644 --- a/dubbo-remoting/dubbo-remoting-http/src/test/java/org/apache/dubbo/remoting/http/tomcat/TomcatHttpBinderTest.java +++ b/dubbo-remoting/dubbo-remoting-http/src/test/java/org/apache/dubbo/remoting/http/tomcat/TomcatHttpBinderTest.java @@ -21,9 +21,8 @@ import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.remoting.http.HttpHandler; import org.apache.dubbo.remoting.http.HttpServer; - import org.apache.http.client.fluent.Request; -import org.junit.jupiter.api.Test; +import org.junit.Test; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; diff --git a/dubbo-remoting/dubbo-remoting-mina/pom.xml b/dubbo-remoting/dubbo-remoting-mina/pom.xml index a10aacc52d2..c46a6928533 100644 --- a/dubbo-remoting/dubbo-remoting-mina/pom.xml +++ b/dubbo-remoting/dubbo-remoting-mina/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-remoting - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-remoting-mina jar diff --git a/dubbo-remoting/dubbo-remoting-mina/src/main/java/org/apache/dubbo/remoting/transport/mina/MinaServer.java b/dubbo-remoting/dubbo-remoting-mina/src/main/java/org/apache/dubbo/remoting/transport/mina/MinaServer.java index 540213b4443..80e336d7e82 100644 --- a/dubbo-remoting/dubbo-remoting-mina/src/main/java/org/apache/dubbo/remoting/transport/mina/MinaServer.java +++ b/dubbo-remoting/dubbo-remoting-mina/src/main/java/org/apache/dubbo/remoting/transport/mina/MinaServer.java @@ -60,7 +60,7 @@ protected void doOpen() throws Throwable { Executors.newCachedThreadPool(new NamedThreadFactory("MinaServerWorker", true))); // config - SocketAcceptorConfig cfg = acceptor.getDefaultConfig(); + SocketAcceptorConfig cfg = (SocketAcceptorConfig) acceptor.getDefaultConfig(); cfg.setThreadModel(ThreadModel.MANUAL); // set codec. acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaCodecAdapter(getCodec(), getUrl(), this))); diff --git a/dubbo-remoting/dubbo-remoting-mina/src/test/java/org/apache/remoting/transport/mina/ClientToServerTest.java b/dubbo-remoting/dubbo-remoting-mina/src/test/java/org/apache/remoting/transport/mina/ClientToServerTest.java index 987139275d5..30e2cc7f4c2 100644 --- a/dubbo-remoting/dubbo-remoting-mina/src/test/java/org/apache/remoting/transport/mina/ClientToServerTest.java +++ b/dubbo-remoting/dubbo-remoting-mina/src/test/java/org/apache/remoting/transport/mina/ClientToServerTest.java @@ -22,15 +22,16 @@ import org.apache.dubbo.remoting.exchange.ResponseFuture; import org.apache.dubbo.remoting.exchange.support.Replier; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import junit.framework.TestCase; +import org.junit.Test; /** * ClientToServer + * + * */ -public abstract class ClientToServerTest { +public abstract class ClientToServerTest extends TestCase { protected static final String LOCALHOST = "127.0.0.1"; @@ -44,15 +45,17 @@ public abstract class ClientToServerTest { protected abstract ExchangeChannel newClient(int port) throws RemotingException; - @BeforeEach + @Override protected void setUp() throws Exception { + super.setUp(); int port = (int) (1000 * Math.random() + 10000); server = newServer(port, handler); client = newClient(port); } - @AfterEach - protected void tearDown() { + @Override + protected void tearDown() throws Exception { + super.tearDown(); try { if (server != null) server.close(); @@ -66,7 +69,7 @@ protected void tearDown() { public void testFuture() throws Exception { ResponseFuture future = client.request(new World("world")); Hello result = (Hello) future.get(); - Assertions.assertEquals("hello,world", result.getName()); + Assert.assertEquals("hello,world", result.getName()); } // @Test @@ -74,7 +77,7 @@ public void testFuture() throws Exception { // final Object waitter = new Object(); // client.invoke(new World("world"), new InvokeCallback() { // public void callback(Hello result) { -// Assertions.assertEquals("hello,world", result.getName()); +// Assert.assertEquals("hello,world", result.getName()); // synchronized (waitter) { // waitter.notifyAll(); // } diff --git a/dubbo-remoting/dubbo-remoting-mina/src/test/java/org/apache/remoting/transport/mina/ClientsTest.java b/dubbo-remoting/dubbo-remoting-mina/src/test/java/org/apache/remoting/transport/mina/ClientsTest.java index b475d49b0bf..ef7d9566215 100644 --- a/dubbo-remoting/dubbo-remoting-mina/src/test/java/org/apache/remoting/transport/mina/ClientsTest.java +++ b/dubbo-remoting/dubbo-remoting-mina/src/test/java/org/apache/remoting/transport/mina/ClientsTest.java @@ -20,14 +20,13 @@ import org.apache.dubbo.remoting.Transporter; import org.apache.dubbo.remoting.transport.mina.MinaTransporter; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsString; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import static org.junit.matchers.JUnitMatchers.containsString; public class ClientsTest { @@ -41,12 +40,10 @@ public void testGetTransportEmpty() { } } - @Test + @Test(expected = IllegalArgumentException.class) public void testGetTransportNull() { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - String name = null; - ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name); - }); + String name = null; + ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name); } @Test @@ -55,11 +52,9 @@ public void testGetTransport1() { assertEquals(MinaTransporter.class, ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name).getClass()); } - @Test + @Test(expected = IllegalStateException.class) public void testGetTransportWrong() { - Assertions.assertThrows(IllegalStateException.class, () -> { - String name = "nety"; - assertNull(ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name).getClass()); - }); + String name = "nety"; + assertNull(ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name).getClass()); } } diff --git a/dubbo-remoting/dubbo-remoting-netty/pom.xml b/dubbo-remoting/dubbo-remoting-netty/pom.xml index cfa826279e5..2098b6d304a 100644 --- a/dubbo-remoting/dubbo-remoting-netty/pom.xml +++ b/dubbo-remoting/dubbo-remoting-netty/pom.xml @@ -1,25 +1,24 @@ 4.0.0 org.apache.dubbo dubbo-remoting - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-remoting-netty jar diff --git a/dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyBackedChannelBuffer.java b/dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyBackedChannelBuffer.java index 51c697cafa5..3bd911fedf6 100644 --- a/dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyBackedChannelBuffer.java +++ b/dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyBackedChannelBuffer.java @@ -119,7 +119,7 @@ public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) { // careful byte[] data = new byte[length]; buffer.getBytes(srcIndex, data, 0, length); - setBytes(index, data, 0, length); + setBytes(0, data, index, length); } diff --git a/dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyServer.java b/dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyServer.java index 61cd3f1ef0c..f2ca73c3477 100644 --- a/dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyServer.java +++ b/dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyServer.java @@ -20,7 +20,6 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.ExecutorUtil; import org.apache.dubbo.common.utils.NamedThreadFactory; import org.apache.dubbo.common.utils.NetUtils; @@ -107,7 +106,7 @@ protected void doClose() throws Throwable { } try { Collection channels = getChannels(); - if (CollectionUtils.isNotEmpty(channels)) { + if (channels != null && !channels.isEmpty()) { for (org.apache.dubbo.remoting.Channel channel : channels) { try { channel.close(); diff --git a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeartbeatHandlerTest.java b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeartbeatHandlerTest.java index 52c3e9d03e7..7db1fb5743d 100644 --- a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeartbeatHandlerTest.java +++ b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeartbeatHandlerTest.java @@ -29,9 +29,9 @@ import org.apache.dubbo.remoting.exchange.ExchangeServer; import org.apache.dubbo.remoting.exchange.Exchangers; import org.apache.dubbo.remoting.transport.dispatcher.FakeChannelHandlers; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Test; import java.util.concurrent.CompletableFuture; @@ -42,7 +42,7 @@ public class HeartbeatHandlerTest { private ExchangeServer server; private ExchangeClient client; - @AfterEach + @After public void after() throws Exception { if (client != null) { client.close(); @@ -53,9 +53,6 @@ public void after() throws Exception { server.close(); server = null; } - - // wait for timer to finish - Thread.sleep(2000); } @Test @@ -75,7 +72,7 @@ public void testServerHeartbeat() throws Exception { client = Exchangers.connect(serverURL); Thread.sleep(10000); - Assertions.assertTrue(handler.disconnectCount > 0); + Assert.assertTrue(handler.disconnectCount > 0); System.out.println("disconnect count " + handler.disconnectCount); } @@ -91,8 +88,8 @@ public void testHeartbeat() throws Exception { Thread.sleep(10000); System.err.println("++++++++++++++ disconnect count " + handler.disconnectCount); System.err.println("++++++++++++++ connect count " + handler.connectCount); - Assertions.assertTrue(handler.disconnectCount == 0); - Assertions.assertTrue(handler.connectCount == 1); + Assert.assertTrue(handler.disconnectCount == 0); + Assert.assertTrue(handler.connectCount == 1); } @Test @@ -107,7 +104,7 @@ public void testClientHeartbeat() throws Exception { serverURL = serverURL.addParameter(Constants.HEARTBEAT_KEY, 1000); client = Exchangers.connect(serverURL); Thread.sleep(10000); - Assertions.assertTrue(handler.connectCount > 0); + Assert.assertTrue(handler.connectCount > 0); System.out.println("connect count " + handler.connectCount); } diff --git a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientReconnectTest.java b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientReconnectTest.java index 4cb7eb6784d..1444f9bb6f3 100644 --- a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientReconnectTest.java +++ b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientReconnectTest.java @@ -18,6 +18,7 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.utils.DubboAppender; +import org.apache.dubbo.common.utils.LogUtil; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.Client; @@ -26,16 +27,20 @@ import org.apache.dubbo.remoting.exchange.Exchangers; import org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.apache.log4j.Level; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; /** * Client reconnect test */ public class ClientReconnectTest { + public static void main(String[] args) { + System.out.println(3 % 1); + } - @BeforeEach + @Before public void clear() { DubboAppender.clear(); } @@ -45,32 +50,54 @@ public void testReconnect() throws RemotingException, InterruptedException { { int port = NetUtils.getAvailablePort(); Client client = startClient(port, 200); - Assertions.assertEquals(false, client.isConnected()); + Assert.assertEquals(false, client.isConnected()); Server server = startServer(port); for (int i = 0; i < 100 && !client.isConnected(); i++) { Thread.sleep(10); } - Assertions.assertEquals(true, client.isConnected()); + Assert.assertEquals(true, client.isConnected()); client.close(2000); server.close(2000); } { int port = NetUtils.getAvailablePort(); Client client = startClient(port, 20000); - Assertions.assertEquals(false, client.isConnected()); + Assert.assertEquals(false, client.isConnected()); Server server = startServer(port); for (int i = 0; i < 5; i++) { Thread.sleep(200); } - Assertions.assertEquals(false, client.isConnected()); + Assert.assertEquals(false, client.isConnected()); client.close(2000); server.close(2000); } } + /** + * Reconnect log check, when the time is not enough for shutdown time, there is no error log, but there must be a warn log + */ + @Test + public void testReconnectWarnLog() throws RemotingException, InterruptedException { + int port = NetUtils.getAvailablePort(); + DubboAppender.doStart(); + String url = "exchange://127.0.0.1:" + port + "/client.reconnect.test?check=false&client=netty3&" + + Constants.RECONNECT_KEY + "=" + 1; //1ms reconnect, ensure that there is enough frequency to reconnect + try { + Exchangers.connect(url); + } catch (Exception e) { + + //do nothing + } + Thread.sleep(1500); + //Time is not long enough to produce a error log + Assert.assertEquals("no error message ", 0, LogUtil.findMessage(Level.ERROR, "client reconnect to ")); + //The first reconnection failed to have a warn log + Assert.assertEquals("must have one warn message ", 1, LogUtil.findMessage(Level.WARN, "client reconnect to ")); + DubboAppender.doStop(); + } - public Client startClient(int port, int heartbeat) throws RemotingException { - final String url = "exchange://127.0.0.1:" + port + "/client.reconnect.test?check=false&client=netty3&" + Constants.HEARTBEAT_KEY + "=" + heartbeat; + public Client startClient(int port, int reconnectPeriod) throws RemotingException { + final String url = "exchange://127.0.0.1:" + port + "/client.reconnect.test?check=false&client=netty3&" + Constants.RECONNECT_KEY + "=" + reconnectPeriod; return Exchangers.connect(url); } diff --git a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientToServerTest.java b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientToServerTest.java index 03466bed688..b2172756630 100644 --- a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientToServerTest.java +++ b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientToServerTest.java @@ -22,15 +22,14 @@ import org.apache.dubbo.remoting.exchange.ResponseFuture; import org.apache.dubbo.remoting.exchange.support.Replier; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import junit.framework.TestCase; +import org.junit.Test; /** * ClientToServer */ -public abstract class ClientToServerTest { +public abstract class ClientToServerTest extends TestCase { protected static final String LOCALHOST = "127.0.0.1"; @@ -44,15 +43,17 @@ public abstract class ClientToServerTest { protected abstract ExchangeChannel newClient(int port) throws RemotingException; - @BeforeEach + @Override protected void setUp() throws Exception { + super.setUp(); int port = (int) (1000 * Math.random() + 10000); server = newServer(port, handler); client = newClient(port); } - @BeforeEach + @Override protected void tearDown() throws Exception { + super.tearDown(); try { if (server != null) server.close(); @@ -66,7 +67,7 @@ protected void tearDown() throws Exception { public void testFuture() throws Exception { ResponseFuture future = client.request(new World("world")); Hello result = (Hello) future.get(); - Assertions.assertEquals("hello,world", result.getName()); + Assert.assertEquals("hello,world", result.getName()); } // @Test @@ -74,7 +75,7 @@ public void testFuture() throws Exception { // final Object waitter = new Object(); // client.invoke(new World("world"), new InvokeCallback() { // public void callback(Hello result) { -// Assertions.assertEquals("hello,world", result.getName()); +// Assert.assertEquals("hello,world", result.getName()); // synchronized (waitter) { // waitter.notifyAll(); // } diff --git a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientsTest.java b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientsTest.java index b131880de91..6926dd5ef68 100644 --- a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientsTest.java +++ b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientsTest.java @@ -19,14 +19,13 @@ import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.remoting.Transporter; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsString; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import static org.junit.matchers.JUnitMatchers.containsString; public class ClientsTest { @@ -40,12 +39,10 @@ public void testGetTransportEmpty() { } } - @Test + @Test(expected = IllegalArgumentException.class) public void testGetTransportNull() { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - String name = null; - ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name); - }); + String name = null; + ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name); } @Test @@ -54,11 +51,9 @@ public void testGetTransport3() { assertEquals(NettyTransporter.class, ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name).getClass()); } - @Test + @Test(expected = IllegalStateException.class) public void testGetTransportWrong() { - Assertions.assertThrows(IllegalStateException.class, () -> { - String name = "nety"; - assertNull(ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name).getClass()); - }); + String name = "nety"; + assertNull(ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name).getClass()); } } diff --git a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/NettyClientTest.java b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/NettyClientTest.java index af4fca1faad..7fc55b33fa6 100644 --- a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/NettyClientTest.java +++ b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/NettyClientTest.java @@ -22,9 +22,9 @@ import org.apache.dubbo.remoting.exchange.ExchangeChannel; import org.apache.dubbo.remoting.exchange.Exchangers; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; import java.util.ArrayList; import java.util.List; @@ -37,13 +37,13 @@ public class NettyClientTest { static Server server; - @BeforeAll + @BeforeClass public static void setUp() throws Exception { server = Exchangers.bind(URL.valueOf("exchange://localhost:10001?server=netty3"), new TelnetServerHandler()); } - @AfterAll - public static void tearDown() { + @AfterClass + public static void tearDown() throws Exception { try { if (server != null) server.close(); @@ -73,7 +73,7 @@ public void testClientClose() throws Exception { @Test public void testServerClose() throws Exception { for (int i = 0; i < 100; i++) { - Server aServer = Exchangers.bind(URL.valueOf("exchange://localhost:" + (6000 + i) + "?server=netty3"), new TelnetServerHandler()); + Server aServer = Exchangers.bind(URL.valueOf("exchange://localhost:" + (5000 + i) + "?server=netty3"), new TelnetServerHandler()); aServer.close(); } } diff --git a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/NettyStringTest.java b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/NettyStringTest.java index b3d22c6c987..47f0e93e0be 100644 --- a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/NettyStringTest.java +++ b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/NettyStringTest.java @@ -21,9 +21,9 @@ import org.apache.dubbo.remoting.exchange.ExchangeServer; import org.apache.dubbo.remoting.exchange.Exchangers; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; /** * Date: 4/26/11 @@ -33,7 +33,7 @@ public class NettyStringTest { static ExchangeServer server; static ExchangeChannel client; - @BeforeAll + @BeforeClass public static void setUp() throws Exception { //int port = (int) (1000 * Math.random() + 10000); int port = 10001; @@ -42,7 +42,7 @@ public static void setUp() throws Exception { client = Exchangers.connect(URL.valueOf("telnet://127.0.0.1:" + port + "?client=netty3"), new TelnetClientHandler()); } - @AfterAll + @AfterClass public static void tearDown() throws Exception { try { if (server != null) @@ -59,6 +59,6 @@ public void testHandler() throws Exception { /*client.request("world\r\n"); Future future = client.request("world", 10000); String result = (String)future.get(); - Assertions.assertEquals("Did you say 'world'?\r\n",result);*/ + Assert.assertEquals("Did you say 'world'?\r\n",result);*/ } } \ No newline at end of file diff --git a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ThreadNameTest.java b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ThreadNameTest.java index 684d525c99f..951ed5b9c67 100644 --- a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ThreadNameTest.java +++ b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ThreadNameTest.java @@ -21,10 +21,10 @@ import org.apache.dubbo.remoting.ChannelHandler; import org.apache.dubbo.remoting.RemotingException; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; public class ThreadNameTest { @@ -37,7 +37,7 @@ public class ThreadNameTest { private ThreadNameVerifyHandler serverHandler; private ThreadNameVerifyHandler clientHandler; - @BeforeEach + @Before public void before() throws Exception { int port = 55555; serverURL = URL.valueOf("netty://localhost").setPort(port); @@ -50,7 +50,7 @@ public void before() throws Exception { client = new NettyClient(clientURL, clientHandler); } - @AfterEach + @After public void after() throws Exception { if (client != null) { client.close(); @@ -68,7 +68,7 @@ public void testThreadName() throws Exception { client.send("hello"); Thread.sleep(1000L * 5L); if (!serverHandler.isSuccess() || !clientHandler.isSuccess()) { - Assertions.fail(); + Assert.fail(); } } diff --git a/dubbo-remoting/dubbo-remoting-netty/src/test/resources/log4j.xml b/dubbo-remoting/dubbo-remoting-netty/src/test/resources/log4j.xml index fc40926e12a..acb85098b27 100644 --- a/dubbo-remoting/dubbo-remoting-netty/src/test/resources/log4j.xml +++ b/dubbo-remoting/dubbo-remoting-netty/src/test/resources/log4j.xml @@ -21,7 +21,6 @@ - diff --git a/dubbo-remoting/dubbo-remoting-netty4/pom.xml b/dubbo-remoting/dubbo-remoting-netty4/pom.xml index e17cfe185f7..6253fa1d2f9 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/pom.xml +++ b/dubbo-remoting/dubbo-remoting-netty4/pom.xml @@ -18,7 +18,7 @@ dubbo-remoting org.apache.dubbo - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT 4.0.0 diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyBackedChannelBuffer.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyBackedChannelBuffer.java index 2c00e9499e2..c6e0c6ddee1 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyBackedChannelBuffer.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyBackedChannelBuffer.java @@ -37,13 +37,13 @@ public NettyBackedChannelBuffer(ByteBuf buffer) { this.buffer = buffer; } - + @Override public int capacity() { return buffer.capacity(); } - + @Override public ChannelBuffer copy(int index, int length) { return new NettyBackedChannelBuffer(buffer.copy(index, length)); @@ -55,25 +55,25 @@ public ChannelBufferFactory factory() { return null; } - + @Override public byte getByte(int index) { return buffer.getByte(index); } - + @Override public void getBytes(int index, byte[] dst, int dstIndex, int length) { buffer.getBytes(index, dst, dstIndex, length); } - + @Override public void getBytes(int index, ByteBuffer dst) { buffer.getBytes(index, dst); } - + @Override public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) { // careful @@ -82,70 +82,70 @@ public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) { dst.setBytes(dstIndex, data, 0, length); } - + @Override public void getBytes(int index, OutputStream dst, int length) throws IOException { buffer.getBytes(index, dst, length); } - + @Override public boolean isDirect() { return buffer.isDirect(); } - + @Override public void setByte(int index, int value) { buffer.setByte(index, value); } - + @Override public void setBytes(int index, byte[] src, int srcIndex, int length) { buffer.setBytes(index, src, srcIndex, length); } - + @Override public void setBytes(int index, ByteBuffer src) { buffer.setBytes(index, src); } - + @Override public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) { // careful byte[] data = new byte[length]; buffer.getBytes(srcIndex, data, 0, length); - setBytes(index, data, 0, length); + setBytes(0, data, index, length); } - + @Override public int setBytes(int index, InputStream src, int length) throws IOException { return buffer.setBytes(index, src, length); } - + @Override public ByteBuffer toByteBuffer(int index, int length) { return buffer.nioBuffer(index, length); } - + @Override public byte[] array() { return buffer.array(); } - + @Override public boolean hasArray() { return buffer.hasArray(); } - + @Override public int arrayOffset() { return buffer.arrayOffset(); @@ -155,44 +155,44 @@ public int arrayOffset() { // AbstractChannelBuffer - + @Override public void clear() { buffer.clear(); } - + @Override public ChannelBuffer copy() { return new NettyBackedChannelBuffer(buffer.copy()); } - + @Override public void discardReadBytes() { buffer.discardReadBytes(); } - + @Override public void ensureWritableBytes(int writableBytes) { buffer.ensureWritable(writableBytes); } - + @Override public void getBytes(int index, byte[] dst) { buffer.getBytes(index, dst); } - + @Override public void getBytes(int index, ChannelBuffer dst) { // careful getBytes(index, dst, dst.writableBytes()); } - + @Override public void getBytes(int index, ChannelBuffer dst, int length) { // careful @@ -203,62 +203,62 @@ public void getBytes(int index, ChannelBuffer dst, int length) { dst.writerIndex(dst.writerIndex() + length); } - + @Override public void markReaderIndex() { buffer.markReaderIndex(); } - + @Override public void markWriterIndex() { buffer.markWriterIndex(); } - + @Override public boolean readable() { return buffer.isReadable(); } - + @Override public int readableBytes() { return buffer.readableBytes(); } - + @Override public byte readByte() { return buffer.readByte(); } - + @Override public void readBytes(byte[] dst) { buffer.readBytes(dst); } - + @Override public void readBytes(byte[] dst, int dstIndex, int length) { buffer.readBytes(dst, dstIndex, length); } - + @Override public void readBytes(ByteBuffer dst) { buffer.readBytes(dst); } - + @Override public void readBytes(ChannelBuffer dst) { // careful readBytes(dst, dst.writableBytes()); } - + @Override public void readBytes(ChannelBuffer dst, int length) { // careful @@ -269,7 +269,7 @@ public void readBytes(ChannelBuffer dst, int length) { dst.writerIndex(dst.writerIndex() + length); } - + @Override public void readBytes(ChannelBuffer dst, int dstIndex, int length) { // careful @@ -281,56 +281,56 @@ public void readBytes(ChannelBuffer dst, int dstIndex, int length) { dst.setBytes(dstIndex, data, 0, length); } - + @Override public ChannelBuffer readBytes(int length) { return new NettyBackedChannelBuffer(buffer.readBytes(length)); } - + @Override public void resetReaderIndex() { buffer.resetReaderIndex(); } - + @Override public void resetWriterIndex() { buffer.resetWriterIndex(); } - + @Override public int readerIndex() { return buffer.readerIndex(); } - + @Override public void readerIndex(int readerIndex) { buffer.readerIndex(readerIndex); } - + @Override public void readBytes(OutputStream dst, int length) throws IOException { buffer.readBytes(dst, length); } - + @Override public void setBytes(int index, byte[] src) { buffer.setBytes(index, src); } - + @Override public void setBytes(int index, ChannelBuffer src) { // careful setBytes(index, src, src.readableBytes()); } - + @Override public void setBytes(int index, ChannelBuffer src, int length) { // careful @@ -341,68 +341,68 @@ public void setBytes(int index, ChannelBuffer src, int length) { src.readerIndex(src.readerIndex() + length); } - + @Override public void setIndex(int readerIndex, int writerIndex) { buffer.setIndex(readerIndex, writerIndex); } - + @Override public void skipBytes(int length) { buffer.skipBytes(length); } - + @Override public ByteBuffer toByteBuffer() { return buffer.nioBuffer(); } - + @Override public boolean writable() { return buffer.isWritable(); } - + @Override public int writableBytes() { return buffer.writableBytes(); } - + @Override public void writeByte(int value) { buffer.writeByte(value); } - + @Override public void writeBytes(byte[] src) { buffer.writeBytes(src); } - + @Override public void writeBytes(byte[] src, int index, int length) { buffer.writeBytes(src, index, length); } - + @Override public void writeBytes(ByteBuffer src) { buffer.writeBytes(src); } - + @Override public void writeBytes(ChannelBuffer src) { // careful writeBytes(src, src.readableBytes()); } - + @Override public void writeBytes(ChannelBuffer src, int length) { // careful @@ -413,7 +413,7 @@ public void writeBytes(ChannelBuffer src, int length) { src.readerIndex(src.readerIndex() + length); } - + @Override public void writeBytes(ChannelBuffer src, int srcIndex, int length) { // careful @@ -422,25 +422,25 @@ public void writeBytes(ChannelBuffer src, int srcIndex, int length) { writeBytes(data, 0, length); } - + @Override public int writeBytes(InputStream src, int length) throws IOException { return buffer.writeBytes(src, length); } - + @Override public int writerIndex() { return buffer.writerIndex(); } - + @Override public void writerIndex(int writerIndex) { buffer.writerIndex(writerIndex); } - + @Override public int compareTo(ChannelBuffer o) { return ChannelBuffers.compare(this, o); diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java index 7ba4a63248f..4ea6fe12c52 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java @@ -22,7 +22,6 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.NetUtils; -import org.apache.dubbo.common.utils.UrlUtils; import org.apache.dubbo.remoting.ChannelHandler; import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.transport.AbstractClient; @@ -35,10 +34,9 @@ import io.netty.channel.ChannelOption; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.handler.timeout.IdleStateHandler; import io.netty.util.concurrent.DefaultThreadFactory; -import static java.util.concurrent.TimeUnit.MILLISECONDS; +import java.util.concurrent.TimeUnit; /** * NettyClient. @@ -78,12 +76,10 @@ protected void doOpen() throws Throwable { @Override protected void initChannel(Channel ch) throws Exception { - int heartbeatInterval = UrlUtils.getHeartbeat(getUrl()); NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this); ch.pipeline()//.addLast("logging",new LoggingHandler(LogLevel.INFO))//for debug .addLast("decoder", adapter.getDecoder()) .addLast("encoder", adapter.getEncoder()) - .addLast("client-idle-handler", new IdleStateHandler(heartbeatInterval, 0, 0, MILLISECONDS)) .addLast("handler", nettyClientHandler); } }); @@ -94,7 +90,7 @@ protected void doConnect() throws Throwable { long start = System.currentTimeMillis(); ChannelFuture future = bootstrap.connect(getConnectAddress()); try { - boolean ret = future.awaitUninterruptibly(getConnectTimeout(), MILLISECONDS); + boolean ret = future.awaitUninterruptibly(getConnectTimeout(), TimeUnit.MILLISECONDS); if (ret && future.isSuccess()) { Channel newChannel = future.channel(); @@ -166,8 +162,4 @@ protected org.apache.dubbo.remoting.Channel getChannel() { return NettyChannel.getOrAddChannel(c, getUrl(), this); } - @Override - public boolean canHandleIdle() { - return true; - } } diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClientHandler.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClientHandler.java index c7077086dbb..2d8981170f3 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClientHandler.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClientHandler.java @@ -17,9 +17,6 @@ package org.apache.dubbo.remoting.transport.netty4; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.Version; -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.remoting.ChannelHandler; import org.apache.dubbo.remoting.exchange.Request; @@ -28,14 +25,12 @@ import io.netty.channel.ChannelDuplexHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; -import io.netty.handler.timeout.IdleStateEvent; /** * NettyClientHandler */ @io.netty.channel.ChannelHandler.Sharable public class NettyClientHandler extends ChannelDuplexHandler { - private static final Logger logger = LoggerFactory.getLogger(NettyClient.class); private final URL url; @@ -52,6 +47,7 @@ public NettyClientHandler(URL url, ChannelHandler handler) { this.handler = handler; } + @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler); @@ -82,53 +78,26 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } } + @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { super.write(ctx, msg, promise); - final NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler); - final boolean isRequest = msg instanceof Request; - - // We add listeners to make sure our out bound event is correct. - // If our out bound event has an error (in most cases the encoder fails), - // we need to have the request return directly instead of blocking the invoke process. - promise.addListener(future -> { - try { - if (future.isSuccess()) { - // if our future is success, mark the future to sent. - handler.sent(channel, msg); - return; - } - - Throwable t = future.cause(); - if (t != null && isRequest) { - Request request = (Request) msg; - Response response = buildErrorResponse(request, t); - handler.received(channel, response); - } - } finally { - NettyChannel.removeChannelIfDisconnected(ctx.channel()); - } - }); - } - - @Override - public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { - if (evt instanceof IdleStateEvent) { - try { - NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler); - if (logger.isDebugEnabled()) { - logger.debug("IdleStateEvent triggered, send heartbeat to channel " + channel); - } - Request req = new Request(); - req.setVersion(Version.getProtocolVersion()); - req.setTwoWay(true); - req.setEvent(Request.HEARTBEAT_EVENT); - channel.send(req); - } finally { - NettyChannel.removeChannelIfDisconnected(ctx.channel()); + NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler); + try { + // if error happens from write, mock a BAD_REQUEST response so that invoker can return immediately without + // waiting until timeout. FIXME: not sure if this is the right approach, but exceptionCaught doesn't work + // as expected. + if (promise.cause() != null && msg instanceof Request) { + Request request = (Request) msg; + Response response = new Response(request.getId(), request.getVersion()); + response.setStatus(Response.BAD_REQUEST); + response.setErrorMessage(StringUtils.toString(promise.cause())); + handler.received(channel, response); + } else { + handler.sent(channel, msg); } - } else { - super.userEventTriggered(ctx, evt); + } finally { + NettyChannel.removeChannelIfDisconnected(ctx.channel()); } } @@ -142,18 +111,4 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) NettyChannel.removeChannelIfDisconnected(ctx.channel()); } } - - /** - * build a bad request's response - * - * @param request the request - * @param t the throwable. In most cases, serialization fails. - * @return the response - */ - private static Response buildErrorResponse(Request request, Throwable t) { - Response response = new Response(request.getId(), request.getVersion()); - response.setStatus(Response.BAD_REQUEST); - response.setErrorMessage(StringUtils.toString(t)); - return response; - } -} +} \ No newline at end of file diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyCodecAdapter.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyCodecAdapter.java index e0e4513c5c6..24d5d00f926 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyCodecAdapter.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyCodecAdapter.java @@ -33,7 +33,7 @@ /** * NettyCodecAdapter. */ -final public class NettyCodecAdapter { +final class NettyCodecAdapter { private final ChannelHandler encoder = new InternalEncoder(); @@ -83,11 +83,19 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf input, List out NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler); + Object msg; + + int saveReaderIndex; + try { // decode object. do { - int saveReaderIndex = message.readerIndex(); - Object msg = codec.decode(channel, message); + saveReaderIndex = message.readerIndex(); + try { + msg = codec.decode(channel, message); + } catch (IOException e) { + throw e; + } if (msg == Codec2.DecodeResult.NEED_MORE_INPUT) { message.readerIndex(saveReaderIndex); break; diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServer.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServer.java index 62b6c55f93c..a8da94e05bd 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServer.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServer.java @@ -22,7 +22,6 @@ import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.ExecutorUtil; import org.apache.dubbo.common.utils.NetUtils; -import org.apache.dubbo.common.utils.UrlUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.ChannelHandler; import org.apache.dubbo.remoting.RemotingException; @@ -39,7 +38,6 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.handler.timeout.IdleStateHandler; import io.netty.util.concurrent.DefaultThreadFactory; import java.net.InetSocketAddress; @@ -47,8 +45,6 @@ import java.util.HashSet; import java.util.Map; -import static java.util.concurrent.TimeUnit.MILLISECONDS; - /** * NettyServer */ @@ -88,13 +84,10 @@ protected void doOpen() throws Throwable { .childHandler(new ChannelInitializer() { @Override protected void initChannel(NioSocketChannel ch) throws Exception { - // FIXME: should we use getTimeout()? - int idleTimeout = UrlUtils.getIdleTimeout(getUrl()); NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyServer.this); ch.pipeline()//.addLast("logging",new LoggingHandler(LogLevel.INFO))//for debug .addLast("decoder", adapter.getDecoder()) .addLast("encoder", adapter.getEncoder()) - .addLast("server-idle-handler", new IdleStateHandler(0, 0, idleTimeout, MILLISECONDS)) .addLast("handler", nettyServerHandler); } }); @@ -164,11 +157,6 @@ public Channel getChannel(InetSocketAddress remoteAddress) { return channels.get(NetUtils.toAddressString(remoteAddress)); } - @Override - public boolean canHandleIdle() { - return true; - } - @Override public boolean isBound() { return channel.isActive(); diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServerHandler.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServerHandler.java index ea5b3249839..56d4ff69c99 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServerHandler.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServerHandler.java @@ -17,8 +17,6 @@ package org.apache.dubbo.remoting.transport.netty4; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.ChannelHandler; @@ -26,7 +24,6 @@ import io.netty.channel.ChannelDuplexHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; -import io.netty.handler.timeout.IdleStateEvent; import java.net.InetSocketAddress; import java.util.Map; @@ -37,7 +34,6 @@ */ @io.netty.channel.ChannelHandler.Sharable public class NettyServerHandler extends ChannelDuplexHandler { - private static final Logger logger = LoggerFactory.getLogger(NettyServerHandler.class); private final Map channels = new ConcurrentHashMap(); // @@ -106,20 +102,6 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) } } - @Override - public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { - if (evt instanceof IdleStateEvent) { - NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler); - try { - logger.info("IdleStateEvent triggered, close channel " + channel); - channel.close(); - } finally { - NettyChannel.removeChannelIfDisconnected(ctx.channel()); - } - } - super.userEventTriggered(ctx, evt); - } - @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { @@ -130,4 +112,4 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) NettyChannel.removeChannelIfDisconnected(ctx.channel()); } } -} +} \ No newline at end of file diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/logging/FormattingTuple.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/logging/FormattingTuple.java index eac0a474946..49bbce1f7ab 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/logging/FormattingTuple.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/logging/FormattingTuple.java @@ -17,8 +17,6 @@ package org.apache.dubbo.remoting.transport.netty4.logging; -import org.apache.dubbo.common.utils.ArrayUtils; - /** * Holds the results of formatting done by {@link MessageFormatter}. */ @@ -45,7 +43,7 @@ class FormattingTuple { } static Object[] trimmedCopy(Object[] argArray) { - if (ArrayUtils.isEmpty(argArray)) { + if (argArray == null || argArray.length == 0) { throw new IllegalStateException("non-sensical empty or null argument array"); } final int trimmedLen = argArray.length - 1; diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/logging/MessageFormatter.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/logging/MessageFormatter.java index c7cde8b3e0c..f860875bce1 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/logging/MessageFormatter.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/logging/MessageFormatter.java @@ -16,10 +16,6 @@ */ package org.apache.dubbo.remoting.transport.netty4.logging; -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.ArrayUtils; - import java.text.MessageFormat; import java.util.HashMap; import java.util.Map; @@ -90,7 +86,6 @@ * {@link #arrayFormat(String, Object[])} methods for more details. */ final class MessageFormatter { - private static final Logger logger = LoggerFactory.getLogger(MessageFormatter.class); static final char DELIM_START = '{'; static final char DELIM_STOP = '}'; static final String DELIM_STR = "{}"; @@ -142,7 +137,7 @@ static FormattingTuple format(final String messagePattern, } static Throwable getThrowableCandidate(Object[] argArray) { - if (ArrayUtils.isEmpty(argArray)) { + if (argArray == null || argArray.length == 0) { return null; } @@ -284,7 +279,7 @@ private static void safeObjectAppend(StringBuffer sbuf, Object o) { System.err .println("SLF4J: Failed toString() invocation on an object of type [" + o.getClass().getName() + ']'); - logger.error(t.getMessage(), t); + t.printStackTrace(); sbuf.append("[FAILED toString()]"); } } diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientReconnectTest.java b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientReconnectTest.java index f21918d3d15..aeadd06f9a0 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientReconnectTest.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientReconnectTest.java @@ -25,9 +25,9 @@ import org.apache.dubbo.remoting.Server; import org.apache.dubbo.remoting.exchange.Exchangers; import org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; /** * Client reconnect test @@ -37,7 +37,7 @@ public static void main(String[] args) { System.out.println(3 % 1); } - @BeforeEach + @Before public void clear() { DubboAppender.clear(); } @@ -47,32 +47,32 @@ public void testReconnect() throws RemotingException, InterruptedException { { int port = NetUtils.getAvailablePort(); Client client = startClient(port, 200); - Assertions.assertEquals(false, client.isConnected()); + Assert.assertEquals(false, client.isConnected()); Server server = startServer(port); for (int i = 0; i < 100 && !client.isConnected(); i++) { Thread.sleep(10); } - Assertions.assertEquals(true, client.isConnected()); + Assert.assertEquals(true, client.isConnected()); client.close(2000); server.close(2000); } { int port = NetUtils.getAvailablePort(); Client client = startClient(port, 20000); - Assertions.assertEquals(false, client.isConnected()); + Assert.assertEquals(false, client.isConnected()); Server server = startServer(port); for (int i = 0; i < 5; i++) { Thread.sleep(200); } - Assertions.assertEquals(false, client.isConnected()); + Assert.assertEquals(false, client.isConnected()); client.close(2000); server.close(2000); } } - public Client startClient(int port, int heartbeat) throws RemotingException { - final String url = "exchange://127.0.0.1:" + port + "/client.reconnect.test?client=netty4&check=false&" + Constants.HEARTBEAT_KEY + "=" + heartbeat; + public Client startClient(int port, int reconnectPeriod) throws RemotingException { + final String url = "exchange://127.0.0.1:" + port + "/client.reconnect.test?client=netty4&check=false&" + Constants.RECONNECT_KEY + "=" + reconnectPeriod; return Exchangers.connect(url); } diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientToServerTest.java b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientToServerTest.java index 9d2311fa5ff..4e89a0a5647 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientToServerTest.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientToServerTest.java @@ -21,16 +21,14 @@ import org.apache.dubbo.remoting.exchange.ExchangeServer; import org.apache.dubbo.remoting.exchange.ResponseFuture; import org.apache.dubbo.remoting.exchange.support.Replier; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import junit.framework.TestCase; +import org.junit.Assert; +import org.junit.Test; /** * ClientToServer */ -public abstract class ClientToServerTest { +public abstract class ClientToServerTest extends TestCase { protected static final String LOCALHOST = "127.0.0.1"; @@ -44,15 +42,17 @@ public abstract class ClientToServerTest { protected abstract ExchangeChannel newClient(int port) throws RemotingException; - @BeforeEach + @Override protected void setUp() throws Exception { + super.setUp(); int port = (int) (1000 * Math.random() + 10000); server = newServer(port, handler); client = newClient(port); } - @AfterEach - protected void tearDown() { + @Override + protected void tearDown() throws Exception { + super.tearDown(); try { if (server != null) server.close(); @@ -66,6 +66,6 @@ protected void tearDown() { public void testFuture() throws Exception { ResponseFuture future = client.request(new World("world")); Hello result = (Hello) future.get(); - Assertions.assertEquals("hello,world", result.getName()); + Assert.assertEquals("hello,world", result.getName()); } } \ No newline at end of file diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientsTest.java b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientsTest.java index 24b755323ff..93d53e9b153 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientsTest.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientsTest.java @@ -18,20 +18,14 @@ import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.remoting.Transporter; +import org.junit.Test; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsString; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.*; +import static org.junit.matchers.JUnitMatchers.containsString; public class ClientsTest { @Test public void testGetTransportEmpty() { - try { ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(""); fail(); @@ -40,12 +34,10 @@ public void testGetTransportEmpty() { } } - @Test + @Test(expected = IllegalArgumentException.class) public void testGetTransportNull() { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - String name = null; - ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name); - }); + String name = null; + ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name); } @Test @@ -54,11 +46,9 @@ public void testGetTransport3() { assertEquals(NettyTransporter.class, ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name).getClass()); } - @Test + @Test(expected = IllegalStateException.class) public void testGetTransportWrong() { - Assertions.assertThrows(IllegalStateException.class, () -> { - String name = "nety"; - assertNull(ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name).getClass()); - }); + String name = "nety"; + assertNull(ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name).getClass()); } } diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/NettyTransporterTest.java b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/NettyTransporterTest.java index b0bf7209a0c..836c575a526 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/NettyTransporterTest.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/NettyTransporterTest.java @@ -23,13 +23,12 @@ import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.Server; import org.apache.dubbo.remoting.transport.ChannelHandlerAdapter; - -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.concurrent.CountDownLatch; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class NettyTransporterTest { @Test @@ -54,7 +53,7 @@ public void shouldConnectToNetty4Server() throws Exception { new NettyTransporter().bind(url, new ChannelHandlerAdapter() { @Override - public void connected(Channel channel) { + public void connected(Channel channel) throws RemotingException { lock.countDown(); } }); diff --git a/dubbo-remoting/dubbo-remoting-p2p/pom.xml b/dubbo-remoting/dubbo-remoting-p2p/pom.xml index 238398e4a16..18be11a1e20 100644 --- a/dubbo-remoting/dubbo-remoting-p2p/pom.xml +++ b/dubbo-remoting/dubbo-remoting-p2p/pom.xml @@ -14,12 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. --> - + 4.0.0 org.apache.dubbo dubbo-remoting - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-remoting-p2p jar diff --git a/dubbo-remoting/dubbo-remoting-p2p/src/main/java/org/apache/dubbo/remoting/p2p/exchange/support/FileExchangeGroup.java b/dubbo-remoting/dubbo-remoting-p2p/src/main/java/org/apache/dubbo/remoting/p2p/exchange/support/FileExchangeGroup.java index 554b0e3dc2d..f2511a19abd 100644 --- a/dubbo-remoting/dubbo-remoting-p2p/src/main/java/org/apache/dubbo/remoting/p2p/exchange/support/FileExchangeGroup.java +++ b/dubbo-remoting/dubbo-remoting-p2p/src/main/java/org/apache/dubbo/remoting/p2p/exchange/support/FileExchangeGroup.java @@ -17,7 +17,6 @@ package org.apache.dubbo.remoting.p2p.exchange.support; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.utils.ExecutorUtil; import org.apache.dubbo.common.utils.IOUtils; import org.apache.dubbo.common.utils.NamedThreadFactory; import org.apache.dubbo.common.utils.NetUtils; @@ -71,7 +70,9 @@ public void run() { public void close() { super.close(); try { - ExecutorUtil.cancelScheduledFuture(checkModifiedFuture); + if (!checkModifiedFuture.isCancelled()) { + checkModifiedFuture.cancel(true); + } } catch (Throwable t) { logger.error(t.getMessage(), t); } diff --git a/dubbo-remoting/dubbo-remoting-p2p/src/main/java/org/apache/dubbo/remoting/p2p/support/FileGroup.java b/dubbo-remoting/dubbo-remoting-p2p/src/main/java/org/apache/dubbo/remoting/p2p/support/FileGroup.java index 68562c15255..8a67e0edcc8 100644 --- a/dubbo-remoting/dubbo-remoting-p2p/src/main/java/org/apache/dubbo/remoting/p2p/support/FileGroup.java +++ b/dubbo-remoting/dubbo-remoting-p2p/src/main/java/org/apache/dubbo/remoting/p2p/support/FileGroup.java @@ -17,7 +17,6 @@ package org.apache.dubbo.remoting.p2p.support; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.utils.ExecutorUtil; import org.apache.dubbo.common.utils.IOUtils; import org.apache.dubbo.common.utils.NamedThreadFactory; import org.apache.dubbo.common.utils.NetUtils; @@ -68,7 +67,9 @@ public void run() { public void close() { super.close(); try { - ExecutorUtil.cancelScheduledFuture(checkModifiedFuture); + if (!checkModifiedFuture.isCancelled()) { + checkModifiedFuture.cancel(true); + } } catch (Throwable t) { logger.error(t.getMessage(), t); } diff --git a/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/exchange/support/MulticastExchangeNetworkerTest.java b/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/exchange/support/MulticastExchangeNetworkerTest.java index d102c992c42..1366ec0d072 100644 --- a/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/exchange/support/MulticastExchangeNetworkerTest.java +++ b/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/exchange/support/MulticastExchangeNetworkerTest.java @@ -30,13 +30,13 @@ import org.apache.dubbo.remoting.p2p.Group; import org.apache.dubbo.remoting.p2p.Networkers; import org.apache.dubbo.remoting.p2p.Peer; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; public class MulticastExchangeNetworkerTest { diff --git a/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/support/FileNetworkerTest.java b/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/support/FileNetworkerTest.java index a442173c45c..621e3c4aa6a 100644 --- a/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/support/FileNetworkerTest.java +++ b/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/support/FileNetworkerTest.java @@ -23,33 +23,35 @@ import org.apache.dubbo.remoting.p2p.Group; import org.apache.dubbo.remoting.p2p.Peer; import org.apache.dubbo.remoting.transport.ChannelHandlerAdapter; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; - -import java.nio.file.Path; +import java.io.IOException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import static org.mockito.Mockito.mock; public class FileNetworkerTest { + @Rule + public TemporaryFolder folder = new TemporaryFolder(); - @BeforeEach - public void setUp(@TempDir Path folder) throws Exception { - folder.toFile().createNewFile(); + @Before + public void setUp() throws Exception { + folder.create(); } - @AfterEach - public void tearDown(@TempDir Path folder) { - folder.getFileName().toAbsolutePath().toFile().delete(); + @After + public void tearDown() throws Exception { + folder.delete(); } @Test - public void testJoin(@TempDir Path folder) throws RemotingException, InterruptedException { - final String groupURL = "file:///" + folder.getFileName().toAbsolutePath(); + public void testJoin() throws RemotingException, InterruptedException, IOException { + final String groupURL = "file:///" + folder.newFile(); FileNetworker networker = new FileNetworker(); Group group = networker.lookup(URL.valueOf(groupURL)); diff --git a/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/support/MulticastNetworkerTest.java b/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/support/MulticastNetworkerTest.java index 2229f0480a0..eefc27a01a7 100644 --- a/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/support/MulticastNetworkerTest.java +++ b/dubbo-remoting/dubbo-remoting-p2p/src/test/java/org/apache/dubbo/remoting/p2p/support/MulticastNetworkerTest.java @@ -23,14 +23,14 @@ import org.apache.dubbo.remoting.p2p.Networkers; import org.apache.dubbo.remoting.p2p.Peer; import org.apache.dubbo.remoting.transport.ChannelHandlerAdapter; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; public class MulticastNetworkerTest { diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/pom.xml b/dubbo-remoting/dubbo-remoting-zookeeper/pom.xml index 24b14e9bd7e..a6cb828d878 100644 --- a/dubbo-remoting/dubbo-remoting-zookeeper/pom.xml +++ b/dubbo-remoting/dubbo-remoting-zookeeper/pom.xml @@ -1,25 +1,24 @@ 4.0.0 org.apache.dubbo dubbo-remoting - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-remoting-zookeeper jar @@ -35,10 +34,16 @@ ${project.parent.version} - org.apache.dubbo - dubbo-dependencies-zookeeper - ${project.parent.version} - pom + org.apache.zookeeper + zookeeper + + + com.101tec + zkclient + + + org.apache.curator + curator-framework org.apache.curator @@ -46,4 +51,4 @@ test - + \ No newline at end of file diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/EventType.java b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/EventType.java deleted file mode 100644 index a1de0373652..00000000000 --- a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/EventType.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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 org.apache.dubbo.remoting.zookeeper; - -import org.apache.zookeeper.Watcher; - -/** - * 2019-02-26 - */ -public enum EventType { - None(-1), - NodeCreated(1), - NodeDeleted(2), - NodeDataChanged(3), - NodeChildrenChanged(4), - CONNECTION_SUSPENDED(11), - CONNECTION_RECONNECTED(12), - CONNECTION_LOST(12), - INITIALIZED(10); - - - - private final int intValue; // Integer representation of value - // for sending over wire - - EventType(int intValue) { - this.intValue = intValue; - } - - public int getIntValue() { - return intValue; - } - - public static Watcher.Event.EventType fromInt(int intValue) { - switch (intValue) { - case -1: - return Watcher.Event.EventType.None; - case 1: - return Watcher.Event.EventType.NodeCreated; - case 2: - return Watcher.Event.EventType.NodeDeleted; - case 3: - return Watcher.Event.EventType.NodeDataChanged; - case 4: - return Watcher.Event.EventType.NodeChildrenChanged; - - default: - throw new RuntimeException("Invalid integer value for conversion to EventType"); - } - } -} diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/ZookeeperClient.java b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/ZookeeperClient.java index cbb37479cd3..b6875ee8e52 100644 --- a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/ZookeeperClient.java +++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/ZookeeperClient.java @@ -19,7 +19,6 @@ import org.apache.dubbo.common.URL; import java.util.List; -import java.util.concurrent.Executor; public interface ZookeeperClient { @@ -31,21 +30,6 @@ public interface ZookeeperClient { List addChildListener(String path, ChildListener listener); - /** - * @param path: directory. All of child of path will be listened. - * @param listener - */ - void addDataListener(String path, DataListener listener); - - /** - * @param path: directory. All of child of path will be listened. - * @param listener - * @param executor another thread - */ - void addDataListener(String path, DataListener listener, Executor executor); - - void removeDataListener(String path, DataListener listener); - void removeChildListener(String path, ChildListener listener); void addStateListener(StateListener listener); diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperClient.java b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperClient.java index 4bf7b6d3bf5..a78edda76a6 100644 --- a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperClient.java +++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperClient.java @@ -16,24 +16,18 @@ */ package org.apache.dubbo.remoting.zookeeper.curator; -import org.apache.dubbo.common.Constants; -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.remoting.zookeeper.ChildListener; -import org.apache.dubbo.remoting.zookeeper.DataListener; -import org.apache.dubbo.remoting.zookeeper.EventType; -import org.apache.dubbo.remoting.zookeeper.StateListener; -import org.apache.dubbo.remoting.zookeeper.support.AbstractZookeeperClient; - import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.api.CuratorWatcher; -import org.apache.curator.framework.recipes.cache.TreeCache; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheListener; import org.apache.curator.framework.state.ConnectionState; import org.apache.curator.framework.state.ConnectionStateListener; import org.apache.curator.retry.RetryNTimes; +import org.apache.dubbo.common.Constants; +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.utils.StringUtils; +import org.apache.dubbo.remoting.zookeeper.ChildListener; +import org.apache.dubbo.remoting.zookeeper.StateListener; +import org.apache.dubbo.remoting.zookeeper.support.AbstractZookeeperClient; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException.NoNodeException; import org.apache.zookeeper.KeeperException.NodeExistsException; @@ -42,15 +36,11 @@ import java.nio.charset.Charset; import java.util.Collections; import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.Executor; -public class CuratorZookeeperClient extends AbstractZookeeperClient { +public class CuratorZookeeperClient extends AbstractZookeeperClient { - static final Charset charset = Charset.forName("UTF-8"); + private final Charset charset = Charset.forName("UTF-8"); private final CuratorFramework client; - private Map treeCacheMap = new ConcurrentHashMap<>(); public CuratorZookeeperClient(URL url) { @@ -106,15 +96,10 @@ public void createEphemeral(String path) { @Override protected void createPersistent(String path, String data) { - byte[] dataBytes = data.getBytes(charset); try { + byte[] dataBytes = data.getBytes(charset); client.create().forPath(path, dataBytes); } catch (NodeExistsException e) { - try { - client.setData().forPath(path, dataBytes); - } catch (Exception e1) { - throw new IllegalStateException(e.getMessage(), e1); - } } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } @@ -122,15 +107,10 @@ protected void createPersistent(String path, String data) { @Override protected void createEphemeral(String path, String data) { - byte[] dataBytes = data.getBytes(charset); try { + byte[] dataBytes = data.getBytes(charset); client.create().withMode(CreateMode.EPHEMERAL).forPath(path, dataBytes); } catch (NodeExistsException e) { - try { - client.setData().forPath(path, dataBytes); - } catch (Exception e1) { - throw new IllegalStateException(e.getMessage(), e1); - } } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } @@ -192,12 +172,12 @@ public void doClose() { } @Override - public CuratorZookeeperClient.CuratorWatcherImpl createTargetChildListener(String path, ChildListener listener) { - return new CuratorZookeeperClient.CuratorWatcherImpl(client, listener); + public CuratorWatcher createTargetChildListener(String path, ChildListener listener) { + return new CuratorWatcherImpl(listener); } @Override - public List addTargetChildListener(String path, CuratorWatcherImpl listener) { + public List addTargetChildListener(String path, CuratorWatcher listener) { try { return client.getChildren().usingWatcher(listener).forPath(path); } catch (NoNodeException e) { @@ -208,73 +188,27 @@ public List addTargetChildListener(String path, CuratorWatcherImpl liste } @Override - protected CuratorZookeeperClient.CuratorWatcherImpl createTargetDataListener(String path, DataListener listener) { - return new CuratorWatcherImpl(client, listener); - } - - @Override - protected void addTargetDataListener(String path, CuratorZookeeperClient.CuratorWatcherImpl treeCacheListener) { - this.addTargetDataListener(path, treeCacheListener, null); - } - - @Override - protected void addTargetDataListener(String path, CuratorZookeeperClient.CuratorWatcherImpl treeCacheListener, Executor executor) { - try { - TreeCache treeCache = TreeCache.newBuilder(client, path).setCacheData(false).build(); - treeCacheMap.putIfAbsent(path, treeCache); - treeCache.start(); - if (executor == null) { - treeCache.getListenable().addListener(treeCacheListener); - } else { - treeCache.getListenable().addListener(treeCacheListener, executor); - } - } catch (Exception e) { - throw new IllegalStateException("Add treeCache listener for path:" + path, e); - } - } - - @Override - protected void removeTargetDataListener(String path, CuratorZookeeperClient.CuratorWatcherImpl treeCacheListener) { - TreeCache treeCache = treeCacheMap.get(path); - if (treeCache != null) { - treeCache.getListenable().removeListener(treeCacheListener); - } - treeCacheListener.dataListener = null; + public void removeTargetChildListener(String path, CuratorWatcher listener) { + ((CuratorWatcherImpl) listener).unwatch(); } - @Override - public void removeTargetChildListener(String path, CuratorWatcherImpl listener) { - listener.unwatch(); - } - - static class CuratorWatcherImpl implements CuratorWatcher, TreeCacheListener { - - private CuratorFramework client; - private volatile ChildListener childListener; - private volatile DataListener dataListener; + private class CuratorWatcherImpl implements CuratorWatcher { + private volatile ChildListener listener; - public CuratorWatcherImpl(CuratorFramework client, ChildListener listener) { - this.client = client; - this.childListener = listener; - } - - public CuratorWatcherImpl(CuratorFramework client, DataListener dataListener) { - this.dataListener = dataListener; - } - - protected CuratorWatcherImpl() { + public CuratorWatcherImpl(ChildListener listener) { + this.listener = listener; } public void unwatch() { - this.childListener = null; + this.listener = null; } @Override public void process(WatchedEvent event) throws Exception { - if (childListener != null) { + if (listener != null) { String path = event.getPath() == null ? "" : event.getPath(); - childListener.childChanged(path, + listener.childChanged(path, // if path is null, curator using watcher will throw NullPointerException. // if client connect or disconnect to server, zookeeper will queue // watched event(Watcher.Event.EventType.None, .., path = null). @@ -283,54 +217,6 @@ public void process(WatchedEvent event) throws Exception { : Collections.emptyList()); } } - - @Override - public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { - if (dataListener != null) { - TreeCacheEvent.Type type = event.getType(); - EventType eventType = null; - String content = null; - String path = null; - switch (type) { - case NODE_ADDED: - eventType = EventType.NodeCreated; - path = event.getData().getPath(); - content = new String(event.getData().getData(), charset); - break; - case NODE_UPDATED: - eventType = EventType.NodeDataChanged; - path = event.getData().getPath(); - content = new String(event.getData().getData(), charset); - break; - case NODE_REMOVED: - path = event.getData().getPath(); - eventType = EventType.NodeDeleted; - break; - case INITIALIZED: - eventType = EventType.INITIALIZED; - break; - case CONNECTION_LOST: - eventType = EventType.CONNECTION_LOST; - break; - case CONNECTION_RECONNECTED: - eventType = EventType.CONNECTION_RECONNECTED; - break; - case CONNECTION_SUSPENDED: - eventType = EventType.CONNECTION_SUSPENDED; - break; - - } - dataListener.dataChanged(path, content, eventType); - } - } } - /** - * just for unit test - * - * @return - */ - CuratorFramework getClient() { - return client; - } } diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperTransporter.java b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperTransporter.java index 21a2e095422..b6a6c3ec9f9 100644 --- a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperTransporter.java +++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperTransporter.java @@ -21,7 +21,7 @@ import org.apache.dubbo.remoting.zookeeper.support.AbstractZookeeperTransporter; public class CuratorZookeeperTransporter extends AbstractZookeeperTransporter { - @Override + public ZookeeperClient createZookeeperClient(URL url) { return new CuratorZookeeperClient(url); } diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/support/AbstractZookeeperClient.java b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/support/AbstractZookeeperClient.java index 9697cea012e..852c974111e 100644 --- a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/support/AbstractZookeeperClient.java +++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/support/AbstractZookeeperClient.java @@ -20,7 +20,6 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.remoting.zookeeper.ChildListener; -import org.apache.dubbo.remoting.zookeeper.DataListener; import org.apache.dubbo.remoting.zookeeper.StateListener; import org.apache.dubbo.remoting.zookeeper.ZookeeperClient; @@ -29,9 +28,8 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.CopyOnWriteArraySet; -import java.util.concurrent.Executor; -public abstract class AbstractZookeeperClient implements ZookeeperClient { +public abstract class AbstractZookeeperClient implements ZookeeperClient { protected static final Logger logger = LoggerFactory.getLogger(AbstractZookeeperClient.class); @@ -41,8 +39,6 @@ public abstract class AbstractZookeeperClient> childListeners = new ConcurrentHashMap>(); - private final ConcurrentMap> listeners = new ConcurrentHashMap>(); - private volatile boolean closed = false; public AbstractZookeeperClient(URL url) { @@ -101,37 +97,6 @@ public List addChildListener(String path, final ChildListener listener) return addTargetChildListener(path, targetListener); } - @Override - public void addDataListener(String path, DataListener listener) { - this.addDataListener(path, listener, null); - } - - @Override - public void addDataListener(String path, DataListener listener, Executor executor) { - ConcurrentMap dataListenerMap = listeners.get(path); - if (dataListenerMap == null) { - listeners.putIfAbsent(path, new ConcurrentHashMap()); - dataListenerMap = listeners.get(path); - } - TargetDataListener targetListener = dataListenerMap.get(listener); - if (targetListener == null) { - dataListenerMap.putIfAbsent(listener, createTargetDataListener(path, listener)); - targetListener = dataListenerMap.get(listener); - } - addTargetDataListener(path, targetListener, executor); - } - - @Override - public void removeDataListener(String path, DataListener listener ){ - ConcurrentMap dataListenerMap = listeners.get(path); - if (dataListenerMap != null) { - TargetDataListener targetListener = dataListenerMap.remove(listener); - if(targetListener != null){ - removeTargetDataListener(path, targetListener); - } - } - } - @Override public void removeChildListener(String path, ChildListener listener) { ConcurrentMap listeners = childListeners.get(path); @@ -162,7 +127,6 @@ public void close() { } } - @Override public void create(String path, String content, boolean ephemeral) { if (checkExists(path)) { delete(path); @@ -178,7 +142,6 @@ public void create(String path, String content, boolean ephemeral) { } } - @Override public String getContent(String path) { if (!checkExists(path)) { return null; @@ -202,14 +165,6 @@ public String getContent(String path) { protected abstract List addTargetChildListener(String path, TargetChildListener listener); - protected abstract TargetDataListener createTargetDataListener(String path, DataListener listener); - - protected abstract void addTargetDataListener(String path, TargetDataListener listener); - - protected abstract void addTargetDataListener(String path, TargetDataListener listener, Executor executor); - - protected abstract void removeTargetDataListener(String path, TargetDataListener listener); - protected abstract void removeTargetChildListener(String path, TargetChildListener listener); protected abstract String doGetContent(String path); diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/support/AbstractZookeeperTransporter.java b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/support/AbstractZookeeperTransporter.java index 72c702a1df3..5b58dc7b8c3 100644 --- a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/support/AbstractZookeeperTransporter.java +++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/support/AbstractZookeeperTransporter.java @@ -47,7 +47,6 @@ public abstract class AbstractZookeeperTransporter implements ZookeeperTransport * @param url * @return */ - @Override public ZookeeperClient connect(URL url) { ZookeeperClient zookeeperClient; List addressList = getURLBackupAddress(url); diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkClientWrapper.java b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkClientWrapper.java new file mode 100644 index 00000000000..ae8a3ef87b2 --- /dev/null +++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkClientWrapper.java @@ -0,0 +1,144 @@ +/* + * 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 org.apache.dubbo.remoting.zookeeper.zkclient; + +import org.I0Itec.zkclient.IZkChildListener; +import org.I0Itec.zkclient.IZkStateListener; +import org.I0Itec.zkclient.ZkClient; +import org.apache.dubbo.common.logger.Logger; +import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.utils.Assert; +import org.apache.zookeeper.Watcher.Event.KeeperState; + +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; + +/** + * Zkclient wrapper class that can monitor the state of the connection automatically after the connection is out of time + * It is also consistent with the use of curator + * + * @date 2017/10/29 + */ +public class ZkClientWrapper { + private Logger logger = LoggerFactory.getLogger(ZkClientWrapper.class); + private long timeout; + private ZkClient client; + private volatile KeeperState state; + private CompletableFuture completableFuture; + private volatile boolean started = false; + + public ZkClientWrapper(final String serverAddr, long timeout) { + this.timeout = timeout; + completableFuture = CompletableFuture.supplyAsync(() -> new ZkClient(serverAddr, Integer.MAX_VALUE)); + } + + public void start() { + if (!started) { + try { + client = completableFuture.get(timeout, TimeUnit.MILLISECONDS); +// this.client.subscribeStateChanges(IZkStateListener); + } catch (Throwable t) { + logger.error("Timeout! zookeeper server can not be connected in : " + timeout + "ms!", t); + completableFuture.whenComplete(this::makeClientReady); + } + started = true; + } else { + logger.warn("Zkclient has already been started!"); + } + } + + public void addListener(IZkStateListener listener) { + completableFuture.whenComplete((value, exception) -> { + this.makeClientReady(value, exception); + if (exception == null) { + client.subscribeStateChanges(listener); + } + }); + } + + public boolean isConnected() { +// return client != null && state == KeeperState.SyncConnected; + return client != null; + } + + public void createPersistent(String path) { + Assert.notNull(client, new IllegalStateException("Zookeeper is not connected yet!")); + client.createPersistent(path, true); + } + + public void createEphemeral(String path) { + Assert.notNull(client, new IllegalStateException("Zookeeper is not connected yet!")); + client.createEphemeral(path); + } + + public void createPersistent(String path, String data) { + Assert.notNull(client, new IllegalStateException("Zookeeper is not connected yet!")); + client.createPersistent(path, data); + } + + public void createEphemeral(String path, String data) { + Assert.notNull(client, new IllegalStateException("Zookeeper is not connected yet!")); + client.createEphemeral(path, data); + } + + public void delete(String path) { + Assert.notNull(client, new IllegalStateException("Zookeeper is not connected yet!")); + client.delete(path); + } + + public List getChildren(String path) { + Assert.notNull(client, new IllegalStateException("Zookeeper is not connected yet!")); + return client.getChildren(path); + } + + public String getData(String path) { + Assert.notNull(client, new IllegalStateException("Zookeeper is not connected yet!")); + return client.readData(path); + } + + public boolean exists(String path) { + Assert.notNull(client, new IllegalStateException("Zookeeper is not connected yet!")); + return client.exists(path); + } + + public void close() { + Assert.notNull(client, new IllegalStateException("Zookeeper is not connected yet!")); + client.close(); + } + + public List subscribeChildChanges(String path, final IZkChildListener listener) { + Assert.notNull(client, new IllegalStateException("Zookeeper is not connected yet!")); + return client.subscribeChildChanges(path, listener); + } + + public void unsubscribeChildChanges(String path, IZkChildListener listener) { + Assert.notNull(client, new IllegalStateException("Zookeeper is not connected yet!")); + client.unsubscribeChildChanges(path, listener); + } + + private void makeClientReady(ZkClient client, Throwable e) { + if (e != null) { + logger.error("Got an exception when trying to create zkclient instance, can not connect to zookeeper server, please check!", e); + } else { + this.client = client; +// this.client.subscribeStateChanges(IZkStateListener); + } + } + + +} diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperClient.java b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperClient.java new file mode 100644 index 00000000000..c36640025b9 --- /dev/null +++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperClient.java @@ -0,0 +1,168 @@ +/* + * 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 org.apache.dubbo.remoting.zookeeper.zkclient; + + +import org.I0Itec.zkclient.IZkChildListener; +import org.I0Itec.zkclient.IZkStateListener; +import org.I0Itec.zkclient.exception.ZkNoNodeException; +import org.I0Itec.zkclient.exception.ZkNodeExistsException; +import org.apache.dubbo.common.Constants; +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.logger.Logger; +import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.remoting.zookeeper.ChildListener; +import org.apache.dubbo.remoting.zookeeper.StateListener; +import org.apache.dubbo.remoting.zookeeper.support.AbstractZookeeperClient; +import org.apache.zookeeper.Watcher.Event.KeeperState; + +import java.util.List; + +public class ZkclientZookeeperClient extends AbstractZookeeperClient { + + private Logger logger = LoggerFactory.getLogger(ZkclientZookeeperClient.class); + + private final ZkClientWrapper client; + + private volatile KeeperState state = KeeperState.SyncConnected; + + public ZkclientZookeeperClient(URL url) { + super(url); + long timeout = url.getParameter(Constants.TIMEOUT_KEY, 30000L); + client = new ZkClientWrapper(url.getBackupAddress(), timeout); + client.addListener(new IZkStateListener() { + @Override + public void handleStateChanged(KeeperState state) throws Exception { + ZkclientZookeeperClient.this.state = state; + if (state == KeeperState.Disconnected) { + stateChanged(StateListener.DISCONNECTED); + } else if (state == KeeperState.SyncConnected) { + stateChanged(StateListener.CONNECTED); + } + } + + @Override + public void handleNewSession() throws Exception { + stateChanged(StateListener.RECONNECTED); + } + }); + client.start(); + } + + @Override + public void createPersistent(String path) { + try { + client.createPersistent(path); + } catch (ZkNodeExistsException e) { + logger.error("zookeeper failed to create persistent node with " + path + ": ", e); + } + } + + @Override + public void createEphemeral(String path) { + try { + client.createEphemeral(path); + } catch (ZkNodeExistsException e) { + logger.error("zookeeper failed to create ephemeral node with " + path + ": ", e); + } + } + + @Override + protected void createPersistent(String path, String data) { + try { + client.createPersistent(path, data); + } catch (ZkNodeExistsException e) { + logger.error("zookeeper failed to create persistent node with " + + path + " and " + data + " : ", e); + } + } + + @Override + protected void createEphemeral(String path, String data) { + try { + client.createEphemeral(path, data); + } catch (ZkNodeExistsException e) { + logger.error("zookeeper failed to create ephemeral node with " + + path + " and " + data + " : ", e); + } + } + + @Override + public void delete(String path) { + try { + client.delete(path); + } catch (ZkNoNodeException e) { + logger.error("zookeeper failed to delete node with " + path + ": ", e); + } + } + + @Override + public List getChildren(String path) { + try { + return client.getChildren(path); + } catch (ZkNoNodeException e) { + logger.error("zookeeper failed to get children node with " + path + ": ", e); + return null; + } + } + + @Override + public boolean checkExists(String path) { + try { + return client.exists(path); + } catch (Throwable t) { + logger.error("zookeeper failed to check node existing with " + path + ": ", t); + } + return false; + } + + @Override + public boolean isConnected() { + return state == KeeperState.SyncConnected; + } + + @Override + public String doGetContent(String path) { + try { + return client.getData(path); + } catch (ZkNoNodeException e) { + logger.error("zookeeper failed to get data with " + path + ": ", e); + return null; + } + } + + @Override + public void doClose() { + client.close(); + } + + @Override + public IZkChildListener createTargetChildListener(String path, final ChildListener listener) { + return listener::childChanged; + } + + @Override + public List addTargetChildListener(String path, final IZkChildListener listener) { + return client.subscribeChildChanges(path, listener); + } + + @Override + public void removeTargetChildListener(String path, IZkChildListener listener) { + client.unsubscribeChildChanges(path, listener); + } + +} diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdTransporter.java b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperTransporter.java similarity index 68% rename from dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdTransporter.java rename to dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperTransporter.java index 5ddec8ee1fd..eef25afb3f0 100644 --- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdTransporter.java +++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperTransporter.java @@ -14,17 +14,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.remoting.etcd.jetcd; +package org.apache.dubbo.remoting.zookeeper.zkclient; import org.apache.dubbo.common.URL; -import org.apache.dubbo.remoting.etcd.EtcdClient; -import org.apache.dubbo.remoting.etcd.EtcdTransporter; +import org.apache.dubbo.remoting.zookeeper.ZookeeperClient; +import org.apache.dubbo.remoting.zookeeper.support.AbstractZookeeperTransporter; -public class JEtcdTransporter implements EtcdTransporter { +public class ZkclientZookeeperTransporter extends AbstractZookeeperTransporter { - @Override - public EtcdClient connect(URL url) { - return new JEtcdClient(url); + public ZookeeperClient createZookeeperClient(URL url) { + return new ZkclientZookeeperClient(url); } } diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter index f8cbd5b417d..e9b9349f339 100644 --- a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter +++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter @@ -1 +1,2 @@ -curator=org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperTransporter +zkclient=org.apache.dubbo.remoting.zookeeper.zkclient.ZkclientZookeeperTransporter +curator=org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperTransporter \ No newline at end of file diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperClientTest.java b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperClientTest.java index f1882e1c759..95506c70e6b 100644 --- a/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperClientTest.java +++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperClientTest.java @@ -15,47 +15,36 @@ * limitations under the License. */ package org.apache.dubbo.remoting.zookeeper.curator; - import org.apache.dubbo.common.URL; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.remoting.zookeeper.ChildListener; - -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.CuratorFrameworkFactory; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheListener; -import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.curator.framework.api.CuratorWatcher; import org.apache.curator.test.TestingServer; import org.apache.zookeeper.WatchedEvent; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import java.io.IOException; import java.util.List; import java.util.concurrent.CountDownLatch; -import java.util.concurrent.atomic.AtomicInteger; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; public class CuratorZookeeperClientTest { private TestingServer zkServer; private CuratorZookeeperClient curatorClient; - CuratorFramework client = null; - @BeforeEach + @Before public void setUp() throws Exception { int zkServerPort = NetUtils.getAvailablePort(); zkServer = new TestingServer(zkServerPort, true); curatorClient = new CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:" + zkServerPort + "/org.apache.dubbo.registry.RegistryService")); - client = CuratorFrameworkFactory.newClient(zkServer.getConnectString(), new ExponentialBackoffRetry(1000, 3)); - client.start(); } @Test @@ -82,8 +71,7 @@ public void testChildrenListener() throws InterruptedException { String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers"; curatorClient.create(path, false); final CountDownLatch countDownLatch = new CountDownLatch(1); - curatorClient.addTargetChildListener(path, new CuratorZookeeperClient.CuratorWatcherImpl() { - + curatorClient.addTargetChildListener(path, new CuratorWatcher() { @Override public void process(WatchedEvent watchedEvent) throws Exception { countDownLatch.countDown(); @@ -94,21 +82,17 @@ public void process(WatchedEvent watchedEvent) throws Exception { } - @Test + @Test(expected = IllegalStateException.class) public void testWithInvalidServer() { - Assertions.assertThrows(IllegalStateException.class, () -> { - curatorClient = new CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:1/service")); - curatorClient.create("/testPath", true); - }); + curatorClient = new CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:1/service")); + curatorClient.create("/testPath", true); } - @Test + @Test(expected = IllegalStateException.class) public void testWithStoppedServer() throws IOException { - Assertions.assertThrows(IllegalStateException.class, () -> { - curatorClient.create("/testPath", true); - zkServer.stop(); - curatorClient.delete("/testPath"); - }); + curatorClient.create("/testPath", true); + zkServer.stop(); + curatorClient.delete("/testPath"); } @Test @@ -157,40 +141,9 @@ public void testCreateContent4Temp() { assertEquals(curatorClient.getContent(path), content); } - @AfterEach + @After public void tearDown() throws Exception { curatorClient.close(); zkServer.stop(); } - - @Test - public void testAddTargetDataListener() throws Exception { - String listenerPath = "/dubbo/service.name/configuration"; - String path = listenerPath + "/dat/data"; - String value = "vav"; - - curatorClient.create(path + "/d.json", value, true); - String valueFromCache = curatorClient.getContent(path + "/d.json"); - Assertions.assertEquals(value, valueFromCache); - final AtomicInteger atomicInteger = new AtomicInteger(0); - curatorClient.addTargetDataListener(listenerPath, new CuratorZookeeperClient.CuratorWatcherImpl() { - @Override - public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { - System.out.println("===" + event); - atomicInteger.incrementAndGet(); - } - }); - - valueFromCache = curatorClient.getContent(path + "/d.json"); - Assertions.assertNotNull(valueFromCache); - curatorClient.getClient().setData().forPath(path + "/d.json", "sdsdf".getBytes()); - curatorClient.getClient().setData().forPath(path + "/d.json", "dfsasf".getBytes()); - curatorClient.delete(path + "/d.json"); - curatorClient.delete(path); - valueFromCache = curatorClient.getContent(path + "/d.json"); - Assertions.assertNull(valueFromCache); - Thread.sleep(2000l); - Assertions.assertTrue(9l >= atomicInteger.get()); - Assertions.assertTrue(2l <= atomicInteger.get()); - } } diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperTransporterTest.java b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperTransporterTest.java index 2fe78745add..c9339cf6673 100644 --- a/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperTransporterTest.java +++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/curator/CuratorZookeeperTransporterTest.java @@ -20,11 +20,9 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.remoting.zookeeper.ZookeeperClient; - -import org.apache.curator.test.TestingServer; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.IsNot.not; @@ -36,7 +34,7 @@ public class CuratorZookeeperTransporterTest { private CuratorZookeeperTransporter curatorZookeeperTransporter; private int zkServerPort; - @BeforeEach + @Before public void setUp() throws Exception { zkServerPort = NetUtils.getAvailablePort(); zkServer = new TestingServer(zkServerPort, true); @@ -45,14 +43,16 @@ public void setUp() throws Exception { curatorZookeeperTransporter = new CuratorZookeeperTransporter(); } + + @After + public void tearDown() throws Exception { + zkServer.stop(); + } + @Test public void testZookeeperClient() { assertThat(zookeeperClient, not(nullValue())); zookeeperClient.close(); } - @AfterEach - public void tearDown() throws Exception { - zkServer.stop(); - } } diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/support/AbstractZookeeperTransporterTest.java b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/support/AbstractZookeeperTransporterTest.java index eb6965a0c65..b089e3d42f4 100644 --- a/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/support/AbstractZookeeperTransporterTest.java +++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/support/AbstractZookeeperTransporterTest.java @@ -24,10 +24,10 @@ import org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperTransporter; import org.apache.curator.test.TestingServer; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import java.util.List; @@ -44,7 +44,7 @@ public class AbstractZookeeperTransporterTest { private AbstractZookeeperTransporter abstractZookeeperTransporter; private int zkServerPort; - @BeforeEach + @Before public void setUp() throws Exception { zkServerPort = NetUtils.getAvailablePort(); zkServer = new TestingServer(zkServerPort, true); @@ -54,7 +54,7 @@ public void setUp() throws Exception { } - @AfterEach + @After public void tearDown() throws Exception { zkServer.stop(); } @@ -69,14 +69,14 @@ public void testZookeeperClient() { public void testCreateServerURL() { URL url = URL.valueOf("zookeeper://127.0.0.1:" + zkServerPort + "/org.apache.dubbo.registry.RegistryService?application=metadatareport-local-xml-provider2&dubbo=2.0.2&interface=org.apache.dubbo.registry.RegistryService&pid=47418&specVersion=2.7.0-SNAPSHOT×tamp=1547102428828&timeout=2300"); URL newUrl = abstractZookeeperTransporter.toClientURL(url); - Assertions.assertEquals(newUrl.getProtocol(), "zookeeper"); - Assertions.assertEquals(newUrl.getHost(), "127.0.0.1"); - Assertions.assertEquals(newUrl.getPort(), zkServerPort); - Assertions.assertNull(newUrl.getUsername()); - Assertions.assertNull(newUrl.getPassword()); - Assertions.assertEquals(newUrl.getParameter(Constants.TIMEOUT_KEY, 5000), 2300); - Assertions.assertEquals(newUrl.getParameters().size(), 1); - Assertions.assertEquals(newUrl.getPath(), ZookeeperTransporter.class.getName()); + Assert.assertEquals(newUrl.getProtocol(), "zookeeper"); + Assert.assertEquals(newUrl.getHost(), "127.0.0.1"); + Assert.assertEquals(newUrl.getPort(), zkServerPort); + Assert.assertNull(newUrl.getUsername()); + Assert.assertNull(newUrl.getPassword()); + Assert.assertEquals(newUrl.getParameter(Constants.TIMEOUT_KEY, 5000), 2300); + Assert.assertEquals(newUrl.getParameters().size(), 1); + Assert.assertEquals(newUrl.getPath(), ZookeeperTransporter.class.getName()); } @@ -84,30 +84,30 @@ public void testCreateServerURL() { public void testToCreateURLWhenHasUser() { URL url = URL.valueOf("zookeeper://us2:pw2@127.0.0.1:" + zkServerPort + "/org.apache.dubbo.registry.RegistryService?application=metadatareport-local-xml-provider2&dubbo=2.0.2&interface=org.apache.dubbo.registry.RegistryService&pid=47418&specVersion=2.7.0-SNAPSHOT×tamp=1547102428828"); URL newUrl = abstractZookeeperTransporter.toClientURL(url); - Assertions.assertEquals(newUrl.getProtocol(), "zookeeper"); - Assertions.assertEquals(newUrl.getHost(), "127.0.0.1"); - Assertions.assertEquals(newUrl.getPort(), zkServerPort); - Assertions.assertEquals(newUrl.getUsername(), "us2"); - Assertions.assertEquals(newUrl.getPassword(), "pw2"); - Assertions.assertEquals(newUrl.getParameters().size(), 0); - Assertions.assertEquals(newUrl.getPath(), ZookeeperTransporter.class.getName()); + Assert.assertEquals(newUrl.getProtocol(), "zookeeper"); + Assert.assertEquals(newUrl.getHost(), "127.0.0.1"); + Assert.assertEquals(newUrl.getPort(), zkServerPort); + Assert.assertEquals(newUrl.getUsername(), "us2"); + Assert.assertEquals(newUrl.getPassword(), "pw2"); + Assert.assertEquals(newUrl.getParameters().size(), 0); + Assert.assertEquals(newUrl.getPath(), ZookeeperTransporter.class.getName()); } @Test public void testGetURLBackupAddress() { URL url = URL.valueOf("zookeeper://127.0.0.1:" + zkServerPort + "/org.apache.dubbo.registry.RegistryService?backup=127.0.0.1:" + 9099 + "&application=metadatareport-local-xml-provider2&dubbo=2.0.2&interface=org.apache.dubbo.registry.RegistryService&pid=47418&specVersion=2.7.0-SNAPSHOT×tamp=1547102428828"); List stringList = abstractZookeeperTransporter.getURLBackupAddress(url); - Assertions.assertEquals(stringList.size(), 2); - Assertions.assertEquals(stringList.get(0), "127.0.0.1:" + zkServerPort); - Assertions.assertEquals(stringList.get(1), "127.0.0.1:9099"); + Assert.assertEquals(stringList.size(), 2); + Assert.assertEquals(stringList.get(0), "127.0.0.1:" + zkServerPort); + Assert.assertEquals(stringList.get(1), "127.0.0.1:9099"); } @Test public void testGetURLBackupAddressNoBack() { URL url = URL.valueOf("zookeeper://127.0.0.1:" + zkServerPort + "/org.apache.dubbo.registry.RegistryService?application=metadatareport-local-xml-provider2&dubbo=2.0.2&interface=org.apache.dubbo.registry.RegistryService&pid=47418&specVersion=2.7.0-SNAPSHOT×tamp=1547102428828"); List stringList = abstractZookeeperTransporter.getURLBackupAddress(url); - Assertions.assertEquals(stringList.size(), 1); - Assertions.assertEquals(stringList.get(0), "127.0.0.1:" + zkServerPort); + Assert.assertEquals(stringList.size(), 1); + Assert.assertEquals(stringList.get(0), "127.0.0.1:" + zkServerPort); } @Test @@ -122,8 +122,8 @@ public void testFetchAndUpdateZookeeperClientCache() throws Exception { ZookeeperClient newZookeeperClient = abstractZookeeperTransporter.connect(url); //just for connected newZookeeperClient.getContent("/dubbo/test"); - Assertions.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().size(), 3); - Assertions.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().get("127.0.0.1:" + zkServerPort), newZookeeperClient); + Assert.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().size(), 3); + Assert.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().get("127.0.0.1:" + zkServerPort), newZookeeperClient); URL url2 = URL.valueOf("zookeeper://127.0.0.1:" + zkServerPort + "/org.apache.dubbo.metadata.store.MetadataReport?address=zookeeper://127.0.0.1:2181&application=metadatareport-local-xml-provider2&cycle-report=false&interface=org.apache.dubbo.metadata.store.MetadataReport&retry-period=4590&retry-times=23&sync-report=true"); checkFetchAndUpdateCacheNotNull(url2); @@ -137,7 +137,7 @@ public void testFetchAndUpdateZookeeperClientCache() throws Exception { private void checkFetchAndUpdateCacheNotNull(URL url) { List addressList = abstractZookeeperTransporter.getURLBackupAddress(url); ZookeeperClient zookeeperClient = abstractZookeeperTransporter.fetchAndUpdateZookeeperClientCache(addressList); - Assertions.assertNotNull(zookeeperClient); + Assert.assertNotNull(zookeeperClient); } @Test @@ -147,16 +147,16 @@ public void testRepeatConnect() { ZookeeperClient newZookeeperClient = abstractZookeeperTransporter.connect(url); //just for connected newZookeeperClient.getContent("/dubbo/test"); - Assertions.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().size(), 1); - Assertions.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().get("127.0.0.1:" + zkServerPort), newZookeeperClient); - Assertions.assertTrue(newZookeeperClient.isConnected()); + Assert.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().size(), 1); + Assert.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().get("127.0.0.1:" + zkServerPort), newZookeeperClient); + Assert.assertTrue(newZookeeperClient.isConnected()); ZookeeperClient newZookeeperClient2 = abstractZookeeperTransporter.connect(url2); //just for connected newZookeeperClient2.getContent("/dubbo/test"); - Assertions.assertEquals(newZookeeperClient, newZookeeperClient2); - Assertions.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().size(), 1); - Assertions.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().get("127.0.0.1:" + zkServerPort), newZookeeperClient); + Assert.assertEquals(newZookeeperClient, newZookeeperClient2); + Assert.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().size(), 1); + Assert.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().get("127.0.0.1:" + zkServerPort), newZookeeperClient); } @Test @@ -169,15 +169,15 @@ public void testNotRepeatConnect() throws Exception { ZookeeperClient newZookeeperClient = abstractZookeeperTransporter.connect(url); //just for connected newZookeeperClient.getContent("/dubbo/test"); - Assertions.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().size(), 1); - Assertions.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().get("127.0.0.1:" + zkServerPort), newZookeeperClient); + Assert.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().size(), 1); + Assert.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().get("127.0.0.1:" + zkServerPort), newZookeeperClient); ZookeeperClient newZookeeperClient2 = abstractZookeeperTransporter.connect(url2); //just for connected newZookeeperClient2.getContent("/dubbo/test"); - Assertions.assertNotEquals(newZookeeperClient, newZookeeperClient2); - Assertions.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().size(), 2); - Assertions.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().get("127.0.0.1:" + zkServerPort2), newZookeeperClient2); + Assert.assertNotEquals(newZookeeperClient, newZookeeperClient2); + Assert.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().size(), 2); + Assert.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().get("127.0.0.1:" + zkServerPort2), newZookeeperClient2); zkServer2.stop(); } @@ -195,15 +195,15 @@ public void testRepeatConnectForBackUpAdd() throws Exception { ZookeeperClient newZookeeperClient = abstractZookeeperTransporter.connect(url); //just for connected newZookeeperClient.getContent("/dubbo/test"); - Assertions.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().size(), 2); - Assertions.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().get("127.0.0.1:" + zkServerPort), newZookeeperClient); + Assert.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().size(), 2); + Assert.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().get("127.0.0.1:" + zkServerPort), newZookeeperClient); ZookeeperClient newZookeeperClient2 = abstractZookeeperTransporter.connect(url2); //just for connected newZookeeperClient2.getContent("/dubbo/test"); - Assertions.assertEquals(newZookeeperClient, newZookeeperClient2); - Assertions.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().size(), 3); - Assertions.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().get("127.0.0.1:" + zkServerPort2), newZookeeperClient2); + Assert.assertEquals(newZookeeperClient, newZookeeperClient2); + Assert.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().size(), 3); + Assert.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().get("127.0.0.1:" + zkServerPort2), newZookeeperClient2); zkServer2.stop(); zkServer3.stop(); @@ -222,15 +222,15 @@ public void testRepeatConnectForNoMatchBackUpAdd() throws Exception { ZookeeperClient newZookeeperClient = abstractZookeeperTransporter.connect(url); //just for connected newZookeeperClient.getContent("/dubbo/test"); - Assertions.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().size(), 2); - Assertions.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().get("127.0.0.1:" + zkServerPort), newZookeeperClient); + Assert.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().size(), 2); + Assert.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().get("127.0.0.1:" + zkServerPort), newZookeeperClient); ZookeeperClient newZookeeperClient2 = abstractZookeeperTransporter.connect(url2); //just for connected newZookeeperClient2.getContent("/dubbo/test"); - Assertions.assertNotEquals(newZookeeperClient, newZookeeperClient2); - Assertions.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().size(), 3); - Assertions.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().get("127.0.0.1:" + zkServerPort2), newZookeeperClient2); + Assert.assertNotEquals(newZookeeperClient, newZookeeperClient2); + Assert.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().size(), 3); + Assert.assertEquals(abstractZookeeperTransporter.getZookeeperClientMap().get("127.0.0.1:" + zkServerPort2), newZookeeperClient2); zkServer2.stop(); zkServer3.stop(); diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkClientWrapperTest.java b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkClientWrapperTest.java new file mode 100644 index 00000000000..9105b842fac --- /dev/null +++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkClientWrapperTest.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 org.apache.dubbo.remoting.zookeeper.zkclient; + +import org.apache.dubbo.common.utils.NetUtils; +import org.I0Itec.zkclient.IZkChildListener; +import org.apache.curator.test.TestingServer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; + +public class ZkClientWrapperTest { + private TestingServer zkServer; + private ZkClientWrapper zkClientWrapper; + + @Before + public void setUp() throws Exception { + int zkServerPort = NetUtils.getAvailablePort(); + zkServer = new TestingServer(zkServerPort, true); + zkClientWrapper = new ZkClientWrapper("127.0.0.1:" + zkServerPort, 10000); + } + + @After + public void tearDown() throws Exception { + zkServer.stop(); + } + + @Test + public void testConnectedStatus() { + boolean connected = zkClientWrapper.isConnected(); + assertThat(connected, is(false)); + zkClientWrapper.start(); + + IZkChildListener listener = mock(IZkChildListener.class); + zkClientWrapper.subscribeChildChanges("/path", listener); + zkClientWrapper.unsubscribeChildChanges("/path", listener); + } +} \ No newline at end of file diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperClientTest.java b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperClientTest.java new file mode 100644 index 00000000000..6a45a8f7c6f --- /dev/null +++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperClientTest.java @@ -0,0 +1,140 @@ +/* + * 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 org.apache.dubbo.remoting.zookeeper.zkclient; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.utils.NetUtils; +import org.apache.dubbo.remoting.zookeeper.StateListener; +import org.I0Itec.zkclient.IZkChildListener; +import org.apache.curator.test.TestingServer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.CountDownLatch; + +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; + +public class ZkclientZookeeperClientTest { + private TestingServer zkServer; + private ZkclientZookeeperClient zkclientZookeeperClient; + + @Before + public void setUp() throws Exception { + int zkServerPort = NetUtils.getAvailablePort(); + zkServer = new TestingServer(zkServerPort, true); + zkclientZookeeperClient = new ZkclientZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:" + + zkServerPort + "/org.apache.dubbo.registry.RegistryService")); + } + + @Test + public void testCheckExists() { + String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers"; + zkclientZookeeperClient.create(path, false); + assertThat(zkclientZookeeperClient.checkExists(path), is(true)); + assertThat(zkclientZookeeperClient.checkExists(path + "/noneexits"), is(false)); + } + + @Test + public void testDeletePath() { + String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers"; + zkclientZookeeperClient.create(path, false); + assertThat(zkclientZookeeperClient.checkExists(path), is(true)); + + zkclientZookeeperClient.delete(path); + assertThat(zkclientZookeeperClient.checkExists(path), is(false)); + } + + @Test + public void testConnectState() throws Exception { + assertThat(zkclientZookeeperClient.isConnected(), is(true)); + final CountDownLatch stopLatch = new CountDownLatch(1); + zkclientZookeeperClient.addStateListener(new StateListener() { + @Override + public void stateChanged(int connected) { + stopLatch.countDown(); + } + }); + zkServer.stop(); + stopLatch.await(); + assertThat(zkclientZookeeperClient.isConnected(), is(false)); + } + + @Test + public void testChildrenListener() throws InterruptedException { + String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers"; + zkclientZookeeperClient.create(path, false); + final CountDownLatch countDownLatch = new CountDownLatch(1); + zkclientZookeeperClient.addTargetChildListener(path, new IZkChildListener() { + @Override + public void handleChildChange(String s, List list) throws Exception { + countDownLatch.countDown(); + } + }); + zkclientZookeeperClient.createPersistent(path + "/provider1"); + countDownLatch.await(); + } + + @Test + public void testGetChildren() throws IOException { + String path = "/dubbo/org.apache.dubbo.demo.DemoService/parentProviders"; + zkclientZookeeperClient.create(path, false); + for (int i = 0; i < 5; i++) { + zkclientZookeeperClient.createEphemeral(path + "/server" + i); + } + List zookeeperClientChildren = zkclientZookeeperClient.getChildren(path); + assertThat(zookeeperClientChildren, hasSize(5)); + } + + @Test + public void testCreateContentPersistent() { + String path = "/ZkclientZookeeperClient/content.data"; + String content = "createContentTest"; + zkclientZookeeperClient.delete(path); + assertThat(zkclientZookeeperClient.checkExists(path), is(false)); + assertNull(zkclientZookeeperClient.getContent(path)); + + zkclientZookeeperClient.create(path, content, false); + assertThat(zkclientZookeeperClient.checkExists(path), is(true)); + assertEquals(zkclientZookeeperClient.getContent(path), content); + } + + @Test + public void testCreateContentTem() { + String path = "/ZkclientZookeeperClient/content.data"; + String content = "createContentTest"; + zkclientZookeeperClient.delete(path); + assertThat(zkclientZookeeperClient.checkExists(path), is(false)); + assertNull(zkclientZookeeperClient.getContent(path)); + + zkclientZookeeperClient.create(path, content, true); + assertThat(zkclientZookeeperClient.checkExists(path), is(true)); + assertEquals(zkclientZookeeperClient.getContent(path), content); + } + + @After + public void tearDown() throws Exception { + zkclientZookeeperClient.close(); + zkServer.stop(); + } +} diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperTransporterTest.java b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperTransporterTest.java new file mode 100644 index 00000000000..7d2755bc712 --- /dev/null +++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/org/apache/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperTransporterTest.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 org.apache.dubbo.remoting.zookeeper.zkclient; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.utils.NetUtils; +import org.apache.dubbo.remoting.zookeeper.ZookeeperClient; +import org.apache.curator.test.TestingServer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.IsNot.not; +import static org.hamcrest.core.IsNull.nullValue; + +public class ZkclientZookeeperTransporterTest { + private TestingServer zkServer; + private ZookeeperClient zookeeperClient; + + @Before + public void setUp() throws Exception { + int zkServerPort = NetUtils.getAvailablePort(); + zkServer = new TestingServer(zkServerPort, true); + zookeeperClient = new ZkclientZookeeperTransporter().connect(URL.valueOf("zookeeper://127.0.0.1:" + + zkServerPort + "/service")); + } + + @Test + public void testZookeeperClient() { + assertThat(zookeeperClient, not(nullValue())); + zookeeperClient.close(); + } + + @After + public void tearDown() throws Exception { + zkServer.stop(); + } +} \ No newline at end of file diff --git a/dubbo-remoting/pom.xml b/dubbo-remoting/pom.xml index fd17dbf0cd6..e756783ba8a 100644 --- a/dubbo-remoting/pom.xml +++ b/dubbo-remoting/pom.xml @@ -20,7 +20,7 @@ org.apache.dubbo dubbo-parent - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-remoting pom @@ -38,6 +38,5 @@ dubbo-remoting-http dubbo-remoting-zookeeper dubbo-remoting-netty4 - dubbo-remoting-etcd3 \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-api/pom.xml b/dubbo-rpc/dubbo-rpc-api/pom.xml index 06d17aec9e9..2467d3f5fbe 100644 --- a/dubbo-rpc/dubbo-rpc-api/pom.xml +++ b/dubbo-rpc/dubbo-rpc-api/pom.xml @@ -1,48 +1,48 @@ - - - 4.0.0 - - org.apache.dubbo - dubbo-rpc - 2.7.1-SNAPSHOT - - dubbo-rpc-api - jar - ${project.artifactId} - The rpc module of dubbo project - - false - - - - org.apache.dubbo - dubbo-common - ${project.parent.version} - - - org.apache.dubbo - dubbo-serialization-api - ${project.parent.version} - - - org.apache.dubbo - dubbo-remoting-api - ${project.parent.version} - - + + + 4.0.0 + + org.apache.dubbo + dubbo-rpc + 2.7.0-SNAPSHOT + + dubbo-rpc-api + jar + ${project.artifactId} + The rpc module of dubbo project + + false + + + + org.apache.dubbo + dubbo-common + ${project.parent.version} + + + org.apache.dubbo + dubbo-serialization-api + ${project.parent.version} + + + org.apache.dubbo + dubbo-remoting-api + ${project.parent.version} + + \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AbstractResult.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AbstractResult.java index b898934c63c..760a9c1a111 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AbstractResult.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AbstractResult.java @@ -16,8 +16,6 @@ */ package org.apache.dubbo.rpc; -import org.apache.dubbo.common.utils.StringUtils; - import java.util.HashMap; import java.util.Map; @@ -60,7 +58,7 @@ public String getAttachment(String key) { @Override public String getAttachment(String key, String defaultValue) { String result = attachments.get(key); - if (StringUtils.isEmpty(result)) { + if (result == null || result.length() == 0) { result = defaultValue; } return result; diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java index 7c10081e5f2..aaa92c875c7 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java @@ -19,9 +19,7 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.threadlocal.InternalThreadLocal; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.NetUtils; -import org.apache.dubbo.common.utils.StringUtils; import java.net.InetSocketAddress; import java.util.ArrayList; @@ -366,7 +364,7 @@ public String getLocalAddressString() { */ public String getLocalHostName() { String host = localAddress == null ? null : localAddress.getHostName(); - if (StringUtils.isEmpty(host)) { + if (host == null || host.length() == 0) { return getLocalHost(); } return host; @@ -606,7 +604,7 @@ public List> getInvokers() { public RpcContext setInvokers(List> invokers) { this.invokers = invokers; - if (CollectionUtils.isNotEmpty(invokers)) { + if (invokers != null && !invokers.isEmpty()) { List urls = new ArrayList(invokers.size()); for (Invoker invoker : invokers) { urls.add(invoker.getUrl()); diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcException.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcException.java index dc8a1b416b5..deb37caa588 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcException.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcException.java @@ -34,10 +34,7 @@ public static final int SERIALIZATION_EXCEPTION = 5; public static final int NO_INVOKER_AVAILABLE_AFTER_FILTER = 6; private static final long serialVersionUID = 7815426752583648734L; - /** - * RpcException cannot be extended, use error code for exception type to keep compatibility - */ - private int code; + private int code; // RpcException cannot be extended, use error code for exception type to keep compatibility public RpcException() { super(); diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java index 66d68de4c1c..2d949d532be 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java @@ -18,7 +18,6 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.utils.StringUtils; import java.io.Serializable; import java.lang.reflect.Method; @@ -214,7 +213,7 @@ public String getAttachment(String key, String defaultValue) { return defaultValue; } String value = attachments.get(key); - if (StringUtils.isEmpty(value)) { + if (value == null || value.length() == 0) { return defaultValue; } return value; diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/AccessLogFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/AccessLogFilter.java index f1bffc2a30b..89eb070603d 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/AccessLogFilter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/AccessLogFilter.java @@ -29,20 +29,21 @@ import org.apache.dubbo.rpc.Result; import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.rpc.RpcException; -import org.apache.dubbo.rpc.support.AccessLogData; + +import com.alibaba.fastjson.JSON; import java.io.File; import java.io.FileWriter; -import java.io.IOException; -import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; /** @@ -66,134 +67,141 @@ public class AccessLogFilter implements Filter { private static final String ACCESS_LOG_KEY = "dubbo.accesslog"; - private static final int LOG_MAX_BUFFER = 5000; + private static final String FILE_DATE_FORMAT = "yyyyMMdd"; - private static final long LOG_OUTPUT_INTERVAL = 5000; + private static final String MESSAGE_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; - private static final String FILE_DATE_FORMAT = "yyyyMMdd"; + private static final int LOG_MAX_BUFFER = 5000; - // It's safe to declare it as singleton since it runs on single thread only - private static final DateFormat FILE_NAME_FORMATTER = new SimpleDateFormat(FILE_DATE_FORMAT); + private static final long LOG_OUTPUT_INTERVAL = 5000; - private static final Map> logEntries = new ConcurrentHashMap>(); + private final ConcurrentMap> logQueue = new ConcurrentHashMap>(); - private static final ScheduledExecutorService logScheduled = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Dubbo-Access-Log", true)); + private final ScheduledExecutorService logScheduled = Executors.newScheduledThreadPool(2, new NamedThreadFactory("Dubbo-Access-Log", true)); - /** - * Default constructor initialize demon thread for writing into access log file with names with access log key - * defined in url accesslog - */ - public AccessLogFilter() { - logScheduled.scheduleWithFixedDelay(this::writeLogToFile, LOG_OUTPUT_INTERVAL, LOG_OUTPUT_INTERVAL, TimeUnit.MILLISECONDS); - } + private volatile ScheduledFuture logFuture = null; - /** - * This method logs the access log for service method invocation call. - * - * @param invoker service - * @param inv Invocation service method. - * @return Result from service method. - * @throws RpcException - */ - @Override - public Result invoke(Invoker invoker, Invocation inv) throws RpcException { - try { - String accessLogKey = invoker.getUrl().getParameter(Constants.ACCESS_LOG_KEY); - if (ConfigUtils.isNotEmpty(accessLogKey)) { - AccessLogData logData = buildAccessLogData(invoker, inv); - log(accessLogKey, logData); + private void init() { + if (logFuture == null) { + synchronized (logScheduled) { + if (logFuture == null) { + logFuture = logScheduled.scheduleWithFixedDelay(new LogTask(), LOG_OUTPUT_INTERVAL, LOG_OUTPUT_INTERVAL, TimeUnit.MILLISECONDS); + } } - } catch (Throwable t) { - logger.warn("Exception in AccessLogFilter of service(" + invoker + " -> " + inv + ")", t); } - return invoker.invoke(inv); } - private void log(String accessLog, AccessLogData accessLogData) { - Set logSet = logEntries.computeIfAbsent(accessLog, k -> new ConcurrentHashSet<>()); - + private void log(String accesslog, String logmessage) { + init(); + Set logSet = logQueue.get(accesslog); + if (logSet == null) { + logQueue.putIfAbsent(accesslog, new ConcurrentHashSet()); + logSet = logQueue.get(accesslog); + } if (logSet.size() < LOG_MAX_BUFFER) { - logSet.add(accessLogData); - } else { - //TODO we needs use force writing to file so that buffer gets clear and new log can be written. - logger.warn("AccessLog buffer is full skipping buffer "); + logSet.add(logmessage); } } - private void writeLogToFile() { - if (!logEntries.isEmpty()) { - for (Map.Entry> entry : logEntries.entrySet()) { - try { - String accessLog = entry.getKey(); - Set logSet = entry.getValue(); - if (ConfigUtils.isDefault(accessLog)) { - processWithServiceLogger(logSet); - } else { - File file = new File(accessLog); - createIfLogDirAbsent(file); - if (logger.isDebugEnabled()) { - logger.debug("Append log to " + accessLog); + @Override + public Result invoke(Invoker invoker, Invocation inv) throws RpcException { + try { + String accesslog = invoker.getUrl().getParameter(Constants.ACCESS_LOG_KEY); + if (ConfigUtils.isNotEmpty(accesslog)) { + RpcContext context = RpcContext.getContext(); + String serviceName = invoker.getInterface().getName(); + String version = invoker.getUrl().getParameter(Constants.VERSION_KEY); + String group = invoker.getUrl().getParameter(Constants.GROUP_KEY); + StringBuilder sn = new StringBuilder(); + sn.append("[").append(new SimpleDateFormat(MESSAGE_DATE_FORMAT).format(new Date())).append("] ").append(context.getRemoteHost()).append(":").append(context.getRemotePort()) + .append(" -> ").append(context.getLocalHost()).append(":").append(context.getLocalPort()) + .append(" - "); + if (null != group && group.length() > 0) { + sn.append(group).append("/"); + } + sn.append(serviceName); + if (null != version && version.length() > 0) { + sn.append(":").append(version); + } + sn.append(" "); + sn.append(inv.getMethodName()); + sn.append("("); + Class[] types = inv.getParameterTypes(); + if (types != null && types.length > 0) { + boolean first = true; + for (Class type : types) { + if (first) { + first = false; + } else { + sn.append(","); } - renameFile(file); - processWithAccessKeyLogger(logSet, file); + sn.append(type.getName()); } - - } catch (Exception e) { - logger.error(e.getMessage(), e); + } + sn.append(") "); + Object[] args = inv.getArguments(); + if (args != null && args.length > 0) { + sn.append(JSON.toJSONString(args)); + } + String msg = sn.toString(); + if (ConfigUtils.isDefault(accesslog)) { + LoggerFactory.getLogger(ACCESS_LOG_KEY + "." + invoker.getInterface().getName()).info(msg); + } else { + log(accesslog, msg); } } + } catch (Throwable t) { + logger.warn("Exception in AcessLogFilter of service(" + invoker + " -> " + inv + ")", t); } + return invoker.invoke(inv); } - private void processWithAccessKeyLogger(Set logSet, File file) throws IOException { - try (FileWriter writer = new FileWriter(file, true)) { - for (Iterator iterator = logSet.iterator(); - iterator.hasNext(); - iterator.remove()) { - writer.write(iterator.next().getLogMessage()); - writer.write("\r\n"); + private class LogTask implements Runnable { + @Override + public void run() { + try { + if (logQueue != null && logQueue.size() > 0) { + for (Map.Entry> entry : logQueue.entrySet()) { + try { + String accesslog = entry.getKey(); + Set logSet = entry.getValue(); + File file = new File(accesslog); + File dir = file.getParentFile(); + if (null != dir && !dir.exists()) { + dir.mkdirs(); + } + if (logger.isDebugEnabled()) { + logger.debug("Append log to " + accesslog); + } + if (file.exists()) { + String now = new SimpleDateFormat(FILE_DATE_FORMAT).format(new Date()); + String last = new SimpleDateFormat(FILE_DATE_FORMAT).format(new Date(file.lastModified())); + if (!now.equals(last)) { + File archive = new File(file.getAbsolutePath() + "." + last); + file.renameTo(archive); + } + } + FileWriter writer = new FileWriter(file, true); + try { + for (Iterator iterator = logSet.iterator(); + iterator.hasNext(); + iterator.remove()) { + writer.write(iterator.next()); + writer.write("\r\n"); + } + writer.flush(); + } finally { + writer.close(); + } + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + } + } + } catch (Exception e) { + logger.error(e.getMessage(), e); } - writer.flush(); - } - } - - private AccessLogData buildAccessLogData(Invoker invoker, Invocation inv) { - RpcContext context = RpcContext.getContext(); - AccessLogData logData = AccessLogData.newLogData(); - logData.setServiceName(invoker.getInterface().getName()); - logData.setMethodName(inv.getMethodName()); - logData.setVersion(invoker.getUrl().getParameter(Constants.VERSION_KEY)); - logData.setGroup(invoker.getUrl().getParameter(Constants.GROUP_KEY)); - logData.setInvocationTime(new Date()); - logData.setTypes(inv.getParameterTypes()); - logData.setArguments(inv.getArguments()); - return logData; - } - - private void processWithServiceLogger(Set logSet) { - for (Iterator iterator = logSet.iterator(); - iterator.hasNext(); - iterator.remove()) { - AccessLogData logData = iterator.next(); - LoggerFactory.getLogger(ACCESS_LOG_KEY + "." + logData.getServiceName()).info(logData.getLogMessage()); - } - } - - private void createIfLogDirAbsent(File file) { - File dir = file.getParentFile(); - if (null != dir && !dir.exists()) { - dir.mkdirs(); } } - private void renameFile(File file) { - if (file.exists()) { - String now = FILE_NAME_FORMATTER.format(new Date()); - String last = FILE_NAME_FORMATTER.format(new Date(file.lastModified())); - if (!now.equals(last)) { - File archive = new File(file.getAbsolutePath() + "." + last); - file.renameTo(archive); - } - } - } } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ActiveLimitFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ActiveLimitFilter.java index 1c2f69fecb9..926fd019425 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ActiveLimitFilter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ActiveLimitFilter.java @@ -47,12 +47,12 @@ public Result invoke(Invoker invoker, Invocation invocation) throws RpcExcept String methodName = invocation.getMethodName(); int max = invoker.getUrl().getMethodParameter(methodName, Constants.ACTIVES_KEY, 0); RpcStatus count = RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName()); - if (!count.beginCount(url, methodName, max)) { + if (!RpcStatus.beginCount(url, methodName, max)) { long timeout = invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.TIMEOUT_KEY, 0); long start = System.currentTimeMillis(); long remain = timeout; synchronized (count) { - while (!count.beginCount(url, methodName, max)) { + while (!RpcStatus.beginCount(url, methodName, max)) { try { count.wait(remain); } catch (InterruptedException e) { @@ -79,7 +79,7 @@ public Result invoke(Invoker invoker, Invocation invocation) throws RpcExcept isSuccess = false; throw t; } finally { - count.endCount(url, methodName, System.currentTimeMillis() - begin, isSuccess); + RpcStatus.endCount(url, methodName, System.currentTimeMillis() - begin, isSuccess); if (max > 0) { synchronized (count) { count.notifyAll(); diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ContextFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ContextFilter.java index 7f30151d1d1..db68ea1a7f3 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ContextFilter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ContextFilter.java @@ -42,9 +42,8 @@ public class ContextFilter implements Filter { public Result invoke(Invoker invoker, Invocation invocation) throws RpcException { Map attachments = invocation.getAttachments(); if (attachments != null) { - attachments = new HashMap<>(attachments); + attachments = new HashMap(attachments); attachments.remove(Constants.PATH_KEY); - attachments.remove(Constants.INTERFACE_KEY); attachments.remove(Constants.GROUP_KEY); attachments.remove(Constants.VERSION_KEY); attachments.remove(Constants.DUBBO_VERSION_KEY); diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java index ae7b92c40f1..7995468f675 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java @@ -76,7 +76,8 @@ public Result invoke(Invoker invoker, Invocation inv) throws RpcException { } else if (ProtocolUtils.isJavaGenericSerialization(generic)) { for (int i = 0; i < args.length; i++) { if (byte[].class == args[i].getClass()) { - try(UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream((byte[]) args[i])) { + try { + UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream((byte[]) args[i]); args[i] = ExtensionLoader.getExtensionLoader(Serialization.class) .getExtension(Constants.GENERIC_SERIALIZATION_NATIVE_JAVA) .deserialize(null, is).readObject(); diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/TimeoutFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/TimeoutFilter.java index c00689fc1c8..ad783efd992 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/TimeoutFilter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/TimeoutFilter.java @@ -25,7 +25,6 @@ import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Result; import org.apache.dubbo.rpc.RpcException; -import org.apache.dubbo.rpc.RpcInvocation; import java.util.Arrays; @@ -37,38 +36,21 @@ public class TimeoutFilter implements Filter { private static final Logger logger = LoggerFactory.getLogger(TimeoutFilter.class); - private static final String TIMEOUT_FILTER_START_TIME = "timeout_filter_start_time"; - @Override public Result invoke(Invoker invoker, Invocation invocation) throws RpcException { - if (invocation.getAttachments() != null) { - long start = System.currentTimeMillis(); - invocation.getAttachments().put(TIMEOUT_FILTER_START_TIME, String.valueOf(start)); - } else { - if (invocation instanceof RpcInvocation) { - RpcInvocation invc = (RpcInvocation) invocation; - long start = System.currentTimeMillis(); - invc.setAttachment(TIMEOUT_FILTER_START_TIME, String.valueOf(start)); - } - } - return invoker.invoke(invocation); - } - - @Override - public Result onResponse(Result result, Invoker invoker, Invocation invocation) { - String startAttach = invocation.getAttachment(TIMEOUT_FILTER_START_TIME); - if (startAttach != null) { - long elapsed = System.currentTimeMillis() - Long.valueOf(startAttach); - if (invoker.getUrl() != null - && elapsed > invoker.getUrl().getMethodParameter(invocation.getMethodName(), - "timeout", Integer.MAX_VALUE)) { - if (logger.isWarnEnabled()) { - logger.warn("invoke time out. method: " + invocation.getMethodName() - + " arguments: " + Arrays.toString(invocation.getArguments()) + " , url is " - + invoker.getUrl() + ", invoke elapsed " + elapsed + " ms."); - } + long start = System.currentTimeMillis(); + Result result = invoker.invoke(invocation); + long elapsed = System.currentTimeMillis() - start; + if (invoker.getUrl() != null + && elapsed > invoker.getUrl().getMethodParameter(invocation.getMethodName(), + "timeout", Integer.MAX_VALUE)) { + if (logger.isWarnEnabled()) { + logger.warn("invoke time out. method: " + invocation.getMethodName() + + " arguments: " + Arrays.toString(invocation.getArguments()) + " , url is " + + invoker.getUrl() + ", invoke elapsed " + elapsed + " ms."); } } return result; } + } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/tps/DefaultTPSLimiter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/tps/DefaultTPSLimiter.java index 65985bbf603..a623e2e6bdf 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/tps/DefaultTPSLimiter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/tps/DefaultTPSLimiter.java @@ -31,24 +31,21 @@ */ public class DefaultTPSLimiter implements TPSLimiter { - private final ConcurrentMap stats = new ConcurrentHashMap(); + private final ConcurrentMap stats + = new ConcurrentHashMap(); @Override public boolean isAllowable(URL url, Invocation invocation) { int rate = url.getParameter(Constants.TPS_LIMIT_RATE_KEY, -1); - long interval = url.getParameter(Constants.TPS_LIMIT_INTERVAL_KEY, Constants.DEFAULT_TPS_LIMIT_INTERVAL); + long interval = url.getParameter(Constants.TPS_LIMIT_INTERVAL_KEY, + Constants.DEFAULT_TPS_LIMIT_INTERVAL); String serviceKey = url.getServiceKey(); if (rate > 0) { StatItem statItem = stats.get(serviceKey); if (statItem == null) { - stats.putIfAbsent(serviceKey, new StatItem(serviceKey, rate, interval)); + stats.putIfAbsent(serviceKey, + new StatItem(serviceKey, rate, interval)); statItem = stats.get(serviceKey); - } else { - //rate or interval has changed, rebuild - if (statItem.getRate() != rate || statItem.getInterval() != interval) { - stats.put(serviceKey, new StatItem(serviceKey, rate, interval)); - statItem = stats.get(serviceKey); - } } return statItem.isAllowable(); } else { diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/tps/StatItem.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/tps/StatItem.java index 9fb3fab7d91..2fcdaef82a8 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/tps/StatItem.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/tps/StatItem.java @@ -16,7 +16,7 @@ */ package org.apache.dubbo.rpc.filter.tps; -import java.util.concurrent.atomic.LongAdder; +import java.util.concurrent.atomic.AtomicInteger; /** * Judge whether a particular invocation of service provider method should be allowed within a configured time interval. @@ -30,7 +30,7 @@ class StatItem { private long interval; - private LongAdder token; + private AtomicInteger token; private int rate; @@ -39,39 +39,32 @@ class StatItem { this.rate = rate; this.interval = interval; this.lastResetTime = System.currentTimeMillis(); - this.token = buildLongAdder(rate); + this.token = new AtomicInteger(rate); } public boolean isAllowable() { long now = System.currentTimeMillis(); if (now > lastResetTime + interval) { - token = buildLongAdder(rate); + token.set(rate); lastResetTime = now; } - if (token.sum() < 0) { - return false; + int value = token.get(); + boolean flag = false; + while (value > 0 && !flag) { + flag = token.compareAndSet(value, value - 1); + value = token.get(); } - token.decrement(); - return true; - } - - public long getInterval() { - return interval; - } - - public int getRate() { - return rate; + return flag; } - long getLastResetTime() { return lastResetTime; } - long getToken() { - return token.sum(); + int getToken() { + return token.get(); } @Override @@ -83,10 +76,4 @@ public String toString() { .toString(); } - private LongAdder buildLongAdder(int rate) { - LongAdder adder = new LongAdder(); - adder.add(rate); - return adder; - } - } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/listener/ListenerExporterWrapper.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/listener/ListenerExporterWrapper.java index 7ac188e21d0..ca7fd93aae1 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/listener/ListenerExporterWrapper.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/listener/ListenerExporterWrapper.java @@ -18,7 +18,6 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.rpc.Exporter; import org.apache.dubbo.rpc.ExporterListener; import org.apache.dubbo.rpc.Invoker; @@ -42,7 +41,7 @@ public ListenerExporterWrapper(Exporter exporter, List list } this.exporter = exporter; this.listeners = listeners; - if (CollectionUtils.isNotEmpty(listeners)) { + if (listeners != null && !listeners.isEmpty()) { RuntimeException exception = null; for (ExporterListener listener : listeners) { if (listener != null) { @@ -70,7 +69,7 @@ public void unexport() { try { exporter.unexport(); } finally { - if (CollectionUtils.isNotEmpty(listeners)) { + if (listeners != null && !listeners.isEmpty()) { RuntimeException exception = null; for (ExporterListener listener : listeners) { if (listener != null) { diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/listener/ListenerInvokerWrapper.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/listener/ListenerInvokerWrapper.java index 4c5498a383d..f3e8f8054f1 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/listener/ListenerInvokerWrapper.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/listener/ListenerInvokerWrapper.java @@ -19,7 +19,6 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.InvokerListener; @@ -45,7 +44,7 @@ public ListenerInvokerWrapper(Invoker invoker, List listener } this.invoker = invoker; this.listeners = listeners; - if (CollectionUtils.isNotEmpty(listeners)) { + if (listeners != null && !listeners.isEmpty()) { for (InvokerListener listener : listeners) { if (listener != null) { try { @@ -88,7 +87,7 @@ public void destroy() { try { invoker.destroy(); } finally { - if (CollectionUtils.isNotEmpty(listeners)) { + if (listeners != null && !listeners.isEmpty()) { for (InvokerListener listener : listeners) { if (listener != null) { try { diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AbstractInvoker.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AbstractInvoker.java index 3df1f1d741e..fd0cca6d8ed 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AbstractInvoker.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AbstractInvoker.java @@ -21,8 +21,6 @@ import org.apache.dubbo.common.Version; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.ArrayUtils; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; @@ -77,7 +75,7 @@ public AbstractInvoker(Class type, URL url, Map attachment) { } private static Map convertAttachment(URL url, String[] keys) { - if (ArrayUtils.isEmpty(keys)) { + if (keys == null || keys.length == 0) { return null; } Map attachment = new HashMap(); @@ -135,11 +133,11 @@ public Result invoke(Invocation inv) throws RpcException { } RpcInvocation invocation = (RpcInvocation) inv; invocation.setInvoker(this); - if (CollectionUtils.isNotEmptyMap(attachment)) { + if (attachment != null && attachment.size() > 0) { invocation.addAttachmentsIfAbsent(attachment); } Map contextAttachments = RpcContext.getContext().getAttachments(); - if (CollectionUtils.isNotEmptyMap(contextAttachments)) { + if (contextAttachments != null && contextAttachments.size() != 0) { /** * invocation.addAttachmentsIfAbsent(context){@link RpcInvocation#addAttachmentsIfAbsent(Map)}should not be used here, * because the {@link RpcContext#setAttachment(String, String)} is passed in the Filter when the call is triggered @@ -153,6 +151,7 @@ public Result invoke(Invocation inv) throws RpcException { } RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation); + try { return doInvoke(invocation); } catch (InvocationTargetException e) { // biz exception diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AbstractProxyProtocol.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AbstractProxyProtocol.java index a77a5bdc5f3..c87f244ac62 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AbstractProxyProtocol.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AbstractProxyProtocol.java @@ -28,7 +28,6 @@ import org.apache.dubbo.rpc.RpcException; import java.util.List; -import java.util.Objects; import java.util.concurrent.CopyOnWriteArrayList; /** @@ -67,10 +66,7 @@ public Exporter export(final Invoker invoker) throws RpcException { final String uri = serviceKey(invoker.getUrl()); Exporter exporter = (Exporter) exporterMap.get(uri); if (exporter != null) { - // When modifying the configuration through override, you need to re-expose the newly modified service. - if (Objects.equals(exporter.getInvoker().getUrl(), invoker.getUrl())) { - return exporter; - } + return exporter; } final Runnable runnable = doExport(proxyFactory.getProxy(invoker, true), invoker.getInterface(), invoker.getUrl()); exporter = new AbstractExporter(invoker) { diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/jdk/JdkProxyFactory.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/jdk/JdkProxyFactory.java index dc9bdfc0162..b74fb702567 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/jdk/JdkProxyFactory.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/jdk/JdkProxyFactory.java @@ -26,7 +26,7 @@ import java.lang.reflect.Proxy; /** - * JdkRpcProxyFactory + * JavaassistRpcProxyFactory */ public class JdkProxyFactory extends AbstractProxyFactory { diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/wrapper/StubProxyFactoryWrapper.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/wrapper/StubProxyFactoryWrapper.java index e4f7930d505..b1f6e7a5469 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/wrapper/StubProxyFactoryWrapper.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/wrapper/StubProxyFactoryWrapper.java @@ -18,7 +18,6 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.Version; import org.apache.dubbo.common.bytecode.Wrapper; import org.apache.dubbo.common.logger.Logger; @@ -65,12 +64,11 @@ public T getProxy(Invoker invoker, boolean generic) throws RpcException { public T getProxy(Invoker invoker) throws RpcException { T proxy = proxyFactory.getProxy(invoker); if (GenericService.class != invoker.getInterface()) { - URL url = invoker.getUrl(); - String stub = url.getParameter(Constants.STUB_KEY, url.getParameter(Constants.LOCAL_KEY)); + String stub = invoker.getUrl().getParameter(Constants.STUB_KEY, invoker.getUrl().getParameter(Constants.LOCAL_KEY)); if (ConfigUtils.isNotEmpty(stub)) { Class serviceType = invoker.getInterface(); if (ConfigUtils.isDefault(stub)) { - if (url.hasParameter(Constants.STUB_KEY)) { + if (invoker.getUrl().hasParameter(Constants.STUB_KEY)) { stub = serviceType.getName() + "Stub"; } else { stub = serviceType.getName() + "Local"; @@ -85,12 +83,12 @@ public T getProxy(Invoker invoker) throws RpcException { Constructor constructor = ReflectUtils.findConstructor(stubClass, serviceType); proxy = (T) constructor.newInstance(new Object[]{proxy}); //export stub service - URLBuilder urlBuilder = URLBuilder.from(url); + URL url = invoker.getUrl(); if (url.getParameter(Constants.STUB_EVENT_KEY, Constants.DEFAULT_STUB_EVENT)) { - urlBuilder.addParameter(Constants.STUB_EVENT_METHODS_KEY, StringUtils.join(Wrapper.getWrapper(proxy.getClass()).getDeclaredMethodNames(), ",")); - urlBuilder.addParameter(Constants.IS_SERVER_KEY, Boolean.FALSE.toString()); + url = url.addParameter(Constants.STUB_EVENT_METHODS_KEY, StringUtils.join(Wrapper.getWrapper(proxy.getClass()).getDeclaredMethodNames(), ",")); + url = url.addParameter(Constants.IS_SERVER_KEY, Boolean.FALSE.toString()); try { - export(proxy, (Class) invoker.getInterface(), urlBuilder.build()); + export(proxy, (Class) invoker.getInterface(), url); } catch (Exception e) { LOGGER.error("export a stub service error.", e); } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/service/GenericService.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/service/GenericService.java index 07517d474a4..124db22807b 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/service/GenericService.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/service/GenericService.java @@ -31,7 +31,7 @@ public interface GenericService { * @param parameterTypes Parameter types * @param args Arguments * @return invocation return value - * @throws GenericException potential exception thrown from the invocation + * @throws Throwable potential exception thrown from the invocation */ Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException; diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/AccessLogData.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/AccessLogData.java deleted file mode 100644 index b7d10961470..00000000000 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/AccessLogData.java +++ /dev/null @@ -1,266 +0,0 @@ -/* - * 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 org.apache.dubbo.rpc.support; - -import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.rpc.RpcContext; - -import com.alibaba.fastjson.JSON; - -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Arrays; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; - -/** - * AccessLogData is a container for log event data. In internally uses map and store each filed of log as value. It - * does not generate any dynamic value e.g. time stamp, local jmv machine host address etc. It does not allow any null - * or empty key. - * - * Note: since its date formatter is a singleton, make sure to run it in single thread only. - */ -public final class AccessLogData { - - private static final String MESSAGE_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; - private static final DateFormat MESSAGE_DATE_FORMATTER = new SimpleDateFormat(MESSAGE_DATE_FORMAT); - - private static final String VERSION = "version"; - private static final String GROUP = "group"; - private static final String SERVICE = "service"; - private static final String METHOD_NAME = "method-name"; - private static final String INVOCATION_TIME = "invocation-time"; - private static final String TYPES = "types"; - private static final String ARGUMENTS = "arguments"; - private static final String REMOTE_HOST = "remote-host"; - private static final String REMOTE_PORT = "remote-port"; - private static final String LOCAL_HOST = "localhost"; - private static final String LOCAL_PORT = "local-port"; - - /** - * This is used to store log data in key val format. - */ - private Map data; - - /** - * Default constructor. - */ - private AccessLogData() { - RpcContext context = RpcContext.getContext(); - data = new HashMap<>(); - setLocalHost(context.getLocalHost()); - setLocalPort(context.getLocalPort()); - setRemoteHost(context.getRemoteHost()); - setRemotePort(context.getRemotePort()); - } - - /** - * Get new instance of log data. - * - * @return instance of AccessLogData - */ - public static AccessLogData newLogData() { - return new AccessLogData(); - } - - - /** - * Add version information. - * - * @param version - */ - public void setVersion(String version) { - set(VERSION, version); - } - - /** - * Add service name. - * - * @param serviceName - */ - public void setServiceName(String serviceName) { - set(SERVICE, serviceName); - } - - /** - * Add group name - * - * @param group - */ - public void setGroup(String group) { - set(GROUP, group); - } - - /** - * Set the invocation date. As an argument it accept date string. - * - * @param invocationTime - */ - public void setInvocationTime(Date invocationTime) { - set(INVOCATION_TIME, invocationTime); - } - - /** - * Set caller remote host - * - * @param remoteHost - */ - private void setRemoteHost(String remoteHost) { - set(REMOTE_HOST, remoteHost); - } - - /** - * Set caller remote port. - * - * @param remotePort - */ - private void setRemotePort(Integer remotePort) { - set(REMOTE_PORT, remotePort); - } - - /** - * Set local host - * - * @param localHost - */ - private void setLocalHost(String localHost) { - set(LOCAL_HOST, localHost); - } - - /** - * Set local port of exported service - * - * @param localPort - */ - private void setLocalPort(Integer localPort) { - set(LOCAL_PORT, localPort); - } - - /** - * Set target method name. - * - * @param methodName - */ - public void setMethodName(String methodName) { - set(METHOD_NAME, methodName); - } - - /** - * Set invocation's method's input parameter's types - * - * @param types - */ - public void setTypes(Class[] types) { - set(TYPES, types != null ? Arrays.copyOf(types, types.length) : null); - } - - /** - * Sets invocation arguments - * - * @param arguments - */ - public void setArguments(Object[] arguments) { - set(ARGUMENTS, arguments != null ? Arrays.copyOf(arguments, arguments.length) : null); - } - - /** - * Return gthe service of access log entry - * - * @return - */ - public String getServiceName() { - return get(SERVICE).toString(); - } - - - public String getLogMessage() { - StringBuilder sn = new StringBuilder(); - - sn.append("[") - .append(MESSAGE_DATE_FORMATTER.format(getInvocationTime())) - .append("] ") - .append(get(REMOTE_HOST)) - .append(":") - .append(get(REMOTE_PORT)) - .append(" -> ") - .append(get(LOCAL_HOST)) - .append(":") - .append(get(LOCAL_PORT)) - .append(" - "); - - String group = get(GROUP) != null ? get(GROUP).toString() : ""; - if (StringUtils.isNotEmpty(group.toString())) { - sn.append(group).append("/"); - } - - sn.append(get(SERVICE)); - - String version = get(VERSION) != null ? get(VERSION).toString() : ""; - if (StringUtils.isNotEmpty(version.toString())) { - sn.append(":").append(version); - } - - sn.append(" "); - sn.append(get(METHOD_NAME)); - - sn.append("("); - Class[] types = get(TYPES) != null ? (Class[]) get(TYPES) : new Class[0]; - boolean first = true; - for (Class type : types) { - if (first) { - first = false; - } else { - sn.append(","); - } - sn.append(type.getName()); - } - sn.append(") "); - - - Object[] args = get(ARGUMENTS) != null ? (Object[]) get(ARGUMENTS) : null; - if (args != null && args.length > 0) { - sn.append(JSON.toJSONString(args)); - } - - return sn.toString(); - } - - private Date getInvocationTime() { - return (Date)get(INVOCATION_TIME); - } - /** - * Return value of key - * - * @param key - * @return - */ - private Object get(String key) { - return data.get(key); - } - - /** - * Add log key along with his value. - * - * @param key Any not null or non empty string - * @param value Any object including null. - */ - private void set(String key, Object value) { - data.put(key, value); - } - -} diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/MockInvoker.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/MockInvoker.java index 436fb4799b5..ef9dac3b89a 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/MockInvoker.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/MockInvoker.java @@ -23,7 +23,6 @@ import org.apache.dubbo.common.utils.PojoUtils; import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.common.utils.ArrayUtils; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.ProxyFactory; @@ -70,7 +69,7 @@ public static Object parseMockValue(String mock, Type[] returnTypes) throws Exce value = mock.subSequence(1, mock.length() - 1); } else if (returnTypes != null && returnTypes.length > 0 && returnTypes[0] == String.class) { value = mock; - } else if (StringUtils.isNumeric(mock, false)) { + } else if (StringUtils.isNumeric(mock)) { value = JSON.parse(mock); } else if (mock.startsWith("{")) { value = JSON.parseObject(mock, Map.class); @@ -79,7 +78,7 @@ public static Object parseMockValue(String mock, Type[] returnTypes) throws Exce } else { value = mock; } - if (ArrayUtils.isNotEmpty(returnTypes)) { + if (returnTypes != null && returnTypes.length > 0) { value = PojoUtils.realize(value, (Class) returnTypes[0], returnTypes.length > 1 ? returnTypes[1] : null); } return value; diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/ProtocolUtils.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/ProtocolUtils.java index ce5da6b51be..5242cbd893a 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/ProtocolUtils.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/ProtocolUtils.java @@ -18,7 +18,6 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.utils.StringUtils; public class ProtocolUtils { @@ -32,7 +31,7 @@ public static String serviceKey(URL url) { public static String serviceKey(int port, String serviceName, String serviceVersion, String serviceGroup) { StringBuilder buf = new StringBuilder(); - if (StringUtils.isNotEmpty(serviceGroup)) { + if (serviceGroup != null && serviceGroup.length() > 0) { buf.append(serviceGroup); buf.append("/"); } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java index a5ae76133cf..e5a389950c2 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java @@ -21,7 +21,6 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.ReflectUtils; -import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.RpcInvocation; @@ -48,7 +47,7 @@ public static Class getReturnType(Invocation invocation) { && invocation.getInvoker().getUrl() != null && !invocation.getMethodName().startsWith("$")) { String service = invocation.getInvoker().getUrl().getServiceInterface(); - if (StringUtils.isNotEmpty(service)) { + if (service != null && service.length() > 0) { Class invokerInterface = invocation.getInvoker().getInterface(); Class cls = invokerInterface != null ? ReflectUtils.forName(invokerInterface.getClassLoader(), service) : ReflectUtils.forName(service); @@ -72,7 +71,7 @@ public static Type[] getReturnTypes(Invocation invocation) { && invocation.getInvoker().getUrl() != null && !invocation.getMethodName().startsWith("$")) { String service = invocation.getInvoker().getUrl().getServiceInterface(); - if (StringUtils.isNotEmpty(service)) { + if (service != null && service.length() > 0) { Class invokerInterface = invocation.getInvoker().getInterface(); Class cls = invokerInterface != null ? ReflectUtils.forName(invokerInterface.getClassLoader(), service) : ReflectUtils.forName(service); diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcContextTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcContextTest.java index 86717ef7011..0d4d95f8158 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcContextTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcContextTest.java @@ -18,8 +18,8 @@ import org.apache.dubbo.common.URL; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.util.HashMap; import java.util.Map; @@ -30,19 +30,19 @@ public class RpcContextTest { public void testGetContext() { RpcContext rpcContext = RpcContext.getContext(); - Assertions.assertNotNull(rpcContext); + Assert.assertNotNull(rpcContext); RpcContext.removeContext(); // if null, will return the initialize value. - //Assertions.assertNull(RpcContext.getContext()); - Assertions.assertNotNull(RpcContext.getContext()); - Assertions.assertNotEquals(rpcContext, RpcContext.getContext()); + //Assert.assertNull(RpcContext.getContext()); + Assert.assertNotNull(RpcContext.getContext()); + Assert.assertNotEquals(rpcContext, RpcContext.getContext()); RpcContext serverRpcContext = RpcContext.getServerContext(); - Assertions.assertNotNull(serverRpcContext); + Assert.assertNotNull(serverRpcContext); RpcContext.removeServerContext(); - Assertions.assertNotEquals(serverRpcContext, RpcContext.getServerContext()); + Assert.assertNotEquals(serverRpcContext, RpcContext.getServerContext()); } @@ -50,19 +50,19 @@ public void testGetContext() { public void testAddress() { RpcContext context = RpcContext.getContext(); context.setLocalAddress("127.0.0.1", 20880); - Assertions.assertTrue(context.getLocalAddress().getPort() == 20880); - Assertions.assertEquals("127.0.0.1:20880", context.getLocalAddressString()); + Assert.assertTrue(context.getLocalAddress().getPort() == 20880); + Assert.assertEquals("127.0.0.1:20880", context.getLocalAddressString()); context.setRemoteAddress("127.0.0.1", 20880); - Assertions.assertTrue(context.getRemoteAddress().getPort() == 20880); - Assertions.assertEquals("127.0.0.1:20880", context.getRemoteAddressString()); + Assert.assertTrue(context.getRemoteAddress().getPort() == 20880); + Assert.assertEquals("127.0.0.1:20880", context.getRemoteAddressString()); context.setRemoteAddress("127.0.0.1", -1); context.setLocalAddress("127.0.0.1", -1); - Assertions.assertTrue(context.getRemoteAddress().getPort() == 0); - Assertions.assertTrue(context.getLocalAddress().getPort() == 0); - Assertions.assertEquals("127.0.0.1", context.getRemoteHostName()); - Assertions.assertEquals("127.0.0.1", context.getLocalHostName()); + Assert.assertTrue(context.getRemoteAddress().getPort() == 0); + Assert.assertTrue(context.getLocalAddress().getPort() == 0); + Assert.assertEquals("127.0.0.1", context.getRemoteHostName()); + Assert.assertEquals("127.0.0.1", context.getLocalHostName()); } @Test @@ -74,12 +74,12 @@ public void testCheckSide() { //context.isProviderSide(); context.setUrl(URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1")); - Assertions.assertFalse(context.isConsumerSide()); - Assertions.assertTrue(context.isProviderSide()); + Assert.assertFalse(context.isConsumerSide()); + Assert.assertTrue(context.isProviderSide()); context.setUrl(URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&side=consumer")); - Assertions.assertTrue(context.isConsumerSide()); - Assertions.assertFalse(context.isProviderSide()); + Assert.assertTrue(context.isConsumerSide()); + Assert.assertFalse(context.isProviderSide()); } @Test @@ -92,22 +92,22 @@ public void testAttachments() { map.put(".33", "3333"); context.setAttachments(map); - Assertions.assertEquals(map, context.getAttachments()); + Assert.assertEquals(map, context.getAttachments()); - Assertions.assertEquals("1111", context.getAttachment("_11")); + Assert.assertEquals("1111", context.getAttachment("_11")); context.setAttachment("_11", "11.11"); - Assertions.assertEquals("11.11", context.getAttachment("_11")); + Assert.assertEquals("11.11", context.getAttachment("_11")); context.setAttachment(null, "22222"); context.setAttachment("_22", null); - Assertions.assertEquals("22222", context.getAttachment(null)); - Assertions.assertNull(context.getAttachment("_22")); + Assert.assertEquals("22222", context.getAttachment(null)); + Assert.assertNull(context.getAttachment("_22")); - Assertions.assertNull(context.getAttachment("_33")); - Assertions.assertEquals("3333", context.getAttachment(".33")); + Assert.assertNull(context.getAttachment("_33")); + Assert.assertEquals("3333", context.getAttachment(".33")); context.clearAttachments(); - Assertions.assertNull(context.getAttachment("_11")); + Assert.assertNull(context.getAttachment("_11")); } @Test @@ -121,38 +121,38 @@ public void testObject() { map.forEach(context::set); - Assertions.assertEquals(map, context.get()); + Assert.assertEquals(map, context.get()); - Assertions.assertEquals("1111", context.get("_11")); + Assert.assertEquals("1111", context.get("_11")); context.set("_11", "11.11"); - Assertions.assertEquals("11.11", context.get("_11")); + Assert.assertEquals("11.11", context.get("_11")); context.set(null, "22222"); context.set("_22", null); - Assertions.assertEquals("22222", context.get(null)); - Assertions.assertNull(context.get("_22")); + Assert.assertEquals("22222", context.get(null)); + Assert.assertNull(context.get("_22")); - Assertions.assertNull(context.get("_33")); - Assertions.assertEquals("3333", context.get(".33")); + Assert.assertNull(context.get("_33")); + Assert.assertEquals("3333", context.get(".33")); map.keySet().forEach(context::remove); - Assertions.assertNull(context.get("_11")); + Assert.assertNull(context.get("_11")); } @Test public void testAsync() { RpcContext rpcContext = RpcContext.getContext(); - Assertions.assertFalse(rpcContext.isAsyncStarted()); + Assert.assertFalse(rpcContext.isAsyncStarted()); AsyncContext asyncContext = RpcContext.startAsync(); - Assertions.assertTrue(rpcContext.isAsyncStarted()); + Assert.assertTrue(rpcContext.isAsyncStarted()); asyncContext.write(new Object()); - Assertions.assertTrue(((AsyncContextImpl)asyncContext).getInternalFuture().isDone()); + Assert.assertTrue(((AsyncContextImpl)asyncContext).getInternalFuture().isDone()); rpcContext.stopAsync(); - Assertions.assertTrue(rpcContext.isAsyncStarted()); + Assert.assertTrue(rpcContext.isAsyncStarted()); } } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcResultTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcResultTest.java index eda5117f9e4..8083a604ea0 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcResultTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcResultTest.java @@ -17,10 +17,10 @@ package org.apache.dubbo.rpc; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.fail; public class RpcResultTest { @Test @@ -32,8 +32,8 @@ public void testRecreateWithNormalException() { fail(); } catch (Throwable throwable) { StackTraceElement[] stackTrace = throwable.getStackTrace(); - Assertions.assertNotNull(stackTrace); - Assertions.assertTrue(stackTrace.length > 1); + Assert.assertNotNull(stackTrace); + Assert.assertTrue(stackTrace.length > 1); } } @@ -70,8 +70,8 @@ public void testRecreateWithEmptyStackTraceException() { fail(); } catch (Throwable t) { StackTraceElement[] stackTrace = t.getStackTrace(); - Assertions.assertNotNull(stackTrace); - Assertions.assertTrue(stackTrace.length == 0); + Assert.assertNotNull(stackTrace); + Assert.assertTrue(stackTrace.length == 0); } } } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/AccessLogFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/AccessLogFilterTest.java index ca84381ff5d..05b1aa91f2a 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/AccessLogFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/AccessLogFilterTest.java @@ -24,9 +24,9 @@ import org.apache.dubbo.rpc.support.MockInvocation; import org.apache.dubbo.rpc.support.MyInvoker; -import org.junit.jupiter.api.Test; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; /** * AccessLogFilterTest.java @@ -42,7 +42,7 @@ public void testInvokeException() { Invocation invocation = new MockInvocation(); LogUtil.start(); accessLogFilter.invoke(invoker, invocation); - assertEquals(1, LogUtil.findMessage("Exception in AccessLogFilter of service")); + assertEquals(1, LogUtil.findMessage("Exception in AcessLogFilter of service")); LogUtil.stop(); } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ActiveLimitFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ActiveLimitFilterTest.java index 6d085f70617..2dff8861f28 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ActiveLimitFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ActiveLimitFilterTest.java @@ -27,14 +27,13 @@ import org.apache.dubbo.rpc.support.MyInvoker; import org.apache.dubbo.rpc.support.RuntimeExceptionInvoker; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; /** * ActiveLimitFilterTest.java @@ -105,7 +104,7 @@ public void testInvokeTimeOut() { AtomicInteger count = new AtomicInteger(0); final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latchBlocking = new CountDownLatch(totalThread); - URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&actives=" + maxActives + "&timeout=" + timeout); + URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&actives="+maxActives+"&timeout="+timeout); final Invoker invoker = new BlockMyInvoker(url, blockTime); final Invocation invocation = new MockInvocation(); RpcStatus.removeStatus(url); @@ -113,7 +112,7 @@ public void testInvokeTimeOut() { for (int i = 0; i < totalThread; i++) { Thread thread = new Thread(new Runnable() { public void run() { - try { + try{ try { latch.await(); } catch (InterruptedException e) { @@ -124,7 +123,7 @@ public void run() { } catch (RpcException expected) { count.incrementAndGet(); } - } finally { + }finally { latchBlocking.countDown(); } } @@ -150,13 +149,13 @@ public void testInvokeNotTimeOut() { AtomicInteger count = new AtomicInteger(0); final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latchBlocking = new CountDownLatch(totalThread); - URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&actives=" + maxActives + "&timeout=" + timeout); + URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&actives="+maxActives+"&timeout="+timeout); final Invoker invoker = new BlockMyInvoker(url, blockTime); final Invocation invocation = new MockInvocation(); for (int i = 0; i < totalThread; i++) { Thread thread = new Thread(new Runnable() { public void run() { - try { + try{ try { latch.await(); } catch (InterruptedException e) { @@ -167,7 +166,7 @@ public void run() { } catch (RpcException expected) { count.incrementAndGet(); } - } finally { + }finally { latchBlocking.countDown(); } } @@ -184,18 +183,17 @@ public void run() { assertEquals(0, count.intValue()); } - @Test + @Test(expected = RuntimeException.class) public void testInvokeRuntimeException() { - Assertions.assertThrows(RuntimeException.class, () -> { - URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&actives=0"); - Invoker invoker = new RuntimeExceptionInvoker(url); - Invocation invocation = new MockInvocation(); - RpcStatus count = RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName()); - int beforeExceptionActiveCount = count.getActive(); - activeLimitFilter.invoke(invoker, invocation); - int afterExceptionActiveCount = count.getActive(); - assertEquals(beforeExceptionActiveCount, afterExceptionActiveCount, "After exception active count should be same"); - }); + URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&actives=0"); + Invoker invoker = new RuntimeExceptionInvoker(url); + Invocation invocation = new MockInvocation(); + RpcStatus count = RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName()); + int beforeExceptionActiveCount = count.getActive(); + activeLimitFilter.invoke(invoker, invocation); + int afterExceptionActiveCount = count.getActive(); + assertEquals("After exception active count should be same" + , beforeExceptionActiveCount, afterExceptionActiveCount); } @Test @@ -209,7 +207,8 @@ public void testInvokeRuntimeExceptionWithActiveCountMatch() { activeLimitFilter.invoke(invoker, invocation); } catch (RuntimeException ex) { int afterExceptionActiveCount = count.getActive(); - assertEquals(beforeExceptionActiveCount, afterExceptionActiveCount, "After exception active count should be same"); + assertEquals("After exception active count should be same" + , beforeExceptionActiveCount, afterExceptionActiveCount); } } } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ClassLoaderFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ClassLoaderFilterTest.java index f75f4ab2e53..c439d46bc73 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ClassLoaderFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ClassLoaderFilterTest.java @@ -25,8 +25,8 @@ import org.apache.dubbo.rpc.support.DemoService; import org.apache.dubbo.rpc.support.MyInvoker; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import org.mockito.Mockito; import java.net.URLClassLoader; @@ -59,7 +59,7 @@ public Class getInterface() { @Override public Result invoke(Invocation invocation) throws RpcException { - Assertions.assertEquals(cl, Thread.currentThread().getContextClassLoader()); + Assert.assertEquals(cl, Thread.currentThread().getContextClassLoader()); return null; } }; diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/CompatibleFilterFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/CompatibleFilterFilterTest.java index b91c92d1640..b94090745e5 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/CompatibleFilterFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/CompatibleFilterFilterTest.java @@ -24,12 +24,12 @@ import org.apache.dubbo.rpc.RpcResult; import org.apache.dubbo.rpc.support.DemoService; import org.apache.dubbo.rpc.support.Type; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Test; import org.mockito.Mockito; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -41,7 +41,7 @@ public class CompatibleFilterFilterTest { private Invocation invocation; private Invoker invoker; - @AfterEach + @After public void tearDown() { Mockito.reset(invocation, invoker); } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ConsumerContextFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ConsumerContextFilterTest.java index a906af47be2..076361b3c9c 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ConsumerContextFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ConsumerContextFilterTest.java @@ -26,9 +26,9 @@ import org.apache.dubbo.rpc.support.MockInvocation; import org.apache.dubbo.rpc.support.MyInvoker; -import org.junit.jupiter.api.Test; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; /** * ConsumerContextFilterTest.java diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ContextFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ContextFilterTest.java index c753c7e0e2e..a954785a011 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ContextFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ContextFilterTest.java @@ -27,9 +27,9 @@ import org.apache.dubbo.rpc.support.MockInvocation; import org.apache.dubbo.rpc.support.MyInvoker; -import org.junit.jupiter.api.Test; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.Assert.assertNull; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/DeprecatedFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/DeprecatedFilterTest.java index 5795ee5245d..d3a973a121b 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/DeprecatedFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/DeprecatedFilterTest.java @@ -24,9 +24,9 @@ import org.apache.dubbo.rpc.support.MockInvocation; import org.apache.dubbo.rpc.support.MyInvoker; -import org.junit.jupiter.api.Test; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; /** * DeprecatedFilterTest.java diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/EchoFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/EchoFilterTest.java index 02f367b8a8e..ea9dd4d19cd 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/EchoFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/EchoFilterTest.java @@ -24,9 +24,9 @@ import org.apache.dubbo.rpc.RpcResult; import org.apache.dubbo.rpc.support.DemoService; -import org.junit.jupiter.api.Test; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ExceptionFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ExceptionFilterTest.java index f40f8776e45..9b44dd66068 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ExceptionFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ExceptionFilterTest.java @@ -29,11 +29,11 @@ import com.alibaba.com.caucho.hessian.HessianException; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import org.mockito.Mockito; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -85,7 +85,7 @@ public void testJavaException() { Result newResult = exceptionFilter.invoke(invoker, invocation); - Assertions.assertEquals(rpcResult.getException(), newResult.getException()); + Assert.assertEquals(rpcResult.getException(), newResult.getException()); } @@ -105,7 +105,7 @@ public void testRuntimeException() { Result newResult = exceptionFilter.invoke(invoker, invocation); - Assertions.assertEquals(rpcResult.getException(), newResult.getException()); + Assert.assertEquals(rpcResult.getException(), newResult.getException()); } @@ -127,10 +127,10 @@ public void testConvertToRunTimeException() { newResult = exceptionFilter.onResponse(newResult, invoker, invocation); - Assertions.assertFalse(newResult.getException() instanceof HessianException); + Assert.assertFalse(newResult.getException() instanceof HessianException); - Assertions.assertEquals(newResult.getException().getClass(), RuntimeException.class); - Assertions.assertEquals(newResult.getException().getMessage(), StringUtils.toString(rpcResult.getException())); + Assert.assertEquals(newResult.getException().getClass(), RuntimeException.class); + Assert.assertEquals(newResult.getException().getMessage(), StringUtils.toString(rpcResult.getException())); } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ExecuteLimitFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ExecuteLimitFilterTest.java index 2fab7894e32..f0a3b5cc5b7 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ExecuteLimitFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ExecuteLimitFilterTest.java @@ -25,8 +25,8 @@ import org.apache.dubbo.rpc.RpcStatus; import org.apache.dubbo.rpc.support.BlockMyInvoker; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import org.mockito.Mockito; import java.util.concurrent.CountDownLatch; @@ -50,7 +50,7 @@ public void testNoExecuteLimitInvoke() throws Exception { when(invocation.getMethodName()).thenReturn("testNoExecuteLimitInvoke"); Result result = executeLimitFilter.invoke(invoker, invocation); - Assertions.assertEquals("result", result.getValue()); + Assert.assertEquals("result", result.getValue()); } @Test @@ -63,7 +63,7 @@ public void testExecuteLimitInvoke() throws Exception { when(invocation.getMethodName()).thenReturn("testExecuteLimitInvoke"); Result result = executeLimitFilter.invoke(invoker, invocation); - Assertions.assertEquals("result", result.getValue()); + Assert.assertEquals("result", result.getValue()); } @Test @@ -81,9 +81,9 @@ public void testExecuteLimitInvokeWitException() throws Exception { try { executeLimitFilter.invoke(invoker, invocation); } catch (Exception e) { - Assertions.assertTrue(e instanceof RpcException); + Assert.assertTrue(e instanceof RpcException); } - Assertions.assertEquals(1, RpcStatus.getStatus(url, invocation.getMethodName()).getFailed()); + Assert.assertEquals(1, RpcStatus.getStatus(url, invocation.getMethodName()).getFailed()); } @Test @@ -126,6 +126,6 @@ public void run() { e.printStackTrace(); } - Assertions.assertEquals(totalExecute - maxExecute, failed.get()); + Assert.assertEquals(totalExecute - maxExecute, failed.get()); } } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericFilterTest.java index bd680e7502e..cc1ba030ece 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericFilterTest.java @@ -22,9 +22,8 @@ import org.apache.dubbo.rpc.service.GenericService; import org.apache.dubbo.rpc.support.DemoService; import org.apache.dubbo.rpc.support.Person; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import org.mockito.Mockito; import java.lang.reflect.Method; @@ -49,7 +48,7 @@ public void testInvokeWithDefault() throws Exception { RpcInvocation invocation = new RpcInvocation(Constants.$INVOKE, genericInvoke.getParameterTypes(), new Object[]{"getPerson", new String[]{Person.class.getCanonicalName()}, new Object[]{person}}); - URL url = URL.valueOf("test://test:11/org.apache.dubbo.rpc.support.DemoService?" + + URL url = URL.valueOf("test://test:11/com.alibaba.dubbo.rpc.support.DemoService?" + "accesslog=true&group=dubbo&version=1.1"); Invoker invoker = Mockito.mock(Invoker.class); when(invoker.invoke(any(Invocation.class))).thenReturn(new RpcResult(new Person("person", 10))); @@ -58,33 +57,32 @@ public void testInvokeWithDefault() throws Exception { Result result = genericFilter.invoke(invoker, invocation); - Assertions.assertEquals(HashMap.class, result.getValue().getClass()); - Assertions.assertEquals(10, ((HashMap) result.getValue()).get("age")); + Assert.assertEquals(HashMap.class, result.getValue().getClass()); + Assert.assertEquals(10, ((HashMap) result.getValue()).get("age")); } - @Test + @Test(expected = RpcException.class) public void testInvokeWithJavaException() throws Exception { - Assertions.assertThrows(RpcException.class, () -> { - Method genericInvoke = GenericService.class.getMethods()[0]; - - Map person = new HashMap(); - person.put("name", "dubbo"); - person.put("age", 10); - - RpcInvocation invocation = new RpcInvocation(Constants.$INVOKE, genericInvoke.getParameterTypes(), - new Object[]{"getPerson", new String[]{Person.class.getCanonicalName()}, new Object[]{person}}); - invocation.setAttachment(Constants.GENERIC_KEY, Constants.GENERIC_SERIALIZATION_NATIVE_JAVA); - - URL url = URL.valueOf("test://test:11/org.apache.dubbo.rpc.support.DemoService?" + - "accesslog=true&group=dubbo&version=1.1"); - Invoker invoker = Mockito.mock(Invoker.class); - when(invoker.invoke(any(Invocation.class))).thenReturn(new RpcResult(new Person("person", 10))); - when(invoker.getUrl()).thenReturn(url); - when(invoker.getInterface()).thenReturn(DemoService.class); - - genericFilter.invoke(invoker, invocation); - }); + + Method genericInvoke = GenericService.class.getMethods()[0]; + + Map person = new HashMap(); + person.put("name", "dubbo"); + person.put("age", 10); + + RpcInvocation invocation = new RpcInvocation(Constants.$INVOKE, genericInvoke.getParameterTypes(), + new Object[]{"getPerson", new String[]{Person.class.getCanonicalName()}, new Object[]{person}}); + invocation.setAttachment(Constants.GENERIC_KEY, Constants.GENERIC_SERIALIZATION_NATIVE_JAVA); + + URL url = URL.valueOf("test://test:11/com.alibaba.dubbo.rpc.support.DemoService?" + + "accesslog=true&group=dubbo&version=1.1"); + Invoker invoker = Mockito.mock(Invoker.class); + when(invoker.invoke(any(Invocation.class))).thenReturn(new RpcResult(new Person("person", 10))); + when(invoker.getUrl()).thenReturn(url); + when(invoker.getInterface()).thenReturn(DemoService.class); + + genericFilter.invoke(invoker, invocation); } @Test @@ -99,7 +97,7 @@ public void testInvokeWithJavaException() throws Exception { RpcInvocation invocation = new RpcInvocation("sayHi", genericInvoke.getParameterTypes() , new Object[]{"getPerson", new String[]{Person.class.getCanonicalName()}, new Object[]{person}}); - URL url = URL.valueOf("test://test:11/org.apache.dubbo.rpc.support.DemoService?" + + URL url = URL.valueOf("test://test:11/com.alibaba.dubbo.rpc.support.DemoService?" + "accesslog=true&group=dubbo&version=1.1"); Invoker invoker = Mockito.mock(Invoker.class); when(invoker.invoke(any(Invocation.class))).thenReturn(new RpcResult(new Person("person", 10))); @@ -107,8 +105,8 @@ public void testInvokeWithJavaException() throws Exception { when(invoker.getInterface()).thenReturn(DemoService.class); Result result = genericFilter.invoke(invoker, invocation); - Assertions.assertEquals(Person.class, result.getValue().getClass()); - Assertions.assertEquals(10, ((Person) (result.getValue())).getAge()); + Assert.assertEquals(Person.class, result.getValue().getClass()); + Assert.assertEquals(10, ((Person) (result.getValue())).getAge()); } @Test @@ -123,7 +121,7 @@ public void testInvokeWithMethodArgumentSizeIsNot3() { RpcInvocation invocation = new RpcInvocation(Constants.$INVOKE, genericInvoke.getParameterTypes() , new Object[]{"getPerson", new String[]{Person.class.getCanonicalName()}}); - URL url = URL.valueOf("test://test:11/org.apache.dubbo.rpc.support.DemoService?" + + URL url = URL.valueOf("test://test:11/com.alibaba.dubbo.rpc.support.DemoService?" + "accesslog=true&group=dubbo&version=1.1"); Invoker invoker = Mockito.mock(Invoker.class); when(invoker.invoke(any(Invocation.class))).thenReturn(new RpcResult(new Person("person", 10))); @@ -131,8 +129,8 @@ public void testInvokeWithMethodArgumentSizeIsNot3() { when(invoker.getInterface()).thenReturn(DemoService.class); Result result = genericFilter.invoke(invoker, invocation); - Assertions.assertEquals(Person.class, result.getValue().getClass()); - Assertions.assertEquals(10, ((Person) (result.getValue())).getAge()); + Assert.assertEquals(Person.class, result.getValue().getClass()); + Assert.assertEquals(10, ((Person) (result.getValue())).getAge()); } } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericImplFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericImplFilterTest.java index 43930ef6605..02ea4edc39f 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericImplFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericImplFilterTest.java @@ -28,8 +28,8 @@ import org.apache.dubbo.rpc.support.DemoService; import org.apache.dubbo.rpc.support.Person; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import org.mockito.Mockito; import java.lang.reflect.Method; @@ -50,7 +50,7 @@ public void testInvoke() throws Exception { new Class[]{Person.class}, new Object[]{new Person("dubbo", 10)}); - URL url = URL.valueOf("test://test:11/org.apache.dubbo.rpc.support.DemoService?" + + URL url = URL.valueOf("test://test:11/com.alibaba.dubbo.rpc.support.DemoService?" + "accesslog=true&group=dubbo&version=1.1&generic=true"); Invoker invoker = Mockito.mock(Invoker.class); @@ -64,8 +64,8 @@ public void testInvoke() throws Exception { Result result = genericImplFilter.invoke(invoker, invocation); - Assertions.assertEquals(Person.class, result.getValue().getClass()); - Assertions.assertEquals(10, ((Person) result.getValue()).getAge()); + Assert.assertEquals(Person.class, result.getValue().getClass()); + Assert.assertEquals(10, ((Person) result.getValue()).getAge()); } @Test @@ -74,7 +74,7 @@ public void testInvokeWithException() throws Exception { RpcInvocation invocation = new RpcInvocation("getPerson", new Class[]{Person.class}, new Object[]{new Person("dubbo", 10)}); - URL url = URL.valueOf("test://test:11/org.apache.dubbo.rpc.support.DemoService?" + + URL url = URL.valueOf("test://test:11/com.alibaba.dubbo.rpc.support.DemoService?" + "accesslog=true&group=dubbo&version=1.1&generic=true"); Invoker invoker = Mockito.mock(Invoker.class); @@ -84,7 +84,7 @@ public void testInvokeWithException() throws Exception { when(invoker.getInterface()).thenReturn(DemoService.class); Result result = genericImplFilter.invoke(invoker, invocation); - Assertions.assertEquals(RuntimeException.class, result.getException().getClass()); + Assert.assertEquals(RuntimeException.class, result.getException().getClass()); } @@ -100,14 +100,14 @@ public void testInvokeWithException() throws Exception { RpcInvocation invocation = new RpcInvocation(Constants.$INVOKE, genericInvoke.getParameterTypes(), new Object[]{"getPerson", new String[]{Person.class.getCanonicalName()}, new Object[]{person}}); - URL url = URL.valueOf("test://test:11/org.apache.dubbo.rpc.support.DemoService?" + + URL url = URL.valueOf("test://test:11/com.alibaba.dubbo.rpc.support.DemoService?" + "accesslog=true&group=dubbo&version=1.1&generic=true"); Invoker invoker = Mockito.mock(Invoker.class); when(invoker.invoke(any(Invocation.class))).thenReturn(new RpcResult(new Person("person", 10))); when(invoker.getUrl()).thenReturn(url); genericImplFilter.invoke(invoker, invocation); - Assertions.assertEquals("true", invocation.getAttachment(Constants.GENERIC_KEY)); + Assert.assertEquals("true", invocation.getAttachment(Constants.GENERIC_KEY)); } } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/TimeoutFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/TimeoutFilterTest.java index f99a0f35480..4ee8e5565e5 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/TimeoutFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/TimeoutFilterTest.java @@ -23,8 +23,8 @@ import org.apache.dubbo.rpc.RpcResult; import org.apache.dubbo.rpc.support.BlockMyInvoker; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import org.mockito.Mockito; import static org.mockito.Mockito.any; @@ -46,7 +46,7 @@ public void testInvokeWithoutTimeout() throws Exception { when(invocation.getMethodName()).thenReturn("testInvokeWithoutTimeout"); Result result = timeoutFilter.invoke(invoker, invocation); - Assertions.assertEquals("result", result.getValue()); + Assert.assertEquals("result", result.getValue()); } @Test @@ -60,7 +60,7 @@ public void testInvokeWithTimeout() throws Exception { when(invocation.getMethodName()).thenReturn("testInvokeWithTimeout"); Result result = timeoutFilter.invoke(invoker, invocation); - Assertions.assertEquals("alibaba", result.getValue()); + Assert.assertEquals("alibaba", result.getValue()); } } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/TokenFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/TokenFilterTest.java index fea5c300e07..6d8b0f3a6c9 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/TokenFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/TokenFilterTest.java @@ -24,8 +24,8 @@ import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.RpcResult; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import org.mockito.Mockito; import java.util.HashMap; @@ -53,42 +53,38 @@ public void testInvokeWithToken() throws Exception { when(invocation.getAttachments()).thenReturn(attachments); Result result = tokenFilter.invoke(invoker, invocation); - Assertions.assertEquals("result", result.getValue()); + Assert.assertEquals("result", result.getValue()); } - @Test + @Test(expected = RpcException.class) public void testInvokeWithWrongToken() throws Exception { - Assertions.assertThrows(RpcException.class, () -> { - String token = "token"; + String token = "token"; - Invoker invoker = Mockito.mock(Invoker.class); - URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&token=" + token); - when(invoker.getUrl()).thenReturn(url); - when(invoker.invoke(any(Invocation.class))).thenReturn(new RpcResult("result")); + Invoker invoker = Mockito.mock(Invoker.class); + URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&token=" + token); + when(invoker.getUrl()).thenReturn(url); + when(invoker.invoke(any(Invocation.class))).thenReturn(new RpcResult("result")); - Map attachments = new HashMap(); - attachments.put(Constants.TOKEN_KEY, "wrongToken"); - Invocation invocation = Mockito.mock(Invocation.class); - when(invocation.getAttachments()).thenReturn(attachments); + Map attachments = new HashMap(); + attachments.put(Constants.TOKEN_KEY, "wrongToken"); + Invocation invocation = Mockito.mock(Invocation.class); + when(invocation.getAttachments()).thenReturn(attachments); - tokenFilter.invoke(invoker, invocation); - }); + tokenFilter.invoke(invoker, invocation); } - @Test + @Test(expected = RpcException.class) public void testInvokeWithoutToken() throws Exception { - Assertions.assertThrows(RpcException.class, () -> { - String token = "token"; + String token = "token"; - Invoker invoker = Mockito.mock(Invoker.class); - URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&token=" + token); - when(invoker.getUrl()).thenReturn(url); - when(invoker.invoke(any(Invocation.class))).thenReturn(new RpcResult("result")); + Invoker invoker = Mockito.mock(Invoker.class); + URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&token=" + token); + when(invoker.getUrl()).thenReturn(url); + when(invoker.invoke(any(Invocation.class))).thenReturn(new RpcResult("result")); - Invocation invocation = Mockito.mock(Invocation.class); + Invocation invocation = Mockito.mock(Invocation.class); - tokenFilter.invoke(invoker, invocation); - }); + tokenFilter.invoke(invoker, invocation); } } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/tps/TpsLimitFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/TpsLimitFilterTest.java similarity index 64% rename from dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/tps/TpsLimitFilterTest.java rename to dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/TpsLimitFilterTest.java index 235eeddd6e2..acb96ccc490 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/tps/TpsLimitFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/TpsLimitFilterTest.java @@ -14,21 +14,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.rpc.filter.tps; +package org.apache.dubbo.rpc.filter; import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.RpcException; -import org.apache.dubbo.rpc.filter.TpsLimitFilter; import org.apache.dubbo.rpc.support.MockInvocation; import org.apache.dubbo.rpc.support.MyInvoker; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertTrue; public class TpsLimitFilterTest { @@ -45,24 +43,22 @@ public void testWithoutCount() throws Exception { filter.invoke(invoker, invocation); } - @Test + @Test(expected = RpcException.class) public void testFail() throws Exception { - Assertions.assertThrows(RpcException.class, () -> { - URL url = URL.valueOf("test://test"); - url = url.addParameter(Constants.INTERFACE_KEY, - "org.apache.dubbo.rpc.file.TpsService"); - url = url.addParameter(Constants.TPS_LIMIT_RATE_KEY, 5); - Invoker invoker = new MyInvoker(url); - Invocation invocation = new MockInvocation(); - for (int i = 0; i < 10; i++) { - try { - filter.invoke(invoker, invocation); - } catch (Exception e) { - assertTrue(i >= 5); - throw e; - } + URL url = URL.valueOf("test://test"); + url = url.addParameter(Constants.INTERFACE_KEY, + "org.apache.dubbo.rpc.file.TpsService"); + url = url.addParameter(Constants.TPS_LIMIT_RATE_KEY, 5); + Invoker invoker = new MyInvoker(url); + Invocation invocation = new MockInvocation(); + for (int i = 0; i < 10; i++) { + try { + filter.invoke(invoker, invocation); + } catch (Exception e) { + assertTrue(i >= 5); + throw e; } - }); - + } } -} \ No newline at end of file + +} diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/tps/DefaultTPSLimiterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/tps/DefaultTPSLimiterTest.java deleted file mode 100644 index 7852e42e8dc..00000000000 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/tps/DefaultTPSLimiterTest.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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 org.apache.dubbo.rpc.filter.tps; - -import org.apache.dubbo.common.Constants; -import org.apache.dubbo.common.URL; -import org.apache.dubbo.rpc.Invocation; -import org.apache.dubbo.rpc.support.MockInvocation; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -/** - * @author: lizhen - * @since: 2019-03-19 - * @description: - */ -public class DefaultTPSLimiterTest { - - private DefaultTPSLimiter defaultTPSLimiter = new DefaultTPSLimiter(); - - @Test - public void testIsAllowable() throws Exception { - Invocation invocation = new MockInvocation(); - URL url = URL.valueOf("test://test"); - url = url.addParameter(Constants.INTERFACE_KEY, "org.apache.dubbo.rpc.file.TpsService"); - url = url.addParameter(Constants.TPS_LIMIT_RATE_KEY, 2); - url = url.addParameter(Constants.TPS_LIMIT_INTERVAL_KEY, 1000); - for (int i = 0; i < 3; i++) { - Assertions.assertTrue(defaultTPSLimiter.isAllowable(url, invocation)); - } - } - - @Test - public void testIsNotAllowable() throws Exception { - Invocation invocation = new MockInvocation(); - URL url = URL.valueOf("test://test"); - url = url.addParameter(Constants.INTERFACE_KEY, "org.apache.dubbo.rpc.file.TpsService"); - url = url.addParameter(Constants.TPS_LIMIT_RATE_KEY, 2); - url = url.addParameter(Constants.TPS_LIMIT_INTERVAL_KEY, 1000); - for (int i = 0; i < 4; i++) { - if (i == 3) { - Assertions.assertFalse(defaultTPSLimiter.isAllowable(url, invocation)); - } else { - Assertions.assertTrue(defaultTPSLimiter.isAllowable(url, invocation)); - } - } - } - - - @Test - public void testConfigChange() throws Exception { - Invocation invocation = new MockInvocation(); - URL url = URL.valueOf("test://test"); - url = url.addParameter(Constants.INTERFACE_KEY, "org.apache.dubbo.rpc.file.TpsService"); - url = url.addParameter(Constants.TPS_LIMIT_RATE_KEY, 2); - url = url.addParameter(Constants.TPS_LIMIT_INTERVAL_KEY, 1000); - for (int i = 0; i < 3; i++) { - Assertions.assertTrue(defaultTPSLimiter.isAllowable(url, invocation)); - } - url = url.addParameter(Constants.TPS_LIMIT_RATE_KEY, 2000); - for (int i = 0; i < 3; i++) { - Assertions.assertTrue(defaultTPSLimiter.isAllowable(url, invocation)); - } - } -} diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/tps/StatItemTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/tps/StatItemTest.java index 86308072576..2693bb1c564 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/tps/StatItemTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/tps/StatItemTest.java @@ -16,17 +16,17 @@ */ package org.apache.dubbo.rpc.filter.tps; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; public class StatItemTest { private StatItem statItem; - @AfterEach + @After public void tearDown() throws Exception { statItem = null; } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/proxy/AbstractProxyTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/proxy/AbstractProxyTest.java index 1bd288f12f7..8bc30a4db4f 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/proxy/AbstractProxyTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/proxy/AbstractProxyTest.java @@ -25,8 +25,8 @@ import org.apache.dubbo.rpc.support.DemoServiceImpl; import org.apache.dubbo.rpc.support.MyInvoker; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.util.Arrays; @@ -43,15 +43,15 @@ public void testGetProxy() throws Exception { DemoService proxy = factory.getProxy(invoker); - Assertions.assertNotNull(proxy); + Assert.assertNotNull(proxy); - Assertions.assertTrue(Arrays.asList(proxy.getClass().getInterfaces()).contains(DemoService.class)); + Assert.assertTrue(Arrays.asList(proxy.getClass().getInterfaces()).contains(DemoService.class)); // Not equal - //Assertions.assertEquals(proxy.toString(), invoker.toString()); - //Assertions.assertEquals(proxy.hashCode(), invoker.hashCode()); + //Assert.assertEquals(proxy.toString(), invoker.toString()); + //Assert.assertEquals(proxy.hashCode(), invoker.hashCode()); - Assertions.assertEquals(invoker.invoke(new RpcInvocation("echo", new Class[]{String.class}, new Object[]{"aa"})).getValue() + Assert.assertEquals(invoker.invoke(new RpcInvocation("echo", new Class[]{String.class}, new Object[]{"aa"})).getValue() , proxy.echo("aa")); } @@ -63,9 +63,9 @@ public void testGetInvoker() throws Exception { Invoker invoker = factory.getInvoker(new DemoServiceImpl(), DemoService.class, url); - Assertions.assertEquals(invoker.getInterface(), DemoService.class); + Assert.assertEquals(invoker.getInterface(), DemoService.class); - Assertions.assertEquals(invoker.invoke(new RpcInvocation("echo", new Class[]{String.class}, new Object[]{"aa"})).getValue(), + Assert.assertEquals(invoker.invoke(new RpcInvocation("echo", new Class[]{String.class}, new Object[]{"aa"})).getValue(), origin.echo("aa")); } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/proxy/InvokerInvocationHandlerTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/proxy/InvokerInvocationHandlerTest.java index 9c2d40d7e7d..f06df9a200f 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/proxy/InvokerInvocationHandlerTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/proxy/InvokerInvocationHandlerTest.java @@ -17,9 +17,9 @@ package org.apache.dubbo.rpc.proxy; import org.apache.dubbo.rpc.Invoker; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -35,7 +35,7 @@ public class InvokerInvocationHandlerTest { @InjectMocks private InvokerInvocationHandler invokerInvocationHandler; - @BeforeEach + @Before public void setUp() { MockitoAnnotations.initMocks(this); } @@ -48,7 +48,7 @@ public void testInvokeToString() throws Throwable { Method method = invoker.getClass().getMethod(methodName); Object result = invokerInvocationHandler.invoke(null, method, new Object[]{}); - Assertions.assertEquals(methodName, result); + Assert.assertEquals(methodName, result); } } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/RpcUtilsTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/RpcUtilsTest.java index b3f502eecfc..cbe17e47255 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/RpcUtilsTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/RpcUtilsTest.java @@ -21,18 +21,18 @@ import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.RpcInvocation; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.lang.reflect.ParameterizedType; import java.util.HashMap; import java.util.List; import java.util.Map; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -112,40 +112,40 @@ public void testGetReturnTypes() throws Exception { Invocation inv = new RpcInvocation("testReturnType", new Class[]{String.class}, null, null, invoker); java.lang.reflect.Type[] types = RpcUtils.getReturnTypes(inv); - Assertions.assertEquals(2, types.length); - Assertions.assertEquals(String.class, types[0]); - Assertions.assertEquals(String.class, types[1]); + Assert.assertEquals(2, types.length); + Assert.assertEquals(String.class, types[0]); + Assert.assertEquals(String.class, types[1]); Invocation inv1 = new RpcInvocation("testReturnType1", new Class[]{String.class}, null, null, invoker); java.lang.reflect.Type[] types1 = RpcUtils.getReturnTypes(inv1); - Assertions.assertEquals(2, types1.length); - Assertions.assertEquals(List.class, types1[0]); - Assertions.assertEquals(DemoService.class.getMethod("testReturnType1", new Class[]{String.class}).getGenericReturnType(), types1[1]); + Assert.assertEquals(2, types1.length); + Assert.assertEquals(List.class, types1[0]); + Assert.assertEquals(DemoService.class.getMethod("testReturnType1", new Class[]{String.class}).getGenericReturnType(), types1[1]); Invocation inv2 = new RpcInvocation("testReturnType2", new Class[]{String.class}, null, null, invoker); java.lang.reflect.Type[] types2 = RpcUtils.getReturnTypes(inv2); - Assertions.assertEquals(2, types2.length); - Assertions.assertEquals(String.class, types2[0]); - Assertions.assertEquals(String.class, types2[1]); + Assert.assertEquals(2, types2.length); + Assert.assertEquals(String.class, types2[0]); + Assert.assertEquals(String.class, types2[1]); Invocation inv3 = new RpcInvocation("testReturnType3", new Class[]{String.class}, null, null, invoker); java.lang.reflect.Type[] types3 = RpcUtils.getReturnTypes(inv3); - Assertions.assertEquals(2, types3.length); - Assertions.assertEquals(List.class, types3[0]); + Assert.assertEquals(2, types3.length); + Assert.assertEquals(List.class, types3[0]); java.lang.reflect.Type genericReturnType3 = DemoService.class.getMethod("testReturnType3", new Class[]{String.class}).getGenericReturnType(); - Assertions.assertEquals(((ParameterizedType) genericReturnType3).getActualTypeArguments()[0], types3[1]); + Assert.assertEquals(((ParameterizedType) genericReturnType3).getActualTypeArguments()[0], types3[1]); Invocation inv4 = new RpcInvocation("testReturnType4", new Class[]{String.class}, null, null, invoker); java.lang.reflect.Type[] types4 = RpcUtils.getReturnTypes(inv4); - Assertions.assertEquals(2, types4.length); - Assertions.assertEquals(null, types4[0]); - Assertions.assertEquals(null, types4[1]); + Assert.assertEquals(2, types4.length); + Assert.assertEquals(null, types4[0]); + Assert.assertEquals(null, types4[1]); Invocation inv5 = new RpcInvocation("testReturnType5", new Class[]{String.class}, null, null, invoker); java.lang.reflect.Type[] types5 = RpcUtils.getReturnTypes(inv5); - Assertions.assertEquals(2, types5.length); - Assertions.assertEquals(Map.class, types5[0]); + Assert.assertEquals(2, types5.length); + Assert.assertEquals(Map.class, types5[0]); java.lang.reflect.Type genericReturnType5 = DemoService.class.getMethod("testReturnType5", new Class[]{String.class}).getGenericReturnType(); - Assertions.assertEquals(((ParameterizedType) genericReturnType5).getActualTypeArguments()[0], types5[1]); + Assert.assertEquals(((ParameterizedType) genericReturnType5).getActualTypeArguments()[0], types5[1]); } } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/resources/log4j.xml b/dubbo-rpc/dubbo-rpc-api/src/test/resources/log4j.xml index e0eda90175d..f041044f081 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/resources/log4j.xml +++ b/dubbo-rpc/dubbo-rpc-api/src/test/resources/log4j.xml @@ -21,7 +21,6 @@ - diff --git a/dubbo-rpc/dubbo-rpc-dubbo/pom.xml b/dubbo-rpc/dubbo-rpc-dubbo/pom.xml index 290f627b36f..354930c04e8 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/pom.xml +++ b/dubbo-rpc/dubbo-rpc-dubbo/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-rpc - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-rpc-dubbo jar diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/CallbackServiceCodec.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/CallbackServiceCodec.java index 4d2f3c6756b..aa5c02b4a90 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/CallbackServiceCodec.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/CallbackServiceCodec.java @@ -77,41 +77,41 @@ private static byte isCallBack(URL url, String methodName, int argIndex) { * @throws IOException */ @SuppressWarnings({"unchecked", "rawtypes"}) - private static String exportOrUnexportCallbackService(Channel channel, URL url, Class clazz, Object inst, Boolean export) throws IOException { + private static String exportOrunexportCallbackService(Channel channel, URL url, Class clazz, Object inst, Boolean export) throws IOException { int instid = System.identityHashCode(inst); - Map params = new HashMap<>(3); + Map params = new HashMap(3); // no need to new client again params.put(Constants.IS_SERVER_KEY, Boolean.FALSE.toString()); // mark it's a callback, for troubleshooting params.put(Constants.IS_CALLBACK_SERVICE, Boolean.TRUE.toString()); - String group = (url == null ? null : url.getParameter(Constants.GROUP_KEY)); + String group = url.getParameter(Constants.GROUP_KEY); if (group != null && group.length() > 0) { params.put(Constants.GROUP_KEY, group); } // add method, for verifying against method, automatic fallback (see dubbo protocol) params.put(Constants.METHODS_KEY, StringUtils.join(Wrapper.getWrapper(clazz).getDeclaredMethodNames(), ",")); - Map tmpMap = new HashMap<>(url.getParameters()); - tmpMap.putAll(params); - tmpMap.remove(Constants.VERSION_KEY);// doesn't need to distinguish version for callback - tmpMap.put(Constants.INTERFACE_KEY, clazz.getName()); - URL exportUrl = new URL(DubboProtocol.NAME, channel.getLocalAddress().getAddress().getHostAddress(), channel.getLocalAddress().getPort(), clazz.getName() + "." + instid, tmpMap); + Map tmpmap = new HashMap(url.getParameters()); + tmpmap.putAll(params); + tmpmap.remove(Constants.VERSION_KEY);// doesn't need to distinguish version for callback + tmpmap.put(Constants.INTERFACE_KEY, clazz.getName()); + URL exporturl = new URL(DubboProtocol.NAME, channel.getLocalAddress().getAddress().getHostAddress(), channel.getLocalAddress().getPort(), clazz.getName() + "." + instid, tmpmap); // no need to generate multiple exporters for different channel in the same JVM, cache key cannot collide. String cacheKey = getClientSideCallbackServiceCacheKey(instid); - String countKey = getClientSideCountKey(clazz.getName()); + String countkey = getClientSideCountKey(clazz.getName()); if (export) { // one channel can have multiple callback instances, no need to re-export for different instance. if (!channel.hasAttribute(cacheKey)) { if (!isInstancesOverLimit(channel, url, clazz.getName(), instid, false)) { - Invoker invoker = proxyFactory.getInvoker(inst, clazz, exportUrl); + Invoker invoker = proxyFactory.getInvoker(inst, clazz, exporturl); // should destroy resource? Exporter exporter = protocol.export(invoker); // this is used for tracing if instid has published service or not. channel.setAttribute(cacheKey, exporter); - logger.info("Export a callback service :" + exportUrl + ", on " + channel + ", url is: " + url); - increaseInstanceCount(channel, countKey); + logger.info("export a callback service :" + exporturl + ", on " + channel + ", url is: " + url); + increaseInstanceCount(channel, countkey); } } } else { @@ -119,7 +119,7 @@ private static String exportOrUnexportCallbackService(Channel channel, URL url, Exporter exporter = (Exporter) channel.getAttribute(cacheKey); exporter.unexport(); channel.removeAttribute(cacheKey); - decreaseInstanceCount(channel, countKey); + decreaseInstanceCount(channel, countkey); } } return String.valueOf(instid); @@ -245,17 +245,17 @@ private static void decreaseInstanceCount(Channel channel, String countkey) { public static Object encodeInvocationArgument(Channel channel, RpcInvocation inv, int paraIndex) throws IOException { // get URL directly URL url = inv.getInvoker() == null ? null : inv.getInvoker().getUrl(); - byte callbackStatus = isCallBack(url, inv.getMethodName(), paraIndex); + byte callbackstatus = isCallBack(url, inv.getMethodName(), paraIndex); Object[] args = inv.getArguments(); Class[] pts = inv.getParameterTypes(); - switch (callbackStatus) { + switch (callbackstatus) { case CallbackServiceCodec.CALLBACK_NONE: return args[paraIndex]; case CallbackServiceCodec.CALLBACK_CREATE: - inv.setAttachment(INV_ATT_CALLBACK_KEY + paraIndex, exportOrUnexportCallbackService(channel, url, pts[paraIndex], args[paraIndex], true)); + inv.setAttachment(INV_ATT_CALLBACK_KEY + paraIndex, exportOrunexportCallbackService(channel, url, pts[paraIndex], args[paraIndex], true)); return null; case CallbackServiceCodec.CALLBACK_DESTROY: - inv.setAttachment(INV_ATT_CALLBACK_KEY + paraIndex, exportOrUnexportCallbackService(channel, url, pts[paraIndex], args[paraIndex], false)); + inv.setAttachment(INV_ATT_CALLBACK_KEY + paraIndex, exportOrunexportCallbackService(channel, url, pts[paraIndex], args[paraIndex], false)); return null; default: return args[paraIndex]; diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java index fac7c64a6ca..7c72133e66b 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java @@ -20,7 +20,6 @@ import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.serialize.Cleanable; import org.apache.dubbo.common.serialize.ObjectInput; -import org.apache.dubbo.common.utils.ArrayUtils; import org.apache.dubbo.common.utils.Assert; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.remoting.Channel; @@ -74,27 +73,61 @@ public void encode(Channel channel, OutputStream output, Object message) throws public Object decode(Channel channel, InputStream input) throws IOException { ObjectInput in = CodecSupport.getSerialization(channel.getUrl(), serializationType) .deserialize(channel.getUrl(), input); - + byte flag = in.readByte(); switch (flag) { case DubboCodec.RESPONSE_NULL_VALUE: break; case DubboCodec.RESPONSE_VALUE: - handleValue(in); + try { + Type[] returnType = RpcUtils.getReturnTypes(invocation); + setValue(returnType == null || returnType.length == 0 ? in.readObject() : + (returnType.length == 1 ? in.readObject((Class) returnType[0]) + : in.readObject((Class) returnType[0], returnType[1]))); + } catch (ClassNotFoundException e) { + throw new IOException(StringUtils.toString("Read response data failed.", e)); + } break; case DubboCodec.RESPONSE_WITH_EXCEPTION: - handleException(in); + try { + Object obj = in.readObject(); + if (obj instanceof Throwable == false) { + throw new IOException("Response data error, expect Throwable, but get " + obj); + } + setException((Throwable) obj); + } catch (ClassNotFoundException e) { + throw new IOException(StringUtils.toString("Read response data failed.", e)); + } break; case DubboCodec.RESPONSE_NULL_VALUE_WITH_ATTACHMENTS: - handleAttachment(in); + try { + setAttachments((Map) in.readObject(Map.class)); + } catch (ClassNotFoundException e) { + throw new IOException(StringUtils.toString("Read response data failed.", e)); + } break; case DubboCodec.RESPONSE_VALUE_WITH_ATTACHMENTS: - handleValue(in); - handleAttachment(in); + try { + Type[] returnType = RpcUtils.getReturnTypes(invocation); + setValue(returnType == null || returnType.length == 0 ? in.readObject() : + (returnType.length == 1 ? in.readObject((Class) returnType[0]) + : in.readObject((Class) returnType[0], returnType[1]))); + setAttachments((Map) in.readObject(Map.class)); + } catch (ClassNotFoundException e) { + throw new IOException(StringUtils.toString("Read response data failed.", e)); + } break; case DubboCodec.RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS: - handleException(in); - handleAttachment(in); + try { + Object obj = in.readObject(); + if (obj instanceof Throwable == false) { + throw new IOException("Response data error, expect Throwable, but get " + obj); + } + setException((Throwable) obj); + setAttachments((Map) in.readObject(Map.class)); + } catch (ClassNotFoundException e) { + throw new IOException(StringUtils.toString("Read response data failed.", e)); + } break; default: throw new IOException("Unknown result flag, expect '0' '1' '2', get " + flag); @@ -122,44 +155,4 @@ public void decode() throws Exception { } } - private void handleValue(ObjectInput in) throws IOException { - try { - Type[] returnTypes = RpcUtils.getReturnTypes(invocation); - Object value = null; - if (ArrayUtils.isEmpty(returnTypes)) { - value = in.readObject(); - } else if (returnTypes.length == 1) { - value = in.readObject((Class) returnTypes[0]); - } else { - value = in.readObject((Class) returnTypes[0], returnTypes[1]); - } - setValue(value); - } catch (ClassNotFoundException e) { - rethrow(e); - } - } - - private void handleException(ObjectInput in) throws IOException { - try { - Object obj = in.readObject(); - if (!(obj instanceof Throwable)) { - throw new IOException("Response data error, expect Throwable, but get " + obj); - } - setException((Throwable) obj); - } catch (ClassNotFoundException e) { - rethrow(e); - } - } - - private void handleAttachment(ObjectInput in) throws IOException { - try { - setAttachments((Map) in.readObject(Map.class)); - } catch (ClassNotFoundException e) { - rethrow(e); - } - } - - private void rethrow(Exception e) throws IOException { - throw new IOException(StringUtils.toString("Read response data failed.", e)); - } } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboCodec.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboCodec.java index 38650c2b2ef..d6a8e14ed39 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboCodec.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboCodec.java @@ -27,6 +27,7 @@ import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.remoting.Channel; +import org.apache.dubbo.remoting.Codec2; import org.apache.dubbo.remoting.exchange.Request; import org.apache.dubbo.remoting.exchange.Response; import org.apache.dubbo.remoting.exchange.codec.ExchangeCodec; @@ -44,7 +45,7 @@ /** * Dubbo codec. */ -public class DubboCodec extends ExchangeCodec { +public class DubboCodec extends ExchangeCodec implements Codec2 { public static final String NAME = "dubbo"; public static final String DUBBO_VERSION = Version.getProtocolVersion(); @@ -67,7 +68,7 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro // decode response. Response res = new Response(id); if ((flag & FLAG_EVENT) != 0) { - res.setEvent(true); + res.setEvent(Response.HEARTBEAT_EVENT); } // get status. byte status = header[3]; @@ -113,7 +114,7 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro req.setVersion(Version.getProtocolVersion()); req.setTwoWay((flag & FLAG_TWOWAY) != 0); if ((flag & FLAG_EVENT) != 0) { - req.setEvent(true); + req.setEvent(Request.HEARTBEAT_EVENT); } try { Object data; @@ -144,7 +145,6 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro req.setBroken(true); req.setData(t); } - return req; } } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java index 77443016cfb..86eb013f985 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java @@ -18,14 +18,11 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.config.ConfigurationUtils; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.serialize.support.SerializableClassRegistry; import org.apache.dubbo.common.serialize.support.SerializationOptimizer; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.ConcurrentHashSet; -import org.apache.dubbo.common.utils.ConfigUtils; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.remoting.Channel; @@ -52,13 +49,11 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.CopyOnWriteArrayList; /** * dubbo protocol support. @@ -70,76 +65,63 @@ public class DubboProtocol extends AbstractProtocol { public static final int DEFAULT_PORT = 20880; private static final String IS_CALLBACK_SERVICE_INVOKE = "_isCallBackServiceInvoke"; private static DubboProtocol INSTANCE; - - /** - * - */ - private final Map serverMap = new ConcurrentHashMap<>(); - /** - * - */ - private final Map> referenceClientMap = new ConcurrentHashMap<>(); - private final ConcurrentMap locks = new ConcurrentHashMap<>(); - private final Set optimizers = new ConcurrentHashSet<>(); - /** - * consumer side export a stub service for dispatching event - * servicekey-stubmethods - */ - private final ConcurrentMap stubServiceMethodsMap = new ConcurrentHashMap<>(); - + private final Map serverMap = new ConcurrentHashMap(); // + private final Map referenceClientMap = new ConcurrentHashMap(); // + private final ConcurrentMap ghostClientMap = new ConcurrentHashMap(); + private final ConcurrentMap locks = new ConcurrentHashMap(); + private final Set optimizers = new ConcurrentHashSet(); + //consumer side export a stub service for dispatching event + //servicekey-stubmethods + private final ConcurrentMap stubServiceMethodsMap = new ConcurrentHashMap(); private ExchangeHandler requestHandler = new ExchangeHandlerAdapter() { @Override public CompletableFuture reply(ExchangeChannel channel, Object message) throws RemotingException { - - if (!(message instanceof Invocation)) { - throw new RemotingException(channel, "Unsupported request: " - + (message == null ? null : (message.getClass().getName() + ": " + message)) - + ", channel: consumer: " + channel.getRemoteAddress() + " --> provider: " + channel.getLocalAddress()); - } - - Invocation inv = (Invocation) message; - Invoker invoker = getInvoker(channel, inv); - // need to consider backward-compatibility if it's a callback - if (Boolean.TRUE.toString().equals(inv.getAttachments().get(IS_CALLBACK_SERVICE_INVOKE))) { - String methodsStr = invoker.getUrl().getParameters().get("methods"); - boolean hasMethod = false; - if (methodsStr == null || !methodsStr.contains(",")) { - hasMethod = inv.getMethodName().equals(methodsStr); - } else { - String[] methods = methodsStr.split(","); - for (String method : methods) { - if (inv.getMethodName().equals(method)) { - hasMethod = true; - break; + if (message instanceof Invocation) { + Invocation inv = (Invocation) message; + Invoker invoker = getInvoker(channel, inv); + // need to consider backward-compatibility if it's a callback + if (Boolean.TRUE.toString().equals(inv.getAttachments().get(IS_CALLBACK_SERVICE_INVOKE))) { + String methodsStr = invoker.getUrl().getParameters().get("methods"); + boolean hasMethod = false; + if (methodsStr == null || !methodsStr.contains(",")) { + hasMethod = inv.getMethodName().equals(methodsStr); + } else { + String[] methods = methodsStr.split(","); + for (String method : methods) { + if (inv.getMethodName().equals(method)) { + hasMethod = true; + break; + } } } + if (!hasMethod) { + logger.warn(new IllegalStateException("The methodName " + inv.getMethodName() + + " not found in callback service interface ,invoke will be ignored." + + " please update the api interface. url is:" + + invoker.getUrl()) + " ,invocation is :" + inv); + return null; + } } - if (!hasMethod) { - logger.warn(new IllegalStateException("The methodName " + inv.getMethodName() - + " not found in callback service interface ,invoke will be ignored." - + " please update the api interface. url is:" - + invoker.getUrl()) + " ,invocation is :" + inv); - return null; - } - } - RpcContext rpcContext = RpcContext.getContext(); - rpcContext.setRemoteAddress(channel.getRemoteAddress()); - Result result = invoker.invoke(inv); + RpcContext rpcContext = RpcContext.getContext(); + rpcContext.setRemoteAddress(channel.getRemoteAddress()); + Result result = invoker.invoke(inv); - if (result instanceof AsyncRpcResult) { - return ((AsyncRpcResult) result).getResultFuture().thenApply(r -> (Object) r); - - } else { - return CompletableFuture.completedFuture(result); + if (result instanceof AsyncRpcResult) { + return ((AsyncRpcResult) result).getResultFuture().thenApply(r -> (Object) r); + } else { + return CompletableFuture.completedFuture(result); + } } + throw new RemotingException(channel, "Unsupported request: " + + (message == null ? null : (message.getClass().getName() + ": " + message)) + + ", channel: consumer: " + channel.getRemoteAddress() + " --> provider: " + channel.getLocalAddress()); } @Override public void received(Channel channel, Object message) throws RemotingException { if (message instanceof Invocation) { reply((ExchangeChannel) channel, message); - } else { super.received(channel, message); } @@ -152,8 +134,8 @@ public void connected(Channel channel) throws RemotingException { @Override public void disconnected(Channel channel) throws RemotingException { - if (logger.isDebugEnabled()) { - logger.debug("disconnected from " + channel.getRemoteAddress() + ",url:" + channel.getUrl()); + if (logger.isInfoEnabled()) { + logger.info("disconnected from " + channel.getRemoteAddress() + ",url:" + channel.getUrl()); } invoke(channel, Constants.ON_DISCONNECT_KEY); } @@ -174,7 +156,6 @@ private Invocation createInvocation(Channel channel, URL url, String methodKey) if (method == null || method.length() == 0) { return null; } - RpcInvocation invocation = new RpcInvocation(method, new Class[0], new Object[0]); invocation.setAttachment(Constants.PATH_KEY, url.getPath()); invocation.setAttachment(Constants.GROUP_KEY, url.getParameter(Constants.GROUP_KEY)); @@ -183,7 +164,6 @@ private Invocation createInvocation(Channel channel, URL url, String methodKey) if (url.getParameter(Constants.STUB_EVENT_KEY, false)) { invocation.setAttachment(Constants.STUB_EVENT_KEY, Boolean.TRUE.toString()); } - return invocation; } }; @@ -194,10 +174,8 @@ public DubboProtocol() { public static DubboProtocol getDubboProtocol() { if (INSTANCE == null) { - // load - ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(DubboProtocol.NAME); + ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(DubboProtocol.NAME); // load } - return INSTANCE; } @@ -226,26 +204,23 @@ Invoker getInvoker(Channel channel, Invocation inv) throws RemotingException boolean isStubServiceInvoke = false; int port = channel.getLocalAddress().getPort(); String path = inv.getAttachments().get(Constants.PATH_KEY); - // if it's callback service on client side isStubServiceInvoke = Boolean.TRUE.toString().equals(inv.getAttachments().get(Constants.STUB_EVENT_KEY)); if (isStubServiceInvoke) { port = channel.getRemoteAddress().getPort(); } - //callback isCallBackServiceInvoke = isClientSide(channel) && !isStubServiceInvoke; if (isCallBackServiceInvoke) { - path += "." + inv.getAttachments().get(Constants.CALLBACK_SERVICE_KEY); + path = inv.getAttachments().get(Constants.PATH_KEY) + "." + inv.getAttachments().get(Constants.CALLBACK_SERVICE_KEY); inv.getAttachments().put(IS_CALLBACK_SERVICE_INVOKE, Boolean.TRUE.toString()); } - String serviceKey = serviceKey(port, path, inv.getAttachments().get(Constants.VERSION_KEY), inv.getAttachments().get(Constants.GROUP_KEY)); + DubboExporter exporter = (DubboExporter) exporterMap.get(serviceKey); if (exporter == null) { - throw new RemotingException(channel, "Not found exported service: " + serviceKey + " in " + exporterMap.keySet() + ", may be version or group mismatch " + - ", channel: consumer: " + channel.getRemoteAddress() + " --> provider: " + channel.getLocalAddress() + ", message:" + inv); + throw new RemotingException(channel, "Not found exported service: " + serviceKey + " in " + exporterMap.keySet() + ", may be version or group mismatch " + ", channel: consumer: " + channel.getRemoteAddress() + " --> provider: " + channel.getLocalAddress() + ", message:" + inv); } return exporter.getInvoker(); @@ -279,7 +254,6 @@ public Exporter export(Invoker invoker) throws RpcException { logger.warn(new IllegalStateException("consumer [" + url.getParameter(Constants.INTERFACE_KEY) + "], has set stubproxy support event ,but no stub methods founded.")); } - } else { stubServiceMethodsMap.put(url.getServiceKey(), stubServiceMethods); } @@ -287,7 +261,6 @@ public Exporter export(Invoker invoker) throws RpcException { openServer(url); optimizeSerialization(url); - return exporter; } @@ -313,26 +286,23 @@ private void openServer(URL url) { } private ExchangeServer createServer(URL url) { - url = URLBuilder.from(url) - // send readonly event when server closes, it's enabled by default - .addParameterIfAbsent(Constants.CHANNEL_READONLYEVENT_SENT_KEY, Boolean.TRUE.toString()) - // enable heartbeat by default - .addParameterIfAbsent(Constants.HEARTBEAT_KEY, String.valueOf(Constants.DEFAULT_HEARTBEAT)) - .addParameter(Constants.CODEC_KEY, DubboCodec.NAME) - .build(); + // send readonly event when server closes, it's enabled by default + url = url.addParameterIfAbsent(Constants.CHANNEL_READONLYEVENT_SENT_KEY, Boolean.TRUE.toString()); + // enable heartbeat by default + url = url.addParameterIfAbsent(Constants.HEARTBEAT_KEY, String.valueOf(Constants.DEFAULT_HEARTBEAT)); String str = url.getParameter(Constants.SERVER_KEY, Constants.DEFAULT_REMOTING_SERVER); if (str != null && str.length() > 0 && !ExtensionLoader.getExtensionLoader(Transporter.class).hasExtension(str)) { throw new RpcException("Unsupported server type: " + str + ", url: " + url); } + url = url.addParameter(Constants.CODEC_KEY, DubboCodec.NAME); ExchangeServer server; try { server = Exchangers.bind(url, requestHandler); } catch (RemotingException e) { throw new RpcException("Fail to start server(url: " + url + ") " + e.getMessage(), e); } - str = url.getParameter(Constants.CLIENT_KEY); if (str != null && str.length() > 0) { Set supportedTypes = ExtensionLoader.getExtensionLoader(Transporter.class).getSupportedExtensions(); @@ -340,7 +310,6 @@ private ExchangeServer createServer(URL url) { throw new RpcException("Unsupported client type: " + str); } } - return server; } @@ -369,13 +338,10 @@ private void optimizeSerialization(URL url) throws RpcException { } optimizers.add(className); - } catch (ClassNotFoundException e) { throw new RpcException("Cannot find the serialization optimizer class: " + className, e); - } catch (InstantiationException e) { throw new RpcException("Cannot instantiate the serialization optimizer class: " + className, e); - } catch (IllegalAccessException e) { throw new RpcException("Cannot instantiate the serialization optimizer class: " + className, e); } @@ -384,173 +350,65 @@ private void optimizeSerialization(URL url) throws RpcException { @Override public Invoker refer(Class serviceType, URL url) throws RpcException { optimizeSerialization(url); - // create rpc invoker. DubboInvoker invoker = new DubboInvoker(serviceType, url, getClients(url), invokers); invokers.add(invoker); - return invoker; } private ExchangeClient[] getClients(URL url) { // whether to share connection - - boolean useShareConnect = false; - + boolean service_share_connect = false; int connections = url.getParameter(Constants.CONNECTIONS_KEY, 0); - List shareClients = null; // if not configured, connection is shared, otherwise, one connection for one service if (connections == 0) { - useShareConnect = true; - - /** - * The xml configuration should have a higher priority than properties. - */ - String shareConnectionsStr = url.getParameter(Constants.SHARE_CONNECTIONS_KEY, (String) null); - connections = Integer.parseInt(StringUtils.isBlank(shareConnectionsStr) ? ConfigUtils.getProperty(Constants.SHARE_CONNECTIONS_KEY, - Constants.DEFAULT_SHARE_CONNECTIONS) : shareConnectionsStr); - shareClients = getSharedClient(url, connections); + service_share_connect = true; + connections = 1; } ExchangeClient[] clients = new ExchangeClient[connections]; for (int i = 0; i < clients.length; i++) { - if (useShareConnect) { - clients[i] = shareClients.get(i); - + if (service_share_connect) { + clients[i] = getSharedClient(url); } else { clients[i] = initClient(url); } } - return clients; } /** * Get shared connection - * - * @param url - * @param connectNum connectNum must be greater than or equal to 1 */ - private List getSharedClient(URL url, int connectNum) { + private ExchangeClient getSharedClient(URL url) { String key = url.getAddress(); - List clients = referenceClientMap.get(key); - - if (checkClientCanUse(clients)) { - batchClientRefIncr(clients); - return clients; + ReferenceCountExchangeClient client = referenceClientMap.get(key); + if (client != null) { + if (!client.isClosed()) { + client.incrementAndGetCount(); + return client; + } else { + referenceClientMap.remove(key); + } } locks.putIfAbsent(key, new Object()); synchronized (locks.get(key)) { - clients = referenceClientMap.get(key); - // dubbo check - if (checkClientCanUse(clients)) { - batchClientRefIncr(clients); - return clients; + if (referenceClientMap.containsKey(key)) { + return referenceClientMap.get(key); } - // connectNum must be greater than or equal to 1 - connectNum = Math.max(connectNum, 1); - - // If the clients is empty, then the first initialization is - if (CollectionUtils.isEmpty(clients)) { - clients = buildReferenceCountExchangeClientList(url, connectNum); - referenceClientMap.put(key, clients); - - } else { - for (int i = 0; i < clients.size(); i++) { - ReferenceCountExchangeClient referenceCountExchangeClient = clients.get(i); - // If there is a client in the list that is no longer available, create a new one to replace him. - if (referenceCountExchangeClient == null || referenceCountExchangeClient.isClosed()) { - clients.set(i, buildReferenceCountExchangeClient(url)); - continue; - } - - referenceCountExchangeClient.incrementAndGetCount(); - } - } - - /** - * I understand that the purpose of the remove operation here is to avoid the expired url key - * always occupying this memory space. - */ + ExchangeClient exchangeClient = initClient(url); + client = new ReferenceCountExchangeClient(exchangeClient, ghostClientMap); + referenceClientMap.put(key, client); + ghostClientMap.remove(key); locks.remove(key); - - return clients; + return client; } } - /** - * Check if the client list is all available - * - * @param referenceCountExchangeClients - * @return true-available,false-unavailable - */ - private boolean checkClientCanUse(List referenceCountExchangeClients) { - if (CollectionUtils.isEmpty(referenceCountExchangeClients)) { - return false; - } - - for (ReferenceCountExchangeClient referenceCountExchangeClient : referenceCountExchangeClients) { - // As long as one client is not available, you need to replace the unavailable client with the available one. - if (referenceCountExchangeClient == null || referenceCountExchangeClient.isClosed()) { - return false; - } - } - - return true; - } - - /** - * Add client references in bulk - * - * @param referenceCountExchangeClients - */ - private void batchClientRefIncr(List referenceCountExchangeClients) { - if (CollectionUtils.isEmpty(referenceCountExchangeClients)) { - return; - } - - for (ReferenceCountExchangeClient referenceCountExchangeClient : referenceCountExchangeClients) { - if (referenceCountExchangeClient != null) { - referenceCountExchangeClient.incrementAndGetCount(); - } - } - } - - /** - * Bulk build client - * - * @param url - * @param connectNum - * @return - */ - private List buildReferenceCountExchangeClientList(URL url, int connectNum) { - List clients = new CopyOnWriteArrayList<>(); - - for (int i = 0; i < connectNum; i++) { - clients.add(buildReferenceCountExchangeClient(url)); - } - - return clients; - } - - /** - * Build a single client - * - * @param url - * @return - */ - private ReferenceCountExchangeClient buildReferenceCountExchangeClient(URL url) { - ExchangeClient exchangeClient = initClient(url); - - return new ReferenceCountExchangeClient(exchangeClient); - } - /** * Create new connection - * - * @param url */ private ExchangeClient initClient(URL url) { @@ -572,80 +430,59 @@ private ExchangeClient initClient(URL url) { // connection should be lazy if (url.getParameter(Constants.LAZY_CONNECT_KEY, false)) { client = new LazyConnectExchangeClient(url, requestHandler); - } else { client = Exchangers.connect(url, requestHandler); } - } catch (RemotingException e) { throw new RpcException("Fail to create remoting client for service(" + url + "): " + e.getMessage(), e); } - return client; } @Override public void destroy() { - for (String key : new ArrayList<>(serverMap.keySet())) { + for (String key : new ArrayList(serverMap.keySet())) { ExchangeServer server = serverMap.remove(key); - - if (server == null) { - continue; - } - - try { - if (logger.isInfoEnabled()) { - logger.info("Close dubbo server: " + server.getLocalAddress()); + if (server != null) { + try { + if (logger.isInfoEnabled()) { + logger.info("Close dubbo server: " + server.getLocalAddress()); + } + server.close(ConfigurationUtils.getServerShutdownTimeout()); + } catch (Throwable t) { + logger.warn(t.getMessage(), t); } - - server.close(ConfigurationUtils.getServerShutdownTimeout()); - - } catch (Throwable t) { - logger.warn(t.getMessage(), t); } } - for (String key : new ArrayList<>(referenceClientMap.keySet())) { - List clients = referenceClientMap.remove(key); - - if (CollectionUtils.isEmpty(clients)) { - continue; + for (String key : new ArrayList(referenceClientMap.keySet())) { + ExchangeClient client = referenceClientMap.remove(key); + if (client != null) { + try { + if (logger.isInfoEnabled()) { + logger.info("Close dubbo connect: " + client.getLocalAddress() + "-->" + client.getRemoteAddress()); + } + client.close(ConfigurationUtils.getServerShutdownTimeout()); + } catch (Throwable t) { + logger.warn(t.getMessage(), t); + } } + } - for (ReferenceCountExchangeClient client : clients) { - closeReferenceCountExchangeClient(client); + for (String key : new ArrayList(ghostClientMap.keySet())) { + ExchangeClient client = ghostClientMap.remove(key); + if (client != null) { + try { + if (logger.isInfoEnabled()) { + logger.info("Close dubbo connect: " + client.getLocalAddress() + "-->" + client.getRemoteAddress()); + } + client.close(ConfigurationUtils.getServerShutdownTimeout()); + } catch (Throwable t) { + logger.warn(t.getMessage(), t); + } } } - stubServiceMethodsMap.clear(); super.destroy(); } - - /** - * close ReferenceCountExchangeClient - * - * @param client - */ - private void closeReferenceCountExchangeClient(ReferenceCountExchangeClient client) { - if (client == null) { - return; - } - - try { - if (logger.isInfoEnabled()) { - logger.info("Close dubbo connect: " + client.getLocalAddress() + "-->" + client.getRemoteAddress()); - } - - client.close(ConfigurationUtils.getServerShutdownTimeout()); - - // TODO - /** - * At this time, ReferenceCountExchangeClient#client has been replaced with LazyConnectExchangeClient. - * Do you need to call client.close again to ensure that LazyConnectExchangeClient is also closed? - */ - - } catch (Throwable t) { - logger.warn(t.getMessage(), t); - } - } } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/LazyConnectExchangeClient.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/LazyConnectExchangeClient.java index f2bc4534fdc..b5a4057f54d 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/LazyConnectExchangeClient.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/LazyConnectExchangeClient.java @@ -40,19 +40,14 @@ @SuppressWarnings("deprecation") final class LazyConnectExchangeClient implements ExchangeClient { - /** - * when this warning rises from invocation, program probably have bug. - */ - protected static final String REQUEST_WITH_WARNING_KEY = "lazyclient_request_with_warning"; + // when this warning rises from invocation, program probably have bug. + static final String REQUEST_WITH_WARNING_KEY = "lazyclient_request_with_warning"; private final static Logger logger = LoggerFactory.getLogger(LazyConnectExchangeClient.class); protected final boolean requestWithWarning; private final URL url; private final ExchangeHandler requestHandler; private final Lock connectLock = new ReentrantLock(); - private final int warning_period = 5000; - /** - * lazy connect, initial state for connection - */ + // lazy connect, initial state for connection private final boolean initialState; private volatile ExchangeClient client; private AtomicLong warningcount = new AtomicLong(0); @@ -65,6 +60,7 @@ public LazyConnectExchangeClient(URL url, ExchangeHandler requestHandler) { this.requestWithWarning = url.getParameter(REQUEST_WITH_WARNING_KEY, false); } + private void initClient() throws RemotingException { if (client != null) { return; @@ -85,7 +81,7 @@ private void initClient() throws RemotingException { @Override public ResponseFuture request(Object request) throws RemotingException { - warning(); + warning(request); initClient(); return client.request(request); } @@ -106,17 +102,19 @@ public InetSocketAddress getRemoteAddress() { @Override public ResponseFuture request(Object request, int timeout) throws RemotingException { - warning(); + warning(request); initClient(); return client.request(request, timeout); } /** * If {@link #REQUEST_WITH_WARNING_KEY} is configured, then warn once every 5000 invocations. + * + * @param request */ - private void warning() { + private void warning(Object request) { if (requestWithWarning) { - if (warningcount.get() % warning_period == 0) { + if (warningcount.get() % 5000 == 0) { logger.warn(new IllegalStateException("safe guard client , should not be called ,must have a bug.")); } warningcount.incrementAndGet(); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClient.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClient.java index faafe082852..834711515f8 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClient.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClient.java @@ -19,7 +19,6 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.Parameters; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.remoting.ChannelHandler; import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.exchange.ExchangeClient; @@ -27,6 +26,7 @@ import org.apache.dubbo.remoting.exchange.ResponseFuture; import java.net.InetSocketAddress; +import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicInteger; /** @@ -38,12 +38,19 @@ final class ReferenceCountExchangeClient implements ExchangeClient { private final URL url; private final AtomicInteger referenceCount = new AtomicInteger(0); + // private final ExchangeHandler handler; + private final ConcurrentMap ghostClientMap; private ExchangeClient client; - public ReferenceCountExchangeClient(ExchangeClient client) { + + public ReferenceCountExchangeClient(ExchangeClient client, ConcurrentMap ghostClientMap) { this.client = client; referenceCount.incrementAndGet(); this.url = client.getUrl(); + if (ghostClientMap == null) { + throw new IllegalStateException("ghostClientMap can not be null, url: " + url); + } + this.ghostClientMap = ghostClientMap; } @Override @@ -144,12 +151,10 @@ public void close(int timeout) { if (referenceCount.decrementAndGet() <= 0) { if (timeout == 0) { client.close(); - } else { client.close(timeout); } - - replaceWithLazyClient(); + client = replaceWithLazyClient(); } } @@ -158,29 +163,24 @@ public void startClose() { client.startClose(); } - /** - * when closing the client, the client needs to be set to LazyConnectExchangeClient, and if a new call is made, - * the client will "resurrect". - * - * @return - */ - private void replaceWithLazyClient() { + // ghost client + private LazyConnectExchangeClient replaceWithLazyClient() { // this is a defensive operation to avoid client is closed by accident, the initial state of the client is false - URL lazyUrl = URLBuilder.from(url) - .addParameter(Constants.LAZY_CONNECT_INITIAL_STATE_KEY, Boolean.FALSE) + URL lazyUrl = url.addParameter(Constants.LAZY_CONNECT_INITIAL_STATE_KEY, Boolean.FALSE) .addParameter(Constants.RECONNECT_KEY, Boolean.FALSE) .addParameter(Constants.SEND_RECONNECT_KEY, Boolean.TRUE.toString()) .addParameter("warning", Boolean.TRUE.toString()) .addParameter(LazyConnectExchangeClient.REQUEST_WITH_WARNING_KEY, true) - .addParameter("_client_memo", "referencecounthandler.replacewithlazyclient") - .build(); - - /** - * the order of judgment in the if statement cannot be changed. - */ - if (!(client instanceof LazyConnectExchangeClient) || client.isClosed()) { - client = new LazyConnectExchangeClient(lazyUrl, client.getExchangeHandler()); + .addParameter("_client_memo", "referencecounthandler.replacewithlazyclient"); + + String key = url.getAddress(); + // in worst case there's only one ghost connection. + LazyConnectExchangeClient gclient = ghostClientMap.get(key); + if (gclient == null || gclient.isClosed()) { + gclient = new LazyConnectExchangeClient(lazyUrl, client.getExchangeHandler()); + ghostClientMap.put(key, gclient); } + return gclient; } @Override @@ -192,4 +192,3 @@ public void incrementAndGetCount() { referenceCount.incrementAndGet(); } } - diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java index 1f50d11378e..4f2c1733d27 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java @@ -206,22 +206,14 @@ private ConsumerMethodModel.AsyncMethodInfo getAsyncMethodInfo(Invoker invoke if (consumerModel == null) { return null; } - - String methodName = invocation.getMethodName(); - if (methodName.equals(Constants.$INVOKE)) { - methodName = (String) invocation.getArguments()[0]; - } - - ConsumerMethodModel methodModel = consumerModel.getMethodModel(methodName); + ConsumerMethodModel methodModel = consumerModel.getMethodModel(invocation.getMethodName()); if (methodModel == null) { return null; } - final ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = (ConsumerMethodModel.AsyncMethodInfo) methodModel.getAttribute(Constants.ASYNC_KEY); if (asyncMethodInfo == null) { return null; } - return asyncMethodInfo; } } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/TraceFilter.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/TraceFilter.java index 1249c93e2c8..53ae2bd457c 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/TraceFilter.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/TraceFilter.java @@ -20,7 +20,6 @@ import org.apache.dubbo.common.extension.Activate; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.ConcurrentHashSet; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.rpc.Filter; @@ -50,7 +49,7 @@ public class TraceFilter implements Filter { private static final String TRACE_COUNT = "trace.count"; - private static final ConcurrentMap> tracers = new ConcurrentHashMap<>(); + private static final ConcurrentMap> tracers = new ConcurrentHashMap>(); public static void addTracer(Class type, String method, Channel channel, int max) { channel.setAttribute(TRACE_MAX, max); @@ -58,7 +57,7 @@ public static void addTracer(Class type, String method, Channel channel, int String key = method != null && method.length() > 0 ? type.getName() + "." + method : type.getName(); Set channels = tracers.get(key); if (channels == null) { - tracers.putIfAbsent(key, new ConcurrentHashSet<>()); + tracers.putIfAbsent(key, new ConcurrentHashSet()); channels = tracers.get(key); } channels.add(channel); @@ -86,14 +85,14 @@ public Result invoke(Invoker invoker, Invocation invocation) throws RpcExcept key = invoker.getInterface().getName(); channels = tracers.get(key); } - if (CollectionUtils.isNotEmpty(channels)) { - for (Channel channel : new ArrayList<>(channels)) { + if (channels != null && !channels.isEmpty()) { + for (Channel channel : new ArrayList(channels)) { if (channel.isConnected()) { try { int max = 1; Integer m = (Integer) channel.getAttribute(TRACE_MAX); if (m != null) { - max = m; + max = (int) m; } int count = 0; AtomicInteger c = (AtomicInteger) channel.getAttribute(TRACE_COUNT); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java index 56fe5b8e564..adbce4e11b2 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java @@ -16,9 +16,7 @@ */ package org.apache.dubbo.rpc.protocol.dubbo.telnet; -import com.alibaba.fastjson.JSON; import org.apache.dubbo.common.extension.Activate; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.remoting.Channel; @@ -29,6 +27,9 @@ import org.apache.dubbo.rpc.model.ProviderMethodModel; import org.apache.dubbo.rpc.model.ProviderModel; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; + import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collection; @@ -36,18 +37,89 @@ import java.util.Map; import static org.apache.dubbo.common.utils.PojoUtils.realize; +import static org.apache.dubbo.rpc.RpcContext.getContext; /** * InvokeTelnetHandler */ @Activate -@Help(parameter = "[service.]method(args) ", summary = "Invoke the service method.", +@Help(parameter = "[service.]method(args) [-p parameter classes]", summary = "Invoke the service method.", detail = "Invoke the service method.") public class InvokeTelnetHandler implements TelnetHandler { + private static Method findMethod(List methods, String method, List args, + Class[] paramTypes) { + for (ProviderMethodModel model : methods) { + Method m = model.getMethod(); + if (isMatch(m, args, paramTypes,method)) { + return m; + } + } + return null; + } - public static final String INVOKE_MESSAGE_KEY = "telnet.invoke.method.message"; - public static final String INVOKE_METHOD_LIST_KEY = "telnet.invoke.method.list"; - public static final String INVOKE_METHOD_PROVIDER_KEY = "telnet.invoke.method.provider"; + private static boolean isMatch(Method method,List args, Class[] paramClasses,String lookupMethodName) { + if(!method.getName().equals(lookupMethodName)) { + return false; + } + + Class types[]=method.getParameterTypes(); + if (types.length != args.size()) { + return false; + } + for (int i = 0; i < types.length; i++) { + Class type = types[i]; + Object arg = args.get(i); + + if (paramClasses != null && type != paramClasses[i]) { + return false; + } + + if (arg == null) { + // if the type is primitive, the method to invoke will cause NullPointerException definitely + // so we can offer a specified error message to the invoker in advance and avoid unnecessary invoking + if (type.isPrimitive()) { + throw new NullPointerException(String.format("The type of No.%d parameter is primitive(%s), " + + "but the value passed is null.", i + 1, type.getName())); + } + + // if the type is not primitive, we choose to believe what the invoker want is a null value + continue; + } + + if (ReflectUtils.isPrimitive(arg.getClass())) { + // allow string arg to enum type, @see PojoUtils.realize0() + if (arg instanceof String && type.isEnum()) { + continue; + } + + if (!ReflectUtils.isPrimitive(type)) { + return false; + } + + if (!ReflectUtils.isCompatible(type, arg)) { + return false; + } + } else if (arg instanceof Map) { + String name = (String) ((Map) arg).get("class"); + Class cls = arg.getClass(); + if (name != null && name.length() > 0) { + cls = ReflectUtils.forName(name); + } + if (!type.isAssignableFrom(cls)) { + return false; + } + } else if (arg instanceof Collection) { + if (!type.isArray() && !type.isAssignableFrom(arg.getClass())) { + return false; + } + } else { + if (!type.isAssignableFrom(arg.getClass())) { + return false; + } + } + } + return true; + } @Override @SuppressWarnings("unchecked") @@ -58,9 +130,33 @@ public String telnet(Channel channel, String message) { "invoke com.xxx.XxxService.xxxMethod(1234, \"abcd\", {\"prop\" : \"value\"})"; } + StringBuilder buf = new StringBuilder(); String service = (String) channel.getAttribute(ChangeTelnetHandler.SERVICE_KEY); + if (!StringUtils.isEmpty(service)) { + buf.append("Use default service ").append(service).append(".\r\n"); + } int i = message.indexOf("("); + String originalMessage = message; + Class[] paramTypes = null; + if (message.contains("-p")) { + message = originalMessage.substring(0, originalMessage.indexOf("-p")).trim(); + String paramClassesString = originalMessage.substring(originalMessage.indexOf("-p") + 2).trim(); + if (paramClassesString.length() > 0) { + String[] split = paramClassesString.split("\\s+"); + if (split.length > 0) { + paramTypes = new Class[split.length]; + for (int j = 0; j < split.length; j++) { + try { + paramTypes[j] = Class.forName(split[j]); + } catch (ClassNotFoundException e) { + return "Unknown parameter class for name " + split[j]; + } + } + + } + } + } if (i < 0 || !message.endsWith(")")) { return "Invalid parameters, format: service.method(args)"; @@ -80,49 +176,38 @@ public String telnet(Channel channel, String message) { } catch (Throwable t) { return "Invalid json argument, cause: " + t.getMessage(); } - StringBuilder buf = new StringBuilder(); - Method invokeMethod = null; - ProviderModel selectedProvider = null; - if (isInvokedSelectCommand(channel)) { - selectedProvider = (ProviderModel) channel.getAttribute(INVOKE_METHOD_PROVIDER_KEY); - invokeMethod = (Method) channel.getAttribute(SelectTelnetHandler.SELECT_METHOD_KEY); - } else { - for (ProviderModel provider : ApplicationModel.allProviderModels()) { - if (isServiceMatch(service, provider)) { - selectedProvider = provider; - List methodList = findSameSignatureMethod(provider.getAllMethods(), method, list); - if (CollectionUtils.isNotEmpty(methodList)) { - if (methodList.size() == 1) { - invokeMethod = methodList.get(0); - } else { - List matchMethods = findMatchMethods(methodList, list); - if (CollectionUtils.isNotEmpty(matchMethods)) { - if (matchMethods.size() == 1) { - invokeMethod = matchMethods.get(0); - } else { //exist overridden method - channel.setAttribute(INVOKE_METHOD_PROVIDER_KEY, provider); - channel.setAttribute(INVOKE_METHOD_LIST_KEY, matchMethods); - channel.setAttribute(INVOKE_MESSAGE_KEY, message); - printSelectMessage(buf, matchMethods); - return buf.toString(); - } - } - } - } - break; + if (paramTypes != null) { + if (paramTypes.length != list.size()) { + return "Parameter's number does not match the number of parameter class"; + } + List listOfActualClass = new ArrayList<>(list.size()); + for (int ii = 0; ii < list.size(); ii++) { + if (list.get(ii) instanceof JSONObject) { + JSONObject jsonObject = (JSONObject) list.get(ii); + listOfActualClass.add(jsonObject.toJavaObject(paramTypes[ii])); + } else { + listOfActualClass.add(list.get(ii)); } } + list = listOfActualClass; } - - if (!StringUtils.isEmpty(service)) { - buf.append("Use default service ").append(service).append("."); + Method invokeMethod = null; + ProviderModel selectedProvider = null; + for (ProviderModel provider : ApplicationModel.allProviderModels()) { + if (isServiceMatch(service, provider)) { + invokeMethod = findMethod(provider.getAllMethods(), method, list, paramTypes); + selectedProvider = provider; + break; + } } + if (selectedProvider != null) { if (invokeMethod != null) { try { Object[] array = realize(list.toArray(), invokeMethod.getParameterTypes(), invokeMethod.getGenericParameterTypes()); + getContext().setLocalAddress(channel.getLocalAddress()).setRemoteAddress(channel.getRemoteAddress()); long start = System.currentTimeMillis(); RpcResult result = new RpcResult(); try { @@ -132,7 +217,6 @@ public String telnet(Channel channel, String message) { result.setException(t); } long end = System.currentTimeMillis(); - buf.append("\r\nresult: "); buf.append(JSON.toJSONString(result.recreate())); buf.append("\r\nelapsed: "); buf.append(end - start); @@ -141,119 +225,18 @@ public String telnet(Channel channel, String message) { return "Failed to invoke method " + invokeMethod.getName() + ", cause: " + StringUtils.toString(t); } } else { - buf.append("\r\nNo such method ").append(method).append(" in service ").append(service); + buf.append("No such method ").append(method).append(" in service ").append(service); } } else { - buf.append("\r\nNo such service ").append(service); + buf.append("No such service ").append(service); } return buf.toString(); } - private boolean isServiceMatch(String service, ProviderModel provider) { return provider.getServiceName().equalsIgnoreCase(service) || provider.getServiceInterfaceClass().getSimpleName().equalsIgnoreCase(service) || provider.getServiceInterfaceClass().getName().equalsIgnoreCase(service) || StringUtils.isEmpty(service); } - - private List findSameSignatureMethod(List methods, String lookupMethodName, List args) { - List sameSignatureMethods = new ArrayList<>(); - for (ProviderMethodModel model : methods) { - Method method = model.getMethod(); - if (method.getName().equals(lookupMethodName) && method.getParameterTypes().length == args.size()) { - sameSignatureMethods.add(method); - } - } - return sameSignatureMethods; - } - - private List findMatchMethods(List methods, List args) { - List matchMethod = new ArrayList<>(); - for (Method method : methods) { - if (isMatch(method, args)) { - matchMethod.add(method); - } - } - return matchMethod; - } - - private static boolean isMatch(Method method, List args) { - Class[] types = method.getParameterTypes(); - if (types.length != args.size()) { - return false; - } - for (int i = 0; i < types.length; i++) { - Class type = types[i]; - Object arg = args.get(i); - - if (arg == null) { - if (type.isPrimitive()) { - return false; - } - - // if the type is not primitive, we choose to believe what the invoker want is a null value - continue; - } - - if (ReflectUtils.isPrimitive(arg.getClass())) { - // allow string arg to enum type, @see PojoUtils.realize0() - if (arg instanceof String && type.isEnum()) { - continue; - } - - if (!ReflectUtils.isPrimitive(type)) { - return false; - } - - if (!ReflectUtils.isCompatible(type, arg)) { - return false; - } - } else if (arg instanceof Map) { - String name = (String) ((Map) arg).get("class"); - if (StringUtils.isNotEmpty(name)) { - Class cls = ReflectUtils.forName(name); - if (!type.isAssignableFrom(cls)) { - return false; - } - } else { - return true; - } - } else if (arg instanceof Collection) { - if (!type.isArray() && !type.isAssignableFrom(arg.getClass())) { - return false; - } - } else { - if (!type.isAssignableFrom(arg.getClass())) { - return false; - } - } - } - return true; - } - - private void printSelectMessage(StringBuilder buf, List methods) { - buf.append("Methods:\r\n"); - for (int i = 0; i < methods.size(); i++) { - Method method = methods.get(i); - buf.append((i + 1) + ". " + method.getName() + "("); - Class[] parameterTypes = method.getParameterTypes(); - for (int n = 0; n < parameterTypes.length; n++) { - buf.append(parameterTypes[n].getSimpleName()); - if (n != parameterTypes.length - 1) { - buf.append(","); - } - } - buf.append(")\r\n"); - } - buf.append("Please use the select command to select the method you want to invoke. eg: select 1"); - } - - private boolean isInvokedSelectCommand(Channel channel) { - if (channel.hasAttribute(SelectTelnetHandler.SELECT_KEY)) { - channel.removeAttribute(SelectTelnetHandler.SELECT_KEY); - return true; - } - return false; - } } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/LogTelnetHandler.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/LogTelnetHandler.java index b62198d11d4..ac2379784c1 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/LogTelnetHandler.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/LogTelnetHandler.java @@ -52,19 +52,20 @@ public String telnet(Channel channel, String message) { if (!StringUtils.isInteger(str[0])) { LoggerFactory.setLevel(Level.valueOf(message.toUpperCase())); } else { - int showLogLength = Integer.parseInt(str[0]); + int SHOW_LOG_LENGTH = Integer.parseInt(str[0]); if (file != null && file.exists()) { - try(FileInputStream fis = new FileInputStream(file)) { + try { + FileInputStream fis = new FileInputStream(file); FileChannel filechannel = fis.getChannel(); size = filechannel.size(); ByteBuffer bb; - if (size <= showLogLength) { + if (size <= SHOW_LOG_LENGTH) { bb = ByteBuffer.allocate((int) size); filechannel.read(bb, 0); } else { - int pos = (int) (size - showLogLength); - bb = ByteBuffer.allocate(showLogLength); + int pos = (int) (size - SHOW_LOG_LENGTH); + bb = ByteBuffer.allocate(SHOW_LOG_LENGTH); filechannel.read(bb, pos); } bb.flip(); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/SelectTelnetHandler.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/SelectTelnetHandler.java deleted file mode 100644 index ed76ebb3913..00000000000 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/SelectTelnetHandler.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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 org.apache.dubbo.rpc.protocol.dubbo.telnet; - -import org.apache.dubbo.common.extension.Activate; -import org.apache.dubbo.common.utils.CollectionUtils; -import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.remoting.Channel; -import org.apache.dubbo.remoting.telnet.TelnetHandler; -import org.apache.dubbo.remoting.telnet.support.Help; - -import java.lang.reflect.Method; -import java.util.List; - -/** - * SelectTelnetHandler - */ -@Activate -@Help(parameter = "[index]", summary = "Select the index of the method you want to invoke.", - detail = "Select the index of the method you want to invoke.") -public class SelectTelnetHandler implements TelnetHandler { - public static final String SELECT_METHOD_KEY = "telnet.select.method"; - public static final String SELECT_KEY = "telnet.select"; - - private InvokeTelnetHandler invokeTelnetHandler = new InvokeTelnetHandler(); - - @Override - @SuppressWarnings("unchecked") - public String telnet(Channel channel, String message) { - if (message == null || message.length() == 0) { - return "Please input the index of the method you want to invoke, eg: \r\n select 1"; - } - List methodList = (List) channel.getAttribute(InvokeTelnetHandler.INVOKE_METHOD_LIST_KEY); - if (CollectionUtils.isEmpty(methodList)) { - return "Please use the invoke command first."; - } - if (!StringUtils.isInteger(message) || Integer.parseInt(message) < 1 || Integer.parseInt(message) > methodList.size()) { - return "Illegal index ,please input select 1~" + methodList.size(); - } - Method method = methodList.get(Integer.parseInt(message)); - channel.setAttribute(SELECT_METHOD_KEY, method); - channel.setAttribute(SELECT_KEY, Boolean.TRUE); - String invokeMessage = (String) channel.getAttribute(InvokeTelnetHandler.INVOKE_MESSAGE_KEY); - return invokeTelnetHandler.telnet(channel, invokeMessage); - } -} diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ShutdownTelnetHandler.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ShutdownTelnetHandler.java deleted file mode 100644 index ff5bf07cb33..00000000000 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ShutdownTelnetHandler.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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 org.apache.dubbo.rpc.protocol.dubbo.telnet; - -import org.apache.dubbo.common.extension.Activate; -import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.config.DubboShutdownHook; -import org.apache.dubbo.remoting.Channel; -import org.apache.dubbo.remoting.RemotingException; -import org.apache.dubbo.remoting.telnet.TelnetHandler; -import org.apache.dubbo.remoting.telnet.support.Help; - -/** - * ShutdownTelnetHandler - */ -@Activate -@Help(parameter = "[-t ]", summary = "Shutdown Dubbo Application.", detail = "Shutdown Dubbo Application.") -public class ShutdownTelnetHandler implements TelnetHandler { - @Override - public String telnet(Channel channel, String message) throws RemotingException { - - int sleepMilliseconds = 0; - if (StringUtils.isNotEmpty(message)) { - String[] parameters = message.split("\\s+"); - if (parameters.length == 2 && parameters[0].equals("-t") && StringUtils.isInteger(parameters[1])) { - sleepMilliseconds = Integer.parseInt(parameters[1]); - } else { - return "Invalid parameter,please input like shutdown -t 10000"; - } - } - long start = System.currentTimeMillis(); - if (sleepMilliseconds > 0) { - try { - Thread.sleep(sleepMilliseconds); - } catch (InterruptedException e) { - return "Failed to invoke shutdown command, cause: " + e.getMessage(); - } - } - StringBuilder buf = new StringBuilder(); - DubboShutdownHook.getDubboShutdownHook().unregister(); - DubboShutdownHook.getDubboShutdownHook().doDestroy(); - long end = System.currentTimeMillis(); - buf.append("Application has shutdown successfully"); - buf.append("\r\nelapsed: "); - buf.append(end - start); - buf.append(" ms."); - return buf.toString(); - } -} diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/TraceTelnetHandler.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/TraceTelnetHandler.java index a3940e1613e..5045b31dacc 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/TraceTelnetHandler.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/TraceTelnetHandler.java @@ -45,18 +45,17 @@ public String telnet(Channel channel, String message) { String[] parts = message.split("\\s+"); String method; String times; - // message like : XxxService , XxxService 10 , XxxService xxxMethod , XxxService xxxMethod 10 - if (StringUtils.isEmpty(service)) { + if (service == null || service.length() == 0) { service = parts.length > 0 ? parts[0] : null; method = parts.length > 1 ? parts[1] : null; - times = parts.length > 2 ? parts[2] : "1"; - } else { //message like : xxxMethod, xxxMethod 10 + } else { method = parts.length > 0 ? parts[0] : null; - times = parts.length > 1 ? parts[1] : "1"; } if (StringUtils.isInteger(method)) { times = method; method = null; + } else { + times = parts.length > 2 ? parts[2] : "1"; } if (!StringUtils.isInteger(times)) { return "Illegal times " + times + ", must be integer."; diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.telnet.TelnetHandler b/dubbo-rpc/dubbo-rpc-dubbo/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.telnet.TelnetHandler index 99111639c26..ad5b55f9e04 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.telnet.TelnetHandler +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.telnet.TelnetHandler @@ -4,6 +4,4 @@ cd=org.apache.dubbo.rpc.protocol.dubbo.telnet.ChangeTelnetHandler pwd=org.apache.dubbo.rpc.protocol.dubbo.telnet.CurrentTelnetHandler invoke=org.apache.dubbo.rpc.protocol.dubbo.telnet.InvokeTelnetHandler trace=org.apache.dubbo.rpc.protocol.dubbo.telnet.TraceTelnetHandler -count=org.apache.dubbo.rpc.protocol.dubbo.telnet.CountTelnetHandler -select=org.apache.dubbo.rpc.protocol.dubbo.telnet.SelectTelnetHandler -shutdown=org.apache.dubbo.rpc.protocol.dubbo.telnet.ShutdownTelnetHandler \ No newline at end of file +count=org.apache.dubbo.rpc.protocol.dubbo.telnet.CountTelnetHandler \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvokerAvilableTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvokerAvilableTest.java index e2b71d05432..08444f7c658 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvokerAvilableTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvokerAvilableTest.java @@ -26,16 +26,16 @@ import org.apache.dubbo.rpc.Exporter; import org.apache.dubbo.rpc.ProxyFactory; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; import java.lang.reflect.Field; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.fail; /** * Check available status for dubboInvoker @@ -44,15 +44,15 @@ public class DubboInvokerAvilableTest { private static DubboProtocol protocol = DubboProtocol.getDubboProtocol(); private static ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); - @BeforeAll + @BeforeClass public static void setUpBeforeClass() throws Exception { } - @BeforeEach + @Before public void setUp() throws Exception { } - @AfterAll + @AfterClass public static void tearDownAfterClass() { ProtocolUtils.closeAll(); } @@ -63,9 +63,9 @@ public void test_Normal_available() { ProtocolUtils.export(new DemoServiceImpl(), IDemoService.class, url); DubboInvoker invoker = (DubboInvoker) protocol.refer(IDemoService.class, url); - Assertions.assertEquals(true, invoker.isAvailable()); + Assert.assertEquals(true, invoker.isAvailable()); invoker.destroy(); - Assertions.assertEquals(false, invoker.isAvailable()); + Assert.assertEquals(false, invoker.isAvailable()); } @Test @@ -74,17 +74,17 @@ public void test_Normal_ChannelReadOnly() throws Exception { ProtocolUtils.export(new DemoServiceImpl(), IDemoService.class, url); DubboInvoker invoker = (DubboInvoker) protocol.refer(IDemoService.class, url); - Assertions.assertEquals(true, invoker.isAvailable()); + Assert.assertEquals(true, invoker.isAvailable()); getClients(invoker)[0].setAttribute(Constants.CHANNEL_ATTRIBUTE_READONLY_KEY, Boolean.TRUE); - Assertions.assertEquals(false, invoker.isAvailable()); + Assert.assertEquals(false, invoker.isAvailable()); // reset status since connection is shared among invokers getClients(invoker)[0].removeAttribute(Constants.CHANNEL_ATTRIBUTE_READONLY_KEY); } - @Disabled + @Ignore public void test_normal_channel_close_wait_gracefully() throws Exception { int testPort = NetUtils.getAvailablePort(); URL url = URL.valueOf("dubbo://127.0.0.1:" + testPort + "/org.apache.dubbo.rpc.protocol.dubbo.IDemoService?scope=true&lazy=false"); @@ -104,8 +104,8 @@ public void test_normal_channel_close_wait_gracefully() throws Exception { long waitTime = System.currentTimeMillis() - start; - Assertions.assertTrue(waitTime >= 2000); - Assertions.assertEquals(false, invoker.isAvailable()); + Assert.assertTrue(waitTime >= 2000); + Assert.assertEquals(false, invoker.isAvailable()); } @Test @@ -117,7 +117,7 @@ public void test_NoInvokers() throws Exception { ExchangeClient[] clients = getClients(invoker); clients[0].close(); - Assertions.assertEquals(false, invoker.isAvailable()); + Assert.assertEquals(false, invoker.isAvailable()); } @@ -127,7 +127,7 @@ public void test_Lazy_ChannelReadOnly() throws Exception { ProtocolUtils.export(new DemoServiceImpl(), IDemoService.class, url); DubboInvoker invoker = (DubboInvoker) protocol.refer(IDemoService.class, url); - Assertions.assertEquals(true, invoker.isAvailable()); + Assert.assertEquals(true, invoker.isAvailable()); try { getClients(invoker)[0].setAttribute(Constants.CHANNEL_ATTRIBUTE_READONLY_KEY, Boolean.TRUE); @@ -137,18 +137,18 @@ public void test_Lazy_ChannelReadOnly() throws Exception { } //invoke method --> init client IDemoService service = (IDemoService) proxy.getProxy(invoker); - Assertions.assertEquals("ok", service.get()); + Assert.assertEquals("ok", service.get()); - Assertions.assertEquals(true, invoker.isAvailable()); + Assert.assertEquals(true, invoker.isAvailable()); getClients(invoker)[0].setAttribute(Constants.CHANNEL_ATTRIBUTE_READONLY_KEY, Boolean.TRUE); - Assertions.assertEquals(false, invoker.isAvailable()); + Assert.assertEquals(false, invoker.isAvailable()); } private ExchangeClient[] getClients(DubboInvoker invoker) throws Exception { Field field = DubboInvoker.class.getDeclaredField("clients"); field.setAccessible(true); ExchangeClient[] clients = (ExchangeClient[]) field.get(invoker); - Assertions.assertEquals(1, clients.length); + Assert.assertEquals(1, clients.length); return clients; } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboLazyConnectTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboLazyConnectTest.java index d4a98bab90d..31a4421e76f 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboLazyConnectTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboLazyConnectTest.java @@ -22,37 +22,34 @@ import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; - -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; /** * dubbo protocol lazy connect test */ public class DubboLazyConnectTest { - @BeforeAll - public static void setUpBeforeClass() { + @BeforeClass + public static void setUpBeforeClass() throws Exception { } - @BeforeEach - public void setUp() { + @Before + public void setUp() throws Exception { } - @AfterAll + @AfterClass public static void tearDownAfterClass() { ProtocolUtils.closeAll(); } - @Test + @Test(expected = RpcException.class) public void testSticky1() { - Assertions.assertThrows(RpcException.class, () -> { - URL url = URL.valueOf("dubbo://127.0.0.1:9090/org.apache.dubbo.rpc.protocol.dubbo.IDemoService"); - ProtocolUtils.refer(IDemoService.class, url); - }); + URL url = URL.valueOf("dubbo://127.0.0.1:9090/org.apache.dubbo.rpc.protocol.dubbo.IDemoService"); + ProtocolUtils.refer(IDemoService.class, url); } @Test @@ -61,13 +58,11 @@ public void testSticky2() { ProtocolUtils.refer(IDemoService.class, url); } - @Test + @Test(expected = RpcException.class) public void testSticky3() { - Assertions.assertThrows(RpcException.class, () -> { - URL url = URL.valueOf("dubbo://127.0.0.1:9090/org.apache.dubbo.rpc.protocol.dubbo.IDemoService?" + Constants.LAZY_CONNECT_KEY + "=true"); - IDemoService service = (IDemoService) ProtocolUtils.refer(IDemoService.class, url); - service.get(); - }); + URL url = URL.valueOf("dubbo://127.0.0.1:9090/org.apache.dubbo.rpc.protocol.dubbo.IDemoService?" + Constants.LAZY_CONNECT_KEY + "=true"); + IDemoService service = (IDemoService) ProtocolUtils.refer(IDemoService.class, url); + service.get(); } @Test @@ -78,7 +73,7 @@ public void testSticky4() { ProtocolUtils.export(new DemoServiceImpl(), IDemoService.class, url); IDemoService service = (IDemoService) ProtocolUtils.refer(IDemoService.class, url); - Assertions.assertEquals("ok", service.get()); + Assert.assertEquals("ok", service.get()); } public class DemoServiceImpl implements IDemoService { diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocolTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocolTest.java index 61d9b6cc148..ef04a857bda 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocolTest.java @@ -32,15 +32,15 @@ import org.apache.dubbo.rpc.protocol.dubbo.support.Type; import org.apache.dubbo.rpc.service.EchoService; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Test; import java.util.HashMap; import java.util.Map; import java.util.Set; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; /** * ProxiesTest @@ -50,7 +50,7 @@ public class DubboProtocolTest { private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); - @AfterAll + @AfterClass public static void after() { ProtocolUtils.closeAll(); } @@ -170,9 +170,9 @@ public void testNonSerializedParameter() throws Exception { service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:9050/" + DemoService.class.getName() + "?codec=exchange").addParameter("timeout", 3000l))); try { service.nonSerializedParameter(new NonSerialized()); - Assertions.fail(); + Assert.fail(); } catch (RpcException e) { - Assertions.assertTrue(e.getMessage().contains("org.apache.dubbo.rpc.protocol.dubbo.support.NonSerialized must implement java.io.Serializable")); + Assert.assertTrue(e.getMessage().contains("org.apache.dubbo.rpc.protocol.dubbo.support.NonSerialized must implement java.io.Serializable")); } } @@ -183,9 +183,9 @@ public void testReturnNonSerialized() throws Exception { service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:9050/" + DemoService.class.getName() + "?codec=exchange").addParameter("timeout", 3000l))); try { service.returnNonSerialized(); - Assertions.fail(); + Assert.fail(); } catch (RpcException e) { - Assertions.assertTrue(e.getMessage().contains("org.apache.dubbo.rpc.protocol.dubbo.support.NonSerialized must implement java.io.Serializable")); + Assert.assertTrue(e.getMessage().contains("org.apache.dubbo.rpc.protocol.dubbo.support.NonSerialized must implement java.io.Serializable")); } } } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ExplicitCallbackTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ExplicitCallbackTest.java index bcf80dfc36b..e7a2b2a8837 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ExplicitCallbackTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ExplicitCallbackTest.java @@ -24,11 +24,11 @@ import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; import java.util.ArrayList; import java.util.List; @@ -46,7 +46,7 @@ public class ExplicitCallbackTest { // ============================A gorgeous line of segmentation================================================ IDemoService demoProxy = null; - @AfterEach + @After public void tearDown() { destroyService(); ProtocolUtils.closeAll(); @@ -64,7 +64,7 @@ void referService() { demoProxy = (IDemoService) ProtocolUtils.refer(IDemoService.class, consumerUrl); } - @BeforeEach + @Before public void setUp() { } @@ -140,89 +140,85 @@ public String yyy(String msg) { }; { demoProxy.xxx2(callback); - Assertions.assertEquals(1, demoProxy.getCallbackCount()); + Assert.assertEquals(1, demoProxy.getCallbackCount()); Thread.sleep(500); demoProxy.unxxx2(callback); - Assertions.assertEquals(0, demoProxy.getCallbackCount()); + Assert.assertEquals(0, demoProxy.getCallbackCount()); demoProxy.xxx2(callback2); - Assertions.assertEquals(1, demoProxy.getCallbackCount()); + Assert.assertEquals(1, demoProxy.getCallbackCount()); Thread.sleep(500); demoProxy.unxxx2(callback2); - Assertions.assertEquals(0, demoProxy.getCallbackCount()); + Assert.assertEquals(0, demoProxy.getCallbackCount()); demoProxy.xxx2(callback); Thread.sleep(500); - Assertions.assertEquals(1, demoProxy.getCallbackCount()); + Assert.assertEquals(1, demoProxy.getCallbackCount()); demoProxy.unxxx2(callback); - Assertions.assertEquals(0, demoProxy.getCallbackCount()); + Assert.assertEquals(0, demoProxy.getCallbackCount()); } { demoProxy.xxx2(callback); - Assertions.assertEquals(1, demoProxy.getCallbackCount()); + Assert.assertEquals(1, demoProxy.getCallbackCount()); demoProxy.xxx2(callback); - Assertions.assertEquals(1, demoProxy.getCallbackCount()); + Assert.assertEquals(1, demoProxy.getCallbackCount()); demoProxy.xxx2(callback2); - Assertions.assertEquals(2, demoProxy.getCallbackCount()); + Assert.assertEquals(2, demoProxy.getCallbackCount()); } destroyService(); } - @Test + @Test(expected = RpcException.class) public void TestCallbackConsumerLimit() throws Exception { - Assertions.assertThrows(RpcException.class, () -> { - initOrResetUrl(1, 1000); - // URL cannot be transferred automatically from the server side to the client side by using API, instead, - // it needs manually specified. - initOrResetService(); - final AtomicInteger count = new AtomicInteger(0); - demoProxy.xxx(new IDemoCallback() { - public String yyy(String msg) { - System.out.println("Recived callback: " + msg); - count.incrementAndGet(); - return "ok"; - } - }, "other custom args", 10, 100); + initOrResetUrl(1, 1000); + // URL cannot be transferred automatically from the server side to the client side by using API, instead, + // it needs manually specified. + initOrResetService(); + final AtomicInteger count = new AtomicInteger(0); + demoProxy.xxx(new IDemoCallback() { + public String yyy(String msg) { + System.out.println("Recived callback: " + msg); + count.incrementAndGet(); + return "ok"; + } + }, "other custom args", 10, 100); - demoProxy.xxx(new IDemoCallback() { - public String yyy(String msg) { - System.out.println("Recived callback: " + msg); - count.incrementAndGet(); - return "ok"; - } - }, "other custom args", 10, 100); - destroyService(); - }); + demoProxy.xxx(new IDemoCallback() { + public String yyy(String msg) { + System.out.println("Recived callback: " + msg); + count.incrementAndGet(); + return "ok"; + } + }, "other custom args", 10, 100); + destroyService(); } - @Test + @Test(expected = RpcException.class) public void TestCallbackProviderLimit() throws Exception { - Assertions.assertThrows(RpcException.class, () -> { - initOrResetUrl(1, 1000); - // URL cannot be transferred automatically from the server side to the client side by using API, instead, - // it needs manually specified. - serviceURL = serviceURL.addParameter(Constants.CALLBACK_INSTANCES_LIMIT_KEY, 1 + ""); - initOrResetService(); - final AtomicInteger count = new AtomicInteger(0); - demoProxy.xxx(new IDemoCallback() { - public String yyy(String msg) { - System.out.println("Recived callback: " + msg); - count.incrementAndGet(); - return "ok"; - } - }, "other custom args", 10, 100); + initOrResetUrl(1, 1000); + // URL cannot be transferred automatically from the server side to the client side by using API, instead, + // it needs manually specified. + serviceURL = serviceURL.addParameter(Constants.CALLBACK_INSTANCES_LIMIT_KEY, 1 + ""); + initOrResetService(); + final AtomicInteger count = new AtomicInteger(0); + demoProxy.xxx(new IDemoCallback() { + public String yyy(String msg) { + System.out.println("Recived callback: " + msg); + count.incrementAndGet(); + return "ok"; + } + }, "other custom args", 10, 100); - demoProxy.xxx(new IDemoCallback() { - public String yyy(String msg) { - System.out.println("Recived callback: " + msg); - count.incrementAndGet(); - return "ok"; - } - }, "other custom args", 10, 100); - destroyService(); - }); + demoProxy.xxx(new IDemoCallback() { + public String yyy(String msg) { + System.out.println("Recived callback: " + msg); + count.incrementAndGet(); + return "ok"; + } + }, "other custom args", 10, 100); + destroyService(); } private void assertCallbackCount(int runs, int sleep, AtomicInteger count) throws InterruptedException { @@ -231,14 +227,14 @@ private void assertCallbackCount(int runs, int sleep, AtomicInteger count) throw if (last > runs) break; Thread.sleep(sleep * 2); System.out.println(count.get() + " " + last); - Assertions.assertTrue(count.get() > last); + Assert.assertTrue(count.get() > last); last = count.get(); } // has one sync callback - Assertions.assertEquals(runs + 1, count.get()); + Assert.assertEquals(runs + 1, count.get()); } - @Disabled("need start with separate process") + @Ignore("need start with separate process") @Test public void startProvider() throws Exception { exportService(); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/FutureFilterTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/FutureFilterTest.java index 7e9818d07f1..69d27eaf6ff 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/FutureFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/FutureFilterTest.java @@ -27,11 +27,10 @@ import org.apache.dubbo.rpc.protocol.dubbo.filter.FutureFilter; import org.apache.dubbo.rpc.protocol.dubbo.support.DemoService; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; +import org.junit.BeforeClass; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -43,7 +42,7 @@ public class FutureFilterTest { private static RpcInvocation invocation; private Filter eventFilter = new FutureFilter(); - @BeforeAll + @BeforeClass public static void setUp() { invocation = new RpcInvocation(); invocation.setMethodName("echo"); @@ -67,20 +66,18 @@ public void testSyncCallback() { assertEquals("High", filterResult.getValue()); } - @Test + @Test(expected = RuntimeException.class) public void testSyncCallbackHasException() throws RpcException, Throwable { - Assertions.assertThrows(RuntimeException.class, () -> { - @SuppressWarnings("unchecked") - Invoker invoker = mock(Invoker.class); - given(invoker.isAvailable()).willReturn(true); - given(invoker.getInterface()).willReturn(DemoService.class); - RpcResult result = new RpcResult(); - result.setException(new RuntimeException()); - given(invoker.invoke(invocation)).willReturn(result); - URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1&" + Constants.ON_THROW_METHOD_KEY + "=echo"); - given(invoker.getUrl()).willReturn(url); + @SuppressWarnings("unchecked") + Invoker invoker = mock(Invoker.class); + given(invoker.isAvailable()).willReturn(true); + given(invoker.getInterface()).willReturn(DemoService.class); + RpcResult result = new RpcResult(); + result.setException(new RuntimeException()); + given(invoker.invoke(invocation)).willReturn(result); + URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1&" + Constants.ON_THROW_METHOD_KEY + "=echo"); + given(invoker.getUrl()).willReturn(url); - eventFilter.invoke(invoker, invocation).recreate(); - }); + eventFilter.invoke(invoker, invocation).recreate(); } } \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ImplicitCallBackTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ImplicitCallBackTest.java index c1f24f23c62..9f8a39e6115 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ImplicitCallBackTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ImplicitCallBackTest.java @@ -28,10 +28,10 @@ import org.apache.dubbo.rpc.model.ConsumerModel; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import java.io.Serializable; import java.lang.reflect.Method; @@ -57,14 +57,14 @@ public class ImplicitCallBackTest { //================================================================================================ IDemoService demoProxy = null; - @BeforeEach + @Before public void setUp() throws SecurityException, NoSuchMethodException { onReturnMethod = Nofify.class.getMethod("onreturn", new Class[]{Person.class, Integer.class}); onThrowMethod = Nofify.class.getMethod("onthrow", new Class[]{Throwable.class, Integer.class}); onInvokeMethod = Nofify.class.getMethod("oninvoke", new Class[]{Integer.class}); } - @AfterEach + @After public void tearDown() { ProtocolUtils.closeAll(); } @@ -144,7 +144,7 @@ public void test_CloseCallback() throws Exception { initOrResetUrl(false); initOrResetService(); Person ret = demoProxy.get(1); - Assertions.assertEquals(1, ret.getId()); + Assert.assertEquals(1, ret.getId()); destroyService(); } @@ -156,7 +156,7 @@ public void test_Sync_Onreturn() throws Exception { int requestId = 2; Person ret = demoProxy.get(requestId); - Assertions.assertEquals(requestId, ret.getId()); + Assert.assertEquals(requestId, ret.getId()); for (int i = 0; i < 10; i++) { if (!notify.ret.containsKey(requestId)) { Thread.sleep(200); @@ -164,7 +164,7 @@ public void test_Sync_Onreturn() throws Exception { break; } } - Assertions.assertEquals(requestId, notify.ret.get(requestId).getId()); + Assert.assertEquals(requestId, notify.ret.get(requestId).getId()); destroyService(); } @@ -177,7 +177,7 @@ public void test_Ex_OnReturn() throws Exception { int requestId = 2; Person ret = demoProxy.get(requestId); - Assertions.assertEquals(null, ret); + Assert.assertEquals(null, ret); for (int i = 0; i < 10; i++) { if (!notify.errors.containsKey(requestId)) { Thread.sleep(200); @@ -185,7 +185,7 @@ public void test_Ex_OnReturn() throws Exception { break; } } - Assertions.assertTrue(!notify.errors.containsKey(requestId)); + Assert.assertTrue(!notify.errors.containsKey(requestId)); destroyService(); } @@ -197,7 +197,7 @@ public void test_Ex_OnInvoke() throws Exception { int requestId = 2; Person ret = demoProxy.get(requestId); - Assertions.assertEquals(null, ret); + Assert.assertEquals(null, ret); for (int i = 0; i < 10; i++) { if (!notify.inv.contains(requestId)) { Thread.sleep(200); @@ -205,7 +205,7 @@ public void test_Ex_OnInvoke() throws Exception { break; } } - Assertions.assertTrue(notify.inv.contains(requestId)); + Assert.assertTrue(notify.inv.contains(requestId)); destroyService(); } @@ -217,7 +217,7 @@ public void test_Ex_Onthrow() throws Exception { int requestId = 2; Person ret = demoProxy.get(requestId); - Assertions.assertEquals(null, ret); + Assert.assertEquals(null, ret); for (int i = 0; i < 10; i++) { if (!notify.errors.containsKey(requestId)) { Thread.sleep(200); @@ -225,8 +225,8 @@ public void test_Ex_Onthrow() throws Exception { break; } } - Assertions.assertTrue(notify.errors.containsKey(requestId)); - Assertions.assertTrue(notify.errors.get(requestId) instanceof Throwable); + Assert.assertTrue(notify.errors.containsKey(requestId)); + Assert.assertTrue(notify.errors.get(requestId) instanceof Throwable); destroyService(); } @@ -238,9 +238,9 @@ public void test_Sync_NoFuture() throws Exception { int requestId = 2; Person ret = demoProxy.get(requestId); - Assertions.assertEquals(requestId, ret.getId()); + Assert.assertEquals(requestId, ret.getId()); Future pFuture = RpcContext.getContext().getFuture(); - Assertions.assertEquals(null, pFuture); + Assert.assertEquals(null, pFuture); destroyService(); } @@ -251,10 +251,10 @@ public void test_Async_Future() throws Exception { int requestId = 2; Person ret = demoProxy.get(requestId); - Assertions.assertEquals(null, ret); + Assert.assertEquals(null, ret); Future pFuture = RpcContext.getContext().getFuture(); ret = pFuture.get(1000 * 1000, TimeUnit.MICROSECONDS); - Assertions.assertEquals(requestId, ret.getId()); + Assert.assertEquals(requestId, ret.getId()); destroyService(); } @@ -265,52 +265,48 @@ public void test_Async_Future_Multi() throws Exception { int requestId1 = 1; Person ret = demoProxy.get(requestId1); - Assertions.assertEquals(null, ret); + Assert.assertEquals(null, ret); Future p1Future = RpcContext.getContext().getFuture(); int requestId2 = 1; Person ret2 = demoProxy.get(requestId2); - Assertions.assertEquals(null, ret2); + Assert.assertEquals(null, ret2); Future p2Future = RpcContext.getContext().getFuture(); ret = p1Future.get(1000 * 1000, TimeUnit.MICROSECONDS); ret2 = p2Future.get(1000 * 1000, TimeUnit.MICROSECONDS); - Assertions.assertEquals(requestId1, ret.getId()); - Assertions.assertEquals(requestId2, ret.getId()); + Assert.assertEquals(requestId1, ret.getId()); + Assert.assertEquals(requestId2, ret.getId()); destroyService(); } - @Test + @Test(expected = RuntimeException.class) public void test_Async_Future_Ex() throws Throwable { - Assertions.assertThrows(RuntimeException.class, () -> { - try { - initOrResetUrl(true); - initOrResetExService(); - - int requestId = 2; - Person ret = demoProxy.get(requestId); - Assertions.assertEquals(null, ret); - Future pFuture = RpcContext.getContext().getFuture(); - ret = pFuture.get(1000 * 1000, TimeUnit.MICROSECONDS); - Assertions.assertEquals(requestId, ret.getId()); - } catch (ExecutionException e) { - throw e.getCause(); - } finally { - destroyService(); - } - }); - } - - @Test - public void test_Normal_Ex() throws Exception { - Assertions.assertThrows(RuntimeException.class, () -> { - initOrResetUrl(false); + try { + initOrResetUrl(true); initOrResetExService(); int requestId = 2; Person ret = demoProxy.get(requestId); - Assertions.assertEquals(requestId, ret.getId()); - }); + Assert.assertEquals(null, ret); + Future pFuture = RpcContext.getContext().getFuture(); + ret = pFuture.get(1000 * 1000, TimeUnit.MICROSECONDS); + Assert.assertEquals(requestId, ret.getId()); + } catch (ExecutionException e) { + throw e.getCause(); + } finally { + destroyService(); + } + } + + @Test(expected = RuntimeException.class) + public void test_Normal_Ex() throws Exception { + initOrResetUrl(false); + initOrResetExService(); + + int requestId = 2; + Person ret = demoProxy.get(requestId); + Assert.assertEquals(requestId, ret.getId()); } interface Nofify { diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/MultiThreadTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/MultiThreadTest.java index ca3aebc3e12..04c3645824a 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/MultiThreadTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/MultiThreadTest.java @@ -24,9 +24,9 @@ import org.apache.dubbo.rpc.protocol.dubbo.support.DemoService; import org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Test; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -38,7 +38,7 @@ public class MultiThreadTest { private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); - @AfterEach + @After public void after() { ProtocolUtils.closeAll(); } @@ -49,12 +49,12 @@ public void testDubboMultiThreadInvoke() throws Exception { final AtomicInteger counter = new AtomicInteger(); final DemoService service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:20259/TestService"))); - Assertions.assertEquals(service.getSize(new String[]{"123", "456", "789"}), 3); + Assert.assertEquals(service.getSize(new String[]{"123", "456", "789"}), 3); final StringBuffer sb = new StringBuffer(); for (int i = 0; i < 1024 * 64 + 32; i++) sb.append('A'); - Assertions.assertEquals(sb.toString(), service.echo(sb.toString())); + Assert.assertEquals(sb.toString(), service.echo(sb.toString())); ExecutorService exec = Executors.newFixedThreadPool(10); for (int i = 0; i < 10; i++) { @@ -63,7 +63,7 @@ public void testDubboMultiThreadInvoke() throws Exception { public void run() { for (int i = 0; i < 30; i++) { System.out.println(fi + ":" + counter.getAndIncrement()); - Assertions.assertEquals(service.echo(sb.toString()), sb.toString()); + Assert.assertEquals(service.echo(sb.toString()), sb.toString()); } } }); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClientTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClientTest.java index b77e5e18b3c..fb338769b65 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClientTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClientTest.java @@ -27,19 +27,14 @@ import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.ProxyFactory; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; -import java.util.Objects; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import java.lang.reflect.Field; public class ReferenceCountExchangeClientTest { @@ -55,11 +50,11 @@ public class ReferenceCountExchangeClientTest { ExchangeClient helloClient; String errorMsg = "safe guard client , should not be called ,must have a bug"; - @BeforeAll + @BeforeClass public static void setUpBeforeClass() throws Exception { } - @AfterAll + @AfterClass public static void tearDownAfterClass() { ProtocolUtils.closeAll(); } @@ -76,7 +71,7 @@ public static Exporter export(T instance, Class type, URL url) { return protocol.export(proxy.getInvoker(instance, type, url)); } - @BeforeEach + @Before public void setUp() throws Exception { } @@ -85,9 +80,9 @@ public void setUp() throws Exception { */ @Test public void test_share_connect() { - init(0, 1); - Assertions.assertEquals(demoClient.getLocalAddress(), helloClient.getLocalAddress()); - Assertions.assertEquals(demoClient, helloClient); + init(0); + Assert.assertEquals(demoClient.getLocalAddress(), helloClient.getLocalAddress()); + Assert.assertEquals(demoClient, helloClient); destoy(); } @@ -96,34 +91,9 @@ public void test_share_connect() { */ @Test public void test_not_share_connect() { - init(1, 1); - Assertions.assertNotSame(demoClient.getLocalAddress(), helloClient.getLocalAddress()); - Assertions.assertNotSame(demoClient, helloClient); - destoy(); - } - - /** - * test using multiple shared connections - */ - @Test - public void test_mult_share_connect() { - // here a three shared connection is established between a consumer process and a provider process. - final int shareConnectionNum = 3; - - init(0, shareConnectionNum); - - List helloReferenceClientList = getReferenceClientList(helloServiceInvoker); - Assertions.assertEquals(shareConnectionNum, helloReferenceClientList.size()); - - List demoReferenceClientList = getReferenceClientList(demoServiceInvoker); - Assertions.assertEquals(shareConnectionNum, demoReferenceClientList.size()); - - // because helloServiceInvoker and demoServiceInvoker use share connect, so client list must be equal - Assertions.assertTrue(Objects.equals(helloReferenceClientList, demoReferenceClientList)); - - Assertions.assertEquals(demoClient.getLocalAddress(), helloClient.getLocalAddress()); - Assertions.assertEquals(demoClient, helloClient); - + init(1); + Assert.assertNotSame(demoClient.getLocalAddress(), helloClient.getLocalAddress()); + Assert.assertNotSame(demoClient, helloClient); destoy(); } @@ -132,13 +102,13 @@ public void test_mult_share_connect() { */ @Test public void test_multi_destory() { - init(0, 1); + init(0); DubboAppender.doStart(); DubboAppender.clear(); demoServiceInvoker.destroy(); demoServiceInvoker.destroy(); - Assertions.assertEquals("hello", helloService.hello()); - Assertions.assertEquals(0, LogUtil.findMessage(errorMsg), "should not warning message"); + Assert.assertEquals("hello", helloService.hello()); + Assert.assertEquals("should not warning message", 0, LogUtil.findMessage(errorMsg)); LogUtil.checkNoError(); DubboAppender.doStop(); destoy(); @@ -149,78 +119,61 @@ public void test_multi_destory() { */ @Test public void test_counter_error() { - init(0, 1); + init(0); DubboAppender.doStart(); DubboAppender.clear(); - // because the two interfaces are initialized, the ReferenceCountExchangeClient reference counter is 2 ReferenceCountExchangeClient client = getReferenceClient(helloServiceInvoker); - // close once, counter counts down from 2 to 1, no warning occurs client.close(); - Assertions.assertEquals("hello", helloService.hello()); - Assertions.assertEquals(0, LogUtil.findMessage(errorMsg), "should not warning message"); - - // generally a client can only be closed once, here it is closed twice, counter is incorrect + Assert.assertEquals("hello", helloService.hello()); + Assert.assertEquals("should not warning message", 0, LogUtil.findMessage(errorMsg)); + // counter is incorrect, invocation still succeeds client.close(); // wait close done. try { Thread.sleep(1000); } catch (InterruptedException e) { - Assertions.fail(); + Assert.fail(); } - // due to the effect of LazyConnectExchangeClient, the client will be "revived" whenever there is a call. - Assertions.assertEquals("hello", helloService.hello()); - Assertions.assertEquals(1, LogUtil.findMessage(errorMsg), "should warning message"); + Assert.assertEquals("hello", helloService.hello()); + Assert.assertEquals("should warning message", 1, LogUtil.findMessage(errorMsg)); // output one error every 5000 invocations. - Assertions.assertEquals("hello", helloService.hello()); - Assertions.assertEquals(1, LogUtil.findMessage(errorMsg), "should warning message"); + Assert.assertEquals("hello", helloService.hello()); + Assert.assertEquals("should warning message", 1, LogUtil.findMessage(errorMsg)); DubboAppender.doStop(); // status switch to available once invoke again - Assertions.assertEquals(true, helloServiceInvoker.isAvailable(), "client status available"); - - /** - * This is the third time to close the same client. Under normal circumstances, - * a client value should be closed once (that is, the shutdown operation is irreversible). - * After closing, the value of the reference counter of the client has become -1. - * - * But this is a bit special, because after the client is closed twice, there are several calls to helloService, - * that is, the client inside the ReferenceCountExchangeClient is actually active, so the third shutdown here is still effective, - * let the resurrection After the client is really closed. - */ - client.close(); + Assert.assertEquals("client status available", true, helloServiceInvoker.isAvailable()); + client.close(); // client has been replaced with lazy client. lazy client is fetched from referenceclientmap, and since it's // been invoked once, it's close status is false - Assertions.assertEquals(false, client.isClosed(), "client status close"); - Assertions.assertEquals(false, helloServiceInvoker.isAvailable(), "client status close"); + Assert.assertEquals("client status close", false, client.isClosed()); + Assert.assertEquals("client status close", false, helloServiceInvoker.isAvailable()); destoy(); } @SuppressWarnings("unchecked") - private void init(int connections, int shareConnections) { - Assertions.assertTrue(connections >= 0); - Assertions.assertTrue(shareConnections >= 1); - + private void init(int connections) { int port = NetUtils.getAvailablePort(); - URL demoUrl = URL.valueOf("dubbo://127.0.0.1:" + port + "/demo?" + Constants.CONNECTIONS_KEY + "=" + connections + "&" + Constants.SHARE_CONNECTIONS_KEY + "=" + shareConnections); - URL helloUrl = URL.valueOf("dubbo://127.0.0.1:" + port + "/hello?" + Constants.CONNECTIONS_KEY + "=" + connections + "&" + Constants.SHARE_CONNECTIONS_KEY + "=" + shareConnections); + URL demoUrl = URL.valueOf("dubbo://127.0.0.1:" + port + "/demo?" + Constants.CONNECTIONS_KEY + "=" + connections); + URL helloUrl = URL.valueOf("dubbo://127.0.0.1:" + port + "/hello?" + Constants.CONNECTIONS_KEY + "=" + connections); demoExporter = export(new DemoServiceImpl(), IDemoService.class, demoUrl); helloExporter = export(new HelloServiceImpl(), IHelloService.class, helloUrl); demoServiceInvoker = (Invoker) referInvoker(IDemoService.class, demoUrl); demoService = proxy.getProxy(demoServiceInvoker); - Assertions.assertEquals("demo", demoService.demo()); + Assert.assertEquals("demo", demoService.demo()); helloServiceInvoker = (Invoker) referInvoker(IHelloService.class, helloUrl); helloService = proxy.getProxy(helloServiceInvoker); - Assertions.assertEquals("hello", helloService.hello()); + Assert.assertEquals("hello", helloService.hello()); demoClient = getClient(demoServiceInvoker); helloClient = getClient(helloServiceInvoker); @@ -244,53 +197,28 @@ private ExchangeClient getClient(Invoker invoker) { return (ExchangeClient) clientField.get(client); } catch (Exception e) { e.printStackTrace(); - Assertions.fail(e.getMessage()); + Assert.fail(e.getMessage()); throw new RuntimeException(e); } } } private ReferenceCountExchangeClient getReferenceClient(Invoker invoker) { - return getReferenceClientList(invoker).get(0); - } - - private List getReferenceClientList(Invoker invoker) { - List invokerClientList = getInvokerClientList(invoker); - - List referenceCountExchangeClientList = new ArrayList<>(invokerClientList.size()); - for (ExchangeClient exchangeClient : invokerClientList) { - Assertions.assertTrue(exchangeClient instanceof ReferenceCountExchangeClient); - referenceCountExchangeClientList.add((ReferenceCountExchangeClient) exchangeClient); - } - - return referenceCountExchangeClientList; + return (ReferenceCountExchangeClient) getInvokerClient(invoker); } private ExchangeClient getInvokerClient(Invoker invoker) { - return getInvokerClientList(invoker).get(0); - } - - private List getInvokerClientList(Invoker invoker) { @SuppressWarnings("rawtypes") DubboInvoker dInvoker = (DubboInvoker) invoker; try { Field clientField = DubboInvoker.class.getDeclaredField("clients"); clientField.setAccessible(true); ExchangeClient[] clients = (ExchangeClient[]) clientField.get(dInvoker); - - List clientList = new ArrayList(clients.length); - for (ExchangeClient client : clients) { - clientList.add(client); - } - - // sorting makes it easy to compare between lists - Collections.sort(clientList, Comparator.comparing(c -> Integer.valueOf(Objects.hashCode(c)))); - - return clientList; + return clients[0]; } catch (Exception e) { e.printStackTrace(); - Assertions.fail(e.getMessage()); + Assert.fail(e.getMessage()); throw new RuntimeException(e); } } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/RpcFilterTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/RpcFilterTest.java index 95652470899..43297a9d6ba 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/RpcFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/RpcFilterTest.java @@ -25,15 +25,15 @@ import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; import org.apache.dubbo.rpc.service.EchoService; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Test; public class RpcFilterTest { private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); - @AfterEach + @After public void after() { ProtocolUtils.closeAll(); } @@ -44,12 +44,12 @@ public void testRpcFilter() throws Exception { URL url = URL.valueOf("dubbo://127.0.0.1:9010/org.apache.dubbo.rpc.DemoService?service.filter=echo"); protocol.export(proxy.getInvoker(service, DemoService.class, url)); service = proxy.getProxy(protocol.refer(DemoService.class, url)); - Assertions.assertEquals("123", service.echo("123")); + Assert.assertEquals("123", service.echo("123")); // cast to EchoService EchoService echo = proxy.getProxy(protocol.refer(EchoService.class, url)); - Assertions.assertEquals(echo.$echo("test"), "test"); - Assertions.assertEquals(echo.$echo("abcdefg"), "abcdefg"); - Assertions.assertEquals(echo.$echo(1234), 1234); + Assert.assertEquals(echo.$echo("test"), "test"); + Assert.assertEquals(echo.$echo("abcdefg"), "abcdefg"); + Assert.assertEquals(echo.$echo(1234), 1234); } } \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/DubboTelnetDecodeTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/DubboTelnetDecodeTest.java deleted file mode 100644 index e54d2e6508a..00000000000 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/DubboTelnetDecodeTest.java +++ /dev/null @@ -1,477 +0,0 @@ -/* - * 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 org.apache.dubbo.rpc.protocol.dubbo.decode; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.extension.ExtensionLoader; -import org.apache.dubbo.remoting.Codec2; -import org.apache.dubbo.remoting.buffer.ChannelBuffer; -import org.apache.dubbo.remoting.exchange.ExchangeChannel; -import org.apache.dubbo.remoting.exchange.Request; -import org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter; -import org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler; -import org.apache.dubbo.remoting.transport.DecodeHandler; -import org.apache.dubbo.remoting.transport.MultiMessageHandler; -import org.apache.dubbo.remoting.transport.netty4.NettyBackedChannelBuffer; -import org.apache.dubbo.remoting.transport.netty4.NettyCodecAdapter; -import org.apache.dubbo.rpc.RpcInvocation; -import org.apache.dubbo.rpc.protocol.dubbo.DecodeableRpcInvocation; -import org.apache.dubbo.rpc.protocol.dubbo.DubboCodec; -import org.apache.dubbo.rpc.protocol.dubbo.support.DemoService; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; -import io.netty.channel.embedded.EmbeddedChannel; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.io.IOException; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; - -/** - * These junit tests aim to test unpack and stick pack of dubbo and telnet - */ -public class DubboTelnetDecodeTest { - private static AtomicInteger dubbo = new AtomicInteger(0); - - private static AtomicInteger telnet = new AtomicInteger(0); - - private static AtomicInteger telnetDubbo = new AtomicInteger(0); - - private static AtomicInteger dubboDubbo = new AtomicInteger(0); - - private static AtomicInteger dubboTelnet = new AtomicInteger(0); - - private static AtomicInteger telnetTelnet = new AtomicInteger(0); - - /** - * just dubbo request - * - * @throws InterruptedException - */ - @Test - public void testDubboDecode() throws InterruptedException, IOException { - ByteBuf dubboByteBuf = createDubboByteBuf(); - - EmbeddedChannel ch = null; - try { - Codec2 codec = ExtensionLoader.getExtensionLoader(Codec2.class).getExtension("dubbo"); - URL url = new URL("dubbo", "localhost", 22226); - NettyCodecAdapter adapter = new NettyCodecAdapter(codec, url, new MockChannelHandler()); - - MockHandler mockHandler = new MockHandler(null, - new MultiMessageHandler( - new DecodeHandler( - new HeaderExchangeHandler(new ExchangeHandlerAdapter() { - @Override - public CompletableFuture reply(ExchangeChannel channel, Object msg) { - if (checkDubboDecoded(msg)) { - dubbo.incrementAndGet(); - } - return null; - } - })))); - - ch = new LocalEmbeddedChannel(); - ch.pipeline() - .addLast("decoder", adapter.getDecoder()) - .addLast("handler", mockHandler); - - ch.writeInbound(dubboByteBuf); - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (ch != null) { - ch.close().await(200, TimeUnit.MILLISECONDS); - } - } - - TimeUnit.MILLISECONDS.sleep(100); - - Assertions.assertEquals(1, dubbo.get()); - } - - /** - * just telnet request - * - * @throws InterruptedException - */ - @Test - public void testTelnetDecode() throws InterruptedException { - ByteBuf telnetByteBuf = Unpooled.wrappedBuffer("ls\r\n".getBytes()); - - EmbeddedChannel ch = null; - try { - Codec2 codec = ExtensionLoader.getExtensionLoader(Codec2.class).getExtension("dubbo"); - URL url = new URL("dubbo", "localhost", 22226); - NettyCodecAdapter adapter = new NettyCodecAdapter(codec, url, new MockChannelHandler()); - - MockHandler mockHandler = new MockHandler((msg) -> { - if (checkTelnetDecoded(msg)) { - telnet.incrementAndGet(); - } - }, - new MultiMessageHandler( - new DecodeHandler( - new HeaderExchangeHandler(new ExchangeHandlerAdapter() { - @Override - public CompletableFuture reply(ExchangeChannel channel, Object msg) { - return null; - } - })))); - - ch = new LocalEmbeddedChannel(); - ch.pipeline() - .addLast("decoder", adapter.getDecoder()) - .addLast("handler", mockHandler); - - ch.writeInbound(telnetByteBuf); - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (ch != null) { - ch.close().await(200, TimeUnit.MILLISECONDS); - } - } - - TimeUnit.MILLISECONDS.sleep(100); - - Assertions.assertEquals(1, telnet.get()); - } - - /** - * telnet and dubbo request - * - *

    - * First ByteBuf: - * +--------------------------------------------------+ - * | telnet(incomplete) | - * +--------------------------------------------------+ - *

    - * - * Second ByteBuf: - * +--------------------------++----------------------+ - * | telnet(the remaining) || dubbo(complete) | - * +--------------------------++----------------------+ - * || - * Magic Code - * - * @throws InterruptedException - */ - @Test - public void testTelnetDubboDecoded() throws InterruptedException, IOException { - ByteBuf dubboByteBuf = createDubboByteBuf(); - - ByteBuf telnetByteBuf = Unpooled.wrappedBuffer("ls\r".getBytes()); - EmbeddedChannel ch = null; - try { - Codec2 codec = ExtensionLoader.getExtensionLoader(Codec2.class).getExtension("dubbo"); - URL url = new URL("dubbo", "localhost", 22226); - NettyCodecAdapter adapter = new NettyCodecAdapter(codec, url, new MockChannelHandler()); - - MockHandler mockHandler = new MockHandler((msg) -> { - if (checkTelnetDecoded(msg)) { - telnetDubbo.incrementAndGet(); - } - }, - new MultiMessageHandler( - new DecodeHandler( - new HeaderExchangeHandler(new ExchangeHandlerAdapter() { - @Override - public CompletableFuture reply(ExchangeChannel channel, Object msg) { - if (checkDubboDecoded(msg)) { - telnetDubbo.incrementAndGet(); - } - return null; - } - })))); - - ch = new LocalEmbeddedChannel(); - ch.pipeline() - .addLast("decoder", adapter.getDecoder()) - .addLast("handler", mockHandler); - - ch.writeInbound(telnetByteBuf); - ch.writeInbound(Unpooled.wrappedBuffer(Unpooled.wrappedBuffer("\n".getBytes()), dubboByteBuf)); - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (ch != null) { - ch.close().await(200, TimeUnit.MILLISECONDS); - } - } - - TimeUnit.MILLISECONDS.sleep(100); - - Assertions.assertEquals(2, telnetDubbo.get()); - } - - /** - * NOTE: This test case actually will fail, but the probability of this case is very small, - * and users should use telnet in new QOS port(default port is 22222) since dubbo 2.5.8, - * so we could ignore this problem. - * - *

    - * telnet and telnet request - * - *

    - * First ByteBuf (firstByteBuf): - * +--------------------------------------------------+ - * | telnet(incomplete) | - * +--------------------------------------------------+ - *

    - * - * Second ByteBuf (secondByteBuf): - * +--------------------------------------------------+ - * | telnet(the remaining) | telnet(complete) | - * +--------------------------------------------------+ - * - * @throws InterruptedException - */ - // @Test - public void testTelnetTelnetDecoded() throws InterruptedException { - ByteBuf firstByteBuf = Unpooled.wrappedBuffer("ls\r".getBytes()); - ByteBuf secondByteBuf = Unpooled.wrappedBuffer("\nls\r\n".getBytes()); - - EmbeddedChannel ch = null; - try { - Codec2 codec = ExtensionLoader.getExtensionLoader(Codec2.class).getExtension("dubbo"); - URL url = new URL("dubbo", "localhost", 22226); - NettyCodecAdapter adapter = new NettyCodecAdapter(codec, url, new MockChannelHandler()); - - MockHandler mockHandler = new MockHandler((msg) -> { - if (checkTelnetDecoded(msg)) { - telnetTelnet.incrementAndGet(); - } - }, - new MultiMessageHandler( - new DecodeHandler( - new HeaderExchangeHandler(new ExchangeHandlerAdapter() { - @Override - public CompletableFuture reply(ExchangeChannel channel, Object msg) { - return null; - } - })))); - - ch = new LocalEmbeddedChannel(); - ch.pipeline() - .addLast("decoder", adapter.getDecoder()) - .addLast("handler", mockHandler); - - ch.writeInbound(firstByteBuf); - ch.writeInbound(secondByteBuf); - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (ch != null) { - ch.close().await(200, TimeUnit.MILLISECONDS); - } - } - - TimeUnit.MILLISECONDS.sleep(100); - - Assertions.assertEquals(2, telnetTelnet.get()); - } - - /** - * dubbo and dubbo request - * - *

    - * First ByteBuf (firstDubboByteBuf): - * ++-------------------------------------------------+ - * || dubbo(incomplete) | - * ++-------------------------------------------------+ - * || - * Magic Code - *

    - * - *

    - * Second ByteBuf (secondDubboByteBuf): - * +-------------------------++-----------------------+ - * | dubbo(the remaining) || dubbo(complete) | - * +-------------------------++-----------------------+ - * || - * Magic Code - * - * @throws InterruptedException - */ - @Test - public void testDubboDubboDecoded() throws InterruptedException, IOException { - ByteBuf dubboByteBuf = createDubboByteBuf(); - - ByteBuf firstDubboByteBuf = dubboByteBuf.copy(0, 50); - ByteBuf secondLeftDubboByteBuf = dubboByteBuf.copy(50, dubboByteBuf.readableBytes() - 50); - ByteBuf secondDubboByteBuf = Unpooled.wrappedBuffer(secondLeftDubboByteBuf, dubboByteBuf); - - - EmbeddedChannel ch = null; - try { - Codec2 codec = ExtensionLoader.getExtensionLoader(Codec2.class).getExtension("dubbo"); - URL url = new URL("dubbo", "localhost", 22226); - NettyCodecAdapter adapter = new NettyCodecAdapter(codec, url, new MockChannelHandler()); - - MockHandler mockHandler = new MockHandler(null, - new MultiMessageHandler( - new DecodeHandler( - new HeaderExchangeHandler(new ExchangeHandlerAdapter() { - @Override - public CompletableFuture reply(ExchangeChannel channel, Object msg) { - if (checkDubboDecoded(msg)) { - dubboDubbo.incrementAndGet(); - } - return null; - } - })))); - - ch = new LocalEmbeddedChannel(); - ch.pipeline() - .addLast("decoder", adapter.getDecoder()) - .addLast("handler", mockHandler); - - ch.writeInbound(firstDubboByteBuf); - ch.writeInbound(secondDubboByteBuf); - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (ch != null) { - ch.close().await(200, TimeUnit.MILLISECONDS); - } - } - - TimeUnit.MILLISECONDS.sleep(100); - - Assertions.assertEquals(2, dubboDubbo.get()); - } - - /** - * dubbo and telnet request - * - *

    - * First ByteBuf: - * ++-------------------------------------------------+ - * || dubbo(incomplete) | - * ++-------------------------------------------------+ - * || - * Magic Code - * - *

    - * Second ByteBuf: - * +--------------------------------------------------+ - * | dubbo(the remaining) | telnet(complete) | - * +--------------------------------------------------+ - * - * @throws InterruptedException - */ - @Test - public void testDubboTelnetDecoded() throws InterruptedException, IOException { - ByteBuf dubboByteBuf = createDubboByteBuf(); - ByteBuf firstDubboByteBuf = dubboByteBuf.copy(0, 50); - ByteBuf secondLeftDubboByteBuf = dubboByteBuf.copy(50, dubboByteBuf.readableBytes()); - - ByteBuf telnetByteBuf = Unpooled.wrappedBuffer("\r\n".getBytes()); - ByteBuf secondByteBuf = Unpooled.wrappedBuffer(secondLeftDubboByteBuf, telnetByteBuf); - - EmbeddedChannel ch = null; - try { - Codec2 codec = ExtensionLoader.getExtensionLoader(Codec2.class).getExtension("dubbo"); - URL url = new URL("dubbo", "localhost", 22226); - NettyCodecAdapter adapter = new NettyCodecAdapter(codec, url, new MockChannelHandler()); - - MockHandler mockHandler = new MockHandler((msg) -> { - if (checkTelnetDecoded(msg)) { - dubboTelnet.incrementAndGet(); - } - }, - new MultiMessageHandler( - new DecodeHandler( - new HeaderExchangeHandler(new ExchangeHandlerAdapter() { - @Override - public CompletableFuture reply(ExchangeChannel channel, Object msg) { - if (checkDubboDecoded(msg)) { - dubboTelnet.incrementAndGet(); - } - return null; - } - })))); - - ch = new LocalEmbeddedChannel(); - ch.pipeline() - .addLast("decoder", adapter.getDecoder()) - .addLast("handler", mockHandler); - - ch.writeInbound(firstDubboByteBuf); - ch.writeInbound(secondByteBuf); - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (ch != null) { - ch.close().await(200, TimeUnit.MILLISECONDS); - } - } - - TimeUnit.MILLISECONDS.sleep(100); - - Assertions.assertEquals(2, dubboTelnet.get()); - } - - private ByteBuf createDubboByteBuf() throws IOException { - Request request = new Request(); - RpcInvocation rpcInvocation = new RpcInvocation(); - rpcInvocation.setMethodName("sayHello"); - rpcInvocation.setParameterTypes(new Class[]{String.class}); - rpcInvocation.setArguments(new String[]{"dubbo"}); - rpcInvocation.setAttachment("path", DemoService.class.getName()); - rpcInvocation.setAttachment("interface", DemoService.class.getName()); - rpcInvocation.setAttachment("version", "0.0.0"); - - request.setData(rpcInvocation); - request.setVersion("2.0.2"); - - ByteBuf dubboByteBuf = Unpooled.buffer(); - ChannelBuffer buffer = new NettyBackedChannelBuffer(dubboByteBuf); - DubboCodec dubboCodec = new DubboCodec(); - dubboCodec.encode(new MockChannel(), buffer, request); - - return dubboByteBuf; - } - - private static boolean checkTelnetDecoded(Object msg) { - if (msg != null && msg instanceof String && !msg.toString().contains("Unsupported command:")) { - return true; - } - return false; - } - - private static boolean checkDubboDecoded(Object msg) { - if (msg instanceof DecodeableRpcInvocation) { - DecodeableRpcInvocation invocation = (DecodeableRpcInvocation) msg; - if ("sayHello".equals(invocation.getMethodName()) - && invocation.getParameterTypes().length == 1 - && String.class.equals(invocation.getParameterTypes()[0]) - && invocation.getArguments().length == 1 - && "dubbo".equals(invocation.getArguments()[0]) - && DemoService.class.getName().equals(invocation.getAttachment("path")) - && DemoService.class.getName().equals(invocation.getAttachment("interface")) - && "0.0.0".equals(invocation.getAttachment("version"))) { - return true; - } - } - return false; - } -} \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/LocalEmbeddedChannel.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/LocalEmbeddedChannel.java deleted file mode 100644 index adc590d7516..00000000000 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/LocalEmbeddedChannel.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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 org.apache.dubbo.rpc.protocol.dubbo.decode; - -import org.apache.dubbo.common.utils.NetUtils; - -import io.netty.channel.embedded.EmbeddedChannel; - -import java.net.InetSocketAddress; -import java.net.SocketAddress; - -public class LocalEmbeddedChannel extends EmbeddedChannel { - public SocketAddress localAddress() { - return new InetSocketAddress(20883); - } - - @Override - protected SocketAddress remoteAddress0() { - return new InetSocketAddress(NetUtils.getAvailablePort()); - } -} diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/MockChannel.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/MockChannel.java deleted file mode 100644 index 80a2dc5b016..00000000000 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/MockChannel.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * 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 org.apache.dubbo.rpc.protocol.dubbo.decode; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.utils.NetUtils; -import org.apache.dubbo.remoting.Channel; -import org.apache.dubbo.remoting.ChannelHandler; -import org.apache.dubbo.remoting.RemotingException; - -import java.net.InetSocketAddress; -import java.util.function.Consumer; - -public class MockChannel implements Channel { - private Consumer consumer; - - public MockChannel() { - - } - - public MockChannel(Consumer consumer) { - this.consumer = consumer; - } - - @Override - public InetSocketAddress getRemoteAddress() { - return new InetSocketAddress(NetUtils.getAvailablePort()); - } - - @Override - public boolean isConnected() { - return false; - } - - @Override - public boolean hasAttribute(String key) { - return false; - } - - @Override - public Object getAttribute(String key) { - return null; - } - - @Override - public void setAttribute(String key, Object value) { - - } - - @Override - public void removeAttribute(String key) { - - } - - @Override - public URL getUrl() { - return new URL("dubbo", "localhost", 20880); - } - - @Override - public ChannelHandler getChannelHandler() { - return null; - } - - @Override - public InetSocketAddress getLocalAddress() { - return new InetSocketAddress(20883); - } - - @Override - public void send(Object message) throws RemotingException { - if (consumer != null) { - consumer.accept(message); - } - } - - @Override - public void send(Object message, boolean sent) throws RemotingException { - - } - - @Override - public void close() { - - } - - @Override - public void close(int timeout) { - - } - - @Override - public void startClose() { - - } - - @Override - public boolean isClosed() { - return false; - } -} diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/MockChannelHandler.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/MockChannelHandler.java deleted file mode 100644 index 492929706dd..00000000000 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/MockChannelHandler.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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 org.apache.dubbo.rpc.protocol.dubbo.decode; - -import org.apache.dubbo.common.utils.ConcurrentHashSet; -import org.apache.dubbo.remoting.Channel; -import org.apache.dubbo.remoting.ChannelHandler; -import org.apache.dubbo.remoting.RemotingException; - -import java.util.Collections; -import java.util.Set; - -public class MockChannelHandler implements ChannelHandler { - // ConcurrentMap channels = new ConcurrentHashMap(); - ConcurrentHashSet channels = new ConcurrentHashSet(); - - @Override - public void connected(Channel channel) throws RemotingException { - channels.add(channel); - } - - @Override - public void disconnected(Channel channel) throws RemotingException { - channels.remove(channel); - } - - @Override - public void sent(Channel channel, Object message) throws RemotingException { - channel.send(message); - } - - @Override - public void received(Channel channel, Object message) throws RemotingException { - //echo - channel.send(message); - } - - @Override - public void caught(Channel channel, Throwable exception) throws RemotingException { - throw new RemotingException(channel, exception); - - } - - public Set getChannels() { - return Collections.unmodifiableSet(channels); - } -} diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/MockHandler.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/MockHandler.java deleted file mode 100644 index f4f432a733e..00000000000 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/decode/MockHandler.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 org.apache.dubbo.rpc.protocol.dubbo.decode; - -import org.apache.dubbo.remoting.ChannelHandler; - -import io.netty.channel.ChannelDuplexHandler; -import io.netty.channel.ChannelHandlerContext; - -import java.util.function.Consumer; - -public class MockHandler extends ChannelDuplexHandler { - private final Consumer consumer; - - private final ChannelHandler handler; - - public MockHandler(Consumer consumer, ChannelHandler handler) { - this.consumer = consumer; - this.handler = handler; - } - - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - this.handler.received(new MockChannel(consumer), msg); - } -} diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoService.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoService.java index a1323b852b7..fe777e37de2 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoService.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoService.java @@ -47,7 +47,7 @@ public interface DemoService { Type enumlength(Type... types); - Type getType(Type type); + Type getType(Type type); String get(CustomArgument arg1); @@ -63,5 +63,4 @@ public interface DemoService { int getPerson(Person person1, Person perso2); - String getPerson(Man man); } \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoServiceImpl.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoServiceImpl.java index a5af9be110d..c1b03f64e25 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoServiceImpl.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoServiceImpl.java @@ -71,7 +71,7 @@ public Type enumlength(Type... types) { return Type.Lower; return types[0]; } - + public Type getType(Type type) { return type; } @@ -109,16 +109,12 @@ public long add(int a, long b) { @Override public int getPerson(Person person) { - return person.getAge(); + return 1; } @Override public int getPerson(Person person1, Person perso2) { - return person1.getAge() + perso2.getAge(); + return 2; } - @Override - public String getPerson(Man man) { - return man.getName(); - } } \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/EnumBak.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/EnumBak.java index 6ef0255fb5e..a3d2972ac48 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/EnumBak.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/EnumBak.java @@ -25,9 +25,9 @@ import org.apache.dubbo.rpc.ProxyFactory; import org.apache.dubbo.rpc.service.GenericService; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Test; import java.util.HashMap; import java.util.Map; @@ -52,13 +52,13 @@ public void testNormal() { Invoker reference = protocol.refer(DemoService.class, consumerurl); DemoService demoProxy = (DemoService) proxy.getProxy(reference); // System.out.println(demoProxy.getThreadName()); - Assertions.assertEquals((byte) -128, demoProxy.getbyte((byte) -128)); + Assert.assertEquals((byte) -128, demoProxy.getbyte((byte) -128)); // invoker.destroy(); reference.destroy(); } - @Disabled + @Ignore @Test public void testExportService() throws InterruptedException { int port = NetUtils.getAvailablePort(); @@ -95,14 +95,14 @@ public void testNormalEnum() { DemoService demoProxy = (DemoService) proxy.getProxy(reference); Type type = demoProxy.enumlength(Type.High); System.out.println(type); - Assertions.assertEquals(Type.High, type); + Assert.assertEquals(Type.High, type); invoker.destroy(); reference.destroy(); } // verify compatibility when 2.0.5 invokes 2.0.3 - @Disabled + @Ignore @Test public void testEnumCompat() { int port = 20880; @@ -112,12 +112,12 @@ public void testEnumCompat() { DemoService demoProxy = (DemoService) proxy.getProxy(reference); Type type = demoProxy.enumlength(Type.High); System.out.println(type); - Assertions.assertEquals(Type.High, type); + Assert.assertEquals(Type.High, type); reference.destroy(); } // verify compatibility when 2.0.5 invokes 2.0.3 - @Disabled + @Ignore @Test public void testGenricEnumCompat() { int port = 20880; @@ -132,7 +132,7 @@ public void testGenricEnumCompat() { } // verify compatibility when 2.0.5 invokes 2.0.3, enum in custom parameter - @Disabled + @Ignore @Test public void testGenricCustomArg() { @@ -151,7 +151,7 @@ public void testGenricCustomArg() { reference.destroy(); } - @Disabled + @Ignore @Test public void testGenericExport() throws InterruptedException { int port = NetUtils.getAvailablePort(); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ChangeTelnetHandlerTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ChangeTelnetHandlerTest.java index 6d88139b7ca..428ff18edb5 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ChangeTelnetHandlerTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ChangeTelnetHandlerTest.java @@ -25,12 +25,12 @@ import org.apache.dubbo.rpc.protocol.dubbo.support.DemoService; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.reset; @@ -44,13 +44,13 @@ public class ChangeTelnetHandlerTest { private Channel mockChannel; private Invoker mockInvoker; - @AfterAll + @AfterClass public static void tearDown() { } @SuppressWarnings("unchecked") - @BeforeEach + @Before public void setUp() { mockChannel = mock(Channel.class); mockInvoker = mock(Invoker.class); @@ -71,7 +71,7 @@ private void givenLastCall() { } - @AfterEach + @After public void after() { ProtocolUtils.closeAll(); reset(mockChannel, mockInvoker); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/CurrentTelnetHandlerTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/CurrentTelnetHandlerTest.java index 81e971bea31..eea4884e5d7 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/CurrentTelnetHandlerTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/CurrentTelnetHandlerTest.java @@ -20,10 +20,10 @@ import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.telnet.TelnetHandler; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java index c8029c710b4..b8e2b2498ee 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java @@ -26,13 +26,13 @@ import org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertTrue; +import static org.junit.Assert.fail; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -44,12 +44,12 @@ public class InvokerTelnetHandlerTest { private static TelnetHandler invoke = new InvokeTelnetHandler(); private Channel mockChannel; - @BeforeEach + @Before public void setup() { ApplicationModel.reset(); } - @AfterEach + @After public void after() { ProtocolUtils.closeAll(); } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandlerTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandlerTest.java index 1b2e33daeb0..b94f3ae8036 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandlerTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandlerTest.java @@ -26,15 +26,15 @@ import org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; import java.lang.reflect.Method; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertTrue; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -46,17 +46,17 @@ public class ListTelnetHandlerTest { private static TelnetHandler list = new ListTelnetHandler(); private Channel mockChannel; - @BeforeAll + @BeforeClass public static void setUp() { ProtocolUtils.closeAll(); } - @BeforeEach + @Before public void init() { ApplicationModel.reset(); } - @AfterEach + @After public void after() { ProtocolUtils.closeAll(); } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/LogTelnetHandlerTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/LogTelnetHandlerTest.java index 5299e9cc9b8..092f458d8b7 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/LogTelnetHandlerTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/LogTelnetHandlerTest.java @@ -20,9 +20,9 @@ import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.telnet.TelnetHandler; -import org.junit.jupiter.api.Test; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; /** diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/PortTelnetHandlerTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/PortTelnetHandlerTest.java index 47df1c296b6..ff6ec433c30 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/PortTelnetHandlerTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/PortTelnetHandlerTest.java @@ -26,12 +26,14 @@ import org.apache.dubbo.rpc.protocol.dubbo.support.DemoService; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -44,7 +46,7 @@ public class PortTelnetHandlerTest { private static Invoker mockInvoker; @SuppressWarnings("unchecked") - @BeforeAll + @BeforeClass public static void before() { mockInvoker = mock(Invoker.class); given(mockInvoker.getInterface()).willReturn(DemoService.class); @@ -53,7 +55,7 @@ public static void before() { DubboProtocol.getDubboProtocol().export(mockInvoker); } - @AfterAll + @AfterClass public static void after() { ProtocolUtils.closeAll(); } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/SelectTelnetHandlerTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/SelectTelnetHandlerTest.java deleted file mode 100644 index 222f43a35a2..00000000000 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/SelectTelnetHandlerTest.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * 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 org.apache.dubbo.rpc.protocol.dubbo.telnet; - -import org.apache.dubbo.common.utils.NetUtils; -import org.apache.dubbo.remoting.Channel; -import org.apache.dubbo.remoting.RemotingException; -import org.apache.dubbo.remoting.telnet.TelnetHandler; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.ProviderModel; -import org.apache.dubbo.rpc.protocol.dubbo.support.DemoService; -import org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl; -import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.mock; - -/** - * SelectTelnetHandlerTest.java - */ -public class SelectTelnetHandlerTest { - - private static TelnetHandler select = new SelectTelnetHandler(); - private Channel mockChannel; - List methods; - - @BeforeEach - public void setup() { - String methodName = "getPerson"; - methods = new ArrayList<>(); - for (Method method : DemoService.class.getMethods()) { - if (method.getName().equals(methodName)) { - methods.add(method); - } - } - - ApplicationModel.reset(); - } - - @AfterEach - public void after() { - ProtocolUtils.closeAll(); - } - - @Test - public void testInvokeWithoutMethodList() throws RemotingException { - mockChannel = mock(Channel.class); - given(mockChannel.getAttribute("telnet.service")).willReturn(DemoService.class.getName()); - given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555")); - given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886")); - - ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), "", "", new DemoServiceImpl(), DemoService.class); - ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel); - - String result = select.telnet(mockChannel, "1"); - assertTrue(result.contains("Please use the invoke command first.")); - } - - @Test - public void testInvokeWithIllegalMessage() throws RemotingException { - mockChannel = mock(Channel.class); - given(mockChannel.getAttribute("telnet.service")).willReturn(DemoService.class.getName()); - given(mockChannel.getAttribute(InvokeTelnetHandler.INVOKE_METHOD_LIST_KEY)).willReturn(methods); - given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555")); - given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886")); - - ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), "", "", new DemoServiceImpl(), DemoService.class); - ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel); - - String result = select.telnet(mockChannel, "index"); - assertTrue(result.contains("Illegal index ,please input select 1")); - - result = select.telnet(mockChannel, "0"); - assertTrue(result.contains("Illegal index ,please input select 1")); - - result = select.telnet(mockChannel, "1000"); - assertTrue(result.contains("Illegal index ,please input select 1")); - } - - @Test - public void testInvokeWithNull() throws RemotingException { - mockChannel = mock(Channel.class); - given(mockChannel.getAttribute("telnet.service")).willReturn(DemoService.class.getName()); - given(mockChannel.getAttribute(InvokeTelnetHandler.INVOKE_METHOD_LIST_KEY)).willReturn(methods); - given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555")); - given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886")); - - ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), "", "", new DemoServiceImpl(), DemoService.class); - ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel); - - String result = select.telnet(mockChannel, null); - assertTrue(result.contains("Please input the index of the method you want to invoke")); - } -} diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ShutdownTelnetHandlerTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ShutdownTelnetHandlerTest.java deleted file mode 100644 index 6dbd026300e..00000000000 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ShutdownTelnetHandlerTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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 org.apache.dubbo.rpc.protocol.dubbo.telnet; - -import org.apache.dubbo.remoting.Channel; -import org.apache.dubbo.remoting.RemotingException; -import org.apache.dubbo.remoting.telnet.TelnetHandler; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.mock; - -/** - * SelectTelnetHandlerTest.java - */ -public class ShutdownTelnetHandlerTest { - - private static TelnetHandler handler = new ShutdownTelnetHandler(); - private Channel mockChannel; - - @SuppressWarnings("unchecked") - @Test - public void testInvoke() throws RemotingException { - mockChannel = mock(Channel.class); - String result = handler.telnet(mockChannel, ""); - assertTrue(result.contains("Application has shutdown successfully")); - } - - - @SuppressWarnings("unchecked") - @Test - public void testInvokeWithTimeParameter() throws RemotingException { - mockChannel = mock(Channel.class); - int sleepTime = 2000; - long start = System.currentTimeMillis(); - String result = handler.telnet(mockChannel, "-t " + sleepTime); - long end = System.currentTimeMillis(); - assertTrue(result.contains("Application has shutdown successfully") && (end - start) > sleepTime); - } - - -} diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/service/GenericServiceTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/service/GenericServiceTest.java index 6cbba5b13ff..f40223897d7 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/service/GenericServiceTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/service/GenericServiceTest.java @@ -31,9 +31,9 @@ import org.apache.dubbo.config.RegistryConfig; import org.apache.dubbo.config.ServiceConfig; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Test; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -46,7 +46,7 @@ /** * GenericServiceTest */ -@Disabled("Keeps failing on Travis, but can not be reproduced locally.") +@Ignore("Keeps failing on Travis, but can not be reproduced locally.") public class GenericServiceTest { @Test @@ -81,18 +81,18 @@ public void testGenericServiceException() { DemoService demoService = reference.get(); try { // say name - Assertions.assertEquals("Generic Haha", demoService.sayName("Haha")); + Assert.assertEquals("Generic Haha", demoService.sayName("Haha")); // get users List users = new ArrayList(); users.add(new User("Aaa")); users = demoService.getUsers(users); - Assertions.assertEquals("Aaa", users.get(0).getName()); + Assert.assertEquals("Aaa", users.get(0).getName()); // throw demo exception try { demoService.throwDemoException(); - Assertions.fail(); + Assert.fail(); } catch (DemoException e) { - Assertions.assertEquals("Generic", e.getMessage()); + Assert.assertEquals("Generic", e.getMessage()); } } finally { reference.destroy(); @@ -126,8 +126,8 @@ public void testGenericReferenceException() { user.put("name", "actual.provider"); users.add(user); users = (List>) genericService.$invoke("getUsers", new String[]{List.class.getName()}, new Object[]{users}); - Assertions.assertEquals(1, users.size()); - Assertions.assertEquals("actual.provider", users.get(0).get("name")); + Assert.assertEquals(1, users.size()); + Assert.assertEquals("actual.provider", users.get(0).get("name")); } finally { reference.destroy(); } @@ -160,9 +160,9 @@ public void testGenericSerializationJava() throws Exception { .getExtension("nativejava").serialize(null, bos).writeObject(name); byte[] arg = bos.toByteArray(); Object obj = genericService.$invoke("sayName", new String[]{String.class.getName()}, new Object[]{arg}); - Assertions.assertTrue(obj instanceof byte[]); + Assert.assertTrue(obj instanceof byte[]); byte[] result = (byte[]) obj; - Assertions.assertEquals(ref.sayName(name), ExtensionLoader.getExtensionLoader(Serialization.class) + Assert.assertEquals(ref.sayName(name), ExtensionLoader.getExtensionLoader(Serialization.class) .getExtension("nativejava").deserialize(null, new ByteArrayInputStream(result)).readObject().toString()); // getUsers @@ -176,9 +176,9 @@ public void testGenericSerializationJava() throws Exception { obj = genericService.$invoke("getUsers", new String[]{List.class.getName()}, new Object[]{bos.toByteArray()}); - Assertions.assertTrue(obj instanceof byte[]); + Assert.assertTrue(obj instanceof byte[]); result = (byte[]) obj; - Assertions.assertEquals(users, + Assert.assertEquals(users, ExtensionLoader.getExtensionLoader(Serialization.class) .getExtension("nativejava") .deserialize(null, new ByteArrayInputStream(result)) @@ -189,8 +189,8 @@ public void testGenericSerializationJava() throws Exception { ExtensionLoader.getExtensionLoader(Serialization.class).getExtension("nativejava") .serialize(null, bos).writeObject(Integer.MAX_VALUE); obj = genericService.$invoke("echo", new String[]{int.class.getName()}, new Object[]{bos.toByteArray()}); - Assertions.assertTrue(obj instanceof byte[]); - Assertions.assertEquals(Integer.MAX_VALUE, + Assert.assertTrue(obj instanceof byte[]); + Assert.assertEquals(Integer.MAX_VALUE, ExtensionLoader.getExtensionLoader(Serialization.class) .getExtension("nativejava") .deserialize(null, new ByteArrayInputStream((byte[]) obj)) @@ -227,13 +227,13 @@ public void testGenericInvokeWithBeanSerialization() throws Exception { List users = new ArrayList(); users.add(user); Object result = genericService.$invoke("getUsers", new String[]{ReflectUtils.getName(List.class)}, new Object[]{JavaBeanSerializeUtil.serialize(users, JavaBeanAccessor.METHOD)}); - Assertions.assertTrue(result instanceof JavaBeanDescriptor); + Assert.assertTrue(result instanceof JavaBeanDescriptor); JavaBeanDescriptor descriptor = (JavaBeanDescriptor) result; - Assertions.assertTrue(descriptor.isCollectionType()); - Assertions.assertEquals(1, descriptor.propertySize()); + Assert.assertTrue(descriptor.isCollectionType()); + Assert.assertEquals(1, descriptor.propertySize()); descriptor = (JavaBeanDescriptor) descriptor.getProperty(0); - Assertions.assertTrue(descriptor.isBeanType()); - Assertions.assertEquals(user.getName(), ((JavaBeanDescriptor) descriptor.getProperty("name")).getPrimitiveProperty()); + Assert.assertTrue(descriptor.isBeanType()); + Assert.assertEquals(user.getName(), ((JavaBeanDescriptor) descriptor.getProperty("name")).getPrimitiveProperty()); } finally { if (reference != null) { reference.destroy(); @@ -280,24 +280,24 @@ public void testGenericImplementationWithBeanSerialization() throws Exception { List users = new ArrayList(); users.add(user); List result = demoService.getUsers(users); - Assertions.assertEquals(users.size(), result.size()); - Assertions.assertEquals(user.getName(), result.get(0).getName()); + Assert.assertEquals(users.size(), result.size()); + Assert.assertEquals(user.getName(), result.get(0).getName()); GenericParameter gp = (GenericParameter) reference.get(); - Assertions.assertEquals("getUsers", gp.method); - Assertions.assertEquals(1, gp.parameterTypes.length); - Assertions.assertEquals(ReflectUtils.getName(List.class), gp.parameterTypes[0]); - Assertions.assertEquals(1, gp.arguments.length); - Assertions.assertTrue(gp.arguments[0] instanceof JavaBeanDescriptor); + Assert.assertEquals("getUsers", gp.method); + Assert.assertEquals(1, gp.parameterTypes.length); + Assert.assertEquals(ReflectUtils.getName(List.class), gp.parameterTypes[0]); + Assert.assertEquals(1, gp.arguments.length); + Assert.assertTrue(gp.arguments[0] instanceof JavaBeanDescriptor); JavaBeanDescriptor descriptor = (JavaBeanDescriptor) gp.arguments[0]; - Assertions.assertTrue(descriptor.isCollectionType()); - Assertions.assertEquals(ArrayList.class.getName(), descriptor.getClassName()); - Assertions.assertEquals(1, descriptor.propertySize()); + Assert.assertTrue(descriptor.isCollectionType()); + Assert.assertEquals(ArrayList.class.getName(), descriptor.getClassName()); + Assert.assertEquals(1, descriptor.propertySize()); descriptor = (JavaBeanDescriptor) descriptor.getProperty(0); - Assertions.assertTrue(descriptor.isBeanType()); - Assertions.assertEquals(User.class.getName(), descriptor.getClassName()); - Assertions.assertEquals(user.getName(), ((JavaBeanDescriptor) descriptor.getProperty("name")).getPrimitiveProperty()); - Assertions.assertNull(demoService.sayName("zhangsan")); + Assert.assertTrue(descriptor.isBeanType()); + Assert.assertEquals(User.class.getName(), descriptor.getClassName()); + Assert.assertEquals(user.getName(), ((JavaBeanDescriptor) descriptor.getProperty("name")).getPrimitiveProperty()); + Assert.assertNull(demoService.sayName("zhangsan")); } finally { if (ref != null) { ref.destroy(); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/validation/ValidationTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/validation/ValidationTest.java index 691f925216c..f0e85f94680 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/validation/ValidationTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/validation/ValidationTest.java @@ -28,10 +28,10 @@ import org.apache.dubbo.rpc.service.GenericException; import org.apache.dubbo.rpc.service.GenericService; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; @@ -48,12 +48,12 @@ public class ValidationTest { private RegistryConfig registryNA = new RegistryConfig("N/A"); private ProtocolConfig protocolDubo29582 = new ProtocolConfig("dubbo", 29582); - @BeforeEach + @Before public void setUp() { ConfigManager.getInstance().clear(); } - @AfterEach + @After public void tearDown() { ConfigManager.getInstance().clear(); } @@ -92,10 +92,10 @@ public void testValidation() { parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000)); parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000)); validationService.save(parameter); - Assertions.fail(); + Assert.fail(); } catch (ConstraintViolationException ve) { Set> violations = ve.getConstraintViolations(); - Assertions.assertNotNull(violations); + Assert.assertNotNull(violations); } // verify save group, save error @@ -106,10 +106,10 @@ public void testValidation() { parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000)); parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000)); validationService.save(parameter); - Assertions.fail(); + Assert.fail(); } catch (ConstraintViolationException ve) { Set> violations = ve.getConstraintViolations(); - Assertions.assertNotNull(violations); + Assert.assertNotNull(violations); } // relatedQuery error, no id and email is passed, will trigger validation exception for both Save @@ -121,21 +121,21 @@ public void testValidation() { parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000)); parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000)); validationService.relatedQuery(parameter); - Assertions.fail(); + Assert.fail(); } catch (ConstraintViolationException ve) { Set> violations = ve.getConstraintViolations(); - Assertions.assertEquals(violations.size(),2); + Assert.assertEquals(violations.size(),2); } // Save Error try { parameter = new ValidationParameter(); validationService.save(parameter); - Assertions.fail(); + Assert.fail(); } catch (ConstraintViolationException ve) { Set> violations = ve.getConstraintViolations(); - Assertions.assertTrue(violations.size() == 3); - Assertions.assertNotNull(violations); + Assert.assertTrue(violations.size() == 3); + Assert.assertNotNull(violations); } // Delete OK @@ -144,37 +144,37 @@ public void testValidation() { // Delete Error try { validationService.delete(2, "a"); - Assertions.fail(); + Assert.fail(); } catch (ConstraintViolationException ve) { Set> violations = ve.getConstraintViolations(); - Assertions.assertNotNull(violations); - Assertions.assertEquals(1, violations.size()); + Assert.assertNotNull(violations); + Assert.assertEquals(1, violations.size()); } // Delete Error try { validationService.delete(0, "abc"); - Assertions.fail(); + Assert.fail(); } catch (ConstraintViolationException ve) { Set> violations = ve.getConstraintViolations(); - Assertions.assertNotNull(violations); - Assertions.assertEquals(1, violations.size()); + Assert.assertNotNull(violations); + Assert.assertEquals(1, violations.size()); } try { validationService.delete(2, null); - Assertions.fail(); + Assert.fail(); } catch (ConstraintViolationException ve) { Set> violations = ve.getConstraintViolations(); - Assertions.assertNotNull(violations); - Assertions.assertEquals(1, violations.size()); + Assert.assertNotNull(violations); + Assert.assertEquals(1, violations.size()); } try { validationService.delete(0, null); - Assertions.fail(); + Assert.fail(); } catch (ConstraintViolationException ve) { Set> violations = ve.getConstraintViolations(); - Assertions.assertNotNull(violations); - Assertions.assertEquals(2, violations.size()); + Assert.assertNotNull(violations); + Assert.assertEquals(2, violations.size()); } } finally { reference.destroy(); @@ -214,9 +214,9 @@ public void testProviderValidation() { try { parameter = new ValidationParameter(); validationService.save(parameter); - Assertions.fail(); + Assert.fail(); } catch (RpcException e) { - Assertions.assertTrue(e.getMessage().contains("ConstraintViolation")); + Assert.assertTrue(e.getMessage().contains("ConstraintViolation")); } // Delete OK @@ -225,21 +225,21 @@ public void testProviderValidation() { // Delete Error try { validationService.delete(0, "abc"); - Assertions.fail(); + Assert.fail(); } catch (RpcException e) { - Assertions.assertTrue(e.getMessage().contains("ConstraintViolation")); + Assert.assertTrue(e.getMessage().contains("ConstraintViolation")); } try { validationService.delete(2, null); - Assertions.fail(); + Assert.fail(); } catch (RpcException e) { - Assertions.assertTrue(e.getMessage().contains("ConstraintViolation")); + Assert.assertTrue(e.getMessage().contains("ConstraintViolation")); } try { validationService.delete(0, null); - Assertions.fail(); + Assert.fail(); } catch (RpcException e) { - Assertions.assertTrue(e.getMessage().contains("ConstraintViolation")); + Assert.assertTrue(e.getMessage().contains("ConstraintViolation")); } } finally { reference.destroy(); @@ -280,9 +280,9 @@ public void testGenericValidation() { try { parameter = new HashMap(); validationService.$invoke("save", new String[]{ValidationParameter.class.getName()}, new Object[]{parameter}); - Assertions.fail(); + Assert.fail(); } catch (GenericException e) { - Assertions.assertTrue(e.getMessage().contains("Failed to validate service")); + Assert.assertTrue(e.getMessage().contains("Failed to validate service")); } // Delete OK @@ -291,24 +291,24 @@ public void testGenericValidation() { // Delete Error try { validationService.$invoke("delete", new String[]{long.class.getName(), String.class.getName()}, new Object[]{0, "abc"}); - Assertions.fail(); + Assert.fail(); } catch (GenericException e) { - Assertions.assertTrue(e.getMessage().contains("Failed to validate service")); + Assert.assertTrue(e.getMessage().contains("Failed to validate service")); } try { validationService.$invoke("delete", new String[]{long.class.getName(), String.class.getName()}, new Object[]{2, null}); - Assertions.fail(); + Assert.fail(); } catch (GenericException e) { - Assertions.assertTrue(e.getMessage().contains("Failed to validate service")); + Assert.assertTrue(e.getMessage().contains("Failed to validate service")); } try { validationService.$invoke("delete", new String[]{long.class.getName(), String.class.getName()}, new Object[]{0, null}); - Assertions.fail(); + Assert.fail(); } catch (GenericException e) { - Assertions.assertTrue(e.getMessage().contains("Failed to validate service")); + Assert.assertTrue(e.getMessage().contains("Failed to validate service")); } } catch (GenericException e) { - Assertions.assertTrue(e.getMessage().contains("Failed to validate service")); + Assert.assertTrue(e.getMessage().contains("Failed to validate service")); } finally { reference.destroy(); } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/resources/log4j.xml b/dubbo-rpc/dubbo-rpc-dubbo/src/test/resources/log4j.xml index 09dba05e6cc..3c5d2ba218e 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/resources/log4j.xml +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/resources/log4j.xml @@ -21,7 +21,6 @@ - @@ -31,8 +30,17 @@ --> + + + + + + + + - + \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-hessian/pom.xml b/dubbo-rpc/dubbo-rpc-hessian/pom.xml index 0eed79d254a..05867c7cc4a 100644 --- a/dubbo-rpc/dubbo-rpc-hessian/pom.xml +++ b/dubbo-rpc/dubbo-rpc-hessian/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-rpc - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-rpc-hessian jar @@ -53,10 +53,5 @@ ${project.parent.version} test - - org.springframework - spring-test - test - \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-hessian/src/test/java/org/apache/dubbo/rpc/protocol/hessian/HessianProtocolTest.java b/dubbo-rpc/dubbo-rpc-hessian/src/test/java/org/apache/dubbo/rpc/protocol/hessian/HessianProtocolTest.java index 9fcadaa2e0f..e9abc0f30e7 100644 --- a/dubbo-rpc/dubbo-rpc-hessian/src/test/java/org/apache/dubbo/rpc/protocol/hessian/HessianProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-hessian/src/test/java/org/apache/dubbo/rpc/protocol/hessian/HessianProtocolTest.java @@ -33,14 +33,14 @@ import org.apache.dubbo.rpc.protocol.hessian.HessianServiceImpl.MyException; import org.apache.dubbo.rpc.service.GenericService; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.fail; /** * HessianProtocolTest @@ -50,7 +50,7 @@ public class HessianProtocolTest { @Test public void testHessianProtocol() { HessianServiceImpl server = new HessianServiceImpl(); - Assertions.assertFalse(server.isCalled()); + Assert.assertFalse(server.isCalled()); ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); URL url = URL.valueOf("hessian://127.0.0.1:5342/" + HessianService.class.getName() + "?version=1.0.0&hessian.overload.method=true"); @@ -58,8 +58,8 @@ public void testHessianProtocol() { Invoker invoker = protocol.refer(HessianService.class, url); HessianService client = proxyFactory.getProxy(invoker); String result = client.sayHello("haha"); - Assertions.assertTrue(server.isCalled()); - Assertions.assertEquals("Hello, haha", result); + Assert.assertTrue(server.isCalled()); + Assert.assertEquals("Hello, haha", result); invoker.destroy(); exporter.unexport(); } @@ -67,7 +67,7 @@ public void testHessianProtocol() { @Test public void testGenericInvoke() { HessianServiceImpl server = new HessianServiceImpl(); - Assertions.assertFalse(server.isCalled()); + Assert.assertFalse(server.isCalled()); ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); URL url = URL.valueOf("hessian://127.0.0.1:5342/" + HessianService.class.getName() + "?version=1.0.0"); @@ -75,8 +75,8 @@ public void testGenericInvoke() { Invoker invoker = protocol.refer(GenericService.class, url); GenericService client = proxyFactory.getProxy(invoker, true); String result = (String) client.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{"haha"}); - Assertions.assertTrue(server.isCalled()); - Assertions.assertEquals("Hello, haha", result); + Assert.assertTrue(server.isCalled()); + Assert.assertEquals("Hello, haha", result); invoker.destroy(); exporter.unexport(); } @@ -84,7 +84,7 @@ public void testGenericInvoke() { @Test public void testGenericInvokeWithNativeJava() throws IOException, ClassNotFoundException { HessianServiceImpl server = new HessianServiceImpl(); - Assertions.assertFalse(server.isCalled()); + Assert.assertFalse(server.isCalled()); ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); URL url = URL.valueOf("hessian://127.0.0.1:5342/" + HessianService.class.getName() + "?version=1.0.0&generic=nativejava"); @@ -102,8 +102,8 @@ public void testGenericInvokeWithNativeJava() throws IOException, ClassNotFoundE Object result = client.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{byteArrayOutputStream.toByteArray()}); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream((byte[]) result); ObjectInput objectInput = serialization.deserialize(url, byteArrayInputStream); - Assertions.assertTrue(server.isCalled()); - Assertions.assertEquals("Hello, haha", objectInput.readObject()); + Assert.assertTrue(server.isCalled()); + Assert.assertEquals("Hello, haha", objectInput.readObject()); invoker.destroy(); exporter.unexport(); } @@ -120,7 +120,7 @@ public void testGenericInvokeWithRpcContext() { Invoker invoker = protocol.refer(GenericService.class, url); GenericService client = proxyFactory.getProxy(invoker, true); String result = (String) client.$invoke("context", new String[]{"java.lang.String"}, new Object[]{"haha"}); - Assertions.assertEquals("Hello, haha context, 123", result); + Assert.assertEquals("Hello, haha context, 123", result); invoker.destroy(); exporter.unexport(); } @@ -128,7 +128,7 @@ public void testGenericInvokeWithRpcContext() { @Test public void testGenericInvokeWithBean() { HessianServiceImpl server = new HessianServiceImpl(); - Assertions.assertFalse(server.isCalled()); + Assert.assertFalse(server.isCalled()); ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); URL url = URL.valueOf("hessian://127.0.0.1:5342/" + HessianService.class.getName() + "?version=1.0.0&generic=bean"); @@ -139,8 +139,8 @@ public void testGenericInvokeWithBean() { JavaBeanDescriptor javaBeanDescriptor = JavaBeanSerializeUtil.serialize("haha"); Object result = client.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{javaBeanDescriptor}); - Assertions.assertTrue(server.isCalled()); - Assertions.assertEquals("Hello, haha", JavaBeanSerializeUtil.deserialize((JavaBeanDescriptor) result)); + Assert.assertTrue(server.isCalled()); + Assert.assertEquals("Hello, haha", JavaBeanSerializeUtil.deserialize((JavaBeanDescriptor) result)); invoker.destroy(); exporter.unexport(); } @@ -148,7 +148,7 @@ public void testGenericInvokeWithBean() { @Test public void testOverload() { HessianServiceImpl server = new HessianServiceImpl(); - Assertions.assertFalse(server.isCalled()); + Assert.assertFalse(server.isCalled()); ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); URL url = URL.valueOf("hessian://127.0.0.1:5342/" + HessianService.class.getName() + "?version=1.0.0&hessian.overload.method=true&hessian2.request=false"); @@ -156,9 +156,9 @@ public void testOverload() { Invoker invoker = protocol.refer(HessianService.class, url); HessianService client = proxyFactory.getProxy(invoker); String result = client.sayHello("haha"); - Assertions.assertEquals("Hello, haha", result); + Assert.assertEquals("Hello, haha", result); result = client.sayHello("haha", 1); - Assertions.assertEquals("Hello, haha. ", result); + Assert.assertEquals("Hello, haha. ", result); invoker.destroy(); exporter.unexport(); } @@ -166,7 +166,7 @@ public void testOverload() { @Test public void testHttpClient() { HessianServiceImpl server = new HessianServiceImpl(); - Assertions.assertFalse(server.isCalled()); + Assert.assertFalse(server.isCalled()); ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); URL url = URL.valueOf("hessian://127.0.0.1:5342/" + HessianService.class.getName() + "?version=1.0.0&client=httpclient&hessian.overload.method=true"); @@ -174,8 +174,8 @@ public void testHttpClient() { Invoker invoker = protocol.refer(HessianService.class, url); HessianService client = proxyFactory.getProxy(invoker); String result = client.sayHello("haha"); - Assertions.assertTrue(server.isCalled()); - Assertions.assertEquals("Hello, haha", result); + Assert.assertTrue(server.isCalled()); + Assert.assertEquals("Hello, haha", result); invoker.destroy(); exporter.unexport(); } @@ -193,7 +193,7 @@ public void testTimeOut() { client.timeOut(6000); fail(); } catch (RpcException expected) { - Assertions.assertTrue(expected.isTimeout()); + Assert.assertTrue(expected.isTimeout()); } finally { invoker.destroy(); exporter.unexport(); diff --git a/dubbo-rpc/dubbo-rpc-http/pom.xml b/dubbo-rpc/dubbo-rpc-http/pom.xml index 5ab4d25d156..f98d062d373 100644 --- a/dubbo-rpc/dubbo-rpc-http/pom.xml +++ b/dubbo-rpc/dubbo-rpc-http/pom.xml @@ -1,57 +1,57 @@ - - - 4.0.0 - - org.apache.dubbo - dubbo-rpc - 2.7.1-SNAPSHOT - - dubbo-rpc-http - jar - ${project.artifactId} - The http rpc module of dubbo project - - false - - - - org.apache.dubbo - dubbo-rpc-api - ${project.parent.version} - - - org.apache.dubbo - dubbo-remoting-http - ${project.parent.version} - - - org.springframework - spring-context - - - org.springframework - spring-web - - - org.apache.dubbo - dubbo-serialization-jdk - ${project.parent.version} - test - - + + + 4.0.0 + + org.apache.dubbo + dubbo-rpc + 2.7.0-SNAPSHOT + + dubbo-rpc-http + jar + ${project.artifactId} + The http rpc module of dubbo project + + false + + + + org.apache.dubbo + dubbo-rpc-api + ${project.parent.version} + + + org.apache.dubbo + dubbo-remoting-http + ${project.parent.version} + + + org.springframework + spring-context + + + org.springframework + spring-web + + + org.apache.dubbo + dubbo-serialization-jdk + ${project.parent.version} + test + + \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-http/src/main/java/org/apache/dubbo/rpc/protocol/http/HttpProtocol.java b/dubbo-rpc/dubbo-rpc-http/src/main/java/org/apache/dubbo/rpc/protocol/http/HttpProtocol.java index 0188a0fe329..09ea0559bee 100644 --- a/dubbo-rpc/dubbo-rpc-http/src/main/java/org/apache/dubbo/rpc/protocol/http/HttpProtocol.java +++ b/dubbo-rpc/dubbo-rpc-http/src/main/java/org/apache/dubbo/rpc/protocol/http/HttpProtocol.java @@ -18,7 +18,6 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.common.Version; import org.apache.dubbo.remoting.http.HttpBinder; import org.apache.dubbo.remoting.http.HttpHandler; @@ -154,7 +153,7 @@ public RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation httpProxyFactoryBean.setServiceUrl(key); httpProxyFactoryBean.setServiceInterface(serviceType); String client = url.getParameter(Constants.CLIENT_KEY); - if (StringUtils.isEmpty(client) || "simple".equals(client)) { + if (client == null || client.length() == 0 || "simple".equals(client)) { SimpleHttpInvokerRequestExecutor httpInvokerRequestExecutor = new SimpleHttpInvokerRequestExecutor() { @Override protected void prepareConnection(HttpURLConnection con, diff --git a/dubbo-rpc/dubbo-rpc-http/src/test/java/org/apache/dubbo/rpc/protocol/http/HttpProtocolTest.java b/dubbo-rpc/dubbo-rpc-http/src/test/java/org/apache/dubbo/rpc/protocol/http/HttpProtocolTest.java index 009f01d1924..1ff5438a3f4 100644 --- a/dubbo-rpc/dubbo-rpc-http/src/test/java/org/apache/dubbo/rpc/protocol/http/HttpProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-http/src/test/java/org/apache/dubbo/rpc/protocol/http/HttpProtocolTest.java @@ -24,21 +24,16 @@ import org.apache.dubbo.common.serialize.ObjectOutput; import org.apache.dubbo.common.serialize.Serialization; import org.apache.dubbo.common.serialize.nativejava.NativeJavaSerialization; -import org.apache.dubbo.rpc.Exporter; -import org.apache.dubbo.rpc.Invoker; -import org.apache.dubbo.rpc.Protocol; -import org.apache.dubbo.rpc.ProxyFactory; -import org.apache.dubbo.rpc.RpcException; +import org.apache.dubbo.rpc.*; import org.apache.dubbo.rpc.service.GenericService; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import junit.framework.Assert; +import org.junit.Test; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.fail; /** * HttpProtocolTest @@ -48,7 +43,7 @@ public class HttpProtocolTest { @Test public void testHttpProtocol() { HttpServiceImpl server = new HttpServiceImpl(); - Assertions.assertFalse(server.isCalled()); + Assert.assertFalse(server.isCalled()); ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); URL url = URL.valueOf("http://127.0.0.1:5342/" + HttpService.class.getName() + "?release=2.7.0"); @@ -56,8 +51,8 @@ public void testHttpProtocol() { Invoker invoker = protocol.refer(HttpService.class, url); HttpService client = proxyFactory.getProxy(invoker); String result = client.sayHello("haha"); - Assertions.assertTrue(server.isCalled()); - Assertions.assertEquals("Hello, haha", result); + Assert.assertTrue(server.isCalled()); + Assert.assertEquals("Hello, haha", result); invoker.destroy(); exporter.unexport(); } @@ -65,7 +60,7 @@ public void testHttpProtocol() { @Test public void testGenericInvoke() { HttpServiceImpl server = new HttpServiceImpl(); - Assertions.assertFalse(server.isCalled()); + Assert.assertFalse(server.isCalled()); ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); URL url = URL.valueOf("http://127.0.0.1:5342/" + HttpService.class.getName() + "?release=2.7.0"); @@ -73,8 +68,8 @@ public void testGenericInvoke() { Invoker invoker = protocol.refer(GenericService.class, url); GenericService client = proxyFactory.getProxy(invoker, true); String result = (String) client.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{"haha"}); - Assertions.assertTrue(server.isCalled()); - Assertions.assertEquals("Hello, haha", result); + Assert.assertTrue(server.isCalled()); + Assert.assertEquals("Hello, haha", result); invoker.destroy(); exporter.unexport(); } @@ -82,7 +77,7 @@ public void testGenericInvoke() { @Test public void testGenericInvokeWithNativeJava() throws IOException, ClassNotFoundException { HttpServiceImpl server = new HttpServiceImpl(); - Assertions.assertFalse(server.isCalled()); + Assert.assertFalse(server.isCalled()); ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); URL url = URL.valueOf("http://127.0.0.1:5342/" + HttpService.class.getName() + "?release=2.7.0&generic=nativejava"); @@ -100,8 +95,8 @@ public void testGenericInvokeWithNativeJava() throws IOException, ClassNotFoundE Object result = client.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{byteArrayOutputStream.toByteArray()}); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream((byte[]) result); ObjectInput objectInput = serialization.deserialize(url, byteArrayInputStream); - Assertions.assertTrue(server.isCalled()); - Assertions.assertEquals("Hello, haha", objectInput.readObject()); + Assert.assertTrue(server.isCalled()); + Assert.assertEquals("Hello, haha", objectInput.readObject()); invoker.destroy(); exporter.unexport(); } @@ -109,7 +104,7 @@ public void testGenericInvokeWithNativeJava() throws IOException, ClassNotFoundE @Test public void testGenericInvokeWithBean() { HttpServiceImpl server = new HttpServiceImpl(); - Assertions.assertFalse(server.isCalled()); + Assert.assertFalse(server.isCalled()); ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); URL url = URL.valueOf("http://127.0.0.1:5342/" + HttpService.class.getName() + "?release=2.7.0&generic=bean"); @@ -120,8 +115,8 @@ public void testGenericInvokeWithBean() { JavaBeanDescriptor javaBeanDescriptor = JavaBeanSerializeUtil.serialize("haha"); Object result = client.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{javaBeanDescriptor}); - Assertions.assertTrue(server.isCalled()); - Assertions.assertEquals("Hello, haha", JavaBeanSerializeUtil.deserialize((JavaBeanDescriptor) result)); + Assert.assertTrue(server.isCalled()); + Assert.assertEquals("Hello, haha", JavaBeanSerializeUtil.deserialize((JavaBeanDescriptor) result)); invoker.destroy(); exporter.unexport(); } @@ -129,7 +124,7 @@ public void testGenericInvokeWithBean() { @Test public void testOverload() { HttpServiceImpl server = new HttpServiceImpl(); - Assertions.assertFalse(server.isCalled()); + Assert.assertFalse(server.isCalled()); ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); URL url = URL.valueOf("http://127.0.0.1:5342/" + HttpService.class.getName() + "?release=2.7.0&hessian.overload.method=true&hessian2.request=false"); @@ -137,9 +132,9 @@ public void testOverload() { Invoker invoker = protocol.refer(HttpService.class, url); HttpService client = proxyFactory.getProxy(invoker); String result = client.sayHello("haha"); - Assertions.assertEquals("Hello, haha", result); + Assert.assertEquals("Hello, haha", result); result = client.sayHello("haha", 1); - Assertions.assertEquals("Hello, haha. ", result); + Assert.assertEquals("Hello, haha. ", result); invoker.destroy(); exporter.unexport(); } @@ -147,7 +142,7 @@ public void testOverload() { @Test public void testSimpleClient() { HttpServiceImpl server = new HttpServiceImpl(); - Assertions.assertFalse(server.isCalled()); + Assert.assertFalse(server.isCalled()); ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); URL url = URL.valueOf("http://127.0.0.1:5342/" + HttpService.class.getName() + "?release=2.7.0&client=simple"); @@ -155,8 +150,8 @@ public void testSimpleClient() { Invoker invoker = protocol.refer(HttpService.class, url); HttpService client = proxyFactory.getProxy(invoker); String result = client.sayHello("haha"); - Assertions.assertTrue(server.isCalled()); - Assertions.assertEquals("Hello, haha", result); + Assert.assertTrue(server.isCalled()); + Assert.assertEquals("Hello, haha", result); invoker.destroy(); exporter.unexport(); } @@ -174,7 +169,7 @@ public void testTimeOut() { client.timeOut(6000); fail(); } catch (RpcException expected) { - Assertions.assertEquals(true, expected.isTimeout()); + Assert.assertEquals(true, expected.isTimeout()); } finally { invoker.destroy(); exporter.unexport(); diff --git a/dubbo-rpc/dubbo-rpc-injvm/pom.xml b/dubbo-rpc/dubbo-rpc-injvm/pom.xml index 9a3de8e080e..15fb6bc04dc 100644 --- a/dubbo-rpc/dubbo-rpc-injvm/pom.xml +++ b/dubbo-rpc/dubbo-rpc-injvm/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-rpc - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-rpc-injvm jar diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java index 37d23d5bdf7..591022ecde4 100644 --- a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java +++ b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java @@ -16,8 +16,8 @@ */ package org.apache.dubbo.rpc.protocol.injvm; -import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.rpc.Exporter; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Result; @@ -58,7 +58,7 @@ public Result doInvoke(Invocation invocation) throws Throwable { if (exporter == null) { throw new RpcException("Service [" + key + "] not found."); } - RpcContext.getContext().setRemoteAddress(Constants.LOCALHOST_VALUE, 0); + RpcContext.getContext().setRemoteAddress(NetUtils.LOCALHOST, 0); return exporter.getInvoker().invoke(invocation); } } diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocol.java b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocol.java index 1181e899d71..290e2084304 100644 --- a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocol.java +++ b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocol.java @@ -19,7 +19,6 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.extension.ExtensionLoader; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.UrlUtils; import org.apache.dubbo.rpc.Exporter; import org.apache.dubbo.rpc.Invoker; @@ -57,7 +56,7 @@ static Exporter getExporter(Map> map, URL key) { if (!key.getServiceKey().contains("*")) { result = map.get(key.getServiceKey()); } else { - if (CollectionUtils.isNotEmptyMap(map)) { + if (map != null && !map.isEmpty()) { for (Exporter exporter : map.values()) { if (UrlUtils.isServiceKeyMatch(key, exporter.getInvoker().getUrl())) { result = exporter; @@ -93,23 +92,27 @@ public Invoker refer(Class serviceType, URL url) throws RpcException { } public boolean isInjvmRefer(URL url) { + final boolean isJvmRefer; String scope = url.getParameter(Constants.SCOPE_KEY); // Since injvm protocol is configured explicitly, we don't need to set any extra flag, use normal refer process. - if (Constants.SCOPE_LOCAL.equals(scope) || (url.getParameter(Constants.LOCAL_PROTOCOL, false))) { + if (Constants.LOCAL_PROTOCOL.toString().equals(url.getProtocol())) { + isJvmRefer = false; + } else if (Constants.SCOPE_LOCAL.equals(scope) || (url.getParameter(Constants.LOCAL_PROTOCOL, false))) { // if it's declared as local reference // 'scope=local' is equivalent to 'injvm=true', injvm will be deprecated in the future release - return true; + isJvmRefer = true; } else if (Constants.SCOPE_REMOTE.equals(scope)) { // it's declared as remote reference - return false; + isJvmRefer = false; } else if (url.getParameter(Constants.GENERIC_KEY, false)) { // generic invocation is not local reference - return false; + isJvmRefer = false; } else if (getExporter(exporterMap, url) != null) { // by default, go through local reference if there's the service exposed locally - return true; + isJvmRefer = true; } else { - return false; + isJvmRefer = false; } + return isJvmRefer; } } diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocolTest.java b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocolTest.java index 5285e92507f..c6c0bdb1237 100644 --- a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocolTest.java @@ -25,16 +25,16 @@ import org.apache.dubbo.rpc.Protocol; import org.apache.dubbo.rpc.ProxyFactory; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * ProxiesTest @@ -50,7 +50,7 @@ public class InjvmProtocolTest { private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); private List> exporters = new ArrayList>(); - @AfterEach + @After public void after() throws Exception { for (Exporter exporter : exporters) { exporter.unexport(); diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/ProtocolTest.java b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/ProtocolTest.java index 12de553d195..45ee265d3d3 100644 --- a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/ProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/ProtocolTest.java @@ -22,11 +22,11 @@ import org.apache.dubbo.rpc.Protocol; import org.apache.dubbo.rpc.ProxyFactory; -import org.junit.jupiter.api.Test; +import org.junit.Test; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.matchers.JUnitMatchers.containsString; public class ProtocolTest { @@ -36,7 +36,7 @@ public String echo(String e) { } }; - static { + static{ InjvmProtocol injvm = InjvmProtocol.getInjvmProtocol(); } @@ -52,7 +52,7 @@ public void test_destroyWontCloseAllProtocol() throws Exception { Protocol InjvmProtocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension("injvm"); - assertEquals(0, InjvmProtocol.getDefaultPort()); + assertEquals(0,InjvmProtocol.getDefaultPort()); InjvmProtocol.export(invoker); diff --git a/dubbo-rpc/dubbo-rpc-memcached/pom.xml b/dubbo-rpc/dubbo-rpc-memcached/pom.xml index 6544e9c9a18..97573635f6b 100644 --- a/dubbo-rpc/dubbo-rpc-memcached/pom.xml +++ b/dubbo-rpc/dubbo-rpc-memcached/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-rpc - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-rpc-memcached jar diff --git a/dubbo-rpc/dubbo-rpc-redis/pom.xml b/dubbo-rpc/dubbo-rpc-redis/pom.xml index 056b9013979..df5be049862 100644 --- a/dubbo-rpc/dubbo-rpc-redis/pom.xml +++ b/dubbo-rpc/dubbo-rpc-redis/pom.xml @@ -1,53 +1,53 @@ - - - 4.0.0 - - org.apache.dubbo - dubbo-rpc - 2.7.1-SNAPSHOT - - dubbo-rpc-redis - jar - ${project.artifactId} - The redis rpc module of dubbo project - - false - - - - org.apache.dubbo - dubbo-rpc-api - ${project.parent.version} - - - redis.clients - jedis - - - com.github.kstyrc - embedded-redis - test - - - org.apache.dubbo - dubbo-serialization-jdk - ${project.parent.version} - test - - + + + 4.0.0 + + org.apache.dubbo + dubbo-rpc + 2.7.0-SNAPSHOT + + dubbo-rpc-redis + jar + ${project.artifactId} + The redis rpc module of dubbo project + + false + + + + org.apache.dubbo + dubbo-rpc-api + ${project.parent.version} + + + redis.clients + jedis + + + com.github.kstyrc + embedded-redis + test + + + org.apache.dubbo + dubbo-serialization-jdk + ${project.parent.version} + test + + \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-redis/src/test/java/org/apache/dubbo/rpc/protocol/redis/RedisProtocolTest.java b/dubbo-rpc/dubbo-rpc-redis/src/test/java/org/apache/dubbo/rpc/protocol/redis/RedisProtocolTest.java index 9abada0a49e..8c69c38767c 100644 --- a/dubbo-rpc/dubbo-rpc-redis/src/test/java/org/apache/dubbo/rpc/protocol/redis/RedisProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-redis/src/test/java/org/apache/dubbo/rpc/protocol/redis/RedisProtocolTest.java @@ -16,6 +16,7 @@ */ package org.apache.dubbo.rpc.protocol.redis; +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.extension.ExtensionLoader; @@ -26,14 +27,13 @@ import org.apache.dubbo.rpc.Protocol; import org.apache.dubbo.rpc.ProxyFactory; import org.apache.dubbo.rpc.RpcException; - -import org.apache.commons.pool2.impl.GenericObjectPoolConfig; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; +import org.apache.dubbo.rpc.RpcResult; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TestName; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.exceptions.JedisConnectionException; @@ -41,10 +41,11 @@ import redis.embedded.RedisServer; import java.io.ByteArrayInputStream; +import java.io.IOException; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class RedisProtocolTest { private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); @@ -52,14 +53,16 @@ public class RedisProtocolTest { private RedisServer redisServer; private URL registryUrl; - @BeforeEach - public void setUp(TestInfo testInfo) { + @Rule + public TestName name = new TestName(); + + @Before + public void setUp() throws Exception { int redisPort = NetUtils.getAvailablePort(); - String methodName = testInfo.getTestMethod().get().getName(); - if ("testAuthRedis".equals(methodName) || ("testWrongAuthRedis".equals(methodName))) { + if (name.getMethodName().equals("testAuthRedis") || name.getMethodName().equals("testWrongAuthRedis")) { String password = "123456"; this.redisServer = RedisServer.builder().port(redisPort).setting("requirepass " + password).build(); - this.registryUrl = URL.valueOf("redis://username:" + password + "@localhost:" + redisPort + "?db.index=0"); + this.registryUrl = URL.valueOf("redis://username:"+password+"@localhost:"+redisPort+"?db.index=0"); } else { this.redisServer = RedisServer.builder().port(redisPort).build(); this.registryUrl = URL.valueOf("redis://localhost:" + redisPort); @@ -67,7 +70,7 @@ public void setUp(TestInfo testInfo) { this.redisServer.start(); } - @AfterEach + @After public void tearDown() { this.redisServer.stop(); } @@ -102,39 +105,33 @@ public void testInvocation() { refer.destroy(); } - @Test + @Test(expected = RpcException.class) public void testUnsupportedMethod() { - Assertions.assertThrows(RpcException.class, () -> { - Invoker refer = protocol.refer(IDemoService.class, registryUrl); - IDemoService demoService = this.proxy.getProxy(refer); + Invoker refer = protocol.refer(IDemoService.class, registryUrl); + IDemoService demoService = this.proxy.getProxy(refer); - demoService.unsupported(null); - }); + demoService.unsupported(null); } - @Test + @Test(expected = RpcException.class) public void testWrongParameters() { - Assertions.assertThrows(RpcException.class, () -> { - Invoker refer = protocol.refer(IDemoService.class, registryUrl); - IDemoService demoService = this.proxy.getProxy(refer); + Invoker refer = protocol.refer(IDemoService.class, registryUrl); + IDemoService demoService = this.proxy.getProxy(refer); - demoService.set("key", "value", "wrongValue"); - }); + demoService.set("key", "value", "wrongValue"); } - @Test + @Test(expected = RpcException.class) public void testWrongRedis() { - Assertions.assertThrows(RpcException.class, () -> { - Invoker refer = protocol.refer(IDemoService.class, URL.valueOf("redis://localhost:1")); - IDemoService demoService = this.proxy.getProxy(refer); + Invoker refer = protocol.refer(IDemoService.class, URL.valueOf("redis://localhost:1")); + IDemoService demoService = this.proxy.getProxy(refer); - demoService.get("key"); - }); + demoService.get("key"); } - @Test + @Test(expected = UnsupportedOperationException.class) public void testExport() { - Assertions.assertThrows(UnsupportedOperationException.class, () -> protocol.export(protocol.refer(IDemoService.class, registryUrl))); + protocol.export(protocol.refer(IDemoService.class, registryUrl)); } @Test @@ -174,16 +171,21 @@ public void testAuthRedis() { assertThat(value, is("newValue")); // jedis gets the result comparison - JedisPool pool = new JedisPool(new GenericObjectPoolConfig(), "localhost", registryUrl.getPort(), 2000, password, database, (String) null); - try (Jedis jedis = pool.getResource()) { + JedisPool pool = new JedisPool(new GenericObjectPoolConfig(), "localhost", registryUrl.getPort(), 2000, password, database, (String)null); + Jedis jedis = null; + try { + jedis = pool.getResource(); byte[] valueByte = jedis.get("key".getBytes()); Serialization serialization = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(this.registryUrl.getParameter(Constants.SERIALIZATION_KEY, "java")); ObjectInput oin = serialization.deserialize(this.registryUrl, new ByteArrayInputStream(valueByte)); String actual = (String) oin.readObject(); assertThat(value, is(actual)); - } catch (Exception e) { - Assertions.fail("jedis gets the result comparison is error!"); + } catch(Exception e) { + Assert.fail("jedis gets the result comparison is error!"); } finally { + if (jedis != null) { + jedis.close(); + } pool.destroy(); } @@ -209,9 +211,9 @@ public void testWrongAuthRedis() { assertThat(value, is(nullValue())); } catch (RpcException e) { if (e.getCause() instanceof JedisConnectionException && e.getCause().getCause() instanceof JedisDataException) { - Assertions.assertEquals("ERR invalid password", e.getCause().getCause().getMessage()); + Assert.assertEquals("ERR invalid password" , e.getCause().getCause().getMessage()); } else { - Assertions.fail("no invalid password exception!"); + Assert.fail("no invalid password exception!"); } } diff --git a/dubbo-rpc/dubbo-rpc-rest/pom.xml b/dubbo-rpc/dubbo-rpc-rest/pom.xml index 126b26d3a0d..2ccefbcbb1f 100644 --- a/dubbo-rpc/dubbo-rpc-rest/pom.xml +++ b/dubbo-rpc/dubbo-rpc-rest/pom.xml @@ -1,25 +1,24 @@ 4.0.0 org.apache.dubbo dubbo-rpc - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-rpc-rest jar diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java index 6265254d4c6..7f8bc79f401 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java @@ -28,13 +28,16 @@ import org.apache.http.HeaderElement; import org.apache.http.HeaderElementIterator; +import org.apache.http.HttpResponse; import org.apache.http.client.config.RequestConfig; import org.apache.http.config.SocketConfig; +import org.apache.http.conn.ConnectionKeepAliveStrategy; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.message.BasicHeaderElementIterator; import org.apache.http.protocol.HTTP; +import org.apache.http.protocol.HttpContext; import org.jboss.resteasy.client.jaxrs.ResteasyClient; import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; @@ -58,16 +61,16 @@ public class RestProtocol extends AbstractProxyProtocol { private static final int HTTPCLIENTCONNECTIONMANAGER_MAXPERROUTE = 20; private static final int HTTPCLIENTCONNECTIONMANAGER_MAXTOTAL = 20; - private static final int HTTPCLIENT_KEEPALIVEDURATION = 30 * 1000; + private static final int HTTPCLIENT_KEEPALIVEDURATION = 30*1000; private static final int HTTPCLIENTCONNECTIONMANAGER_CLOSEWAITTIME_MS = 1000; private static final int HTTPCLIENTCONNECTIONMANAGER_CLOSEIDLETIME_S = 30; - private final Map servers = new ConcurrentHashMap<>(); + private final Map servers = new ConcurrentHashMap(); private final RestServerFactory serverFactory = new RestServerFactory(); // TODO in the future maybe we can just use a single rest client and connection manager - private final List clients = Collections.synchronizedList(new LinkedList<>()); + private final List clients = Collections.synchronizedList(new LinkedList()); private volatile ConnectionMonitor connectionMonitor; @@ -87,12 +90,13 @@ public int getDefaultPort() { @Override protected Runnable doExport(T impl, Class type, URL url) throws RpcException { String addr = getAddr(url); - Class implClass = ApplicationModel.getProviderModel(url.getPathKey()).getServiceInstance().getClass(); - RestServer server = servers.computeIfAbsent(addr, restServer -> { - RestServer s = serverFactory.createServer(url.getParameter(Constants.SERVER_KEY, DEFAULT_SERVER)); - s.start(url); - return s; - }); + Class implClass = ApplicationModel.getProviderModel(url.getServiceKey()).getServiceInstance().getClass(); + RestServer server = servers.get(addr); + if (server == null) { + server = serverFactory.createServer(url.getParameter(Constants.SERVER_KEY, DEFAULT_SERVER)); + server.start(url); + servers.put(addr, server); + } String contextPath = getContextPath(url); if ("servlet".equalsIgnoreCase(url.getParameter(Constants.SERVER_KEY, DEFAULT_SERVER))) { @@ -120,15 +124,21 @@ protected Runnable doExport(T impl, Class type, URL url) throws RpcExcept server.deploy(resourceDef, impl, contextPath); final RestServer s = server; - return () -> { - // TODO due to dubbo's current architecture, - // it will be called from registry protocol in the shutdown process and won't appear in logs - s.undeploy(resourceDef); + return new Runnable() { + @Override + public void run() { + // TODO due to dubbo's current architecture, + // it will be called from registry protocol in the shutdown process and won't appear in logs + s.undeploy(resourceDef); + } }; } @Override protected T doRefer(Class serviceType, URL url) throws RpcException { + if (connectionMonitor == null) { + connectionMonitor = new ConnectionMonitor(); + } // TODO more configs to add PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); @@ -136,10 +146,6 @@ protected T doRefer(Class serviceType, URL url) throws RpcException { connectionManager.setMaxTotal(url.getParameter(Constants.CONNECTIONS_KEY, HTTPCLIENTCONNECTIONMANAGER_MAXTOTAL)); connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, HTTPCLIENTCONNECTIONMANAGER_MAXPERROUTE)); - if (connectionMonitor == null) { - connectionMonitor = new ConnectionMonitor(); - connectionMonitor.start(); - } connectionMonitor.addConnectionManager(connectionManager); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(url.getParameter(Constants.CONNECT_TIMEOUT_KEY, Constants.DEFAULT_CONNECT_TIMEOUT)) @@ -152,17 +158,20 @@ protected T doRefer(Class serviceType, URL url) throws RpcException { .build(); CloseableHttpClient httpClient = HttpClientBuilder.create() - .setKeepAliveStrategy((response, context) -> { - HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE)); - while (it.hasNext()) { - HeaderElement he = it.nextElement(); - String param = he.getName(); - String value = he.getValue(); - if (value != null && param.equalsIgnoreCase(Constants.TIMEOUT_KEY)) { - return Long.parseLong(value) * 1000; + .setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { + @Override + public long getKeepAliveDuration(HttpResponse response, HttpContext context) { + HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE)); + while (it.hasNext()) { + HeaderElement he = it.nextElement(); + String param = he.getName(); + String value = he.getValue(); + if (value != null && param.equalsIgnoreCase(Constants.TIMEOUT_KEY)) { + return Long.parseLong(value) * 1000; + } } + return HTTPCLIENT_KEEPALIVEDURATION; } - return HTTPCLIENT_KEEPALIVEDURATION; }) .setDefaultRequestConfig(requestConfig) .setDefaultSocketConfig(socketConfig) @@ -228,31 +237,14 @@ public void destroy() { clients.clear(); } - /** - * getPath() will return: [contextpath + "/" +] path - * 1. contextpath is empty if user does not set through ProtocolConfig or ProviderConfig - * 2. path will never be empty, it's default value is the interface name. - * - * @return return path only if user has explicitly gave then a value. - */ protected String getContextPath(URL url) { String contextPath = url.getPath(); - if (contextPath != null) { - if (contextPath.equalsIgnoreCase(url.getParameter(Constants.INTERFACE_KEY))) { - return ""; - } - if (contextPath.endsWith(url.getParameter(Constants.INTERFACE_KEY))) { - contextPath = contextPath.substring(0, contextPath.lastIndexOf(url.getParameter(Constants.INTERFACE_KEY))); - } - return contextPath.endsWith("/") ? contextPath.substring(0, contextPath.length() - 1) : contextPath; - } else { - return ""; - } + return contextPath.endsWith("/") ? contextPath.substring(0,contextPath.length()-1) : contextPath; } protected class ConnectionMonitor extends Thread { private volatile boolean shutdown; - private final List connectionManagers = Collections.synchronizedList(new LinkedList<>()); + private final List connectionManagers = Collections.synchronizedList(new LinkedList()); public void addConnectionManager(PoolingHttpClientConnectionManager connectionManager) { connectionManagers.add(connectionManager); diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RpcContextFilter.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RpcContextFilter.java index 735c21b5910..b99bf1dc4eb 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RpcContextFilter.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RpcContextFilter.java @@ -18,6 +18,7 @@ import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.rpc.RpcContext; + import org.jboss.resteasy.spi.ResteasyProviderFactory; import javax.annotation.Priority; @@ -28,7 +29,6 @@ import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.container.ContainerRequestFilter; import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.util.Map; @Priority(Integer.MIN_VALUE + 1) @@ -71,14 +71,14 @@ public void filter(ClientRequestContext requestContext) throws IOException { int size = 0; for (Map.Entry entry : RpcContext.getContext().getAttachments().entrySet()) { String key = entry.getKey(); - String value = entry.getValue(); + String value = entry.getKey(); if (illegalForRest(key) || illegalForRest(value)) { throw new IllegalArgumentException("The attachments of " + RpcContext.class.getSimpleName() + " must not contain ',' or '=' when using rest protocol"); } // TODO for now we don't consider the differences of encoding and server limit if (value != null) { - size += value.getBytes(StandardCharsets.UTF_8).length; + size += value.getBytes("UTF-8").length; } if (size > MAX_HEADER_SIZE) { throw new IllegalArgumentException("The attachments of " + RpcContext.class.getSimpleName() + " is too big"); diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoService.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoService.java index bb9e4a8f1f7..5bcbbd21a67 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoService.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoService.java @@ -16,26 +16,15 @@ */ package org.apache.dubbo.rpc.protocol.rest; -import javax.ws.rs.Consumes; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.MediaType; +public class DemoService implements IDemoService { + @Override + public Integer hello(Integer a, Integer b) { + return a + b; + } -@Path("/demoService") -public interface DemoService { - @GET - @Path("/hello") - Integer hello(@QueryParam("a") Integer a, @QueryParam("b") Integer b); - - @GET - @Path("/error") - String error(); - - @POST - @Path("/say") - @Consumes({MediaType.TEXT_PLAIN}) - String sayHello(String name); + @Override + public String error() { + throw new RuntimeException(); + } } diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/StateListener.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/IDemoService.java similarity index 70% rename from dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/StateListener.java rename to dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/IDemoService.java index 43580837395..47dbfb76cba 100644 --- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/StateListener.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/IDemoService.java @@ -14,14 +14,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.remoting.etcd; +package org.apache.dubbo.rpc.protocol.rest; -public interface StateListener { +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.QueryParam; - int DISCONNECTED = 0; - - int CONNECTED = 1; - - void stateChanged(int connected); +@Path("/demoService") +public interface IDemoService { + @GET + @Path("/hello") + Integer hello(@QueryParam("a") Integer a, @QueryParam("b") Integer b); + @GET + @Path("/error") + String error(); } diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java index 0e1b5e8fcb5..35b78b6ddeb 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java @@ -21,7 +21,6 @@ import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.rpc.Exporter; -import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Protocol; import org.apache.dubbo.rpc.ProxyFactory; import org.apache.dubbo.rpc.Result; @@ -32,73 +31,33 @@ import org.apache.dubbo.rpc.model.ProviderModel; import org.hamcrest.CoreMatchers; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Test; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; public class RestProtocolTest { private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension("rest"); private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); private final int availablePort = NetUtils.getAvailablePort(); - private final URL exportUrl = URL.valueOf("rest://127.0.0.1:" + availablePort + "/rest?interface=org.apache.dubbo.rpc.protocol.rest.DemoService"); + private final URL exportUrl = URL.valueOf("rest://127.0.0.1:" + availablePort + "/rest"); - @AfterEach + @After public void tearDown() { protocol.destroy(); } - @Test - public void testRestProtocol() { - URL url = URL.valueOf("rest://127.0.0.1:5342/rest/say?version=1.0.0&interface=org.apache.dubbo.rpc.protocol.rest.DemoService"); - DemoServiceImpl server = new DemoServiceImpl(); - ProviderModel providerModel = new ProviderModel(url.getPathKey(), "", "", server, DemoService.class); - ApplicationModel.initProviderModel(url.getPathKey(), providerModel); - - Exporter exporter = protocol.export(proxy.getInvoker(server, DemoService.class, url)); - Invoker invoker = protocol.refer(DemoService.class, url); - Assertions.assertFalse(server.isCalled()); - - DemoService client = proxy.getProxy(invoker); - String result = client.sayHello("haha"); - Assertions.assertTrue(server.isCalled()); - Assertions.assertEquals("Hello, haha", result); - invoker.destroy(); - exporter.unexport(); - } - - @Test - public void testRestProtocolWithContextPath() { - DemoServiceImpl server = new DemoServiceImpl(); - Assertions.assertFalse(server.isCalled()); - URL url = URL.valueOf("rest://127.0.0.1:5341/a/b/c?version=1.0.0&interface=org.apache.dubbo.rpc.protocol.rest.DemoService"); - ProviderModel providerModel = new ProviderModel(url.getPathKey(), "", "", server, DemoService.class); - ApplicationModel.initProviderModel(url.getPathKey(), providerModel); - - Exporter exporter = protocol.export(proxy.getInvoker(server, DemoService.class, url)); - - url = URL.valueOf("rest://127.0.0.1:5341/a/b/c/?version=1.0.0&interface=org.apache.dubbo.rpc.protocol.rest.DemoService"); - Invoker invoker = protocol.refer(DemoService.class, url); - DemoService client = proxy.getProxy(invoker); - String result = client.sayHello("haha"); - Assertions.assertTrue(server.isCalled()); - Assertions.assertEquals("Hello, haha", result); - invoker.destroy(); - exporter.unexport(); - } - @Test public void testExport() { - DemoService server = new DemoServiceImpl(); - ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), "", "", server, DemoService.class); + IDemoService server = new DemoService(); + ProviderModel providerModel = new ProviderModel(exportUrl.getServiceInterface(), "Dubbo", "1.0.0", server, IDemoService.class); ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel); RpcContext.getContext().setAttachment("timeout", "200"); - Exporter exporter = protocol.export(proxy.getInvoker(server, DemoService.class, exportUrl)); + Exporter exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, exportUrl)); - DemoService demoService = this.proxy.getProxy(protocol.refer(DemoService.class, exportUrl)); + IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, exportUrl)); Integer echoString = demoService.hello(1, 2); assertThat(echoString, is(3)); @@ -108,14 +67,14 @@ public void testExport() { @Test public void testNettyServer() { - DemoService server = new DemoServiceImpl(); - ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), "", "", server, DemoService.class); + IDemoService server = new DemoService(); + ProviderModel providerModel = new ProviderModel(exportUrl.getServiceInterface(), "Dubbo", "1.0.0", server, IDemoService.class); ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel); URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty"); - Exporter exporter = protocol.export(proxy.getInvoker(new DemoServiceImpl(), DemoService.class, nettyUrl)); + Exporter exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl)); - DemoService demoService = this.proxy.getProxy(protocol.refer(DemoService.class, nettyUrl)); + IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl)); Integer echoString = demoService.hello(10, 10); assertThat(echoString, is(20)); @@ -123,43 +82,39 @@ public void testNettyServer() { exporter.unexport(); } - @Test + @Test(expected = RpcException.class) public void testServletWithoutWebConfig() { - Assertions.assertThrows(RpcException.class, () -> { - DemoService server = new DemoServiceImpl(); - ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), "", "", server, DemoService.class); - ApplicationModel.initProviderModel(exportUrl.getPathKey(), providerModel); + IDemoService server = new DemoService(); + ProviderModel providerModel = new ProviderModel(exportUrl.getServiceInterface(), "Dubbo", "1.0.0", server, IDemoService.class); + ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel); - URL servletUrl = exportUrl.addParameter(Constants.SERVER_KEY, "servlet"); + URL servletUrl = exportUrl.addParameter(Constants.SERVER_KEY, "servlet"); - protocol.export(proxy.getInvoker(server, DemoService.class, servletUrl)); - }); + protocol.export(proxy.getInvoker(server, IDemoService.class, servletUrl)); } - @Test + @Test(expected = RpcException.class) public void testErrorHandler() { - Assertions.assertThrows(RpcException.class, () -> { - DemoService server = new DemoServiceImpl(); - ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), "", "", server, DemoService.class); - ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel); + IDemoService server = new DemoService(); + ProviderModel providerModel = new ProviderModel(exportUrl.getServiceInterface(), "Dubbo", "1.0.0", server, IDemoService.class); + ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel); - URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty"); - Exporter exporter = protocol.export(proxy.getInvoker(server, DemoService.class, nettyUrl)); + URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty"); + Exporter exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, nettyUrl)); - DemoService demoService = this.proxy.getProxy(protocol.refer(DemoService.class, nettyUrl)); + IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl)); - demoService.error(); - }); + demoService.error(); } @Test public void testInvoke() { - DemoService server = new DemoServiceImpl(); - ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), "", "", server, DemoService.class); + IDemoService server = new DemoService(); + ProviderModel providerModel = new ProviderModel(exportUrl.getServiceInterface(), "Dubbo", "1.0.0", server, IDemoService.class); ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel); - Exporter exporter = protocol.export(proxy.getInvoker(server, DemoService.class, exportUrl)); + Exporter exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, exportUrl)); RpcInvocation rpcInvocation = new RpcInvocation("hello", new Class[]{Integer.class, Integer.class}, new Integer[]{2, 3}); @@ -169,15 +124,15 @@ public void testInvoke() { @Test public void testFilter() { - DemoService server = new DemoServiceImpl(); - ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), "", "", server, DemoService.class); + IDemoService server = new DemoService(); + ProviderModel providerModel = new ProviderModel(exportUrl.getServiceInterface(), "Dubbo", "1.0.0", server, IDemoService.class); ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel); URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty") .addParameter(Constants.EXTENSION_KEY, "org.apache.dubbo.rpc.protocol.rest.support.LoggingFilter"); - Exporter exporter = protocol.export(proxy.getInvoker(server, DemoService.class, nettyUrl)); + Exporter exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, nettyUrl)); - DemoService demoService = this.proxy.getProxy(protocol.refer(DemoService.class, nettyUrl)); + IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl)); Integer result = demoService.hello(1, 2); @@ -188,16 +143,16 @@ public void testFilter() { @Test public void testRpcContextFilter() { - DemoService server = new DemoServiceImpl(); - ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), "", "", server, DemoService.class); + IDemoService server = new DemoService(); + ProviderModel providerModel = new ProviderModel(exportUrl.getServiceInterface(), "Dubbo", "1.0.0", server, IDemoService.class); ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel); // use RpcContextFilter URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty") .addParameter(Constants.EXTENSION_KEY, "org.apache.dubbo.rpc.protocol.rest.RpcContextFilter"); - Exporter exporter = protocol.export(proxy.getInvoker(server, DemoService.class, nettyUrl)); + Exporter exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, nettyUrl)); - DemoService demoService = this.proxy.getProxy(protocol.refer(DemoService.class, nettyUrl)); + IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl)); String value = null; // put a null value into attachment. @@ -209,20 +164,18 @@ public void testRpcContextFilter() { exporter.unexport(); } - @Test + @Test(expected = RuntimeException.class) public void testRegFail() { - Assertions.assertThrows(RuntimeException.class, () -> { - DemoService server = new DemoServiceImpl(); - ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), "", "", server, DemoService.class); - ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel); - - URL nettyUrl = exportUrl.addParameter(Constants.EXTENSION_KEY, "com.not.existing.Filter"); - protocol.export(proxy.getInvoker(server, DemoService.class, nettyUrl)); - }); + IDemoService server = new DemoService(); + ProviderModel providerModel = new ProviderModel(exportUrl.getServiceInterface(), "Dubbo", "1.0.0", server, IDemoService.class); + ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel); + + URL nettyUrl = exportUrl.addParameter(Constants.EXTENSION_KEY, "com.not.existing.Filter"); + protocol.export(proxy.getInvoker(server, IDemoService.class, nettyUrl)); } @Test public void testDefaultPort() { assertThat(protocol.getDefaultPort(), is(80)); } -} +} \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RpcExceptionMapperTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RpcExceptionMapperTest.java index 74a133a45af..25f763ea85a 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RpcExceptionMapperTest.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RpcExceptionMapperTest.java @@ -17,8 +17,8 @@ package org.apache.dubbo.rpc.protocol.rest; import org.apache.dubbo.rpc.RpcException; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.Before; +import org.junit.Test; import org.mockito.Answers; import org.mockito.internal.util.collections.Sets; @@ -29,7 +29,7 @@ import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -37,7 +37,7 @@ public class RpcExceptionMapperTest { private RpcExceptionMapper exceptionMapper; - @BeforeEach + @Before public void setUp() { this.exceptionMapper = new RpcExceptionMapper(); } diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/integration/swagger/DubboSwaggerApiListingResourceTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/integration/swagger/DubboSwaggerApiListingResourceTest.java index 15e685611ed..8d819b254cb 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/integration/swagger/DubboSwaggerApiListingResourceTest.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/integration/swagger/DubboSwaggerApiListingResourceTest.java @@ -18,8 +18,8 @@ import io.swagger.models.Swagger; import org.jboss.resteasy.spi.ResteasyUriInfo; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; @@ -55,10 +55,10 @@ public void test() throws Exception { Response response = resource.getListingJson(app, sc, null, new ResteasyUriInfo(new URI("http://rest.test"))); - Assertions.assertNotNull(response); + Assert.assertNotNull(response); Swagger swagger = (Swagger)response.getEntity(); - Assertions.assertEquals("SwaggerService",swagger.getTags().get(0).getName()); - Assertions.assertEquals("/demoService/hello",swagger.getPaths().keySet().toArray()[0].toString()); + Assert.assertEquals("SwaggerService",swagger.getTags().get(0).getName()); + Assert.assertEquals("/demoService/hello",swagger.getPaths().keySet().toArray()[0].toString()); } } diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestProtocolTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestProtocolTest.java new file mode 100644 index 00000000000..81b2f79ff81 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestProtocolTest.java @@ -0,0 +1,78 @@ +/* + * 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 org.apache.dubbo.rpc.protol.rest; + +import junit.framework.Assert; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.extension.ExtensionLoader; +import org.apache.dubbo.rpc.Exporter; +import org.apache.dubbo.rpc.Invoker; +import org.apache.dubbo.rpc.Protocol; +import org.apache.dubbo.rpc.ProxyFactory; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.ProviderModel; + +import org.junit.Test; + +/** + * RestProtocolTest + */ +public class RestProtocolTest { + + private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); + private ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); + + @Test + public void testRestProtocol() { + URL url = URL.valueOf("rest://127.0.0.1:5342/rest/say1?version=1.0.0"); + RestServiceImpl server = new RestServiceImpl(); + ProviderModel providerModel = new ProviderModel(url.getServiceInterface(), "Dubbo", "1.0.0", server, RestService.class); + ApplicationModel.initProviderModel(url.getServiceKey(), providerModel); + + Exporter exporter = protocol.export(proxyFactory.getInvoker(server, RestService.class, url)); + Invoker invoker = protocol.refer(RestService.class, url); + Assert.assertFalse(server.isCalled()); + + RestService client = proxyFactory.getProxy(invoker); + String result = client.sayHello("haha"); + Assert.assertTrue(server.isCalled()); + Assert.assertEquals("Hello, haha", result); + invoker.destroy(); + exporter.unexport(); + } + + @Test + public void testRestProtocolWithContextPath() { + RestServiceImpl server = new RestServiceImpl(); + Assert.assertFalse(server.isCalled()); + URL url = URL.valueOf("rest://127.0.0.1:5341/a/b/c?version=1.0.0"); + ProviderModel providerModel = new ProviderModel(url.getServiceInterface(), "Dubbo", "1.0.0", server, RestService.class); + ApplicationModel.initProviderModel(url.getServiceKey(), providerModel); + + Exporter exporter = protocol.export(proxyFactory.getInvoker(server, RestService.class, url)); + + url = URL.valueOf("rest://127.0.0.1:5341/a/b/c/?version=1.0.0"); + Invoker invoker = protocol.refer(RestService.class, url); + RestService client = proxyFactory.getProxy(invoker); + String result = client.sayHello("haha"); + Assert.assertTrue(server.isCalled()); + Assert.assertEquals("Hello, haha", result); + invoker.destroy(); + exporter.unexport(); + } +} diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/option/Constants.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestService.java similarity index 69% rename from dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/option/Constants.java rename to dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestService.java index c935808b095..fe0d5884408 100644 --- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/option/Constants.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestService.java @@ -1,30 +1,36 @@ -/* - * 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 org.apache.dubbo.remoting.etcd.option; - -/** - * Etcd registry constants. - */ -public class Constants extends org.apache.dubbo.common.Constants { - - public static final String HTTP_SUBFIX_KEY = "://"; - - public static final String HTTP_KEY = "http://"; - - public static final int DEFAULT_KEEPALIVE_TIMEOUT = DEFAULT_SESSION_TIMEOUT / 2; - -} +/* + * 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 org.apache.dubbo.rpc.protol.rest; + + +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.core.MediaType; + +/** + * RestService + */ +@Path("/rest") +public interface RestService { + + @POST + @Path("/say1") + @Consumes({MediaType.TEXT_PLAIN}) + String sayHello(String name); + +} diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoServiceImpl.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestServiceImpl.java similarity index 78% rename from dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoServiceImpl.java rename to dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestServiceImpl.java index 37ec29a6d4e..e9d31dcac35 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoServiceImpl.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestServiceImpl.java @@ -1,42 +1,36 @@ -/* - * 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 org.apache.dubbo.rpc.protocol.rest; - - -public class DemoServiceImpl implements DemoService { - private boolean called; - - public String sayHello(String name) { - called = true; - return "Hello, " + name; - } - - - public boolean isCalled() { - return called; - } - - @Override - public Integer hello(Integer a, Integer b) { - return a + b; - } - - @Override - public String error() { - throw new RuntimeException(); - } -} +/* + * 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 org.apache.dubbo.rpc.protol.rest; + +/** + * RestServiceImpl + */ + +public class RestServiceImpl implements RestService { + + private boolean called; + + public String sayHello(String name) { + called = true; + return "Hello, " + name; + } + + + public boolean isCalled() { + return called; + } +} diff --git a/dubbo-rpc/dubbo-rpc-rmi/pom.xml b/dubbo-rpc/dubbo-rpc-rmi/pom.xml index bb5efb4ed33..60dd87bf4d7 100644 --- a/dubbo-rpc/dubbo-rpc-rmi/pom.xml +++ b/dubbo-rpc/dubbo-rpc-rmi/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-rpc - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-rpc-rmi jar diff --git a/dubbo-rpc/dubbo-rpc-rmi/src/main/java/org/apache/dubbo/rpc/protocol/rmi/RmiProtocol.java b/dubbo-rpc/dubbo-rpc-rmi/src/main/java/org/apache/dubbo/rpc/protocol/rmi/RmiProtocol.java index 63e9fcdfff1..d3ba2e8fde0 100644 --- a/dubbo-rpc/dubbo-rpc-rmi/src/main/java/org/apache/dubbo/rpc/protocol/rmi/RmiProtocol.java +++ b/dubbo-rpc/dubbo-rpc-rmi/src/main/java/org/apache/dubbo/rpc/protocol/rmi/RmiProtocol.java @@ -20,12 +20,10 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.protocol.AbstractProxyProtocol; -import org.apache.dubbo.rpc.service.GenericService; -import org.apache.dubbo.rpc.support.ProtocolUtils; + import org.springframework.remoting.RemoteAccessException; import org.springframework.remoting.rmi.RmiProxyFactoryBean; import org.springframework.remoting.rmi.RmiServiceExporter; -import org.springframework.remoting.support.RemoteInvocation; import java.io.IOException; import java.net.SocketTimeoutException; @@ -52,14 +50,21 @@ public int getDefaultPort() { @Override protected Runnable doExport(final T impl, Class type, URL url) throws RpcException { - RmiServiceExporter rmiServiceExporter = createExporter(impl, type, url, false); - RmiServiceExporter genericServiceExporter = createExporter(impl, GenericService.class, url, true); + final RmiServiceExporter rmiServiceExporter = new RmiServiceExporter(); + rmiServiceExporter.setRegistryPort(url.getPort()); + rmiServiceExporter.setServiceName(url.getPath()); + rmiServiceExporter.setServiceInterface(type); + rmiServiceExporter.setService(impl); + try { + rmiServiceExporter.afterPropertiesSet(); + } catch (RemoteException e) { + throw new RpcException(e.getMessage(), e); + } return new Runnable() { @Override public void run() { try { rmiServiceExporter.destroy(); - genericServiceExporter.destroy(); } catch (Throwable e) { logger.warn(e.getMessage(), e); } @@ -71,8 +76,6 @@ public void run() { @SuppressWarnings("unchecked") protected T doRefer(final Class serviceType, final URL url) throws RpcException { final RmiProxyFactoryBean rmiProxyFactoryBean = new RmiProxyFactoryBean(); - final String generic = url.getParameter(Constants.GENERIC_KEY); - final boolean isGeneric = ProtocolUtils.isGeneric(generic) || serviceType.equals(GenericService.class); /* RMI needs extra parameter since it uses customized remote invocation object @@ -83,27 +86,11 @@ protected T doRefer(final Class serviceType, final URL url) throws RpcExc 3. if the provider version is lower than v2.6.3, does not use customized RemoteInvocation. */ if (isRelease270OrHigher(url.getParameter(Constants.RELEASE_KEY))) { - rmiProxyFactoryBean.setRemoteInvocationFactory((methodInvocation) -> { - RemoteInvocation invocation = new RmiRemoteInvocation(methodInvocation); - if (invocation != null && isGeneric) { - invocation.addAttribute(Constants.GENERIC_KEY, generic); - } - return invocation; - }); + rmiProxyFactoryBean.setRemoteInvocationFactory(RmiRemoteInvocation::new); } else if (isRelease263OrHigher(url.getParameter(Constants.DUBBO_VERSION_KEY))) { - rmiProxyFactoryBean.setRemoteInvocationFactory((methodInvocation) -> { - RemoteInvocation invocation = new com.alibaba.dubbo.rpc.protocol.rmi.RmiRemoteInvocation(methodInvocation); - if (invocation != null && isGeneric) { - invocation.addAttribute(Constants.GENERIC_KEY, generic); - } - return invocation; - }); - } - String serviceUrl = url.toIdentityString(); - if (isGeneric) { - serviceUrl = serviceUrl + "/" + Constants.GENERIC_KEY; + rmiProxyFactoryBean.setRemoteInvocationFactory(com.alibaba.dubbo.rpc.protocol.rmi.RmiRemoteInvocation::new); } - rmiProxyFactoryBean.setServiceUrl(serviceUrl); + rmiProxyFactoryBean.setServiceUrl(url.toIdentityString()); rmiProxyFactoryBean.setServiceInterface(serviceType); rmiProxyFactoryBean.setCacheStub(true); rmiProxyFactoryBean.setLookupStubOnStartup(true); @@ -130,22 +117,4 @@ protected int getErrorCode(Throwable e) { return super.getErrorCode(e); } - private RmiServiceExporter createExporter(T impl, Class type, URL url, boolean isGeneric) { - final RmiServiceExporter rmiServiceExporter = new RmiServiceExporter(); - rmiServiceExporter.setRegistryPort(url.getPort()); - if (isGeneric) { - rmiServiceExporter.setServiceName(url.getPath() + "/" + Constants.GENERIC_KEY); - } else { - rmiServiceExporter.setServiceName(url.getPath()); - } - rmiServiceExporter.setServiceInterface(type); - rmiServiceExporter.setService(impl); - try { - rmiServiceExporter.afterPropertiesSet(); - } catch (RemoteException e) { - throw new RpcException(e.getMessage(), e); - } - return rmiServiceExporter; - } - } diff --git a/dubbo-rpc/dubbo-rpc-rmi/src/main/java/org/apache/dubbo/rpc/protocol/rmi/RmiRemoteInvocation.java b/dubbo-rpc/dubbo-rpc-rmi/src/main/java/org/apache/dubbo/rpc/protocol/rmi/RmiRemoteInvocation.java index 96b49e7e493..dbbe9d66867 100644 --- a/dubbo-rpc/dubbo-rpc-rmi/src/main/java/org/apache/dubbo/rpc/protocol/rmi/RmiRemoteInvocation.java +++ b/dubbo-rpc/dubbo-rpc-rmi/src/main/java/org/apache/dubbo/rpc/protocol/rmi/RmiRemoteInvocation.java @@ -16,8 +16,6 @@ */ package org.apache.dubbo.rpc.protocol.rmi; -import org.apache.dubbo.common.Constants; -import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.rpc.RpcContext; import org.aopalliance.intercept.MethodInvocation; @@ -36,7 +34,7 @@ public class RmiRemoteInvocation extends RemoteInvocation { */ public RmiRemoteInvocation(MethodInvocation methodInvocation) { super(methodInvocation); - addAttribute(dubboAttachmentsAttrName, new HashMap<>(RpcContext.getContext().getAttachments())); + addAttribute(dubboAttachmentsAttrName, new HashMap(RpcContext.getContext().getAttachments())); } /** @@ -50,10 +48,6 @@ public Object invoke(Object targetObject) throws NoSuchMethodException, IllegalA InvocationTargetException { RpcContext context = RpcContext.getContext(); context.setAttachments((Map) getAttribute(dubboAttachmentsAttrName)); - String generic = (String) getAttribute(Constants.GENERIC_KEY); - if (StringUtils.isNotEmpty(generic)) { - context.setAttachment(Constants.GENERIC_KEY, generic); - } try { return super.invoke(targetObject); } finally { diff --git a/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/DemoService.java b/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/DemoService.java index c139b1afc28..9e1d334fbcf 100644 --- a/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/DemoService.java +++ b/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/DemoService.java @@ -23,8 +23,6 @@ public interface DemoService { void sayHello(String name); - String sayHi(String name); - String echo(String text); long timestamp(); diff --git a/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/DemoServiceImpl.java b/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/DemoServiceImpl.java index ec461eb6f11..833969b2431 100644 --- a/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/DemoServiceImpl.java +++ b/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/DemoServiceImpl.java @@ -31,10 +31,6 @@ public void sayHello(String name) { System.out.println("hello " + name); } - public String sayHi(String name) { - return "Hi, " + name; - } - public String echo(String text) { return text; } diff --git a/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/RmiProtocolTest.java b/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/RmiProtocolTest.java index 4dcd21dfccb..10e1d6be125 100644 --- a/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/RmiProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/RmiProtocolTest.java @@ -23,15 +23,12 @@ import org.apache.dubbo.rpc.Protocol; import org.apache.dubbo.rpc.ProxyFactory; import org.apache.dubbo.rpc.RpcException; -import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.service.EchoService; -import org.apache.dubbo.rpc.service.GenericService; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; +import org.junit.Ignore; +import org.junit.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; public class RmiProtocolTest { private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); @@ -92,7 +89,7 @@ public void testRmiProtocol() throws Exception { } // FIXME RMI protocol doesn't support casting to EchoService yet. - @Disabled + @Ignore @Test public void testRmiProtocol_echoService() throws Exception { DemoService service = new DemoServiceImpl(); @@ -118,19 +115,6 @@ public void testRmiProtocol_echoService() throws Exception { rpcExporter.unexport(); } - @Test - public void testGenericInvoke() { - DemoService service = new DemoServiceImpl(); - URL url = URL.valueOf("rmi://127.0.0.1:9003/" + DemoService.class.getName() + "?release=2.7.0"); - Exporter exporter = protocol.export(proxy.getInvoker(service, DemoService.class, url)); - Invoker invoker = protocol.refer(GenericService.class, url); - GenericService client = proxy.getProxy(invoker, true); - String result = (String) client.$invoke("sayHi", new String[]{"java.lang.String"}, new Object[]{"haha"}); - Assertions.assertEquals("Hi, haha", result); - invoker.destroy(); - exporter.unexport(); - } - public static interface NonStdRmiInterface { void bark(); } diff --git a/dubbo-rpc/dubbo-rpc-rsocket/src/main/java/org/apache/dubbo/rpc/protocol/rsocket/RSocketProtocol.java b/dubbo-rpc/dubbo-rpc-rsocket/src/main/java/org/apache/dubbo/rpc/protocol/rsocket/RSocketProtocol.java index 4514823a802..be537eb48ba 100644 --- a/dubbo-rpc/dubbo-rpc-rsocket/src/main/java/org/apache/dubbo/rpc/protocol/rsocket/RSocketProtocol.java +++ b/dubbo-rpc/dubbo-rpc-rsocket/src/main/java/org/apache/dubbo/rpc/protocol/rsocket/RSocketProtocol.java @@ -16,6 +16,16 @@ */ package org.apache.dubbo.rpc.protocol.rsocket; +import io.rsocket.AbstractRSocket; +import io.rsocket.ConnectionSetupPayload; +import io.rsocket.Payload; +import io.rsocket.RSocket; +import io.rsocket.RSocketFactory; +import io.rsocket.SocketAcceptor; +import io.rsocket.transport.netty.client.TcpClientTransport; +import io.rsocket.transport.netty.server.CloseableChannel; +import io.rsocket.transport.netty.server.TcpServerTransport; +import io.rsocket.util.DefaultPayload; import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.extension.ExtensionLoader; @@ -37,17 +47,6 @@ import org.apache.dubbo.rpc.RpcInvocation; import org.apache.dubbo.rpc.protocol.AbstractProtocol; import org.apache.dubbo.rpc.support.RpcUtils; - -import io.rsocket.AbstractRSocket; -import io.rsocket.ConnectionSetupPayload; -import io.rsocket.Payload; -import io.rsocket.RSocket; -import io.rsocket.RSocketFactory; -import io.rsocket.SocketAcceptor; -import io.rsocket.transport.netty.client.TcpClientTransport; -import io.rsocket.transport.netty.server.CloseableChannel; -import io.rsocket.transport.netty.server.TcpServerTransport; -import io.rsocket.util.DefaultPayload; import org.reactivestreams.Publisher; import reactor.core.Exceptions; import reactor.core.publisher.Flux; @@ -159,7 +158,7 @@ private CloseableChannel createServer(URL url) { String bindIp = url.getParameter(Constants.BIND_IP_KEY, url.getHost()); int bindPort = url.getParameter(Constants.BIND_PORT_KEY, url.getPort()); if (url.getParameter(Constants.ANYHOST_KEY, false) || NetUtils.isInvalidLocalHost(bindIp)) { - bindIp = Constants.ANYHOST_VALUE; + bindIp = NetUtils.ANYHOST; } return RSocketFactory.receive() .acceptor(new SocketAcceptorImpl(bindPort)) diff --git a/dubbo-rpc/dubbo-rpc-rsocket/src/test/java/org/apache/dubbo/rpc/protocol/rsocket/RSocketProtocolTest.java b/dubbo-rpc/dubbo-rpc-rsocket/src/test/java/org/apache/dubbo/rpc/protocol/rsocket/RSocketProtocolTest.java index 4d834a111cb..d9733eb4676 100644 --- a/dubbo-rpc/dubbo-rpc-rsocket/src/test/java/org/apache/dubbo/rpc/protocol/rsocket/RSocketProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-rsocket/src/test/java/org/apache/dubbo/rpc/protocol/rsocket/RSocketProtocolTest.java @@ -26,9 +26,8 @@ import org.apache.dubbo.rpc.service.EchoService; import org.apache.dubbo.rpc.service.RemoteService; import org.apache.dubbo.rpc.service.RemoteServiceImpl; - -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Test; +import org.junit.AfterClass; +import org.junit.Test; import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -39,14 +38,14 @@ import java.util.function.Consumer; import java.util.function.Function; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.Assert.assertEquals; public class RSocketProtocolTest { private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); - @AfterAll + @AfterClass public static void after() { RSocketProtocol.getRSocketProtocol().destroy(); } diff --git a/dubbo-rpc/dubbo-rpc-thrift/pom.xml b/dubbo-rpc/dubbo-rpc-thrift/pom.xml index 5e106b2ba3c..78e619b641d 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/pom.xml +++ b/dubbo-rpc/dubbo-rpc-thrift/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-rpc - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-rpc-thrift jar diff --git a/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodec.java b/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodec.java index 58058f4f64f..02c0699af2c 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodec.java +++ b/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodec.java @@ -66,12 +66,11 @@ *

    * header fields in version 1 *

      - *
    1. string - service name
    2. - *
    3. long - dubbo request id
    4. + *
    5. string - service name
    6. + *
    7. long - dubbo request id
    8. *
    *

    */ - /** * @since 2.7.0, use https://github.com/dubbo/dubbo-rpc-native-thrift instead */ @@ -109,7 +108,7 @@ public void encode(Channel channel, ChannelBuffer buffer, Object message) } else if (message instanceof Response) { encodeResponse(channel, buffer, (Response) message); } else { - throw new UnsupportedOperationException("Thrift codec only support encode " + throw new UnsupportedOperationException("Thrift codec only support encode " + Request.class.getName() + " and " + Response.class.getName()); } @@ -163,7 +162,6 @@ private Object decode(TProtocol protocol) // version String serviceName; - String path; long id; TMessage message; @@ -172,7 +170,6 @@ private Object decode(TProtocol protocol) protocol.readI16(); protocol.readByte(); serviceName = protocol.readString(); - path = protocol.readString(); id = protocol.readI64(); message = protocol.readMessageBegin(); } catch (TException e) { @@ -183,7 +180,6 @@ private Object decode(TProtocol protocol) RpcInvocation result = new RpcInvocation(); result.setAttachment(Constants.INTERFACE_KEY, serviceName); - result.setAttachment(Constants.PATH_KEY, path); result.setMethodName(message.name); String argsClassName = ExtensionLoader.getExtensionLoader(ClassNameGenerator.class) @@ -303,7 +299,7 @@ private Object decode(TProtocol protocol) .getExtension(ThriftClassNameGenerator.NAME).generateResultClassName(serviceName, message.name); if (StringUtils.isEmpty(resultClassName)) { - throw new IllegalArgumentException("Could not infer service result class name from service name " + throw new IllegalArgumentException("Could not infer service result class name from service name " + serviceName + ", the service name you specified may not generated by thrift idl compiler"); } @@ -405,7 +401,7 @@ private void encodeRequest(Channel channel, ChannelBuffer buffer, Request reques String serviceName = inv.getAttachment(Constants.INTERFACE_KEY); if (StringUtils.isEmpty(serviceName)) { - throw new IllegalArgumentException("Could not find service name in attachment with key " + throw new IllegalArgumentException("Could not find service name in attachment with key " + Constants.INTERFACE_KEY); } @@ -499,8 +495,6 @@ private void encodeRequest(Channel channel, ChannelBuffer buffer, Request reques protocol.writeByte(VERSION); // service name protocol.writeString(serviceName); - // path - protocol.writeString(inv.getAttachment(Constants.PATH_KEY)); // dubbo request id protocol.writeI64(request.getId()); protocol.getTransport().flush(); @@ -673,7 +667,6 @@ private void encodeResponse(Channel channel, ChannelBuffer buffer, Response resp case TMessageType.REPLY: resultObj.write(protocol); break; - default: } protocol.writeMessageEnd(); protocol.getTransport().flush(); diff --git a/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftProtocol.java b/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftProtocol.java index 055af6f8bb3..958d8e63234 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftProtocol.java +++ b/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftProtocol.java @@ -63,9 +63,9 @@ public CompletableFuture reply(ExchangeChannel channel, Object msg) thro if (msg instanceof Invocation) { Invocation inv = (Invocation) msg; - String path = inv.getAttachments().get(Constants.PATH_KEY); + String serviceName = inv.getAttachments().get(Constants.INTERFACE_KEY); String serviceKey = serviceKey(channel.getLocalAddress().getPort(), - path, null, null); + serviceName, null, null); DubboExporter exporter = (DubboExporter) exporterMap.get(serviceKey); if (exporter == null) { throw new RemotingException(channel, diff --git a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/ClassNameTest.java b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/ClassNameTest.java index c7d1050676c..f383501e2ca 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/ClassNameTest.java +++ b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/ClassNameTest.java @@ -17,27 +17,27 @@ import org.apache.dubbo.rpc.protocol.thrift.ThriftUtils; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; public class ClassNameTest { @Test public void testThriftUtils() { - Assertions.assertEquals($__ClassNameTestDubboStub.echo_args.class.getName(), + Assert.assertEquals($__ClassNameTestDubboStub.echo_args.class.getName(), ThriftUtils.generateMethodArgsClassName( ClassNameTestDubbo.class.getName(), "echo")); - Assertions.assertEquals($__ClassNameTestDubboStub.echo_result.class.getName(), + Assert.assertEquals($__ClassNameTestDubboStub.echo_result.class.getName(), ThriftUtils.generateMethodResultClassName( ClassNameTestDubbo.class.getName(), "echo")); - Assertions.assertEquals(ClassNameTestThrift.echo_args.class.getName(), + Assert.assertEquals(ClassNameTestThrift.echo_args.class.getName(), ThriftUtils.generateMethodArgsClassNameThrift( ClassNameTestThrift.Iface.class.getName(), "echo")); - Assertions.assertEquals(ClassNameTestThrift.echo_result.class.getName(), + Assert.assertEquals(ClassNameTestThrift.echo_result.class.getName(), ThriftUtils.generateMethodResultClassNameThrift( ClassNameTestThrift.Iface.class.getName(), "echo")); diff --git a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/AbstractTest.java b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/AbstractTest.java index 234afa563a9..5db52ad934c 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/AbstractTest.java +++ b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/AbstractTest.java @@ -32,8 +32,8 @@ import org.apache.thrift.transport.TServerSocket; import org.apache.thrift.transport.TServerTransport; import org.apache.thrift.transport.TTransportFactory; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; +import org.junit.After; +import org.junit.Before; public abstract class AbstractTest { @@ -138,12 +138,12 @@ protected URL getUrl() { "thrift://127.0.0.1:" + PORT + "/" + getInterface().getName()); } - @AfterEach + @After public void tearDown() throws Exception { destroy(); } - @BeforeEach + @Before public void setUp() throws Exception { init(); } diff --git a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ServerExceptionTest.java b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ServerExceptionTest.java index fc9c2a4975f..5414a0d0abe 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ServerExceptionTest.java +++ b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ServerExceptionTest.java @@ -22,8 +22,8 @@ import org.apache.dubbo.rpc.RpcInvocation; import org.apache.dubbo.rpc.gen.dubbo.$__DemoStub; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; public class ServerExceptionTest extends AbstractTest { @@ -69,25 +69,24 @@ public String echoString(String arg) { } - @Test + @Test(expected = RpcException.class) public void testServerException() throws Exception { - Assertions.assertThrows(RpcException.class, () -> { - Assertions.assertNotNull(invoker); - RpcInvocation invocation = new RpcInvocation(); + Assert.assertNotNull(invoker); - invocation.setMethodName("echoString"); + RpcInvocation invocation = new RpcInvocation(); - invocation.setParameterTypes(new Class[]{String.class}); + invocation.setMethodName("echoString"); - String arg = "Hello, World!"; + invocation.setParameterTypes(new Class[]{String.class}); - invocation.setArguments(new Object[]{arg}); + String arg = "Hello, World!"; - Result result = invoker.invoke(invocation); + invocation.setArguments(new Object[]{arg}); - System.out.println(result); - }); + Result result = invoker.invoke(invocation); + + System.out.println(result); } diff --git a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ServiceMethodNotFoundTest.java b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ServiceMethodNotFoundTest.java index fffe6c8322f..20283416725 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ServiceMethodNotFoundTest.java +++ b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ServiceMethodNotFoundTest.java @@ -25,9 +25,9 @@ import org.apache.thrift.server.TThreadPoolServer; import org.apache.thrift.transport.TServerSocket; import org.apache.thrift.transport.TServerTransport; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import java.lang.reflect.Field; import java.util.Map; @@ -87,7 +87,7 @@ public void run() { } - @BeforeEach + @Before public void setUp() throws Exception { init(); @@ -98,7 +98,7 @@ public void setUp() throws Exception { } - @AfterEach + @After public void tearDown() throws Exception { destroy(); @@ -122,7 +122,7 @@ public void testServiceMethodNotFound() throws Exception { invoker = protocol.refer( Demo.class, url ); - org.junit.jupiter.api.Assertions.assertNotNull( invoker ); + org.junit.Assert.assertNotNull( invoker ); RpcInvocation invocation = new RpcInvocation(); @@ -138,9 +138,9 @@ public void testServiceMethodNotFound() throws Exception { Result result = invoker.invoke( invocation ); - Assertions.assertNull( result.getResult() ); + Assert.assertNull( result.getResult() ); - Assertions.assertTrue( result.getException() instanceof RpcException );*/ + Assert.assertTrue( result.getException() instanceof RpcException );*/ } diff --git a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodecTest.java b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodecTest.java index c2277f7fba5..a21e3786bd6 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodecTest.java +++ b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodecTest.java @@ -37,8 +37,8 @@ import org.apache.thrift.transport.TFramedTransport; import org.apache.thrift.transport.TIOStreamTransport; import org.apache.thrift.transport.TTransport; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.io.ByteArrayInputStream; @@ -81,22 +81,20 @@ public void testEncodeRequest() throws Exception { } // magic - Assertions.assertEquals(ThriftCodec.MAGIC, protocol.readI16()); + Assert.assertEquals(ThriftCodec.MAGIC, protocol.readI16()); // message length int messageLength = protocol.readI32(); - Assertions.assertEquals(messageLength + 4, bytes.length); + Assert.assertEquals(messageLength + 4, bytes.length); // header length short headerLength = protocol.readI16(); // version - Assertions.assertEquals(ThriftCodec.VERSION, protocol.readByte()); + Assert.assertEquals(ThriftCodec.VERSION, protocol.readByte()); // service name - Assertions.assertEquals(Demo.Iface.class.getName(), protocol.readString()); - // path - Assertions.assertEquals(Demo.Iface.class.getName(), protocol.readString()); + Assert.assertEquals(Demo.Iface.class.getName(), protocol.readString()); // dubbo request id - Assertions.assertEquals(request.getId(), protocol.readI64()); + Assert.assertEquals(request.getId(), protocol.readI64()); // test message header length if (bis.markSupported()) { @@ -112,11 +110,11 @@ public void testEncodeRequest() throws Exception { protocol.readMessageEnd(); - Assertions.assertEquals("echoString", message.name); + Assert.assertEquals("echoString", message.name); - Assertions.assertEquals(TMessageType.CALL, message.type); + Assert.assertEquals(TMessageType.CALL, message.type); - Assertions.assertEquals("Hello, World!", args.getArg()); + Assert.assertEquals("Hello, World!", args.getArg()); } @@ -150,8 +148,6 @@ public void testDecodeReplyResponse() throws Exception { protocol.writeI16(Short.MAX_VALUE); protocol.writeByte(ThriftCodec.VERSION); protocol.writeString(Demo.Iface.class.getName()); - // path - protocol.writeString(Demo.Iface.class.getName()); protocol.writeI64(request.getId()); protocol.getTransport().flush(); headerLength = bos.size(); @@ -179,21 +175,21 @@ public void testDecodeReplyResponse() throws Exception { Object obj = codec.decode((Channel) null, bis); - Assertions.assertNotNull(obj); + Assert.assertNotNull(obj); - Assertions.assertEquals(true, obj instanceof Response); + Assert.assertEquals(true, obj instanceof Response); Response response = (Response) obj; - Assertions.assertEquals(request.getId(), response.getId()); + Assert.assertEquals(request.getId(), response.getId()); - Assertions.assertTrue(response.getResult() instanceof RpcResult); + Assert.assertTrue(response.getResult() instanceof RpcResult); RpcResult result = (RpcResult) response.getResult(); - Assertions.assertTrue(result.getResult() instanceof String); + Assert.assertTrue(result.getResult() instanceof String); - Assertions.assertEquals(methodResult.success, result.getResult()); + Assert.assertEquals(methodResult.success, result.getResult()); } @@ -225,8 +221,6 @@ public void testDecodeExceptionResponse() throws Exception { protocol.writeI16(Short.MAX_VALUE); protocol.writeByte(ThriftCodec.VERSION); protocol.writeString(Demo.class.getName()); - // path - protocol.writeString(Demo.class.getName()); protocol.writeI64(request.getId()); protocol.getTransport().flush(); headerLength = bos.size(); @@ -251,19 +245,19 @@ public void testDecodeExceptionResponse() throws Exception { Object obj = codec.decode((Channel) null, bis); - Assertions.assertNotNull(obj); + Assert.assertNotNull(obj); - Assertions.assertTrue(obj instanceof Response); + Assert.assertTrue(obj instanceof Response); Response response = (Response) obj; - Assertions.assertTrue(response.getResult() instanceof RpcResult); + Assert.assertTrue(response.getResult() instanceof RpcResult); RpcResult result = (RpcResult) response.getResult(); - Assertions.assertTrue(result.hasException()); + Assert.assertTrue(result.hasException()); - Assertions.assertTrue(result.getException() instanceof RpcException); + Assert.assertTrue(result.getException() instanceof RpcException); } @@ -301,13 +295,13 @@ public void testEncodeReplyResponse() throws Exception { TIOStreamTransport transport = new TIOStreamTransport(bis); TBinaryProtocol protocol = new TBinaryProtocol(transport); - Assertions.assertEquals(ThriftCodec.MAGIC, protocol.readI16()); - Assertions.assertEquals(protocol.readI32() + 4, bos.writerIndex()); + Assert.assertEquals(ThriftCodec.MAGIC, protocol.readI16()); + Assert.assertEquals(protocol.readI32() + 4, bos.writerIndex()); int headerLength = protocol.readI16(); - Assertions.assertEquals(ThriftCodec.VERSION, protocol.readByte()); - Assertions.assertEquals(Demo.Iface.class.getName(), protocol.readString()); - Assertions.assertEquals(request.getId(), protocol.readI64()); + Assert.assertEquals(ThriftCodec.VERSION, protocol.readByte()); + Assert.assertEquals(Demo.Iface.class.getName(), protocol.readString()); + Assert.assertEquals(request.getId(), protocol.readI64()); if (bis.markSupported()) { bis.reset(); @@ -315,14 +309,14 @@ public void testEncodeReplyResponse() throws Exception { } TMessage message = protocol.readMessageBegin(); - Assertions.assertEquals("echoString", message.name); - Assertions.assertEquals(TMessageType.REPLY, message.type); - //Assertions.assertEquals(ThriftCodec.getSeqId(), message.seqid); + Assert.assertEquals("echoString", message.name); + Assert.assertEquals(TMessageType.REPLY, message.type); + //Assert.assertEquals(ThriftCodec.getSeqId(), message.seqid); Demo.echoString_result result = new Demo.echoString_result(); result.read(protocol); protocol.readMessageEnd(); - Assertions.assertEquals(rpcResult.getValue(), result.getSuccess()); + Assert.assertEquals(rpcResult.getValue(), result.getSuccess()); } @Test @@ -359,13 +353,13 @@ public void testEncodeExceptionResponse() throws Exception { TIOStreamTransport transport = new TIOStreamTransport(bis); TBinaryProtocol protocol = new TBinaryProtocol(transport); - Assertions.assertEquals(ThriftCodec.MAGIC, protocol.readI16()); - Assertions.assertEquals(protocol.readI32() + 4, bos.writerIndex()); + Assert.assertEquals(ThriftCodec.MAGIC, protocol.readI16()); + Assert.assertEquals(protocol.readI32() + 4, bos.writerIndex()); int headerLength = protocol.readI16(); - Assertions.assertEquals(ThriftCodec.VERSION, protocol.readByte()); - Assertions.assertEquals(Demo.Iface.class.getName(), protocol.readString()); - Assertions.assertEquals(request.getId(), protocol.readI64()); + Assert.assertEquals(ThriftCodec.VERSION, protocol.readByte()); + Assert.assertEquals(Demo.Iface.class.getName(), protocol.readString()); + Assert.assertEquals(request.getId(), protocol.readI64()); if (bis.markSupported()) { bis.reset(); @@ -373,13 +367,13 @@ public void testEncodeExceptionResponse() throws Exception { } TMessage message = protocol.readMessageBegin(); - Assertions.assertEquals("echoString", message.name); - Assertions.assertEquals(TMessageType.EXCEPTION, message.type); - Assertions.assertEquals(ThriftCodec.getSeqId(), message.seqid); + Assert.assertEquals("echoString", message.name); + Assert.assertEquals(TMessageType.EXCEPTION, message.type); + Assert.assertEquals(ThriftCodec.getSeqId(), message.seqid); TApplicationException exception = TApplicationException.read(protocol); protocol.readMessageEnd(); - Assertions.assertEquals(exceptionMessage, exception.getMessage()); + Assert.assertEquals(exceptionMessage, exception.getMessage()); } @@ -402,9 +396,6 @@ public void testDecodeRequest() throws Exception { protocol.writeString( ((RpcInvocation) request.getData()) .getAttachment(Constants.INTERFACE_KEY)); - protocol.writeString( - ((RpcInvocation) request.getData()) - .getAttachment(Constants.PATH_KEY)); protocol.writeI64(request.getId()); protocol.getTransport().flush(); headerLength = bos.size(); @@ -432,17 +423,17 @@ public void testDecodeRequest() throws Exception { Object obj = codec.decode((Channel) null, ChannelBuffers.wrappedBuffer( encodeFrame(bos.toByteArray()))); - Assertions.assertTrue(obj instanceof Request); + Assert.assertTrue(obj instanceof Request); obj = ((Request) obj).getData(); - Assertions.assertTrue(obj instanceof RpcInvocation); + Assert.assertTrue(obj instanceof RpcInvocation); RpcInvocation invocation = (RpcInvocation) obj; - Assertions.assertEquals("echoString", invocation.getMethodName()); - Assertions.assertArrayEquals(new Class[]{String.class}, invocation.getParameterTypes()); - Assertions.assertArrayEquals(new Object[]{args.getArg()}, invocation.getArguments()); + Assert.assertEquals("echoString", invocation.getMethodName()); + Assert.assertArrayEquals(new Class[]{String.class}, invocation.getParameterTypes()); + Assert.assertArrayEquals(new Object[]{args.getArg()}, invocation.getArguments()); } @@ -457,7 +448,6 @@ private Request createRequest() { invocation.setParameterTypes(new Class[]{String.class}); invocation.setAttachment(Constants.INTERFACE_KEY, Demo.Iface.class.getName()); - invocation.setAttachment(Constants.PATH_KEY, Demo.Iface.class.getName()); Request request = new Request(1L); diff --git a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftProtocolTest.java b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftProtocolTest.java index 364b31536a4..4b99899a5e1 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftProtocolTest.java @@ -21,8 +21,8 @@ import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.gen.dubbo.Demo; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; +import org.junit.After; +import org.junit.Before; public class ThriftProtocolTest extends AbstractTest { @@ -34,7 +34,7 @@ public class ThriftProtocolTest extends AbstractTest { private URL url; - @BeforeEach + @Before public void setUp() throws Exception { init(); @@ -45,7 +45,7 @@ public void setUp() throws Exception { } - @AfterEach + @After public void tearDown() throws Exception { destroy(); @@ -67,7 +67,7 @@ public void testRefer() throws Exception { // FIXME *//*invoker = protocol.refer( Demo.class, url ); - Assertions.assertNotNull( invoker ); + Assert.assertNotNull( invoker ); RpcInvocation invocation = new RpcInvocation(); @@ -81,7 +81,7 @@ public void testRefer() throws Exception { Result result = invoker.invoke( invocation ); - Assertions.assertEquals( arg, result.getResult() );*//* + Assert.assertEquals( arg, result.getResult() );*//* }*/ diff --git a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftUtilsTest.java b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftUtilsTest.java index 1e60315b697..31af56db71d 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftUtilsTest.java +++ b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftUtilsTest.java @@ -19,21 +19,21 @@ import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.rpc.gen.dubbo.$__DemoStub; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; public class ThriftUtilsTest { @Test public void testGenerateMethodArgsClassName() { - Assertions.assertEquals( + Assert.assertEquals( $__DemoStub.echoString_args.class.getName(), ThriftUtils.generateMethodArgsClassName( org.apache.dubbo.rpc.gen.dubbo.Demo.class.getName(), "echoString")); - Assertions.assertEquals( + Assert.assertEquals( $__DemoStub.echoString_args.class.getName(), ExtensionLoader.getExtensionLoader(ClassNameGenerator.class) .getExtension(DubboClassNameGenerator.NAME).generateArgsClassName( @@ -44,12 +44,12 @@ public void testGenerateMethodArgsClassName() { @Test public void testGenerateMethodResultClassName() { - Assertions.assertEquals($__DemoStub.echoString_result.class.getName(), + Assert.assertEquals($__DemoStub.echoString_result.class.getName(), ThriftUtils.generateMethodResultClassName( org.apache.dubbo.rpc.gen.dubbo.Demo.class.getName(), "echoString")); - Assertions.assertEquals($__DemoStub.echoString_result.class.getName(), + Assert.assertEquals($__DemoStub.echoString_result.class.getName(), ExtensionLoader.getExtensionLoader(ClassNameGenerator.class) .getExtension(DubboClassNameGenerator.NAME).generateResultClassName( org.apache.dubbo.rpc.gen.dubbo.Demo.class.getName(), "echoString")); @@ -58,12 +58,12 @@ public void testGenerateMethodResultClassName() { @Test public void testGenerateMethodArgsClassNameThrift() { - Assertions.assertEquals(org.apache.dubbo.rpc.gen.thrift.Demo.echoString_args.class.getName(), + Assert.assertEquals(org.apache.dubbo.rpc.gen.thrift.Demo.echoString_args.class.getName(), ThriftUtils.generateMethodArgsClassNameThrift( org.apache.dubbo.rpc.gen.thrift.Demo.Iface.class.getName(), "echoString")); - Assertions.assertEquals(org.apache.dubbo.rpc.gen.thrift.Demo.echoString_args.class.getName(), + Assert.assertEquals(org.apache.dubbo.rpc.gen.thrift.Demo.echoString_args.class.getName(), ExtensionLoader.getExtensionLoader(ClassNameGenerator.class) .getExtension(ThriftClassNameGenerator.NAME).generateArgsClassName( org.apache.dubbo.rpc.gen.thrift.Demo.Iface.class.getName(), @@ -73,12 +73,12 @@ public void testGenerateMethodArgsClassNameThrift() { @Test public void testGenerateMethodResultClassNameThrift() { - Assertions.assertEquals(org.apache.dubbo.rpc.gen.thrift.Demo.echoString_result.class.getName(), + Assert.assertEquals(org.apache.dubbo.rpc.gen.thrift.Demo.echoString_result.class.getName(), ThriftUtils.generateMethodResultClassNameThrift( org.apache.dubbo.rpc.gen.thrift.Demo.Iface.class.getName(), "echoString")); - Assertions.assertEquals(org.apache.dubbo.rpc.gen.thrift.Demo.echoString_result.class.getName(), + Assert.assertEquals(org.apache.dubbo.rpc.gen.thrift.Demo.echoString_result.class.getName(), ExtensionLoader.getExtensionLoader(ClassNameGenerator.class) .getExtension(ThriftClassNameGenerator.NAME).generateResultClassName( org.apache.dubbo.rpc.gen.thrift.Demo.Iface.class.getName(), diff --git a/dubbo-rpc/dubbo-rpc-webservice/pom.xml b/dubbo-rpc/dubbo-rpc-webservice/pom.xml index a859b58546c..2722827c53a 100644 --- a/dubbo-rpc/dubbo-rpc-webservice/pom.xml +++ b/dubbo-rpc/dubbo-rpc-webservice/pom.xml @@ -1,76 +1,76 @@ - - - 4.0.0 - - org.apache.dubbo - dubbo-rpc - 2.7.1-SNAPSHOT - - dubbo-rpc-webservice - jar - ${project.artifactId} - The webservice rpc module of dubbo project - - false - - - - org.apache.dubbo - dubbo-rpc-api - ${project.parent.version} - - - org.apache.dubbo - dubbo-remoting-http - ${project.parent.version} - - - javax.xml.bind - jaxb-api - - - com.sun.xml.bind - jaxb-impl - - - com.sun.xml.bind - jaxb-core - - - javax.activation - javax.activation-api - - - com.sun.activation - javax.activation - - - org.apache.cxf - cxf-rt-frontend-simple - - - org.apache.cxf - cxf-rt-transports-http - - - org.springframework - spring-context - - - + + + 4.0.0 + + org.apache.dubbo + dubbo-rpc + 2.7.0-SNAPSHOT + + dubbo-rpc-webservice + jar + ${project.artifactId} + The webservice rpc module of dubbo project + + false + + + + org.apache.dubbo + dubbo-rpc-api + ${project.parent.version} + + + org.apache.dubbo + dubbo-remoting-http + ${project.parent.version} + + + javax.xml.bind + jaxb-api + + + com.sun.xml.bind + jaxb-impl + + + com.sun.xml.bind + jaxb-core + + + javax.activation + javax.activation-api + + + com.sun.activation + javax.activation + + + org.apache.cxf + cxf-rt-frontend-simple + + + org.apache.cxf + cxf-rt-transports-http + + + org.springframework + spring-context + + + \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-webservice/src/test/java/org/apache/dubbo/rpc/protocol/webservice/WebserviceProtocolTest.java b/dubbo-rpc/dubbo-rpc-webservice/src/test/java/org/apache/dubbo/rpc/protocol/webservice/WebserviceProtocolTest.java index 8990edf9001..632e12c1d6c 100644 --- a/dubbo-rpc/dubbo-rpc-webservice/src/test/java/org/apache/dubbo/rpc/protocol/webservice/WebserviceProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-webservice/src/test/java/org/apache/dubbo/rpc/protocol/webservice/WebserviceProtocolTest.java @@ -18,14 +18,12 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.extension.ExtensionLoader; -import org.apache.dubbo.rpc.Exporter; import org.apache.dubbo.rpc.Protocol; import org.apache.dubbo.rpc.ProxyFactory; +import org.apache.dubbo.rpc.service.EchoService; +import org.junit.Test; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - +import static junit.framework.Assert.assertEquals; /** * @@ -38,10 +36,9 @@ public class WebserviceProtocolTest { @Test public void testDemoProtocol() throws Exception { DemoService service = new DemoServiceImpl(); - Exporter exporter = protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("webservice://127.0.0.1:9019/" + DemoService.class.getName() + "?codec=exchange"))); + protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("webservice://127.0.0.1:9019/" + DemoService.class.getName() + "?codec=exchange"))); service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("webservice://127.0.0.1:9019/" + DemoService.class.getName() + "?codec=exchange&timeout=3000"))); assertEquals(service.getSize(new String[]{"", "", ""}), 3); - exporter.unexport(); } @Test @@ -49,7 +46,7 @@ public void testWebserviceProtocol() throws Exception { DemoService service = new DemoServiceImpl(); protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("webservice://127.0.0.1:9019/" + DemoService.class.getName()))); service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("webservice://127.0.0.1:9019/" + DemoService.class.getName() + "?timeout=3000"))); - assertEquals(service.create(1, "kk").getName(), "kk"); + assertEquals(service.create(1,"kk").getName(), "kk"); assertEquals(service.getSize(null), -1); assertEquals(service.getSize(new String[]{"", "", ""}), 3); Object object = service.invoke("webservice://127.0.0.1:9019/" + DemoService.class.getName() + "", "invoke"); @@ -59,7 +56,7 @@ public void testWebserviceProtocol() throws Exception { StringBuffer buf = new StringBuffer(); for (int i = 0; i < 1024 * 32 + 32; i++) buf.append('A'); - assertEquals(32800, service.stringLength(buf.toString())); + assertEquals(32800,service.stringLength(buf.toString())); // a method start with $ is illegal in soap // // cast to EchoService diff --git a/dubbo-rpc/pom.xml b/dubbo-rpc/pom.xml index d175da8f6fe..184bc30dc4b 100644 --- a/dubbo-rpc/pom.xml +++ b/dubbo-rpc/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-parent - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-rpc pom diff --git a/dubbo-serialization/dubbo-serialization-api/pom.xml b/dubbo-serialization/dubbo-serialization-api/pom.xml index cd1ff42939e..486fc9d2b8e 100644 --- a/dubbo-serialization/dubbo-serialization-api/pom.xml +++ b/dubbo-serialization/dubbo-serialization-api/pom.xml @@ -20,7 +20,7 @@ limitations under the License. org.apache.dubbo dubbo-serialization - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-serialization-api jar diff --git a/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/Cleanable.java b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/Cleanable.java index 425bb0a4ede..a8f96d3b0e9 100644 --- a/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/Cleanable.java +++ b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/Cleanable.java @@ -16,13 +16,7 @@ */ package org.apache.dubbo.common.serialize; -/** - * Interface defines that the object is cleanable. - */ public interface Cleanable { - /** - * Implementations must implement this cleanup method - */ void cleanup(); } diff --git a/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/DataInput.java b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/DataInput.java index 7930d04d101..d122e5cfa13 100644 --- a/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/DataInput.java +++ b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/DataInput.java @@ -19,7 +19,7 @@ import java.io.IOException; /** - * Basic data type input interface. + * Data input. */ public interface DataInput { diff --git a/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/DataOutput.java b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/DataOutput.java index 58973271542..36577240427 100644 --- a/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/DataOutput.java +++ b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/DataOutput.java @@ -19,7 +19,7 @@ import java.io.IOException; /** - * Basic data type output interface. + * Data output. */ public interface DataOutput { @@ -98,9 +98,9 @@ public interface DataOutput { /** * Write byte array. * - * @param v value. - * @param off the start offset in the data. - * @param len the number of bytes that are written. + * @param v value. + * @param off offset. + * @param len length. * @throws IOException */ void writeBytes(byte[] v, int off, int len) throws IOException; diff --git a/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/ObjectInput.java b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/ObjectInput.java index 1d8646e880e..81207cb720a 100644 --- a/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/ObjectInput.java +++ b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/ObjectInput.java @@ -20,37 +20,30 @@ import java.lang.reflect.Type; /** - * Object input interface. + * Object input. */ public interface ObjectInput extends DataInput { /** - * read object + * read object. * - * @return object - * @throws IOException if an I/O error occurs - * @throws ClassNotFoundException if an ClassNotFoundException occurs + * @return object. */ Object readObject() throws IOException, ClassNotFoundException; /** - * read object + * read object. * - * @param cls object class - * @return object - * @throws IOException if an I/O error occurs - * @throws ClassNotFoundException if an ClassNotFoundException occurs + * @param cls object type. + * @return object. */ T readObject(Class cls) throws IOException, ClassNotFoundException; /** - * read object + * read object. * - * @param cls object class - * @param type object type - * @return object - * @throws IOException if an I/O error occurs - * @throws ClassNotFoundException if an ClassNotFoundException occurs + * @param cls object type. + * @return object. */ T readObject(Class cls, Type type) throws IOException, ClassNotFoundException; diff --git a/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/ObjectOutput.java b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/ObjectOutput.java index 73fa3b61fd5..bf50167d810 100644 --- a/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/ObjectOutput.java +++ b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/ObjectOutput.java @@ -19,7 +19,7 @@ import java.io.IOException; /** - * Object output interface. + * Object output. */ public interface ObjectOutput extends DataOutput { diff --git a/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/Serialization.java b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/Serialization.java index 0c09df99a3d..eb9a72e98fd 100644 --- a/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/Serialization.java +++ b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/Serialization.java @@ -25,35 +25,30 @@ import java.io.OutputStream; /** - * Serialization strategy interface that specifies a serializer. (SPI, Singleton, ThreadSafe) - * - * The default extension is hessian2 and the default serialization implementation of the dubbo protocol. - *
    - *     e.g. <dubbo:protocol serialization="xxx" />
    - * 
    + * Serialization. (SPI, Singleton, ThreadSafe) */ @SPI("hessian2") public interface Serialization { /** - * Get content type unique id, recommended that custom implementations use values greater than 20. + * get content type id * * @return content type id */ byte getContentTypeId(); /** - * Get content type + * get content type * * @return content type */ String getContentType(); /** - * Get a serialization implementation instance + * create serializer * - * @param url URL address for the remote service - * @param output the underlying output stream + * @param url + * @param output * @return serializer * @throws IOException */ @@ -61,10 +56,10 @@ public interface Serialization { ObjectOutput serialize(URL url, OutputStream output) throws IOException; /** - * Get a deserialization implementation instance + * create deserializer * - * @param url URL address for the remote service - * @param input the underlying input stream + * @param url + * @param input * @return deserializer * @throws IOException */ diff --git a/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/support/SerializableClassRegistry.java b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/support/SerializableClassRegistry.java index 22de45d5c74..2ebb3a3c7fd 100644 --- a/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/support/SerializableClassRegistry.java +++ b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/support/SerializableClassRegistry.java @@ -21,10 +21,6 @@ import java.util.LinkedHashMap; import java.util.Map; -/** - * Provide a unified serialization registry, this class used for {@code dubbo-serialization-fst} - * and {@code dubbo-serialization-kryo}, it will register some classes at startup time (for example {@link AbstractKryoFactory#create}) - */ public abstract class SerializableClassRegistry { @@ -32,8 +28,6 @@ public abstract class SerializableClassRegistry { /** * only supposed to be called at startup time - * - * @param clazz object type */ public static void registerClass(Class clazz) { registerClass(clazz, null); @@ -41,9 +35,6 @@ public static void registerClass(Class clazz) { /** * only supposed to be called at startup time - * - * @param clazz object type - * @param serializer object serializer */ public static void registerClass(Class clazz, Serializer serializer) { if (clazz == null) { @@ -52,11 +43,6 @@ public static void registerClass(Class clazz, Serializer serializer) { registrations.put(clazz, serializer); } - /** - * get registered classes - * - * @return class serializer - * */ public static Map getRegisteredClasses() { return registrations; } diff --git a/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/support/SerializationOptimizer.java b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/support/SerializationOptimizer.java index b043f3448f8..7085f2de89a 100644 --- a/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/support/SerializationOptimizer.java +++ b/dubbo-serialization/dubbo-serialization-api/src/main/java/org/apache/dubbo/common/serialize/support/SerializationOptimizer.java @@ -19,14 +19,10 @@ import java.util.Collection; /** - * Interface defining serialization optimizer, there are nothing implementations for now. + * This class can be replaced with the contents in config file, but for now I think the class is easier to write + * */ public interface SerializationOptimizer { - /** - * Get serializable classes - * - * @return serializable classes - * */ Collection getSerializableClasses(); } diff --git a/dubbo-serialization/dubbo-serialization-fastjson/pom.xml b/dubbo-serialization/dubbo-serialization-fastjson/pom.xml index 92dbbec8820..d1221b348ec 100644 --- a/dubbo-serialization/dubbo-serialization-fastjson/pom.xml +++ b/dubbo-serialization/dubbo-serialization-fastjson/pom.xml @@ -20,7 +20,7 @@ limitations under the License. org.apache.dubbo dubbo-serialization - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-serialization-fastjson jar diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/main/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectInput.java b/dubbo-serialization/dubbo-serialization-fastjson/src/main/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectInput.java index 1a38f277dc7..96824fd58cf 100644 --- a/dubbo-serialization/dubbo-serialization-fastjson/src/main/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectInput.java +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/main/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectInput.java @@ -29,9 +29,6 @@ import java.io.Reader; import java.lang.reflect.Type; -/** - * FastJson object input implementation - */ public class FastJsonObjectInput implements ObjectInput { private final BufferedReader reader; diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/main/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectOutput.java b/dubbo-serialization/dubbo-serialization-fastjson/src/main/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectOutput.java index 3f9ec2083da..d0e4ed546e9 100644 --- a/dubbo-serialization/dubbo-serialization-fastjson/src/main/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectOutput.java +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/main/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectOutput.java @@ -28,9 +28,6 @@ import java.io.PrintWriter; import java.io.Writer; -/** - * FastJson object output implementation - */ public class FastJsonObjectOutput implements ObjectOutput { private final PrintWriter writer; diff --git a/dubbo-serialization/dubbo-serialization-fastjson/src/main/java/org/apache/dubbo/common/serialize/fastjson/FastJsonSerialization.java b/dubbo-serialization/dubbo-serialization-fastjson/src/main/java/org/apache/dubbo/common/serialize/fastjson/FastJsonSerialization.java index 097587132c7..189ef0b0b86 100644 --- a/dubbo-serialization/dubbo-serialization-fastjson/src/main/java/org/apache/dubbo/common/serialize/fastjson/FastJsonSerialization.java +++ b/dubbo-serialization/dubbo-serialization-fastjson/src/main/java/org/apache/dubbo/common/serialize/fastjson/FastJsonSerialization.java @@ -25,13 +25,6 @@ import java.io.InputStream; import java.io.OutputStream; -/** - * FastJson serialization implementation - * - *
    - *     e.g. <dubbo:protocol serialization="fastjson" />
    - * 
    - */ public class FastJsonSerialization implements Serialization { @Override diff --git a/dubbo-serialization/dubbo-serialization-fst/pom.xml b/dubbo-serialization/dubbo-serialization-fst/pom.xml index 4332453a907..ad73437c060 100644 --- a/dubbo-serialization/dubbo-serialization-fst/pom.xml +++ b/dubbo-serialization/dubbo-serialization-fst/pom.xml @@ -19,7 +19,7 @@ org.apache.dubbo dubbo-serialization - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-serialization-fst jar diff --git a/dubbo-serialization/dubbo-serialization-fst/src/main/java/org/apache/dubbo/common/serialize/fst/FstFactory.java b/dubbo-serialization/dubbo-serialization-fst/src/main/java/org/apache/dubbo/common/serialize/fst/FstFactory.java index e41c358b43e..b0830332b52 100644 --- a/dubbo-serialization/dubbo-serialization-fst/src/main/java/org/apache/dubbo/common/serialize/fst/FstFactory.java +++ b/dubbo-serialization/dubbo-serialization-fst/src/main/java/org/apache/dubbo/common/serialize/fst/FstFactory.java @@ -25,9 +25,6 @@ import java.io.InputStream; import java.io.OutputStream; -/** - * Fst object input/output factory - */ public class FstFactory { private static final FstFactory factory = new FstFactory(); diff --git a/dubbo-serialization/dubbo-serialization-fst/src/main/java/org/apache/dubbo/common/serialize/fst/FstObjectInput.java b/dubbo-serialization/dubbo-serialization-fst/src/main/java/org/apache/dubbo/common/serialize/fst/FstObjectInput.java index 51e8340a988..b94a85f2fd8 100644 --- a/dubbo-serialization/dubbo-serialization-fst/src/main/java/org/apache/dubbo/common/serialize/fst/FstObjectInput.java +++ b/dubbo-serialization/dubbo-serialization-fst/src/main/java/org/apache/dubbo/common/serialize/fst/FstObjectInput.java @@ -24,9 +24,7 @@ import java.io.InputStream; import java.lang.reflect.Type; -/** - * Fst object input implementation - */ + public class FstObjectInput implements ObjectInput { private FSTObjectInput input; diff --git a/dubbo-serialization/dubbo-serialization-fst/src/main/java/org/apache/dubbo/common/serialize/fst/FstObjectOutput.java b/dubbo-serialization/dubbo-serialization-fst/src/main/java/org/apache/dubbo/common/serialize/fst/FstObjectOutput.java index a2c40e5d0da..46fee0b3aa0 100644 --- a/dubbo-serialization/dubbo-serialization-fst/src/main/java/org/apache/dubbo/common/serialize/fst/FstObjectOutput.java +++ b/dubbo-serialization/dubbo-serialization-fst/src/main/java/org/apache/dubbo/common/serialize/fst/FstObjectOutput.java @@ -23,9 +23,7 @@ import java.io.IOException; import java.io.OutputStream; -/** - * Fst object output implementation - */ + public class FstObjectOutput implements ObjectOutput { private FSTObjectOutput output; diff --git a/dubbo-serialization/dubbo-serialization-fst/src/main/java/org/apache/dubbo/common/serialize/fst/FstSerialization.java b/dubbo-serialization/dubbo-serialization-fst/src/main/java/org/apache/dubbo/common/serialize/fst/FstSerialization.java index e8b984c8bba..89881df3969 100644 --- a/dubbo-serialization/dubbo-serialization-fst/src/main/java/org/apache/dubbo/common/serialize/fst/FstSerialization.java +++ b/dubbo-serialization/dubbo-serialization-fst/src/main/java/org/apache/dubbo/common/serialize/fst/FstSerialization.java @@ -25,13 +25,6 @@ import java.io.InputStream; import java.io.OutputStream; -/** - * Fst serialization implementation - * - *
    - *     e.g. <dubbo:protocol serialization="fst" />
    - * 
    - */ public class FstSerialization implements Serialization { @Override diff --git a/dubbo-serialization/dubbo-serialization-hessian2/pom.xml b/dubbo-serialization/dubbo-serialization-hessian2/pom.xml index b5ba85a2953..0f91b78bd97 100644 --- a/dubbo-serialization/dubbo-serialization-hessian2/pom.xml +++ b/dubbo-serialization/dubbo-serialization-hessian2/pom.xml @@ -20,7 +20,7 @@ limitations under the License. org.apache.dubbo dubbo-serialization - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-serialization-hessian2 jar diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ObjectInput.java b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ObjectInput.java index d1ef21402c7..348d04d9913 100644 --- a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ObjectInput.java +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ObjectInput.java @@ -25,7 +25,7 @@ import java.lang.reflect.Type; /** - * Hessian2 object input implementation + * Hessian2 Object input. */ public class Hessian2ObjectInput implements ObjectInput { private final Hessian2Input mH2i; diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ObjectOutput.java b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ObjectOutput.java index 38fd3db0b27..fec356a668a 100644 --- a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ObjectOutput.java +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ObjectOutput.java @@ -24,7 +24,7 @@ import java.io.OutputStream; /** - * Hessian2 object output implementation + * Hessian2 Object output. */ public class Hessian2ObjectOutput implements ObjectOutput { private final Hessian2Output mH2o; diff --git a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2Serialization.java b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2Serialization.java index 225df10908a..71c6233659c 100644 --- a/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2Serialization.java +++ b/dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2Serialization.java @@ -25,13 +25,6 @@ import java.io.InputStream; import java.io.OutputStream; -/** - * Hessian2 serialization implementation, hessian2 is the default serialization protocol for dubbo - * - *
    - *     e.g. <dubbo:protocol serialization="hessian2" />
    - * 
    - */ public class Hessian2Serialization implements Serialization { public static final byte ID = 2; diff --git a/dubbo-serialization/dubbo-serialization-jdk/pom.xml b/dubbo-serialization/dubbo-serialization-jdk/pom.xml index 1f78b9a9c1b..23b56a21e27 100644 --- a/dubbo-serialization/dubbo-serialization-jdk/pom.xml +++ b/dubbo-serialization/dubbo-serialization-jdk/pom.xml @@ -20,7 +20,7 @@ limitations under the License. org.apache.dubbo dubbo-serialization - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-serialization-jdk jar diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/CompactedJavaSerialization.java b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/CompactedJavaSerialization.java index 8222ac45091..9573ed77891 100644 --- a/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/CompactedJavaSerialization.java +++ b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/CompactedJavaSerialization.java @@ -25,13 +25,6 @@ import java.io.InputStream; import java.io.OutputStream; -/** - * Compacted java serialization implementation - * - *
    - *     e.g. <dubbo:protocol serialization="compactedjava" />
    - * 
    - */ public class CompactedJavaSerialization implements Serialization { @Override diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/CompactedObjectInputStream.java b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/CompactedObjectInputStream.java index 2ed71f814ec..a6bda0aca99 100644 --- a/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/CompactedObjectInputStream.java +++ b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/CompactedObjectInputStream.java @@ -26,7 +26,7 @@ import java.io.StreamCorruptedException; /** - * Compacted java object input implementation + * Compacted java object input stream. */ public class CompactedObjectInputStream extends ObjectInputStream { private ClassLoader mClassLoader; diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/CompactedObjectOutputStream.java b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/CompactedObjectOutputStream.java index 56847e5e254..2bbf42e97eb 100644 --- a/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/CompactedObjectOutputStream.java +++ b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/CompactedObjectOutputStream.java @@ -22,7 +22,7 @@ import java.io.OutputStream; /** - * Compacted java object output implementation + * Compacted java object output stream. */ public class CompactedObjectOutputStream extends ObjectOutputStream { public CompactedObjectOutputStream(OutputStream out) throws IOException { diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/JavaObjectInput.java b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/JavaObjectInput.java index 936ab265063..00e712e6e97 100644 --- a/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/JavaObjectInput.java +++ b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/JavaObjectInput.java @@ -24,7 +24,7 @@ import java.lang.reflect.Type; /** - * Java object input implementation + * Java Object input. */ public class JavaObjectInput extends NativeJavaObjectInput { public final static int MAX_BYTE_ARRAY_LENGTH = 8 * 1024 * 1024; diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/JavaObjectOutput.java b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/JavaObjectOutput.java index 0e78747c919..49a4b45123f 100644 --- a/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/JavaObjectOutput.java +++ b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/JavaObjectOutput.java @@ -23,7 +23,7 @@ import java.io.OutputStream; /** - * Java object output implementation + * Java Object output. */ public class JavaObjectOutput extends NativeJavaObjectOutput { public JavaObjectOutput(OutputStream os) throws IOException { diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/JavaSerialization.java b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/JavaSerialization.java index bf98818fd43..72d9ef3a94d 100644 --- a/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/JavaSerialization.java +++ b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/JavaSerialization.java @@ -25,13 +25,6 @@ import java.io.InputStream; import java.io.OutputStream; -/** - * Java serialization implementation - * - *
    - *     e.g. <dubbo:protocol serialization="java" />
    - * 
    - */ public class JavaSerialization implements Serialization { @Override diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/nativejava/NativeJavaObjectInput.java b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/nativejava/NativeJavaObjectInput.java index c791250f7af..a01f1ebaa73 100644 --- a/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/nativejava/NativeJavaObjectInput.java +++ b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/nativejava/NativeJavaObjectInput.java @@ -25,9 +25,6 @@ import java.io.ObjectInputStream; import java.lang.reflect.Type; -/** - * Native java object input implementation - */ public class NativeJavaObjectInput implements ObjectInput { private final ObjectInputStream inputStream; diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/nativejava/NativeJavaObjectOutput.java b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/nativejava/NativeJavaObjectOutput.java index 2ff82f76c36..89f8145a68f 100644 --- a/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/nativejava/NativeJavaObjectOutput.java +++ b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/nativejava/NativeJavaObjectOutput.java @@ -24,9 +24,6 @@ import java.io.ObjectOutputStream; import java.io.OutputStream; -/** - * Native java object output implementation - */ public class NativeJavaObjectOutput implements ObjectOutput { private final ObjectOutputStream outputStream; diff --git a/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/nativejava/NativeJavaSerialization.java b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/nativejava/NativeJavaSerialization.java index 55198690607..3b692487445 100644 --- a/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/nativejava/NativeJavaSerialization.java +++ b/dubbo-serialization/dubbo-serialization-jdk/src/main/java/org/apache/dubbo/common/serialize/nativejava/NativeJavaSerialization.java @@ -26,13 +26,6 @@ import java.io.InputStream; import java.io.OutputStream; -/** - * Native java serialization implementation - * - *
    - *     e.g. <dubbo:protocol serialization="nativejava" />
    - * 
    - */ public class NativeJavaSerialization implements Serialization { public static final String NAME = "nativejava"; diff --git a/dubbo-serialization/dubbo-serialization-kryo/pom.xml b/dubbo-serialization/dubbo-serialization-kryo/pom.xml index 4ebf59bea3d..df29f2ad60c 100644 --- a/dubbo-serialization/dubbo-serialization-kryo/pom.xml +++ b/dubbo-serialization/dubbo-serialization-kryo/pom.xml @@ -20,7 +20,7 @@ limitations under the License. org.apache.dubbo dubbo-serialization - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-serialization-kryo jar diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/main/java/org/apache/dubbo/common/serialize/kryo/KryoObjectInput.java b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/org/apache/dubbo/common/serialize/kryo/KryoObjectInput.java index 91648e5c628..fcfc91a4fe5 100644 --- a/dubbo-serialization/dubbo-serialization-kryo/src/main/java/org/apache/dubbo/common/serialize/kryo/KryoObjectInput.java +++ b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/org/apache/dubbo/common/serialize/kryo/KryoObjectInput.java @@ -28,9 +28,6 @@ import java.io.InputStream; import java.lang.reflect.Type; -/** - * Kryo object input implementation, kryo object can be clean - */ public class KryoObjectInput implements ObjectInput, Cleanable { private Kryo kryo; diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/main/java/org/apache/dubbo/common/serialize/kryo/KryoObjectOutput.java b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/org/apache/dubbo/common/serialize/kryo/KryoObjectOutput.java index dd127005fe8..ef2f219acd7 100644 --- a/dubbo-serialization/dubbo-serialization-kryo/src/main/java/org/apache/dubbo/common/serialize/kryo/KryoObjectOutput.java +++ b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/org/apache/dubbo/common/serialize/kryo/KryoObjectOutput.java @@ -26,9 +26,6 @@ import java.io.IOException; import java.io.OutputStream; -/** - * Kryo object output implementation, kryo object can be clean - */ public class KryoObjectOutput implements ObjectOutput, Cleanable { private Output output; diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/main/java/org/apache/dubbo/common/serialize/kryo/KryoSerialization.java b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/org/apache/dubbo/common/serialize/kryo/KryoSerialization.java index aa10df4af90..7ff6617a704 100644 --- a/dubbo-serialization/dubbo-serialization-kryo/src/main/java/org/apache/dubbo/common/serialize/kryo/KryoSerialization.java +++ b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/org/apache/dubbo/common/serialize/kryo/KryoSerialization.java @@ -28,9 +28,6 @@ /** * TODO for now kryo serialization doesn't deny classes that don't implement the serializable interface * - *
    - *     e.g. <dubbo:protocol serialization="kryo" />
    - * 
    */ public class KryoSerialization implements Serialization { diff --git a/dubbo-serialization/dubbo-serialization-protostuff/pom.xml b/dubbo-serialization/dubbo-serialization-protostuff/pom.xml index ab3a2a5f390..cd1b644000d 100644 --- a/dubbo-serialization/dubbo-serialization-protostuff/pom.xml +++ b/dubbo-serialization/dubbo-serialization-protostuff/pom.xml @@ -15,12 +15,14 @@ See the License for the specific language governing permissions and limitations under the License. --> - + 4.0.0 dubbo-serialization org.apache.dubbo - 2.7.1-SNAPSHOT + 2.7.0-SNAPSHOT dubbo-serialization-protostuff diff --git a/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectInput.java b/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectInput.java index e8307748deb..0202762cfa2 100644 --- a/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectInput.java +++ b/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectInput.java @@ -17,19 +17,18 @@ package org.apache.dubbo.common.serialize.protostuff; -import io.protostuff.GraphIOUtil; +import org.apache.dubbo.common.serialize.ObjectInput; +import org.apache.dubbo.common.serialize.protostuff.utils.WrapperUtils; + +import io.protostuff.ProtobufIOUtil; import io.protostuff.Schema; import io.protostuff.runtime.RuntimeSchema; + import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Type; -import org.apache.dubbo.common.serialize.ObjectInput; -import org.apache.dubbo.common.serialize.protostuff.utils.WrapperUtils; -/** - * Protostuff object input implementation - */ public class ProtostuffObjectInput implements ObjectInput { private DataInputStream dis; @@ -61,12 +60,12 @@ public Object readObject() throws IOException, ClassNotFoundException { if (WrapperUtils.needWrapper(clazz)) { Schema schema = RuntimeSchema.getSchema(Wrapper.class); Wrapper wrapper = schema.newMessage(); - GraphIOUtil.mergeFrom(bytes, wrapper, schema); + ProtobufIOUtil.mergeFrom(bytes, wrapper, schema); result = wrapper.getData(); } else { Schema schema = RuntimeSchema.getSchema(clazz); result = schema.newMessage(); - GraphIOUtil.mergeFrom(bytes, result, schema); + ProtobufIOUtil.mergeFrom(bytes, result, schema); } return result; diff --git a/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutput.java b/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutput.java index f52c755d5d1..2e91d17ef6f 100644 --- a/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutput.java +++ b/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutput.java @@ -17,19 +17,18 @@ package org.apache.dubbo.common.serialize.protostuff; -import io.protostuff.GraphIOUtil; +import org.apache.dubbo.common.serialize.ObjectOutput; +import org.apache.dubbo.common.serialize.protostuff.utils.WrapperUtils; + import io.protostuff.LinkedBuffer; +import io.protostuff.ProtobufIOUtil; import io.protostuff.Schema; import io.protostuff.runtime.RuntimeSchema; + import java.io.DataOutputStream; import java.io.IOException; import java.io.OutputStream; -import org.apache.dubbo.common.serialize.ObjectOutput; -import org.apache.dubbo.common.serialize.protostuff.utils.WrapperUtils; -/** - * Protostuff object output implementation - */ public class ProtostuffObjectOutput implements ObjectOutput { private LinkedBuffer buffer = LinkedBuffer.allocate(); @@ -50,11 +49,11 @@ public void writeObject(Object obj) throws IOException { if (WrapperUtils.needWrapper(obj)) { Schema schema = RuntimeSchema.getSchema(Wrapper.class); Wrapper wrapper = new Wrapper(obj); - bytes = GraphIOUtil.toByteArray(wrapper, schema, buffer); + bytes = ProtobufIOUtil.toByteArray(wrapper, schema, buffer); classNameBytes = Wrapper.class.getName().getBytes(); } else { Schema schema = RuntimeSchema.getSchema(obj.getClass()); - bytes = GraphIOUtil.toByteArray(obj, schema, buffer); + bytes = ProtobufIOUtil.toByteArray(obj, schema, buffer); classNameBytes = obj.getClass().getName().getBytes(); } } finally { diff --git a/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffSerialization.java b/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffSerialization.java index acdeaeaff4c..aaf5bddc36d 100644 --- a/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffSerialization.java +++ b/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffSerialization.java @@ -26,13 +26,6 @@ import java.io.InputStream; import java.io.OutputStream; -/** - * Protostuff serialization implementation - * - *
    - *     e.g. <dubbo:protocol serialization="protostuff" />
    - * 
    - */ public class ProtostuffSerialization implements Serialization { @Override public byte getContentTypeId() { diff --git a/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/delegate/TimeDelegate.java b/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/delegate/TimeDelegate.java index 6ead01827e2..8cc33c7d0be 100644 --- a/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/delegate/TimeDelegate.java +++ b/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/delegate/TimeDelegate.java @@ -26,9 +26,6 @@ import java.io.IOException; import java.sql.Time; -/** - * Custom {@link Time} delegate - */ public class TimeDelegate implements Delegate