28
28
* @author Rick Evans
29
29
* @author Stephane Nicoll
30
30
* @author Juergen Hoeller
31
+ * @author Sam Brannen
31
32
*/
32
33
public class OrderComparatorTests {
33
34
@@ -69,6 +70,40 @@ public void compareTwoNonOrderedInstancesEndsUpAsSame() {
69
70
assertThat (this .comparator .compare (new Object (), new Object ())).isEqualTo (0 );
70
71
}
71
72
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
+
72
107
@ Test
73
108
public void compareWithSimpleSourceProvider () {
74
109
Comparator <Object > customComparator = this .comparator .withSourceProvider (
@@ -86,7 +121,7 @@ public void compareWithSourceProviderArray() {
86
121
@ Test
87
122
public void compareWithSourceProviderArrayNoMatch () {
88
123
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 ()}));
90
125
assertThat (customComparator .compare (new Object (), 5L )).isEqualTo (0 );
91
126
}
92
127
@@ -98,33 +133,25 @@ public void compareWithSourceProviderEmpty() {
98
133
}
99
134
100
135
101
- private static final class TestSourceProvider implements OrderComparator .OrderSourceProvider {
102
-
103
- private final Object target ;
136
+ private static class StubOrdered implements Ordered {
104
137
105
- private final Object orderSource ;
138
+ private final int order ;
106
139
107
- public TestSourceProvider (Object target , Object orderSource ) {
108
- this .target = target ;
109
- this .orderSource = orderSource ;
140
+ StubOrdered (int order ) {
141
+ this .order = order ;
110
142
}
111
143
112
144
@ 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 ;
118
147
}
119
148
}
120
149
121
-
122
- private static final class StubOrdered implements Ordered {
150
+ private static class StubPriorityOrdered implements PriorityOrdered {
123
151
124
152
private final int order ;
125
153
126
-
127
- public StubOrdered (int order ) {
154
+ StubPriorityOrdered (int order ) {
128
155
this .order = order ;
129
156
}
130
157
@@ -134,4 +161,24 @@ public int getOrder() {
134
161
}
135
162
}
136
163
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
+
137
184
}
0 commit comments