001: //
002: // Informa -- RSS Library for Java
003: // Copyright (c) 2002, 2003 by Niko Schmuck
004: //
005: // Niko Schmuck
006: // http://sourceforge.net/projects/informa
007: // mailto:niko_schmuck@users.sourceforge.net
008: //
009: // This library is free software.
010: //
011: // You may redistribute it and/or modify it under the terms of the GNU
012: // Lesser General Public License as published by the Free Software Foundation.
013: //
014: // Version 2.1 of the license should be included with this distribution in
015: // the file LICENSE. If the license is not included with this distribution,
016: // you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
017: // or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
018: // MA 02139 USA.
019: //
020: // This library is distributed in the hope that it will be useful,
021: // but WITHOUT ANY WARRANTY; without even the implied waranty of
022: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
023: // Lesser General Public License for more details.
024: //
025:
026: // $Id: TestPersistChannelGrp.java,v 1.9 2005/09/24 22:35:15 niko_schmuck Exp $
027:
028: package de.nava.informa.impl.hibernate;
029:
030: import java.util.Collection;
031: import java.util.Iterator;
032:
033: import org.hibernate.HibernateException;
034:
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037:
038: import de.nava.informa.utils.InformaTestCase;
039: import de.nava.informa.utils.PersistChanGrpMgr;
040: import de.nava.informa.utils.PersistChanGrpMgrTask;
041: import de.nava.informa.utils.RssUrlTestData;
042:
043: /**
044: * Tests for the PersistChanGrpMgr and PersistChanGrpMgrTask classes
045: *
046: * @author Pito Salas
047: */
048: public class TestPersistChannelGrp extends InformaTestCase {
049:
050: private SessionHandler handler;
051:
052: private static Log logger = LogFactory
053: .getLog(TestPersistChannelGrp.class);
054:
055: /**
056: * Constructor.
057: *
058: * @param name Name of the JUnit Test
059: */
060: public TestPersistChannelGrp(String name) {
061: super ("TestPersistChannelGrp", name);
062: }
063:
064: /**
065: * Common setup for every single test.
066: *
067: * @throws HibernateException
068: */
069: protected void setUp() throws HibernateException {
070: handler = SessionHandler.getInstance();
071: handler.getSession(); // make sure the session is opened
072: }
073:
074: /**
075: * A battery of empty groups
076: *
077: * @exception Exception (Caught by JUnit)
078: *
079: */
080: public void testEmptyGroups() throws Exception {
081: PersistChanGrpMgr agroup, bgroup, cgroup;
082: agroup = makeEmptyGroup("Group Foo");
083: bgroup = makeEmptyGroup("Group Bar");
084: cgroup = makeEmptyGroup("Group Too");
085: assertValidGroup(agroup, "Group Foo", 0);
086: assertValidGroup(bgroup, "Group Bar", 0);
087: assertValidGroup(cgroup, "Group Too", 0);
088: }
089:
090: /**
091: * Create a group, and add some channels and see if all is well
092: *
093: * @exception Exception (Caught by JUnit)
094: */
095: public void testGroupWithChannels() throws Exception {
096: PersistChanGrpMgr agroup;
097: agroup = makeEmptyGroup("Group Secret With Channels");
098: assertValidGroup(agroup, "Group Secret With Channels", 0);
099:
100: addChannel(agroup, "Joho",
101: "http://www.hyperorg.com/blogger/index.rdf");
102: addChannel(agroup, "Raliable",
103: "http://www.raibledesigns.com/rss/rd");
104: addChannel(agroup, "Active Window",
105: "http://www.activewin.com/awin/headlines.rss");
106: addChannel(agroup, "Pitos Blog",
107: "http://radio.weblogs.com/0125664/rss.xml");
108:
109: runPersistChanGrpTask(agroup, 3);
110: assertValidGroup(agroup, "Group Secret With Channels", 4);
111: }
112:
113: /**
114: * Test adding a Channel to a group which already exists in a different group, which should just
115: * work.
116: *
117: * @throws Exception
118: */
119: public void testAddingExistingChannel() throws Exception {
120: final PersistChanGrpMgr agroup;
121: agroup = makeEmptyGroup("A Group");
122: assertValidGroup(agroup, "A Group", 0);
123: addGenChannels(agroup, 5, 3);
124:
125: final PersistChanGrpMgr bgroup;
126: bgroup = makeEmptyGroup("B Group");
127: assertValidGroup(bgroup, "B Group", 0);
128: addGenChannels(bgroup, 5, 3);
129:
130: // Add a certain channel to this group
131: addChannel(agroup, "Terrestial",
132: "http://www.transterrestrial.com/index.xml");
133: assertValidGroup(agroup, "A Group", 6);
134:
135: // and now add the SAME channel to a different group. make sure that it gets there
136: addChannel(bgroup, "Terrestial",
137: "http://www.transterrestrial.com/index.xml");
138: assertValidGroup(bgroup, "B Group", 6);
139: }
140:
141: /**
142: * Test the functioning of the hasChannel() method.
143: *
144: * @throws Exception
145: */
146: public void testChannelMembership() throws Exception {
147: final PersistChanGrpMgr agroup;
148: agroup = makeEmptyGroup("Membership Group");
149: addGenChannels(agroup, 3, 3);
150:
151: final Iterator channels = agroup.getChannelGroup()
152: .getChannels().iterator();
153: while (channels.hasNext()) {
154: final Channel nextchan = (Channel) channels.next();
155: assertTrue("Expected member channel not found", agroup
156: .hasChannel(nextchan));
157: }
158: }
159:
160: /**
161: * Test activation and deactivation of PersistChanGrpMgrs
162: *
163: * @throws Exception
164: */
165: public void testGroupDeactivate() throws Exception {
166: final PersistChanGrpMgr group1, group2, group3;
167: group1 = makeEmptyGroup("Activation Group 1");
168: group2 = makeEmptyGroup("Activation Group 2");
169: group3 = makeEmptyGroup("Activation Group 3");
170:
171: addGenChannels(group1, 5, 3);
172: addGenChannels(group2, 5, 3);
173: addGenChannels(group3, 5, 3);
174:
175: assertFalse("expected group 1 to be inactive", group1
176: .isActivated());
177: assertFalse("expected group 2 to be inactive", group2
178: .isActivated());
179: assertFalse("expected group 3 to be inactive", group3
180: .isActivated());
181:
182: group1.activate();
183: assertTrue("expected group 1 to be active", group1
184: .isActivated());
185: assertFalse("expected group 2 to be inactive", group2
186: .isActivated());
187: assertFalse("expected group 3 to be inactive", group3
188: .isActivated());
189:
190: Thread.sleep(500);
191:
192: group2.activate();
193: assertTrue("expected group 1 to be active", group1
194: .isActivated());
195: assertTrue("expected group 2 to be active", group2
196: .isActivated());
197: assertFalse("expected group 3 to be inactive", group3
198: .isActivated());
199:
200: Thread.sleep(500);
201:
202: group3.activate();
203: assertTrue("expected group 1 to be active", group1
204: .isActivated());
205: assertTrue("expected group 2 to be active", group2
206: .isActivated());
207: assertTrue("expected group 3 to be active", group3
208: .isActivated());
209:
210: Thread.sleep(500);
211:
212: group1.deActivate(true);
213: assertFalse("expected group 1 to be inactive", group1
214: .isActivated());
215: assertTrue("expected group 2 to be active", group2
216: .isActivated());
217: assertTrue("expected group 3 to be active", group3
218: .isActivated());
219:
220: Thread.sleep(500);
221:
222: group2.deActivate(true);
223: assertFalse("expected group 1 to be inactive", group1
224: .isActivated());
225: assertFalse("expected group 2 to be inactive", group2
226: .isActivated());
227: assertTrue("expected group 3 to be active", group3
228: .isActivated());
229:
230: Thread.sleep(500);
231:
232: group3.deActivate(true);
233: assertFalse("expected group 1 to be inactive", group1
234: .isActivated());
235: assertFalse("expected group 2 to be inactive", group2
236: .isActivated());
237: assertFalse("expected group 3 to be inactive", group3
238: .isActivated());
239: }
240:
241: /**
242: * Test moving a Channels from one group to another
243: *
244: * @throws Exception
245: */
246: public void testMoveChannelBetweenGroups() throws Exception {
247: // Create the two groups
248: PersistChanGrpMgr sourceGrp, destGrp;
249: sourceGrp = makeEmptyGroup("Source Group");
250: destGrp = makeEmptyGroup("Destination Group");
251:
252: // Populate source group with some channels.
253: int count = 5;
254: int sourceHas = count;
255: int destHas = 0;
256: addGenChannels(sourceGrp, count, 3);
257:
258: // Now move them one by one to the other ChannelGroup and verify the counts as we go
259: for (int i = 0; i < count; i++) {
260: // Verify that each group has the expected number of channels
261: assertValidGroup(sourceGrp, "Source Group", sourceHas - i);
262: assertValidGroup(destGrp, "Destination Group", destHas + i);
263:
264: // Identify the first channel after ensuring that there is at least one.
265: Channel channelToMove;
266: Collection chans = sourceGrp.getChannelGroup()
267: .getChannels();
268: Iterator iter = chans.iterator();
269: assertTrue(chans.size() > 0);
270: channelToMove = (Channel) iter.next();
271: sourceGrp.moveChannelTo(channelToMove, destGrp);
272: }
273: }
274:
275: /**
276: * Test deleting individual items from Channels in a PersistChanGrp
277: *
278: * @throws Exception
279: */
280: public void testDeleteItemsFromChannels() throws Exception {
281: // Create a ChannelGroup and populate it with channels and items
282: PersistChanGrpMgr delGrp;
283: delGrp = makeEmptyGroup("deleteTest Group");
284: int count = 10;
285: addGenChannels(delGrp, count, 5);
286:
287: Object[] channels = delGrp.getChannelGroup().getChannels()
288: .toArray();
289: assertEquals("Wrong number of channels in group",
290: channels.length, count);
291:
292: // Iterate through all the new channels
293: for (int i = 0; i < count; i++) {
294: Channel aChan = (Channel) channels[i];
295: Object[] items = aChan.getItems().toArray();
296: int howManyItems = items.length;
297:
298: // Now iterate through all the items of this channel, and delete them one by one
299: for (int j = 0; j < howManyItems; j++) {
300: Item anItem = (Item) items[j];
301: assertEquals("Wrong number of items after delete",
302: howManyItems - j, aChan.getItems().size());
303: delGrp.deleteItemFromChannel(aChan, anItem);
304: }
305: }
306: }
307:
308: /**
309: * Test deleting channels from a group by adding a certain number, and then deleting them one at a
310: * time.
311: *
312: * @throws Exception
313: */
314: public void testRemoveChannelsFromGroup() throws Exception {
315: PersistChanGrpMgr theGrp;
316: int count = 5;
317: theGrp = makeEmptyGroup("The Group");
318: addGenChannels(theGrp, count, 5);
319:
320: for (int i = 0; i < count; i++) {
321: ChannelBuilder bld = theGrp.getBuilder();
322: bld.beginTransaction();
323: bld.update(theGrp.getChannelGroup());
324:
325: // Check that we have the right number of Channels
326: assertValidGroup(theGrp, "The Group", count - i);
327:
328: // Identify the first channel after ensuring that there is at least one.
329: Channel channelToDelete;
330: Collection chans = theGrp.getChannelGroup().getChannels();
331: Iterator iter = chans.iterator();
332: assertTrue(chans.size() > 0);
333: channelToDelete = (Channel) iter.next();
334:
335: bld.endTransaction();
336:
337: // And delete it from the group
338: theGrp.deleteChannel(channelToDelete);
339: }
340: }
341:
342: /**
343: * Test activation and deactivation, with thread handling too/
344: *
345: * @throws Exception
346: */
347: public void testStartStopGroup() throws Exception {
348: PersistChanGrpMgr groupStopStart;
349: groupStopStart = makeEmptyGroup("stopStartGroup");
350:
351: // Start and stop a few times
352: for (int i = 0; i < 2; i++) {
353: groupStopStart.activate();
354: assertTrue("Channel Group is supposed to be activated",
355: groupStopStart.isActivated());
356: groupStopStart.deActivate(true);
357: assertFalse("ChannelGroup is not supposed to be activated",
358: groupStopStart.isActivated());
359: Thread.sleep(500);
360: }
361: }
362:
363: /**
364: * Check to see that itemCount method on PersistChanGrp works correctly.
365: *
366: * @throws Exception
367: */
368: public void testItemCount() throws Exception {
369: // Create a ChannelGroup and populate it with channels and items
370: PersistChanGrpMgr countedGrp;
371: countedGrp = makeEmptyGroup("Item Count Group");
372: int count = 10;
373: addGenChannels(countedGrp, count, 5);
374:
375: Object[] channels = countedGrp.getChannelGroup().getChannels()
376: .toArray();
377: assertEquals("Wrong number of channels in group",
378: channels.length, count);
379:
380: // Iterate through all the new channels
381: for (int i = 0; i < count; i++) {
382: Channel aChan = (Channel) channels[i];
383: Object[] items = aChan.getItems().toArray();
384: int howManyItems = items.length;
385: assertEquals("Wrong Count returned", howManyItems,
386: countedGrp.getItemCount(aChan));
387: }
388: }
389:
390: // ---------------------------------------------------------------------
391: // Internal helper methods
392: // ---------------------------------------------------------------------
393:
394: /**
395: * Verify that a specified group has the expected number of Channels as well as the expected name.
396: *
397: * @param gp - Group to check
398: * @param name - expected name
399: * @param size - expected Channels.
400: * @exception Exception (caught by JUnit)
401: */
402:
403: protected void assertValidGroup(PersistChanGrpMgr gp, String name,
404: int size) throws Exception {
405: ChannelGroup gpc = gp.getChannelGroup();
406: assertEquals(gpc.getTitle(), name);
407: assertEquals(size, gpc.getChannels().size());
408: }
409:
410: /**
411: * Helper to add a certain number of channels to a group. Note this uses the handy-dandy
412: * RssUrlTestData to give us useful RSS urls.
413: *
414: * @param aGroup what group to add to
415: * @param count how many channels to use
416: * @param taskRuns home many times to 'pump' the PersistChanGrpTask
417: */
418: private void addGenChannels(PersistChanGrpMgr aGroup, int count,
419: int taskRuns) {
420: int i;
421: for (i = 0; i < count; i++) {
422: Channel achannel;
423: achannel = aGroup.addChannel(RssUrlTestData.generate());
424: aGroup.notifyChannelsAndItems(achannel);
425: }
426: runPersistChanGrpTask(aGroup, taskRuns);
427: }
428:
429: /**
430: * Helper to create an empty group
431: *
432: * @param name - Name to use
433: * @return created empty group
434: */
435: public PersistChanGrpMgr makeEmptyGroup(String name) {
436: PersistChanGrpMgr res;
437: logger.info("Creating group: " + name);
438: res = new PersistChanGrpMgr(handler, false);
439: res.createGroup(name);
440: logger.info("Result: " + res);
441: assertEquals("Newly created group has non-zero Channels", 0,
442: res.getChannelGroup().getChannels().size());
443: return res;
444: }
445:
446: /**
447: * Helper to add a channel to an existing group
448: *
449: * @param agroup - group to receive them
450: * @param label - label to use for tracing
451: * @param url - url to give to informa
452: */
453: private void addChannel(PersistChanGrpMgr agroup, String label,
454: String url) {
455: Channel achannel;
456: logger.info("Adding channel: " + label + " to " + agroup + " "
457: + url);
458: achannel = agroup.addChannel(url);
459: agroup.notifyChannelsAndItems(achannel);
460: runPersistChanGrpTask(agroup, 3);
461: }
462:
463: /**
464: * Run the PersistChanGrpTask n times for specified PersistChanGrpMgr. This does *NOT* Check
465: * threading bugs but it does check the functionality of the background task.
466: *
467: * @param mgr - Persistent group manager
468: * @param count - How many times to manually run PersistenChanGrpMgr
469: */
470:
471: protected void runPersistChanGrpTask(PersistChanGrpMgr mgr,
472: int count) {
473: PersistChanGrpMgrTask task = new PersistChanGrpMgrTask(mgr, 0);
474: for (int i = 0; i < count; i++) {
475: task.performUpdates();
476: }
477: }
478:
479: }
|