KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > utils > poller > TestPollerWorkerThread


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: TestPollerWorkerThread.java,v 1.3 2004/10/15 06:22:27 spyromus Exp $
26
//
27

28 package de.nava.informa.utils.poller;
29
30 import de.nava.informa.core.ChannelIF;
31 import de.nava.informa.core.ItemIF;
32 import de.nava.informa.utils.manager.PersistenceManagerIF;
33 import de.nava.informa.utils.manager.PersistenceManagerException;
34 import de.nava.informa.utils.manager.memory.PersistenceManager;
35 import junit.framework.TestCase;
36
37 import java.net.MalformedURLException JavaDoc;
38 import java.net.URL JavaDoc;
39
40 /**
41  * @author Aleksey Gureev (spyromus@noizeramp.com)
42  * @see PollerWorkerThread
43  */

44 public class TestPollerWorkerThread extends TestCase {
45   private PersistenceManagerIF pm = new PersistenceManager();
46
47   /**
48    * Checks the case when existing channel has no duplicate items (empty).
49    *
50    * @see PollerWorkerThread#checkItems
51    */

52   public void testCheckItemsEmpty() {
53     // Create worker with observer. We don't need job source for this test.
54
final PollerObserverIF o = new CustomObserver();
55     final PollerApproverIF a = new CustomApprover("wanted");
56     final PollerWorkerThread worker = new PollerWorkerThread(o, a);
57
58     // Create temp and main channels.
59
try {
60       final ChannelIF cMain = pm.createChannel("main", getTestURL());
61       final ChannelIF cTemp = pm.createChannel("temp", getTestURL());
62       final ItemIF item1 = pm.createItem(cTemp, "wanted");
63       pm.createItem(cTemp, "unwanted");
64
65       // Check items in temp agains main channel.
66
worker.checkItems(cTemp, cMain);
67
68       // Check results.
69
assertEquals(1, cMain.getItems().size());
70       assertEquals(item1, cMain.getItems().toArray()[0]);
71     } catch (PersistenceManagerException e) {
72       e.printStackTrace();
73       fail();
74     }
75   }
76
77   /**
78    * Checks the case when existing channel has duplicate items.
79    *
80    * @see PollerWorkerThread#checkItems
81    */

82   public void testCheckItemsDuplicate() {
83     // Create worker with observer. We don't need job source for this test.
84
final PollerObserverIF o = new CustomObserver();
85     final PollerApproverIF a = new CustomApprover("wanted");
86     final PollerWorkerThread worker = new PollerWorkerThread(o, a);
87
88     // Create temp and main channels.
89
try {
90       final ChannelIF cMain = pm.createChannel("main", getTestURL());
91       final ChannelIF cTemp = pm.createChannel("temp", getTestURL());
92       pm.createItem(cTemp, "wanted");
93       pm.createItem(cTemp, "unwanted");
94
95       // Add duplicate "wanted" item to main channel.
96
final ItemIF item3 = pm.createItem(cMain, "wanted");
97
98       // Check items in temp agains main channel.
99
worker.checkItems(cTemp, cMain);
100
101       // Check results.
102
assertEquals(1, cMain.getItems().size());
103       assertEquals(item3, cMain.getItems().toArray()[0]);
104     } catch (PersistenceManagerException e) {
105       e.printStackTrace();
106       fail();
107     }
108   }
109
110   /**
111    * @see PollerWorkerThread#differ
112    */

113   public void testDiffer() {
114     assertFalse(PollerWorkerThread.differ(null, null));
115     assertTrue(PollerWorkerThread.differ(null, "a"));
116     assertTrue(PollerWorkerThread.differ("a", null));
117     assertTrue(PollerWorkerThread.differ("a", "b"));
118     assertFalse(PollerWorkerThread.differ("a", "a"));
119   }
120
121   public void testChannelHasChanged() {
122     try {
123       final ChannelIF a = pm.createChannel("test", getTestURL());
124
125       assertTrue(PollerWorkerThread.channelHasChanged(null, a));
126       assertFalse(PollerWorkerThread.channelHasChanged(a, a));
127
128       final ChannelIF b = pm.createChannel("test2", getTestURL());
129       assertTrue(PollerWorkerThread.channelHasChanged(a, b));
130
131       final ChannelIF c = pm.createChannel("test", getTestURL());
132       assertFalse(PollerWorkerThread.channelHasChanged(a, c));
133     } catch (PersistenceManagerException e) {
134       e.printStackTrace();
135       fail();
136     }
137   }
138
139   /**
140    * Returns test URL.
141    *
142    * @return test URL.
143    */

144   private static URL JavaDoc getTestURL() {
145     URL JavaDoc url = null;
146
147     try {
148       url = new URL JavaDoc("http://www.test.org");
149     } catch (MalformedURLException JavaDoc e) {
150       e.printStackTrace();
151     }
152
153     return url;
154   }
155
156   /**
157    * Custom observer of poller events.
158    */

159   private static class CustomObserver implements PollerObserverIF {
160     /**
161      * Invoked by Poller when new item is approved for addition. Item is transient
162      * and should be added to specified channel.
163      *
164      * @param item item added.
165      * @param channel destination channel.
166      */

167     public void itemFound(ItemIF item, ChannelIF channel) {
168       channel.addItem(item);
169     }
170
171     /**
172      * Invoked by Poller when poller of the channel failed.
173      *
174      * @param channel channel.
175      * @param e original cause of failure.
176      */

177     public void channelErrored(ChannelIF channel, Exception JavaDoc e) {
178     }
179
180     /**
181      * Invoked when Poller detected changes in channel information (title and etc).
182      *
183      * @param channel channel.
184      */

185     public void channelChanged(ChannelIF channel) {
186     }
187
188     /**
189      * Invoked by Poller when checking of the channel started.
190      *
191      * @param channel channel.
192      */

193     public void pollStarted(ChannelIF channel) {
194     }
195
196     /**
197      * Invoked by Poller when checking of the channel finished.
198      *
199      * @param channel channel.
200      */

201     public void pollFinished(ChannelIF channel) {
202     }
203   }
204
205   /**
206    * Custom approver, which approves items with given title.
207    */

208   private static class CustomApprover implements PollerApproverIF {
209     private String JavaDoc title;
210
211     /**
212      * Creates approver.
213      *
214      * @param itemTitle title of the item to approve.
215      */

216     public CustomApprover(String JavaDoc itemTitle) {
217       title = itemTitle;
218     }
219
220     /**
221      * Decides whether it's possible to add item to the channel or no.
222      *
223      * @param item item to add.
224      * @param channel destination channel.
225      * @return TRUE if addition is allowed.
226      */

227     public boolean canAddItem(ItemIF item, ChannelIF channel) {
228       return title.equals(item.getTitle());
229     }
230   }
231 }
232
Popular Tags