Fixed unit test

Refactored some code
This commit is contained in:
Patrick Gotthard 2014-04-13 12:47:31 +02:00
parent 18e31ddf15
commit 381eacd0bd
3 changed files with 32 additions and 27 deletions

View file

@ -56,11 +56,11 @@ public final class SleUtility {
* @param groups Group fields (from the SimpleListExtension module)
* @return Grouped list of entries.
*/
public static List<Extendable> group(final List<Extendable> values, final Group[] groups) {
final SortableList list = values instanceof SortableList ? (SortableList) values : new SortableList(values);
final GroupStrategy gs = new GroupStrategy();
public static <T extends Extendable> List<T> group(final List<T> values, final Group[] groups) {
final SortableList<T> list = getSortableList(values);
final GroupStrategy strategy = new GroupStrategy();
for (int i = groups.length - 1; i >= 0; i--) {
list.sortOnProperty(groups[i], true, gs);
list.sortOnProperty(groups[i], true, strategy);
}
return list;
}
@ -73,13 +73,8 @@ public final class SleUtility {
* @param ascending Sort ascending/descending.
* @return Sorted list of values
*/
public static List<Extendable> sort(final List<Extendable> values, final Sort sort, final boolean ascending) {
final SortableList list;
if (values instanceof SortableList) {
list = (SortableList) values;
} else {
list = new SortableList(values);
}
public static <T extends Extendable> List<T> sort(final List<T> values, final Sort sort, final boolean ascending) {
final SortableList<T> list = getSortableList(values);
list.sortOnProperty(sort, ascending, new SortStrategy());
return list;
}
@ -93,8 +88,8 @@ public final class SleUtility {
* @param ascending Sort ascending/descending
* @return Grouped and sorted list of entries.
*/
public static List<Extendable> sortAndGroup(final List<Extendable> values, final Group[] groups, final Sort sort, final boolean ascending) {
List<Extendable> list = sort(values, sort, ascending);
public static <T extends Extendable> List<T> sortAndGroup(final List<T> values, final Group[] groups, final Sort sort, final boolean ascending) {
List<T> list = sort(values, sort, ascending);
list = group(list, groups);
return list;
}
@ -115,4 +110,14 @@ public final class SleUtility {
feed.copyFrom(copy);
}
private static <T extends Extendable> SortableList<T> getSortableList(final List<T> list) {
SortableList<T> sortableList;
if (list instanceof SortableList) {
sortableList = (SortableList<T>) list;
} else {
sortableList = new SortableList<T>(list);
}
return sortableList;
}
}

View file

@ -5,11 +5,11 @@ import java.util.Collection;
import com.sun.syndication.feed.module.Extendable;
class SortableList extends ArrayList<Extendable> {
class SortableList<T extends Extendable> extends ArrayList<T> {
private static final long serialVersionUID = 1L;
public SortableList(final Collection<Extendable> c) {
public SortableList(final Collection<T> c) {
super(c);
}
@ -20,10 +20,10 @@ class SortableList extends ArrayList<Extendable> {
for (int i = 0; i < size() - 1; i++) {
for (int j = i + 1; j < size(); j++) {
final Extendable o1 = get(i);
final T o1 = get(i);
final Comparable oc1 = strat.getValue(o1, value);
final Extendable o2 = get(j);
final T o2 = get(j);
final Comparable oc2 = strat.getValue(o2, value);
System.out.println(oc1 + " < " + oc2);

View file

@ -37,41 +37,41 @@ public class GroupAndSortTest extends AbstractTestCase {
final File testdata = new File(super.getTestFile("data/bookexample.xml"));
final SyndFeed feed = new SyndFeedInput().build(testdata);
final List<SyndEntry> feedEntries = feed.getEntries();
final List<Extendable> entries = new ArrayList<Extendable>(feedEntries);
List<SyndEntry> entries = feed.getEntries();
final SimpleListExtension sle = (SimpleListExtension) feed.getModule(SimpleListExtension.URI);
final Sort[] sortFields = sle.getSortFields();
final SimpleListExtension extention = (SimpleListExtension) feed.getModule(SimpleListExtension.URI);
final Sort[] sortFields = extention.getSortFields();
final Sort sortByDate = sortFields[1];
final Sort sortByTitle = sortFields[2];
// sort by date ascending
List<Extendable> sortedEntries = SleUtility.sort(entries, sortByDate, true);
SyndEntry entry = (SyndEntry) sortedEntries.get(0);
List<SyndEntry> sortedEntries = SleUtility.sort(entries, sortByDate, true);
SyndEntry entry = sortedEntries.get(0);
assertEquals("Great Journeys of the Past", entry.getTitle());
// sort by date descending
sortedEntries = SleUtility.sort(entries, sortByDate, false);
entry = (SyndEntry) sortedEntries.get(0);
entry = sortedEntries.get(0);
assertEquals("Horror Stories, vol 16", entry.getTitle());
// sort by date ascending
sortedEntries = SleUtility.sort(entries, sortByDate, true);
// update first entry and reinitialize
entry = (SyndEntry) sortedEntries.get(0);
entry = sortedEntries.get(0);
entry.setTitle("ZZZZZ");
SleUtility.initializeForSorting(feed);
entries = feed.getEntries();
// sort by title desc
sortedEntries = SleUtility.sort(entries, sortByTitle, false);
entry = (SyndEntry) sortedEntries.get(0);
entry = sortedEntries.get(0);
assertEquals("ZZZZZ", entry.getTitle());
// sort by title asc
sortedEntries = SleUtility.sort(entries, sortByTitle, true);
entry = (SyndEntry) sortedEntries.get(0);
entry = sortedEntries.get(0);
assertEquals("Horror Stories, vol 16", entry.getTitle());
}