锲而舍之,朽木不折;锲而不舍,金石可镂。——荀子

这还是个实验性功能,首先引入:

1
2
3
4
5
<dependency>
<groupId>org.dromara.stream-query</groupId>
<artifactId>stream-plugin-mybatis-plus</artifactId>
<version>2.1.0-alpha</version>
</dependency>

然后对应的单元测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.dromara.streamquery.stream.core.bean;

import lombok.Data;
import lombok.experimental.Accessors;
import lombok.val;
import org.junit.jupiter.api.Test;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.*;

/**
* BeanHelperTest
*
* @author VampireAchao
* @since 2023/3/15
*/
class BeanHelperTest {

@Test
void testGetSetterName() {
assertEquals("setName", BeanHelper.getSetterName("name"));
assertEquals("setLambda", BeanHelper.getSetterName("lambda"));
}

@Test
void testGetGetterName() {
assertEquals("getName", BeanHelper.getGetterName("name"));
assertEquals("getLambda", BeanHelper.getGetterName("lambda"));
}

@Data
public static class User {
private String name;
}

@Data
@Accessors(chain = true)
public static class Person {
private String name;
}

@Data
public static class Artist implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
}

@Test
void testCopyProperties() {
User source =
new User() {
{
setName("test");
}
};
Person person = BeanHelper.copyProperties(source, Person.class);
assertEquals(source.getName(), person.getName());

Artist artist =
new Artist() {
private static final long serialVersionUID = 6276191330280044345L;

{
setName("test");
}
};
User user = BeanHelper.copyProperties(source, User.class);
assertEquals(artist.getName(), user.getName());
}

@Data
public static class EntityWithStringId {
private String id;
}

@Data
public static class EntityWithIntegerId {
private Integer id;
}

@Test
void testCopyPropertiesWithConverter() {
val source = new EntityWithStringId();
source.setId("1");
EntityWithIntegerId target = BeanHelper.copyProperties(source, EntityWithIntegerId.class);
assertEquals(source.getId(), target.getId().toString());
assertEquals(
source.getId(), BeanHelper.copyProperties(target, EntityWithStringId.class).getId());
assertEquals(
target.getId(),
BeanHelper.copyProperties(
source,
EntityWithIntegerId.class,
new CopyOption().addConverter(String.class, Integer.class, Integer::new))
.getId());
assertEquals(
target.getId(),
BeanHelper.copyProperties(
source,
EntityWithIntegerId.class,
new CopyOption()
.addConverter(
EntityWithStringId.class,
EntityWithIntegerId.class,
s -> {
val t = new EntityWithIntegerId();
t.setId(Integer.valueOf(s.getId()));
return t;
}))
.getId());

assertNull(BeanHelper.copyProperties(null, Object.class));
assertEquals("1", BeanHelper.copyProperties(1, String.class));
assertEquals(Integer.valueOf(1), BeanHelper.copyProperties(1L, Integer.class));
assertEquals(Integer.valueOf(1), BeanHelper.copyProperties("1", Integer.class));
assertEquals(Long.valueOf(1L), BeanHelper.copyProperties("1", Long.class));
assertEquals(Long.valueOf(1L), BeanHelper.copyProperties(1, Long.class));
assertEquals(Float.valueOf(1.0f), BeanHelper.copyProperties(1.0, Float.class));
assertEquals(Double.valueOf(1.0), BeanHelper.copyProperties("1.0", Double.class));
assertEquals(Float.valueOf(1.0f), BeanHelper.copyProperties("1.0", Float.class));
assertEquals(Double.valueOf(1.0), BeanHelper.copyProperties(1.0f, Double.class));
assertEquals(Double.valueOf(1.0), BeanHelper.copyProperties(1, Double.class));
assertEquals(Float.valueOf(1.0f), BeanHelper.copyProperties(1, Float.class));
assertEquals(Float.valueOf(1.0f), BeanHelper.copyProperties(1L, Float.class));
assertEquals(Double.valueOf(1.0), BeanHelper.copyProperties(1L, Double.class));
assertEquals(Boolean.TRUE, BeanHelper.copyProperties("true", Boolean.class));
Date now = new Date();
assertEquals(now.getTime(), BeanHelper.copyProperties(now, Long.class));
assertEquals(new Date(now.getTime()), BeanHelper.copyProperties(now.getTime(), Date.class));
UUID uuid = UUID.randomUUID();
assertEquals(uuid, BeanHelper.copyProperties(uuid.toString(), UUID.class));
LocalDateTime ldt = LocalDateTime.now();
assertEquals(
Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant()),
BeanHelper.copyProperties(ldt, Date.class));
LocalDate ld = LocalDate.now();
assertEquals(
Date.from(ld.atStartOfDay(ZoneId.systemDefault()).toInstant()),
BeanHelper.copyProperties(ld, Date.class));
assertEquals(ld, BeanHelper.copyProperties(ldt, LocalDate.class));
assertEquals(ld.atStartOfDay(), BeanHelper.copyProperties(ld, LocalDateTime.class));
}

@Test
void testIgnoreError() {
val source = new EntityWithStringId();
source.setId("1");
assertThrows(
ConvertFailException.class,
() -> BeanHelper.copyProperties(source, EntityWithIntegerId.class, new CopyOption()));
assertDoesNotThrow(
() ->
BeanHelper.copyProperties(
source, EntityWithIntegerId.class, new CopyOption().setIgnoreError(true)));
}
}

这里可以自定义Converter,此处new CopyOption()是不带默认内置的Converter的,但是CopyOption.of()是带的