Skip to content

Commit 88586d6

Browse files
committed
[LANG-1708] Add tests that assert the current behavior
1 parent 7c8d26d commit 88586d6

File tree

1 file changed

+237
-0
lines changed

1 file changed

+237
-0
lines changed

src/test/java/org/apache/commons/lang3/builder/ReflectionDiffBuilderTest.java

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@
1717
package org.apache.commons.lang3.builder;
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
2021
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
import static org.junit.jupiter.api.Assertions.assertSame;
2123
import static org.junit.jupiter.api.Assertions.assertThrows;
2224

2325
import java.math.BigDecimal;
2426
import java.math.BigInteger;
27+
import java.util.List;
28+
import java.util.Objects;
29+
import java.util.concurrent.atomic.AtomicInteger;
2530

2631
import org.apache.commons.lang3.AbstractLangTest;
2732
import org.junit.jupiter.api.Test;
@@ -31,6 +36,121 @@
3136
*/
3237
class ReflectionDiffBuilderTest extends AbstractLangTest {
3338

39+
private static class AtomicIntegerWrapper {
40+
41+
private /* not final might not matter for the test. */ AtomicInteger value;
42+
43+
AtomicIntegerWrapper(final int a) {
44+
value = new AtomicInteger(a);
45+
}
46+
}
47+
48+
private static class FloatWrapper {
49+
50+
private /* not final might not matter for the test. */ float value;
51+
52+
FloatWrapper(final float a) {
53+
value = a;
54+
}
55+
}
56+
57+
private static class FloatWrapperEquals {
58+
59+
private /* not final might not matter for the test. */ float value;
60+
61+
FloatWrapperEquals(final float a) {
62+
value = a;
63+
}
64+
65+
@Override
66+
public boolean equals(final Object obj) {
67+
if (this == obj) {
68+
return true;
69+
}
70+
if (obj == null || getClass() != obj.getClass()) {
71+
return false;
72+
}
73+
final FloatWrapperEquals other = (FloatWrapperEquals) obj;
74+
return Float.floatToIntBits(value) == Float.floatToIntBits(other.value);
75+
}
76+
77+
@Override
78+
public int hashCode() {
79+
return Objects.hash(value);
80+
}
81+
}
82+
83+
private static class FloatWrapperWrapper {
84+
85+
private /* not final might not matter for the test. */ FloatWrapper value;
86+
87+
FloatWrapperWrapper(final float a) {
88+
value = new FloatWrapper(a);
89+
}
90+
}
91+
92+
private static class FloatWrapperWrapperDeepEquals {
93+
94+
private /* not final might not matter for the test. */ FloatWrapperEquals value;
95+
96+
FloatWrapperWrapperDeepEquals(final float a) {
97+
value = new FloatWrapperEquals(a);
98+
}
99+
100+
FloatWrapperWrapperDeepEquals(final FloatWrapperEquals a) {
101+
value = a;
102+
}
103+
104+
@Override
105+
public boolean equals(final Object obj) {
106+
if (this == obj) {
107+
return true;
108+
}
109+
if (obj == null || getClass() != obj.getClass()) {
110+
return false;
111+
}
112+
final FloatWrapperWrapperDeepEquals other = (FloatWrapperWrapperDeepEquals) obj;
113+
return Objects.equals(value, other.value);
114+
}
115+
116+
@Override
117+
public int hashCode() {
118+
return Objects.hash(value);
119+
}
120+
121+
}
122+
123+
private static class FloatWrapperWrapperEquals {
124+
125+
private /* not final might not matter for the test. */ FloatWrapper value;
126+
127+
FloatWrapperWrapperEquals(final float a) {
128+
value = new FloatWrapper(a);
129+
}
130+
131+
FloatWrapperWrapperEquals(final FloatWrapper a) {
132+
value = a;
133+
}
134+
135+
@Override
136+
public boolean equals(final Object obj) {
137+
if (this == obj) {
138+
return true;
139+
}
140+
if (obj == null || getClass() != obj.getClass()) {
141+
return false;
142+
}
143+
final FloatWrapperWrapperEquals other = (FloatWrapperWrapperEquals) obj;
144+
return Objects.equals(value, other.value);
145+
}
146+
147+
@Override
148+
public int hashCode() {
149+
return Objects.hash(value);
150+
}
151+
152+
}
153+
34154
@SuppressWarnings("unused")
35155
private static final class TypeTestChildClass extends TypeTestClass {
36156
String field = "a";
@@ -147,6 +267,123 @@ void testDifferenceInInherited_field() {
147267
assertEquals(1, list.getNumberOfDiffs());
148268
}
149269

270+
/*
271+
* See https://issues.apache.org/jira/browse/LANG-1708
272+
*/
273+
@Test
274+
void testGetDiffAtomicInteger() {
275+
final AtomicInteger a = new AtomicInteger(1);
276+
final AtomicInteger b = new AtomicInteger(1);
277+
assertEquals(0, new ReflectionDiffBuilder(a, b, ToStringStyle.JSON_STYLE).build().getDiffs().size());
278+
assertEquals(0, new ReflectionDiffBuilder(a, a, ToStringStyle.JSON_STYLE).build().getDiffs().size());
279+
assertEquals(1,
280+
((List<Diff<?>>) new ReflectionDiffBuilder(new AtomicInteger(1), new AtomicInteger(2), ToStringStyle.JSON_STYLE).build().getDiffs()).size());
281+
}
282+
283+
/*
284+
* See https://issues.apache.org/jira/browse/LANG-1708
285+
*/
286+
@Test
287+
void testGetDiffAtomicIntegerWrapper() {
288+
final AtomicIntegerWrapper a = new AtomicIntegerWrapper(1);
289+
final AtomicIntegerWrapper b = new AtomicIntegerWrapper(1);
290+
final List<Diff<?>> diffList = new ReflectionDiffBuilder(a, b, ToStringStyle.JSON_STYLE).build().getDiffs();
291+
assertEquals(1, diffList.size());
292+
final Diff<?> diff = diffList.get(0);
293+
assertFalse(diffList.isEmpty(), diff.toString());
294+
assertSame(a.value, diff.getKey());
295+
assertSame(b.value, diff.getValue());
296+
assertEquals(0, ((List<Diff<?>>) new ReflectionDiffBuilder(a, a, ToStringStyle.JSON_STYLE).build().getDiffs()).size());
297+
}
298+
299+
/*
300+
* See https://issues.apache.org/jira/browse/LANG-1708
301+
*/
302+
@Test
303+
void testGetDiffFloatWrapper() {
304+
final FloatWrapper a = new FloatWrapper(1f);
305+
final FloatWrapper b = new FloatWrapper(1f);
306+
assertEquals(0, ((List<Diff<?>>) new ReflectionDiffBuilder(a, b, ToStringStyle.JSON_STYLE).build().getDiffs()).size());
307+
assertEquals(0, ((List<Diff<?>>) new ReflectionDiffBuilder(a, a, ToStringStyle.JSON_STYLE).build().getDiffs()).size());
308+
assertEquals(1,
309+
((List<Diff<?>>) new ReflectionDiffBuilder(new FloatWrapper(1f), new FloatWrapper(2f), ToStringStyle.JSON_STYLE).build().getDiffs()).size());
310+
}
311+
312+
/*
313+
* See https://issues.apache.org/jira/browse/LANG-1708
314+
*/
315+
@Test
316+
void testGetDiffFloatWrapperDeepEquals() {
317+
final FloatWrapperWrapperDeepEquals a = new FloatWrapperWrapperDeepEquals(1f);
318+
final FloatWrapperWrapperDeepEquals b = new FloatWrapperWrapperDeepEquals(1f);
319+
assertEquals(0, ((List<Diff<?>>) new ReflectionDiffBuilder(a, b, ToStringStyle.JSON_STYLE).build().getDiffs()).size());
320+
assertEquals(0, ((List<Diff<?>>) new ReflectionDiffBuilder(a, a, ToStringStyle.JSON_STYLE).build().getDiffs()).size());
321+
assertEquals(1, ((List<Diff<?>>) new ReflectionDiffBuilder(new FloatWrapperWrapperDeepEquals(1f), new FloatWrapperWrapperDeepEquals(2f),
322+
ToStringStyle.JSON_STYLE).build().getDiffs()).size());
323+
final FloatWrapperEquals fw1 = new FloatWrapperEquals(1f);
324+
assertEquals(0, ((List<Diff<?>>) new ReflectionDiffBuilder(new FloatWrapperWrapperDeepEquals(fw1), new FloatWrapperWrapperDeepEquals(fw1),
325+
ToStringStyle.JSON_STYLE).build().getDiffs()).size());
326+
final FloatWrapperEquals fw2 = new FloatWrapperEquals(2f);
327+
assertEquals(1, ((List<Diff<?>>) new ReflectionDiffBuilder(new FloatWrapperWrapperDeepEquals(fw1), new FloatWrapperWrapperDeepEquals(fw2),
328+
ToStringStyle.JSON_STYLE).build().getDiffs()).size());
329+
}
330+
331+
/*
332+
* See https://issues.apache.org/jira/browse/LANG-1708
333+
*/
334+
@Test
335+
void testGetDiffFloatWrapperEquals() {
336+
final FloatWrapperEquals a = new FloatWrapperEquals(1f);
337+
final FloatWrapperEquals b = new FloatWrapperEquals(1f);
338+
assertEquals(0, ((List<Diff<?>>) new ReflectionDiffBuilder(a, b, ToStringStyle.JSON_STYLE).build().getDiffs()).size());
339+
assertEquals(0, ((List<Diff<?>>) new ReflectionDiffBuilder(a, a, ToStringStyle.JSON_STYLE).build().getDiffs()).size());
340+
assertEquals(1,
341+
((List<Diff<?>>) new ReflectionDiffBuilder(new FloatWrapperEquals(1f), new FloatWrapperEquals(2f), ToStringStyle.JSON_STYLE).build().getDiffs())
342+
.size());
343+
}
344+
345+
/*
346+
* See https://issues.apache.org/jira/browse/LANG-1708
347+
*/
348+
@Test
349+
void testGetDiffFloatWrapperWrapper() {
350+
final FloatWrapperWrapper a = new FloatWrapperWrapper(1f);
351+
final FloatWrapperWrapper b = new FloatWrapperWrapper(1f);
352+
final List<Diff<?>> diffList = new ReflectionDiffBuilder(a, b, ToStringStyle.JSON_STYLE).build().getDiffs();
353+
assertEquals(1, diffList.size());
354+
final Diff<?> diff = diffList.get(0);
355+
assertFalse(diffList.isEmpty(), diff.toString());
356+
assertSame(a.value, diff.getKey());
357+
assertSame(b.value, diff.getValue());
358+
assertEquals(0, ((List<Diff<?>>) new ReflectionDiffBuilder(a, a, ToStringStyle.JSON_STYLE).build().getDiffs()).size());
359+
assertEquals(1,
360+
((List<Diff<?>>) new ReflectionDiffBuilder(new FloatWrapperWrapper(1f), new FloatWrapperWrapper(2f), ToStringStyle.JSON_STYLE)
361+
.build().getDiffs()).size());
362+
}
363+
364+
/*
365+
* See https://issues.apache.org/jira/browse/LANG-1708
366+
*/
367+
@Test
368+
void testGetDiffFloatWrapperWrapperEquals() {
369+
final FloatWrapperWrapperEquals a = new FloatWrapperWrapperEquals(1f);
370+
final FloatWrapperWrapperEquals b = new FloatWrapperWrapperEquals(1f);
371+
final List<Diff<?>> diffList = new ReflectionDiffBuilder(a, b, ToStringStyle.JSON_STYLE).build().getDiffs();
372+
assertEquals(1, diffList.size());
373+
final Diff<?> diff = diffList.get(0);
374+
assertFalse(diffList.isEmpty(), diff.toString());
375+
assertSame(a.value, diff.getKey());
376+
assertSame(b.value, diff.getValue());
377+
assertEquals(0, ((List<Diff<?>>) new ReflectionDiffBuilder(a, a, ToStringStyle.JSON_STYLE).build().getDiffs()).size());
378+
assertEquals(1,
379+
((List<Diff<?>>) new ReflectionDiffBuilder(new FloatWrapperWrapperEquals(1f), new FloatWrapperWrapperEquals(2f), ToStringStyle.JSON_STYLE)
380+
.build().getDiffs()).size());
381+
final FloatWrapper fw1 = new FloatWrapper(1f);
382+
assertEquals(0,
383+
((List<Diff<?>>) new ReflectionDiffBuilder(new FloatWrapperWrapperEquals(fw1), new FloatWrapperWrapperEquals(fw1), ToStringStyle.JSON_STYLE)
384+
.build().getDiffs()).size());
385+
}
386+
150387
@Test
151388
void testGetExcludeFieldNamesEmpty() {
152389
final ReflectionDiffBuilder reflectionDiffBuilder = new ReflectionDiffBuilder(new TypeTestClass(), new TypeTestChildClass(), SHORT_STYLE);

0 commit comments

Comments
 (0)