001: //
002: // Informa -- RSS Library for Java
003: // Copyright (c) 2002 by Niko Schmuck
004: //
005: // Niko Schmuck
006: // http://sourceforge.net/projects/informa
007: // mailto:niko_schmuck@users.sourceforge.net
008: //
009: // This library is free software.
010: //
011: // You may redistribute it and/or modify it under the terms of the GNU
012: // Lesser General Public License as published by the Free Software Foundation.
013: //
014: // Version 2.1 of the license should be included with this distribution in
015: // the file LICENSE. If the license is not included with this distribution,
016: // you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
017: // or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
018: // MA 02139 USA.
019: //
020: // This library is distributed in the hope that it will be useful,
021: // but WITHOUT ANY WARRANTY; without even the implied waranty of
022: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
023: // Lesser General Public License for more details.
024: //
025:
026: // $Id: TestItemComparator.java,v 1.13 2006/12/04 23:43:28 italobb Exp $
027:
028: package de.nava.informa.utils;
029:
030: import java.io.File;
031: import java.util.Arrays;
032: import java.util.Calendar;
033: import java.util.Set;
034: import java.util.TimeZone;
035:
036: import de.nava.informa.core.ChannelIF;
037: import de.nava.informa.core.ItemIF;
038: import de.nava.informa.impl.basic.ChannelBuilder;
039: import de.nava.informa.parsers.FeedParser;
040:
041: public class TestItemComparator extends InformaTestCase {
042:
043: public TestItemComparator(String name) {
044: super ("TestItemComparator", name);
045: }
046:
047: public void testSort() throws Exception {
048:
049: File inpFile = new File(getDataDir(), "snipsnap-org.rss");
050: ChannelIF channel = FeedParser.parse(new ChannelBuilder(),
051: inpFile);
052:
053: // convert from List to Array
054: Set<ItemIF> itemsSet = channel.getItems();
055: ItemIF[] items = itemsSet.toArray(new ItemIF[itemsSet.size()]);
056: // sort news items
057: Arrays.sort(items, new ItemComparator(true));
058:
059: // compare dates
060: Calendar cal = Calendar.getInstance();
061:
062: cal.set(2002, Calendar.OCTOBER, 16, 0, 0, 0);
063: cal.setTimeZone(TimeZone.getTimeZone("GMT"));
064: compareDates(cal, items, 0);
065:
066: cal.set(2002, Calendar.OCTOBER, 14, 0, 0, 0);
067: cal.setTimeZone(TimeZone.getTimeZone("GMT"));
068: compareDates(cal, items, 1);
069:
070: cal.set(2002, Calendar.OCTOBER, 10, 0, 0, 0);
071: cal.setTimeZone(TimeZone.getTimeZone("GMT"));
072: compareDates(cal, items, 2);
073:
074: cal.set(2002, Calendar.OCTOBER, 1, 0, 0, 0);
075: cal.setTimeZone(TimeZone.getTimeZone("GMT"));
076: compareDates(cal, items, 8);
077:
078: cal.set(2002, Calendar.SEPTEMBER, 30, 0, 0, 0);
079: cal.setTimeZone(TimeZone.getTimeZone("GMT"));
080: compareDates(cal, items, 9);
081:
082: /*
083: for (int i = 0; i < items.length; i++) {
084: ItemIF item = (ItemIF) items[i];
085: System.out.println("--> title: " + item.getTitle() +
086: ", date: " + item.getDate());
087: }
088: */
089: }
090:
091: private void compareDates(Calendar expectedCal,
092: Object[] actualItems, int index) {
093: ItemIF item = (ItemIF) actualItems[index];
094: // ignore milliseconds
095: long milliExp = expectedCal.getTime().getTime();
096: long milliAct = item.getDate().getTime();
097:
098: assertEquals("Wrong date for item " + (index + 1),
099: milliExp / 1000, milliAct / 1000);
100: }
101:
102: }
|