Merge branch '2021.0' of https://github.com/Tencent/spring-cloud-tencent into optimize/optimize-omponent-annotation
commit
3ffb3947ad
@ -1,34 +0,0 @@
|
|||||||
name: Codecov
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- hoxton
|
|
||||||
- 2021.0
|
|
||||||
- 2020.0
|
|
||||||
- greenwich
|
|
||||||
pull_request:
|
|
||||||
branches:
|
|
||||||
- hoxton
|
|
||||||
- 2021.0
|
|
||||||
- 2020.0
|
|
||||||
- greenwich
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Checkout codes
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: Set up JDK 8
|
|
||||||
uses: actions/setup-java@v3
|
|
||||||
with:
|
|
||||||
distribution: 'temurin'
|
|
||||||
java-version: 8
|
|
||||||
- name: Test with Maven
|
|
||||||
run: mvn -B test --file pom.xml
|
|
||||||
- name: Upload coverage to Codecov
|
|
||||||
uses: codecov/codecov-action@v3
|
|
||||||
with:
|
|
||||||
file: ${{ github.workspace }}/target/site/jacoco/jacoco.xml
|
|
@ -1,22 +1,4 @@
|
|||||||
# Change Log
|
# Change Log
|
||||||
---
|
---
|
||||||
|
|
||||||
- [Fix issue 593:When the project depends on spring-retry, executing restTemplate to call microservice and reporting NPE error](https://github.com/Tencent/spring-cloud-tencent/pull/594)
|
- [Optimize:optimize SpringValueProcessor.](https://github.com/Tencent/spring-cloud-tencent/pull/655)
|
||||||
- [Optimize: remove discovery module useless code](https://github.com/Tencent/spring-cloud-tencent/pull/595)
|
|
||||||
- [Optimize: remove useless code for rest template router](https://github.com/Tencent/spring-cloud-tencent/pull/601)
|
|
||||||
- [Optimize: optimize configuration conditional & optimize config data tips](https://github.com/Tencent/spring-cloud-tencent/pull/603)
|
|
||||||
- [Automatically transmit some headers specified by an environment variable that directly defines header keys.](https://github.com/Tencent/spring-cloud-tencent/pull/606)
|
|
||||||
- [Optimize: Maybe remove Chinese characters](https://github.com/Tencent/spring-cloud-tencent/pull/607)
|
|
||||||
- [Optimize: InstanceId of PolarisRegistration and PolarisServiceRegistry](https://github.com/Tencent/spring-cloud-tencent/pull/610)
|
|
||||||
- [ fix pr #606 cause ci fail](https://github.com/Tencent/spring-cloud-tencent/pull/613)
|
|
||||||
- [fix pr 613: modify a judgment logic](https://github.com/Tencent/spring-cloud-tencent/pull/618)
|
|
||||||
- [fix pr 606: modify a part of changes requested by review.](https://github.com/Tencent/spring-cloud-tencent/pull/620)
|
|
||||||
- [Bugfix: fix feign report call result error when using feign direct call](https://github.com/Tencent/spring-cloud-tencent/pull/621)
|
|
||||||
- [remove useless code for router](https://github.com/Tencent/spring-cloud-tencent/pull/626)
|
|
||||||
- [Feature: support new label expression](https://github.com/Tencent/spring-cloud-tencent/pull/627)
|
|
||||||
- [feat:report the labels when using RestTemplate.](https://github.com/Tencent/spring-cloud-tencent/pull/629)
|
|
||||||
- [Bugfix: fix throw npe when router context is null](https://github.com/Tencent/spring-cloud-tencent/pull/635)
|
|
||||||
- [Optimize:optimize transfer](https://github.com/Tencent/spring-cloud-tencent/pull/636)
|
|
||||||
- [fix:cancel reporting useless metadata.](https://github.com/Tencent/spring-cloud-tencent/pull/639)
|
|
||||||
- [Feature: support read config file from local file system](https://github.com/Tencent/spring-cloud-tencent/pull/640)
|
|
||||||
- [fix:optimize expression parser V1.](https://github.com/Tencent/spring-cloud-tencent/pull/642)
|
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
# Change Log
|
||||||
|
---
|
||||||
|
|
||||||
|
- [Fix issue 593:When the project depends on spring-retry, executing restTemplate to call microservice and reporting NPE error](https://github.com/Tencent/spring-cloud-tencent/pull/594)
|
||||||
|
- [Optimize: remove discovery module useless code](https://github.com/Tencent/spring-cloud-tencent/pull/595)
|
||||||
|
- [Optimize: remove useless code for rest template router](https://github.com/Tencent/spring-cloud-tencent/pull/601)
|
||||||
|
- [Optimize: optimize configuration conditional & optimize config data tips](https://github.com/Tencent/spring-cloud-tencent/pull/603)
|
||||||
|
- [Automatically transmit some headers specified by an environment variable that directly defines header keys.](https://github.com/Tencent/spring-cloud-tencent/pull/606)
|
||||||
|
- [Optimize: Maybe remove Chinese characters](https://github.com/Tencent/spring-cloud-tencent/pull/607)
|
||||||
|
- [Optimize: InstanceId of PolarisRegistration and PolarisServiceRegistry](https://github.com/Tencent/spring-cloud-tencent/pull/610)
|
||||||
|
- [ fix pr #606 cause ci fail](https://github.com/Tencent/spring-cloud-tencent/pull/613)
|
||||||
|
- [fix pr 613: modify a judgment logic](https://github.com/Tencent/spring-cloud-tencent/pull/618)
|
||||||
|
- [fix pr 606: modify a part of changes requested by review.](https://github.com/Tencent/spring-cloud-tencent/pull/620)
|
||||||
|
- [Bugfix: fix feign report call result error when using feign direct call](https://github.com/Tencent/spring-cloud-tencent/pull/621)
|
||||||
|
- [remove useless code for router](https://github.com/Tencent/spring-cloud-tencent/pull/626)
|
||||||
|
- [Feature: support new label expression](https://github.com/Tencent/spring-cloud-tencent/pull/627)
|
||||||
|
- [feat:report the labels when using RestTemplate.](https://github.com/Tencent/spring-cloud-tencent/pull/629)
|
||||||
|
- [Bugfix: fix throw npe when router context is null](https://github.com/Tencent/spring-cloud-tencent/pull/635)
|
||||||
|
- [Optimize:optimize transfer](https://github.com/Tencent/spring-cloud-tencent/pull/636)
|
||||||
|
- [fix:cancel reporting useless metadata.](https://github.com/Tencent/spring-cloud-tencent/pull/639)
|
||||||
|
- [Feature: support read config file from local file system](https://github.com/Tencent/spring-cloud-tencent/pull/640)
|
||||||
|
- [fix:optimize expression parser V1.](https://github.com/Tencent/spring-cloud-tencent/pull/642)
|
||||||
|
- [Optimize: add transfer metadata unit test.](https://github.com/Tencent/spring-cloud-tencent/pull/646)
|
||||||
|
- [feat: publish spring event named ConfigChangeSpringEvent when the configuration is changed](https://github.com/Tencent/spring-cloud-tencent/pull/647)
|
@ -0,0 +1,4 @@
|
|||||||
|
# Change Log
|
||||||
|
---
|
||||||
|
|
||||||
|
- [fix:fix transfer http headers not working bug.](https://github.com/Tencent/spring-cloud-tencent/pull/664)
|
@ -0,0 +1,86 @@
|
|||||||
|
/*
|
||||||
|
* 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.metadata.core;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||||
|
import com.tencent.cloud.common.util.JacksonUtils;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
||||||
|
import org.springframework.mock.web.MockHttpServletRequest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link TransHeadersTransfer}.
|
||||||
|
*
|
||||||
|
* @author lingxiao.wlx
|
||||||
|
*/
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(webEnvironment = RANDOM_PORT,
|
||||||
|
classes = DecodeTransferMetadataServletFilterTest.TestApplication.class,
|
||||||
|
properties = {"spring.config.location = classpath:application-test.yml"})
|
||||||
|
public class TransHeadersTransferTest {
|
||||||
|
@AfterClass
|
||||||
|
public static void afterClass() {
|
||||||
|
MetadataContextHolder.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void transferServletTest() {
|
||||||
|
MetadataContext metadataContext = MetadataContextHolder.get();
|
||||||
|
metadataContext.setTransHeaders("header1,header2,header3", "");
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
request.addHeader("header1", "1");
|
||||||
|
request.addHeader("header2", "2");
|
||||||
|
request.addHeader("header3", "3");
|
||||||
|
TransHeadersTransfer.transfer(request);
|
||||||
|
Map<String, String> transHeadersKV = MetadataContextHolder.get().getTransHeadersKV();
|
||||||
|
Assertions.assertEquals(transHeadersKV.get("header1"), "1");
|
||||||
|
Assertions.assertEquals(transHeadersKV.get("header2"), "2");
|
||||||
|
Assertions.assertEquals(transHeadersKV.get("header3"), "3");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void transferReactiveTest() {
|
||||||
|
MetadataContext metadataContext = MetadataContextHolder.get();
|
||||||
|
metadataContext.setTransHeaders("header1,header2,header3", "");
|
||||||
|
MockServerHttpRequest.BaseBuilder<?> builder = MockServerHttpRequest.get("");
|
||||||
|
String[] header1 = {"1"};
|
||||||
|
String[] header2 = {"2"};
|
||||||
|
String[] header3 = {"3"};
|
||||||
|
builder.header("header1", header1);
|
||||||
|
builder.header("header2", header2);
|
||||||
|
builder.header("header3", header3);
|
||||||
|
MockServerHttpRequest request = builder.build();
|
||||||
|
TransHeadersTransfer.transfer(request);
|
||||||
|
Map<String, String> transHeadersKV = MetadataContextHolder.get().getTransHeadersKV();
|
||||||
|
Assertions.assertEquals(transHeadersKV.get("header1"), JacksonUtils.serialize2Json(header1));
|
||||||
|
Assertions.assertEquals(transHeadersKV.get("header2"), JacksonUtils.serialize2Json(header2));
|
||||||
|
Assertions.assertEquals(transHeadersKV.get("header3"), JacksonUtils.serialize2Json(header3));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* 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.event;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo;
|
||||||
|
|
||||||
|
import org.springframework.context.ApplicationEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A spring change event when config is changed.
|
||||||
|
*
|
||||||
|
* @author Derek Yi 2022-10-16
|
||||||
|
*/
|
||||||
|
public class ConfigChangeSpringEvent extends ApplicationEvent {
|
||||||
|
/**
|
||||||
|
* @param source all changed keys map.
|
||||||
|
*/
|
||||||
|
public ConfigChangeSpringEvent(Map<String, ConfigPropertyChangeInfo> source) {
|
||||||
|
super(source);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the changed keys.
|
||||||
|
* @return the list of the keys
|
||||||
|
*/
|
||||||
|
public Set<String> changedKeys() {
|
||||||
|
return changeMap().keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a specific change instance for the key specified.
|
||||||
|
* @param key the changed key
|
||||||
|
* @return the change instance
|
||||||
|
*/
|
||||||
|
public ConfigPropertyChangeInfo getChange(String key) {
|
||||||
|
return changeMap().get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether the specified key is changed .
|
||||||
|
* @param key the key
|
||||||
|
* @return true if the key is changed, false otherwise.
|
||||||
|
*/
|
||||||
|
public boolean isChanged(String key) {
|
||||||
|
return changeMap().containsKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, ConfigPropertyChangeInfo> changeMap() {
|
||||||
|
return (Map<String, ConfigPropertyChangeInfo>) getSource();
|
||||||
|
}
|
||||||
|
}
|
@ -1,126 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import com.google.common.collect.LinkedListMultimap;
|
|
||||||
import com.google.common.collect.Maps;
|
|
||||||
import com.google.common.collect.Multimap;
|
|
||||||
import com.google.common.collect.Sets;
|
|
||||||
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
|
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.beans.MutablePropertyValues;
|
|
||||||
import org.springframework.beans.PropertyValue;
|
|
||||||
import org.springframework.beans.factory.config.BeanDefinition;
|
|
||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
|
||||||
import org.springframework.beans.factory.config.TypedStringValue;
|
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
|
|
||||||
import org.springframework.lang.NonNull;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* To process xml config placeholders, e.g.
|
|
||||||
*
|
|
||||||
* <pre>
|
|
||||||
* <bean class="com.demo.bean.XmlBean">
|
|
||||||
* <property name="timeout" value="${timeout:200}"/>
|
|
||||||
* <property name="batch" value="${batch:100}"/>
|
|
||||||
* </bean>
|
|
||||||
* </pre>
|
|
||||||
*
|
|
||||||
* This source file was originally from:
|
|
||||||
* <code><a href=https://github.com/apolloconfig/apollo/blob/master/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/SpringValueDefinitionProcessor.java>
|
|
||||||
* SpringValueDefinitionProcessor</a></code>
|
|
||||||
*
|
|
||||||
* @author weihubeats 2022-7-10
|
|
||||||
*/
|
|
||||||
public class SpringValueDefinitionProcessor implements BeanDefinitionRegistryPostProcessor {
|
|
||||||
private static final Map<BeanDefinitionRegistry, Multimap<String, SpringValueDefinition>> beanName2SpringValueDefinitions =
|
|
||||||
Maps.newConcurrentMap();
|
|
||||||
private static final Set<BeanDefinitionRegistry> PROPERTY_VALUES_PROCESSED_BEAN_FACTORIES = Sets.newConcurrentHashSet();
|
|
||||||
|
|
||||||
private final PlaceholderHelper placeholderHelper;
|
|
||||||
|
|
||||||
private final PolarisConfigProperties polarisConfigProperties;
|
|
||||||
|
|
||||||
public SpringValueDefinitionProcessor(PlaceholderHelper placeholderHelper, PolarisConfigProperties polarisConfigProperties) {
|
|
||||||
this.polarisConfigProperties = polarisConfigProperties;
|
|
||||||
this.placeholderHelper = placeholderHelper;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Multimap<String, SpringValueDefinition> getBeanName2SpringValueDefinitions(BeanDefinitionRegistry registry) {
|
|
||||||
Multimap<String, SpringValueDefinition> springValueDefinitions = beanName2SpringValueDefinitions.get(registry);
|
|
||||||
if (springValueDefinitions == null) {
|
|
||||||
springValueDefinitions = LinkedListMultimap.create();
|
|
||||||
}
|
|
||||||
|
|
||||||
return springValueDefinitions;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void postProcessBeanDefinitionRegistry(@NonNull BeanDefinitionRegistry registry) throws BeansException {
|
|
||||||
if (polarisConfigProperties.isAutoRefresh()) {
|
|
||||||
processPropertyValues(registry);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void postProcessBeanFactory(@NonNull ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void processPropertyValues(BeanDefinitionRegistry beanRegistry) {
|
|
||||||
if (!PROPERTY_VALUES_PROCESSED_BEAN_FACTORIES.add(beanRegistry)) {
|
|
||||||
// already initialized
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!beanName2SpringValueDefinitions.containsKey(beanRegistry)) {
|
|
||||||
beanName2SpringValueDefinitions.put(beanRegistry, LinkedListMultimap.create());
|
|
||||||
}
|
|
||||||
|
|
||||||
Multimap<String, SpringValueDefinition> springValueDefinitions = beanName2SpringValueDefinitions.get(beanRegistry);
|
|
||||||
|
|
||||||
String[] beanNames = beanRegistry.getBeanDefinitionNames();
|
|
||||||
for (String beanName : beanNames) {
|
|
||||||
BeanDefinition beanDefinition = beanRegistry.getBeanDefinition(beanName);
|
|
||||||
MutablePropertyValues mutablePropertyValues = beanDefinition.getPropertyValues();
|
|
||||||
List<PropertyValue> propertyValues = mutablePropertyValues.getPropertyValueList();
|
|
||||||
for (PropertyValue propertyValue : propertyValues) {
|
|
||||||
Object value = propertyValue.getValue();
|
|
||||||
if (!(value instanceof TypedStringValue)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
String placeholder = ((TypedStringValue) value).getValue();
|
|
||||||
Set<String> keys = placeholderHelper.extractPlaceholderKeys(placeholder);
|
|
||||||
|
|
||||||
if (keys.isEmpty()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (String key : keys) {
|
|
||||||
springValueDefinitions.put(beanName, new SpringValueDefinition(key, placeholder, propertyValue.getName()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,80 @@
|
|||||||
|
/*
|
||||||
|
* 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.event;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
import com.tencent.polaris.configuration.api.core.ChangeType;
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
import org.springframework.context.ApplicationListener;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link ConfigChangeSpringEvent}.
|
||||||
|
*
|
||||||
|
* @author derek.yi 2022-10-16
|
||||||
|
*/
|
||||||
|
public class ConfigChangeSpringEventTest {
|
||||||
|
|
||||||
|
private static CountDownLatch countDownLatch = new CountDownLatch(1);
|
||||||
|
|
||||||
|
private static AtomicInteger receiveEventTimes = new AtomicInteger();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPublishConfigChangeSpringEvent() {
|
||||||
|
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||||
|
.withConfiguration(AutoConfigurations.of(ConfigChangeSpringEventListener.class));
|
||||||
|
contextRunner.run(context -> {
|
||||||
|
HashMap<String, ConfigPropertyChangeInfo> changeMap = new HashMap<>();
|
||||||
|
changeMap.put("key", new ConfigPropertyChangeInfo("key", null, "value", ChangeType.ADDED));
|
||||||
|
context.publishEvent(new ConfigChangeSpringEvent(changeMap));
|
||||||
|
countDownLatch.await(5, TimeUnit.SECONDS);
|
||||||
|
Assert.assertEquals(1, receiveEventTimes.get());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
static class ConfigChangeSpringEventListener implements ApplicationListener<ConfigChangeSpringEvent> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onApplicationEvent(ConfigChangeSpringEvent event) {
|
||||||
|
Set<String> changedKeys = event.changedKeys();
|
||||||
|
Assert.assertEquals(1, changedKeys.size());
|
||||||
|
Assert.assertTrue(event.isChanged("key"));
|
||||||
|
ConfigPropertyChangeInfo changeInfo = event.getChange("key");
|
||||||
|
Assert.assertNotNull(changeInfo);
|
||||||
|
Assert.assertEquals("key", changeInfo.getPropertyName());
|
||||||
|
Assert.assertEquals("value", changeInfo.getNewValue());
|
||||||
|
Assert.assertEquals(ChangeType.ADDED, changeInfo.getChangeType());
|
||||||
|
|
||||||
|
receiveEventTimes.incrementAndGet();
|
||||||
|
countDownLatch.countDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,75 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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<SpringValue> name = springValueRegistry.get(beanFactory, "name");
|
|
||||||
Assert.assertFalse(CollectionUtils.isEmpty(name));
|
|
||||||
Optional<SpringValue> 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<SpringValue> age = springValueRegistry.get(beanFactory, "age");
|
|
||||||
Assert.assertFalse(CollectionUtils.isEmpty(age));
|
|
||||||
Optional<SpringValue> 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());
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue