From b2743159bfe4f9c95e3df3b2ac226fe1eb4543c2 Mon Sep 17 00:00:00 2001 From: HzjNeverStop <441627022@qq.com> Date: Thu, 31 Aug 2023 15:28:40 +0800 Subject: [PATCH] Add mock inject error code (#1241) (#1242) --- .../InjectorMockTestExecutionListener.java | 4 ++-- .../injector/resolver/BeanInjectorResolver.java | 16 ++++++---------- .../mock/injector/resolver/BeanInjectorStub.java | 4 ++-- .../resources/sofa-boot/log-codes.properties | 8 ++++++++ 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/sofa-boot-project/sofa-boot-core/test-sofa-boot/src/main/java/com/alipay/sofa/test/mock/injector/InjectorMockTestExecutionListener.java b/sofa-boot-project/sofa-boot-core/test-sofa-boot/src/main/java/com/alipay/sofa/test/mock/injector/InjectorMockTestExecutionListener.java index 98d01ab71..edeea6bd1 100644 --- a/sofa-boot-project/sofa-boot-core/test-sofa-boot/src/main/java/com/alipay/sofa/test/mock/injector/InjectorMockTestExecutionListener.java +++ b/sofa-boot-project/sofa-boot-core/test-sofa-boot/src/main/java/com/alipay/sofa/test/mock/injector/InjectorMockTestExecutionListener.java @@ -16,6 +16,7 @@ */ package com.alipay.sofa.test.mock.injector; +import com.alipay.sofa.boot.log.ErrorCode; import com.alipay.sofa.test.mock.injector.annotation.MockBeanInjector; import com.alipay.sofa.test.mock.injector.annotation.SpyBeanInjector; import com.alipay.sofa.test.mock.injector.definition.Definition; @@ -85,8 +86,7 @@ private void injectTestClass(DefinitionParser parser, TestContext testContext) { if (existingValue == injectValue) { return; } - Assert.state(existingValue == null, () -> "The existing value '" + existingValue + "' of field '" + field - + "' is not the same as the new value '" + injectValue + "'"); + Assert.state(existingValue == null, () -> ErrorCode.convert("01-30000", existingValue, field, injectValue)); ReflectionUtils.setField(field, target, injectValue); } }); diff --git a/sofa-boot-project/sofa-boot-core/test-sofa-boot/src/main/java/com/alipay/sofa/test/mock/injector/resolver/BeanInjectorResolver.java b/sofa-boot-project/sofa-boot-core/test-sofa-boot/src/main/java/com/alipay/sofa/test/mock/injector/resolver/BeanInjectorResolver.java index db9d27edb..153a3bcb0 100644 --- a/sofa-boot-project/sofa-boot-core/test-sofa-boot/src/main/java/com/alipay/sofa/test/mock/injector/resolver/BeanInjectorResolver.java +++ b/sofa-boot-project/sofa-boot-core/test-sofa-boot/src/main/java/com/alipay/sofa/test/mock/injector/resolver/BeanInjectorResolver.java @@ -18,6 +18,7 @@ import com.alipay.sofa.boot.isle.ApplicationRuntimeModel; import com.alipay.sofa.boot.isle.IsleDeploymentModel; +import com.alipay.sofa.boot.log.ErrorCode; import com.alipay.sofa.test.mock.injector.definition.Definition; import com.alipay.sofa.test.mock.injector.definition.QualifierDefinition; import org.springframework.aop.framework.AopProxyUtils; @@ -87,8 +88,7 @@ public BeanInjectorStub resolveStub(Definition definition) { // find target bean instance if (!beanFactory.containsBean(beanName)) { - throw new IllegalStateException("Unable to create bean injector to bean [" + beanName - + "] target bean not exist"); + throw new IllegalStateException(ErrorCode.convert("01-30005", beanName)); } Object bean = resolveTargetObject(beanFactory.getBean(beanName)); @@ -101,8 +101,7 @@ private ApplicationContext getApplicationContext(Definition definition) { if (StringUtils.hasText(module)) { ApplicationContext applicationContext = isleApplicationContexts.get(module); if (applicationContext == null) { - throw new IllegalStateException("Unable to find target module [" + module - + "] when resolve injector: " + definition); + throw new IllegalStateException(ErrorCode.convert("01-30002", module, definition)); } return applicationContext; } else { @@ -142,9 +141,7 @@ private String getBeanName(ConfigurableListableBeanFactory beanFactory, Definiti Set existingBeans = getExistingBeans(beanFactory, definition.getType(), definition.getQualifier()); if (existingBeans.isEmpty()) { - throw new IllegalStateException( - "Unable to create bean injector to bean by type [" + definition.getType() - + "] expected a single matching bean to injector but no bean found"); + throw new IllegalStateException(ErrorCode.convert("01-30003", definition.getType())); } if (existingBeans.size() == 1) { return existingBeans.iterator().next(); @@ -154,9 +151,8 @@ private String getBeanName(ConfigurableListableBeanFactory beanFactory, Definiti if (primaryCandidate != null) { return primaryCandidate; } - throw new IllegalStateException( - "Unable to create bean injector to bean by type [" + definition.getType() - + "] expected a single matching bean to injector but found " + existingBeans); + throw new IllegalStateException(ErrorCode.convert("01-30004", definition.getType(), + existingBeans)); } private Set getExistingBeans(ConfigurableListableBeanFactory beanFactory, diff --git a/sofa-boot-project/sofa-boot-core/test-sofa-boot/src/main/java/com/alipay/sofa/test/mock/injector/resolver/BeanInjectorStub.java b/sofa-boot-project/sofa-boot-core/test-sofa-boot/src/main/java/com/alipay/sofa/test/mock/injector/resolver/BeanInjectorStub.java index 8b2bcb6de..d3ea11f30 100644 --- a/sofa-boot-project/sofa-boot-core/test-sofa-boot/src/main/java/com/alipay/sofa/test/mock/injector/resolver/BeanInjectorStub.java +++ b/sofa-boot-project/sofa-boot-core/test-sofa-boot/src/main/java/com/alipay/sofa/test/mock/injector/resolver/BeanInjectorStub.java @@ -16,6 +16,7 @@ */ package com.alipay.sofa.test.mock.injector.resolver; +import com.alipay.sofa.boot.log.ErrorCode; import com.alipay.sofa.test.mock.injector.definition.Definition; import com.alipay.sofa.test.mock.injector.definition.MockDefinition; import com.alipay.sofa.test.mock.injector.definition.SpyDefinition; @@ -58,8 +59,7 @@ public BeanInjectorStub(Definition definition, Field field, Object bean) { ReflectionUtils.makeAccessible(field); this.originalValue = ReflectionUtils.getField(field, bean); if (definition instanceof SpyDefinition && this.originalValue == null) { - throw new IllegalStateException("Unable to create spy to inject target field " + field - + " when origin value is null"); + throw new IllegalStateException(ErrorCode.convert("01-30001", field)); } } diff --git a/sofa-boot-project/sofa-boot/src/main/resources/sofa-boot/log-codes.properties b/sofa-boot-project/sofa-boot/src/main/resources/sofa-boot/log-codes.properties index e47e1c87e..f36ecdcfb 100644 --- a/sofa-boot-project/sofa-boot/src/main/resources/sofa-boot/log-codes.properties +++ b/sofa-boot-project/sofa-boot/src/main/resources/sofa-boot/log-codes.properties @@ -99,6 +99,14 @@ 01-24001=SOFABoot ReadinessCheckCallback[%s] check failed, the details is: %s 01-24002=Error occurred while doing ReadinessCheckCallback[%s] check +#Test component error +01-30000=The existing value '%s' of field '%s' is not the same as the new value '%s' +01-30001=Unable to create spy to inject target field %s when origin value is null +01-30002=Unable to find target module [%s] when resolve injector: %s +01-30003=Unable to create bean injector to bean by type [%s] expected a single matching bean to injector but no bean found +01-30004=Unable to create bean injector to bean by type [%s] expected a single matching bean to injector but found %s +01-30005=Unable to create bean injector to bean [%s] target bean not exist +