-
Notifications
You must be signed in to change notification settings - Fork 77
/
AndroidRecordTest.java
160 lines (130 loc) · 4.17 KB
/
AndroidRecordTest.java
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
package tools.jackson.module.androidrecord;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import com.android.tools.r8.RecordTag;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.exc.InvalidDefinitionException;
import tools.jackson.databind.json.JsonMapper;
import junit.framework.TestCase;
import org.junit.Assert;
/**
* Inner test classes simulate Android-desugared records.
*
* @author Eran Leshem
**/
public class AndroidRecordTest extends TestCase {
static final class Simple extends RecordTag {
static int si = 7;
private final int i;
private final int j;
private final String s;
private final List<String> l;
private final AtomicInteger ai;
Simple(int i, int j, String s, List<String> l, AtomicInteger ai) {
this.i = i;
this.j = j;
this.s = s;
this.l = l;
this.ai = ai;
}
int i() {
return i;
}
String s() {
return s;
}
List<String> l() {
return l;
}
int j() {
return j;
}
AtomicInteger ai() {
return ai;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Simple)) {
return false;
}
Simple simple = (Simple) o;
return i == simple.i && j == simple.j && Objects.equals(s, simple.s) && Objects.equals(l, simple.l)
&& Objects.equals(ai.get(), simple.ai.get());
}
}
static final class MultipleConstructors extends RecordTag {
private final int i;
private final List<String> l;
MultipleConstructors(int i, List<String> l) {
this.i = i;
this.l = l;
}
MultipleConstructors(String s, List<String> l) {
i = Integer.parseInt(s);
this.l = l;
}
MultipleConstructors(int i, String s, List<String> l) {
this.i = i;
this.l = l;
}
MultipleConstructors(List<Integer> l, int i) {
this.i = i;
this.l = null;
}
int i() {
return i;
}
List<String> l() {
return l;
}
}
static final class ConflictingConstructors extends RecordTag {
private final int i;
private final String s;
ConflictingConstructors(int i, String s) {
this.i = i;
this.s = s;
}
ConflictingConstructors(String s, int i) {
this.i = i;
this.s = s;
}
public int i() {
return i;
}
public String s() {
return s;
}
}
private final ObjectMapper _objectMapper = JsonMapper.builder()
.addModule(new AndroidRecordModule())
.changeDefaultVisibility(vc -> vc.withVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY))
.build();
public void testSimple() throws Exception {
Simple simple = new Simple(9, 3, "foo", Arrays.asList("bar", "baz"), new AtomicInteger(8));
assertEquals(simple, _objectMapper.readValue(_objectMapper.writeValueAsString(simple), Simple.class));
}
public void testMultipleConstructors() throws Exception {
List<String> l = Arrays.asList("bar", "baz");
assertEquals(9, _objectMapper.readValue(_objectMapper.writeValueAsString(new MultipleConstructors(9, l)),
MultipleConstructors.class).i());
assertEquals(9, _objectMapper.readValue(_objectMapper.writeValueAsString(
new MultipleConstructors("9", l)), MultipleConstructors.class).i());
assertEquals(9, _objectMapper.readValue(_objectMapper.writeValueAsString(
new MultipleConstructors(9,"foobar", l)), MultipleConstructors.class).i());
assertEquals(9, _objectMapper.readValue(_objectMapper.writeValueAsString(
new MultipleConstructors(Arrays.asList(1, 2), 9)), MultipleConstructors.class).i());
}
public void testConflictingConstructors() {
Assert.assertThrows(InvalidDefinitionException.class,
() -> _objectMapper.readValue(_objectMapper.writeValueAsString(
new ConflictingConstructors(9, "foo")), ConflictingConstructors.class));
}
}