Skip to content

Commit 719a9e1

Browse files
philwebbcbeams
authored andcommitted
Refactor ComparatorTests into separate classes
1 parent 9821868 commit 719a9e1

File tree

7 files changed

+395
-135
lines changed

7 files changed

+395
-135
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.util.comparator;
18+
19+
import static org.hamcrest.Matchers.is;
20+
import static org.junit.Assert.*;
21+
22+
import java.util.Comparator;
23+
24+
import org.junit.Test;
25+
26+
/**
27+
* Tests for {@link BooleanComparator}.
28+
*
29+
* @author Keith Donald
30+
* @author Chris Beams
31+
* @author Phillip Webb
32+
*/
33+
public class BooleanComparatorTests {
34+
35+
@Test
36+
public void shouldCompareWithTrueLow() {
37+
Comparator<Boolean> c = new BooleanComparator(true);
38+
assertThat(c.compare(new Boolean(true), new Boolean(false)), is(-1));
39+
assertThat(c.compare(Boolean.TRUE, Boolean.TRUE), is(0));
40+
}
41+
42+
@Test
43+
public void shouldCompareWithTrueHigh() {
44+
Comparator<Boolean> c = new BooleanComparator(false);
45+
assertThat(c.compare(new Boolean(true), new Boolean(false)), is(1));
46+
assertThat(c.compare(Boolean.TRUE, Boolean.TRUE), is(0));
47+
}
48+
49+
@Test
50+
public void shouldCompareFromTrueLow() {
51+
Comparator<Boolean> c = BooleanComparator.TRUE_LOW;
52+
assertThat(c.compare(true, false), is(-1));
53+
assertThat(c.compare(Boolean.TRUE, Boolean.TRUE), is(0));
54+
}
55+
56+
@Test
57+
public void shouldCompareFromTrueHigh() {
58+
Comparator<Boolean> c = BooleanComparator.TRUE_HIGH;
59+
assertThat(c.compare(true, false), is(1));
60+
assertThat(c.compare(Boolean.TRUE, Boolean.TRUE), is(0));
61+
}
62+
63+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.util.comparator;
18+
19+
import static org.junit.Assert.assertTrue;
20+
21+
import java.util.Comparator;
22+
23+
import org.junit.Rule;
24+
import org.junit.Test;
25+
import org.junit.rules.ExpectedException;
26+
27+
/**
28+
* Tests for {@link ComparableComparator}.
29+
*
30+
* @author Keith Donald
31+
* @author Chris Beams
32+
* @author Phillip Webb
33+
*/
34+
public class ComparableComparatorTests {
35+
36+
@Rule
37+
public ExpectedException thrown = ExpectedException.none();
38+
39+
@Test
40+
public void testComparableComparator() {
41+
Comparator<String> c = new ComparableComparator<String>();
42+
String s1 = "abc";
43+
String s2 = "cde";
44+
assertTrue(c.compare(s1, s2) < 0);
45+
}
46+
47+
@SuppressWarnings({ "unchecked", "rawtypes" })
48+
@Test
49+
public void shouldNeedComparable() {
50+
Comparator c = new ComparableComparator();
51+
Object o1 = new Object();
52+
Object o2 = new Object();
53+
thrown.expect(ClassCastException.class);
54+
c.compare(o1, o2);
55+
}
56+
57+
}

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

Lines changed: 0 additions & 135 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.util.comparator;
18+
19+
import java.util.Comparator;
20+
21+
import org.junit.Rule;
22+
import org.junit.Test;
23+
import org.junit.rules.ExpectedException;
24+
25+
/**
26+
* Test for {@link ComparableComparator}.
27+
*
28+
* @author Keith Donald
29+
* @author Chris Beams
30+
* @author Phillip Webb
31+
*/
32+
public class CompoundComparatorTests {
33+
34+
@Rule
35+
public ExpectedException thrown = ExpectedException.none();
36+
37+
@Test
38+
public void shouldNeedAtLeastOneComparator() {
39+
Comparator<String> c = new CompoundComparator<String>();
40+
thrown.expect(IllegalStateException.class);
41+
c.compare("foo", "bar");
42+
}
43+
44+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.util.comparator;
18+
19+
import static org.hamcrest.Matchers.is;
20+
import static org.junit.Assert.*;
21+
22+
import java.util.Comparator;
23+
24+
import org.junit.Test;
25+
26+
/**
27+
* Tests for {@link InstanceComparator}.
28+
*
29+
* @author Phillip Webb
30+
*/
31+
public class InstanceComparatorTests {
32+
33+
private C1 c1 = new C1();
34+
35+
private C2 c2 = new C2();
36+
37+
private C3 c3 = new C3();
38+
39+
private C4 c4 = new C4();
40+
41+
@Test
42+
public void shouldCompareClasses() throws Exception {
43+
Comparator<Object> comparator = new InstanceComparator<Object>(C1.class, C2.class);
44+
assertThat(comparator.compare(c1, c1), is(0));
45+
assertThat(comparator.compare(c1, c2), is(-1));
46+
assertThat(comparator.compare(c2, c1), is(1));
47+
assertThat(comparator.compare(c2, c3), is(-1));
48+
assertThat(comparator.compare(c2, c4), is(-1));
49+
assertThat(comparator.compare(c3, c4), is(0));
50+
}
51+
52+
@Test
53+
public void shouldCompareInterfaces() throws Exception {
54+
Comparator<Object> comparator = new InstanceComparator<Object>(I1.class, I2.class);
55+
assertThat(comparator.compare(c1, c1), is(0));
56+
assertThat(comparator.compare(c1, c2), is(0));
57+
assertThat(comparator.compare(c2, c1), is(0));
58+
assertThat(comparator.compare(c1, c3), is(-1));
59+
assertThat(comparator.compare(c3, c1), is(1));
60+
assertThat(comparator.compare(c3, c4), is(0));
61+
}
62+
63+
@Test
64+
public void shouldCompareMix() throws Exception {
65+
Comparator<Object> comparator = new InstanceComparator<Object>(I1.class, C3.class);
66+
assertThat(comparator.compare(c1, c1), is(0));
67+
assertThat(comparator.compare(c3, c4), is(-1));
68+
assertThat(comparator.compare(c3, null), is(-1));
69+
assertThat(comparator.compare(c4, null), is(0));
70+
}
71+
72+
private static interface I1 {
73+
74+
}
75+
76+
private static interface I2 {
77+
78+
}
79+
80+
private static class C1 implements I1 {
81+
}
82+
83+
private static class C2 implements I1 {
84+
}
85+
86+
private static class C3 implements I2 {
87+
}
88+
89+
private static class C4 implements I2 {
90+
}
91+
92+
}

0 commit comments

Comments
 (0)