KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > utils > TestItemComparator


1 //
2
// Informa -- RSS Library for Java
3
// Copyright (c) 2002 by Niko Schmuck
4
//
5
// Niko Schmuck
6
// http://sourceforge.net/projects/informa
7
// mailto:niko_schmuck@users.sourceforge.net
8
//
9
// This library is free software.
10
//
11
// You may redistribute it and/or modify it under the terms of the GNU
12
// Lesser General Public License as published by the Free Software Foundation.
13
//
14
// Version 2.1 of the license should be included with this distribution in
15
// the file LICENSE. If the license is not included with this distribution,
16
// you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
17
// or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
18
// MA 02139 USA.
19
//
20
// This library is distributed in the hope that it will be useful,
21
// but WITHOUT ANY WARRANTY; without even the implied waranty of
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23
// Lesser General Public License for more details.
24
//
25

26
27 // $Id: TestItemComparator.java,v 1.12 2004/06/28 12:49:12 jga Exp $
28

29 package de.nava.informa.utils;
30
31 import java.io.File JavaDoc;
32 import java.util.Arrays JavaDoc;
33 import java.util.Calendar JavaDoc;
34 import java.util.TimeZone JavaDoc;
35
36 import de.nava.informa.core.ChannelIF;
37 import de.nava.informa.core.ItemIF;
38 import de.nava.informa.impl.basic.ChannelBuilder;
39 import de.nava.informa.parsers.FeedParser;
40
41 public class TestItemComparator extends InformaTestCase {
42
43   public TestItemComparator(String JavaDoc name) {
44     super("TestItemComparator", name);
45   }
46
47   public void testSort() throws Exception JavaDoc {
48
49     File JavaDoc inpFile = new File JavaDoc(getDataDir(), "snipsnap-org.rss");
50     ChannelIF channel = FeedParser.parse(new ChannelBuilder(), inpFile);
51
52     // convert from List to Array
53
Object JavaDoc[] items = channel.getItems().toArray();
54     // sort news items
55
Arrays.sort(items, new ItemComparator(true));
56
57     // compare dates
58
Calendar JavaDoc cal = Calendar.getInstance();
59
60     cal.set(2002, Calendar.OCTOBER, 16, 0, 0, 0);
61     cal.setTimeZone(TimeZone.getTimeZone("GMT"));
62     compareDates(cal, items, 0);
63
64     cal.set(2002, Calendar.OCTOBER, 14, 0, 0, 0);
65     cal.setTimeZone(TimeZone.getTimeZone("GMT"));
66     compareDates(cal, items, 1);
67
68     cal.set(2002, Calendar.OCTOBER, 10, 0, 0, 0);
69     cal.setTimeZone(TimeZone.getTimeZone("GMT"));
70     compareDates(cal, items, 2);
71
72     cal.set(2002, Calendar.OCTOBER, 1, 0, 0, 0);
73     cal.setTimeZone(TimeZone.getTimeZone("GMT"));
74     compareDates(cal, items, 8);
75
76     cal.set(2002, Calendar.SEPTEMBER, 30, 0, 0, 0);
77     cal.setTimeZone(TimeZone.getTimeZone("GMT"));
78     compareDates(cal, items, 9);
79
80     /*
81     for (int i = 0; i < items.length; i++) {
82       ItemIF item = (ItemIF) items[i];
83       System.out.println("--> title: " + item.getTitle() +
84                          ", date: " + item.getDate());
85     }
86     */

87   }
88
89   private void compareDates(Calendar JavaDoc expectedCal, Object JavaDoc[] actualItems,
90                             int index) {
91     ItemIF item = (ItemIF) actualItems[index];
92     // ignore milliseconds
93
long milliExp = expectedCal.getTime().getTime();
94     long milliAct = item.getDate().getTime();
95
96     assertEquals("Wrong date for item " + (index+1),
97                  milliExp / 1000, milliAct / 1000);
98   }
99
100 }
101
Popular Tags