|
|
|
|
@ -28,6 +28,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@@ -28,6 +28,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
* @author Rick Evans |
|
|
|
|
* @author Stephane Nicoll |
|
|
|
|
* @author Juergen Hoeller |
|
|
|
|
* @author Sam Brannen |
|
|
|
|
*/ |
|
|
|
|
public class OrderComparatorTests { |
|
|
|
|
|
|
|
|
|
@ -69,6 +70,40 @@ public class OrderComparatorTests {
@@ -69,6 +70,40 @@ public class OrderComparatorTests {
|
|
|
|
|
assertThat(this.comparator.compare(new Object(), new Object())).isEqualTo(0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void comparePriorityOrderedInstancesBefore() { |
|
|
|
|
assertThat(this.comparator.compare(new StubPriorityOrdered(100), new StubPriorityOrdered(2000))).isEqualTo(-1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void comparePriorityOrderedInstancesSame() { |
|
|
|
|
assertThat(this.comparator.compare(new StubPriorityOrdered(100), new StubPriorityOrdered(100))).isEqualTo(0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void comparePriorityOrderedInstancesAfter() { |
|
|
|
|
assertThat(this.comparator.compare(new StubPriorityOrdered(982300), new StubPriorityOrdered(100))).isEqualTo(1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void comparePriorityOrderedInstanceToStandardOrderedInstanceWithSamePriority() { |
|
|
|
|
// PriorityOrdered wins in a tie.
|
|
|
|
|
assertThat(this.comparator.compare(new StubPriorityOrdered(100), new StubOrdered(100))).isEqualTo(-1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void comparePriorityOrderedInstanceToStandardOrderedInstanceWithLowerPriority() { |
|
|
|
|
// PriorityOrdered should not be taken into account.
|
|
|
|
|
assertThat(this.comparator.compare(new StubPriorityOrdered(100), new StubOrdered(200))).isEqualTo(-1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void comparePriorityOrderedInstanceToStandardOrderedInstanceWithHigherPriority() { |
|
|
|
|
// PriorityOrdered should probably not be taken into account, but it currently is.
|
|
|
|
|
// assertThat(this.comparator.compare(new StubPriorityOrdered(200), new StubOrdered(100))).isEqualTo(1);
|
|
|
|
|
assertThat(this.comparator.compare(new StubPriorityOrdered(200), new StubOrdered(100))).isEqualTo(-1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void compareWithSimpleSourceProvider() { |
|
|
|
|
Comparator<Object> customComparator = this.comparator.withSourceProvider( |
|
|
|
|
@ -86,7 +121,7 @@ public class OrderComparatorTests {
@@ -86,7 +121,7 @@ public class OrderComparatorTests {
|
|
|
|
|
@Test |
|
|
|
|
public void compareWithSourceProviderArrayNoMatch() { |
|
|
|
|
Comparator<Object> customComparator = this.comparator.withSourceProvider( |
|
|
|
|
new TestSourceProvider(5L, new Object[]{new Object(), new Object()})); |
|
|
|
|
new TestSourceProvider(5L, new Object[] {new Object(), new Object()})); |
|
|
|
|
assertThat(customComparator.compare(new Object(), 5L)).isEqualTo(0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -98,33 +133,25 @@ public class OrderComparatorTests {
@@ -98,33 +133,25 @@ public class OrderComparatorTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final class TestSourceProvider implements OrderComparator.OrderSourceProvider { |
|
|
|
|
|
|
|
|
|
private final Object target; |
|
|
|
|
private static class StubOrdered implements Ordered { |
|
|
|
|
|
|
|
|
|
private final Object orderSource; |
|
|
|
|
private final int order; |
|
|
|
|
|
|
|
|
|
public TestSourceProvider(Object target, Object orderSource) { |
|
|
|
|
this.target = target; |
|
|
|
|
this.orderSource = orderSource; |
|
|
|
|
StubOrdered(int order) { |
|
|
|
|
this.order = order; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Object getOrderSource(Object obj) { |
|
|
|
|
if (target.equals(obj)) { |
|
|
|
|
return orderSource; |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
public int getOrder() { |
|
|
|
|
return this.order; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final class StubOrdered implements Ordered { |
|
|
|
|
private static class StubPriorityOrdered implements PriorityOrdered { |
|
|
|
|
|
|
|
|
|
private final int order; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public StubOrdered(int order) { |
|
|
|
|
StubPriorityOrdered(int order) { |
|
|
|
|
this.order = order; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -134,4 +161,24 @@ public class OrderComparatorTests {
@@ -134,4 +161,24 @@ public class OrderComparatorTests {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static class TestSourceProvider implements OrderComparator.OrderSourceProvider { |
|
|
|
|
|
|
|
|
|
private final Object target; |
|
|
|
|
|
|
|
|
|
private final Object orderSource; |
|
|
|
|
|
|
|
|
|
TestSourceProvider(Object target, Object orderSource) { |
|
|
|
|
this.target = target; |
|
|
|
|
this.orderSource = orderSource; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Object getOrderSource(Object obj) { |
|
|
|
|
if (target.equals(obj)) { |
|
|
|
|
return orderSource; |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|