From efa4873753a3afb1a71cdb4ba7839b5f70b1eba1 Mon Sep 17 00:00:00 2001 From: "lingxiao,wu" <51630311+lingxiao-wu@users.noreply.github.com> Date: Sat, 20 Aug 2022 22:30:15 +0800 Subject: [PATCH] =?UTF-8?q?optimize=EF=BC=9Aoptimize=20config=20module=20c?= =?UTF-8?q?ode=20(#520)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../spring/annotation/PolarisProcessor.java | 15 +- .../annotation/SpringValueProcessor.java | 2 +- .../spring/property/PlaceholderHelper.java | 9 +- .../config/spring/property/SpringValue.java | 11 +- .../annotation/SpringValueProcessorTest.java | 153 ++++++++++++++++++ .../config/spring/property/Person.java | 67 ++++++++ .../property/PlaceholderHelperTest.java | 88 ++++++++++ .../SpringValueDefinitionProcessorTest.java | 75 +++++++++ .../resources/application-test.properties | 2 + .../src/test/resources/bean.xml | 38 +++++ 11 files changed, 448 insertions(+), 13 deletions(-) create mode 100644 spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessorTest.java create mode 100644 spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/property/Person.java create mode 100644 spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/property/PlaceholderHelperTest.java create mode 100644 spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinitionProcessorTest.java create mode 100644 spring-cloud-starter-tencent-polaris-config/src/test/resources/application-test.properties create mode 100644 spring-cloud-starter-tencent-polaris-config/src/test/resources/bean.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index 401edce9..ee5f9db4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,3 +12,4 @@ - [Fix typo & Code optimization](https://github.com/Tencent/spring-cloud-tencent/pull/507) - [Bugfix:BeanFactoryUtils returns all beans including beans defined in ancestor bean factories](https://github.com/Tencent/spring-cloud-tencent/pull/515) - [fix:fix only config file metadata can be used in routing bug.](https://github.com/Tencent/spring-cloud-tencent/pull/518) +- [optimize:optimize config module code](https://github.com/Tencent/spring-cloud-tencent/pull/520) diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/PolarisProcessor.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/PolarisProcessor.java index f0ee2753..42f69f8d 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/PolarisProcessor.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/PolarisProcessor.java @@ -27,6 +27,7 @@ import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.core.Ordered; import org.springframework.core.PriorityOrdered; +import org.springframework.lang.NonNull; import org.springframework.util.ReflectionUtils; /** @@ -37,9 +38,9 @@ import org.springframework.util.ReflectionUtils; public abstract class PolarisProcessor implements BeanPostProcessor, PriorityOrdered { @Override - public Object postProcessBeforeInitialization(Object bean, String beanName) + public Object postProcessBeforeInitialization(Object bean, @NonNull String beanName) throws BeansException { - Class clazz = bean.getClass(); + Class clazz = bean.getClass(); for (Field field : findAllField(clazz)) { processField(bean, beanName, field); } @@ -50,7 +51,7 @@ public abstract class PolarisProcessor implements BeanPostProcessor, PriorityOrd } @Override - public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + public Object postProcessAfterInitialization(@NonNull Object bean, @NonNull String beanName) throws BeansException { return bean; } @@ -77,15 +78,15 @@ public abstract class PolarisProcessor implements BeanPostProcessor, PriorityOrd return Ordered.LOWEST_PRECEDENCE; } - private List findAllField(Class clazz) { + private List findAllField(Class clazz) { final List res = new LinkedList<>(); - ReflectionUtils.doWithFields(clazz, field -> res.add(field)); + ReflectionUtils.doWithFields(clazz, res::add); return res; } - private List findAllMethod(Class clazz) { + private List findAllMethod(Class clazz) { final List res = new LinkedList<>(); - ReflectionUtils.doWithMethods(clazz, method -> res.add(method)); + ReflectionUtils.doWithMethods(clazz, res::add); return res; } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java index da71bd70..24279e07 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java @@ -87,7 +87,7 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanFactor } @Override - public Object postProcessBeforeInitialization(Object bean, String beanName) + public Object postProcessBeforeInitialization(Object bean, @NonNull String beanName) throws BeansException { if (polarisConfigProperties.isAutoRefresh()) { super.postProcessBeforeInitialization(bean, beanName); diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/PlaceholderHelper.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/PlaceholderHelper.java index 2d751566..a2223dc7 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/PlaceholderHelper.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/PlaceholderHelper.java @@ -71,7 +71,7 @@ public class PlaceholderHelper { if (beanFactory.getBeanExpressionResolver() == null) { return value; } - Scope scope = (beanDefinition != null ? beanFactory + Scope scope = (beanDefinition != null && beanDefinition.getScope() != null ? beanFactory .getRegisteredScope(beanDefinition.getScope()) : null); return beanFactory.getBeanExpressionResolver() .evaluate(value, new BeanExpressionContext(beanFactory, scope)); @@ -92,7 +92,7 @@ public class PlaceholderHelper { public Set extractPlaceholderKeys(String propertyString) { Set placeholderKeys = Sets.newHashSet(); - if (Strings.isNullOrEmpty(propertyString) || (!isNormalizedPlaceholder(propertyString) && !isExpressionWithPlaceholder(propertyString))) { + if (!isPlaceholder(propertyString)) { return placeholderKeys; } @@ -147,6 +147,11 @@ public class PlaceholderHelper { return placeholderKeys; } + private boolean isPlaceholder(String propertyString) { + return !Strings.isNullOrEmpty(propertyString) && + (isNormalizedPlaceholder(propertyString) || isExpressionWithPlaceholder(propertyString)); + } + private boolean isNormalizedPlaceholder(String propertyString) { return propertyString.startsWith(PLACEHOLDER_PREFIX) && propertyString.contains(PLACEHOLDER_SUFFIX); } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValue.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValue.java index c092b96d..ddde1f52 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValue.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValue.java @@ -86,7 +86,7 @@ public class SpringValue { private void injectMethod(Object newVal) throws InvocationTargetException, IllegalAccessException { Object bean = beanRef.get(); - if (bean == null) { + if (bean == null || methodParameter.getMethod() == null) { return; } methodParameter.getMethod().invoke(bean, newVal); @@ -131,7 +131,12 @@ public class SpringValue { .format("key: %s, beanName: %s, field: %s.%s", key, beanName, bean.getClass() .getName(), field.getName()); } - return String.format("key: %s, beanName: %s, method: %s.%s", key, beanName, bean.getClass().getName(), - methodParameter.getMethod().getName()); + if (null != methodParameter.getMethod()) { + return String.format("key: %s, beanName: %s, method: %s.%s", key, beanName, bean.getClass().getName(), + methodParameter.getMethod().getName()); + } + else { + return String.format("key: %s, beanName: %s", key, beanName); + } } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessorTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessorTest.java new file mode 100644 index 00000000..fb57206b --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessorTest.java @@ -0,0 +1,153 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * 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 com.tencent.cloud.polaris.config.spring.annotation; + +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.Objects; +import java.util.Optional; + +import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration; +import com.tencent.cloud.polaris.config.enums.RefreshType; +import com.tencent.cloud.polaris.config.spring.property.SpringValue; +import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry; +import com.tencent.polaris.api.utils.CollectionUtils; +import org.junit.Assert; +import org.junit.Test; + +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; + +/** + * Test for {@link SpringValueProcessor}. + * + * @author lingxiao.wlx + */ +public class SpringValueProcessorTest { + + @Test + public void springValueFiledProcessorTest() { + ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(PolarisConfigBootstrapAutoConfiguration.class)) + .withConfiguration(AutoConfigurations.of(RefreshAutoConfiguration.class)) + .withConfiguration(AutoConfigurations.of(ValueTest.class)) + .withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class)) + .withPropertyValues("spring.application.name=" + "conditionalOnConfigReflectEnabledTest") + .withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081") + .withPropertyValues("spring.cloud.polaris.config.refresh-type=" + RefreshType.REFLECT) + .withPropertyValues("spring.cloud.polaris.config.enabled=true") + .withPropertyValues("timeout=10000"); + contextRunner.run(context -> { + SpringValueRegistry springValueRegistry = context.getBean(SpringValueRegistry.class); + PolarisConfigAutoConfiguration polarisConfigAutoConfiguration = context.getBean(PolarisConfigAutoConfiguration.class); + BeanFactory beanFactory = polarisConfigAutoConfiguration.beanFactory; + Collection timeout = springValueRegistry.get(beanFactory, "timeout"); + Assert.assertFalse(CollectionUtils.isEmpty(timeout)); + Optional springValueOptional = timeout.stream().findAny(); + Assert.assertTrue(springValueOptional.isPresent()); + + SpringValue springValue = springValueOptional.get(); + Assert.assertEquals("${timeout:1000}", springValue.getPlaceholder()); + Assert.assertTrue(springValue.isField()); + Assert.assertTrue(Objects.nonNull(springValue.getField())); + Assert.assertEquals("timeout", springValue.getField().getName()); + Assert.assertEquals(int.class, springValue.getTargetType()); + + ValueTest bean = context.getBean(ValueTest.class); + Assert.assertEquals(10000, bean.timeout); + }); + } + + @Test + public void springValueMethodProcessorTest() { + ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(PolarisConfigBootstrapAutoConfiguration.class)) + .withConfiguration(AutoConfigurations.of(RefreshAutoConfiguration.class)) + .withConfiguration(AutoConfigurations.of(ValueTest.class)) + .withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class)) + .withPropertyValues("spring.application.name=" + "conditionalOnConfigReflectEnabledTest") + .withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081") + .withPropertyValues("spring.cloud.polaris.config.refresh-type=" + RefreshType.REFLECT) + .withPropertyValues("spring.cloud.polaris.config.enabled=true") + .withPropertyValues("name=test"); + contextRunner.run(context -> { + SpringValueRegistry springValueRegistry = context.getBean(SpringValueRegistry.class); + PolarisConfigAutoConfiguration polarisConfigAutoConfiguration = context.getBean(PolarisConfigAutoConfiguration.class); + BeanFactory beanFactory = polarisConfigAutoConfiguration.beanFactory; + Collection name = springValueRegistry.get(beanFactory, "name"); + Assert.assertFalse(CollectionUtils.isEmpty(name)); + Optional springValueOptional = name.stream().findAny(); + Assert.assertTrue(springValueOptional.isPresent()); + + SpringValue springValue = springValueOptional.get(); + Method method = springValue.getMethodParameter().getMethod(); + Assert.assertTrue(Objects.nonNull(method)); + Assert.assertEquals("setName", method.getName()); + Assert.assertEquals("${name:1000}", springValue.getPlaceholder()); + Assert.assertFalse(springValue.isField()); + Assert.assertEquals(String.class, springValue.getTargetType()); + + Assert.assertEquals("test", ValueTest.name); + }); + } + + @Configuration + @EnableAutoConfiguration + static class PolarisConfigAutoConfiguration { + + @Autowired + private BeanFactory beanFactory; + + public BeanFactory getBeanFactory() { + return beanFactory; + } + + public void setBeanFactory(BeanFactory beanFactory) { + this.beanFactory = beanFactory; + } + } + + @Component + private static class ValueTest { + @Value("${timeout:1000}") + private int timeout; + + private static String name; + + public int getTimeout() { + return timeout; + } + + public void setTimeout(int timeout) { + this.timeout = timeout; + } + + @Value("${name:1000}") + public void setName(String name) { + ValueTest.name = name; + } + } +} diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/property/Person.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/property/Person.java new file mode 100644 index 00000000..5a09fb88 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/property/Person.java @@ -0,0 +1,67 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * 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 com.tencent.cloud.polaris.config.spring.property; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanFactoryAware; + +/** + * Test example. + * + * @author lingxiao.wlx + */ +public class Person implements BeanFactoryAware { + + private String name; + + private String age; + + private BeanFactory beanFactory; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAge() { + return age; + } + + public void setAge(String age) { + this.age = age; + } + + public BeanFactory getBeanFactory() { + return beanFactory; + } + + @Override + public String toString() { + return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; + } + + @Override + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { + this.beanFactory = beanFactory; + } +} diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/property/PlaceholderHelperTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/property/PlaceholderHelperTest.java new file mode 100644 index 00000000..a605dda8 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/property/PlaceholderHelperTest.java @@ -0,0 +1,88 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * 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 com.tencent.cloud.polaris.config.spring.property; + +import java.util.Set; + +import com.tencent.polaris.api.utils.CollectionUtils; +import org.junit.Assert; +import org.junit.Test; + +/** + * Test for {@link PlaceholderHelper}. + * + * @author lingxiao.wlx + */ +public class PlaceholderHelperTest { + + private static final PlaceholderHelper PLACEHOLDER_HELPER = new PlaceholderHelper(); + + @Test + public void extractNormalPlaceholderKeysTest() { + final String placeholderCase = "${some.key}"; + final String placeholderCase1 = "${some.key:${some.other.key:100}}"; + final String placeholderCase2 = "${${some.key}}"; + final String placeholderCase3 = "${${some.key:other.key}}"; + final String placeholderCase4 = "${${some.key}:${another.key}}"; + final String placeholderCase5 = "#{new java.text.SimpleDateFormat('${some.key}').parse('${another.key}')}"; + + Set placeholderKeys = PLACEHOLDER_HELPER.extractPlaceholderKeys(placeholderCase); + Assert.assertEquals(1, placeholderKeys.size()); + Assert.assertTrue(placeholderKeys.contains("some.key")); + + Set placeholderKeys1 = PLACEHOLDER_HELPER.extractPlaceholderKeys(placeholderCase1); + Assert.assertEquals(2, placeholderKeys1.size()); + Assert.assertTrue(placeholderKeys1.contains("some.key")); + Assert.assertTrue(placeholderKeys1.contains("some.other.key")); + + Set placeholderKeys2 = PLACEHOLDER_HELPER.extractPlaceholderKeys(placeholderCase2); + Assert.assertEquals(1, placeholderKeys2.size()); + Assert.assertTrue(placeholderKeys2.contains("some.key")); + + Set placeholderKeys3 = PLACEHOLDER_HELPER.extractPlaceholderKeys(placeholderCase3); + Assert.assertEquals(1, placeholderKeys3.size()); + Assert.assertTrue(placeholderKeys3.contains("some.key")); + + Set placeholderKeys4 = PLACEHOLDER_HELPER.extractPlaceholderKeys(placeholderCase4); + Assert.assertEquals(2, placeholderKeys4.size()); + Assert.assertTrue(placeholderKeys4.contains("some.key")); + Assert.assertTrue(placeholderKeys4.contains("another.key")); + + Set placeholderKeys5 = PLACEHOLDER_HELPER.extractPlaceholderKeys(placeholderCase5); + Assert.assertEquals(2, placeholderKeys5.size()); + Assert.assertTrue(placeholderKeys5.contains("some.key")); + Assert.assertTrue(placeholderKeys5.contains("another.key")); + } + + @Test + public void extractIllegalPlaceholderKeysTest() { + final String placeholderCase = "${some.key"; + final String placeholderCase1 = "{some.key}"; + final String placeholderCase2 = "some.key"; + + Set placeholderKeys = PLACEHOLDER_HELPER.extractPlaceholderKeys(placeholderCase); + Assert.assertTrue(CollectionUtils.isEmpty(placeholderKeys)); + + Set placeholderKeys1 = PLACEHOLDER_HELPER.extractPlaceholderKeys(placeholderCase1); + Assert.assertTrue(CollectionUtils.isEmpty(placeholderKeys1)); + + Set placeholderKeys2 = PLACEHOLDER_HELPER.extractPlaceholderKeys(placeholderCase2); + Assert.assertTrue(CollectionUtils.isEmpty(placeholderKeys2)); + } +} diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinitionProcessorTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinitionProcessorTest.java new file mode 100644 index 00000000..c82980a4 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinitionProcessorTest.java @@ -0,0 +1,75 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * 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 com.tencent.cloud.polaris.config.spring.property; + +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.Objects; +import java.util.Optional; + +import com.tencent.polaris.api.utils.CollectionUtils; +import org.junit.Assert; +import org.junit.Test; + +import org.springframework.beans.factory.BeanFactory; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * Test for {@link SpringValueDefinitionProcessor}. + * + * @author lingxiao.wlx + */ +public class SpringValueDefinitionProcessorTest { + + @Test + public void springValueDefinitionProcessorTest() { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); + Person person = context.getBean(Person.class); + + SpringValueRegistry springValueRegistry = context.getBean(SpringValueRegistry.class); + + BeanFactory beanFactory = person.getBeanFactory(); + Collection name = springValueRegistry.get(beanFactory, "name"); + Assert.assertFalse(CollectionUtils.isEmpty(name)); + Optional nameSpringValueOptional = name.stream().findAny(); + Assert.assertTrue(nameSpringValueOptional.isPresent()); + + SpringValue nameSpringValue = nameSpringValueOptional.get(); + Method method = nameSpringValue.getMethodParameter().getMethod(); + Assert.assertTrue(Objects.nonNull(method)); + Assert.assertEquals("setName", method.getName()); + Assert.assertEquals("${name:test}", nameSpringValue.getPlaceholder()); + Assert.assertFalse(nameSpringValue.isField()); + Assert.assertEquals(String.class, nameSpringValue.getTargetType()); + + + Collection age = springValueRegistry.get(beanFactory, "age"); + Assert.assertFalse(CollectionUtils.isEmpty(age)); + Optional ageSpringValueOptional = age.stream().findAny(); + Assert.assertTrue(ageSpringValueOptional.isPresent()); + + SpringValue ageSpringValue = ageSpringValueOptional.get(); + Method method1 = ageSpringValue.getMethodParameter().getMethod(); + Assert.assertTrue(Objects.nonNull(method1)); + Assert.assertEquals("setAge", method1.getName()); + Assert.assertEquals("${age:10}", ageSpringValue.getPlaceholder()); + Assert.assertFalse(ageSpringValue.isField()); + Assert.assertEquals(String.class, ageSpringValue.getTargetType()); + } +} diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/resources/application-test.properties b/spring-cloud-starter-tencent-polaris-config/src/test/resources/application-test.properties new file mode 100644 index 00000000..613f52d5 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/test/resources/application-test.properties @@ -0,0 +1,2 @@ +name=test +age=10 diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/resources/bean.xml b/spring-cloud-starter-tencent-polaris-config/src/test/resources/bean.xml new file mode 100644 index 00000000..e60613a5 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/test/resources/bean.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +