[jboss-cvs] JBossAS SVN: r90871 - in projects/ejb-book/trunk/ch07-rsscache/src: main/java/org/jboss/ejb3/examples/ch07 and 8 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Jul 6 21:16:03 EDT 2009
Author: ALRubinger
Date: 2009-07-06 21:16:03 -0400 (Mon, 06 Jul 2009)
New Revision: 90871
Added:
projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/
projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/
projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/impl/
projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/
projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/FileFeedFetcher.java
projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/ProtectExportUtil.java
projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/RomeRssEntry.java
projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/RssCacheBean.java
projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/spi/
projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/spi/RssCacheCommonBusiness.java
projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/spi/RssEntry.java
projects/ejb-book/trunk/ch07-rsscache/src/test/java/org/jboss/ejb3/examples/ch07/rsscache/
projects/ejb-book/trunk/ch07-rsscache/src/test/java/org/jboss/ejb3/examples/ch07/rsscache/RssCacheTestCaseBase.java
projects/ejb-book/trunk/ch07-rsscache/src/test/java/org/jboss/ejb3/examples/ch07/rsscache/RssCacheUnitTestCase.java
projects/ejb-book/trunk/ch07-rsscache/src/test/java/org/jboss/ejb3/examples/ch07/rsscache/impl/
projects/ejb-book/trunk/ch07-rsscache/src/test/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/
projects/ejb-book/trunk/ch07-rsscache/src/test/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/TestRssCacheBean.java
Log:
[EJBBOOK-9] Add RSS Cache Unit Tests
Added: projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/FileFeedFetcher.java
===================================================================
--- projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/FileFeedFetcher.java (rev 0)
+++ projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/FileFeedFetcher.java 2009-07-07 01:16:03 UTC (rev 90871)
@@ -0,0 +1,74 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.ch07.rsscache.impl.rome;
+
+import java.io.IOException;
+import java.net.URL;
+
+import com.sun.syndication.feed.synd.SyndFeed;
+import com.sun.syndication.fetcher.FetcherException;
+import com.sun.syndication.fetcher.impl.AbstractFeedFetcher;
+import com.sun.syndication.io.FeedException;
+
+/**
+ * FileFeedFetcher
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class FileFeedFetcher extends AbstractFeedFetcher
+{
+
+ /* (non-Javadoc)
+ * @see com.sun.syndication.fetcher.FeedFetcher#retrieveFeed(java.net.URL)
+ */
+ @Override
+ public SyndFeed retrieveFeed(URL arg0) throws IllegalArgumentException, IOException, FeedException, FetcherException
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ //-------------------------------------------------------------------------------------||
+ // Required Implementations -----------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ //-------------------------------------------------------------------------------------||
+ // Functional Methods -----------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ //-------------------------------------------------------------------------------------||
+ // Internal Helper Methods ------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+}
Added: projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/ProtectExportUtil.java
===================================================================
--- projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/ProtectExportUtil.java (rev 0)
+++ projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/ProtectExportUtil.java 2009-07-07 01:16:03 UTC (rev 90871)
@@ -0,0 +1,77 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.ch07.rsscache.impl.rome;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+/**
+ * ProtectExportUtil
+ *
+ * Package-private utilities to protect against mutable
+ * state getting exported
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+class ProtectExportUtil
+{
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Internal constructor; protects against instanciation
+ */
+ private ProtectExportUtil()
+ {
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Functional Methods -----------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Returns a copy of the specified URL; used to ensure that mutable
+ * internal state is not leaked out to clients
+ * @param url
+ * @return
+ */
+ static URL copyUrl(final URL url)
+ {
+ // If null, return
+ if (url == null)
+ {
+ return url;
+ }
+
+ try
+ {
+ // Copy
+ return new URL(url.toExternalForm());
+ }
+ catch (final MalformedURLException e)
+ {
+ throw new RuntimeException("Error in copying URL", e);
+ }
+ }
+}
Added: projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/RomeRssEntry.java
===================================================================
--- projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/RomeRssEntry.java (rev 0)
+++ projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/RomeRssEntry.java 2009-07-07 01:16:03 UTC (rev 90871)
@@ -0,0 +1,153 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.ch07.rsscache.impl.rome;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import org.jboss.ejb3.examples.ch07.rsscache.spi.RssEntry;
+
+import com.sun.syndication.feed.synd.SyndContent;
+import com.sun.syndication.feed.synd.SyndEntry;
+
+/**
+ * RomeRssEntry
+ *
+ * The java.net Rome implementation of an RSS Entry
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class RomeRssEntry implements RssEntry
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * The author of the entry
+ */
+ private String author;
+
+ /**
+ * The short description of the entry
+ */
+ private String description;
+
+ /**
+ * The title of the entry
+ */
+ private String title;
+
+ /**
+ * The link to the entry
+ */
+ private URL url;
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Constructor
+ *
+ * @param entry The Rome API's RSS Entry representation
+ * @throws IllegalArgumentException If the entry is not specified
+ */
+ RomeRssEntry(final SyndEntry entry) throws IllegalArgumentException
+ {
+ // Set properties
+ this.author = entry.getAuthor();
+ final SyndContent content = entry.getDescription();
+ this.description = content.getValue();
+ this.title = entry.getTitle();
+ final String urlString = entry.getLink();
+ URL url = null;
+ try
+ {
+ url = new URL(urlString);
+ }
+ catch (final MalformedURLException murle)
+ {
+ throw new RuntimeException("Obtained invalid URL from Rome RSS entry: " + entry, murle);
+ }
+ this.url = url;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Required Implementations -----------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /* (non-Javadoc)
+ * @see org.jboss.ejb3.examples.ch07.rsscache.spi.RssEntry#getAuthor()
+ */
+ @Override
+ public String getAuthor()
+ {
+ return this.author;
+ }
+
+ /* (non-Javadoc)
+ * @see org.jboss.ejb3.examples.ch07.rsscache.spi.RssEntry#getDescription()
+ */
+ @Override
+ public String getDescription()
+ {
+ return this.description;
+ }
+
+ /* (non-Javadoc)
+ * @see org.jboss.ejb3.examples.ch07.rsscache.spi.RssEntry#getTitle()
+ */
+ @Override
+ public String getTitle()
+ {
+ return this.title;
+ }
+
+ /* (non-Javadoc)
+ * @see org.jboss.ejb3.examples.ch07.rsscache.spi.RssEntry#getUrl()
+ */
+ @Override
+ public URL getUrl()
+ {
+ return ProtectExportUtil.copyUrl(this.url);
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Overridden Implementations ---------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString()
+ {
+ final StringBuilder sb = new StringBuilder();
+ sb.append(this.getTitle());
+ sb.append(" - ");
+ sb.append(this.url.toExternalForm());
+ return sb.toString();
+ }
+}
Added: projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/RssCacheBean.java
===================================================================
--- projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/RssCacheBean.java (rev 0)
+++ projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/RssCacheBean.java 2009-07-07 01:16:03 UTC (rev 90871)
@@ -0,0 +1,174 @@
+package org.jboss.ejb3.examples.ch07.rsscache.impl.rome;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.Resource;
+import javax.ejb.Lock;
+import javax.ejb.LockType;
+import javax.ejb.Remote;
+import javax.ejb.SessionContext;
+import javax.ejb.Singleton;
+import javax.ejb.Startup;
+
+import org.jboss.ejb3.examples.ch07.rsscache.spi.RssCacheCommonBusiness;
+import org.jboss.ejb3.examples.ch07.rsscache.spi.RssEntry;
+import org.jboss.logging.Logger;
+
+import com.sun.syndication.feed.synd.SyndEntry;
+import com.sun.syndication.feed.synd.SyndFeed;
+import com.sun.syndication.fetcher.FeedFetcher;
+import com.sun.syndication.fetcher.FetcherException;
+import com.sun.syndication.fetcher.impl.HttpClientFeedFetcher;
+import com.sun.syndication.io.FeedException;
+
+/**
+ * RssCacheBean
+ *
+ * Singleton EJB, to be eagerly instanciated upon application deployment,
+ * exposing a cached view of an RSS Feed
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at Singleton
+ at Startup
+ at Remote(RssCacheCommonBusiness.class)
+public class RssCacheBean implements RssCacheCommonBusiness
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Logger
+ */
+ private static final Logger log = Logger.getLogger(RssCacheBean.class);
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * URL pointing to the RSS Feed
+ */
+ private URL url;
+
+ /**
+ * Cached RSS Entries for the feed
+ */
+ private List<RssEntry> entries;
+
+ /**
+ * The EJB SessionContext
+ */
+ @Resource
+ private SessionContext context;
+
+ //-------------------------------------------------------------------------------------||
+ // Required Implementations -----------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+ /* (non-Javadoc)
+ * @see org.jboss.ejb3.examples.ch07.rsscache.spi.RssCacheCommonBusiness#getEntries()
+ */
+ @Override
+ // @Lock(LockType.READ) // Optional metadata, READ is the default
+ public List<RssEntry> getEntries()
+ {
+ return entries;
+ }
+
+ /* (non-Javadoc)
+ * @see org.jboss.ejb3.examples.ch07.rsscache.spi.RssCacheCommonBusiness#getUrl()
+ */
+ // @Lock(LockType.READ) // Optional metadata, READ is the default
+ @Override
+ public URL getUrl()
+ {
+ // Return a copy so we don't export mutable state to the client
+ return ProtectExportUtil.copyUrl(this.url);
+ }
+
+ /**
+ * @see org.jboss.ejb3.examples.ch07.rsscache.spi.RssCacheCommonBusiness#refresh()
+ * @throws IllegalStateException If the URL has not been set
+ */
+ @PostConstruct
+ @Override
+ // Lock all readers and writers until we're done here
+ @Lock(LockType.WRITE)
+ public void refresh() throws IllegalStateException
+ {
+
+ // Obtain the URL
+ final URL url = this.url;
+ if (url == null)
+ {
+ throw new IllegalStateException("The Feed URL has not been set");
+ }
+ log.info("Requested: " + url);
+
+ // Obtain the feed
+ final FeedFetcher feedFetcher = new HttpClientFeedFetcher();
+ SyndFeed feed = null;
+ try
+ {
+ feed = feedFetcher.retrieveFeed(url);
+ }
+ catch (final FeedException fe)
+ {
+ throw new RuntimeException(fe);
+ }
+ catch (final FetcherException fe)
+ {
+ throw new RuntimeException(fe);
+ }
+ catch (final IOException ioe)
+ {
+ throw new RuntimeException(ioe);
+ }
+
+ // Make a new list for the entries
+ final List<RssEntry> rssEntries = new ArrayList<RssEntry>();
+
+ // For each entry
+ @SuppressWarnings("unchecked")
+ // The Rome API doesn't provide for generics, so suppress the warning
+ final List<SyndEntry> list = (List<SyndEntry>) feed.getEntries();
+ for (final SyndEntry entry : list)
+ {
+ // Make a new entry
+ final RssEntry rssEntry = new RomeRssEntry(entry);
+
+ // Place in the list
+ rssEntries.add(rssEntry);
+ log.debug("Found new RSS Entry: " + rssEntry);
+ }
+
+ // Set the entries
+ this.entries = rssEntries;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Internal Helper Methods ------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Sets the URL pointing to the feed
+ *
+ * @param url
+ * @throws IllegalArgumentException If the URL is null
+ */
+ void setUrl(final URL url) throws IllegalArgumentException
+ {
+ // Set the URL
+ this.url = url;
+
+ // Refresh
+ this.refresh();
+ }
+}
Added: projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/spi/RssCacheCommonBusiness.java
===================================================================
--- projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/spi/RssCacheCommonBusiness.java (rev 0)
+++ projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/spi/RssCacheCommonBusiness.java 2009-07-07 01:16:03 UTC (rev 90871)
@@ -0,0 +1,59 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.ch07.rsscache.spi;
+
+import java.net.URL;
+import java.util.List;
+
+/**
+ * RssCacheCommonBusiness
+ *
+ * Common business interface for beans exposing a cached view of an RSS
+ * Feed (ie. the EJB Container)
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public interface RssCacheCommonBusiness
+{
+ // ---------------------------------------------------------------------------||
+ // Contracts -----------------------------------------------------------------||
+ // ---------------------------------------------------------------------------||
+
+ /**
+ * Returns all entries in the RSS Feed represented by {@link RssCacheCommonBusiness#getUrl()}
+ */
+ List<RssEntry> getEntries();
+
+ /**
+ * Returns the URL of the RSS Feed
+ *
+ * @return
+ */
+ URL getUrl();
+
+ /**
+ * Flushes the cache and refreshes the entries from the feed
+ */
+ void refresh();
+
+}
Added: projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/spi/RssEntry.java
===================================================================
--- projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/spi/RssEntry.java (rev 0)
+++ projects/ejb-book/trunk/ch07-rsscache/src/main/java/org/jboss/ejb3/examples/ch07/rsscache/spi/RssEntry.java 2009-07-07 01:16:03 UTC (rev 90871)
@@ -0,0 +1,67 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.ch07.rsscache.spi;
+
+import java.net.URL;
+
+/**
+ * RssEntry
+ *
+ * Defines the contract for a single RSS Entry
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public interface RssEntry
+{
+ // ---------------------------------------------------------------------------||
+ // Contracts -----------------------------------------------------------------||
+ // ---------------------------------------------------------------------------||
+
+ /**
+ * Obtains the author of the entry
+ *
+ * @return
+ */
+ String getAuthor();
+
+ /**
+ * Obtains the title of the entry
+ *
+ * @return
+ */
+ String getTitle();
+
+ /**
+ * Obtains the URL linking to the entry
+ *
+ * @return
+ */
+ URL getUrl();
+
+ /**
+ * Obtains the short description of the entry
+ *
+ * @return
+ */
+ String getDescription();
+}
Added: projects/ejb-book/trunk/ch07-rsscache/src/test/java/org/jboss/ejb3/examples/ch07/rsscache/RssCacheTestCaseBase.java
===================================================================
--- projects/ejb-book/trunk/ch07-rsscache/src/test/java/org/jboss/ejb3/examples/ch07/rsscache/RssCacheTestCaseBase.java (rev 0)
+++ projects/ejb-book/trunk/ch07-rsscache/src/test/java/org/jboss/ejb3/examples/ch07/rsscache/RssCacheTestCaseBase.java 2009-07-07 01:16:03 UTC (rev 90871)
@@ -0,0 +1,397 @@
+package org.jboss.ejb3.examples.ch07.rsscache;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.List;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import junit.framework.Assert;
+
+import org.jboss.ejb3.examples.ch07.rsscache.spi.RssCacheCommonBusiness;
+import org.jboss.ejb3.examples.ch07.rsscache.spi.RssEntry;
+import org.jboss.logging.Logger;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.mortbay.jetty.Handler;
+import org.mortbay.jetty.Server;
+import org.mortbay.jetty.handler.AbstractHandler;
+
+/**
+ * RssCacheTestCaseBase
+ *
+ * Base tests for the RssCache @Singleton
+ * test classes, may be extended either from unit or
+ * integration tests.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public abstract class RssCacheTestCaseBase
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ private static final Logger log = Logger.getLogger(RssCacheTestCaseBase.class);
+
+ /**
+ * The number of expected RSS entries from the default RSS Feed
+ */
+ private static final int EXPECTED_15_RSS_ENTRIES = 15;
+
+ /**
+ * The number of expected RSS entries from the RSS Feed with 5 entries
+ */
+ private static final int EXPECTED_5_RSS_ENTRIES = 5;
+
+ /**
+ * Filename containing a mock RSS feed for use in testing
+ */
+ static final String FILENAME_RSS_MOCK_FEED_15_ENTRIES = "15_entries.rss";
+
+ /**
+ * Filename containing a mock RSS feed for use in testing
+ */
+ static final String FILENAME_RSS_MOCK_FEED_5_ENTRIES = "5_entries.rss";
+
+ /**
+ * Filename of the target RSS feed to be requested of the HTTP server
+ */
+ static final String FILENAME_RSS_FEED = "feed.rss";
+
+ /**
+ * Port to which the test HTTP Server should bind
+ */
+ static final int HTTP_TEST_BIND_PORT = 12345;
+
+ /**
+ * Content type of an RSS feed
+ */
+ private static final String CONTENT_TYPE_RSS = "text/rss";
+
+ /**
+ * The HTTP Server used to serve out the mock RSS file
+ */
+ static Server httpServer;
+
+ /**
+ * Newline character
+ */
+ private static final char NEWLINE = '\n';
+
+ //-------------------------------------------------------------------------------------||
+ // Lifecycle --------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Starts up an embedded HTTP Server to serve out the Mock
+ * RSS file (Rome FeedFetcher doesn't support obtaining from
+ * file:/ URLs)
+ */
+ @BeforeClass
+ public static void startHttpServer()
+ {
+ // Start an Embedded HTTP Server
+ final Handler handler = new StaticFileHandler();
+ final Server httpServer = new Server(HTTP_TEST_BIND_PORT);
+ httpServer.setHandler(handler);
+ try
+ {
+ httpServer.start();
+ }
+ catch (final Exception e)
+ {
+ throw new RuntimeException("Could not start server");
+ }
+ log.info("HTTP Server Started: " + httpServer);
+ RssCacheUnitTestCase.httpServer = httpServer;
+ }
+
+ /**
+ * Creates the RSS feed file from the default mock template
+ */
+ @BeforeClass
+ public static void createRssFeedFile() throws Exception
+ {
+ writeToRssFeedFile(getMock15EntriesRssFile());
+ }
+
+ /**
+ * Shuts down and clears the Embedded HTTP Server
+ */
+ @AfterClass
+ public static void shutdownHttpServer()
+ {
+ if (httpServer != null)
+ {
+ try
+ {
+ httpServer.stop();
+ }
+ catch (final Exception e)
+ {
+ // Swallow
+ log.error("Could not stop HTTP Server cleanly", e);
+ }
+ log.info("HTTP Server Stopped: " + httpServer);
+ httpServer = null;
+ }
+ }
+
+ /**
+ * Removes the RSS feed file
+ */
+ @AfterClass
+ public static void deleteRssFeedFile() throws Exception
+ {
+ final File rssFile = getRssFeedFile();
+ boolean deleted = rssFile.delete();
+ if (!deleted)
+ {
+ log.warn("RSS Feed File was not cleaned up properly: " + rssFile);
+ }
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Tests ------------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Ensures the RSS Entries have been initialized and parsed
+ * out as expected. Additionally tests that {@link RssCacheCommonBusiness#refresh()}
+ * clears the cache and works as expected
+ */
+ @Test
+ public void testRssEntries() throws Exception
+ {
+ // Log
+ log.info("testRssEntries");
+
+ // Get the RSS Cache Bean
+ final RssCacheCommonBusiness rssCache = this.getRssCacheBean();
+
+ // Get all entries
+ final List<RssEntry> rssEntries = rssCache.getEntries();
+ log.info("Got entries: " + rssEntries);
+
+ // Ensure they've been specified/initialized, and parsed out in proper size
+ this.ensureExpectedEntries(rssEntries, EXPECTED_15_RSS_ENTRIES);
+
+ // Swap out the contents of the RSS Feed File, so a refresh will pull in the new contents
+ writeToRssFeedFile(getMock5EntriesRssFile());
+
+ // Refresh
+ rssCache.refresh();
+
+ // Get all entries
+ final List<RssEntry> rssEntriesAfterRefresh = rssCache.getEntries();
+ log.info("Got entries after refresh: " + rssEntriesAfterRefresh);
+
+ // Ensure they've been specified/initialized, and parsed out in proper size
+ this.ensureExpectedEntries(rssEntriesAfterRefresh, EXPECTED_5_RSS_ENTRIES);
+
+ // Now put back the original 15 mock entries
+ writeToRssFeedFile(getMock15EntriesRssFile());
+ rssCache.refresh();
+
+ // And ensure we're back to normal
+ final List<RssEntry> rssEntriesAfterRestored = rssCache.getEntries();
+ log.info("Got entries: " + rssEntriesAfterRestored);
+ this.ensureExpectedEntries(rssEntriesAfterRestored, EXPECTED_15_RSS_ENTRIES);
+
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Contracts --------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Obtains the RssCache bean to be used for this test
+ */
+ protected abstract RssCacheCommonBusiness getRssCacheBean();
+
+ //-------------------------------------------------------------------------------------||
+ // Internal Helper Methods ------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Ensures that the RSS entries are parsed out and have the specified number of elements
+ * @param entries
+ * @param expectedSize
+ */
+ private void ensureExpectedEntries(final List<RssEntry> entries, final int expectedSize)
+ {
+ // Ensure they've been specified/initialized, and parsed out in proper size
+ Assert.assertNotNull("RSS Entries was either not initialized or is returning null", entries);
+ final int actualSize = entries.size();
+ Assert.assertEquals("Wrong number of RSS entries parsed out from feed", expectedSize, actualSize);
+ log.info("Got expected " + expectedSize + " RSS entries");
+ }
+
+ /**
+ * Obtains the base of the code source
+ */
+ private static URL getCodebaseLocation()
+ {
+ return RssCacheUnitTestCase.class.getProtectionDomain().getCodeSource().getLocation();
+ }
+
+ /**
+ * Writes the contents of the template file to the RSS Feed File
+ *
+ * @param templateFile
+ * @throws Exception
+ */
+ private static void writeToRssFeedFile(final File templateFile) throws Exception
+ {
+ // Get a writer to the target file
+ final File rssFile = getRssFeedFile();
+ final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(rssFile)));
+
+ // Get a reader to the default mock template file
+ final BufferedReader reader = new BufferedReader(new FileReader(templateFile));
+
+ // Read 'n Write
+ String line = null;
+ while ((line = reader.readLine()) != null)
+ {
+ writer.write(line);
+ writer.write(NEWLINE);
+ }
+
+ // Flush and close
+ writer.flush();
+ writer.close();
+ reader.close();
+ }
+
+ /**
+ * Obtains the RSS Feed file served by the server
+ * @return
+ * @throws Exception
+ */
+ private static File getRssFeedFile() throws Exception
+ {
+ final File baseFile = getBaseDirectory();
+ final File rssFile = new File(baseFile, FILENAME_RSS_FEED);
+ return rssFile;
+ }
+
+ /**
+ * Obtains the Mock RSS Template file with 15 entries
+ * @return
+ * @throws Exception
+ */
+ private static File getMock15EntriesRssFile() throws Exception
+ {
+ return getFileFromBase(FILENAME_RSS_MOCK_FEED_15_ENTRIES);
+ }
+
+ /**
+ * Obtains the Mock RSS Template file with 5 entries
+ * @return
+ * @throws Exception
+ */
+ private static File getMock5EntriesRssFile() throws Exception
+ {
+ return getFileFromBase(FILENAME_RSS_MOCK_FEED_5_ENTRIES);
+ }
+
+ /**
+ * Obtains the file with the specified name from the base directory
+ *
+ * @param filename
+ * @return
+ * @throws Exception
+ */
+ private static File getFileFromBase(final String filename) throws Exception
+ {
+ final File baseFile = getBaseDirectory();
+ final File mockTemplateFile = new File(baseFile, filename);
+ return mockTemplateFile;
+ }
+
+ /**
+ * Obtains the base directory in which test files are located
+ * @return
+ */
+ private static File getBaseDirectory() throws Exception
+ {
+ final URL baseLocation = getCodebaseLocation();
+ final URI baseUri = baseLocation.toURI();
+ final File baseFile = new File(baseUri);
+ return baseFile;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Inner Classes ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Jetty Handler to serve a static character file from the web root
+ */
+ private static class StaticFileHandler extends AbstractHandler implements Handler
+ {
+ /*
+ * (non-Javadoc)
+ * @see org.mortbay.jetty.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
+ */
+ public void handle(final String target, final HttpServletRequest request, final HttpServletResponse response,
+ final int dispatch) throws IOException, ServletException
+ {
+ // Set content type and status before we write anything to the stream
+ response.setContentType(CONTENT_TYPE_RSS);
+ response.setStatus(HttpServletResponse.SC_OK);
+
+ // Obtain the requested file relative to the webroot
+ final URL root = getCodebaseLocation();
+ final URL fileUrl = new URL(root.toExternalForm() + target);
+ URI uri = null;
+ try
+ {
+ uri = fileUrl.toURI();
+ }
+ catch (final URISyntaxException urise)
+ {
+ throw new RuntimeException(urise);
+ }
+ final File file = new File(uri);
+
+ // File not found, so 404
+ if (!file.exists())
+ {
+ response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+ log.warn("Requested file is not found: " + file);
+ return;
+ }
+
+ // Write out each line
+ final BufferedReader reader = new BufferedReader(new FileReader(file));
+ final PrintWriter writer = response.getWriter();
+ String line = null;
+ while ((line = reader.readLine()) != null)
+ {
+ writer.println(line);
+ }
+
+ // Close 'er up
+ writer.flush();
+ reader.close();
+ writer.close();
+ }
+ }
+
+}
Added: projects/ejb-book/trunk/ch07-rsscache/src/test/java/org/jboss/ejb3/examples/ch07/rsscache/RssCacheUnitTestCase.java
===================================================================
--- projects/ejb-book/trunk/ch07-rsscache/src/test/java/org/jboss/ejb3/examples/ch07/rsscache/RssCacheUnitTestCase.java (rev 0)
+++ projects/ejb-book/trunk/ch07-rsscache/src/test/java/org/jboss/ejb3/examples/ch07/rsscache/RssCacheUnitTestCase.java 2009-07-07 01:16:03 UTC (rev 90871)
@@ -0,0 +1,133 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.ch07.rsscache;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import org.jboss.ejb3.examples.ch07.rsscache.impl.rome.TestRssCacheBean;
+import org.jboss.ejb3.examples.ch07.rsscache.spi.RssCacheCommonBusiness;
+import org.jboss.logging.Logger;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+
+/**
+ * RssCacheUnitTestCase
+ *
+ * Unit Tests for the RssCache classes,
+ * used as a POJO outside the context of the EJB container
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class RssCacheUnitTestCase extends RssCacheTestCaseBase
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Logger
+ */
+ private static final Logger log = Logger.getLogger(RssCacheUnitTestCase.class);
+
+ /**
+ * The bean (POJO) instance to test, mocking a @Singleton EJB
+ */
+ private static RssCacheCommonBusiness bean;
+
+ //-------------------------------------------------------------------------------------||
+ // Lifecycle --------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Creates a POJO instance to mock the real Container EJB @Singleton
+ * before any tests are run
+ */
+ @BeforeClass
+ public static void createPojo()
+ {
+ // Instanciate and set
+ final TestRssCacheBean bean = new TestRssCacheBean();
+ RssCacheUnitTestCase.bean = bean;
+ log.info("Created POJO instance: " + bean);
+
+ // Set the URL of the Mock RSS File
+ URL url = null;
+ try
+ {
+ url = new URL(getBaseConnectUrl(), FILENAME_RSS_FEED);
+ }
+ catch (final MalformedURLException murle)
+ {
+ throw new RuntimeException("Error in test setup while constructing the mock RSS feed URL", murle);
+ }
+ bean.setUrl(url);
+
+ // Mock container initialization upon the bean
+ bean.refresh();
+ }
+
+ /**
+ * Resets the POJO instance to null after all tests are run
+ */
+ @AfterClass
+ public static void clearPojo()
+ {
+ // Set to null so we don't ever leak instances between test runs
+ bean = null;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Required Implementations -----------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /* (non-Javadoc)
+ * @see org.jboss.ejb3.examples.ch07.envinfo.EnvironmentInformationTestCaseBase#getEnvInfoBean()
+ */
+ @Override
+ protected RssCacheCommonBusiness getRssCacheBean()
+ {
+ return bean;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Internal Helper Methods ------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Obtains the base of the code source
+ */
+ private static URL getBaseConnectUrl()
+ {
+ try
+ {
+ return new URL("http://localhost:" + HTTP_TEST_BIND_PORT);
+ }
+ catch (final MalformedURLException e)
+ {
+ throw new RuntimeException("Error in creating the base URL during set setup", e);
+ }
+ }
+
+}
Added: projects/ejb-book/trunk/ch07-rsscache/src/test/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/TestRssCacheBean.java
===================================================================
--- projects/ejb-book/trunk/ch07-rsscache/src/test/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/TestRssCacheBean.java (rev 0)
+++ projects/ejb-book/trunk/ch07-rsscache/src/test/java/org/jboss/ejb3/examples/ch07/rsscache/impl/rome/TestRssCacheBean.java 2009-07-07 01:16:03 UTC (rev 90871)
@@ -0,0 +1,50 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.ch07.rsscache.impl.rome;
+
+import java.net.URL;
+
+/**
+ * TestRssCacheBean
+ *
+ * Extension of the RSS Cache Bean which exposes support to
+ * set the Feed URL for testing
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class TestRssCacheBean extends RssCacheBean
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Overridden Implementations ---------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /* (non-Javadoc)
+ * @see org.jboss.ejb3.examples.ch07.rsscache.impl.rome.RssCacheBean#setUrl(java.net.URL)
+ */
+ @Override
+ public void setUrl(final URL url) throws IllegalArgumentException
+ {
+ super.setUrl(url);
+ }
+}
More information about the jboss-cvs-commits
mailing list