/
LenientCopyToolTest.java
172 lines (136 loc) · 5.45 KB
/
LenientCopyToolTest.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
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.util.reflection;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
import java.lang.reflect.Field;
import java.util.LinkedList;
import org.junit.Test;
import org.mockito.plugins.MemberAccessor;
import org.mockitoutil.TestBase;
@SuppressWarnings("unchecked")
public class LenientCopyToolTest extends TestBase {
private LenientCopyTool tool = new LenientCopyTool();
static class InheritMe {
protected String protectedInherited = "protected";
private String privateInherited = "private";
}
public static class SomeObject extends InheritMe {
@SuppressWarnings("unused")
// required because static fields needs to be excluded from copying
private static int staticField = -100;
private int privateField = -100;
private transient int privateTransientField = -100;
String defaultField = "-100";
protected Object protectedField = new Object();
public SomeOtherObject instancePublicField = new SomeOtherObject();
final int finalField;
public SomeObject(int finalField) {
this.finalField = finalField;
}
}
public static class SomeOtherObject {}
private SomeObject from = new SomeObject(100);
private SomeObject to = mock(SomeObject.class);
@Test
public void shouldShallowCopyBasicFinalField() throws Exception {
// given
assertEquals(100, from.finalField);
assertThat(to.finalField).isNotEqualTo(100);
// when
tool.copyToMock(from, to);
// then
assertEquals(100, to.finalField);
}
@Test
public void shouldShallowCopyTransientPrivateFields() throws Exception {
// given
from.privateTransientField = 1000;
assertThat(to.privateTransientField).isNotEqualTo(1000);
// when
tool.copyToMock(from, to);
// then
assertEquals(1000, to.privateTransientField);
}
@Test
public void shouldShallowCopyLinkedListIntoMock() throws Exception {
// given
LinkedList fromList = new LinkedList();
LinkedList toList = mock(LinkedList.class);
// when
tool.copyToMock(fromList, toList);
// then no exception is thrown
}
@Test
public void shouldShallowCopyFieldValuesIntoMock() throws Exception {
// given
from.defaultField = "foo";
from.instancePublicField = new SomeOtherObject();
from.privateField = 1;
from.privateTransientField = 2;
from.protectedField = 3;
assertThat(to.defaultField).isNotEqualTo(from.defaultField);
assertThat(to.instancePublicField).isNotEqualTo(from.instancePublicField);
assertThat(to.privateField).isNotEqualTo(from.privateField);
assertThat(to.privateTransientField).isNotEqualTo(from.privateTransientField);
assertThat(to.protectedField).isNotEqualTo(from.protectedField);
// when
tool.copyToMock(from, to);
// then
assertEquals(from.defaultField, to.defaultField);
assertEquals(from.instancePublicField, to.instancePublicField);
assertEquals(from.privateField, to.privateField);
assertEquals(from.privateTransientField, to.privateTransientField);
assertEquals(from.protectedField, to.protectedField);
}
@Test
public void shouldCopyValuesOfInheritedFields() throws Exception {
// given
((InheritMe) from).privateInherited = "foo";
((InheritMe) from).protectedInherited = "bar";
assertThat(((InheritMe) to).privateInherited)
.isNotEqualTo(((InheritMe) from).privateInherited);
// when
tool.copyToMock(from, to);
// then
assertEquals(((InheritMe) from).privateInherited, ((InheritMe) to).privateInherited);
}
@Test
public void shouldContinueEvenIfThereAreProblemsCopyingSingleFieldValue() throws Exception {
// given
tool.accessor = mock(MemberAccessor.class);
doNothing()
.doThrow(new IllegalStateException())
.doNothing()
.when(tool.accessor)
.set(any(Field.class), anyObject(), anyObject());
// when
tool.copyToMock(from, to);
// then
verify(tool.accessor, atLeast(3)).get(any(Field.class), any());
verify(tool.accessor, atLeast(3)).set(any(Field.class), any(), any());
}
@Test
public void shouldBeAbleToCopyFromRealObjectToRealObject() throws Exception {
// given
from.defaultField = "defaultField";
from.instancePublicField = new SomeOtherObject();
from.privateField = 1;
from.privateTransientField = 2;
from.protectedField = "protectedField";
from.protectedInherited = "protectedInherited";
to = new SomeObject(0);
// when
tool.copyToRealObject(from, to);
// then
assertEquals(from.defaultField, to.defaultField);
assertEquals(from.instancePublicField, to.instancePublicField);
assertEquals(from.privateField, to.privateField);
assertEquals(from.privateTransientField, to.privateTransientField);
assertEquals(from.protectedField, to.protectedField);
assertEquals(from.protectedInherited, to.protectedInherited);
}
}