001: package org.andromda.utils;
002:
003: import java.util.Arrays;
004: import java.util.Iterator;
005: import java.util.List;
006:
007: import junit.framework.TestCase;
008:
009: import org.andromda.utils.beans.BeanSorter;
010: import org.andromda.utils.beans.SortCriteria;
011: import org.andromda.utils.beans.SortCriteria.Ordering;
012:
013: /**
014: * Tests {@link BeanSorter}
015: *
016: * @author Chad Brandon
017: */
018: public class BeanSorterTest extends TestCase {
019: public void testSort() {
020: final List persons = Arrays.asList(BeanSorterTest.persons);
021:
022: // - try simple property sorting
023: List sorted = BeanSorter.sort(persons,
024: new SortCriteria[] { new SortCriteria("firstName",
025: Ordering.ASCENDING) });
026: Iterator iterator = sorted.iterator();
027: Person billy = (Person) iterator.next();
028: assertEquals(billy.getFirstName(), "Billy");
029: Person chad = (Person) iterator.next();
030: assertEquals(chad.getFirstName(), "Chad");
031: Person john = (Person) iterator.next();
032: assertEquals(john.getFirstName(), "John");
033:
034: sorted = BeanSorter.sort(persons,
035: new SortCriteria[] { new SortCriteria("firstName",
036: Ordering.DESCENDING) });
037: iterator = sorted.iterator();
038: john = (Person) iterator.next();
039: assertEquals(john.getFirstName(), "John");
040: chad = (Person) iterator.next();
041: assertEquals(chad.getFirstName(), "Chad");
042: billy = (Person) iterator.next();
043: assertEquals(billy.getFirstName(), "Billy");
044:
045: // - try nested property sorting
046: sorted = BeanSorter.sort(persons,
047: new SortCriteria[] { new SortCriteria(
048: "address.streetNumber", Ordering.ASCENDING) });
049: iterator = sorted.iterator();
050: john = (Person) iterator.next();
051: assertEquals(john.getFirstName(), "John");
052: chad = (Person) iterator.next();
053: assertEquals(chad.getFirstName(), "Chad");
054: billy = (Person) iterator.next();
055: assertEquals(billy.getFirstName(), "Billy");
056:
057: sorted = BeanSorter.sort(persons,
058: new SortCriteria[] { new SortCriteria(
059: "address.streetNumber", Ordering.DESCENDING) });
060: iterator = sorted.iterator();
061: billy = (Person) iterator.next();
062: assertEquals(billy.getFirstName(), "Billy");
063: chad = (Person) iterator.next();
064: assertEquals(chad.getFirstName(), "Chad");
065: john = (Person) iterator.next();
066: assertEquals(john.getFirstName(), "John");
067:
068: }
069:
070: private static final Person[] persons = new Person[] {
071: new Person("Chad", "Brandon", new Address(1234, "A Street")),
072: new Person("Billy", "Bob", new Address(2323, "B Street")),
073: new Person("John", "Doe", new Address(1, "C Street")) };
074:
075: public static class Person {
076: public Person(final String firstName, final String lastName,
077: final Address address) {
078: this .firstName = firstName;
079: this .lastName = lastName;
080: this .address = address;
081: }
082:
083: private String firstName;
084: private String lastName;
085: private Address address;
086:
087: public Address getAddress() {
088: return address;
089: }
090:
091: public void setAddress(Address address) {
092: this .address = address;
093: }
094:
095: public String getFirstName() {
096: return firstName;
097: }
098:
099: public void setFirstName(String firstName) {
100: this .firstName = firstName;
101: }
102:
103: public String getLastName() {
104: return lastName;
105: }
106:
107: public void setLastName(String lastName) {
108: this .lastName = lastName;
109: }
110: }
111:
112: public static class Address {
113: public Address(final int streetNumber, final String streetName) {
114: this .streetNumber = streetNumber;
115: this .streetName = streetName;
116: }
117:
118: private int streetNumber;
119: private String streetName;
120:
121: public String getStreetName() {
122: return streetName;
123: }
124:
125: public void setStreetName(String streetName) {
126: this .streetName = streetName;
127: }
128:
129: public int getStreetNumber() {
130: return streetNumber;
131: }
132:
133: public void setStreetNumber(int streetNumber) {
134: this.streetNumber = streetNumber;
135: }
136: }
137: }
|