This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.0 by this push:
new a70363e Add unit test for common-module and fix unregisterProvider
method (#9288)
a70363e is described below
commit a70363e693476eed05b27befde55a7824493ab06
Author: 灼华 <[email protected]>
AuthorDate: Thu Nov 18 22:07:28 2021 +0800
Add unit test for common-module and fix unregisterProvider method (#9288)
* Add unit test for common-module and fix unregisterProvider method
* Remove unused imports
---
.../beans/support/InstantiationStrategy.java | 2 +-
.../dubbo/common/extension/ExtensionLoader.java | 6 +-
.../dubbo/common/extension/LoadingStrategy.java | 4 +-
.../common/utils/ClassLoaderResourceLoader.java | 2 +-
.../org/apache/dubbo/rpc/model/FrameworkModel.java | 2 +-
.../rpc/model/FrameworkServiceRepository.java | 13 +--
.../apache/dubbo/rpc/model/MethodDescriptor.java | 3 -
.../model/ScopeModelAwareExtensionProcessor.java | 2 +-
.../apache/dubbo/rpc/model/ServiceMetadata.java | 2 +-
.../common/CommonScopeModelInitializerTest.java | 62 +++++++++++
.../rpc/model/FrameworkServiceRepositoryTest.java | 113 ++++++++++++++++++++
.../rpc/model/ModuleServiceRepositoryTest.java | 113 ++++++++++++++++++++
.../ScopeModelAwareExtensionProcessorTest.java | 78 ++++++++++++++
.../apache/dubbo/rpc/model/ScopeModelUtilTest.java | 114 +++++++++++++++++++++
.../dubbo/rpc/model/ServiceRepositoryTest.java | 90 ++++++++++++++++
.../org/apache/dubbo/rpc/support/DemoService.java | 29 +-----
.../apache/dubbo/rpc/support/DemoServiceImpl.java | 28 +----
.../dubbo/rpc/support/MockScopeModelAware.java | 72 +++++++++++++
.../config/spring/SpringScopeModelInitializer.java | 3 +-
19 files changed, 665 insertions(+), 73 deletions(-)
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/common/beans/support/InstantiationStrategy.java
b/dubbo-common/src/main/java/org/apache/dubbo/common/beans/support/InstantiationStrategy.java
index 02e4063..b4ca8b4 100644
---
a/dubbo-common/src/main/java/org/apache/dubbo/common/beans/support/InstantiationStrategy.java
+++
b/dubbo-common/src/main/java/org/apache/dubbo/common/beans/support/InstantiationStrategy.java
@@ -46,7 +46,7 @@ public class InstantiationStrategy {
public <T> T instantiate(Class<T> type) throws
ReflectiveOperationException {
// should not use default constructor directly, maybe also has another
constructor matched scope model arguments
- // 1. try get default constructor
+ // 1. try to get default constructor
Constructor<T> defaultConstructor = null;
try {
defaultConstructor = type.getConstructor();
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 604ca14..af2d5fd 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
@@ -584,8 +584,8 @@ public class ExtensionLoader<T> {
public Set<String> getSupportedExtensions() {
checkDestroyed();
- Map<String, Class<?>> clazzes = getExtensionClasses();
- return Collections.unmodifiableSet(new TreeSet<>(clazzes.keySet()));
+ Map<String, Class<?>> classes = getExtensionClasses();
+ return Collections.unmodifiableSet(new TreeSet<>(classes.keySet()));
}
public Set<T> getSupportedExtensionInstances() {
@@ -1023,7 +1023,7 @@ public class ExtensionLoader<T> {
try {
try (BufferedReader reader = new BufferedReader(new
InputStreamReader(resourceURL.openStream(), StandardCharsets.UTF_8))) {
String line;
- String clazz = null;
+ String clazz;
while ((line = reader.readLine()) != null) {
final int ci = line.indexOf('#');
if (ci >= 0) {
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/LoadingStrategy.java
b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/LoadingStrategy.java
index 70c49c1..b41f132 100644
---
a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/LoadingStrategy.java
+++
b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/LoadingStrategy.java
@@ -31,11 +31,11 @@ public interface LoadingStrategy extends Prioritized {
}
/**
- * To restrict some class that should loaded from Dubbo's ClassLoader.
+ * To restrict some class that should load from Dubbo's ClassLoader.
* For example, we can restrict the class declaration in
`org.apache.dubbo` package should
* be loaded from Dubbo's ClassLoader and users cannot declare these
classes.
*
- * @return class packages should loaded
+ * @return class packages should load
* @since 3.0.4
*/
default String[] onlyExtensionClassLoaderPackages() {
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ClassLoaderResourceLoader.java
b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ClassLoaderResourceLoader.java
index f3e060d..2fba54f 100644
---
a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ClassLoaderResourceLoader.java
+++
b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ClassLoaderResourceLoader.java
@@ -75,7 +75,7 @@ public class ClassLoaderResourceLoader {
Map<String, Set<java.net.URL>> urlCache =
classLoaderCache.get(currentClassLoader);
if (!urlCache.containsKey(fileName)) {
Set<java.net.URL> set = new LinkedHashSet<>();
- Enumeration<URL> urls = null;
+ Enumeration<URL> urls;
try {
urls = currentClassLoader.getResources(fileName);
boolean isNative = NativeUtils.isNative();
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/FrameworkModel.java
b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/FrameworkModel.java
index 4430b73..b9f2332 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/FrameworkModel.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/FrameworkModel.java
@@ -65,7 +65,7 @@ public class FrameworkModel extends ScopeModel {
public FrameworkModel() {
super(null, ExtensionScope.FRAMEWORK);
- this.setInternalId(index.getAndIncrement()+"");
+ this.setInternalId(String.valueOf(index.getAndIncrement()));
// register FrameworkModel instance early
synchronized (globalLock) {
allInstances.add(this);
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/FrameworkServiceRepository.java
b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/FrameworkServiceRepository.java
index 0c8cf8e..d858fc1 100644
---
a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/FrameworkServiceRepository.java
+++
b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/FrameworkServiceRepository.java
@@ -17,8 +17,6 @@
package org.apache.dubbo.rpc.model;
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 java.util.ArrayList;
@@ -36,9 +34,8 @@ import static
org.apache.dubbo.common.BaseServiceMetadata.versionFromServiceKey;
* Service repository for framework
*/
public class FrameworkServiceRepository {
- private FrameworkModel frameworkModel;
- private static final Logger logger =
LoggerFactory.getLogger(FrameworkServiceRepository.class);
+ private FrameworkModel frameworkModel;
// useful to find a provider model quickly with
group/serviceInterfaceName:version
private ConcurrentMap<String, ProviderModel> providers = new
ConcurrentHashMap<>();
@@ -65,10 +62,10 @@ public class FrameworkServiceRepository {
}
public void unregisterProvider(ProviderModel providerModel) {
- String key = keyWithoutGroup(providerModel.getServiceKey());
- providers.remove(key);
- providersWithoutGroup.remove(key);
- providerUrlsWithoutGroup.remove(key);
+ providers.remove(providerModel.getServiceKey());
+ String keyWithoutGroup =
keyWithoutGroup(providerModel.getServiceKey());
+ providersWithoutGroup.remove(keyWithoutGroup);
+ providerUrlsWithoutGroup.remove(keyWithoutGroup);
}
public ProviderModel lookupExportedServiceWithoutGroup(String key) {
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/MethodDescriptor.java
b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/MethodDescriptor.java
index 93148df..03e9846 100644
---
a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/MethodDescriptor.java
+++
b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/MethodDescriptor.java
@@ -37,9 +37,6 @@ import static
org.apache.dubbo.common.constants.CommonConstants.$INVOKE;
import static org.apache.dubbo.common.constants.CommonConstants.$INVOKE_ASYNC;
import static
org.apache.dubbo.common.constants.CommonConstants.PROTOBUF_MESSAGE_CLASS_NAME;
-/**
- *
- */
public class MethodDescriptor {
private static final String GRPC_ASYNC_RETURN_CLASS =
"com.google.common.util.concurrent.ListenableFuture";
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ScopeModelAwareExtensionProcessor.java
b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ScopeModelAwareExtensionProcessor.java
index bb85e52..e38bc52 100644
---
a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ScopeModelAwareExtensionProcessor.java
+++
b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ScopeModelAwareExtensionProcessor.java
@@ -35,7 +35,7 @@ public class ScopeModelAwareExtensionProcessor implements
ExtensionPostProcessor
// Only the visible and only matching scope model can be injected,
that is, module -> application -> framework.
// The converse is a one-to-many relationship and cannot be injected.
// One framework may have multiple applications, and one application
may have multiple modules.
- // So, the spi extension/bean of application scope can be injected
it's application model and framework model,
+ // So, the spi extension/bean of application scope can be injected its
application model and framework model,
// but the spi extension/bean of framework scope cannot be injected an
application or module model.
if (scopeModel instanceof FrameworkModel) {
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ServiceMetadata.java
b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ServiceMetadata.java
index 65ee092..d838975 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ServiceMetadata.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ServiceMetadata.java
@@ -26,7 +26,7 @@ import java.util.concurrent.ConcurrentHashMap;
* Notice, this class currently has no usage inside Dubbo.
*
* data related to service level such as name, version, classloader of
business service,
- * security info, etc. Also with a AttributeMap for extension.
+ * security info, etc. Also, with a AttributeMap for extension.
*/
public class ServiceMetadata extends BaseServiceMetadata {
diff --git
a/dubbo-common/src/test/java/org/apache/dubbo/common/CommonScopeModelInitializerTest.java
b/dubbo-common/src/test/java/org/apache/dubbo/common/CommonScopeModelInitializerTest.java
new file mode 100644
index 0000000..3062c58
--- /dev/null
+++
b/dubbo-common/src/test/java/org/apache/dubbo/common/CommonScopeModelInitializerTest.java
@@ -0,0 +1,62 @@
+/*
+ * 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.beans.factory.ScopeBeanFactory;
+import org.apache.dubbo.common.config.ConfigurationCache;
+import org.apache.dubbo.common.lang.ShutdownHookCallbacks;
+import org.apache.dubbo.common.status.reporter.FrameworkStatusReportService;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.apache.dubbo.rpc.model.FrameworkModel;
+import org.apache.dubbo.rpc.model.ModuleModel;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * {@link CommonScopeModelInitializer}
+ */
+public class CommonScopeModelInitializerTest {
+
+ private FrameworkModel frameworkModel;
+ private ApplicationModel applicationModel;
+ private ModuleModel moduleModel;
+
+ @BeforeEach
+ public void setUp() {
+ frameworkModel = new FrameworkModel();
+ applicationModel = new ApplicationModel(frameworkModel);
+ moduleModel = new ModuleModel(applicationModel);
+ }
+
+ @AfterEach
+ public void reset() {
+ frameworkModel.destroy();
+ }
+
+ @Test
+ public void test() {
+ ScopeBeanFactory applicationModelBeanFactory =
applicationModel.getBeanFactory();
+
Assertions.assertNotNull(applicationModelBeanFactory.getBean(ShutdownHookCallbacks.class));
+
Assertions.assertNotNull(applicationModelBeanFactory.getBean(FrameworkStatusReportService.class));
+
Assertions.assertNotNull(applicationModelBeanFactory.getBean(ConfigurationCache.class));
+
+ ScopeBeanFactory moduleModelBeanFactory = moduleModel.getBeanFactory();
+
Assertions.assertNotNull(moduleModelBeanFactory.getBean(ConfigurationCache.class));
+ }
+}
diff --git
a/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/FrameworkServiceRepositoryTest.java
b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/FrameworkServiceRepositoryTest.java
new file mode 100644
index 0000000..6bdef4f
--- /dev/null
+++
b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/FrameworkServiceRepositoryTest.java
@@ -0,0 +1,113 @@
+/*
+ * 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.model;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.utils.StringUtils;
+import org.apache.dubbo.rpc.support.DemoService;
+import org.apache.dubbo.rpc.support.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 java.util.List;
+
+import static
org.apache.dubbo.common.BaseServiceMetadata.interfaceFromServiceKey;
+import static
org.apache.dubbo.common.BaseServiceMetadata.versionFromServiceKey;
+
+/**
+ * {@link FrameworkServiceRepository}
+ */
+public class FrameworkServiceRepositoryTest {
+ private FrameworkModel frameworkModel;
+ private ApplicationModel applicationModel;
+ private ModuleModel moduleModel;
+
+ @BeforeEach
+ public void setUp() {
+ frameworkModel = new FrameworkModel();
+ applicationModel = new ApplicationModel(frameworkModel);
+ moduleModel = new ModuleModel(applicationModel);
+ }
+
+ @AfterEach
+ public void reset() {
+ frameworkModel.destroy();
+ }
+
+ @Test
+ public void test() {
+ FrameworkServiceRepository frameworkServiceRepository =
frameworkModel.getServiceRepository();
+ ModuleServiceRepository moduleServiceRepository =
moduleModel.getServiceRepository();
+
+ ServiceMetadata serviceMetadata = new
ServiceMetadata(DemoService.class.getName(), "GROUP", "1.0.0",
DemoService.class);
+ ServiceDescriptor serviceDescriptor =
moduleServiceRepository.registerService(DemoService.class);
+ String serviceKey = serviceMetadata.getServiceKey();
+ ProviderModel providerModel = new ProviderModel(serviceKey,
+ new DemoServiceImpl(),
+ serviceDescriptor,
+ null,
+ moduleModel,
+ serviceMetadata);
+ frameworkServiceRepository.registerProvider(providerModel);
+
+ ProviderModel lookupExportedService =
frameworkServiceRepository.lookupExportedService(serviceKey);
+ Assertions.assertEquals(lookupExportedService, providerModel);
+
+ List<ProviderModel> allProviderModels =
frameworkServiceRepository.allProviderModels();
+ Assertions.assertEquals(allProviderModels.size(), 1);
+ Assertions.assertEquals(allProviderModels.get(0), providerModel);
+
+ String keyWithoutGroup = keyWithoutGroup(serviceKey);
+ ProviderModel exportedServiceWithoutGroup =
frameworkServiceRepository.lookupExportedServiceWithoutGroup(keyWithoutGroup);
+ Assertions.assertEquals(exportedServiceWithoutGroup, providerModel);
+
+ List<ProviderModel> providerModels =
frameworkServiceRepository.lookupExportedServicesWithoutGroup(keyWithoutGroup);
+ Assertions.assertEquals(providerModels.size(), 1);
+ Assertions.assertEquals(providerModels.get(0), providerModel);
+
+ URL url = URL.valueOf("test://127.0.0.1:9103/" +
DemoService.class.getName() + "?group=GROUP&version=1.0.0");
+ frameworkServiceRepository.registerProviderUrl(url);
+ List<URL> urls =
frameworkServiceRepository.lookupRegisteredProviderUrlsWithoutGroup(keyWithoutGroup(url.getServiceKey()));
+ Assertions.assertEquals(urls.size(), 1);
+ Assertions.assertEquals(urls.get(0), url);
+
+ ConsumerModel consumerModel = new ConsumerModel(
+ serviceMetadata.getServiceKey(), new DemoServiceImpl(),
serviceDescriptor, null,
+ moduleModel, serviceMetadata, null);
+ moduleServiceRepository.registerConsumer(consumerModel);
+ List<ConsumerModel> consumerModels =
frameworkServiceRepository.allConsumerModels();
+ Assertions.assertEquals(consumerModels.size(), 1);
+ Assertions.assertEquals(consumerModels.get(0), consumerModel);
+
+ frameworkServiceRepository.unregisterProvider(providerModel);
+
Assertions.assertNull(frameworkServiceRepository.lookupExportedService(serviceKey));
+
Assertions.assertNull(frameworkServiceRepository.lookupExportedServiceWithoutGroup(keyWithoutGroup));
+
Assertions.assertNull(frameworkServiceRepository.lookupRegisteredProviderUrlsWithoutGroup(keyWithoutGroup));
+
+ }
+
+ private static String keyWithoutGroup(String serviceKey) {
+ String interfaceName = interfaceFromServiceKey(serviceKey);
+ String version = versionFromServiceKey(serviceKey);
+ if (StringUtils.isEmpty(version)) {
+ return interfaceName;
+ }
+ return interfaceName + ":" + version;
+ }
+}
diff --git
a/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/ModuleServiceRepositoryTest.java
b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/ModuleServiceRepositoryTest.java
new file mode 100644
index 0000000..320bb2d
--- /dev/null
+++
b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/ModuleServiceRepositoryTest.java
@@ -0,0 +1,113 @@
+/*
+ * 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.model;
+
+import org.apache.dubbo.rpc.support.DemoService;
+import org.apache.dubbo.rpc.support.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 java.util.List;
+
+/**
+ * {@link ModuleServiceRepository}
+ */
+public class ModuleServiceRepositoryTest {
+
+ private FrameworkModel frameworkModel;
+ private ApplicationModel applicationModel;
+ private ModuleModel moduleModel;
+
+ @BeforeEach
+ public void setUp() {
+ frameworkModel = new FrameworkModel();
+ applicationModel = new ApplicationModel(frameworkModel);
+ moduleModel = new ModuleModel(applicationModel);
+ }
+
+ @AfterEach
+ public void reset() {
+ frameworkModel.destroy();
+ }
+
+ @Test
+ public void test() {
+ ModuleServiceRepository moduleServiceRepository = new
ModuleServiceRepository(moduleModel);
+ Assertions.assertEquals(moduleServiceRepository.getModuleModel(),
moduleModel);
+ ModuleServiceRepository repository =
moduleModel.getServiceRepository();
+
+ // 1.test service
+ ServiceMetadata serviceMetadata = new
ServiceMetadata(DemoService.class.getName(), null, null, DemoService.class);
+ ServiceDescriptor serviceDescriptor =
repository.registerService(DemoService.class);
+
+ ServiceDescriptor lookupServiceResult =
repository.lookupService(DemoService.class.getName());
+ Assertions.assertEquals(lookupServiceResult, serviceDescriptor);
+
+ List<ServiceDescriptor> allServices = repository.getAllServices();
+ Assertions.assertEquals(allServices.size(), 1);
+ Assertions.assertEquals(allServices.get(0), serviceDescriptor);
+
+ ServiceDescriptor serviceDescriptor1 =
repository.registerService(DemoService.class.getSimpleName(),
DemoService.class);
+ Assertions.assertEquals(serviceDescriptor1, serviceDescriptor);
+
+
+ // 2.test consumerModule
+ ConsumerModel consumerModel = new ConsumerModel(
+ serviceMetadata.getServiceKey(), new DemoServiceImpl(),
serviceDescriptor, null,
+ moduleModel, serviceMetadata, null);
+ repository.registerConsumer(consumerModel);
+
+ List<ConsumerModel> allReferredServices =
repository.getReferredServices();
+ Assertions.assertEquals(allReferredServices.size(), 1);
+ Assertions.assertEquals(allReferredServices.get(0), consumerModel);
+
+ List<ConsumerModel> referredServices =
repository.lookupReferredServices(DemoService.class.getName());
+ Assertions.assertEquals(referredServices.size(), 1);
+ Assertions.assertEquals(referredServices.get(0), consumerModel);
+
+ ConsumerModel referredService =
repository.lookupReferredService(DemoService.class.getName());
+ Assertions.assertEquals(referredService, consumerModel);
+
+ // 3.test providerModel
+ ProviderModel providerModel = new
ProviderModel(DemoService.class.getName(),
+ new DemoServiceImpl(),
+ serviceDescriptor,
+ null,
+ moduleModel,
+ serviceMetadata);
+ repository.registerProvider(providerModel);
+ List<ProviderModel> allExportedServices =
repository.getExportedServices();
+ Assertions.assertEquals(allExportedServices.size(), 1);
+ Assertions.assertEquals(allExportedServices.get(0), providerModel);
+
+ ProviderModel exportedService =
repository.lookupExportedService(DemoService.class.getName());
+ Assertions.assertEquals(exportedService, providerModel);
+
+ List<ProviderModel> providerModels =
frameworkModel.getServiceRepository().allProviderModels();
+ Assertions.assertEquals(providerModels.size(), 1);
+ Assertions.assertEquals(providerModels.get(0), providerModel);
+
+ // 4.test destroy
+ repository.destroy();
+ Assertions.assertTrue(repository.getAllServices().isEmpty());
+ Assertions.assertTrue(repository.getReferredServices().isEmpty());
+ Assertions.assertTrue(repository.getExportedServices().isEmpty());
+
Assertions.assertTrue(frameworkModel.getServiceRepository().allProviderModels().isEmpty());
+ }
+}
diff --git
a/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/ScopeModelAwareExtensionProcessorTest.java
b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/ScopeModelAwareExtensionProcessorTest.java
new file mode 100644
index 0000000..aa100e1
--- /dev/null
+++
b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/ScopeModelAwareExtensionProcessorTest.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.model;
+
+import org.apache.dubbo.rpc.support.MockScopeModelAware;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * {@link ScopeModelAwareExtensionProcessor}
+ */
+public class ScopeModelAwareExtensionProcessorTest {
+ private FrameworkModel frameworkModel;
+ private ApplicationModel applicationModel;
+ private ModuleModel moduleModel;
+
+ @BeforeEach
+ public void setUp() {
+ frameworkModel = new FrameworkModel();
+ applicationModel = new ApplicationModel(frameworkModel);
+ moduleModel = new ModuleModel(applicationModel);
+ }
+
+ @AfterEach
+ public void reset() {
+ frameworkModel.destroy();
+ }
+
+ @Test
+ public void testInitialize() {
+ ScopeModelAwareExtensionProcessor processor1 = new
ScopeModelAwareExtensionProcessor(frameworkModel);
+ Assertions.assertEquals(processor1.getFrameworkModel(),
frameworkModel);
+ Assertions.assertEquals(processor1.getScopeModel(), frameworkModel);
+ Assertions.assertNull(processor1.getApplicationModel());
+ Assertions.assertNull(processor1.getModuleModel());
+
+ ScopeModelAwareExtensionProcessor processor2 = new
ScopeModelAwareExtensionProcessor(applicationModel);
+ Assertions.assertEquals(processor2.getApplicationModel(),
applicationModel);
+ Assertions.assertEquals(processor2.getScopeModel(), applicationModel);
+ Assertions.assertEquals(processor2.getFrameworkModel(),
frameworkModel);
+ Assertions.assertNull(processor2.getModuleModel());
+
+ ScopeModelAwareExtensionProcessor processor3 = new
ScopeModelAwareExtensionProcessor(moduleModel);
+ Assertions.assertEquals(processor3.getModuleModel(), moduleModel);
+ Assertions.assertEquals(processor3.getScopeModel(), moduleModel);
+ Assertions.assertEquals(processor2.getApplicationModel(),
applicationModel);
+ Assertions.assertEquals(processor2.getFrameworkModel(),
frameworkModel);
+ }
+
+ @Test
+ public void testPostProcessAfterInitialization() throws Exception {
+ ScopeModelAwareExtensionProcessor processor = new
ScopeModelAwareExtensionProcessor(moduleModel);
+ MockScopeModelAware mockScopeModelAware = new MockScopeModelAware();
+ Object object =
processor.postProcessAfterInitialization(mockScopeModelAware,
mockScopeModelAware.getClass().getName());
+ Assertions.assertEquals(object, mockScopeModelAware);
+
+ Assertions.assertEquals(mockScopeModelAware.getScopeModel(),
moduleModel);
+ Assertions.assertEquals(mockScopeModelAware.getFrameworkModel(),
frameworkModel);
+ Assertions.assertEquals(mockScopeModelAware.getApplicationModel(),
applicationModel);
+ Assertions.assertEquals(mockScopeModelAware.getModuleModel(),
moduleModel);
+ }
+}
diff --git
a/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/ScopeModelUtilTest.java
b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/ScopeModelUtilTest.java
new file mode 100644
index 0000000..7720d10
--- /dev/null
+++
b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/ScopeModelUtilTest.java
@@ -0,0 +1,114 @@
+/*
+ * 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.model;
+
+import org.apache.dubbo.common.config.Environment;
+import org.apache.dubbo.common.extension.ExtensionScope;
+import org.apache.dubbo.common.extension.SPI;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * {@link ScopeModelUtil}
+ */
+public class ScopeModelUtilTest {
+ private FrameworkModel frameworkModel;
+ private ApplicationModel applicationModel;
+ private ModuleModel moduleModel;
+
+ @BeforeEach
+ public void setUp() {
+ frameworkModel = new FrameworkModel();
+ applicationModel = new ApplicationModel(frameworkModel);
+ moduleModel = new ModuleModel(applicationModel);
+ }
+
+ @AfterEach
+ public void reset() {
+ frameworkModel.destroy();
+ }
+
+ @Test
+ public void test() {
+
+ Assertions.assertEquals(ScopeModelUtil.getFrameworkModel(null),
FrameworkModel.defaultModel());
+
Assertions.assertEquals(ScopeModelUtil.getFrameworkModel(frameworkModel),
frameworkModel);
+
Assertions.assertEquals(ScopeModelUtil.getFrameworkModel(applicationModel),
frameworkModel);
+ Assertions.assertEquals(ScopeModelUtil.getFrameworkModel(moduleModel),
frameworkModel);
+ Assertions.assertThrows(IllegalArgumentException.class, () ->
ScopeModelUtil.getFrameworkModel(new MockScopeModel(null, null)));
+
+ Assertions.assertEquals(ScopeModelUtil.getApplicationModel(null),
ApplicationModel.defaultModel());
+
Assertions.assertEquals(ScopeModelUtil.getApplicationModel(applicationModel),
applicationModel);
+
Assertions.assertEquals(ScopeModelUtil.getApplicationModel(moduleModel),
applicationModel);
+ Assertions.assertThrows(IllegalArgumentException.class, () ->
ScopeModelUtil.getApplicationModel(frameworkModel));
+
+ Assertions.assertEquals(ScopeModelUtil.getModuleModel(null),
ApplicationModel.defaultModel().getDefaultModule());
+ Assertions.assertEquals(ScopeModelUtil.getModuleModel(moduleModel),
moduleModel);
+ Assertions.assertThrows(IllegalArgumentException.class, () ->
ScopeModelUtil.getModuleModel(frameworkModel));
+ Assertions.assertThrows(IllegalArgumentException.class, () ->
ScopeModelUtil.getModuleModel(applicationModel));
+
+ Assertions.assertEquals(ScopeModelUtil.getOrDefault(null,
SPIDemo1.class), FrameworkModel.defaultModel());
+ Assertions.assertEquals(ScopeModelUtil.getOrDefault(null,
SPIDemo2.class), ApplicationModel.defaultModel());
+ Assertions.assertEquals(ScopeModelUtil.getOrDefault(null,
SPIDemo3.class), ApplicationModel.defaultModel().getDefaultModule());
+ Assertions.assertThrows(IllegalArgumentException.class, () ->
ScopeModelUtil.getOrDefault(null, SPIDemo4.class));
+
+
Assertions.assertEquals(ScopeModelUtil.getExtensionLoader(SPIDemo1.class,
null), FrameworkModel.defaultModel().getExtensionLoader(SPIDemo1.class));
+
Assertions.assertEquals(ScopeModelUtil.getExtensionLoader(SPIDemo2.class,
null), ApplicationModel.defaultModel().getExtensionLoader(SPIDemo2.class));
+
Assertions.assertEquals(ScopeModelUtil.getExtensionLoader(SPIDemo3.class,
null),
ApplicationModel.defaultModel().getDefaultModule().getExtensionLoader(SPIDemo3.class));
+ Assertions.assertThrows(IllegalArgumentException.class, () ->
ScopeModelUtil.getExtensionLoader(SPIDemo4.class, null));
+ }
+
+
+ @SPI(scope = ExtensionScope.FRAMEWORK)
+ interface SPIDemo1 {
+
+ }
+
+ @SPI(scope = ExtensionScope.APPLICATION)
+ interface SPIDemo2 {
+
+ }
+
+ @SPI(scope = ExtensionScope.MODULE)
+ interface SPIDemo3 {
+
+ }
+
+ interface SPIDemo4 {
+
+ }
+
+ class MockScopeModel extends ScopeModel {
+ public MockScopeModel(ScopeModel parent, ExtensionScope scope) {
+ super(parent, scope);
+ }
+
+ @Override
+ protected void onDestroy() {
+
+ }
+
+ @Override
+ public Environment getModelEnvironment() {
+ return null;
+ }
+ }
+
+}
+
diff --git
a/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/ServiceRepositoryTest.java
b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/ServiceRepositoryTest.java
new file mode 100644
index 0000000..917e7dc
--- /dev/null
+++
b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/ServiceRepositoryTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.model;
+
+import org.apache.dubbo.rpc.support.DemoService;
+import org.apache.dubbo.rpc.support.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 java.util.Collection;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * {@link ServiceRepository}
+ */
+public class ServiceRepositoryTest {
+ private FrameworkModel frameworkModel;
+ private ApplicationModel applicationModel;
+ private ModuleModel moduleModel;
+
+ @BeforeEach
+ public void setUp() {
+ frameworkModel = new FrameworkModel();
+ applicationModel = new ApplicationModel(frameworkModel);
+ moduleModel = new ModuleModel(applicationModel);
+ }
+
+ @AfterEach
+ public void reset() {
+ frameworkModel.destroy();
+ }
+
+ @Test
+ public void test() {
+ // verify BuiltinService
+ Set<BuiltinServiceDetector> builtinServices
+ =
applicationModel.getExtensionLoader(BuiltinServiceDetector.class).getSupportedExtensionInstances();
+ ModuleServiceRepository moduleServiceRepository =
applicationModel.getInternalModule().getServiceRepository();
+ List<ServiceDescriptor> allServices =
moduleServiceRepository.getAllServices();
+ Assertions.assertEquals(allServices.size(), builtinServices.size());
+
+ ModuleServiceRepository repository =
moduleModel.getServiceRepository();
+ ServiceMetadata serviceMetadata = new
ServiceMetadata(DemoService.class.getName(), null, null, DemoService.class);
+ ServiceDescriptor serviceDescriptor =
repository.registerService(DemoService.class);
+
+ // registerConsumer
+ ConsumerModel consumerModel = new ConsumerModel(
+ serviceMetadata.getServiceKey(), new DemoServiceImpl(),
serviceDescriptor, null,
+ moduleModel, serviceMetadata, null);
+ repository.registerConsumer(consumerModel);
+
+ // registerProvider
+ ProviderModel providerModel = new
ProviderModel(DemoService.class.getName(),
+ new DemoServiceImpl(),
+ serviceDescriptor,
+ null,
+ moduleModel,
+ serviceMetadata);
+ repository.registerProvider(providerModel);
+
+ // verify allProviderModels, allConsumerModels
+ ServiceRepository serviceRepository =
applicationModel.getApplicationServiceRepository();
+ Collection<ProviderModel> providerModels =
serviceRepository.allProviderModels();
+ Assertions.assertEquals(providerModels.size(), 1);
+ Assertions.assertTrue(providerModels.contains(providerModel));
+
+ Collection<ConsumerModel> consumerModels =
serviceRepository.allConsumerModels();
+ Assertions.assertEquals(consumerModels.size(), 1);
+ Assertions.assertTrue(consumerModels.contains(consumerModel));
+
+ }
+
+}
diff --git
a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/SpringScopeModelInitializer.java
b/dubbo-common/src/test/java/org/apache/dubbo/rpc/support/DemoService.java
similarity index 50%
copy from
dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/SpringScopeModelInitializer.java
copy to dubbo-common/src/test/java/org/apache/dubbo/rpc/support/DemoService.java
index cd03ba7..7a8e97b 100644
---
a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/SpringScopeModelInitializer.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/rpc/support/DemoService.java
@@ -14,31 +14,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.dubbo.config.spring;
+package org.apache.dubbo.rpc.support;
-import org.apache.dubbo.common.beans.factory.ScopeBeanFactory;
-import org.apache.dubbo.rpc.model.ApplicationModel;
-import org.apache.dubbo.rpc.model.FrameworkModel;
-import org.apache.dubbo.rpc.model.ModuleModel;
-import org.apache.dubbo.rpc.model.ScopeModelInitializer;
-
-/**
- * Register scope beans in spring module
- */
-public class SpringScopeModelInitializer implements ScopeModelInitializer {
-
- @Override
- public void initializeFrameworkModel(FrameworkModel frameworkModel) {
-
- }
-
- @Override
- public void initializeApplicationModel(ApplicationModel applicationModel) {
- ScopeBeanFactory beanFactory = applicationModel.getBeanFactory();
- }
-
- @Override
- public void initializeModuleModel(ModuleModel moduleModel) {
-
- }
+public interface DemoService {
+ String sayHello(String name);
}
diff --git
a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/SpringScopeModelInitializer.java
b/dubbo-common/src/test/java/org/apache/dubbo/rpc/support/DemoServiceImpl.java
similarity index 51%
copy from
dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/SpringScopeModelInitializer.java
copy to
dubbo-common/src/test/java/org/apache/dubbo/rpc/support/DemoServiceImpl.java
index cd03ba7..cf81be8 100644
---
a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/SpringScopeModelInitializer.java
+++
b/dubbo-common/src/test/java/org/apache/dubbo/rpc/support/DemoServiceImpl.java
@@ -14,31 +14,11 @@
* 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.beans.factory.ScopeBeanFactory;
-import org.apache.dubbo.rpc.model.ApplicationModel;
-import org.apache.dubbo.rpc.model.FrameworkModel;
-import org.apache.dubbo.rpc.model.ModuleModel;
-import org.apache.dubbo.rpc.model.ScopeModelInitializer;
-
-/**
- * Register scope beans in spring module
- */
-public class SpringScopeModelInitializer implements ScopeModelInitializer {
+package org.apache.dubbo.rpc.support;
+public class DemoServiceImpl implements DemoService {
@Override
- public void initializeFrameworkModel(FrameworkModel frameworkModel) {
-
- }
-
- @Override
- public void initializeApplicationModel(ApplicationModel applicationModel) {
- ScopeBeanFactory beanFactory = applicationModel.getBeanFactory();
- }
-
- @Override
- public void initializeModuleModel(ModuleModel moduleModel) {
-
+ public String sayHello(String name) {
+ return "hello " + name;
}
}
diff --git
a/dubbo-common/src/test/java/org/apache/dubbo/rpc/support/MockScopeModelAware.java
b/dubbo-common/src/test/java/org/apache/dubbo/rpc/support/MockScopeModelAware.java
new file mode 100644
index 0000000..c4c3e7b
--- /dev/null
+++
b/dubbo-common/src/test/java/org/apache/dubbo/rpc/support/MockScopeModelAware.java
@@ -0,0 +1,72 @@
+/*
+ * 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.rpc.model.ApplicationModel;
+import org.apache.dubbo.rpc.model.FrameworkModel;
+import org.apache.dubbo.rpc.model.ModuleModel;
+import org.apache.dubbo.rpc.model.ScopeModel;
+import org.apache.dubbo.rpc.model.ScopeModelAccessor;
+import org.apache.dubbo.rpc.model.ScopeModelAware;
+
+public class MockScopeModelAware implements ScopeModelAware,
ScopeModelAccessor {
+
+ private ScopeModel scopeModel;
+ private FrameworkModel frameworkModel;
+ private ApplicationModel applicationModel;
+ private ModuleModel moduleModel;
+
+ @Override
+ public ScopeModel getScopeModel() {
+ return scopeModel;
+ }
+
+ @Override
+ public FrameworkModel getFrameworkModel() {
+ return frameworkModel;
+ }
+
+ @Override
+ public ApplicationModel getApplicationModel() {
+ return applicationModel;
+ }
+
+ @Override
+ public ModuleModel getModuleModel() {
+ return moduleModel;
+ }
+
+ @Override
+ public void setScopeModel(ScopeModel scopeModel) {
+ this.scopeModel = scopeModel;
+ }
+
+ @Override
+ public void setFrameworkModel(FrameworkModel frameworkModel) {
+ this.frameworkModel = frameworkModel;
+ }
+
+ @Override
+ public void setApplicationModel(ApplicationModel applicationModel) {
+ this.applicationModel = applicationModel;
+ }
+
+ @Override
+ public void setModuleModel(ModuleModel moduleModel) {
+ this.moduleModel = moduleModel;
+ }
+}
diff --git
a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/SpringScopeModelInitializer.java
b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/SpringScopeModelInitializer.java
index cd03ba7..a0750ee 100644
---
a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/SpringScopeModelInitializer.java
+++
b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/SpringScopeModelInitializer.java
@@ -16,7 +16,6 @@
*/
package org.apache.dubbo.config.spring;
-import org.apache.dubbo.common.beans.factory.ScopeBeanFactory;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.model.ModuleModel;
@@ -34,7 +33,7 @@ public class SpringScopeModelInitializer implements
ScopeModelInitializer {
@Override
public void initializeApplicationModel(ApplicationModel applicationModel) {
- ScopeBeanFactory beanFactory = applicationModel.getBeanFactory();
+
}
@Override