KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > util > comparator > ComparatorTests


1 /*
2  * Copyright 2002-2005 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 JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.springframework.beans.support.PropertyComparator;
24
25 /**
26  * @author Keith Donald
27  */

28 public class ComparatorTests extends TestCase {
29
30     public void testComparableComparator() {
31         Comparator JavaDoc c = new ComparableComparator();
32         String JavaDoc s1 = "abc";
33         String JavaDoc s2 = "cde";
34         assertTrue(c.compare(s1, s2) < 0);
35     }
36
37     public void testComparableComparatorIllegalArgs() {
38         Comparator JavaDoc c = new ComparableComparator();
39         Object JavaDoc o1 = new Object JavaDoc();
40         Object JavaDoc o2 = new Object JavaDoc();
41         try {
42             c.compare(o1, o2);
43         }
44         catch (IllegalArgumentException JavaDoc e) {
45             return;
46         }
47         fail("Comparator should have thrown a cce");
48     }
49
50     public void testBooleanComparatorTrueLow() {
51         Comparator JavaDoc c = BooleanComparator.TRUE_LOW;
52         assertTrue(c.compare(new Boolean JavaDoc(true), new Boolean JavaDoc(false)) < 0);
53     }
54
55     public void testBooleanComparatorTrueHigh() {
56         Comparator JavaDoc c = BooleanComparator.TRUE_HIGH;
57         assertTrue(c.compare(new Boolean JavaDoc(true), new Boolean JavaDoc(false)) > 0);
58         assertTrue(c.compare(Boolean.TRUE, Boolean.TRUE) == 0);
59     }
60
61     public void testPropertyComparator() {
62         Dog dog = new Dog();
63         dog.setNickName("mace");
64
65         Dog dog2 = new Dog();
66         dog2.setNickName("biscy");
67
68         PropertyComparator c = new PropertyComparator("nickName", false, true);
69         assertTrue(c.compare(dog, dog2) > 0);
70         assertTrue(c.compare(dog, dog) == 0);
71         assertTrue(c.compare(dog2, dog) < 0);
72     }
73
74     public void testPropertyComparatorNulls() {
75         Dog dog = new Dog();
76         Dog dog2 = new Dog();
77         PropertyComparator c = new PropertyComparator("nickName", false, true);
78         assertTrue(c.compare(dog, dog2) == 0);
79     }
80
81     public void testNullSafeComparatorNullsLow() {
82         Comparator JavaDoc c = NullSafeComparator.NULLS_LOW;
83         assertTrue(c.compare(null, "boo") < 0);
84     }
85
86     public void testNullSafeComparatorNullsHigh() {
87         Comparator JavaDoc c = NullSafeComparator.NULLS_HIGH;
88         assertTrue(c.compare(null, "boo") > 0);
89         assertTrue(c.compare(null, null) == 0);
90     }
91
92     public void testCompoundComparatorEmpty() {
93         Comparator JavaDoc c = new CompoundComparator();
94         try {
95             c.compare("foo", "bar");
96         }
97         catch (IllegalStateException JavaDoc e) {
98             return;
99         }
100         fail("illegal state should've been thrown on empty list");
101     }
102
103     public void testCompoundComparator() {
104         CompoundComparator c = new CompoundComparator();
105         c.addComparator(new PropertyComparator("lastName", false, true));
106
107         Dog dog1 = new Dog();
108         dog1.setFirstName("macy");
109         dog1.setLastName("grayspots");
110
111         Dog dog2 = new Dog();
112         dog2.setFirstName("biscuit");
113         dog2.setLastName("grayspots");
114
115         assertTrue(c.compare(dog1, dog2) == 0);
116
117         c.addComparator(new PropertyComparator("firstName", false, true));
118         assertTrue(c.compare(dog1, dog2) > 0);
119
120         dog2.setLastName("konikk dog");
121         assertTrue(c.compare(dog2, dog1) > 0);
122     }
123
124     public void testCompoundComparatorInvert() {
125         CompoundComparator c = new CompoundComparator();
126         c.addComparator(new PropertyComparator("lastName", false, true));
127         c.addComparator(new PropertyComparator("firstName", false, true));
128         Dog dog1 = new Dog();
129         dog1.setFirstName("macy");
130         dog1.setLastName("grayspots");
131
132         Dog dog2 = new Dog();
133         dog2.setFirstName("biscuit");
134         dog2.setLastName("grayspots");
135
136         assertTrue(c.compare(dog1, dog2) > 0);
137         c.invertOrder();
138         assertTrue(c.compare(dog1, dog2) < 0);
139     }
140
141
142     private static class Dog implements Comparable JavaDoc {
143
144         private String JavaDoc nickName;
145
146         private String JavaDoc firstName;
147
148         private String JavaDoc lastName;
149
150         public int compareTo(Object JavaDoc o) {
151             return nickName.compareTo(((Dog)o).nickName);
152         }
153
154         public String JavaDoc getNickName() {
155             return nickName;
156         }
157
158         public void setNickName(String JavaDoc nickName) {
159             this.nickName = nickName;
160         }
161
162         public String JavaDoc getFirstName() {
163             return firstName;
164         }
165
166         public void setFirstName(String JavaDoc firstName) {
167             this.firstName = firstName;
168         }
169
170         public String JavaDoc getLastName() {
171             return lastName;
172         }
173
174         public void setLastName(String JavaDoc lastName) {
175             this.lastName = lastName;
176         }
177     }
178
179 }
180
Popular Tags