Skip to content

Commit 6cc6abd

Browse files
committed
Test status quo for OrderComparator's PriorityOrdered support
See gh-23187
1 parent 4357749 commit 6cc6abd

File tree

1 file changed

+64
-17
lines changed

1 file changed

+64
-17
lines changed

spring-core/src/test/java/org/springframework/core/OrderComparatorTests.java

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* @author Rick Evans
2929
* @author Stephane Nicoll
3030
* @author Juergen Hoeller
31+
* @author Sam Brannen
3132
*/
3233
public class OrderComparatorTests {
3334

@@ -69,6 +70,40 @@ public void compareTwoNonOrderedInstancesEndsUpAsSame() {
6970
assertThat(this.comparator.compare(new Object(), new Object())).isEqualTo(0);
7071
}
7172

73+
@Test
74+
public void comparePriorityOrderedInstancesBefore() {
75+
assertThat(this.comparator.compare(new StubPriorityOrdered(100), new StubPriorityOrdered(2000))).isEqualTo(-1);
76+
}
77+
78+
@Test
79+
public void comparePriorityOrderedInstancesSame() {
80+
assertThat(this.comparator.compare(new StubPriorityOrdered(100), new StubPriorityOrdered(100))).isEqualTo(0);
81+
}
82+
83+
@Test
84+
public void comparePriorityOrderedInstancesAfter() {
85+
assertThat(this.comparator.compare(new StubPriorityOrdered(982300), new StubPriorityOrdered(100))).isEqualTo(1);
86+
}
87+
88+
@Test
89+
public void comparePriorityOrderedInstanceToStandardOrderedInstanceWithSamePriority() {
90+
// PriorityOrdered wins in a tie.
91+
assertThat(this.comparator.compare(new StubPriorityOrdered(100), new StubOrdered(100))).isEqualTo(-1);
92+
}
93+
94+
@Test
95+
public void comparePriorityOrderedInstanceToStandardOrderedInstanceWithLowerPriority() {
96+
// PriorityOrdered should not be taken into account.
97+
assertThat(this.comparator.compare(new StubPriorityOrdered(100), new StubOrdered(200))).isEqualTo(-1);
98+
}
99+
100+
@Test
101+
public void comparePriorityOrderedInstanceToStandardOrderedInstanceWithHigherPriority() {
102+
// PriorityOrdered should probably not be taken into account, but it currently is.
103+
// assertThat(this.comparator.compare(new StubPriorityOrdered(200), new StubOrdered(100))).isEqualTo(1);
104+
assertThat(this.comparator.compare(new StubPriorityOrdered(200), new StubOrdered(100))).isEqualTo(-1);
105+
}
106+
72107
@Test
73108
public void compareWithSimpleSourceProvider() {
74109
Comparator<Object> customComparator = this.comparator.withSourceProvider(
@@ -86,7 +121,7 @@ public void compareWithSourceProviderArray() {
86121
@Test
87122
public void compareWithSourceProviderArrayNoMatch() {
88123
Comparator<Object> customComparator = this.comparator.withSourceProvider(
89-
new TestSourceProvider(5L, new Object[]{new Object(), new Object()}));
124+
new TestSourceProvider(5L, new Object[] {new Object(), new Object()}));
90125
assertThat(customComparator.compare(new Object(), 5L)).isEqualTo(0);
91126
}
92127

@@ -98,33 +133,25 @@ public void compareWithSourceProviderEmpty() {
98133
}
99134

100135

101-
private static final class TestSourceProvider implements OrderComparator.OrderSourceProvider {
102-
103-
private final Object target;
136+
private static class StubOrdered implements Ordered {
104137

105-
private final Object orderSource;
138+
private final int order;
106139

107-
public TestSourceProvider(Object target, Object orderSource) {
108-
this.target = target;
109-
this.orderSource = orderSource;
140+
StubOrdered(int order) {
141+
this.order = order;
110142
}
111143

112144
@Override
113-
public Object getOrderSource(Object obj) {
114-
if (target.equals(obj)) {
115-
return orderSource;
116-
}
117-
return null;
145+
public int getOrder() {
146+
return this.order;
118147
}
119148
}
120149

121-
122-
private static final class StubOrdered implements Ordered {
150+
private static class StubPriorityOrdered implements PriorityOrdered {
123151

124152
private final int order;
125153

126-
127-
public StubOrdered(int order) {
154+
StubPriorityOrdered(int order) {
128155
this.order = order;
129156
}
130157

@@ -134,4 +161,24 @@ public int getOrder() {
134161
}
135162
}
136163

164+
private static class TestSourceProvider implements OrderComparator.OrderSourceProvider {
165+
166+
private final Object target;
167+
168+
private final Object orderSource;
169+
170+
TestSourceProvider(Object target, Object orderSource) {
171+
this.target = target;
172+
this.orderSource = orderSource;
173+
}
174+
175+
@Override
176+
public Object getOrderSource(Object obj) {
177+
if (target.equals(obj)) {
178+
return orderSource;
179+
}
180+
return null;
181+
}
182+
}
183+
137184
}

0 commit comments

Comments
 (0)