Skip to content

Commit 9543384

Browse files
committed
Avoid deprecated comparators in tests
Issue: SPR-14779
1 parent f90cd77 commit 9543384

File tree

3 files changed

+27
-35
lines changed

3 files changed

+27
-35
lines changed

spring-beans/src/test/java/org/springframework/beans/support/PropertyComparatorTests.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,16 +16,14 @@
1616

1717
package org.springframework.beans.support;
1818

19-
import org.junit.Test;
19+
import java.util.Comparator;
2020

21-
import org.springframework.util.comparator.CompoundComparator;
21+
import org.junit.Test;
2222

2323
import static org.junit.Assert.*;
2424

2525
/**
26-
* Unit tests for {@link PropertyComparator}
27-
*
28-
* @see org.springframework.util.comparator.ComparatorTests
26+
* Unit tests for {@link PropertyComparator}.
2927
*
3028
* @author Keith Donald
3129
* @author Chris Beams
@@ -40,7 +38,7 @@ public void testPropertyComparator() {
4038
Dog dog2 = new Dog();
4139
dog2.setNickName("biscy");
4240

43-
PropertyComparator c = new PropertyComparator("nickName", false, true);
41+
PropertyComparator<Dog> c = new PropertyComparator<>("nickName", false, true);
4442
assertTrue(c.compare(dog, dog2) > 0);
4543
assertTrue(c.compare(dog, dog) == 0);
4644
assertTrue(c.compare(dog2, dog) < 0);
@@ -50,15 +48,13 @@ public void testPropertyComparator() {
5048
public void testPropertyComparatorNulls() {
5149
Dog dog = new Dog();
5250
Dog dog2 = new Dog();
53-
PropertyComparator c = new PropertyComparator("nickName", false, true);
51+
PropertyComparator<Dog> c = new PropertyComparator<>("nickName", false, true);
5452
assertTrue(c.compare(dog, dog2) == 0);
5553
}
5654

57-
@SuppressWarnings("unchecked")
5855
@Test
5956
public void testCompoundComparator() {
60-
CompoundComparator<Dog> c = new CompoundComparator<>();
61-
c.addComparator(new PropertyComparator("lastName", false, true));
57+
Comparator<Dog> c = new PropertyComparator<>("lastName", false, true);
6258

6359
Dog dog1 = new Dog();
6460
dog1.setFirstName("macy");
@@ -70,19 +66,17 @@ public void testCompoundComparator() {
7066

7167
assertTrue(c.compare(dog1, dog2) == 0);
7268

73-
c.addComparator(new PropertyComparator("firstName", false, true));
69+
c = c.thenComparing(new PropertyComparator<>("firstName", false, true));
7470
assertTrue(c.compare(dog1, dog2) > 0);
7571

7672
dog2.setLastName("konikk dog");
7773
assertTrue(c.compare(dog2, dog1) > 0);
7874
}
7975

80-
@SuppressWarnings("unchecked")
8176
@Test
8277
public void testCompoundComparatorInvert() {
83-
CompoundComparator<Dog> c = new CompoundComparator<>();
84-
c.addComparator(new PropertyComparator("lastName", false, true));
85-
c.addComparator(new PropertyComparator("firstName", false, true));
78+
Comparator<Dog> c = (new PropertyComparator<Dog>("lastName", false, true)).
79+
thenComparing(new PropertyComparator<>("firstName", false, true));
8680
Dog dog1 = new Dog();
8781
dog1.setFirstName("macy");
8882
dog1.setLastName("grayspots");
@@ -92,7 +86,7 @@ public void testCompoundComparatorInvert() {
9286
dog2.setLastName("grayspots");
9387

9488
assertTrue(c.compare(dog1, dog2) > 0);
95-
c.invertOrder();
89+
c = c.reversed();
9690
assertTrue(c.compare(dog1, dog2) < 0);
9791
}
9892

@@ -106,11 +100,6 @@ private static class Dog implements Comparable<Object> {
106100

107101
private String lastName;
108102

109-
@Override
110-
public int compareTo(Object o) {
111-
return nickName.compareTo(((Dog)o).nickName);
112-
}
113-
114103
public String getNickName() {
115104
return nickName;
116105
}
@@ -134,6 +123,11 @@ public String getLastName() {
134123
public void setLastName(String lastName) {
135124
this.lastName = lastName;
136125
}
126+
127+
@Override
128+
public int compareTo(Object o) {
129+
return this.nickName.compareTo(((Dog) o).nickName);
130+
}
137131
}
138132

139133
}

spring-core/src/test/java/org/springframework/util/comparator/CompoundComparatorTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,12 +23,13 @@
2323
import org.junit.rules.ExpectedException;
2424

2525
/**
26-
* Test for {@link ComparableComparator}.
26+
* Test for {@link CompoundComparator}.
2727
*
2828
* @author Keith Donald
2929
* @author Chris Beams
3030
* @author Phillip Webb
3131
*/
32+
@Deprecated
3233
public class CompoundComparatorTests {
3334

3435
@Rule

spring-core/src/test/java/org/springframework/util/comparator/InvertibleComparatorTests.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,10 +30,11 @@
3030
* @author Chris Beams
3131
* @author Phillip Webb
3232
*/
33-
33+
@Deprecated
3434
public class InvertibleComparatorTests {
3535

36-
private Comparator<Integer> comparator = new ComparableComparator<>();
36+
private final Comparator<Integer> comparator = new ComparableComparator<>();
37+
3738

3839
@Test(expected = IllegalArgumentException.class)
3940
public void shouldNeedComparator() throws Exception {
@@ -47,16 +48,14 @@ public void shouldNeedComparatorWithAscending() throws Exception {
4748

4849
@Test
4950
public void shouldDefaultToAscending() throws Exception {
50-
InvertibleComparator<Integer> invertibleComparator =
51-
new InvertibleComparator<>(comparator);
51+
InvertibleComparator<Integer> invertibleComparator = new InvertibleComparator<>(comparator);
5252
assertThat(invertibleComparator.isAscending(), is(true));
5353
assertThat(invertibleComparator.compare(1, 2), is(-1));
5454
}
5555

5656
@Test
5757
public void shouldInvert() throws Exception {
58-
InvertibleComparator<Integer> invertibleComparator =
59-
new InvertibleComparator<>(comparator);
58+
InvertibleComparator<Integer> invertibleComparator = new InvertibleComparator<>(comparator);
6059
assertThat(invertibleComparator.isAscending(), is(true));
6160
assertThat(invertibleComparator.compare(1, 2), is(-1));
6261
invertibleComparator.invertOrder();
@@ -66,15 +65,13 @@ public void shouldInvert() throws Exception {
6665

6766
@Test
6867
public void shouldCompareAscending() throws Exception {
69-
InvertibleComparator<Integer> invertibleComparator =
70-
new InvertibleComparator<>(comparator, true);
68+
InvertibleComparator<Integer> invertibleComparator = new InvertibleComparator<>(comparator, true);
7169
assertThat(invertibleComparator.compare(1, 2), is(-1));
7270
}
7371

7472
@Test
7573
public void shouldCompareDescending() throws Exception {
76-
InvertibleComparator<Integer> invertibleComparator =
77-
new InvertibleComparator<>(comparator, false);
74+
InvertibleComparator<Integer> invertibleComparator = new InvertibleComparator<>(comparator, false);
7875
assertThat(invertibleComparator.compare(1, 2), is(1));
7976
}
8077

0 commit comments

Comments
 (0)