feat: 增加条件判断
This commit is contained in:
parent
50a80ab86d
commit
fad2128600
1
.gitignore
vendored
1
.gitignore
vendored
@ -44,3 +44,4 @@ build/
|
||||
/common-lib/.flattened-pom.xml
|
||||
/dao-support-lib/.flattened-pom.xml
|
||||
/.flattened-pom.xml
|
||||
/unittest-support-lib/.flattened-pom.xml
|
||||
|
||||
1
pom.xml
1
pom.xml
@ -24,6 +24,7 @@
|
||||
<module>dao-support-lib</module>
|
||||
<module>common-lib</module>
|
||||
<module>unittest-support-lib</module>
|
||||
<module>web-support-lib</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@ -12,11 +12,11 @@ import com.alibaba.fastjson.TypeReference;
|
||||
import com.google.common.collect.HashBasedTable;
|
||||
import com.google.common.collect.Table;
|
||||
import com.google.common.io.Files;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
@ -48,7 +48,6 @@ import java.util.List;
|
||||
@Transactional
|
||||
@ActiveProfiles("unittest")
|
||||
@ComponentScan("cn.axzo.**.config")
|
||||
@RequiredArgsConstructor
|
||||
public abstract class BaseTest {
|
||||
private static final String JSON_FILE_CLASSPATH = "classpath:json/";
|
||||
/**
|
||||
@ -57,9 +56,11 @@ public abstract class BaseTest {
|
||||
*/
|
||||
private static Table<Object, String, Object> ORIGIN_FIELD_TABLE = HashBasedTable.create();
|
||||
|
||||
protected final MockMvc mockMvc;
|
||||
@Autowired
|
||||
protected MockMvc mockMvc;
|
||||
|
||||
protected final WebApplicationContext context;
|
||||
@Autowired
|
||||
protected WebApplicationContext context;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
|
||||
36
web-support-lib/pom.xml
Normal file
36
web-support-lib/pom.xml
Normal file
@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>cn.axzo.foundation</groupId>
|
||||
<artifactId>axzo-lib-box</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>web-support-lib</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.axzo.foundation</groupId>
|
||||
<artifactId>common-lib</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,35 @@
|
||||
package cn.axzo.foundation.web.support.conditional;
|
||||
|
||||
import cn.axzo.foundation.enums.AppEnvEnum;
|
||||
import com.google.common.base.Strings;
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.env.Profiles;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
public class LocalCondition implements Condition {
|
||||
|
||||
/**
|
||||
* 匹配local环境或idea启动环境
|
||||
*/
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@org.springframework.context.annotation.Conditional(LocalCondition.class)
|
||||
public @interface Conditional {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
// local环境启动时, 返回true
|
||||
if (context.getEnvironment().acceptsProfiles(Profiles.of(AppEnvEnum.local.name()))) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 通过idea启动时, 返回true
|
||||
return Strings.nullToEmpty(System.getProperty("java.class.path")).contains("idea_rt.jar");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package cn.axzo.foundation.web.support.conditional;
|
||||
|
||||
import cn.axzo.foundation.enums.AppEnvEnum;
|
||||
import com.google.common.base.Strings;
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.env.Profiles;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
public class NonJunitCondition implements Condition {
|
||||
|
||||
/**
|
||||
* 匹配非unittest或idea启动环境启动环境
|
||||
*/
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@org.springframework.context.annotation.Conditional(NonJunitCondition.class)
|
||||
public @interface Conditional {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
// unittest环境启动时, 返回false
|
||||
if (context.getEnvironment().acceptsProfiles(Profiles.of(AppEnvEnum.unittest.name()))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 通过idea启动时, 返回false
|
||||
return !Strings.nullToEmpty(System.getProperty("java.class.path")).contains("junit-rt.jar");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package cn.axzo.foundation.web.support.conditional;
|
||||
|
||||
import cn.axzo.foundation.enums.AppEnvEnum;
|
||||
import com.google.common.base.Strings;
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.env.Profiles;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
public class NonLocalCondition implements Condition {
|
||||
|
||||
/**
|
||||
* 匹配非local环境或idea启动环境
|
||||
*/
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@org.springframework.context.annotation.Conditional(NonLocalCondition.class)
|
||||
public @interface Conditional {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
// local环境启动时, 返回false
|
||||
if (context.getEnvironment().acceptsProfiles(Profiles.of(AppEnvEnum.local.name()))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 通过idea启动时, 返回false
|
||||
return !Strings.nullToEmpty(System.getProperty("java.class.path")).contains("idea_rt.jar");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user