1 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 ; 38 import java.net.URL ; 39 40 44 public class TestPollerWorkerThread extends TestCase { 45 private PersistenceManagerIF pm = new PersistenceManager(); 46 47 52 public void testCheckItemsEmpty() { 53 final PollerObserverIF o = new CustomObserver(); 55 final PollerApproverIF a = new CustomApprover("wanted"); 56 final PollerWorkerThread worker = new PollerWorkerThread(o, a); 57 58 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 worker.checkItems(cTemp, cMain); 67 68 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 82 public void testCheckItemsDuplicate() { 83 final PollerObserverIF o = new CustomObserver(); 85 final PollerApproverIF a = new CustomApprover("wanted"); 86 final PollerWorkerThread worker = new PollerWorkerThread(o, a); 87 88 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 final ItemIF item3 = pm.createItem(cMain, "wanted"); 97 98 worker.checkItems(cTemp, cMain); 100 101 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 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 144 private static URL getTestURL() { 145 URL url = null; 146 147 try { 148 url = new URL ("http://www.test.org"); 149 } catch (MalformedURLException e) { 150 e.printStackTrace(); 151 } 152 153 return url; 154 } 155 156 159 private static class CustomObserver implements PollerObserverIF { 160 167 public void itemFound(ItemIF item, ChannelIF channel) { 168 channel.addItem(item); 169 } 170 171 177 public void channelErrored(ChannelIF channel, Exception e) { 178 } 179 180 185 public void channelChanged(ChannelIF channel) { 186 } 187 188 193 public void pollStarted(ChannelIF channel) { 194 } 195 196 201 public void pollFinished(ChannelIF channel) { 202 } 203 } 204 205 208 private static class CustomApprover implements PollerApproverIF { 209 private String title; 210 211 216 public CustomApprover(String itemTitle) { 217 title = itemTitle; 218 } 219 220 227 public boolean canAddItem(ItemIF item, ChannelIF channel) { 228 return title.equals(item.getTitle()); 229 } 230 } 231 } 232 | Popular Tags |