diff --git a/spring-core/src/main/java/org/springframework/core/OrderComparator.java b/spring-core/src/main/java/org/springframework/core/OrderComparator.java index 57d3d0d67e..d2852bd9d0 100644 --- a/spring-core/src/main/java/org/springframework/core/OrderComparator.java +++ b/spring-core/src/main/java/org/springframework/core/OrderComparator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 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. @@ -65,11 +65,11 @@ public class OrderComparator implements Comparator { } @Override - public int compare(Object o1, Object o2) { + public int compare(@Nullable Object o1, @Nullable Object o2) { return doCompare(o1, o2, null); } - private int doCompare(Object o1, Object o2, @Nullable OrderSourceProvider sourceProvider) { + private int doCompare(@Nullable Object o1, @Nullable Object o2, @Nullable OrderSourceProvider sourceProvider) { boolean p1 = (o1 instanceof PriorityOrdered); boolean p2 = (o2 instanceof PriorityOrdered); if (p1 && !p2) { @@ -92,9 +92,9 @@ public class OrderComparator implements Comparator { * @param obj the object to check * @return the order value, or {@code Ordered.LOWEST_PRECEDENCE} as fallback */ - private int getOrder(Object obj, @Nullable OrderSourceProvider sourceProvider) { + private int getOrder(@Nullable Object obj, @Nullable OrderSourceProvider sourceProvider) { Integer order = null; - if (sourceProvider != null) { + if (obj != null && sourceProvider != null) { Object orderSource = sourceProvider.getOrderSource(obj); if (orderSource != null) { if (orderSource.getClass().isArray()) { @@ -121,9 +121,14 @@ public class OrderComparator implements Comparator { * @param obj the object to check * @return the order value, or {@code Ordered.LOWEST_PRECEDENCE} as fallback */ - protected int getOrder(Object obj) { - Integer order = findOrder(obj); - return (order != null ? order : Ordered.LOWEST_PRECEDENCE); + protected int getOrder(@Nullable Object obj) { + if (obj != null) { + Integer order = findOrder(obj); + if (order != null) { + return order; + } + } + return Ordered.LOWEST_PRECEDENCE; } /** diff --git a/spring-core/src/test/java/org/springframework/core/OrderComparatorTests.java b/spring-core/src/test/java/org/springframework/core/OrderComparatorTests.java index 0f09f958d5..808d04b004 100644 --- a/spring-core/src/test/java/org/springframework/core/OrderComparatorTests.java +++ b/spring-core/src/test/java/org/springframework/core/OrderComparatorTests.java @@ -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; diff --git a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationAwareOrderComparatorTests.java b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationAwareOrderComparatorTests.java index 44b008c1a8..4a4fe29f13 100644 --- a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationAwareOrderComparatorTests.java +++ b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationAwareOrderComparatorTests.java @@ -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 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 {