EnvironmentPostProcessor怎么做单元测试?阿里P7告诉你

import org.springframework.core.env.PropertiesPropertySource;

import java.util.Properties;

public class DecodeEnvironmentPostProcessor implements EnvironmentPostProcessor {

public static final String SPRING_DATASOURCE_PASSWORD = “spring.datasource.password”;

public static final String AES_SECRET = “OneMore”;

@Override

public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {

String password = environment.getProperty(SPRING_DATASOURCE_PASSWORD);

Properties properties = new Properties();

properties.setProperty(SPRING_DATASOURCE_PASSWORD, AESUtil.decrypt(password, AES_SECRET));

PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource(SPRING_DATASOURCE_PASSWORD,

properties);

environment.getPropertySources().addFirst(propertiesPropertySource);

}

}

如果你希望EnvironmentPostProcessor按照特定的顺序被调用,可以实现Ordered接口,或者使用@Order注解。

2.注册实现类

想要在Spring Boot启动过程中调用这个实现类,我们还需要在META-INF/ Spring .factories中注册这个实现类:

org.springframework.boot.env.EnvironmentPostProcessor=

one.more.DecodeEnvironmentPostProcessor

单元测试

下面介绍本文的重点:怎么做EnvironmentPostProcessor实现类的单元测试,话不多说,直接上代码:

package one.more;

import org.junit.Assert;

import org.junit.Test;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.WebApplicationType;

import org.springframework.boot.builder.SpringApplicationBuilder;

import org.springframework.boot.env.EnvironmentPostProcessor;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.core.env.ConfigurableEnvironment;

import org.springframework.core.env.PropertiesPropertySource;

import java.util.Properties;

public class DecodeEnvironmentPostProcessorTest {

@Test

public void testPostProcessEnvironment() {

DecodeEnvironmentPostProcessor processor = new DecodeEnvironmentPostProcessor();

String password = “one-more”;

Properties properties = new Properties();

properties.setProperty(DecodeEnvironmentPostProcessor.SPRING_DATASOURCE_PASSWORD,

AESUtil.encrypt(password, DecodeEnvironmentPostProcessor.AES_SECRET));

ConfigurableEnvironment environment = getEnvironment(processor, properties);

Assert.assertEquals(password,

environment.getProperty(DecodeEnvironmentPostProcessor.SPRING_DATASOURCE_PASSWORD));

}

/**

  • 获取一个经过EnvironmentPostProcessor处理过的Environment

  • @param processor EnvironmentPostProcessor实现类的实例

  • @param properties 预置准备做单元测试的属性

  • @return 处理过的Environment

*/

private ConfigurableEnvironment getEnvironment(EnvironmentPostProcessor processor, Properties properties) {

// 创建一个SpringApplication

SpringApplication springApplication = new SpringApplicationBuilder()

.sources(DecodeEnvironmentPostProcessor.class)

.web(WebApplicationType.NONE).build();

// 获取应用上下文

ConfigurableApplicationContext context = springApplication.run();

// 获取Environment

ConfigurableEnvironment environment = context.getEnvironment();

//添加准备做单元测试的属性

environment.getPropertySources()

.addFirst(new PropertiesPropertySource(“test”, properties));

processor.postProcessEnvironment(environment, springApplication);

context.close();

return environment;

}

}

附:加解密工具类代码

package one.more;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

import java.security.NoSuchAlgorithmException;

import java.security.SecureRandom;

public class AESUtil {

private static final String DEFAULT_CIPHER_ALGORITHM = “AES/ECB/PKCS5Padding”;

private static final String KEY_ALGORITHM = “AES”;

public static String encrypt(String content, String password) {

try {

Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);

byte[] byteContent = content.getBytes(“utf-8”);

cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(password));

byte[] result = cipher.doFinal(byteContent);

return Base64.encodeBase64String(result);

} catch (Exception ex) {

}

return null;

}

public static String decrypt(String content, String password) {

try {

Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);

cipher.init(Cipher.DECRYPT_MODE, getSecretKey(password));

byte[] result = cipher.doFinal(Base64.decodeBase64(content));

return new String(result, “utf-8”);

} catch (Exception ex) {

}

return null;

}

private static SecretKeySpec getSecretKey(final String password) {

KeyGenerator kg = null;

try {

kg = KeyGenerator.getInstance(KEY_ALGORITHM);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值