001: // Informa -- RSS Library for Java
002: // Copyright (c) 2002 by Niko Schmuck
003: //
004: // Niko Schmuck
005: // http://sourceforge.net/projects/informa
006: // mailto:niko_schmuck@users.sourceforge.net
007: //
008: // This library is free software.
009: //
010: // You may redistribute it and/or modify it under the terms of the GNU
011: // Lesser General Public License as published by the Free Software Foundation.
012: //
013: // Version 2.1 of the license should be included with this distribution in
014: // the file LICENSE. If the license is not included with this distribution,
015: // you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
016: // or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
017: // MA 02139 USA.
018: //
019: // This library is distributed in the hope that it will be useful,
020: // but WITHOUT ANY WARRANTY; without even the implied waranty of
021: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
022: // Lesser General Public License for more details.
023: //
024:
025: // $Id: TestAtom_1_0_Parser.java,v 1.7 2006/12/04 23:40:49 italobb Exp $
026: package de.nava.informa.parsers;
027:
028: import de.nava.informa.core.ChannelFormat;
029: import de.nava.informa.core.ChannelIF;
030: import de.nava.informa.impl.basic.ChannelBuilder;
031: import de.nava.informa.impl.basic.Item;
032: import de.nava.informa.utils.AtomParserUtils;
033: import de.nava.informa.utils.InformaTestCase;
034: import de.nava.informa.utils.ParserUtils;
035:
036: import java.io.File;
037:
038: import java.util.Calendar;
039: import java.util.Date;
040: import java.util.GregorianCalendar;
041: import java.util.TimeZone;
042:
043: public class TestAtom_1_0_Parser extends InformaTestCase {
044: public TestAtom_1_0_Parser(String name) {
045: super ("Atom_1_0_Parser", name);
046: this .method_name = name;
047: }
048:
049: void existsEntry(ChannelIF channel, String title, String content,
050: String link, String dateString) {
051: Date date = ParserUtils.getDate(dateString);
052: java.util.Iterator itemsIterator = channel.getItems()
053: .iterator();
054: boolean found = false;
055:
056: while (itemsIterator.hasNext()) {
057: Item item = (Item) itemsIterator.next();
058:
059: if (item.getDate().equals(date)) {
060: found = true;
061:
062: if (title != null) {
063: assertEquals(title, item.getTitle());
064: }
065:
066: if (content != null) {
067: assertTrue(item.getDescription().indexOf(content) >= 0);
068: }
069:
070: if (link != null) {
071: assertEquals(link, item.getLink().toString());
072: }
073: }
074: }
075:
076: assertTrue(found);
077: }
078:
079: public void testGetMode() {
080: assertEquals("xml", Atom_1_0_Parser.getMode("xhtml"));
081: assertEquals("xml", Atom_1_0_Parser.getMode("application/xml"));
082: assertEquals("xml", Atom_1_0_Parser
083: .getMode("application/mathml+xml"));
084: assertEquals("xml", Atom_1_0_Parser.getMode("image/svg+xml"));
085: assertEquals("escaped", Atom_1_0_Parser.getMode("html"));
086: assertEquals("escaped", Atom_1_0_Parser.getMode("text"));
087: assertEquals("escaped", Atom_1_0_Parser.getMode((String) null));
088: assertEquals("base64", Atom_1_0_Parser.getMode("image/png"));
089: assertEquals("base64", Atom_1_0_Parser
090: .getMode("application/pdf"));
091: }
092:
093: public void testParseBloggerJayant7kCheckFeedDescription()
094: throws Exception {
095: File inpFile = new File(getDataDir(), "atom-1.0-jayant7k.xml");
096: ChannelIF channel = FeedParser.parse(new ChannelBuilder(),
097: inpFile);
098:
099: assertEquals(ChannelFormat.ATOM_1_0, channel.getFormat());
100:
101: assertEquals("Whatever....", channel.getTitle());
102: assertEquals("Jayant Kumar", channel.getCreator());
103: assertEquals("Blogger", channel.getGenerator());
104: assertNull(channel.getCopyright());
105:
106: // test Last pub date
107: // should be 2006-08-17T20:50:43.593-07:00
108: Calendar updtDate = new GregorianCalendar(TimeZone
109: .getTimeZone("GMT-7:00"));
110:
111: updtDate.set(Calendar.YEAR, 2006);
112: updtDate.set(Calendar.MONTH, Calendar.AUGUST);
113: updtDate.set(Calendar.DAY_OF_MONTH, 17);
114: updtDate.set(Calendar.HOUR_OF_DAY, 20);
115: updtDate.set(Calendar.MINUTE, 50);
116: updtDate.set(Calendar.SECOND, 43);
117: updtDate.set(Calendar.MILLISECOND, 593);
118: assertEquals(updtDate.getTime(), channel.getPubDate());
119: }
120:
121: public void testParseBloggerJayant7kCheckItems() throws Exception {
122: File inpFile = new File(getDataDir(), "atom-1.0-jayant7k.xml");
123: ChannelIF channel = FeedParser.parse(new ChannelBuilder(),
124: inpFile);
125:
126: assertEquals(ChannelFormat.ATOM_1_0, channel.getFormat());
127: assertEquals(25, channel.getItems().size());
128:
129: // check each item that for null values
130: java.util.Iterator itemsIterator = channel.getItems()
131: .iterator();
132:
133: while (itemsIterator.hasNext()) {
134: Item item = (Item) itemsIterator.next();
135:
136: assertNotNull(item.getDate());
137: assertNotNull(item.getDescription());
138: assertNotNull(item.getLink());
139: assertNotNull(item.getTitle());
140: }
141: }
142:
143: public void testParseBloggerMyLetterOfTheDayCheckItems()
144: throws Exception {
145: File inpFile = new File(getDataDir(),
146: "atom-1.0-myletteroftheday.xml");
147: ChannelIF channel = FeedParser.parse(new ChannelBuilder(),
148: inpFile);
149:
150: assertEquals(ChannelFormat.ATOM_1_0, channel.getFormat());
151: assertEquals(9, channel.getItems().size());
152:
153: existsEntry(
154: channel,
155: "I'm going to pre-post for tomorrow because I wont'...",
156: "I'm going to pre-post for tomorrow because I wont' be able "
157: + "to post cuz I will be terribly busy having more fun "
158: + "than humanly possible",
159: "http://myletteroftheday.blogspot.com/2006/08/im-going-to-pre-post-for-tomorrow.html",
160: "2006-08-19T00:13:00.000");
161:
162: existsEntry(
163: channel,
164: "Dear lawn (and yes, that’s lawn with a little “L”)...",
165: "I torture you, just as you torture me in the spring with your "
166: + "insistent growth and constant need of mowing. It is time "
167: + "for you to lie down, stop growing and turn completely brown.",
168: "http://myletteroftheday.blogspot.com/2006/08/dear-lawn-and-yes-thats-lawn-with.html",
169: "2006-08-16T21:08:00.000-04:00");
170: }
171:
172: public void testParsePodcastingExample() throws Exception {
173: File inpFile = new File(getDataDir(),
174: "atom-1.0-podcastingexample.xml");
175: ChannelIF channel = FeedParser.parse(new ChannelBuilder(),
176: inpFile);
177:
178: assertEquals(ChannelFormat.ATOM_1_0, channel.getFormat());
179:
180: assertEquals(1, channel.getItems().size());
181:
182: //first item
183: Item item = (Item) channel.getItems().toArray()[0];
184:
185: assertEquals(item.getTitle(), "Atom 1.0");
186: assertEquals(item.getDate(), ParserUtils
187: .getDate("2005-07-15T12:00:00Z"));
188: assertTrue(item.getDescription()
189: .contains("<h1>Show Notes</h1>"));
190: assertTrue(item.getDescription().contains(
191: "<li>00:01:00 -- Introduction</li>"));
192: assertTrue(item.getDescription().contains(
193: "<li>00:15:00 -- Talking about Atom 1.0</li>"));
194: assertTrue(item.getDescription().contains(
195: "<li>00:30:00 -- Wrapping up</li>"));
196: assertEquals("http://www.example.org/entries/1", item.getLink()
197: .toString());
198: }
199:
200: public void testPreferenceOrder() {
201: assertTrue(AtomParserUtils.getPreferenceOrderForItemLinkType(
202: "text/html", null) > AtomParserUtils
203: .getPreferenceOrderForItemLinkType("text/plain",
204: "alternate"));
205: assertTrue(AtomParserUtils.getPreferenceOrderForItemLinkType(
206: "text/html", "self") > AtomParserUtils
207: .getPreferenceOrderForItemLinkType("text/plain",
208: "alternate"));
209: assertTrue(AtomParserUtils.getPreferenceOrderForItemLinkType(
210: "text/plain", null) > AtomParserUtils
211: .getPreferenceOrderForItemLinkType("text/plain",
212: "alternate"));
213: assertTrue(AtomParserUtils.getPreferenceOrderForItemLinkType(
214: "text/plain", null) > AtomParserUtils
215: .getPreferenceOrderForItemLinkType("application/png",
216: "alternate"));
217: assertTrue(AtomParserUtils.getPreferenceOrderForItemLinkType(
218: "text/html", "enclouser") > AtomParserUtils
219: .getPreferenceOrderForItemLinkType("text/html",
220: "alternate"));
221: }
222: }
|