修改 starter-test 包配置.
This commit is contained in:
@@ -35,6 +35,8 @@ public interface AutoConfigureOrderConst {
|
||||
|
||||
int FRAMEWORK_REDIS = Integer.MIN_VALUE + 2000;
|
||||
|
||||
int FRAMEWORK_REDIS_CACHE = Integer.MIN_VALUE + 2050;
|
||||
|
||||
int FRAMEWORK_STORAGE = Integer.MIN_VALUE + 2100;
|
||||
|
||||
int FRAMEWORK_MONITOR = Integer.MIN_VALUE + 2200;
|
||||
|
||||
@@ -12,7 +12,6 @@ import org.apache.ibatis.annotations.Mapper;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@@ -32,7 +31,6 @@ public class OrionMybatisAutoConfiguration {
|
||||
* @return 字段填充元数据处理器
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnBean(SecurityHolder.class)
|
||||
public MetaObjectHandler defaultMetaObjectHandler(SecurityHolder securityHolder) {
|
||||
// 设置填充工具参数
|
||||
DomainFillUtils.setSecurityHolder(securityHolder);
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.orion.ops.framework.mybatis.core.cache;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.orion.lang.define.collect.MultiHashMap;
|
||||
import com.orion.lang.define.wrapper.Store;
|
||||
import com.orion.lang.define.wrapper.Ref;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@@ -24,7 +24,7 @@ public class CacheHolder {
|
||||
* key: mapper
|
||||
* value: id > row
|
||||
*/
|
||||
private static final ThreadLocal<MultiHashMap<BaseMapper<?>, Serializable, Store<?>>> HOLDER = ThreadLocal.withInitial(MultiHashMap::new);
|
||||
private static final ThreadLocal<MultiHashMap<BaseMapper<?>, Serializable, Ref<?>>> HOLDER = ThreadLocal.withInitial(MultiHashMap::new);
|
||||
|
||||
/**
|
||||
* 获取缓存
|
||||
@@ -35,8 +35,8 @@ public class CacheHolder {
|
||||
* @return cacheWrapper
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> Store<T> get(BaseMapper<?> mapper, Serializable id) {
|
||||
return (Store<T>) HOLDER.get().get(mapper, id);
|
||||
public static <T> Ref<T> get(BaseMapper<?> mapper, Serializable id) {
|
||||
return (Ref<T>) HOLDER.get().get(mapper, id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,7 +48,7 @@ public class CacheHolder {
|
||||
* @param <T> domainClass
|
||||
*/
|
||||
public static <T> void set(BaseMapper<T> mapper, Serializable id, T row) {
|
||||
HOLDER.get().put(mapper, id, new Store<>(row));
|
||||
HOLDER.get().put(mapper, id, new Ref<>(row));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.orion.ops.framework.mybatis.core.query;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.orion.lang.define.wrapper.Store;
|
||||
import com.orion.lang.define.wrapper.Ref;
|
||||
import com.orion.lang.utils.Valid;
|
||||
import com.orion.ops.framework.mybatis.core.cache.CacheHolder;
|
||||
|
||||
@@ -79,10 +79,10 @@ public class CacheQuery<T> {
|
||||
// 不查询缓存
|
||||
if (!force) {
|
||||
// 从缓存中获取
|
||||
Store<T> store = CacheHolder.get(dao, id);
|
||||
Ref<T> ref = CacheHolder.get(dao, id);
|
||||
// 命中直接返回
|
||||
if (store != null) {
|
||||
return store.get();
|
||||
if (ref != null) {
|
||||
return ref.get();
|
||||
}
|
||||
}
|
||||
// 查询
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.orion.ops.framework.redis.config;
|
||||
|
||||
import com.orion.ops.framework.common.constant.AutoConfigureOrderConst;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
|
||||
import org.springframework.boot.autoconfigure.cache.CacheProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
||||
import org.springframework.data.redis.serializer.RedisSerializationContext;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
|
||||
/**
|
||||
* 缓存配置类
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/8/23 18:13
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@AutoConfigureOrder(AutoConfigureOrderConst.FRAMEWORK_REDIS_CACHE)
|
||||
@EnableCaching
|
||||
@EnableConfigurationProperties({CacheProperties.class})
|
||||
public class OrionCacheAutoConfiguration {
|
||||
|
||||
/**
|
||||
* @param cacheProperties properties
|
||||
* @return 缓存配置
|
||||
*/
|
||||
@Bean
|
||||
@Primary
|
||||
public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
|
||||
// 设置使用 JSON 序列化方式
|
||||
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
|
||||
config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));
|
||||
// 设置 CacheProperties.Redis 的属性
|
||||
CacheProperties.Redis redisProperties = cacheProperties.getRedis();
|
||||
if (redisProperties.getTimeToLive() != null) {
|
||||
config = config.entryTtl(redisProperties.getTimeToLive());
|
||||
}
|
||||
if (redisProperties.getKeyPrefix() != null) {
|
||||
config = config.prefixCacheNameWith(redisProperties.getKeyPrefix());
|
||||
}
|
||||
if (!redisProperties.isCacheNullValues()) {
|
||||
config = config.disableCachingNullValues();
|
||||
}
|
||||
if (!redisProperties.isUseKeyPrefix()) {
|
||||
config = config.disableKeyPrefix();
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,19 +4,13 @@ import com.orion.ops.framework.common.constant.AutoConfigureOrderConst;
|
||||
import com.orion.ops.framework.redis.core.utils.RedisUtils;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
|
||||
import org.springframework.boot.autoconfigure.cache.CacheProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.RedisSerializationContext;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
|
||||
/**
|
||||
* 缓存配置类
|
||||
* redis 配置类
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
@@ -24,8 +18,6 @@ import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@AutoConfigureOrder(AutoConfigureOrderConst.FRAMEWORK_REDIS)
|
||||
@EnableCaching
|
||||
@EnableConfigurationProperties({CacheProperties.class})
|
||||
public class OrionRedisAutoConfiguration {
|
||||
|
||||
/**
|
||||
@@ -46,33 +38,5 @@ public class OrionRedisAutoConfiguration {
|
||||
return redisTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param cacheProperties properties
|
||||
* @return 缓存配置
|
||||
*/
|
||||
@Bean
|
||||
@Primary
|
||||
public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
|
||||
// 设置使用 JSON 序列化方式
|
||||
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
|
||||
config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));
|
||||
// 设置 CacheProperties.Redis 的属性
|
||||
CacheProperties.Redis redisProperties = cacheProperties.getRedis();
|
||||
if (redisProperties.getTimeToLive() != null) {
|
||||
config = config.entryTtl(redisProperties.getTimeToLive());
|
||||
}
|
||||
if (redisProperties.getKeyPrefix() != null) {
|
||||
config = config.prefixCacheNameWith(redisProperties.getKeyPrefix());
|
||||
}
|
||||
if (!redisProperties.isCacheNullValues()) {
|
||||
config = config.disableCachingNullValues();
|
||||
}
|
||||
if (!redisProperties.isUseKeyPrefix()) {
|
||||
config = config.disableKeyPrefix();
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
com.orion.ops.framework.redis.config.OrionRedisAutoConfiguration
|
||||
com.orion.ops.framework.redis.config.OrionRedisAutoConfiguration
|
||||
com.orion.ops.framework.redis.config.OrionCacheAutoConfiguration
|
||||
@@ -125,6 +125,8 @@ public class OrionSecurityAutoConfiguration {
|
||||
|
||||
/**
|
||||
* @return security holder 代理用于内部 framework 调用
|
||||
* <p>
|
||||
* - mybatis fill
|
||||
*/
|
||||
@Bean
|
||||
public SecurityHolderDelegate securityHolder() {
|
||||
|
||||
@@ -21,16 +21,18 @@
|
||||
<groupId>com.orion.ops</groupId>
|
||||
<artifactId>orion-ops-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.orion.ops</groupId>
|
||||
<artifactId>orion-ops-spring-boot-starter-mybatis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.orion.ops</groupId>
|
||||
<artifactId>orion-ops-spring-boot-starter-datasource</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.orion.ops</groupId>
|
||||
<artifactId>orion-ops-spring-boot-starter-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- test -->
|
||||
<dependency>
|
||||
@@ -61,6 +63,7 @@
|
||||
<groupId>uk.co.jemos.podam</groupId>
|
||||
<artifactId>podam</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -32,12 +32,11 @@ import javax.sql.DataSource;
|
||||
public class OrionH2SqlInitializationTestConfiguration {
|
||||
|
||||
/**
|
||||
* 数据源脚本初始化 Bean
|
||||
* @return 数据源脚本初始化器
|
||||
*/
|
||||
@Bean
|
||||
public DataSourceScriptDatabaseInitializer dataSourceScriptDatabaseInitializer(DataSource dataSource,
|
||||
SqlInitializationProperties initializationProperties) {
|
||||
// TODO 看看正常情况下会不会有
|
||||
// 初始化配置
|
||||
DatabaseInitializationSettings settings = new DatabaseInitializationSettings();
|
||||
settings.setSchemaLocations(initializationProperties.getSchemaLocations());
|
||||
|
||||
@@ -9,7 +9,7 @@ import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
|
||||
/**
|
||||
* 单元测试 redis 初始化
|
||||
* 单元测试 redis mock server 初始化
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
@@ -22,17 +22,16 @@ import org.springframework.context.annotation.Profile;
|
||||
public class OrionMockRedisTestConfiguration {
|
||||
|
||||
/**
|
||||
* mockRedisServer
|
||||
* @return redisMockServer
|
||||
*/
|
||||
@Bean
|
||||
public RedisServer redisServer(RedisProperties properties) {
|
||||
// TODO 看看正常情况下会不会有
|
||||
RedisServer redisServer = new RedisServer(properties.getPort());
|
||||
public RedisServer redisMockServer(RedisProperties properties) {
|
||||
RedisServer server = new RedisServer(properties.getPort());
|
||||
try {
|
||||
redisServer.start();
|
||||
server.start();
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
return redisServer;
|
||||
return server;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.orion.ops.framework.test.config;
|
||||
|
||||
import com.orion.ops.framework.common.security.LoginUser;
|
||||
import com.orion.ops.framework.common.security.SecurityHolder;
|
||||
import com.orion.ops.framework.test.core.utils.EntityRandoms;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
|
||||
/**
|
||||
* 单元测试 security 初始化
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/8/24 14:53
|
||||
*/
|
||||
@Lazy(false)
|
||||
@Profile("unit-test")
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
public class OrionSecurityTestConfiguration {
|
||||
|
||||
/**
|
||||
* 空实现数据填充器 用于单元测试 单元测试不会注入 spring-security
|
||||
*
|
||||
* @return SecurityHolder
|
||||
*/
|
||||
@Bean
|
||||
@Profile("unit-test")
|
||||
@ConditionalOnMissingBean(SecurityHolder.class)
|
||||
public SecurityHolder emptySecurityHolder() {
|
||||
return new SecurityHolder() {
|
||||
|
||||
private final LoginUser DEFAULT = EntityRandoms.random(LoginUser.class);
|
||||
|
||||
@Override
|
||||
public LoginUser getLoginUser() {
|
||||
return DEFAULT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getLoginUserId() {
|
||||
return DEFAULT.getId();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,15 +4,20 @@ import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
|
||||
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
|
||||
import com.orion.ops.framework.datasource.config.OrionDataSourceAutoConfiguration;
|
||||
import com.orion.ops.framework.mybatis.config.OrionMybatisAutoConfiguration;
|
||||
import com.orion.ops.framework.redis.config.OrionRedisAutoConfiguration;
|
||||
import com.orion.ops.framework.test.config.OrionH2SqlInitializationTestConfiguration;
|
||||
import com.orion.ops.framework.test.config.OrionMockRedisTestConfiguration;
|
||||
import com.orion.ops.framework.test.config.OrionSecurityTestConfiguration;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.redisson.spring.starter.RedissonAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* 单元测试父类
|
||||
@@ -21,24 +26,29 @@ import org.springframework.test.context.jdbc.Sql;
|
||||
* @version 1.0.0
|
||||
* @since 2023/8/23 15:12
|
||||
*/
|
||||
@Rollback
|
||||
@ActiveProfiles("unit-test")
|
||||
@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseUnitTest.Application.class)
|
||||
public class BaseUnitTest {
|
||||
|
||||
@Import({
|
||||
// security
|
||||
OrionSecurityTestConfiguration.class,
|
||||
// datasource
|
||||
OrionDataSourceAutoConfiguration.class,
|
||||
DruidDataSourceAutoConfigure.class,
|
||||
DataSourceAutoConfiguration.class,
|
||||
DataSourceTransactionManagerAutoConfiguration.class,
|
||||
OrionH2SqlInitializationTestConfiguration.class,
|
||||
DruidDataSourceAutoConfigure.class,
|
||||
// mybatis
|
||||
OrionMybatisAutoConfiguration.class,
|
||||
MybatisPlusAutoConfiguration.class,
|
||||
// redis
|
||||
OrionMockRedisTestConfiguration.class,
|
||||
OrionRedisAutoConfiguration.class,
|
||||
RedisAutoConfiguration.class,
|
||||
RedissonAutoConfiguration.class,
|
||||
})
|
||||
public static class Application {
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
spring:
|
||||
main:
|
||||
lazy-initialization: true
|
||||
banner-mode: OFF
|
||||
datasource:
|
||||
druid:
|
||||
name: orion-ops-pro
|
||||
url: jdbc:h2:mem:testdb;MODE=MYSQL;DATABASE_TO_UPPER=false;NON_KEYWORDS=value;
|
||||
driver-class-name: org.h2.Driver
|
||||
username: sa
|
||||
password:
|
||||
max-active: 1
|
||||
async-init: true
|
||||
initial-size: 1
|
||||
test-while-idle: false
|
||||
sql:
|
||||
init:
|
||||
schema-locations:
|
||||
- classpath:/sql/create-tables-h2-*.sql
|
||||
redis:
|
||||
host: 127.0.0.1
|
||||
port: 16379
|
||||
database: 0
|
||||
|
||||
mybatis-plus:
|
||||
lazy-initialization: true
|
||||
Reference in New Issue
Block a user