Skip to content

Quick Start

jitwxs edited this page Jun 5, 2022 · 3 revisions

01 数据加载

用于测试的 Java 对象:

@Data
public class WebsiteUser {

    private int id;

    private String firstName;

    private String lastName;

    private int age;

    private String email;

    private SexEnum gender;

    private String ipAddress;

    private String agent;

    private LocalDateTime createTime;
}

1.1 CSV 数据加载

新建 resources/easydata/loader/website_user.csv 文件,内容如下:

id,first_name,last_name,age,email,gender,ip_address,agent,create_time
1,Pepito,MacShane,2,pmacshane0@mashable.com,Male,229.152.71.123,"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; zh-tw) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16",2022-03-28 17:11:06
2,Con,Dine-Hart,48,cdinehart1@omniture.com,Female,232.143.248.181,Mozilla/5.0 (Windows NT 6.2; rv:21.0) Gecko/20130326 Firefox/21.0,2021-09-05 02:33:50
3,Amelita,Pennoni,77,apennoni2@state.gov,Female,76.91.68.251,"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36",2021-10-28 10:13:10
4,Sheela,O'Shiel,94,soshiel3@arstechnica.com,Female,127.230.59.89,Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0,2021-03-30 06:30:47
5,Thacher,Okenfold,76,tokenfold4@purevolume.com,Bigender,172.53.50.228,"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4",2021-12-05 18:53:14

测试用例:

// see io.github.jitwxs.easydata.sample.bean.WebsiteUserTest#loadCsv

@Test
public void loadCsv() {
    final LoaderProperties properties = LoaderProperties.builder().url("/easydata/loader/website_user.csv").build();

    final List<WebsiteUser> userList = EasyLoader.FILE_LOADER.loading(WebsiteUser.class, properties);

    Assertions.assertNotNull(userList);
    Assertions.assertEquals(5, userList.size());
}

1.2 JSON 数据加载

新建 resources/easydata/loader/website_user.json 文件,内容如下:

[{
  "id": 1,
  "first_name": "Karlyn",
  "last_name": "Chaize",
  "age": 82,
  "email": "kchaize0@wikispaces.com",
  "gender": "Female",
  "ip_address": "240.224.4.223",
  "agent": "Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.2 (KHTML, like Gecko) Ubuntu/11.10 Chromium/15.0.874.120 Chrome/15.0.874.120 Safari/535.2",
  "create_time": "2022-01-03 15:28:19"
}, {
  "id": 2,
  "first_name": "Nollie",
  "last_name": "Tunny",
  "age": 2,
  "email": "ntunny1@cdc.gov",
  "gender": "Genderqueer",
  "ip_address": "3.251.168.196",
  "agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.62 Safari/537.36",
  "create_time": "2021-08-01 19:44:45"
}, {
  "id": 3,
  "first_name": "Almeria",
  "last_name": "MacGebenay",
  "age": 55,
  "email": "amacgebenay2@sitemeter.com",
  "gender": "Non-binary",
  "ip_address": "201.32.93.245",
  "agent": "Mozilla/5.0 (Windows NT 7.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30",
  "create_time": "2021-09-27 15:11:51"
}, {
  "id": 4,
  "first_name": "Shurwood",
  "last_name": "Beedom",
  "age": 31,
  "email": "sbeedom3@bizjournals.com",
  "gender": "Male",
  "ip_address": "63.214.99.76",
  "agent": "Mozilla/5.0 (X11; NetBSD) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36",
  "create_time": "2022-03-12 17:26:30"
}, {
  "id": 5,
  "first_name": "Jarid",
  "last_name": "Laden",
  "age": 94,
  "email": "jladen4@apache.org",
  "gender": "Male",
  "ip_address": "57.162.181.250",
  "agent": "Mozilla/5.0 (Windows NT 6.0; WOW64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.34 Safari/534.24",
  "create_time": "2021-11-22 00:03:23"
}]

测试用例:

// see io.github.jitwxs.easydata.sample.bean.WebsiteUserTest#loadJson

@Test
public void loadJson() {
    final LoaderProperties properties = LoaderProperties.builder().url("/easydata/loader/website_user.json").build();

    final List<WebsiteUser> userList = EasyLoader.FILE_LOADER.loading(WebsiteUser.class, properties);

    Assertions.assertNotNull(userList);
    Assertions.assertEquals(5, userList.size());
}

1.3 DB 数据加载

这里我使用 testcontainer 进行测试,你也可以将以下 DDL 语句创建在本地的数据库中。

create table website_user (
                           id INT,
                           first_name VARCHAR(50),
                           last_name VARCHAR(50),
                           age INT,
                           email VARCHAR(50),
                           gender VARCHAR(50),
                           ip_address VARCHAR(20),
                           agent VARCHAR(128),
                           create_time DATE
);
insert into website_user (id, first_name, last_name, age, email, gender, ip_address, agent, create_time) values (1, 'Bobbie', 'Nevill', 58, 'bnevill0@google.com', 'Female', '116.182.208.91', 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:16.0.1) Gecko/20121011 Firefox/21.0.1', '2021-08-29 00:05:19');
insert into website_user (id, first_name, last_name, age, email, gender, ip_address, agent, create_time) values (2, 'Sibyl', 'Bezants', 25, 'sbezants1@zimbio.com', 'Non-binary', '248.28.91.246', 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.91 Chromium/12.0.742.91 Safari/534.30', '2021-09-16 16:32:38');
insert into website_user (id, first_name, last_name, age, email, gender, ip_address, agent, create_time) values (3, 'Melesa', 'Hutchins', 38, 'mhutchins2@printfriendly.com', 'Female', '87.22.197.179', 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.815.0 Safari/535.1', '2021-07-13 07:41:06');
insert into website_user (id, first_name, last_name, age, email, gender, ip_address, agent, create_time) values (4, 'Sigismundo', 'Galloway', 78, 'sgalloway3@histats.com', 'Male', '145.67.209.103', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.186 Safari/535.1', '2021-10-03 23:21:23');
insert into website_user (id, first_name, last_name, age, email, gender, ip_address, agent, create_time) values (5, 'Cristin', 'MacHarg', 71, 'cmacharg4@ox.ac.uk', 'Genderqueer', '102.92.94.22', 'Mozilla/5.0 (Windows NT 6.2; rv:22.0) Gecko/20130405 Firefox/23.0', '2022-01-15 11:59:25');

测试用例:

// see io.github.jitwxs.easydata.sample.bean.WebsiteUserTest#loadMySQL

@Test
public void loadMySQL() {
    final MySQLConnection connection = new MySQLConnection(
                container.getDriverClassName(),
                container.getUsername(),
                container.getPassword(),
                container.getJdbcUrl());

    final LoaderProperties properties = LoaderProperties.builder().url("SELECT * FROM website_user").build();

    final List<WebsiteUser> userList = new EasyLoader(connection).loading(WebsiteUser.class, properties);

    Assertions.assertNotNull(userList);
    Assertions.assertEquals(5, userList.size());
}

1.4 内存数据加载

使用该功能,可以更轻量的加载 CSV / JSON 文件,而不强制从文件中读取,以 CSV 为例:

// see io.github.jitwxs.easydata.sample.bean.WebsiteUserTest#loadEmbeddedCsv

@Test
public void loadEmbeddedCsv() {
    final EasyLoader easyLoader = new EasyLoader(new EmbeddedConnection(FileFormatEnum.CSV));

    final LoaderProperties properties = LoaderProperties.builder().url("id,first_name,last_name,age,email,gender,ip_address,agent,create_time\n" +
            "1,Pepito,MacShane,2,pmacshane0@mashable.com,Male,229.152.71.123,\"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; zh-tw) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16\",2022-03-28 17:11:06\n" +
            "2,Con,Dine-Hart,48,cdinehart1@omniture.com,Female,232.143.248.181,Mozilla/5.0 (Windows NT 6.2; rv:21.0) Gecko/20130326 Firefox/21.0,2021-09-05 02:33:50\n" +
            "3,Amelita,Pennoni,77,apennoni2@state.gov,Female,76.91.68.251,\"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36\",2021-10-28 10:13:10\n" +
            "4,Sheela,O'Shiel,94,soshiel3@arstechnica.com,Female,127.230.59.89,Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0,2021-03-30 06:30:47\n" +
            "5,Thacher,Okenfold,76,tokenfold4@purevolume.com,Bigender,172.53.50.228,\"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4\",2021-12-05 18:53:14").build();

    final List<WebsiteUser> userList = easyLoader.loading(WebsiteUser.class, properties);

    Assertions.assertNotNull(userList);
    Assertions.assertEquals(5, userList.size());
}

02 数据 Mock

// 基本数据
final double aDouble = EasyMock.run(double.class);
// 包装数据
final Integer integer = EasyMock.run(Integer.class);
// 数组
final char[] chars = EasyMock.run(char[].class);
// 多维数组
final boolean[][][] booleans = EasyMock.run(boolean[][][].class);

// Java 枚举
final SexEnum sexEnum = EasyMock.run(SexEnum.class);
// Protobuf 枚举
final EnumProto.SexEnum sexEnum = EasyMock.run(EnumProto.SexEnum.class);

// Java 对象
final OrderEvaluate orderEvaluate = EasyMock.run(OrderEvaluate.class);
// Protobuf 对象
final MessageProto.OrderEvaluate orderEvaluate = EasyMock.run(MessageProto.OrderEvaluate.class);

// 集合
final List<String> stringList = EasyMock.run(new TypeKit<List<String>>() {});
// Map
final Map<Boolean, Set<BigDecimal>> listMap = EasyMock.run(new TypeKit<Map<Boolean, Set<BigDecimal>>>() {});
// KV Pair
final Pair<Integer, IgnoreBean> beanPair = EasyMock.run(new TypeKit<Pair<Integer, IgnoreBean>>() {});

更多示例请参考:

  • io.github.jitwxs.easydata.core.mock.EasyMockTest
  • io.github.jitwxs.easydata.core.mock.SpecialClassMockTest
  • io.github.jitwxs.easydata.sample.core.mock.EasyMockTest

03 数据转换

import io.github.jitwxs.easydata.provider.ConvertProvider;
import io.github.jitwxs.easydata.provider.ProviderFactory;

final ConvertProvider delegate = ProviderFactory.delegate(ConvertProvider.class);

// 字符串转数值
assertEquals(111, delegate.convert("111", int.class));
assertEquals(12.5, delegate.convert("12.5", double.class));

// 字符串转 boolean
assertEquals(true, delegate.convert("true", boolean.class));
assertEquals(false, delegate.convert("0", boolean.class));
assertEquals(true, delegate.convert("T", boolean.class));

// 时间转换
assertEquals(LocalDateTime.of(2022, 5, 29, 15, 1, 3, 0),
        delegate.convert("2022-05-29 15:01:03.000", LocalDateTime.class));
assertEquals(LocalDateTime.of(2022, 5, 29, 15, 0, 49, 921 * 1000000),
        delegate.convert(1653807649921L, LocalDateTime.class));

更多示例请参考:

  • io.github.jitwxs.easydata.core.convert.SimpleConvertTest
  • io.github.jitwxs.easydata.core.convert.AboutTimeConvertTest
  • io.github.jitwxs.easydata.sample.core.convert.ProtoConvertTest

04 数据验证

用于比较两个对象是否相等,更多高阶移步后续文档。

// 简单类型,相等
assertDoesNotThrow(() -> EasyVerify.with(15, 15).verify());
// 指定精度误差,相等
assertDoesNotThrow(() -> EasyVerify.with(1D, 1.1D).withPrecision(0.2D).verify());

final Collection<EnumProto.SexEnum> enums1 = Lists.newArrayList(EnumProto.SexEnum.MALE, EnumProto.SexEnum.FEMALE);
final Collection<EnumProto.SexEnum> enums2 = new LinkedList<>(Arrays.asList(EnumProto.SexEnum.MALE, EnumProto.SexEnum.FEMALE));

// arrayList, linkedList 直接比较,不想等
assertThrows(AssertionError.class, () -> EasyVerify.with(enums1, enums2).verify());
// arrayList, linkedList 忽略类型差异,相等
assertDoesNotThrow(() -> EasyVerify.with(enums1, enums2).ignoreClassDiff(CONVERT_SAME_CLASS).verify());

更多示例请参考:

  • io.github.jitwxs.easydata.core.verify.*
  • io.github.jitwxs.easydata.sample.core.verify.*

05 MyBatis Mapper 验证

验证 MyBatis Mapper 文件正确性,继承 MyBatisMapperInspect 抽象类即可。

// see also io.github.jitwxs.easydata.sample.mapper.OrderEvaluateMapperTest
public class OrderEvaluateMapperTest extends MyBatisMapperInspect<OrderEvaluateMapper> {
    @Override
    public List<IMapperInspectAction> actionList() {
        return Arrays.asList(
                new ResultMapAttributeMapperInspectAction("ResultMap", new String[]{"sId", "javaBeanUnknownFields"}),
                new ColumnsMapperInspectAction("columns", "ResultMap"),
                new SqlStatementMapperInspectAction()
        );
    }
}