update: redis unit test
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
package we.util;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.Consts;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -83,4 +84,13 @@ public abstract class Utils {
|
||||
ca[0] += 32;
|
||||
return String.valueOf(ca);
|
||||
}
|
||||
|
||||
public static void threadCurrentStack2stringBuilder(StringBuilder b) {
|
||||
StackTraceElement[] stackTraces = Thread.currentThread().getStackTrace();
|
||||
if (stackTraces != null) {
|
||||
for (int i = 0; i < stackTraces.length; i++) {
|
||||
b.append(stackTraces[i]).append(Constants.Symbol.LF);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
// package we.redis;
|
||||
//
|
||||
// import org.springframework.context.annotation.Bean;
|
||||
// import org.springframework.context.annotation.Configuration;
|
||||
// import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||
// import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
// import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
|
||||
//
|
||||
// /**
|
||||
// * @author hongqiaowei
|
||||
// */
|
||||
//
|
||||
// @Configuration
|
||||
// @EnableRedisRepositories
|
||||
// public class RedisConfiguration {
|
||||
//
|
||||
// @Bean
|
||||
// public LettuceConnectionFactory redisConnectionFactory(
|
||||
// RedisProperties redisProperties) {
|
||||
// LettuceConnectionFactory cf = new LettuceConnectionFactory(
|
||||
// redisProperties.getRedisHost(),
|
||||
// redisProperties.getRedisPort());
|
||||
// cf.setDatabase(redisProperties.getDatabase());
|
||||
// return cf;
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory connectionFactory) {
|
||||
// StringRedisTemplate template = new StringRedisTemplate();
|
||||
// template.setConnectionFactory(connectionFactory);
|
||||
// return template;
|
||||
// }
|
||||
// }
|
||||
@@ -1,58 +1,54 @@
|
||||
// package we.redis;
|
||||
//
|
||||
// import org.springframework.beans.factory.annotation.Value;
|
||||
// import org.springframework.context.annotation.Configuration;
|
||||
// import we.util.JacksonUtils;
|
||||
//
|
||||
// /**
|
||||
// * @author hongqiaowei
|
||||
// */
|
||||
//
|
||||
// @Configuration
|
||||
// public class RedisProperties {
|
||||
//
|
||||
// private int redisPort;
|
||||
// private String redisHost;
|
||||
// private int database;
|
||||
//
|
||||
// public RedisProperties(
|
||||
// /*@Value("${spring.redis.port}") int redisPort,
|
||||
// @Value("${spring.redis.host}") String redisHost,
|
||||
// @Value("${spring.redis.database}") int database*/) {
|
||||
// // this.redisPort = redisPort;
|
||||
// // this.redisHost = redisHost;
|
||||
// // this.database = database;
|
||||
// this.redisPort = 6379;
|
||||
// this.redisHost = "localhost";
|
||||
// this.database = 3;
|
||||
// }
|
||||
//
|
||||
// public int getRedisPort() {
|
||||
// return redisPort;
|
||||
// }
|
||||
//
|
||||
// public void setRedisPort(int redisPort) {
|
||||
// this.redisPort = redisPort;
|
||||
// }
|
||||
//
|
||||
// public String getRedisHost() {
|
||||
// return redisHost;
|
||||
// }
|
||||
//
|
||||
// public void setRedisHost(String redisHost) {
|
||||
// this.redisHost = redisHost;
|
||||
// }
|
||||
//
|
||||
// public int getDatabase() {
|
||||
// return database;
|
||||
// }
|
||||
//
|
||||
// public void setDatabase(int database) {
|
||||
// this.database = database;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public String toString() {
|
||||
// return JacksonUtils.writeValueAsString(this);
|
||||
// }
|
||||
// }
|
||||
package we.redis;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
|
||||
/**
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
@TestConfiguration
|
||||
public class RedisProperties {
|
||||
|
||||
private String host;
|
||||
private int port;
|
||||
private int database;
|
||||
|
||||
public RedisProperties(
|
||||
@Value("${embeded.redis.port}") int port,
|
||||
@Value("${embeded.redis.host}") String host,
|
||||
@Value("${embeded.redis.database}") int database) {
|
||||
this.port = port;
|
||||
this.host = host;
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int redisPort) {
|
||||
this.port = redisPort;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String redisHost) {
|
||||
this.host = redisHost;
|
||||
}
|
||||
|
||||
public int getDatabase() {
|
||||
return database;
|
||||
}
|
||||
|
||||
public void setDatabase(int database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "redis:[host:" + host + ",port:" + port + ",database:" + database + "]";
|
||||
}
|
||||
}
|
||||
|
||||
34
src/test/java/we/redis/RedisServerConfiguration.java
Normal file
34
src/test/java/we/redis/RedisServerConfiguration.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package we.redis;
|
||||
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import redis.embedded.RedisServer;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
/**
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
@TestConfiguration
|
||||
public class RedisServerConfiguration {
|
||||
|
||||
private RedisServer redisServer;
|
||||
|
||||
public RedisServerConfiguration(RedisProperties redisProperties) {
|
||||
redisServer = RedisServer.builder()
|
||||
.port(redisProperties.getPort())
|
||||
.setting("maxmemory 32M")
|
||||
.build();
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void postConstruct() {
|
||||
redisServer.start();
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void preDestroy() {
|
||||
redisServer.stop();
|
||||
}
|
||||
}
|
||||
33
src/test/java/we/redis/RedisTemplateConfiguration.java
Normal file
33
src/test/java/we/redis/RedisTemplateConfiguration.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package we.redis;
|
||||
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
|
||||
|
||||
/**
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
@TestConfiguration
|
||||
// @EnableRedisRepositories
|
||||
public class RedisTemplateConfiguration {
|
||||
|
||||
@Bean
|
||||
public LettuceConnectionFactory redisConnectionFactory(
|
||||
RedisProperties redisProperties) {
|
||||
LettuceConnectionFactory cf = new LettuceConnectionFactory(
|
||||
redisProperties.getHost(),
|
||||
redisProperties.getPort());
|
||||
cf.setDatabase(redisProperties.getDatabase());
|
||||
return cf;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory connectionFactory) {
|
||||
StringRedisTemplate template = new StringRedisTemplate();
|
||||
template.setConnectionFactory(connectionFactory);
|
||||
return template;
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
// package we.redis;
|
||||
//
|
||||
// import org.springframework.context.annotation.Configuration;
|
||||
// import redis.embedded.RedisServer;
|
||||
//
|
||||
// import javax.annotation.PostConstruct;
|
||||
// import javax.annotation.PreDestroy;
|
||||
//
|
||||
// /**
|
||||
// * @author hongqiaowei
|
||||
// */
|
||||
//
|
||||
// @Configuration
|
||||
// public class RedisTestConfiguration {
|
||||
//
|
||||
// private RedisServer redisServer;
|
||||
//
|
||||
// public RedisTestConfiguration(RedisProperties redisProperties) {
|
||||
// redisServer = RedisServer.builder()
|
||||
// .port(redisProperties.getRedisPort())
|
||||
// .setting("maxmemory 32M")
|
||||
// .build();
|
||||
// }
|
||||
//
|
||||
// @PostConstruct
|
||||
// public void postConstruct() {
|
||||
// redisServer.start();
|
||||
// }
|
||||
//
|
||||
// @PreDestroy
|
||||
// public void preDestroy() {
|
||||
// redisServer.stop();
|
||||
// }
|
||||
// }
|
||||
@@ -1,58 +1,49 @@
|
||||
// package we.stats.ratelimit;
|
||||
//
|
||||
// import org.junit.jupiter.api.AfterAll;
|
||||
// import org.junit.jupiter.api.BeforeAll;
|
||||
// import org.junit.jupiter.api.Test;
|
||||
// import org.springframework.boot.test.context.SpringBootTest;
|
||||
// import org.springframework.context.annotation.PropertySource;
|
||||
// import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
// import org.springframework.test.context.ActiveProfiles;
|
||||
// import org.springframework.test.context.ContextConfiguration;
|
||||
// import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
// import redis.embedded.RedisServer;
|
||||
// import we.redis.RedisProperties;
|
||||
// import we.redis.RedisTestConfiguration;
|
||||
//
|
||||
// import javax.annotation.Resource;
|
||||
//
|
||||
// /**
|
||||
// * @author hongqiaowei
|
||||
// */
|
||||
//
|
||||
// // @SpringBootTest(classes = RedisTestConfiguration.class)
|
||||
// // @ContextConfiguration(classes = RedisTestConfiguration.class)
|
||||
// @PropertySource("classpath:/application.yml")
|
||||
// @SpringJUnitConfig(classes = {RedisProperties.class, RedisTestConfiguration.class, RedisTestConfiguration.class})
|
||||
// // @ActiveProfiles("unittest")
|
||||
// public class ResourceRateLimitConfigServiceTests {
|
||||
//
|
||||
// // private static RedisServer redisServer;
|
||||
// //
|
||||
// // @BeforeAll
|
||||
// // static void startRedis() {
|
||||
// // redisServer = RedisServer.builder()
|
||||
// // .port(6379)
|
||||
// // .setting("maxmemory 32M")
|
||||
// // .build();
|
||||
// // redisServer.start();
|
||||
// // }
|
||||
// //
|
||||
// // @AfterAll
|
||||
// // static void stopRedis() {
|
||||
// // redisServer.stop();
|
||||
// // }
|
||||
//
|
||||
// @Resource
|
||||
// private RedisProperties redisProperties;
|
||||
//
|
||||
// @Resource
|
||||
// private StringRedisTemplate stringRedisTemplate;
|
||||
//
|
||||
// @Test
|
||||
// void initTest() {
|
||||
// System.err.println("redis: " + redisProperties);
|
||||
// System.err.println("stringRedisTemplate: " + stringRedisTemplate);
|
||||
// // 其实就是要构建个 context,里面有 redis、ReactiveStringRedisTemplate、ResourceRateLimitConfigService
|
||||
// // ResourceRateLimitConfigService 依赖 ReactiveStringRedisTemplate,然后能 PostConstruct
|
||||
// }
|
||||
// }
|
||||
package we.stats.ratelimit;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
import we.redis.RedisTemplateConfiguration;
|
||||
import we.redis.RedisProperties;
|
||||
import we.redis.RedisServerConfiguration;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
@TestPropertySource("/application.properties")
|
||||
@SpringJUnitConfig(classes = {RedisProperties.class, RedisTemplateConfiguration.class, RedisServerConfiguration.class})
|
||||
// @ActiveProfiles("unittest")
|
||||
public class ResourceRateLimitConfigServiceTests {
|
||||
|
||||
// private static RedisServer redisServer;
|
||||
//
|
||||
// @BeforeAll
|
||||
// static void startRedis() {
|
||||
// redisServer = RedisServer.builder()
|
||||
// .port(6379)
|
||||
// .setting("maxmemory 32M")
|
||||
// .build();
|
||||
// redisServer.start();
|
||||
// }
|
||||
//
|
||||
// @AfterAll
|
||||
// static void stopRedis() {
|
||||
// redisServer.stop();
|
||||
// }
|
||||
|
||||
@Resource
|
||||
private RedisProperties redisProperties;
|
||||
|
||||
@Resource
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@Test
|
||||
void initTest() {
|
||||
System.err.println(redisProperties);
|
||||
System.err.println(stringRedisTemplate);
|
||||
}
|
||||
}
|
||||
|
||||
5
src/test/resources/application.properties
Normal file
5
src/test/resources/application.properties
Normal file
@@ -0,0 +1,5 @@
|
||||
# author: hongqiaowei
|
||||
|
||||
embeded.redis.host = localhost
|
||||
embeded.redis.port = 6379
|
||||
embeded.redis.database = 4
|
||||
@@ -1,5 +0,0 @@
|
||||
#spring:
|
||||
# redis:
|
||||
# host: localhost
|
||||
# port: 6379
|
||||
# database: 3
|
||||
Reference in New Issue
Block a user