Refactor ComparatorTests into separate classes

This commit is contained in:
Phillip Webb 2012-08-27 13:20:24 -07:00 committed by Chris Beams
parent 9821868707
commit 719a9e120d
7 changed files with 395 additions and 135 deletions

View File

@ -0,0 +1,63 @@
/*
* Copyright 2002-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.util.comparator;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
import java.util.Comparator;
import org.junit.Test;
/**
* Tests for {@link BooleanComparator}.
*
* @author Keith Donald
* @author Chris Beams
* @author Phillip Webb
*/
public class BooleanComparatorTests {
@Test
public void shouldCompareWithTrueLow() {
Comparator<Boolean> c = new BooleanComparator(true);
assertThat(c.compare(new Boolean(true), new Boolean(false)), is(-1));
assertThat(c.compare(Boolean.TRUE, Boolean.TRUE), is(0));
}
@Test
public void shouldCompareWithTrueHigh() {
Comparator<Boolean> c = new BooleanComparator(false);
assertThat(c.compare(new Boolean(true), new Boolean(false)), is(1));
assertThat(c.compare(Boolean.TRUE, Boolean.TRUE), is(0));
}
@Test
public void shouldCompareFromTrueLow() {
Comparator<Boolean> c = BooleanComparator.TRUE_LOW;
assertThat(c.compare(true, false), is(-1));
assertThat(c.compare(Boolean.TRUE, Boolean.TRUE), is(0));
}
@Test
public void shouldCompareFromTrueHigh() {
Comparator<Boolean> c = BooleanComparator.TRUE_HIGH;
assertThat(c.compare(true, false), is(1));
assertThat(c.compare(Boolean.TRUE, Boolean.TRUE), is(0));
}
}

View File

@ -0,0 +1,57 @@
/*
* Copyright 2002-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.util.comparator;
import static org.junit.Assert.assertTrue;
import java.util.Comparator;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/**
* Tests for {@link ComparableComparator}.
*
* @author Keith Donald
* @author Chris Beams
* @author Phillip Webb
*/
public class ComparableComparatorTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void testComparableComparator() {
Comparator<String> c = new ComparableComparator<String>();
String s1 = "abc";
String s2 = "cde";
assertTrue(c.compare(s1, s2) < 0);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void shouldNeedComparable() {
Comparator c = new ComparableComparator();
Object o1 = new Object();
Object o2 = new Object();
thrown.expect(ClassCastException.class);
c.compare(o1, o2);
}
}

View File

@ -1,135 +0,0 @@
/*
* Copyright 2002-2005 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.util.comparator;
import static org.junit.Assert.*;
import java.util.Comparator;
import org.junit.Test;
/**
* Unit tests for {@link PropertyComparator}
*
* @see org.springframework.beans.support.PropertyComparatorTests
*
* @author Keith Donald
* @author Chris Beams
*/
public class ComparatorTests {
@Test
public void testComparableComparator() {
Comparator<String> c = new ComparableComparator<String>();
String s1 = "abc";
String s2 = "cde";
assertTrue(c.compare(s1, s2) < 0);
}
@SuppressWarnings("unchecked")
@Test
public void testComparableComparatorIllegalArgs() {
Comparator c = new ComparableComparator();
Object o1 = new Object();
Object o2 = new Object();
try {
c.compare(o1, o2);
}
catch (ClassCastException e) {
return;
}
fail("Comparator should have thrown a cce");
}
@Test
public void testBooleanComparatorTrueLow() {
Comparator<Boolean> c = BooleanComparator.TRUE_LOW;
assertTrue(c.compare(new Boolean(true), new Boolean(false)) < 0);
}
@Test
public void testBooleanComparatorTrueHigh() {
Comparator<Boolean> c = BooleanComparator.TRUE_HIGH;
assertTrue(c.compare(new Boolean(true), new Boolean(false)) > 0);
assertTrue(c.compare(Boolean.TRUE, Boolean.TRUE) == 0);
}
@SuppressWarnings("unchecked")
@Test
public void testNullSafeComparatorNullsLow() {
Comparator<String> c = NullSafeComparator.NULLS_LOW;
assertTrue(c.compare(null, "boo") < 0);
}
@SuppressWarnings("unchecked")
@Test
public void testNullSafeComparatorNullsHigh() {
Comparator<String> c = NullSafeComparator.NULLS_HIGH;
assertTrue(c.compare(null, "boo") > 0);
assertTrue(c.compare(null, null) == 0);
}
@Test
public void testCompoundComparatorEmpty() {
Comparator<String> c = new CompoundComparator<String>();
try {
c.compare("foo", "bar");
}
catch (IllegalStateException e) {
return;
}
fail("illegal state should've been thrown on empty list");
}
private static class Dog implements Comparable<Object> {
private String nickName;
private String firstName;
private String lastName;
public int compareTo(Object o) {
return nickName.compareTo(((Dog)o).nickName);
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
}

View File

@ -0,0 +1,44 @@
/*
* Copyright 2002-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.util.comparator;
import java.util.Comparator;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/**
* Test for {@link ComparableComparator}.
*
* @author Keith Donald
* @author Chris Beams
* @author Phillip Webb
*/
public class CompoundComparatorTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void shouldNeedAtLeastOneComparator() {
Comparator<String> c = new CompoundComparator<String>();
thrown.expect(IllegalStateException.class);
c.compare("foo", "bar");
}
}

View File

@ -0,0 +1,92 @@
/*
* Copyright 2002-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.util.comparator;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
import java.util.Comparator;
import org.junit.Test;
/**
* Tests for {@link InstanceComparator}.
*
* @author Phillip Webb
*/
public class InstanceComparatorTests {
private C1 c1 = new C1();
private C2 c2 = new C2();
private C3 c3 = new C3();
private C4 c4 = new C4();
@Test
public void shouldCompareClasses() throws Exception {
Comparator<Object> comparator = new InstanceComparator<Object>(C1.class, C2.class);
assertThat(comparator.compare(c1, c1), is(0));
assertThat(comparator.compare(c1, c2), is(-1));
assertThat(comparator.compare(c2, c1), is(1));
assertThat(comparator.compare(c2, c3), is(-1));
assertThat(comparator.compare(c2, c4), is(-1));
assertThat(comparator.compare(c3, c4), is(0));
}
@Test
public void shouldCompareInterfaces() throws Exception {
Comparator<Object> comparator = new InstanceComparator<Object>(I1.class, I2.class);
assertThat(comparator.compare(c1, c1), is(0));
assertThat(comparator.compare(c1, c2), is(0));
assertThat(comparator.compare(c2, c1), is(0));
assertThat(comparator.compare(c1, c3), is(-1));
assertThat(comparator.compare(c3, c1), is(1));
assertThat(comparator.compare(c3, c4), is(0));
}
@Test
public void shouldCompareMix() throws Exception {
Comparator<Object> comparator = new InstanceComparator<Object>(I1.class, C3.class);
assertThat(comparator.compare(c1, c1), is(0));
assertThat(comparator.compare(c3, c4), is(-1));
assertThat(comparator.compare(c3, null), is(-1));
assertThat(comparator.compare(c4, null), is(0));
}
private static interface I1 {
}
private static interface I2 {
}
private static class C1 implements I1 {
}
private static class C2 implements I1 {
}
private static class C3 implements I2 {
}
private static class C4 implements I2 {
}
}

View File

@ -0,0 +1,90 @@
/*
* Copyright 2002-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.util.comparator;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.util.Comparator;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/**
* Tests for {@link InvertibleComparator}.
*
* @author Keith Donald
* @author Chris Beams
* @author Phillip Webb
*/
public class InvertibleComparatorTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
private Comparator<Integer> comparator = new ComparableComparator<Integer>();
@Test
public void shouldNeedComparator() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Comparator must not be null");
new InvertibleComparator<Object>(null);
}
@Test
public void shouldNeedComparatorWithAscending() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Comparator must not be null");
new InvertibleComparator<Object>(null, true);
}
@Test
public void shouldDefaultToAscending() throws Exception {
InvertibleComparator<Integer> invertibleComparator =
new InvertibleComparator<Integer>(comparator);
assertThat(invertibleComparator.isAscending(), is(true));
assertThat(invertibleComparator.compare(1, 2), is(-1));
}
@Test
public void shouldInvert() throws Exception {
InvertibleComparator<Integer> invertibleComparator =
new InvertibleComparator<Integer>(comparator);
assertThat(invertibleComparator.isAscending(), is(true));
assertThat(invertibleComparator.compare(1, 2), is(-1));
invertibleComparator.invertOrder();
assertThat(invertibleComparator.isAscending(), is(false));
assertThat(invertibleComparator.compare(1, 2), is(1));
}
@Test
public void shouldCompareAscending() throws Exception {
InvertibleComparator<Integer> invertibleComparator =
new InvertibleComparator<Integer>(comparator, true);
assertThat(invertibleComparator.compare(1, 2), is(-1));
}
@Test
public void shouldCompareDescending() throws Exception {
InvertibleComparator<Integer> invertibleComparator =
new InvertibleComparator<Integer>(comparator, false);
assertThat(invertibleComparator.compare(1, 2), is(1));
}
}

View File

@ -0,0 +1,49 @@
/*
* Copyright 2002-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.util.comparator;
import static org.junit.Assert.*;
import java.util.Comparator;
import org.junit.Test;
/**
* Tests for {@link NullSafeComparator}.
*
* @author Keith Donald
* @author Chris Beams
* @author Phillip Webb
*/
public class NullSafeComparatorTests {
@SuppressWarnings("unchecked")
@Test
public void shouldCompareWithNullsLow() {
Comparator<String> c = NullSafeComparator.NULLS_LOW;
assertTrue(c.compare(null, "boo") < 0);
}
@SuppressWarnings("unchecked")
@Test
public void shouldCompareWithNullsHigh() {
Comparator<String> c = NullSafeComparator.NULLS_HIGH;
assertTrue(c.compare(null, "boo") > 0);
assertTrue(c.compare(null, null) == 0);
}
}