KickJava   Java API By Example, From Geeks To Geeks.

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


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
// $Id: TestCacheSettingsFM.java,v 1.3 2004/06/11 09:22:08 jga Exp $
26

27 package de.nava.informa.utils;
28
29 import java.io.File JavaDoc;
30
31 import de.nava.informa.core.ChannelIF;
32 import de.nava.informa.core.FeedIF;
33 import de.nava.informa.impl.basic.ChannelBuilder;
34 import de.nava.informa.parsers.FeedParser;
35 import de.nava.informa.impl.basic.Feed;
36
37 /**
38  * @author Jean-Guy Avelin
39  */

40 public class TestCacheSettingsFM extends InformaTestCase {
41
42   public TestCacheSettingsFM(String JavaDoc name) {
43     super("TestCacheSettingsFM", name);
44   }
45
46   public void testRSS091SettingsDefault() {
47     FeedManager FM = new FeedManager(ChannelIF.UPDATE_HOURLY, 2);
48     CacheSettings cs = new CacheSettings();
49     cs.setDefaultTtl(2000);
50
51     File JavaDoc inpFile = new File JavaDoc(getDataDir(), "xmlhack-0.91.xml");
52     try {
53       ChannelIF channel = FeedParser.parse(new ChannelBuilder(), inpFile);
54
55       //test RSS0.91 behavior
56
assertEquals(2000, cs.getTtl(channel, -1L));
57
58       String JavaDoc url = new Feed(channel).getLocation().toString();
59       FM.addFeed(url);
60
61       // test RSS0.91 behavior
62
assertEquals(2000, cs.getTtl(FM.getFeed(url).getChannel(), -1L));
63     } catch (Exception JavaDoc e) {
64       System.err.println("testRSS091SettingsDefault error " + e);
65       e.printStackTrace();
66     }
67   }
68
69   public void testRSS091Settings() {
70     FeedManager FM = new FeedManager(ChannelIF.UPDATE_HOURLY, 2);
71     CacheSettings cs = new CacheSettings();
72     cs.setDefaultTtl(2000);
73
74     File JavaDoc inpFile = new File JavaDoc(getDataDir(), "xmlhack-0.91.xml");
75     try {
76       ChannelIF channel = FeedParser.parse(new ChannelBuilder(), inpFile);
77
78       //test RSS0.91 behavior
79
assertEquals(2000L, cs.getTtl(channel, 10000L)); // ttl < MINIMAL_TTL
80
assertEquals(360000L, cs.getTtl(channel, 360000L)); // ttl > MINIMAL_TTL
81

82       String JavaDoc url = new Feed(channel).getLocation().toString();
83       FM.addFeed(url);
84
85       // test RSS0.91 behavior
86
assertEquals(2000L, cs.getTtl(FM.getFeed(url).getChannel(), 10000L));
87       assertEquals(360000L, cs.getTtl(FM.getFeed(url).getChannel(), 360000L)); // ttl > MINIMAL_TTL
88
} catch (Exception JavaDoc e) {
89       System.err.println("testRSS091Settings error " + e);
90     }
91   }
92
93   public void testRSS100SettingsNoUpdatePeriodInFeed() {
94     FeedManager FM = new FeedManager(ChannelIF.UPDATE_HOURLY, 2);
95     CacheSettings cs = new CacheSettings();
96     cs.setDefaultTtl(2000);
97
98     File JavaDoc inpFile = new File JavaDoc(getDataDir(), "bloggy.rdf");
99     try {
100       ChannelIF channel = FeedParser.parse(new ChannelBuilder(), inpFile);
101
102       //test RSS1.00 behavior
103
assertEquals(2000, cs.getTtl(channel, -1L));
104       assertEquals(2000L, cs.getTtl(channel, 10000L)); // ttl < MINIMAL_TTL
105
assertEquals(360000L, cs.getTtl(channel, 360000L)); // ttl > MINIMAL_TTL
106

107       String JavaDoc url = new Feed(channel).getLocation().toString();
108       FM.addFeed(url);
109
110       // test RSS1.00 behavior with FM
111
assertEquals(2000, cs.getTtl(FM.getFeed(url).getChannel(), -1L));
112       assertEquals(2000L, cs.getTtl(FM.getFeed(url).getChannel(), 10000L));
113       assertEquals(360000L, cs.getTtl(FM.getFeed(url).getChannel(), 360000L)); // ttl > MINIMAL_TTL
114
} catch (Exception JavaDoc e) {
115       System.err.println("testRSS100SettingsNoUpdatePeriodInFeed error " + e);
116       e.printStackTrace();
117     }
118   }
119
120   public void testRSS100SettingsUpdatePeriodInFeed() {
121     FeedManager FM = new FeedManager(ChannelIF.UPDATE_HOURLY, 2);
122     CacheSettings cs = new CacheSettings();
123     cs.setDefaultTtl(2000);
124
125     File JavaDoc inpFile = new File JavaDoc(getDataDir(), "slashdot-010604.rdf");
126     try {
127       ChannelIF channel = FeedParser.parse(new ChannelBuilder(), inpFile);
128
129       //UpdatePeriod/UpdateFrequency = hourly/1 in feed
130
assertEquals(3600000, cs.getTtl(channel, -1L));
131       assertEquals(3600000, cs.getTtl(channel, 10000L));
132       assertEquals(3600005, cs.getTtl(channel, 3600005)); // > ttl of the feed
133
// cache verification
134
String JavaDoc url = new Feed(channel).getLocation().toString();
135       FM.addFeed(url);
136       assertEquals(3600000, cs.getTtl(FM.getFeed(url).getChannel(), -1L));
137       assertEquals(3600000, cs.getTtl(FM.getFeed(url).getChannel(), 10000L));
138     } catch (Exception JavaDoc e) {
139       System.err.println("testRSS100SettingsUpdatePeriodInFeed error " + e);
140     }
141   }
142
143   public void testRSS200SettingsUpdatePeriodInFeed() {
144     FeedManager FM = new FeedManager(ChannelIF.UPDATE_HOURLY, 2);
145     CacheSettings cs = new CacheSettings();
146     cs.setDefaultTtl(2000);
147
148     File JavaDoc inpFile = new File JavaDoc(getDataDir(), "mobitopia.xml");
149     try {
150       ChannelIF channel = FeedParser.parse(new ChannelBuilder(), inpFile);
151
152       //test RSS2.00 behavior
153
assertEquals((60*60*1000), cs.getTtl(channel, -1L));
154       assertEquals((60*60*1000), cs.getTtl(channel, 10000L));
155       // < ttl of feed && > MINIMAL_TTL
156
assertEquals((60*60*1000), cs.getTtl(channel, (30*60*1000))); // 30 mins
157
// > ttl of the feed
158
assertEquals((120*60*1000), cs.getTtl(channel, (120*60*1000))); // > feed ttd feed
159

160       String JavaDoc url = new Feed(channel).getLocation().toString();
161       FM.addFeed(url);
162
163       assertEquals( (60*60*1000), cs.getTtl(FM.getFeed(url).getChannel(), -1L));
164       assertEquals( (60*60*1000), cs.getTtl(FM.getFeed(url).getChannel(), 10000L));
165     } catch (Exception JavaDoc e) {
166       System.err.println("testRSS200SettingsUpdatePeriodInFeed error " + e);
167     }
168   }
169   
170   public void testFMCache091() {
171     long t1, t2;
172
173     FeedManager FM = new FeedManager(ChannelIF.UPDATE_HOURLY, 2);
174     CacheSettings cs = new CacheSettings();
175     cs.setDefaultTtl(3000);
176
177     File JavaDoc inpFile = new File JavaDoc(getDataDir(), "xmlhack-0.91.xml");
178     try {
179       ChannelIF channel = FeedParser.parse(new ChannelBuilder(), inpFile);
180
181       //test RSS0.91 behavior
182
// default ttl used
183
assertEquals(3000, cs.getTtl(channel, -1L));
184
185       String JavaDoc url = new Feed(channel).getLocation().toString();
186       FM.removeFeed(url);
187       FM.addFeed(url);
188       assertTrue("feed not in cache !!", FM.hasFeed(url));
189       FeedIF feedRef1 = FM.getFeed(url);
190
191       assertNotNull("feed read null", feedRef1);
192
193       t1 = feedRef1.getLastUpdated().getTime();
194       t2 = FM.getFeed(url).getLastUpdated().getTime();
195       assertTrue("same date for feeds read in cache " + t1, t1 == t2);
196
197       try {
198         Thread.sleep(4000);
199       } catch (Exception JavaDoc e) {
200         System.err.println("Error in Wait..." + e);
201       }
202       // must have benn updated, general Ttl
203
t2 = FM.getFeed(url).getLastUpdated().getTime();
204
205       System.err.println("LAST uptd " + t1 + "," + t2);
206       System.err.println("LAST uptd test (false?)" + (t1 == t2));
207
208       assertTrue("lastUpdated must differ for RSS091 test feed", t1 != t2);
209
210       FM.removeFeed(url);
211       assertFalse("feed should be removed", FM.hasFeed(url));
212
213       // testing ttl for a particular feed
214
System.err.println("ori:"
215           + FM.addFeed(url, 100/* minutes */).getLastUpdated().getTime());
216
217       t1 = FM.getFeed(url).getLastUpdated().getTime();
218       try {
219         Thread.sleep(4000);
220       } catch (Exception JavaDoc e) {
221         System.err.println("Error in Wait..." + e);
222       }
223
224       t2 = FM.getFeed(url).getLastUpdated().getTime();
225       //System.err.println(t1 + " "+t2);
226
assertTrue("lastUpdated must be the same for a 100 mins ttl" + url,
227           t1 == t2);
228
229     } catch (Exception JavaDoc e) {
230       System.err.println("testFMCache091 error " + e);
231       e.printStackTrace();
232     }
233   }
234
235   public void testCondGet() {
236     long t1, t2;
237
238     FeedManager FM = new FeedManager(ChannelIF.UPDATE_HOURLY, 2);
239     CacheSettings cs = new CacheSettings();
240     cs.setDefaultTtl(3000);
241
242     try {
243       ChannelIF channel = FeedParser.parse(new ChannelBuilder(), "http://www.intertwingly.net/blog/index.rss");//inpFile);
244

245       String JavaDoc url = new Feed(channel).getLocation().toString();
246       FM.removeFeed(url);
247       FM.addFeed(url);
248       FeedIF feedRef1 = FM.getFeed(url);
249
250       t1 = feedRef1.getLastUpdated().getTime();
251       t2 = FM.getFeed(url).getLastUpdated().getTime();
252       assertTrue("same date for feeds read in cache " + t1, t1 == t2);
253
254       try {
255         Thread.sleep(4000);
256       } catch (Exception JavaDoc e) {
257         System.err.println("Error in Wait..." + e);
258       }
259       
260       System.err.println("Look at the stderr for trace about cond.get");
261
262       // must have benn updated, general Ttl
263
t2 = FM.getFeed(url).getLastUpdated().getTime();
264
265
266       assertTrue("lastUpdated must be different", t1 != t2);
267
268     } catch (Exception JavaDoc e) {
269       System.err.println("testCondGet error " + e);
270       e.printStackTrace();
271     }
272   }
273
274   public void testFMCacheRSS100WithUpdatePeriod() {
275     long t1, t2;
276
277     FeedManager FM = new FeedManager(ChannelIF.UPDATE_HOURLY, 2);
278     CacheSettings cs = new CacheSettings();
279     cs.setDefaultTtl(3000);
280
281     /* test RDF behavior, this feed has an updatePeriod element */
282     File JavaDoc inpFile = new File JavaDoc(getDataDir(), "slashdot-010604.rdf");
283     try {
284       ChannelIF channel = FeedParser.parse(new ChannelBuilder(), inpFile);
285
286       //test RSS0.91 behavior
287
// default ttl used
288
assertEquals(60 * 60 * 1000, cs.getTtl(channel, -1L));
289
290       String JavaDoc url = new Feed(channel).getLocation().toString();
291       FM.removeFeed(url);
292
293       channel = FeedParser.parse(new ChannelBuilder(), inpFile);
294       //System.err.println("channel:"+channel);
295
url = new Feed(channel).getLocation().toString();
296       FeedIF feedRef1 = FM.addFeed(url);
297       t1 = feedRef1.getLastUpdated().getTime();
298       try {
299         Thread.sleep(4000);
300       } catch (Exception JavaDoc e) {
301         System.err.println("Error in Wait..." + e);
302       }
303       t2 = FM.getFeed(url).getLastUpdated().getTime();
304       assertTrue("lastUpdated must be the same for this RSS1.00 test feed",
305           (t1 == t2));
306
307     } catch (Exception JavaDoc e) {
308       System.err.println("testFMCacheRSS100WithUpdatePeriod error " + e);
309       e.printStackTrace();
310     }
311   }
312 }
313
314
Popular Tags