test[boot]: boot test unit

This commit is contained in:
godotg
2022-10-22 22:28:36 +08:00
parent bbe0579982
commit 033449e7dc
5 changed files with 159 additions and 8 deletions
@@ -14,15 +14,12 @@ package com.zfoo.boot;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zfoo.protocol.util.JsonUtils;
import com.zfoo.protocol.util.ReflectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.lang.reflect.Field;
/**
* @author godotg
* @version 3.0
@@ -34,12 +31,9 @@ public class BootAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public ObjectMapper getObjectMapper() throws NoSuchFieldException {
// 拿到JsonUtils中的ObjectMapper
Field field = JsonUtils.class.getDeclaredField("MAPPER");
ReflectionUtils.makeAccessible(field);
public ObjectMapper getObjectMapper() {
logger.info("Jackson auto config successfully!");
return (ObjectMapper) ReflectionUtils.getField(field, null);
return JsonUtils.MAPPER;
}
}
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2020 The zfoo Authors
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.boot;
import com.zfoo.boot.resource.StudentResource;
import com.zfoo.protocol.util.JsonUtils;
import com.zfoo.storage.StorageContext;
import com.zfoo.storage.model.config.StorageConfig;
import com.zfoo.storage.model.vo.Storage;
import com.zfoo.util.ThreadUtils;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
/**
* @author godotg
* @version 3.0
*/
@Ignore
@SpringBootApplication(exclude = {
// 排除MongoDB自动配置
MongoAutoConfiguration.class
})
public class StorageTest {
private static final Logger logger = LoggerFactory.getLogger(StorageTest.class);
@Bean
public StorageConfig storageConfig(Environment environment) {
var config = new StorageConfig();
config.setScanPackage("com.zfoo.boot");
config.setResourceLocation("classpath:/excel");
config.setWriteable(true);
return config;
}
@Test
public void startApplication() {
var springBoot = new SpringApplicationBuilder();
springBoot.sources(StorageTest.class).web(WebApplicationType.NONE).run();
var storage = (Storage<Integer, StudentResource>) StorageContext.getStorageManager().getStorage(StudentResource.class);
for (StudentResource resource : storage.getAll()) {
logger.info(JsonUtils.object2String(resource));
}
ThreadUtils.sleep(Long.MAX_VALUE);
}
}
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2020 The zfoo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.boot.resource;
import com.zfoo.protocol.IPacket;
import com.zfoo.storage.model.anno.Id;
import com.zfoo.storage.model.anno.Resource;
/**
* @author godotg
* @version 4.0
*/
@Resource
public class StudentResource implements IPacket {
@Id
public int id;
public String name;
public int age;
public float score;
public String[] courses;
public User[] users;
public User user;
}
class User implements IPacket {
public String id;
public String name;
public String sex;
public int age;
}
Binary file not shown.
+44
View File
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="false" debug="false">
<contextName>com.zfoo.boot</contextName>
<property name="LOG_HOME" value="log/boot"/>
<property name="PATTERN_FILE"
value="%d{yyyy-MM-dd HH:mm:ss} [%5level] [%thread] %logger.%M\\(%F:%line\\) - %msg%n"/>
<property name="PATTERN_CONSOLE"
value="%d{yyyy-MM-dd HH:mm:ss} [%highlight(%5level)] [%thread] %logger.%M\\(%F:%line\\) - %msg%n"/>
<!-- 负责写日志,控制台日志,会打印所有的包的所有级别日志 -->
<appender name="zfoo_console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${PATTERN_CONSOLE}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- 根logger -->
<root level="info">
<appender-ref ref="zfoo_console"/>
</root>
<!--
- 1.name:包名或类名,用来指定受此logger约束的某一个包或者具体的某一个类
- 2.未设置打印级别,所以继承他的上级<root>的日志级别“DEBUG”
- 3.未设置additivity,默认为true,将此logger的打印信息向上级传递;
- 4.未设置appender,此logger本身不打印任何信息,级别为“DEBUG”及大于“DEBUG”的日志信息传递给root,
- root接到下级传递的信息,交给已经配置好的名为“STDOUT”的appender处理,“STDOUT”appender将信息打印到控制台;
-->
<logger name="ch.qos.logback" level="info"/>
<!--*******************************************Spring********************************************************-->
<!--logger中的name是指代码的包名或类名,路径要写全,可以配置不同包中的日志输出到不同的文件中。level是日志输出级别 -->
<!--过滤掉spring的一些无用的DEBUG信息-->
<logger name="org.springframework" level="info"/>
<!-- additivity="false"表示不继承父logger的配置和父类没有关系-->
<logger name="org.springframework.core" level="warn"/>
<!--*******************************************Netty*********************************************************-->
<logger name="io.netty" level="info"/>
</configuration>