From 719a9e120d781a23bc81f79a59d0377f8cbc241e Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 27 Aug 2012 13:20:24 -0700 Subject: [PATCH] Refactor ComparatorTests into separate classes --- .../comparator/BooleanComparatorTests.java | 63 ++++++++ .../comparator/ComparableComparatorTests.java | 57 ++++++++ .../util/comparator/ComparatorTests.java | 135 ------------------ .../comparator/CompoundComparatorTests.java | 44 ++++++ .../comparator/InstanceComparatorTests.java | 92 ++++++++++++ .../comparator/InvertibleComparatorTests.java | 90 ++++++++++++ .../comparator/NullSafeComparatorTests.java | 49 +++++++ 7 files changed, 395 insertions(+), 135 deletions(-) create mode 100644 spring-core/src/test/java/org/springframework/util/comparator/BooleanComparatorTests.java create mode 100644 spring-core/src/test/java/org/springframework/util/comparator/ComparableComparatorTests.java delete mode 100644 spring-core/src/test/java/org/springframework/util/comparator/ComparatorTests.java create mode 100644 spring-core/src/test/java/org/springframework/util/comparator/CompoundComparatorTests.java create mode 100644 spring-core/src/test/java/org/springframework/util/comparator/InstanceComparatorTests.java create mode 100644 spring-core/src/test/java/org/springframework/util/comparator/InvertibleComparatorTests.java create mode 100644 spring-core/src/test/java/org/springframework/util/comparator/NullSafeComparatorTests.java diff --git a/spring-core/src/test/java/org/springframework/util/comparator/BooleanComparatorTests.java b/spring-core/src/test/java/org/springframework/util/comparator/BooleanComparatorTests.java new file mode 100644 index 00000000000..8bf0ebd7813 --- /dev/null +++ b/spring-core/src/test/java/org/springframework/util/comparator/BooleanComparatorTests.java @@ -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 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 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 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 c = BooleanComparator.TRUE_HIGH; + assertThat(c.compare(true, false), is(1)); + assertThat(c.compare(Boolean.TRUE, Boolean.TRUE), is(0)); + } + +} diff --git a/spring-core/src/test/java/org/springframework/util/comparator/ComparableComparatorTests.java b/spring-core/src/test/java/org/springframework/util/comparator/ComparableComparatorTests.java new file mode 100644 index 00000000000..450442f2197 --- /dev/null +++ b/spring-core/src/test/java/org/springframework/util/comparator/ComparableComparatorTests.java @@ -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 c = new ComparableComparator(); + 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); + } + +} diff --git a/spring-core/src/test/java/org/springframework/util/comparator/ComparatorTests.java b/spring-core/src/test/java/org/springframework/util/comparator/ComparatorTests.java deleted file mode 100644 index 3bfa266229f..00000000000 --- a/spring-core/src/test/java/org/springframework/util/comparator/ComparatorTests.java +++ /dev/null @@ -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 c = new ComparableComparator(); - 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 c = BooleanComparator.TRUE_LOW; - assertTrue(c.compare(new Boolean(true), new Boolean(false)) < 0); - } - - @Test - public void testBooleanComparatorTrueHigh() { - Comparator 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 c = NullSafeComparator.NULLS_LOW; - assertTrue(c.compare(null, "boo") < 0); - } - - @SuppressWarnings("unchecked") - @Test - public void testNullSafeComparatorNullsHigh() { - Comparator c = NullSafeComparator.NULLS_HIGH; - assertTrue(c.compare(null, "boo") > 0); - assertTrue(c.compare(null, null) == 0); - } - - @Test - public void testCompoundComparatorEmpty() { - Comparator c = new CompoundComparator(); - 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 { - - 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; - } - } - -} diff --git a/spring-core/src/test/java/org/springframework/util/comparator/CompoundComparatorTests.java b/spring-core/src/test/java/org/springframework/util/comparator/CompoundComparatorTests.java new file mode 100644 index 00000000000..10d5ca33c78 --- /dev/null +++ b/spring-core/src/test/java/org/springframework/util/comparator/CompoundComparatorTests.java @@ -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 c = new CompoundComparator(); + thrown.expect(IllegalStateException.class); + c.compare("foo", "bar"); + } + +} diff --git a/spring-core/src/test/java/org/springframework/util/comparator/InstanceComparatorTests.java b/spring-core/src/test/java/org/springframework/util/comparator/InstanceComparatorTests.java new file mode 100644 index 00000000000..002262c25ca --- /dev/null +++ b/spring-core/src/test/java/org/springframework/util/comparator/InstanceComparatorTests.java @@ -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 comparator = new InstanceComparator(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 comparator = new InstanceComparator(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 comparator = new InstanceComparator(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 { + } + +} diff --git a/spring-core/src/test/java/org/springframework/util/comparator/InvertibleComparatorTests.java b/spring-core/src/test/java/org/springframework/util/comparator/InvertibleComparatorTests.java new file mode 100644 index 00000000000..6f9a7feffb1 --- /dev/null +++ b/spring-core/src/test/java/org/springframework/util/comparator/InvertibleComparatorTests.java @@ -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 comparator = new ComparableComparator(); + + @Test + public void shouldNeedComparator() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Comparator must not be null"); + new InvertibleComparator(null); + } + + @Test + public void shouldNeedComparatorWithAscending() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Comparator must not be null"); + new InvertibleComparator(null, true); + } + + @Test + public void shouldDefaultToAscending() throws Exception { + InvertibleComparator invertibleComparator = + new InvertibleComparator(comparator); + assertThat(invertibleComparator.isAscending(), is(true)); + assertThat(invertibleComparator.compare(1, 2), is(-1)); + } + + @Test + public void shouldInvert() throws Exception { + InvertibleComparator invertibleComparator = + new InvertibleComparator(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 invertibleComparator = + new InvertibleComparator(comparator, true); + assertThat(invertibleComparator.compare(1, 2), is(-1)); + } + + @Test + public void shouldCompareDescending() throws Exception { + InvertibleComparator invertibleComparator = + new InvertibleComparator(comparator, false); + assertThat(invertibleComparator.compare(1, 2), is(1)); + } + +} diff --git a/spring-core/src/test/java/org/springframework/util/comparator/NullSafeComparatorTests.java b/spring-core/src/test/java/org/springframework/util/comparator/NullSafeComparatorTests.java new file mode 100644 index 00000000000..9615f0c071d --- /dev/null +++ b/spring-core/src/test/java/org/springframework/util/comparator/NullSafeComparatorTests.java @@ -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 c = NullSafeComparator.NULLS_LOW; + assertTrue(c.compare(null, "boo") < 0); + } + + @SuppressWarnings("unchecked") + @Test + public void shouldCompareWithNullsHigh() { + Comparator c = NullSafeComparator.NULLS_HIGH; + assertTrue(c.compare(null, "boo") > 0); + assertTrue(c.compare(null, null) == 0); + } + +}