1 16 17 package org.springframework.util.comparator; 18 19 import java.util.Comparator ; 20 21 import junit.framework.TestCase; 22 23 import org.springframework.beans.support.PropertyComparator; 24 25 28 public class ComparatorTests extends TestCase { 29 30 public void testComparableComparator() { 31 Comparator c = new ComparableComparator(); 32 String s1 = "abc"; 33 String s2 = "cde"; 34 assertTrue(c.compare(s1, s2) < 0); 35 } 36 37 public void testComparableComparatorIllegalArgs() { 38 Comparator c = new ComparableComparator(); 39 Object o1 = new Object (); 40 Object o2 = new Object (); 41 try { 42 c.compare(o1, o2); 43 } 44 catch (IllegalArgumentException e) { 45 return; 46 } 47 fail("Comparator should have thrown a cce"); 48 } 49 50 public void testBooleanComparatorTrueLow() { 51 Comparator c = BooleanComparator.TRUE_LOW; 52 assertTrue(c.compare(new Boolean (true), new Boolean (false)) < 0); 53 } 54 55 public void testBooleanComparatorTrueHigh() { 56 Comparator c = BooleanComparator.TRUE_HIGH; 57 assertTrue(c.compare(new Boolean (true), new Boolean (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 c = NullSafeComparator.NULLS_LOW; 83 assertTrue(c.compare(null, "boo") < 0); 84 } 85 86 public void testNullSafeComparatorNullsHigh() { 87 Comparator 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 c = new CompoundComparator(); 94 try { 95 c.compare("foo", "bar"); 96 } 97 catch (IllegalStateException 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 { 143 144 private String nickName; 145 146 private String firstName; 147 148 private String lastName; 149 150 public int compareTo(Object o) { 151 return nickName.compareTo(((Dog)o).nickName); 152 } 153 154 public String getNickName() { 155 return nickName; 156 } 157 158 public void setNickName(String nickName) { 159 this.nickName = nickName; 160 } 161 162 public String getFirstName() { 163 return firstName; 164 } 165 166 public void setFirstName(String firstName) { 167 this.firstName = firstName; 168 } 169 170 public String getLastName() { 171 return lastName; 172 } 173 174 public void setLastName(String lastName) { 175 this.lastName = lastName; 176 } 177 } 178 179 } 180 | Popular Tags |