(AnnotationAware)OrderComparator supports null values again

Issue: SPR-15823
This commit is contained in:
Juergen Hoeller
2017-07-27 11:39:05 +02:00
parent 25e6a2da64
commit c5fc400534
3 changed files with 52 additions and 17 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,34 +20,48 @@ import java.util.Comparator;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
/**
* Unit tests for the {@link OrderComparator} class.
*
* @author Rick Evans
* @author Stephane Nicoll
* @author Juergen Hoeller
*/
public class OrderComparatorTests {
private final OrderComparator comparator = new OrderComparator();
@Test
public void compareOrderedInstancesBefore() {
assertEquals(-1, this.comparator.compare(
new StubOrdered(100), new StubOrdered(2000)));
assertEquals(-1, this.comparator.compare(new StubOrdered(100), new StubOrdered(2000)));
}
@Test
public void compareOrderedInstancesSame() {
assertEquals(0, this.comparator.compare(
new StubOrdered(100), new StubOrdered(100)));
assertEquals(0, this.comparator.compare(new StubOrdered(100), new StubOrdered(100)));
}
@Test
public void compareOrderedInstancesAfter() {
assertEquals(1, this.comparator.compare(
new StubOrdered(982300), new StubOrdered(100)));
assertEquals(1, this.comparator.compare(new StubOrdered(982300), new StubOrdered(100)));
}
@Test
public void compareOrderedInstancesNullFirst() {
assertEquals(1, this.comparator.compare(null, new StubOrdered(100)));
}
@Test
public void compareOrderedInstancesNullLast() {
assertEquals(-1, this.comparator.compare(new StubOrdered(100), null));
}
@Test
public void compareOrderedInstancesDoubleNull() {
assertEquals(0, this.comparator.compare(null, null));
}
@Test
@@ -87,6 +101,7 @@ public class OrderComparatorTests {
private static final class TestSourceProvider implements OrderComparator.OrderSourceProvider {
private final Object target;
private final Object orderSource;
public TestSourceProvider(Object target, Object orderSource) {
@@ -103,6 +118,7 @@ public class OrderComparatorTests {
}
}
private static final class StubOrdered implements Ordered {
private final int order;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -96,6 +96,20 @@ public class AnnotationAwareOrderComparatorTests {
assertEquals(B.class, list.get(1));
}
@Test
public void sortWithNulls() {
List<Object> list = new ArrayList<>();
list.add(null);
list.add(B.class);
list.add(null);
list.add(A.class);
AnnotationAwareOrderComparator.sort(list);
assertEquals(A.class, list.get(0));
assertEquals(B.class, list.get(1));
assertNull(list.get(2));
assertNull(list.get(3));
}
@Order(1)
private static class A {