1 26 28 package de.nava.informa.impl.hibernate; 29 30 import java.util.Collection ; 31 import java.util.Iterator ; 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 48 public class TestPersistChannelGrp extends InformaTestCase { 49 50 private SessionHandler handler; 51 52 private static Log logger = LogFactory.getLog(TestPersistChannelGrp.class); 53 54 59 public TestPersistChannelGrp(String name) { 60 super("TestPersistChannelGrp", name); 61 } 62 63 68 protected void setUp() throws HibernateException { 69 handler = SessionHandler.getInstance(); 70 handler.getSession(); } 72 73 79 public void testEmptyGroups() throws Exception { 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 94 public void testGroupWithChannels() throws Exception { 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 114 public void testAddingExistingChannel() throws Exception { 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 addChannel(agroup, "Terrestial", "http://www.transterrestrial.com/index.xml"); 127 assertValidGroup(agroup, "A Group", 6); 128 129 addChannel(bgroup, "Terrestial", "http://www.transterrestrial.com/index.xml"); 131 assertValidGroup(bgroup, "B Group", 6); 132 } 133 134 139 public void testChannelMembership() throws Exception { 140 final PersistChanGrpMgr agroup; 141 agroup = makeEmptyGroup("Membership Group"); 142 addGenChannels(agroup, 3, 3); 143 144 final Iterator 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 156 public void testGroupDeactivate() throws Exception { 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 216 public void testMoveChannelBetweenGroups() throws Exception { 217 PersistChanGrpMgr sourceGrp, destGrp; 219 sourceGrp = makeEmptyGroup("Source Group"); 220 destGrp = makeEmptyGroup("Destination Group"); 221 222 int count = 5; 224 int sourceHas = count; 225 int destHas = 0; 226 addGenChannels(sourceGrp, count, 3); 227 228 for (int i = 0; i < count; i++) { 230 assertValidGroup(sourceGrp, "Source Group", sourceHas - i); 232 assertValidGroup(destGrp, "Destination Group", destHas + i); 233 234 Channel channelToMove; 236 Collection chans = sourceGrp.getChannelGroup().getChannels(); 237 Iterator iter = chans.iterator(); 238 assertTrue(chans.size() > 0); 239 channelToMove = (Channel) iter.next(); 240 sourceGrp.moveChannelTo(channelToMove, destGrp); 241 } 242 } 243 244 249 public void testDeleteItemsFromChannels() throws Exception { 250 PersistChanGrpMgr delGrp; 252 delGrp = makeEmptyGroup("deleteTest Group"); 253 int count = 10; 254 addGenChannels(delGrp, count, 5); 255 256 Object [] channels = delGrp.getChannelGroup().getChannels().toArray(); 257 assertEquals("Wrong number of channels in group", channels.length, count); 258 259 for (int i = 0; i < count; i++) { 261 Channel aChan = (Channel) channels[i]; 262 Object [] items = aChan.getItems().toArray(); 263 int howManyItems = items.length; 264 265 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 281 public void testRemoveChannelsFromGroup() throws Exception { 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 assertValidGroup(theGrp, "The Group", count - i); 294 295 Channel channelToDelete; 297 Collection chans = theGrp.getChannelGroup().getChannels(); 298 Iterator iter = chans.iterator(); 299 assertTrue(chans.size() > 0); 300 channelToDelete = (Channel) iter.next(); 301 302 bld.endTransaction(); 303 304 theGrp.deleteChannel(channelToDelete); 306 } 307 } 308 309 314 public void testStartStopGroup() throws Exception { 315 PersistChanGrpMgr groupStopStart; 316 groupStopStart = makeEmptyGroup("stopStartGroup"); 317 318 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 333 public void testItemCount() throws Exception { 334 PersistChanGrpMgr countedGrp; 336 countedGrp = makeEmptyGroup("Item Count Group"); 337 int count = 10; 338 addGenChannels(countedGrp, count, 5); 339 340 Object [] channels = countedGrp.getChannelGroup().getChannels().toArray(); 341 assertEquals("Wrong number of channels in group", channels.length, count); 342 343 for (int i = 0; i < count; i++) { 345 Channel aChan = (Channel) channels[i]; 346 Object [] items = aChan.getItems().toArray(); 347 int howManyItems = items.length; 348 assertEquals("Wrong Count returned", howManyItems, countedGrp.getItemCount(aChan)); 349 } 350 } 351 352 353 354 358 366 367 protected void assertValidGroup(PersistChanGrpMgr gp, String name, int size) throws Exception { 368 ChannelGroup gpc = gp.getChannelGroup(); 369 assertEquals(gpc.getTitle(), name); 370 assertEquals(size, gpc.getChannels().size()); 371 } 372 373 374 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 398 public PersistChanGrpMgr makeEmptyGroup(String 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 416 private void addChannel(PersistChanGrpMgr agroup, String label, String 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 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 |