Section 3.3. Writing the Quote Generator


3.3. Writing the Quote Generator

Before you go any further with the multiproject setup, let's take a look at how the core project has implemented the quote generator. This application uses the online quote generator located at http://www.quotationspage.com/; a site which generates an RSS feed of quotes. To parse this feed, you will make use of the Rome framework feed parser (https://rome.dev.java.net/) which has built-in support for parsing RSS and Atom feeds. To get started, add the following code to core/src/main/mdn/qotd/core/QuoteGenerator.java:

package mdn.qotd.core;    import java.net.URL;    import com.sun.syndication.feed.synd.SyndContent; import com.sun.syndication.feed.synd.SyndEntry; import com.sun.syndication.feed.synd.SyndFeed; import com.sun.syndication.io.SyndFeedInput; import com.sun.syndication.io.XmlReader;    public class QuoteGenerator {     private static final String QUOTE_URL =          "http://www.quotationspage.com/data/qotd.rss";          public String generate( )     {         SyndFeed quoteFeed;         try         {             SyndFeedInput input = new SyndFeedInput( );             quoteFeed = input.build(new XmlReader(new URL(QUOTE_URL)));         }         catch (Exception e)         {             throw new RuntimeException("Failed to get RSS Quote Feed ["                  + QUOTE_URL + "]", e);         }            SyndEntry firstQuoteEntry =              (SyndEntry) quoteFeed.getEntries( ).get(0);         SyndContent firstQuoteContent =              (SyndContent) firstQuoteEntry.getContents( ).get(0);                  return firstQuoteContent.getValue( );     }      }

If you are test-infected, you are probably wondering why you didn't write a unit test for QuoteGenerator before writing our parsing logic. To make up for this oversight, write a quick test for the QuoteGenerator class in core/src/test/mdn/qotd/core/QuoteGeneratorTest.java:

package mdn.qotd.core;    import mdn.qotd.core.QuoteGenerator; import junit.framework.TestCase;    public class QuoteGeneratorTest extends TestCase {     public void testGenerate( )     {         assertTrue(new QuoteGenerator( ).generate( ).length( ) > 0);     } }

The quote RSS feed sends a different quote every day, and because of this, it is impossible to perform an assertEquals() on the generated quote string. This test simply tests to see that a quote has been returned from quotationspage.com and that it has been successfully parsed without throwing a RuntimeException.

3.3.1. What just happened?

Introducing Rome is beyond the scope of this Notebook, but you should know that Rome is an innovative and useful utility that makes RSS and Atom feeds easier to manage. You've created a program to parse an RSS feed from quotationspage.com, and you are returning the first quote from this feed. The web subproject is going to call the generate( ) method on QuoteGenerator to generate a quote for display on a web page.



Maven. A Developer's Notebook
Maven: A Developers Notebook (Developers Notebooks)
ISBN: 0596007507
EAN: 2147483647
Year: 2003
Pages: 125

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net