Skip to content

Commit

Permalink
Add mock inject error code (#1241) (#1242)
Browse files Browse the repository at this point in the history
  • Loading branch information
HzjNeverStop authored Aug 31, 2023
1 parent a4a8a30 commit b274315
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));

Expand All @@ -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 {
Expand Down Expand Up @@ -142,9 +141,7 @@ private String getBeanName(ConfigurableListableBeanFactory beanFactory, Definiti
Set<String> 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();
Expand All @@ -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<String> getExistingBeans(ConfigurableListableBeanFactory beanFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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




Expand Down

0 comments on commit b274315

Please sign in to comment.