-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from mercyblitz/dev-1.x
Polish #24 : Merged the latest code
- Loading branch information
Showing
6 changed files
with
93 additions
and
113 deletions.
There are no files selected for viewing
77 changes: 64 additions & 13 deletions
77
...-spring-cloud-openfeign/src/test/java/io/microsphere/spring/cloud/openfeign/BaseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,91 @@ | ||
package io.microsphere.spring.cloud.openfeign; | ||
|
||
import io.microsphere.spring.cloud.openfeign.autoconfigure.EnableFeignAutoRefresh; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration; | ||
import org.springframework.cloud.context.environment.EnvironmentChangeEvent; | ||
import org.springframework.cloud.endpoint.event.RefreshEvent; | ||
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient; | ||
import org.springframework.cloud.openfeign.EnableFeignClients; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.core.env.MapPropertySource; | ||
import org.springframework.core.env.MutablePropertySources; | ||
import org.springframework.test.context.TestPropertySource; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">韩超</a> | ||
* @since 0.0.1 | ||
*/ | ||
@SpringBootTest(classes = {BaseTest.class}, | ||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@EnableAutoConfiguration | ||
@TestPropertySource(properties = { | ||
"spring.main.allow-bean-definition-overriding=true", | ||
"feign.client.config.default.encoder=io.microsphere.spring.cloud.openfeign.encoder.AEncoder", | ||
"feign.client.config.default.request-interceptors[0]=io.microsphere.spring.cloud.openfeign.requestInterceptor.ARequestInterceptor", | ||
"feign.client.config.default.default-request-headers.app=my-app", | ||
"feign.client.config.default.default-query-parameters.sign=my-sign", | ||
}) | ||
@ComponentScan(basePackages = "io.microsphere.spring.cloud.openfeign") | ||
@TestPropertySource( | ||
properties = { | ||
"spring.main.allow-bean-definition-overriding=true", | ||
"feign.client.config.default.encoder=io.microsphere.spring.cloud.openfeign.encoder.AEncoder", | ||
"feign.client.config.default.request-interceptors[0]=io.microsphere.spring.cloud.openfeign.requestInterceptor.ARequestInterceptor" | ||
} | ||
) | ||
@EnableFeignClients(clients = BaseClient.class) | ||
@EnableFeignAutoRefresh | ||
@AutoConfigureAfter(ConfigurationPropertiesRebinderAutoConfiguration.class) | ||
public class BaseTest { | ||
public abstract class BaseTest<T> { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(BaseTest.class); | ||
@Autowired | ||
private ApplicationEventPublisher publisher; | ||
@Autowired | ||
private Environment environment; | ||
@Autowired | ||
private BaseClient client; | ||
|
||
protected abstract String afterTestComponentConfigKey(); | ||
protected abstract Class<? extends T> afterTestComponent(); | ||
|
||
public void replaceConfig() { | ||
final String key = afterTestComponentConfigKey(); | ||
Set<String> keys = Collections.singleton(key); | ||
final Class<?> className = afterTestComponent(); | ||
MutablePropertySources propertySources = ((ConfigurableEnvironment)this.environment).getPropertySources(); | ||
Map<String, Object> map = new HashMap<>(); | ||
log.trace("replacing config key {} with value {}", key, className.getName()); | ||
map.put(key, className); | ||
propertySources.addFirst(new MapPropertySource("after", map)); | ||
|
||
EnvironmentChangeEvent event = new EnvironmentChangeEvent(keys); | ||
|
||
triggerRefreshEvent(); | ||
|
||
this.publisher.publishEvent(event); | ||
} | ||
|
||
@Test | ||
public void testInternal() { | ||
try { | ||
this.client.echo("hello", "1.0"); | ||
|
||
} catch (Exception ignored) { | ||
|
||
} | ||
replaceConfig(); | ||
try { | ||
this.client.echo("world", "1.0"); | ||
} catch (Exception ignored) { | ||
|
||
} | ||
|
||
|
||
} | ||
|
||
protected void triggerRefreshEvent() { | ||
this.publisher.publishEvent(new RefreshEvent(new Object(), new Object(), "test")); | ||
|
61 changes: 10 additions & 51 deletions
61
...feign/src/test/java/io/microsphere/spring/cloud/openfeign/encoder/EncoderChangedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,25 @@ | ||
package io.microsphere.spring.cloud.openfeign.encoder; | ||
|
||
import io.microsphere.spring.cloud.openfeign.BaseClient; | ||
import feign.codec.Encoder; | ||
import io.microsphere.spring.cloud.openfeign.BaseTest; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.cloud.context.environment.EnvironmentChangeEvent; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.core.env.MapPropertySource; | ||
import org.springframework.core.env.MutablePropertySources; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">韩超</a> | ||
* @since 0.0.1 | ||
*/ | ||
@SpringBootTest(classes = EncoderChangedTest.class) | ||
@EnableAutoConfiguration | ||
public class EncoderChangedTest extends BaseTest { | ||
|
||
@Autowired | ||
private Environment environment; | ||
@Autowired | ||
private ApplicationEventPublisher eventPublisher; | ||
|
||
@Autowired | ||
private BaseClient baseClient; | ||
|
||
@Test | ||
public void testEncoderChange() { | ||
try { | ||
baseClient.echo("aaa"); | ||
|
||
} catch (Exception ignored) { | ||
} | ||
applyEnvironmentChange(); | ||
try { | ||
baseClient.echo("bbb"); | ||
} catch (Exception ignored) { | ||
public class EncoderChangedTest extends BaseTest<Encoder> { | ||
|
||
} | ||
@Override | ||
protected String afterTestComponentConfigKey() { | ||
return "feign.client.config.my-client.encoder"; | ||
} | ||
|
||
private void applyEnvironmentChange() { | ||
Set<String> keys = Collections.singleton("feign.client.config.aaa.encoder"); | ||
MutablePropertySources propertySources = ((ConfigurableEnvironment)this.environment).getPropertySources(); | ||
Map<String, Object> map = new HashMap<>(); | ||
System.out.println("替换encoder: BEncoder"); | ||
map.put("feign.client.config.aaa.encoder", BEncoder.class); | ||
propertySources.addFirst(new MapPropertySource("addition", map)); | ||
|
||
EnvironmentChangeEvent event = new EnvironmentChangeEvent(keys); | ||
|
||
triggerRefreshEvent(); | ||
|
||
this.eventPublisher.publishEvent(event); | ||
@Override | ||
protected Class<? extends Encoder> afterTestComponent() { | ||
return BEncoder.class; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,15 @@ | |
|
||
import feign.codec.ErrorDecoder; | ||
import io.microsphere.spring.cloud.openfeign.BaseTest; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">韩超</a> | ||
* @since 1.0 | ||
*/ | ||
@SpringBootTest(classes = ErrorDecoderChangedTest.class) | ||
@EnableAutoConfiguration | ||
public class ErrorDecoderChangedTest extends BaseTest<ErrorDecoder> { | ||
|
||
@Override | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,15 @@ | |
|
||
import feign.QueryMapEncoder; | ||
import io.microsphere.spring.cloud.openfeign.BaseTest; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">韩超</a> | ||
* @since 1.0 | ||
*/ | ||
@SpringBootTest(classes = QueryMapEncoderChangedTest.class) | ||
@EnableAutoConfiguration | ||
public class QueryMapEncoderChangedTest extends BaseTest<QueryMapEncoder> { | ||
|
||
@Override | ||
|
62 changes: 13 additions & 49 deletions
62
.../microsphere/spring/cloud/openfeign/requestInterceptor/RequestInterceptorChangedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,29 @@ | ||
package io.microsphere.spring.cloud.openfeign.requestInterceptor; | ||
|
||
import io.microsphere.spring.cloud.openfeign.BaseClient; | ||
import feign.RequestInterceptor; | ||
import io.microsphere.spring.cloud.openfeign.BaseTest; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.cloud.context.environment.EnvironmentChangeEvent; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.core.env.MapPropertySource; | ||
import org.springframework.core.env.MutablePropertySources; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">韩超</a> | ||
* @since 0.0.1 | ||
*/ | ||
@SpringBootTest(classes = RequestInterceptorChangedTest.class, webEnvironment = SpringBootTest.WebEnvironment.NONE) | ||
@EnableAutoConfiguration | ||
public class RequestInterceptorChangedTest extends BaseTest { | ||
|
||
@Autowired | ||
private Environment environment; | ||
@Autowired | ||
private ApplicationEventPublisher eventPublisher; | ||
|
||
@Autowired | ||
private BaseClient baseClient; | ||
|
||
@Test | ||
public void testEncoderChange() { | ||
try { | ||
baseClient.echo("aaa"); | ||
|
||
} catch (Exception ignored) { | ||
} | ||
applyEnvironmentChange(); | ||
try { | ||
baseClient.echo("bbb"); | ||
} catch (Exception ignored) { | ||
|
||
} | ||
public class RequestInterceptorChangedTest extends BaseTest<RequestInterceptor> { | ||
|
||
@Override | ||
protected String afterTestComponentConfigKey() { | ||
return "feign.client.config.my-client.request-interceptors[0]"; | ||
} | ||
|
||
private void applyEnvironmentChange() { | ||
Set<String> keys = Collections.singleton("feign.client.config.aaa.request-interceptors[0]"); | ||
MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources(); | ||
Map<String, Object> map = new HashMap<>(); | ||
System.out.println("替换requestInterceptor: BRequestInterceptor"); | ||
map.put("feign.client.config.aaa.request-interceptors[0]", BRequestInterceptor.class.getName()); | ||
propertySources.addFirst(new MapPropertySource("addition", map)); | ||
|
||
EnvironmentChangeEvent event = new EnvironmentChangeEvent(keys); | ||
|
||
triggerRefreshEvent(); | ||
this.eventPublisher.publishEvent(event); | ||
@Override | ||
protected Class<? extends RequestInterceptor> afterTestComponent() { | ||
return BRequestInterceptor.class; | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,15 @@ | |
|
||
import feign.Retryer; | ||
import io.microsphere.spring.cloud.openfeign.BaseTest; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">韩超</a> | ||
* @since 1.0 | ||
*/ | ||
@SpringBootTest(classes = RetryerChangedTest.class) | ||
@EnableAutoConfiguration | ||
public class RetryerChangedTest extends BaseTest<Retryer> { | ||
|
||
@Override | ||
|