1 17 package com.sun.syndication.fetcher.impl; 18 19 import java.net.URL ; 20 21 import junit.framework.TestCase; 22 23 import org.mortbay.http.BasicAuthenticator; 24 import org.mortbay.http.HashUserRealm; 25 import org.mortbay.http.HttpContext; 26 import org.mortbay.http.HttpServer; 27 import org.mortbay.http.SecurityConstraint; 28 import org.mortbay.http.SocketListener; 29 import org.mortbay.http.UserRealm; 30 import org.mortbay.http.handler.SecurityHandler; 31 import org.mortbay.jetty.servlet.ServletHandler; 32 33 import com.sun.syndication.feed.synd.SyndEntry; 34 import com.sun.syndication.feed.synd.SyndFeed; 35 import com.sun.syndication.fetcher.FeedFetcher; 36 import com.sun.syndication.fetcher.FetcherEvent; 37 import com.sun.syndication.fetcher.FetcherException; 38 import com.sun.syndication.fetcher.FetcherListener; 39 40 43 public abstract class AbstractJettyTest extends TestCase { 44 45 private HttpServer server; 46 47 50 public AbstractJettyTest(String s) { 51 super(s); 52 } 53 54 protected HttpServer getServer() { 55 return server; 56 } 57 58 protected abstract FeedFetcher getFeedFetcher(); 59 60 protected abstract FeedFetcher getFeedFetcher(FeedFetcherCache cache); 61 62 65 protected void setUp() throws Exception { 66 setupServer(); 67 68 HttpContext context = createContext(); 69 70 ServletHandler servlets = createServletHandler(); 71 context.addHandler(servlets); 72 73 server.addContext(context); 74 75 server.start(); 76 } 77 78 81 private void setupServer() throws InterruptedException { 82 if (server != null) { 84 server.stop(); 85 server = null; 86 } 87 server = new HttpServer(); 88 89 SocketListener listener=new SocketListener(); 91 listener.setPort(8080); 92 server.addListener(listener); 93 } 94 95 98 private ServletHandler createServletHandler() { 99 ServletHandler servlets = new ServletHandler(); 100 servlets.addServlet("FetcherTestServlet","/FetcherTestServlet/*","com.sun.syndication.fetcher.impl.FetcherTestServlet"); 101 return servlets; 102 } 103 104 107 private HttpContext createContext() { 108 HttpContext context = new HttpContext(); 109 context.setContextPath("/rome/*"); 110 return context; 111 } 112 113 116 protected void tearDown() throws Exception { 117 if (server != null) { 118 server.stop(); 119 server.destroy(); 120 server = null; 121 } 122 } 123 124 class FetcherEventListenerImpl implements FetcherListener { 125 boolean polled = false; 126 boolean retrieved = false; 127 boolean unchanged = false; 128 129 public void reset() { 130 polled = false; 131 retrieved = false; 132 unchanged = false; 133 } 134 135 138 public void fetcherEvent(FetcherEvent event) { 139 String eventType = event.getEventType(); 140 if (FetcherEvent.EVENT_TYPE_FEED_POLLED.equals(eventType)) { 141 System.err.println("\tEVENT: Feed Polled. URL = " + event.getUrlString()); 142 polled = true; 143 } else if (FetcherEvent.EVENT_TYPE_FEED_RETRIEVED.equals(eventType)) { 144 System.err.println("\tEVENT: Feed Retrieved. URL = " + event.getUrlString()); 145 retrieved = true; 146 } else if (FetcherEvent.EVENT_TYPE_FEED_UNCHANGED.equals(eventType)) { 147 System.err.println("\tEVENT: Feed Unchanged. URL = " + event.getUrlString()); 148 unchanged = true; 149 } 150 } 151 } 152 153 public void testRetrieveFeed() { 154 FeedFetcher feedFetcher = getFeedFetcher(); 155 try { 156 SyndFeed feed = feedFetcher.retrieveFeed(new URL ("http://localhost:8080/rome/FetcherTestServlet/")); 157 assertNotNull(feed); 158 assertEquals("atom_0.3.feed.title", feed.getTitle()); 159 } catch (Exception e) { 160 e.printStackTrace(); 161 fail(e.getMessage()); 162 } 163 } 164 165 public void testBasicAuthentication() { 166 try { 167 setupServer(); 168 169 HttpContext context = createContext(); 170 171 URL url = this.getClass().getResource("/testuser.properties"); 172 UserRealm ur = new HashUserRealm("test", url.getFile()); 173 context.setRealm(ur); 174 175 BasicAuthenticator ba = new BasicAuthenticator(); 176 context.setAuthenticator(ba); 177 178 SecurityHandler sh = new SecurityHandler(); 179 context.addHandler(sh); 180 181 SecurityConstraint sc = new SecurityConstraint(); 182 sc.setName("test"); 183 sc.addRole("*"); 184 sc.setAuthenticate(true); 185 context.addSecurityConstraint("/", sc); 186 187 ServletHandler servlets = createServletHandler(); 188 context.addHandler(servlets); 189 190 server.addContext(context); 191 192 server.start(); 193 194 FeedFetcher feedFetcher = getAuthenticatedFeedFetcher(); 195 SyndFeed feed = feedFetcher.retrieveFeed(new URL ("http://localhost:8080/rome/FetcherTestServlet/")); 196 assertNotNull(feed); 197 assertEquals("atom_0.3.feed.title", feed.getTitle()); 198 199 200 } catch (Exception e) { 201 e.printStackTrace(); 202 fail(e.getMessage()); 203 } 204 205 206 } 207 208 public abstract FeedFetcher getAuthenticatedFeedFetcher(); 209 210 214 public void testRetrieveRedirectedFeed() { 215 FeedFetcher feedFetcher = getFeedFetcher(); 216 try { 217 SyndFeed feed = feedFetcher.retrieveFeed(new URL ("http://localhost:8080/rome/FetcherTestServlet?redirect=TRUE")); 218 assertNotNull(feed); 219 assertEquals("atom_0.3.feed.title", feed.getTitle()); 220 } catch (Exception e) { 221 e.printStackTrace(); 222 fail(e.getMessage()); 223 } 224 } 225 226 230 public void testErrorHandling() { 231 FeedFetcher feedFetcher = getFeedFetcher(); 232 try { 233 SyndFeed feed = feedFetcher.retrieveFeed(new URL ("http://localhost:8080/rome/FetcherTestServlet?error=404")); 234 fail("4xx error handling did not work correctly"); 235 } catch (FetcherException e) { 236 assertEquals(404, e.getResponseCode()); 238 } catch (Exception e) { 239 e.printStackTrace(); 240 fail(e.getMessage()); 241 } 242 243 try { 244 SyndFeed feed = feedFetcher.retrieveFeed(new URL ("http://localhost:8080/rome/FetcherTestServlet?error=500")); 245 fail("5xx error handling did not work correctly"); 246 } catch (FetcherException e) { 247 assertEquals(500, e.getResponseCode()); 249 } catch (Exception e) { 250 e.printStackTrace(); 251 fail(e.getMessage()); 252 } 253 } 254 255 public void testUserAgent() { 256 FeedFetcher feedFetcher = getFeedFetcher(); 257 assertEquals("Rome Client (http://tinyurl.com/64t5n) Ver: " + System.getProperty("rome.fetcher.version", "UNKNOWN"), feedFetcher.getUserAgent()); 260 } 261 262 266 public void testFetchEvents() { 267 FeedFetcher feedFetcher = getFeedFetcher(); 268 FetcherEventListenerImpl listener = new FetcherEventListenerImpl(); 269 feedFetcher.addFetcherEventListener(listener); 270 try { 271 SyndFeed feed = feedFetcher.retrieveFeed(new URL ("http://localhost:8080/rome/FetcherTestServlet/")); 272 assertNotNull(feed); 273 assertTrue(listener.polled); 274 assertTrue(listener.retrieved); 275 assertFalse(listener.unchanged); 276 listener.reset(); 277 278 feed = feedFetcher.retrieveFeed(new URL ("http://localhost:8080/rome/FetcherTestServlet/")); 281 assertNotNull(feed); 282 assertTrue(listener.polled); 283 assertTrue(listener.retrieved); 284 assertFalse(listener.unchanged); 285 listener.reset(); 286 } catch (Exception e) { 287 e.printStackTrace(); 288 fail(e.getMessage()); 289 } 290 } 291 292 296 public void testFetchEventsWithCache() { 297 FeedFetcherCache feedInfoCache = new HashMapFeedInfoCache(); 298 FeedFetcher feedFetcher = getFeedFetcher(feedInfoCache); 299 FetcherEventListenerImpl listener = new FetcherEventListenerImpl(); 300 feedFetcher.addFetcherEventListener(listener); 301 try { 302 SyndFeed feed = feedFetcher.retrieveFeed(new URL ("http://localhost:8080/rome/FetcherTestServlet/")); 303 assertNotNull(feed); 304 assertTrue(listener.polled); 305 assertTrue(listener.retrieved); 306 assertFalse(listener.unchanged); 307 listener.reset(); 308 309 feed = feedFetcher.retrieveFeed(new URL ("http://localhost:8080/rome/FetcherTestServlet/")); 312 assertNotNull(feed); 313 assertTrue(listener.polled); 314 assertFalse(listener.retrieved); 315 assertTrue(listener.unchanged); 316 listener.reset(); 317 318 feed = feedFetcher.retrieveFeed(new URL ("http://localhost:8080/rome/FetcherTestServlet?refreshfeed=TRUE")); 320 assertNotNull(feed); 321 assertTrue(listener.polled); 322 assertTrue(listener.retrieved); 323 assertFalse(listener.unchanged); 324 listener.reset(); 325 } catch (Exception e) { 326 e.printStackTrace(); 327 fail(e.getMessage()); 328 } 329 } 330 331 335 public void testGZippedFeed() { 336 FeedFetcher feedFetcher = getFeedFetcher(); 337 try { 338 SyndFeed feed = feedFetcher.retrieveFeed(new URL ("http://localhost:8080/rome/FetcherTestServlet?gzipfeed=TRUE")); 339 assertNotNull(feed); 340 assertEquals("atom_0.3.feed.title", feed.getTitle()); 341 } catch (Exception e) { 342 e.printStackTrace(); 343 fail(e.getMessage()); 344 } 345 } 346 347 public void testDeltaEncoding() { 348 FeedFetcherCache feedInfoCache = new HashMapFeedInfoCache(); 349 FeedFetcher feedFetcher = getFeedFetcher(feedInfoCache); 350 try { 351 feedFetcher.setUsingDeltaEncoding(true); 352 353 SyndFeed feed1 = feedFetcher.retrieveFeed(new URL ("http://localhost:8080/rome/FetcherTestServlet?deltaencode=TRUE&refreshfeed=TRUE")); 355 assertNotNull(feed1); 356 assertEquals("atom_0.3.feed.title", feed1.getTitle()); 357 assertEquals(2, feed1.getEntries().size()); 358 SyndEntry entry1 = (SyndEntry) feed1.getEntries().get(0); 359 assertEquals("atom_0.3.feed.entry[0].title", entry1.getTitle()); 360 361 365 SyndFeed feed2 = feedFetcher.retrieveFeed(new URL ("http://localhost:8080/rome/FetcherTestServlet?deltaencode=TRUE&refreshfeed=TRUE")); 366 assertNotNull(feed2); 367 assertEquals(FetcherTestServlet.DELTA_FEED_TITLE, feed2.getTitle()); 368 assertEquals(3, feed2.getEntries().size()); 369 entry1 = (SyndEntry) feed2.getEntries().get(0); 370 assertEquals(FetcherTestServlet.DELTA_FEED_ENTRY_TITLE, entry1.getTitle()); 371 372 SyndEntry entry2 = (SyndEntry) feed2.getEntries().get(1); 373 assertEquals("atom_0.3.feed.entry[0].title", entry2.getTitle()); 374 375 } catch (Exception e) { 376 e.printStackTrace(); 377 fail(e.getMessage()); 378 } 379 } 380 381 382 } 383 | Popular Tags |