mirror of
https://gitee.com/dromara/sms4j.git
synced 2025-12-07 01:18:33 +08:00
springboot测试案例编写
This commit is contained in:
parent
1542deffff
commit
ff6e10a428
28
sms4j-example/JavaSE/pom.xml
Normal file
28
sms4j-example/JavaSE/pom.xml
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<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/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.dromara.sms4j</groupId>
|
||||||
|
<artifactId>sms4j-example</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<artifactId>JavaSE</artifactId>
|
||||||
|
<name>JavaSE-Demo</name>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.dromara.sms4j</groupId>
|
||||||
|
<artifactId>sms4j-javase-plugin</artifactId>
|
||||||
|
<version>${sms4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.dromara.sms4j</groupId>
|
||||||
|
<artifactId>TestCase</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.dromara.sms4j</groupId>
|
||||||
|
<artifactId>Customize</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
17
sms4j-example/SpringBoot/SpringBootYamlFile/pom.xml
Normal file
17
sms4j-example/SpringBoot/SpringBootYamlFile/pom.xml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?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>org.dromara.sms4j</groupId>
|
||||||
|
<artifactId>SpringBoot</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<groupId>org.dromara.sms4j</groupId>
|
||||||
|
<artifactId>SpringBootYamlFile</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
</project>
|
||||||
18
sms4j-example/SpringBoot/SpringbootReadConfig/pom.xml
Normal file
18
sms4j-example/SpringBoot/SpringbootReadConfig/pom.xml
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?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>org.dromara.sms4j</groupId>
|
||||||
|
<artifactId>SpringBoot</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<groupId>org.dromara.sms4j</groupId>
|
||||||
|
<artifactId>SpringbootReadConfig</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
||||||
@ -1,16 +0,0 @@
|
|||||||
package org.dromara.sms4j.acct;
|
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
|
||||||
@ComponentScan(basePackages = {"org.dromara.sms4j.source"})
|
|
||||||
public class BootSms4jTestByReadConfig {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
SpringApplication.run(BootSms4jTestByReadConfig.class, args);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,21 +0,0 @@
|
|||||||
package org.dromara.sms4j.source;
|
|
||||||
|
|
||||||
import org.dromara.sms4j.provider.factory.ProviderFactoryHolder;
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
|
||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import sms4j.local.a.AFactory;
|
|
||||||
import sms4j.local.b.BFactory;
|
|
||||||
import sms4j.local.c.CFactory;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class EarlyPostProcessor implements BeanFactoryPostProcessor {
|
|
||||||
@Override
|
|
||||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
|
|
||||||
// 注入工厂实例
|
|
||||||
ProviderFactoryHolder.registerFactory(new AFactory());
|
|
||||||
ProviderFactoryHolder.registerFactory(new BFactory());
|
|
||||||
ProviderFactoryHolder.registerFactory(new CFactory());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,35 +0,0 @@
|
|||||||
package org.dromara.sms4j.source;
|
|
||||||
|
|
||||||
import org.dromara.sms4j.api.dao.SmsDao;
|
|
||||||
import org.dromara.sms4j.core.factory.SmsFactory;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.context.annotation.Import;
|
|
||||||
import org.springframework.context.event.ApplicationContextEvent;
|
|
||||||
import org.springframework.context.event.EventListener;
|
|
||||||
import sms4j.dao.MySmsDao;
|
|
||||||
import sms4j.interceptor.MyIntercepterStrategy;
|
|
||||||
import sms4j.interceptor.MyInterceptor;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class Load {
|
|
||||||
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public MyIntercepterStrategy myIntercepterStrategy(){
|
|
||||||
return new MyIntercepterStrategy();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public MyInterceptor myInterceptor(){
|
|
||||||
return new MyInterceptor();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public SmsDao smsDao(){
|
|
||||||
return new MySmsDao();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,59 +0,0 @@
|
|||||||
package org.dromara.sms4j.source;
|
|
||||||
|
|
||||||
import org.dromara.sms4j.core.datainterface.SmsReadConfig;
|
|
||||||
import org.dromara.sms4j.core.factory.SmsFactory;
|
|
||||||
import org.dromara.sms4j.provider.config.BaseConfig;
|
|
||||||
import org.springframework.context.event.ContextRefreshedEvent;
|
|
||||||
import org.springframework.context.event.EventListener;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import sms4j.local.a.AConfig;
|
|
||||||
import sms4j.local.b.BConfig;
|
|
||||||
import sms4j.local.b.BFactory;
|
|
||||||
import sms4j.local.c.CConfig;
|
|
||||||
import sms4j.local.c.CFactory;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class MyConfig implements SmsReadConfig {
|
|
||||||
|
|
||||||
@EventListener
|
|
||||||
public void init(ContextRefreshedEvent event){
|
|
||||||
// 创建SmsBlend 短信实例
|
|
||||||
SmsFactory.createSmsBlend(this,"在配置中定义的configId");
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public BaseConfig getSupplierConfig(String configId) {
|
|
||||||
// 服务商2
|
|
||||||
BConfig bConfig = new BConfig();
|
|
||||||
bConfig.setMaximum(5);
|
|
||||||
bConfig.setConfigId("b1");
|
|
||||||
return bConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<BaseConfig> getSupplierConfigList() {
|
|
||||||
List<BaseConfig> configs = new ArrayList<BaseConfig>();
|
|
||||||
|
|
||||||
// 服务商1
|
|
||||||
CConfig cConfig = new CConfig();
|
|
||||||
cConfig.setConfigId("c1");
|
|
||||||
cConfig.setMaximum(5);
|
|
||||||
configs.add(cConfig);
|
|
||||||
|
|
||||||
|
|
||||||
// 服务商3
|
|
||||||
AConfig aConfig = new AConfig();
|
|
||||||
aConfig.setMaximum(5);
|
|
||||||
aConfig.setConfigId("a1");
|
|
||||||
configs.add(aConfig);
|
|
||||||
|
|
||||||
// 服务商3
|
|
||||||
AConfig aConfig1 = new AConfig();
|
|
||||||
aConfig1.setMaximum(5);
|
|
||||||
aConfig1.setConfigId("a2");
|
|
||||||
configs.add(aConfig1);
|
|
||||||
return configs;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,61 +0,0 @@
|
|||||||
package org.dromara.sms4j.source;
|
|
||||||
|
|
||||||
import org.dromara.sms4j.core.datainterface.SmsReadConfig;
|
|
||||||
import org.dromara.sms4j.core.factory.SmsFactory;
|
|
||||||
import org.dromara.sms4j.provider.config.BaseConfig;
|
|
||||||
import org.springframework.context.event.ContextRefreshedEvent;
|
|
||||||
import org.springframework.context.event.EventListener;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import sms4j.local.a.AConfig;
|
|
||||||
import sms4j.local.b.BConfig;
|
|
||||||
import sms4j.local.b.BFactory;
|
|
||||||
import sms4j.local.c.CConfig;
|
|
||||||
import sms4j.local.c.CFactory;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class ReLoadConfig implements SmsReadConfig {
|
|
||||||
@EventListener
|
|
||||||
public void init(ContextRefreshedEvent event){
|
|
||||||
// 创建SmsBlend 短信实例
|
|
||||||
SmsFactory.createSmsBlend(this,"在配置中定义的configId");
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public BaseConfig getSupplierConfig(String configId) {
|
|
||||||
// 服务商2
|
|
||||||
|
|
||||||
AConfig aConfig1 = new AConfig();
|
|
||||||
aConfig1.setMaximum(5);
|
|
||||||
aConfig1.setConfigId("b1");
|
|
||||||
return aConfig1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<BaseConfig> getSupplierConfigList() {
|
|
||||||
List<BaseConfig> configs = new ArrayList<BaseConfig>();
|
|
||||||
|
|
||||||
// 服务商1
|
|
||||||
CConfig cConfig = new CConfig();
|
|
||||||
cConfig.setConfigId("a1");
|
|
||||||
cConfig.setMaximum(5);
|
|
||||||
configs.add(cConfig);
|
|
||||||
CFactory cFactory = new CFactory();
|
|
||||||
|
|
||||||
|
|
||||||
BFactory bFactory = new BFactory();
|
|
||||||
|
|
||||||
// 服务商3
|
|
||||||
AConfig aConfig = new AConfig();
|
|
||||||
aConfig.setMaximum(5);
|
|
||||||
aConfig.setConfigId("c1");
|
|
||||||
configs.add(aConfig);
|
|
||||||
|
|
||||||
|
|
||||||
BConfig bConfig = new BConfig();
|
|
||||||
bConfig.setMaximum(5);
|
|
||||||
bConfig.setConfigId("a2");
|
|
||||||
configs.add(bConfig);
|
|
||||||
return configs;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,68 @@
|
|||||||
|
package org.dromara.sms4j.test;
|
||||||
|
|
||||||
|
import org.dromara.sms4j.core.datainterface.SmsBlendsSelectedConfig;
|
||||||
|
import org.dromara.sms4j.core.factory.SmsFactory;
|
||||||
|
import org.dromara.sms4j.provider.config.BaseConfig;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.event.ContextRefreshedEvent;
|
||||||
|
import org.springframework.context.event.EventListener;
|
||||||
|
|
||||||
|
|
||||||
|
import sms4j.dao.MySmsDao;
|
||||||
|
import sms4j.interceptor.MyIntercepterStrategy;
|
||||||
|
import sms4j.interceptor.MyInterceptor;
|
||||||
|
import sms4j.local.a.AConfig;
|
||||||
|
import sms4j.local.a.AFactory;
|
||||||
|
|
||||||
|
// SmsReadConfig 是一个存在但是未来可能被删除的一个接口,理想状态是使用 【SmsBlendsBeanConfig、SmsBlendsSelectedConfig】 SmsReadConfig正是继承了前面提到的两个接口
|
||||||
|
// 这里为了只用一个类配置以此测试演示,使用中可以分开配置不用都写在一起
|
||||||
|
@Configuration
|
||||||
|
public class BootConfigBlackListValidateConfig implements SmsBlendsSelectedConfig {
|
||||||
|
|
||||||
|
//自定义渠道工厂
|
||||||
|
@Bean
|
||||||
|
public AFactory myFactory() {
|
||||||
|
return new AFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
//自定义拦截器
|
||||||
|
@Bean
|
||||||
|
public MyInterceptor myInterceptor() {
|
||||||
|
return new MyInterceptor();
|
||||||
|
}
|
||||||
|
|
||||||
|
//自定义拦截器策略
|
||||||
|
@Bean
|
||||||
|
public MyIntercepterStrategy myIntercepterStrategy() {
|
||||||
|
return new MyIntercepterStrategy();
|
||||||
|
}
|
||||||
|
|
||||||
|
//自定义Dao
|
||||||
|
@Bean
|
||||||
|
public MySmsDao mySmsDao() {
|
||||||
|
return MySmsDao.getInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BaseConfig getSupplierConfig(String configId) {
|
||||||
|
// 服务商渠道配置
|
||||||
|
// 服务商
|
||||||
|
AConfig aConfig = new AConfig();
|
||||||
|
if ("a1".equals(configId)){
|
||||||
|
aConfig.setMaximum(5);
|
||||||
|
aConfig.setConfigId("a1");
|
||||||
|
}
|
||||||
|
if ("a2".equals(configId)){
|
||||||
|
aConfig.setMaximum(5);
|
||||||
|
aConfig.setConfigId("a2");
|
||||||
|
}
|
||||||
|
return aConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventListener
|
||||||
|
//不是非要用监听器,有无数种方法,这里就是介绍这个SmsBlendsSelectedConfig 需要你在本类加载之后手动createSmsBlend选择使用那一个配置
|
||||||
|
public void whenYouAppStarted(ContextRefreshedEvent event){
|
||||||
|
SmsFactory.createSmsBlend(this,"a1");
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,16 @@
|
|||||||
|
package org.dromara.sms4j.test;
|
||||||
|
|
||||||
|
import org.dromara.sms4j.test.validate.BlackListValidate;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class BootConfigBlackListValidateTest {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(BootConfigBlackListValidateTest.class, args);
|
||||||
|
|
||||||
|
BlackListValidate blackListValidate = new BlackListValidate("a","11111111111");
|
||||||
|
blackListValidate.test();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,7 +1,9 @@
|
|||||||
sms:
|
sms:
|
||||||
# 标注从yml读取配置
|
# 标注从yml读取配置
|
||||||
config-type: interface
|
config-type: interface
|
||||||
#restricted: true
|
# 是否开启拦截
|
||||||
|
restricted: true
|
||||||
|
# 账户发送上限
|
||||||
account-max: 4
|
account-max: 4
|
||||||
|
# 分钟发送上限
|
||||||
minute-max: 3
|
minute-max: 3
|
||||||
|
|||||||
51
sms4j-example/SpringBoot/pom.xml
Normal file
51
sms4j-example/SpringBoot/pom.xml
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<?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>org.dromara.sms4j</groupId>
|
||||||
|
<artifactId>sms4j-example</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<modules>
|
||||||
|
<module>SpringbootReadConfig</module>
|
||||||
|
<module>SpringBootYamlFile</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
<artifactId>SpringBoot</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<properties>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-dependencies</artifactId>
|
||||||
|
<version>${spring.boot.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.dromara.sms4j</groupId>
|
||||||
|
<artifactId>sms4j-spring-boot-starter</artifactId>
|
||||||
|
<version>${sms4j.version} </version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
<version>${spring.boot.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.dromara.sms4j</groupId>
|
||||||
|
<artifactId>TestCase</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.dromara.sms4j</groupId>
|
||||||
|
<artifactId>Customize</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
@ -1,7 +1,7 @@
|
|||||||
# 项目结构
|
# 项目结构
|
||||||
* sms4j-example
|
* sms4j-example
|
||||||
* [Component](Component) 基础依赖,包括测试案例和自定义的相关内容
|
* [Component](Component) 基础依赖,包括测试案例和自定义的相关内容
|
||||||
* [Customize](Component%2FCustomize) sms4j扩展的自定义的相关内容
|
* [Customize](Component%2FCustomize) sms4j扩展的自定义案例的相关内容
|
||||||
* 自定义SmsDao实现 [dao](Component%2FCustomize%2Fsrc%2Fmain%2Fjava%2Fsms4j%2Fdao)
|
* 自定义SmsDao实现 [dao](Component%2FCustomize%2Fsrc%2Fmain%2Fjava%2Fsms4j%2Fdao)
|
||||||
* 自定义拦截器、拦截器策略实现 [interceptor](Component%2FCustomize%2Fsrc%2Fmain%2Fjava%2Fsms4j%2Finterceptor)
|
* 自定义拦截器、拦截器策略实现 [interceptor](Component%2FCustomize%2Fsrc%2Fmain%2Fjava%2Fsms4j%2Finterceptor)
|
||||||
* 自定义厂商实现 [local](Component%2FCustomize%2Fsrc%2Fmain%2Fjava%2Fsms4j%2Flocal)
|
* 自定义厂商实现 [local](Component%2FCustomize%2Fsrc%2Fmain%2Fjava%2Fsms4j%2Flocal)
|
||||||
|
|||||||
@ -90,6 +90,7 @@ public class SmsBlendsInitializer extends AbstractInitalizer {
|
|||||||
//SmsBlendsBeanConfig接口实现类的配置信息处理
|
//SmsBlendsBeanConfig接口实现类的配置信息处理
|
||||||
for (SmsBlendsBeanConfig beanConfig : beanConfigs) {
|
for (SmsBlendsBeanConfig beanConfig : beanConfigs) {
|
||||||
SmsFactory.createSmsBlend(beanConfig);
|
SmsFactory.createSmsBlend(beanConfig);
|
||||||
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user