KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > exporters > TestRSS_2_0_Exporter


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: TestRSS_2_0_Exporter.java,v 1.2 2004/10/14 21:01:33 niko_schmuck Exp $
28

29 package de.nava.informa.exporters;
30
31 import java.io.File JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.net.MalformedURLException JavaDoc;
34 import java.net.URL JavaDoc;
35 import java.util.Collection JavaDoc;
36 import java.util.Date JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39
40 import de.nava.informa.core.*;
41 import de.nava.informa.impl.basic.Channel;
42 import de.nava.informa.impl.basic.ChannelBuilder;
43 import de.nava.informa.impl.basic.Item;
44 import de.nava.informa.parsers.FeedParser;
45 import de.nava.informa.utils.InformaTestCase;
46 import junit.framework.TestSuite;
47 import junit.framework.Test;
48 import junit.textui.TestRunner;
49
50 public class TestRSS_2_0_Exporter extends InformaTestCase {
51
52     public TestRSS_2_0_Exporter(String JavaDoc name) {
53         super("TestRSS_2_0_Exporter", name);
54     }
55
56     public void testExportChannel()
57             throws IOException JavaDoc, MalformedURLException JavaDoc, ParseException {
58
59         String JavaDoc ch_title = "The Great Demo Channel";
60         String JavaDoc ch_desc = "Just a very simple short description.";
61
62         ChannelBuilder builder = new ChannelBuilder();
63
64         // create dummy channel
65
ChannelIF channel = new Channel(ch_title);
66         channel.setDescription(ch_desc);
67         channel.setSite(new URL JavaDoc("http://nava.de"));
68         ItemIF itemA = new Item("Bugo", "All about it!",
69                 new URL JavaDoc("http://nava.de/huhu2002"));
70         itemA.setFound(new Date JavaDoc());
71         CategoryIF cat1 = builder.createCategory(null, "cat1");
72         CategoryIF cat1a = builder.createCategory(cat1, "cat1a");
73         CategoryIF cat1aa = builder.createCategory(cat1a, "cat1aa");
74         CategoryIF cat1ab = builder.createCategory(cat1a, "cat1ab");
75         CategoryIF cat1aba = builder.createCategory(cat1ab, "cat1aba");
76         CategoryIF cat1abb = builder.createCategory(cat1ab, "cat1abb");
77         CategoryIF cat1ac = builder.createCategory(cat1a, "cat1ac");
78         CategoryIF cat1b = builder.createCategory(cat1, "cat1b");
79         CategoryIF cat1c = builder.createCategory(cat1, "cat1c");
80         CategoryIF cat1ca = builder.createCategory(cat1c, "cat1ca");
81         CategoryIF cat2 = builder.createCategory(null, "cat2");
82         itemA.addCategory(cat1);
83         itemA.addCategory(cat2);
84         assertEquals(2, itemA.getCategories().size());
85         channel.addItem(itemA);
86         // TODO: what about markup here ???
87
ItemIF itemB = new Item("SoCool",
88                 "????**$12 @??? # <strong>should</strong> work",
89                 new URL JavaDoc("http://nava.de/very/nested/98"));
90         itemB.setFound(new Date JavaDoc());
91         CategoryIF catX = builder.createCategory(null, "catX");
92         CategoryIF catXa = builder.createCategory(catX, "catXa");
93         itemB.addCategory(catX);
94         assertEquals(1, itemB.getCategories().size());
95         channel.addItem(itemB);
96         assertEquals(2, channel.getItems().size());
97         // export this channel to file (encoding: utf-8)
98
String JavaDoc basename = "export-rss20.xml";
99         String JavaDoc exp_file = getOutputDir() + FS + basename;
100         ChannelExporterIF exporter = new RSS_2_0_Exporter(exp_file);
101         exporter.write(channel);
102
103         // clean channel object
104
channel = null;
105
106         // read in again
107
File JavaDoc inpFile = new File JavaDoc(exp_file);
108         channel = FeedParser.parse(new ChannelBuilder(), inpFile);
109
110         assertEquals(ch_title, channel.getTitle());
111         assertEquals(ch_desc, channel.getDescription());
112
113         Collection JavaDoc items = channel.getItems();
114         assertEquals(2, items.size());
115         Iterator JavaDoc it = items.iterator();
116         ItemIF item = (ItemIF) it.next();
117         if (item.equals(itemA)) {
118             assertEquals(item, itemA);
119
120             // test that item categories match
121
List JavaDoc catList = (List JavaDoc)item.getCategories();
122             assertEquals(catList.size(), 2);
123             Iterator JavaDoc itCat = catList.iterator();
124             CategoryIF cat = (CategoryIF)itCat.next();
125             if (cat.equals(cat1)) {
126                 assertEquals(cat.getTitle(), cat1.getTitle());
127             }
128             else {
129                 assertEquals(cat.getTitle(), cat2.getTitle());
130             }
131
132             item = (ItemIF) it.next();
133             assertEquals(item, itemB);
134         } else {
135             assertEquals(item, itemB);
136
137             // test that item categories match
138
List JavaDoc catList = (List JavaDoc)item.getCategories();
139             assertEquals(catList.size(), 1);
140             Iterator JavaDoc itCat = catList.iterator();
141             CategoryIF cat = (CategoryIF)itCat.next();
142             assertEquals(cat.getTitle(), catX.getTitle());
143
144             item = (ItemIF) it.next();
145             assertEquals(item, itemA);
146         }
147     }
148
149     public static Test suite() {
150       TestSuite suite = new TestSuite();
151       suite.addTest(new TestSuite(TestRSS_2_0_Exporter.class));
152       return suite;
153     }
154
155      public static void main(String JavaDoc[] args) {
156         TestRunner.run(suite());
157      }
158
159 }
160
Popular Tags