KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > impl > hibernate > TestPersistChannelGrp


1 //
2
// Informa -- RSS Library for Java
3
// Copyright (c) 2002, 2003 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 // $Id: TestPersistChannelGrp.java,v 1.8 2004/07/01 08:12:59 spyromus Exp $
27

28 package de.nava.informa.impl.hibernate;
29
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import net.sf.hibernate.HibernateException;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 import de.nava.informa.utils.InformaTestCase;
39 import de.nava.informa.utils.PersistChanGrpMgr;
40 import de.nava.informa.utils.PersistChanGrpMgrTask;
41 import de.nava.informa.utils.RssUrlTestData;
42
43 /**
44  * Tests for the PersistChanGrpMgr and PersistChanGrpMgrTask classes
45  *
46  * @author Pito Salas
47  */

48 public class TestPersistChannelGrp extends InformaTestCase {
49
50   private SessionHandler handler;
51
52   private static Log logger = LogFactory.getLog(TestPersistChannelGrp.class);
53
54   /**
55    * Constructor.
56    *
57    * @param name Name of the JUnit Test
58    */

59   public TestPersistChannelGrp(String JavaDoc name) {
60     super("TestPersistChannelGrp", name);
61   }
62
63   /**
64    * Common setup for every single test.
65    *
66    * @throws HibernateException
67    */

68   protected void setUp() throws HibernateException {
69     handler = SessionHandler.getInstance();
70     handler.getSession(); // make sure the session is opened
71
}
72
73   /**
74    * A battery of empty groups
75    *
76    * @exception Exception (Caught by JUnit)
77    *
78    */

79   public void testEmptyGroups() throws Exception JavaDoc {
80     PersistChanGrpMgr agroup, bgroup, cgroup;
81     agroup = makeEmptyGroup("Group Foo");
82     bgroup = makeEmptyGroup("Group Bar");
83     cgroup = makeEmptyGroup("Group Too");
84     assertValidGroup(agroup, "Group Foo", 0);
85     assertValidGroup(bgroup, "Group Bar", 0);
86     assertValidGroup(cgroup, "Group Too", 0);
87   }
88
89   /**
90    * Create a group, and add some channels and see if all is well
91    *
92    * @exception Exception (Caught by JUnit)
93    */

94   public void testGroupWithChannels() throws Exception JavaDoc {
95     PersistChanGrpMgr agroup;
96     agroup = makeEmptyGroup("Group Secret With Channels");
97     assertValidGroup(agroup, "Group Secret With Channels", 0);
98
99     addChannel(agroup, "Joho", "http://www.hyperorg.com/blogger/index.rdf");
100     addChannel(agroup, "Raliable", "http://www.raibledesigns.com/rss/rd");
101     addChannel(agroup, "Active Window", "http://www.activewin.com/awin/headlines.rss");
102     addChannel(agroup, "Pitos Blog", "http://radio.weblogs.com/0125664/rss.xml");
103
104     runPersistChanGrpTask(agroup, 3);
105     assertValidGroup(agroup, "Group Secret With Channels", 4);
106   }
107
108   /**
109    * Test adding a Channel to a group which already exists in a different group, which should just
110    * work.
111    *
112    * @throws Exception
113    */

114   public void testAddingExistingChannel() throws Exception JavaDoc {
115     final PersistChanGrpMgr agroup;
116     agroup = makeEmptyGroup("A Group");
117     assertValidGroup(agroup, "A Group", 0);
118     addGenChannels(agroup, 5, 3);
119
120     final PersistChanGrpMgr bgroup;
121     bgroup = makeEmptyGroup("B Group");
122     assertValidGroup(bgroup, "B Group", 0);
123     addGenChannels(bgroup, 5, 3);
124
125     // Add a certain channel to this group
126
addChannel(agroup, "Terrestial", "http://www.transterrestrial.com/index.xml");
127     assertValidGroup(agroup, "A Group", 6);
128
129     // and now add the SAME channel to a different group. make sure that it gets there
130
addChannel(bgroup, "Terrestial", "http://www.transterrestrial.com/index.xml");
131     assertValidGroup(bgroup, "B Group", 6);
132   }
133
134   /**
135    * Test the functioning of the hasChannel() method.
136    *
137    * @throws Exception
138    */

139   public void testChannelMembership() throws Exception JavaDoc {
140     final PersistChanGrpMgr agroup;
141     agroup = makeEmptyGroup("Membership Group");
142     addGenChannels(agroup, 3, 3);
143
144     final Iterator JavaDoc channels = agroup.getChannelGroup().getChannels().iterator();
145     while (channels.hasNext()) {
146       final Channel nextchan = (Channel) channels.next();
147       assertTrue("Expected member channel not found", agroup.hasChannel(nextchan));
148     }
149   }
150   
151   /**
152    * Test activation and deactivation of PersistChanGrpMgrs
153    *
154    * @throws Exception
155    */

156   public void testGroupDeactivate() throws Exception JavaDoc {
157     final PersistChanGrpMgr group1, group2, group3;
158     group1 = makeEmptyGroup("Activation Group 1");
159     group2 = makeEmptyGroup("Activation Group 2");
160     group3 = makeEmptyGroup("Activation Group 3");
161
162     addGenChannels(group1, 5, 3);
163     addGenChannels(group2, 5, 3);
164     addGenChannels(group3, 5, 3);
165     
166     assertFalse("expected group 1 to be inactive", group1.isActivated());
167     assertFalse("expected group 2 to be inactive", group2.isActivated());
168     assertFalse("expected group 3 to be inactive", group3.isActivated());
169     
170     group1.activate();
171     assertTrue("expected group 1 to be active", group1.isActivated());
172     assertFalse("expected group 2 to be inactive", group2.isActivated());
173     assertFalse("expected group 3 to be inactive", group3.isActivated());
174
175     Thread.sleep(500);
176     
177     group2.activate();
178     assertTrue("expected group 1 to be active", group1.isActivated());
179     assertTrue("expected group 2 to be active", group2.isActivated());
180     assertFalse("expected group 3 to be inactive", group3.isActivated());
181     
182     Thread.sleep(500);
183
184     group3.activate();
185     assertTrue("expected group 1 to be active", group1.isActivated());
186     assertTrue("expected group 2 to be active", group2.isActivated());
187     assertTrue("expected group 3 to be active", group3.isActivated());
188     
189     Thread.sleep(500);
190
191     group1.deActivate(true);
192     assertFalse("expected group 1 to be inactive", group1.isActivated());
193     assertTrue("expected group 2 to be active", group2.isActivated());
194     assertTrue("expected group 3 to be active", group3.isActivated());
195     
196     Thread.sleep(500);
197
198     group2.deActivate(true);
199     assertFalse("expected group 1 to be inactive", group1.isActivated());
200     assertFalse("expected group 2 to be inactive", group2.isActivated());
201     assertTrue("expected group 3 to be active", group3.isActivated());
202     
203     Thread.sleep(500);
204
205     group3.deActivate(true);
206     assertFalse("expected group 1 to be inactive", group1.isActivated());
207     assertFalse("expected group 2 to be inactive", group2.isActivated());
208     assertFalse("expected group 3 to be inactive", group3.isActivated());
209   }
210
211   /**
212    * Test moving a Channels from one group to another
213    *
214    * @throws Exception
215    */

216   public void testMoveChannelBetweenGroups() throws Exception JavaDoc {
217     // Create the two groups
218
PersistChanGrpMgr sourceGrp, destGrp;
219     sourceGrp = makeEmptyGroup("Source Group");
220     destGrp = makeEmptyGroup("Destination Group");
221
222     // Populate source group with some channels.
223
int count = 5;
224     int sourceHas = count;
225     int destHas = 0;
226     addGenChannels(sourceGrp, count, 3);
227
228     // Now move them one by one to the other ChannelGroup and verify the counts as we go
229
for (int i = 0; i < count; i++) {
230       // Verify that each group has the expected number of channels
231
assertValidGroup(sourceGrp, "Source Group", sourceHas - i);
232       assertValidGroup(destGrp, "Destination Group", destHas + i);
233
234       // Identify the first channel after ensuring that there is at least one.
235
Channel channelToMove;
236       Collection JavaDoc chans = sourceGrp.getChannelGroup().getChannels();
237       Iterator JavaDoc iter = chans.iterator();
238       assertTrue(chans.size() > 0);
239       channelToMove = (Channel) iter.next();
240       sourceGrp.moveChannelTo(channelToMove, destGrp);
241     }
242   }
243
244   /**
245    * Test deleting individual items from Channels in a PersistChanGrp
246    *
247    * @throws Exception
248    */

249   public void testDeleteItemsFromChannels() throws Exception JavaDoc {
250     // Create a ChannelGroup and populate it with channels and items
251
PersistChanGrpMgr delGrp;
252     delGrp = makeEmptyGroup("deleteTest Group");
253     int count = 10;
254     addGenChannels(delGrp, count, 5);
255
256     Object JavaDoc[] channels = delGrp.getChannelGroup().getChannels().toArray();
257     assertEquals("Wrong number of channels in group", channels.length, count);
258
259     // Iterate through all the new channels
260
for (int i = 0; i < count; i++) {
261       Channel aChan = (Channel) channels[i];
262       Object JavaDoc[] items = aChan.getItems().toArray();
263       int howManyItems = items.length;
264
265       // Now iterate through all the items of this channel, and delete them one by one
266
for (int j = 0; j < howManyItems; j++) {
267         Item anItem = (Item) items[j];
268         assertEquals("Wrong number of items after delete", howManyItems - j, aChan.getItems()
269             .size());
270         delGrp.deleteItemFromChannel(aChan, anItem);
271       }
272     }
273   }
274
275   /**
276    * Test deleting channels from a group by adding a certain number, and then deleting them one at a
277    * time.
278    *
279    * @throws Exception
280    */

281   public void testRemoveChannelsFromGroup() throws Exception JavaDoc {
282     PersistChanGrpMgr theGrp;
283     int count = 5;
284     theGrp = makeEmptyGroup("The Group");
285     addGenChannels(theGrp, count, 5);
286
287     for (int i = 0; i < count; i++) {
288       ChannelBuilder bld = theGrp.getBuilder();
289       bld.beginTransaction();
290       bld.update(theGrp.getChannelGroup());
291
292       // Check that we have the right number of Channels
293
assertValidGroup(theGrp, "The Group", count - i);
294
295       // Identify the first channel after ensuring that there is at least one.
296
Channel channelToDelete;
297       Collection JavaDoc chans = theGrp.getChannelGroup().getChannels();
298       Iterator JavaDoc iter = chans.iterator();
299       assertTrue(chans.size() > 0);
300       channelToDelete = (Channel) iter.next();
301
302       bld.endTransaction();
303
304       // And delete it from the group
305
theGrp.deleteChannel(channelToDelete);
306     }
307   }
308
309   /**
310    * Test activation and deactivation, with thread handling too/
311    *
312    * @throws Exception
313    */

314   public void testStartStopGroup() throws Exception JavaDoc {
315     PersistChanGrpMgr groupStopStart;
316     groupStopStart = makeEmptyGroup("stopStartGroup");
317
318     // Start and stop a few times
319
for (int i = 0; i < 2; i++) {
320       groupStopStart.activate();
321       assertTrue("Channel Group is supposed to be activated", groupStopStart.isActivated());
322       groupStopStart.deActivate(true);
323       assertFalse("ChannelGroup is not supposed to be activated", groupStopStart.isActivated());
324       Thread.sleep(500);
325     }
326   }
327   
328   /**
329    * Check to see that itemCount method on PersistChanGrp works correctly.
330    *
331    * @throws Exception
332    */

333   public void testItemCount() throws Exception JavaDoc {
334     // Create a ChannelGroup and populate it with channels and items
335
PersistChanGrpMgr countedGrp;
336     countedGrp = makeEmptyGroup("Item Count Group");
337     int count = 10;
338     addGenChannels(countedGrp, count, 5);
339
340     Object JavaDoc[] channels = countedGrp.getChannelGroup().getChannels().toArray();
341     assertEquals("Wrong number of channels in group", channels.length, count);
342
343     // Iterate through all the new channels
344
for (int i = 0; i < count; i++) {
345       Channel aChan = (Channel) channels[i];
346       Object JavaDoc[] items = aChan.getItems().toArray();
347       int howManyItems = items.length;
348       assertEquals("Wrong Count returned", howManyItems, countedGrp.getItemCount(aChan));
349     }
350   }
351
352
353
354   // ---------------------------------------------------------------------
355
// Internal helper methods
356
// ---------------------------------------------------------------------
357

358   /**
359    * Verify that a specified group has the expected number of Channels as well as the expected name.
360    *
361    * @param gp - Group to check
362    * @param name - expected name
363    * @param size - expected Channels.
364    * @exception Exception (caught by JUnit)
365    */

366
367   protected void assertValidGroup(PersistChanGrpMgr gp, String JavaDoc name, int size) throws Exception JavaDoc {
368     ChannelGroup gpc = gp.getChannelGroup();
369     assertEquals(gpc.getTitle(), name);
370     assertEquals(size, gpc.getChannels().size());
371   }
372   
373
374   /**
375    * Helper to add a certain number of channels to a group. Note this uses the handy-dandy
376    * RssUrlTestData to give us useful RSS urls.
377    *
378    * @param aGroup what group to add to
379    * @param count how many channels to use
380    * @param taskRuns home many times to 'pump' the PersistChanGrpTask
381    */

382   private void addGenChannels(PersistChanGrpMgr aGroup, int count, int taskRuns) {
383     int i;
384     for (i = 0; i < count; i++) {
385       Channel achannel;
386       achannel = aGroup.addChannel(RssUrlTestData.generate());
387       aGroup.notifyChannelsAndItems(achannel);
388     }
389     runPersistChanGrpTask(aGroup, taskRuns);
390   }
391
392   /**
393    * Helper to create an empty group
394    *
395    * @param name - Name to use
396    * @return created empty group
397    */

398   public PersistChanGrpMgr makeEmptyGroup(String JavaDoc name) {
399     PersistChanGrpMgr res;
400     logger.info("Creating group: " + name);
401     res = new PersistChanGrpMgr(handler, false);
402     res.createGroup(name);
403     logger.info("Result: " + res);
404     assertEquals("Newly created group has non-zero Channels", 0, res.getChannelGroup()
405         .getChannels().size());
406     return res;
407   }
408
409   /**
410    * Helper to add a channel to an existing group
411    *
412    * @param agroup - group to receive them
413    * @param label - label to use for tracing
414    * @param url - url to give to informa
415    */

416   private void addChannel(PersistChanGrpMgr agroup, String JavaDoc label, String JavaDoc url) {
417     Channel achannel;
418     logger.info("Adding channel: " + label + " to " + agroup + " " + url);
419     achannel = agroup.addChannel(url);
420     agroup.notifyChannelsAndItems(achannel);
421     runPersistChanGrpTask(agroup, 3);
422   }
423
424   /**
425    * Run the PersistChanGrpTask n times for specified PersistChanGrpMgr. This does *NOT* Check
426    * threading bugs but it does check the functionality of the background task.
427    *
428    * @param mgr - Persistent group manager
429    * @param count - How many times to manually run PersistenChanGrpMgr
430    */

431
432   protected void runPersistChanGrpTask(PersistChanGrpMgr mgr, int count) {
433     PersistChanGrpMgrTask task = new PersistChanGrpMgrTask(mgr, 0);
434     for (int i = 0; i < count; i++) {
435       task.performUpdates();
436     }
437   }
438
439 }
Popular Tags