Avoid deprecated comparators in tests

Issue: SPR-14779
This commit is contained in:
Juergen Hoeller 2017-02-16 15:36:27 +01:00
parent f90cd7705f
commit 9543384d9e
3 changed files with 27 additions and 35 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,16 +16,14 @@
package org.springframework.beans.support; package org.springframework.beans.support;
import org.junit.Test; import java.util.Comparator;
import org.springframework.util.comparator.CompoundComparator; import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/** /**
* Unit tests for {@link PropertyComparator} * Unit tests for {@link PropertyComparator}.
*
* @see org.springframework.util.comparator.ComparatorTests
* *
* @author Keith Donald * @author Keith Donald
* @author Chris Beams * @author Chris Beams
@ -40,7 +38,7 @@ public class PropertyComparatorTests {
Dog dog2 = new Dog(); Dog dog2 = new Dog();
dog2.setNickName("biscy"); dog2.setNickName("biscy");
PropertyComparator c = new PropertyComparator("nickName", false, true); PropertyComparator<Dog> c = new PropertyComparator<>("nickName", false, true);
assertTrue(c.compare(dog, dog2) > 0); assertTrue(c.compare(dog, dog2) > 0);
assertTrue(c.compare(dog, dog) == 0); assertTrue(c.compare(dog, dog) == 0);
assertTrue(c.compare(dog2, dog) < 0); assertTrue(c.compare(dog2, dog) < 0);
@ -50,15 +48,13 @@ public class PropertyComparatorTests {
public void testPropertyComparatorNulls() { public void testPropertyComparatorNulls() {
Dog dog = new Dog(); Dog dog = new Dog();
Dog dog2 = new Dog(); Dog dog2 = new Dog();
PropertyComparator c = new PropertyComparator("nickName", false, true); PropertyComparator<Dog> c = new PropertyComparator<>("nickName", false, true);
assertTrue(c.compare(dog, dog2) == 0); assertTrue(c.compare(dog, dog2) == 0);
} }
@SuppressWarnings("unchecked")
@Test @Test
public void testCompoundComparator() { public void testCompoundComparator() {
CompoundComparator<Dog> c = new CompoundComparator<>(); Comparator<Dog> c = new PropertyComparator<>("lastName", false, true);
c.addComparator(new PropertyComparator("lastName", false, true));
Dog dog1 = new Dog(); Dog dog1 = new Dog();
dog1.setFirstName("macy"); dog1.setFirstName("macy");
@ -70,19 +66,17 @@ public class PropertyComparatorTests {
assertTrue(c.compare(dog1, dog2) == 0); assertTrue(c.compare(dog1, dog2) == 0);
c.addComparator(new PropertyComparator("firstName", false, true)); c = c.thenComparing(new PropertyComparator<>("firstName", false, true));
assertTrue(c.compare(dog1, dog2) > 0); assertTrue(c.compare(dog1, dog2) > 0);
dog2.setLastName("konikk dog"); dog2.setLastName("konikk dog");
assertTrue(c.compare(dog2, dog1) > 0); assertTrue(c.compare(dog2, dog1) > 0);
} }
@SuppressWarnings("unchecked")
@Test @Test
public void testCompoundComparatorInvert() { public void testCompoundComparatorInvert() {
CompoundComparator<Dog> c = new CompoundComparator<>(); Comparator<Dog> c = (new PropertyComparator<Dog>("lastName", false, true)).
c.addComparator(new PropertyComparator("lastName", false, true)); thenComparing(new PropertyComparator<>("firstName", false, true));
c.addComparator(new PropertyComparator("firstName", false, true));
Dog dog1 = new Dog(); Dog dog1 = new Dog();
dog1.setFirstName("macy"); dog1.setFirstName("macy");
dog1.setLastName("grayspots"); dog1.setLastName("grayspots");
@ -92,7 +86,7 @@ public class PropertyComparatorTests {
dog2.setLastName("grayspots"); dog2.setLastName("grayspots");
assertTrue(c.compare(dog1, dog2) > 0); assertTrue(c.compare(dog1, dog2) > 0);
c.invertOrder(); c = c.reversed();
assertTrue(c.compare(dog1, dog2) < 0); assertTrue(c.compare(dog1, dog2) < 0);
} }
@ -106,11 +100,6 @@ public class PropertyComparatorTests {
private String lastName; private String lastName;
@Override
public int compareTo(Object o) {
return nickName.compareTo(((Dog)o).nickName);
}
public String getNickName() { public String getNickName() {
return nickName; return nickName;
} }
@ -134,6 +123,11 @@ public class PropertyComparatorTests {
public void setLastName(String lastName) { public void setLastName(String lastName) {
this.lastName = lastName; this.lastName = lastName;
} }
@Override
public int compareTo(Object o) {
return this.nickName.compareTo(((Dog) o).nickName);
}
} }
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,12 +23,13 @@ import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
/** /**
* Test for {@link ComparableComparator}. * Test for {@link CompoundComparator}.
* *
* @author Keith Donald * @author Keith Donald
* @author Chris Beams * @author Chris Beams
* @author Phillip Webb * @author Phillip Webb
*/ */
@Deprecated
public class CompoundComparatorTests { public class CompoundComparatorTests {
@Rule @Rule

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -30,10 +30,11 @@ import static org.junit.Assert.*;
* @author Chris Beams * @author Chris Beams
* @author Phillip Webb * @author Phillip Webb
*/ */
@Deprecated
public class InvertibleComparatorTests { public class InvertibleComparatorTests {
private Comparator<Integer> comparator = new ComparableComparator<>(); private final Comparator<Integer> comparator = new ComparableComparator<>();
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void shouldNeedComparator() throws Exception { public void shouldNeedComparator() throws Exception {
@ -47,16 +48,14 @@ public class InvertibleComparatorTests {
@Test @Test
public void shouldDefaultToAscending() throws Exception { public void shouldDefaultToAscending() throws Exception {
InvertibleComparator<Integer> invertibleComparator = InvertibleComparator<Integer> invertibleComparator = new InvertibleComparator<>(comparator);
new InvertibleComparator<>(comparator);
assertThat(invertibleComparator.isAscending(), is(true)); assertThat(invertibleComparator.isAscending(), is(true));
assertThat(invertibleComparator.compare(1, 2), is(-1)); assertThat(invertibleComparator.compare(1, 2), is(-1));
} }
@Test @Test
public void shouldInvert() throws Exception { public void shouldInvert() throws Exception {
InvertibleComparator<Integer> invertibleComparator = InvertibleComparator<Integer> invertibleComparator = new InvertibleComparator<>(comparator);
new InvertibleComparator<>(comparator);
assertThat(invertibleComparator.isAscending(), is(true)); assertThat(invertibleComparator.isAscending(), is(true));
assertThat(invertibleComparator.compare(1, 2), is(-1)); assertThat(invertibleComparator.compare(1, 2), is(-1));
invertibleComparator.invertOrder(); invertibleComparator.invertOrder();
@ -66,15 +65,13 @@ public class InvertibleComparatorTests {
@Test @Test
public void shouldCompareAscending() throws Exception { public void shouldCompareAscending() throws Exception {
InvertibleComparator<Integer> invertibleComparator = InvertibleComparator<Integer> invertibleComparator = new InvertibleComparator<>(comparator, true);
new InvertibleComparator<>(comparator, true);
assertThat(invertibleComparator.compare(1, 2), is(-1)); assertThat(invertibleComparator.compare(1, 2), is(-1));
} }
@Test @Test
public void shouldCompareDescending() throws Exception { public void shouldCompareDescending() throws Exception {
InvertibleComparator<Integer> invertibleComparator = InvertibleComparator<Integer> invertibleComparator = new InvertibleComparator<>(comparator, false);
new InvertibleComparator<>(comparator, false);
assertThat(invertibleComparator.compare(1, 2), is(1)); assertThat(invertibleComparator.compare(1, 2), is(1));
} }